:root {
  color-scheme: light;
  --ink: #1f2330;
  --muted: #6a7384;
  --paper: #f6f1e7;
  --panel: #fffdf9;
  --line: #d9d3c4;
  --line-strong: #b8b09f;
  --accent: #e8763a;
  --accent-dark: #b65621;
  --highlight: #fff2d4;
  --highlight-strong: #ffd97a;
  --ok: #4f8d5f;
  --warn: #d95043;
  --block: #2a2f3c;
  --shadow: 0 18px 42px rgba(44, 36, 24, 0.14);
  font-family: "Outfit", Inter, ui-sans-serif, system-ui, -apple-system,
    BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(135deg, rgba(232, 118, 58, 0.14), transparent 36%),
    linear-gradient(225deg, rgba(80, 140, 200, 0.14), transparent 42%),
    var(--paper);
  color: var(--ink);
}

button,
select {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
  border-radius: 999px;
  padding: 8px 16px;
  background: var(--accent);
  color: white;
  font-weight: 600;
  box-shadow: 0 2px 0 var(--accent-dark);
  transition: transform 0.08s ease;
}

button:hover {
  transform: translateY(-1px);
}

button:active {
  transform: translateY(1px);
  box-shadow: 0 1px 0 var(--accent-dark);
}

button.ghost {
  background: transparent;
  color: var(--muted);
  box-shadow: inset 0 0 0 1.5px var(--line-strong);
}

button.ghost:hover {
  color: var(--ink);
}

.btn-link {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
  font-weight: 600;
  font-size: 0.95rem;
  padding: 8px 16px;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  box-shadow: inset 0 0 0 1.5px var(--line-strong);
}

.btn-link:hover {
  color: var(--ink);
}

/* Overflow menu — single ⋯ button + dropdown for secondary actions */
.overflow-menu {
  position: relative;
  display: inline-block;
}

.overflow-toggle {
  font-size: 1.1rem;
  line-height: 1;
  padding: 8px 14px;
  letter-spacing: 0.05em;
}

.overflow-menu-list {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  margin: 0;
  padding: 6px 0;
  list-style: none;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 12px 30px rgba(44, 36, 24, 0.18);
  min-width: 200px;
  z-index: 50;
  animation: fadeIn 0.12s ease-out;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-4px); }
  to   { opacity: 1; transform: translateY(0); }
}

.overflow-item {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  box-shadow: none;
  padding: 9px 16px;
  font: inherit;
  font-size: 0.92rem;
  font-weight: 500;
  color: var(--ink);
  text-decoration: none;
  cursor: pointer;
  border-radius: 0;
  transition: background-color 0.08s ease;
}

.overflow-item:hover,
.overflow-item:focus-visible {
  background: var(--highlight);
  transform: none;
  outline: none;
}

.overflow-divider {
  height: 1px;
  background: var(--line);
  margin: 4px 0;
  padding: 0;
  list-style: none;
}

.id-chip {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  padding: 2px 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  cursor: pointer;
  font-size: 0.82rem;
}

.id-chip:hover {
  border-color: var(--accent);
}

.id-chip-label {
  color: var(--muted);
  text-transform: uppercase;
  font-weight: 700;
  font-size: 0.68rem;
  letter-spacing: 0.04em;
}

.id-chip code {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--ink);
  letter-spacing: 0.02em;
}

/* Hidden text input that captures keystrokes + opens soft keyboard on touch.
   IMPORTANT for iOS Safari:
   - Must be inside the viewport (no left:-9999px tricks) or iOS auto-blurs it.
   - Must NOT have pointer-events:none — iOS treats those as non-focusable and
     dismisses the keyboard right after focusing.
   - Must NOT carry aria-hidden — same effect on focus retention.
   We hide it visually with opacity + z-index instead. */
.hidden-input {
  position: fixed;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  font-size: 16px; /* prevent iOS zoom on focus */
  border: 0;
  background: transparent;
  caret-color: transparent;
  z-index: -1;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 16px;
  height: 100vh;
  padding: 16px;
}

.game-panel,
.clue-panel {
  background: rgba(255, 253, 249, 0.95);
  border: 1px solid rgba(217, 211, 196, 0.95);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 18px 22px;
  /* Lock panels to viewport so internal lists scroll instead of pushing the page */
  min-height: 0;
  overflow: hidden;
}

.game-panel {
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr);
  min-width: 0;
  gap: 12px;
}

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: 1.8rem;
  font-weight: 800;
  letter-spacing: -0.01em;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.select-control {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85rem;
  color: var(--muted);
  background: var(--panel);
  border: 1.5px solid var(--line);
  border-radius: 999px;
  padding: 4px 10px;
}

.select-control select {
  border: 0;
  background: transparent;
  color: var(--ink);
  font-weight: 600;
}

.select-control input[type="number"] {
  border: 0;
  background: transparent;
  color: var(--ink);
  font-weight: 600;
  font-family: inherit;
  font-size: inherit;
  width: 3em;
  text-align: center;
  -moz-appearance: textfield;
}

.select-control input[type="number"]::-webkit-inner-spin-button,
.select-control input[type="number"]::-webkit-outer-spin-button {
  opacity: 1;
  height: 22px;
}

.status-strip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 12px;
  background: var(--highlight);
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  font-size: 0.92rem;
  flex-wrap: wrap;
}

.status-strip.success {
  background: rgba(79, 141, 95, 0.12);
  border-color: rgba(79, 141, 95, 0.5);
  color: var(--ok);
}

.status-strip.error {
  background: rgba(217, 80, 67, 0.1);
  border-color: rgba(217, 80, 67, 0.5);
  color: var(--warn);
}

.active-clue {
  display: grid;
  grid-template-columns: 40px minmax(0, 1fr) 40px;
  align-items: stretch;
  gap: 8px;
  padding: 10px 12px;
  background: var(--panel);
  border: 1.5px solid var(--line);
  border-radius: 10px;
  position: sticky;
  top: 0;
  z-index: 2;
}

.clue-nav {
  background: transparent;
  color: var(--muted);
  box-shadow: inset 0 0 0 1.5px var(--line-strong);
  border-radius: 999px;
  padding: 4px 0;
  font-size: 1.2rem;
  font-weight: 800;
  line-height: 1;
  min-width: 40px;
}

.clue-nav:hover {
  color: var(--ink);
}

.active-clue-body {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
  min-width: 0;
}

.active-clue-label {
  color: var(--accent-dark);
  font-size: 0.72rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.active-clue-text {
  font-size: 0.98rem;
  line-height: 1.35;
  color: var(--ink);
  font-weight: 500;
}

.board-wrap {
  display: flex;
  /* Anchor the board to the top of its container so a tall clue panel doesn't
     push the grid down via vertical centering. */
  align-items: flex-start;
  justify-content: center;
  padding: 0;
  min-height: 0;
  overflow: auto;
}

.crossword-board {
  display: grid;
  gap: 0;
  background: var(--line-strong);
  padding: 2px;
  border-radius: 4px;
  user-select: none;
  /* Width-driven sizing. The cells have aspect-ratio:1 individually, so the
     board's height follows naturally from row count × cell width — no extra
     aspect-ratio on the board (forcing it square would break non-square grids). */
  width: min(560px, 100%);
  touch-action: manipulation;
}

.cw-cell {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  background: var(--panel);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(0.9rem, 4.5vw, 1.4rem);
  font-weight: 700;
  text-transform: uppercase;
  color: var(--ink);
  border: 1px solid var(--line);
  outline: none;
  cursor: pointer;
  transition: background-color 0.08s ease;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.cw-cell.block {
  background: var(--block);
  cursor: default;
}

.cw-cell .cw-number {
  position: absolute;
  top: 1px;
  left: 3px;
  font-size: 0.55rem;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0;
}

.cw-cell.focused {
  background: var(--highlight-strong);
  box-shadow: inset 0 0 0 2px var(--accent);
  z-index: 1;
}

.cw-cell.highlighted {
  background: var(--highlight);
}

.cw-cell.correct {
  color: var(--ok);
}

.cw-cell.wrong {
  color: var(--warn);
}

.board-empty {
  color: var(--muted);
  font-style: italic;
}

.clue-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 12px;
  overflow: hidden;
}

.clue-panel-body {
  display: grid;
  grid-template-rows: 1fr 1fr;
  gap: 12px;
  min-height: 0;
  overflow: hidden;
}

.clue-section {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 6px;
  min-height: 0;
}

.clue-list {
  margin: 0;
  padding: 0 0 0 4px;
  list-style: none;
  overflow-y: auto;
  font-size: 0.92rem;
  line-height: 1.4;
}

.clue-list li {
  padding: 6px 8px;
  border-radius: 6px;
  cursor: pointer;
  display: grid;
  grid-template-columns: 26px 1fr;
  gap: 6px;
  align-items: baseline;
}

.clue-list li:hover {
  background: var(--highlight);
}

.clue-list li.active {
  background: var(--highlight-strong);
  font-weight: 600;
}

.clue-list .clue-num {
  color: var(--muted);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* Drawer toggle is mobile-only — hidden on desktop */
.clue-drawer-toggle {
  display: none;
}

/* ---------- Mobile (< 880px) ---------- */

@media (max-width: 880px) {
  .app-shell {
    grid-template-columns: 1fr;
    /* On mobile we want natural page scroll, not the desktop locked-viewport layout */
    height: auto;
    min-height: 100vh;
    padding: 10px;
    gap: 10px;
  }

  .game-panel,
  .clue-panel {
    padding: 12px;
    border-radius: 8px;
    overflow: visible;
  }

  .board-wrap {
    overflow: visible;
  }

  .game-panel {
    gap: 10px;
  }

  .topbar {
    gap: 8px;
  }

  .topbar h1 {
    font-size: 1.4rem;
  }

  /* Drop the eyebrow tag on narrow viewports — the H1 is enough identification */
  .topbar .eyebrow {
    display: none;
  }

  .controls {
    width: 100%;
    gap: 6px;
  }

  .controls button,
  .controls .btn-link {
    padding: 7px 12px;
    font-size: 0.88rem;
  }

  .select-control {
    flex: 1 1 auto;
    font-size: 0.8rem;
  }

  .status-strip {
    font-size: 0.85rem;
    padding: 6px 10px;
  }

  .id-chip {
    font-size: 0.74rem;
  }

  .active-clue {
    padding: 10px;
  }

  .active-clue-text {
    font-size: 1rem;
    line-height: 1.3;
  }

  .clue-nav {
    min-width: 36px;
    font-size: 1.1rem;
  }

  .board-wrap {
    padding: 4px;
  }

  /* Clue panel becomes a collapsible drawer */
  .clue-panel {
    grid-template-rows: auto;
    padding: 0;
  }

  .clue-drawer-toggle {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    background: transparent;
    color: var(--ink);
    box-shadow: none;
    border-radius: 8px;
    padding: 12px 16px;
    font-weight: 700;
    font-size: 0.95rem;
    text-align: left;
  }

  .clue-drawer-toggle:hover {
    transform: none;
    background: var(--highlight);
  }

  .clue-drawer-toggle .caret {
    font-size: 0.9rem;
    color: var(--muted);
  }

  .clue-panel-body {
    grid-template-rows: auto auto;
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.25s ease, padding 0.25s ease;
    padding: 0 12px;
  }

  .clue-panel.expanded .clue-panel-body {
    max-height: 60vh;
    padding: 0 12px 12px;
    overflow-y: auto;
  }

  .clue-list {
    overflow-y: visible;
  }
}

/* Extra-tight phones */
@media (max-width: 420px) {
  .topbar h1 {
    font-size: 1.25rem;
  }
  .controls button,
  .controls .btn-link {
    padding: 6px 10px;
    font-size: 0.82rem;
  }
  .cw-cell .cw-number {
    font-size: 0.5rem;
  }
}
