:root {
  --bg: #f7f3ea;
  --paper: #fffdf8;
  --ink: #1f2937;
  --muted: #6b7280;
  --line: #d6d0c2;
  --accent: #2563eb;
  --word: #fde68a;
  --current: #f59e0b;
  --wrong: #dc2626;
  --radius: 12px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --serif: Georgia, "Iowan Old Style", "Times New Roman", serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171c;
    --paper: #1f232b;
    --ink: #e8e6e1;
    --muted: #9ca3af;
    --line: #3a3f4a;
    --word: #5b4a17;
    --current: #b7791f;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font-family: var(--font); line-height: 1.4; -webkit-tap-highlight-color: transparent; }
a { color: var(--accent); }
h1 { font-family: var(--serif); font-size: 1.5rem; margin: 0.2rem 0; }
h2 { font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 0 0 0.6rem; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.problem { color: var(--wrong); }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

button, .button { font: inherit; border-radius: 999px; border: 1px solid transparent; padding: 0.55rem 1.1rem; cursor: pointer; display: inline-flex; align-items: center; gap: 0.4rem; text-decoration: none; min-height: 44px; }
.primary { background: var(--ink); color: var(--paper); }
.secondary { background: transparent; color: var(--ink); border-color: var(--line); }
.secondary:hover { border-color: var(--ink); }
.big { font-size: 1.1rem; padding: 0.8rem 1.6rem; }
.small-button { min-height: 36px; padding: 0.35rem 0.9rem; font-size: 0.9rem; }
.link-button { background: none; border: none; color: var(--muted); padding: 0.3rem; min-height: 0; font-size: 0.9rem; }
.icon-button { background: none; border: none; color: var(--muted); min-height: 40px; min-width: 40px; justify-content: center; padding: 0; font-size: 1.1rem; }
.disabled { opacity: 0.4; pointer-events: none; }

.top { display: flex; align-items: center; gap: 1rem; padding: 0.6rem max(16px, env(safe-area-inset-left)); border-bottom: 1px solid var(--line); background: var(--paper); }
.brand { display: flex; align-items: center; gap: 0.5rem; font-family: var(--serif); font-weight: 700; color: var(--ink); text-decoration: none; font-size: 1.1rem; }
#people { margin-left: auto; }
.people { display: flex; gap: 0.75rem; flex-wrap: wrap; }
.person { display: inline-flex; align-items: center; gap: 0.35rem; font-size: 0.9rem; color: var(--who); font-weight: 600; }
.dot { width: 9px; height: 9px; border-radius: 50%; border: 2px solid var(--who); }
.dot.on { background: var(--who); }
main { padding: 1rem 16px 2rem; max-width: 1200px; margin: 0 auto; }

.login { max-width: 420px; text-align: center; padding-top: 12vh; display: flex; flex-direction: column; align-items: center; gap: 0.6rem; }
.login ul { list-style: none; padding: 0; }

/* Library */
.library { display: grid; gap: 1rem; grid-template-columns: minmax(0, 1fr); max-width: 760px; margin: 0 auto; }
.card { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; }
textarea { width: 100%; font: inherit; padding: 0.7rem; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); resize: vertical; }
.row { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-top: 0.6rem; }
.format { background: var(--bg); padding: 0.7rem; border-radius: 8px; white-space: pre-wrap; font-size: 0.8rem; }
.filters { display: flex; gap: 0.4rem; flex-wrap: wrap; margin-bottom: 0.6rem; }
.chip { min-height: 34px; padding: 0.3rem 0.8rem; border: 1px solid var(--line); background: transparent; color: var(--ink); font-size: 0.9rem; }
.chip.active { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.puzzle-item { display: flex; align-items: center; gap: 0.5rem; border-top: 1px solid var(--line); }
.puzzle-link { flex: 1; display: flex; flex-direction: column; gap: 0.15rem; padding: 0.75rem 0; color: var(--ink); text-decoration: none; min-width: 0; }
.puzzle-link strong { font-family: var(--serif); font-size: 1.05rem; }
.progress-line { display: flex; align-items: center; gap: 0.5rem; color: var(--muted); flex-wrap: wrap; }
.bar { width: 90px; height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; }
.bar span { display: block; height: 100%; background: var(--accent); }
.badge { border: 1px solid var(--line); border-radius: 999px; padding: 0 0.5rem; font-size: 0.75rem; }
.badge.done { background: #059669; color: white; border-color: #059669; }

/* Solver */
.solver-head { margin-bottom: 0.8rem; }
.back { text-decoration: none; font-size: 0.9rem; }
.notes { background: var(--paper); border-left: 3px solid var(--line); padding: 0.4rem 0.6rem; }
.solver-main { display: grid; gap: 1.25rem; grid-template-columns: minmax(0, 1fr); }
@media (min-width: 900px) {
  .solver-main { grid-template-columns: minmax(0, 560px) minmax(0, 1fr); align-items: start; }
  .clue-lists { max-height: calc(100vh - 140px); overflow: auto; position: sticky; top: 1rem; }
}
.board { container-type: inline-size; }
.grid {
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  aspect-ratio: var(--cols) / var(--rows);
  width: 100%;
  max-width: 640px;
  gap: 1px;
  background: var(--ink);
  border: 2px solid var(--ink);
  user-select: none;
  touch-action: manipulation;
}
.cell { position: relative; background: var(--paper); cursor: pointer; display: flex; align-items: center; justify-content: center; }
.cell.block { background: var(--ink); cursor: default; }
.cell.in-word { background: var(--word); }
.cell.partner-word:not(.in-word) { background: color-mix(in srgb, var(--partner) 18%, var(--paper)); }
.cell.partner::after { content: ""; position: absolute; inset: 1px; border: 3px solid var(--partner); pointer-events: none; }
.cell.current { background: var(--current); }
.cell.circled::before { content: ""; position: absolute; inset: 6%; border: 1px solid var(--muted); border-radius: 50%; pointer-events: none; }
.num { position: absolute; top: 1px; left: 2px; font-size: max(8px, calc(100cqw / var(--cols) * 0.26)); line-height: 1; color: var(--ink); }
.letter { font-family: var(--font); font-weight: 650; font-size: calc(100cqw / var(--cols) * 0.58); line-height: 1; margin-top: 8%; }
.cell.revealed .letter { color: var(--muted); font-style: italic; }
.cell.wrong .letter { text-decoration: line-through; text-decoration-color: var(--wrong); text-decoration-thickness: 2px; }
.cell.wrong { box-shadow: inset 0 0 0 2px var(--wrong); }
.grid.finished { animation: pop 0.6s ease 2; }
@keyframes pop { 50% { transform: scale(1.02); } }

.clue-bar-wrap { display: flex; align-items: center; gap: 0.25rem; background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); margin: 0.75rem 0; padding: 0.2rem; max-width: 640px; }
.clue-bar { flex: 1; padding: 0.4rem; font-family: var(--serif); font-size: 1.05rem; min-height: 2.6em; display: flex; gap: 0.5rem; align-items: center; cursor: pointer; }
.clue-bar .clue-num { font-family: var(--font); font-weight: 700; font-size: 0.85rem; color: var(--muted); }
.enum { color: var(--muted); white-space: nowrap; }

.toolbar { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; max-width: 640px; }
.menu { position: relative; }
.menu summary { list-style: none; }
.menu summary::-webkit-details-marker { display: none; }
.menu-items { position: absolute; z-index: 5; top: calc(100% + 4px); left: 0; background: var(--paper); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 8px 24px rgb(0 0 0 / 0.15); display: flex; flex-direction: column; min-width: 150px; overflow: hidden; }
.menu-item { border-radius: 0; background: none; color: var(--ink); justify-content: flex-start; border: 0; }
.menu-item:hover { background: var(--bg); }
.conn { margin-left: auto; }

.clue-lists { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.clues { list-style: none; margin: 0; padding: 0; }
.clue { display: flex; gap: 0.5rem; padding: 0.4rem 0.5rem; border-radius: 8px; cursor: pointer; font-family: var(--serif); border-left: 3px solid transparent; }
.clue:hover { background: var(--paper); }
.clue.active { background: var(--word); }
.clue.partner-clue { border-left-color: var(--partner); }
.clue.done .clue-text { color: var(--muted); }
.clue-num { font-family: var(--font); font-weight: 700; min-width: 1.8rem; font-size: 0.9rem; padding-top: 0.1rem; }

/* Phones and tablets: keyboard docked at the bottom */
.solver.touch { padding-bottom: 230px; }
.dock { position: fixed; left: 0; right: 0; bottom: 0; background: var(--paper); border-top: 1px solid var(--line); padding: 0.3rem 6px calc(0.4rem + env(safe-area-inset-bottom)); z-index: 10; }
.dock .clue-bar-wrap { margin: 0 0 0.35rem; max-width: none; border: 0; background: var(--bg); }
.dock .clue-bar { font-size: 1rem; }
.keyboard { display: flex; flex-direction: column; gap: 6px; max-width: 640px; margin: 0 auto; }
.kb-row { display: flex; gap: 5px; justify-content: center; }
.key { flex: 1 1 0; min-width: 0; min-height: 46px; padding: 0; justify-content: center; border-radius: 7px; background: var(--bg); color: var(--ink); border: 1px solid var(--line); font-weight: 600; font-size: 1.05rem; touch-action: manipulation; }
.key:active { background: var(--line); }
.kb-wide { flex: 1.5 1 0; }
@media (max-width: 520px) {
  main { padding-top: 0.6rem; }
  .solver-head h1 { font-size: 1.2rem; }
  .top { gap: 0.5rem; }
  .brand { font-size: 1rem; }
}

.overlay { position: fixed; inset: 0; background: rgb(0 0 0 / 0.4); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 16px; }
.dialog { background: var(--paper); border-radius: var(--radius); padding: 1.2rem; max-width: 360px; width: 100%; }
.dialog-buttons { display: flex; gap: 0.5rem; justify-content: flex-end; }
.toast { position: fixed; left: 50%; top: 1rem; transform: translate(-50%, -150%); background: var(--ink); color: var(--paper); padding: 0.6rem 1rem; border-radius: 999px; transition: transform 0.25s; z-index: 60; max-width: calc(100vw - 32px); text-align: center; }
.toast.show { transform: translate(-50%, 0); }

/* Keep the selected square visible above the docked keyboard, and fit the grid above it. */
html:has(.solver.touch) { scroll-padding-bottom: 250px; scroll-padding-top: 12px; }
.solver.touch { --chrome: 330px; }
.solver.touch .grid { max-width: min(640px, max(260px, calc((100dvh - var(--chrome)) * var(--cols) / var(--rows)))); margin: 0 auto; }
@media (min-width: 700px) { .solver.touch { --chrome: 470px; } .solver.touch .solver-head { display: flex; gap: 1rem; align-items: baseline; flex-wrap: wrap; } }
.solver.touch .toolbar { margin-top: 0.75rem; }
@media (max-width: 520px) {
  .solver.touch .solver-head .muted.small, .solver.touch .notes { display: none; }
  .top .brand span { display: none; }
}
