:root {
  --bg: #f6f4ef;
  --panel: #ffffff;
  --ink: #1b1b1b;
  --muted: #6b6b6b;
  --line: #ddd8cc;
  --block: #161616;
  --accent: #1d4e89;
  --active: #ffd84d;
  --word: #cfe3fb;
  --cross: #eef4fc;
  --solved-a: #eceff3;
  --solved-b: #dde3ea;
  --private: #1d63c4;
  --serif: Georgia, 'Times New Roman', serif;
  --sans: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 var(--sans);
}
a { color: var(--accent); }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; }
button, input, textarea, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  cursor: pointer;
  text-decoration: none;
  color: var(--ink);
}
.btn:hover { border-color: #b9b2a2; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #163d6c; }
.btn.small { padding: 3px 9px; font-size: 13px; border-radius: 6px; }
.btn:disabled { opacity: .5; cursor: default; }
.link-btn { border: 0; background: none; padding: 0; color: var(--accent); cursor: pointer; text-decoration: underline; font-size: inherit; }

input[type=text], input[type=number], textarea {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  background: #fff;
  width: 100%;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* --- Landing & create pages --------------------------------------------- */

.page { max-width: 960px; margin: 0 auto; padding: 32px 16px 64px; }
.page h1 { font-size: 34px; margin: 0 0 4px; }
.lede { color: var(--muted); margin: 0 0 28px; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 16px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 20px; }
.card h2 { margin: 0 0 8px; font-size: 20px; }
.card p { color: var(--muted); margin: 0 0 14px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.code-input { text-transform: uppercase; letter-spacing: 3px; font-family: ui-monospace, monospace; max-width: 180px; }
.recent { list-style: none; padding: 0; margin: 0; }
.recent li { display: flex; justify-content: space-between; gap: 8px; padding: 8px 0; border-top: 1px solid var(--line); }
.recent li:first-child { border-top: 0; }
.recent .code { font-family: ui-monospace, monospace; color: var(--muted); }
.error { color: #b3261e; }
.warn { color: #8a5a00; }
.ok { color: #22703a; }

.tabs { display: flex; gap: 4px; margin: 8px 0 16px; }
.tab { border: 1px solid var(--line); background: transparent; padding: 8px 16px; border-radius: 8px; cursor: pointer; }
.tab[aria-selected=true] { background: var(--panel); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.step { margin: 0 0 24px; }
.step h3 { margin: 0 0 6px; font-size: 18px; }
.hint { color: var(--muted); font-size: 13px; margin: 4px 0 10px; }
.builder { display: grid; grid-template-columns: minmax(260px, 460px) 1fr; gap: 24px; align-items: start; }
.clue-inputs { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.clue-inputs textarea { min-height: 300px; font-size: 13px; resize: vertical; }
.report { font-size: 14px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 10px 14px; }
.report p { margin: 2px 0; }
details summary { cursor: pointer; color: var(--accent); }
.dims input[type=number] { width: 70px; }

@media (max-width: 760px) {
  .builder, .clue-inputs { grid-template-columns: 1fr; }
}

/* --- Grid ---------------------------------------------------------------- */

.grid {
  --w: 15;
  display: grid;
  grid-template-columns: repeat(var(--w), 1fr);
  gap: 1px;
  background: var(--block);
  border: 2px solid var(--block);
  width: 100%;
  container-type: inline-size;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
.cell {
  position: relative;
  aspect-ratio: 1;
  background: #fff;
  cursor: pointer;
  overflow: hidden;
}
.cell.block { background: var(--block); }
.cell .num {
  position: absolute;
  top: 1px;
  left: 2px;
  font-size: calc(100cqw / var(--w) * 0.26);
  line-height: 1;
  color: #333;
  pointer-events: none;
  z-index: 2;
}
.cell .letter {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding-top: 8%;
  font-family: var(--sans);
  font-weight: 500;
  font-size: calc(100cqw / var(--w) * 0.58);
  text-transform: uppercase;
  pointer-events: none;
  z-index: 2;
}
.cell.cross { background: var(--cross); }
.cell.word { background: var(--word); }
.cell.solved-hidden { background: repeating-linear-gradient(135deg, var(--solved-a) 0 4px, var(--solved-b) 4px 8px); }
.cell.solved-hidden.word { background: repeating-linear-gradient(135deg, #d6e8f6 0 4px, #bcd9ee 4px 8px); }
.cell.active { background: var(--active) !important; }
.cell.private .letter { color: var(--private); font-style: italic; }
.cell.peer::after {
  content: '';
  position: absolute;
  inset: 0;
  border: 3px solid var(--peer);
  z-index: 1;
  pointer-events: none;
}
.editable .cell:not(.block):hover { background: #eee; }

/* --- Play page ------------------------------------------------------------ */

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.brand { font-family: var(--serif); font-weight: 700; text-decoration: none; color: var(--ink); white-space: nowrap; }
.topbar h1 { font-size: 18px; margin: 0; flex: 1 1 200px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.share { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.share .code { font-family: ui-monospace, monospace; font-size: 15px; letter-spacing: 2px; font-weight: 700; color: var(--ink); }
.people { display: flex; gap: 6px; flex-wrap: wrap; }
.person { display: inline-flex; align-items: center; gap: 5px; font-size: 13px; padding: 2px 8px; border-radius: 999px; background: var(--bg); border: 1px solid var(--line); }
.person .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--c); }
.person.me { cursor: pointer; }
.toggle { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); cursor: pointer; }

.play {
  display: grid;
  grid-template-columns: minmax(280px, 600px) minmax(300px, 1fr) minmax(240px, 300px);
  gap: 20px;
  padding: 16px;
  align-items: start;
  max-width: 1600px;
  margin: 0 auto;
}
.grid-col { position: sticky; top: 16px; }
.grid-wrap { position: relative; }
#kbd {
  position: absolute;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  border: 0;
  padding: 0;
  font-size: 16px; /* stops iOS zooming on focus */
}
.clue-bar {
  min-height: 56px;
  background: var(--word);
  border-radius: 8px;
  padding: 8px 12px;
  margin-bottom: 10px;
  font-size: 16px;
}
.clue-bar .lbl { font-weight: 700; margin-right: 8px; }
.clue-bar .sub { font-size: 13px; color: #2c4a6b; margin-top: 4px; }
.tools { display: flex; flex-wrap: wrap; gap: 6px 16px; margin-bottom: 10px; }
.tool-group { display: flex; align-items: center; gap: 4px; }
.tool-label { font-size: 12px; color: var(--muted); margin-right: 2px; text-transform: uppercase; letter-spacing: .5px; }
.tools .btn:disabled { cursor: not-allowed; }
.cell.wrong .letter { color: #c62828; }
.cell.wrong::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(to top right, transparent calc(50% - 1px), #c62828 calc(50% - 1px), #c62828 calc(50% + 1px), transparent calc(50% + 1px));
  opacity: .55;
  z-index: 1;
  pointer-events: none;
}
.cell.revealed .letter::after {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  border-style: solid;
  border-width: 0 calc(100cqw / var(--w) * 0.28) calc(100cqw / var(--w) * 0.28) 0;
  border-color: transparent #c62828 transparent transparent;
}
.legend { font-size: 12px; color: var(--muted); margin-top: 10px; display: flex; gap: 14px; flex-wrap: wrap; }
.legend .sw { display: inline-block; width: 12px; height: 12px; vertical-align: -2px; margin-right: 4px; border: 1px solid #bbb; }

.clue-lists { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.clue-lists section { min-width: 0; }
.clue-lists h2 { margin: 0 0 6px; font-size: 17px; border-bottom: 2px solid var(--ink); padding-bottom: 4px; }
.clue-lists ol {
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: calc(100vh - 140px);
  overflow-y: auto;
}
.clue {
  display: grid;
  grid-template-columns: 2.4em 1fr;
  gap: 2px 6px;
  padding: 6px 6px;
  border-radius: 6px;
  cursor: pointer;
  border-left: 3px solid transparent;
}
.clue:hover { background: #f0ede5; }
.clue .n { font-weight: 700; text-align: right; }
.clue.cross { background: var(--cross); }
.clue.active { background: var(--word); }
.clue.solved .t { color: var(--muted); }
.clue.solved { border-left-color: #9fb4cc; }
.clue .st { grid-column: 2; font-size: 12px; color: #4f6782; display: flex; gap: 8px; align-items: center; }

.chat-col {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  height: calc(100vh - 100px);
  position: sticky;
  top: 16px;
}
.chat-col h2 { margin: 0; padding: 10px 14px; font-size: 17px; border-bottom: 1px solid var(--line); }
.messages { flex: 1; overflow-y: auto; padding: 10px 14px; display: flex; flex-direction: column; gap: 8px; }
.msg { font-size: 14px; overflow-wrap: anywhere; }
.msg .who { font-weight: 700; color: var(--c); margin-right: 6px; }
.msg .time { font-size: 11px; color: var(--muted); margin-left: 4px; }
.messages .empty { color: var(--muted); font-size: 13px; }
.chat-form { display: flex; gap: 6px; padding: 10px; border-top: 1px solid var(--line); }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: #222;
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
  z-index: 50;
  max-width: calc(100vw - 32px);
}
.toast.show { opacity: .95; }

.modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: flex; align-items: center; justify-content: center; z-index: 40; padding: 16px; }
.modal { background: var(--panel); border-radius: 12px; padding: 22px; width: 100%; max-width: 380px; }
.modal h2 { margin: 0 0 6px; }
.modal p { margin: 0 0 14px; color: var(--muted); }
.fatal { max-width: 520px; margin: 80px auto; text-align: center; padding: 0 16px; }

@media (max-width: 1180px) {
  .play { grid-template-columns: minmax(280px, 560px) 1fr; }
  .chat-col { grid-column: 1 / -1; height: 360px; position: static; }
}
/* Phones: an app-style screen that doesn't scroll as a whole. Grid on top,
   then a tabbed panel (Across / Down / Chat) that scrolls on its own. */
.mobile-tabs, .short { display: none; }
@media (max-width: 760px) {
  body.play-page { height: 100vh; height: 100dvh; overflow: hidden; display: flex; flex-direction: column; }
  .play-page .topbar { flex: none; gap: 6px 10px; padding: 8px 10px; }
  .play-page .brand, .play-page .code-label, .play-page .long { display: none; }
  .play-page .short { display: inline; }
  .play-page .topbar h1 { font-size: 16px; }
  /* Row 1: title + code. Row 2: people (scrolls sideways) + "Show all". */
  .play-page .topbar h1 { flex: 1 1 0; }
  .play-page .people { order: 5; flex: 1 0 calc(100% - 110px); min-width: 0; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .play-page .person { flex: none; }
  .play-page .toggle { order: 6; flex: none; white-space: nowrap; }
  .play-page #app:not([hidden]) {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
    padding: 8px 10px 0;
  }
  .grid-col { position: static; flex: none; }
  .grid-wrap { width: min(100%, max(220px, 100dvh - 430px)); margin: 0 auto; }
  .clue-bar { min-height: 0; margin-bottom: 6px; padding: 6px 10px; font-size: 15px; }
  .tools { margin-bottom: 6px; gap: 4px 10px; }
  .legend { display: none; }

  .mobile-tabs { display: flex; flex: none; border-bottom: 1px solid var(--line); }
  .mobile-tabs button {
    flex: 1;
    border: 0;
    border-bottom: 2px solid transparent;
    background: none;
    padding: 8px 4px;
    font-weight: 600;
    color: var(--muted);
    cursor: pointer;
  }
  .mobile-tabs button[aria-selected=true] { color: var(--accent); border-bottom-color: var(--accent); }
  .badge { display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 9px; background: #c62828; color: #fff; font-size: 11px; line-height: 18px; }

  .clue-lists, .chat-col { flex: 1; min-height: 0; }
  .clue-lists { display: block; }
  .clue-lists > section { height: 100%; display: flex; flex-direction: column; }
  .clue-lists h2 { display: none; }
  .clue-lists ol { flex: 1; min-height: 0; max-height: none; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
  body[data-pane=across] #down-sec, body[data-pane=down] #across-sec, body[data-pane=chat] .clue-lists { display: none; }
  body:not([data-pane=chat]) .chat-col { display: none; }
  .chat-col { height: auto; position: static; border-radius: 0; border-width: 0; }
  .chat-col h2 { display: none; }
  .messages { overscroll-behavior: contain; }
}
