@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@400;700;900&family=Inter:wght@400;500;600&family=Source+Serif+4:ital,wght@0,400;1,400&display=swap');

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

:root {
  --cell-size: 34px;
}

html, body {
  /* Block iOS's pull-to-refresh and rubber-band bounce. Swiping
     down on the title / clue bar / keyboard shouldn't reload the
     page. */
  overscroll-behavior: none;
  height: 100%;
}

body {
  font-family: 'Inter', sans-serif;
  /* Site-wide dark theme — every page in the app is dark, so the
     crossword desktop view matches the landing, Spot It, and the
     placeholder pages. The warm gold/cream accents stay for
     highlights. */
  background: #1a1a1a;
  color: #e8e3dc;
  min-height: 100vh;
  -webkit-text-size-adjust: 100%;
  /* pan-y on body blocks pinch and double-tap zoom; the grid and
     keyboard below keep `manipulation` so single taps stay fast. */
  touch-action: pan-y;
}

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  padding: 24px 16px 10px;
  position: relative;
}

.page-header-titles {
  flex: 1;
  min-width: 0;
  text-align: center;
}

/* Spacer balances the menu button on the right so the title stays
   visually centered in the flex row. aria-hidden; just geometry. */
.page-header-spacer {
  display: inline-block;
  flex-shrink: 0;
  width: 44px;
  height: 44px;
}

/* Menu button (hamburger) sits on the left of the header, 44x44
   tap target per Apple HIG. */
.menu-btn {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  background: transparent;
  border: none;
  color: currentColor;
  padding: 0;
  border-radius: 10px;
  cursor: pointer;
  font-family: inherit;
  -webkit-appearance: none;
  appearance: none;
  opacity: 0.8;
  transition: opacity 0.15s, background 0.15s;
}
.menu-btn svg { display: block; width: 24px; height: 24px; }
.menu-btn:hover { opacity: 1; background: rgba(0,0,0,0.05); }
.menu-btn:active { opacity: 1; background: rgba(0,0,0,0.1); }

/* Menu dropdown — fixed-positioned so it's above the grid/clue bar
   and never gets clipped by overflow: hidden on the body (phone). */
.menu-dropdown {
  position: fixed;
  left: 12px;
  top: 56px;
  z-index: 200;
  min-width: 200px;
  background: #2d2926;
  color: #e8e3dc;
  border: 1px solid #3a3530;
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.35);
  padding: 6px 0;
  font-family: inherit;
  font-size: 0.95em;
}
.menu-dropdown[hidden] { display: none; }

.menu-dropdown .menu-item {
  display: block;
  width: 100%;
  background: transparent;
  border: none;
  color: inherit;
  font: inherit;
  text-align: left;
  padding: 10px 18px;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
}
.menu-dropdown .menu-item:hover,
.menu-dropdown .menu-item:active {
  background: #3a3530;
}

.page-header h1 {
  font-family: 'Playfair Display', serif;
  font-weight: 900;
  font-size: 2.6em;
  color: #e8e3dc;
  letter-spacing: -0.02em;
  line-height: 1.1;
}

.page-header .subtitle {
  font-family: 'Source Serif 4', serif;
  font-style: italic;
  font-size: 1.15em;
  color: #a29b90;
  margin-top: 8px;
}

.page-header .divider {
  width: 60px;
  height: 3px;
  background: #c9a96e;
  margin: 18px auto 0;
  border-radius: 2px;
}

.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 20px;
  display: flex;
  gap: 30px;
  align-items: flex-start;
}

@media (max-width: 900px) {
  .container {
    flex-direction: column;
    align-items: center;
  }
}

/* Grid */
.grid-wrapper {
  flex-shrink: 0;
  position: relative;
}

/* Hidden input that captures all keyboard input (NYT-style).
   Positioned over the active cell so iOS doesn't scroll it
   elsewhere when the keyboard opens. font-size: 16px prevents
   the iOS auto-zoom on focus. */
#hidden-input {
  position: absolute;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  border: none;
  outline: none;
  font-size: 16px;
  padding: 0;
  margin: 0;
  -webkit-appearance: none;
  appearance: none;
  pointer-events: none;
  caret-color: transparent;
  background: transparent;
  color: transparent;
  z-index: -1;
  /* textarea-specific: kill resize handle and scrollbar */
  resize: none;
  overflow: hidden;
  line-height: 1;
  white-space: nowrap;
}

.crossword-grid {
  display: grid;
  grid-template-columns: repeat(15, var(--cell-size));
  grid-template-rows: repeat(15, var(--cell-size));
  gap: 1px;
  background: #3a3530;
  border: 2px solid #3a3530;
  border-radius: 4px;
  overflow: hidden;
  margin: 0 auto;
  touch-action: manipulation;
}

.cell {
  position: relative;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}

.cell.black {
  /* Blacks match the body so they blend into the page on dark theme. */
  background: #1a1a1a;
}

.cell input {
  width: 100%;
  height: 100%;
  border: none;
  outline: none;
  text-align: center;
  font-family: 'Inter', sans-serif;
  font-size: 16px;
  font-weight: 600;
  text-transform: uppercase;
  background: transparent;
  color: #2d2926;
  caret-color: transparent;
  padding: 0;
  -webkit-appearance: none;
  appearance: none;
  border-radius: 0;
  /* Input is display-only; all keyboard input routes
     through #hidden-input. Pointer events pass to the
     cell div which owns the tap handler. */
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
}

.cell .cell-number {
  position: absolute;
  top: 1px;
  left: 2px;
  font-size: 9px;
  font-weight: 600;
  color: #8b7d6b;
  pointer-events: none;
  line-height: 1;
}

.cell.selected input {
  background: #c9a96e33;
}

.cell.highlighted input {
  background: #f5edd8;
}

.cell.correct input {
  color: #4a7c59;
}

.cell.incorrect {
  background: #d94545;
}
.cell.incorrect input {
  color: #fff;
  background: transparent;
}
.cell.incorrect.selected input {
  background: rgba(255,255,255,0.15);
}

.cell.revealed input {
  color: #8b7d6b;
  font-style: italic;
}

/* Clues */
.clues-panel {
  flex: 1;
  min-width: 280px;
}

.clue-section h3 {
  font-family: 'Playfair Display', serif;
  font-size: 1.2em;
  font-weight: 700;
  color: #e8e3dc;
  margin-bottom: 10px;
  padding-bottom: 6px;
  border-bottom: 2px solid #3a3530;
}

.clue-section + .clue-section {
  margin-top: 22px;
}

.clue-list {
  list-style: none;
}

.clue-list li {
  padding: 6px 10px;
  cursor: pointer;
  border-radius: 6px;
  font-size: 0.92em;
  line-height: 1.45;
  color: #d4cfc7;
  transition: background 0.15s;
}

.clue-list li:hover {
  background: #2d2926;
}

.clue-list li.active {
  background: #2d2926;
  color: #f7d94c;
  font-weight: 500;
}

.clue-list li.solved {
  color: #6a6560;
  text-decoration: line-through;
  text-decoration-color: #c9a96e;
}

.clue-num {
  font-weight: 700;
  color: #c9a96e;
  margin-right: 4px;
}

/* Buttons */
.actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  padding: 20px;
  flex-wrap: wrap;
}

.btn {
  font-family: 'Inter', sans-serif;
  font-size: 0.9em;
  font-weight: 600;
  padding: 10px 22px;
  border: 2px solid #555;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.15s;
  background: #2d2926;
  color: #e8e3dc;
}

.btn:hover {
  background: #3a3530;
  color: #faf7f2;
  border-color: #6a6560;
}

.btn.primary {
  background: #c9a96e;
  border-color: #c9a96e;
  color: #fff;
}

.btn.primary:hover {
  background: #b8953a;
  border-color: #b8953a;
}

/* Win overlay */
.win-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(45,41,38,0.7);
  z-index: 100;
  align-items: center;
  justify-content: center;
}

.win-overlay.show {
  display: flex;
}

.win-card {
  background: #2d2926;
  color: #e8e3dc;
  border-radius: 16px;
  padding: 50px;
  text-align: center;
  max-width: 440px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}

.win-card h2 {
  font-family: 'Playfair Display', serif;
  font-size: 2em;
  margin-bottom: 10px;
}

.win-card p {
  font-size: 1.05em;
  color: #a29b90;
  line-height: 1.5;
  margin-bottom: 20px;
}

.win-card .stars {
  font-size: 2em;
  margin-bottom: 12px;
}

/* Timer */
.timer-bar {
  text-align: center;
  padding: 8px;
  font-size: 0.85em;
  color: #8b8680;
  font-variant-numeric: tabular-nums;
}

/* Active-clue banner. Hidden on desktop; shown on tablet/phone. */
.active-clue-bar {
  display: none;
  background: #2d2926;
  color: #faf7f2;
  box-shadow: 0 -2px 10px rgba(0,0,0,0.25);
  align-items: center;
  gap: 6px;
  padding: 10px 10px;
  font-size: 0.95em;
  line-height: 1.35;
}

.active-clue-bar .ac-main {
  flex: 1;
  min-width: 0;
  text-align: center;
  padding: 0 4px;
  /* Allow the clue to wrap to 2 lines before it truncates — our
     longest clues need 2 lines on a phone-sized bar. Short clues
     still render on a single line. */
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.25;
}

.active-clue-bar .ac-label {
  font-family: 'Playfair Display', serif;
  font-weight: 700;
  color: #c9a96e;
  margin-right: 6px;
  font-size: 0.95em;
  letter-spacing: 0.02em;
}

.active-clue-bar .ac-text {
  font-weight: 500;
}

.active-clue-bar .ac-arrow {
  background: transparent;
  border: none;
  color: #faf7f2;
  font-family: 'Inter', sans-serif;
  font-size: 1.3em;
  line-height: 1;
  padding: 8px 14px;
  border-radius: 50%;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s;
}

.active-clue-bar .ac-arrow:active {
  background: rgba(201, 169, 110, 0.25);
}

.active-clue-bar .ac-switch {
  background: transparent;
  border: 1px solid rgba(201, 169, 110, 0.5);
  color: #faf7f2;
  font-family: 'Inter', sans-serif;
  font-size: 1em;
  line-height: 1;
  padding: 6px 10px;
  border-radius: 14px;
  cursor: pointer;
  flex-shrink: 0;
}

.active-clue-bar .ac-switch:active { background: #c9a96e; }

/* Responsive: iPad portrait & smaller.
   Tablet keeps the warm cream aesthetic; clue bar sticks to the TOP. */
@media (max-width: 900px) {
  :root { --cell-size: min(36px, calc((100vw - 16px) / 15)); }

  .page-header { padding: 20px 16px 8px; }
  .page-header h1 { font-size: 1.8em; }
  .page-header .subtitle { font-size: 0.95em; }

  .container {
    padding: 10px 8px;
    gap: 16px;
  }

  .timer-bar { padding: 6px; font-size: 0.8em; }

  .cell .cell-number { font-size: 8px; }
  .cell input { font-size: 15px; }

  .active-clue-bar {
    display: flex;
    position: sticky;
    top: 0;
    z-index: 50;
  }

  .clues-panel {
    min-width: 0;
    width: 100%;
    max-width: 640px;
  }

  .clue-section h3 { font-size: 1.05em; }
  .clue-list li { padding: 10px 12px; font-size: 0.95em; }

  .actions { padding: 14px 10px; gap: 8px; }
  .btn {
    padding: 12px 18px;
    font-size: 0.88em;
    flex: 1 1 40%;
    min-width: 140px;
  }
}

/* Default: custom keyboard is hidden on desktop/tablet. */
.custom-keyboard { display: none; }

/* Touch-device UI: dark theme, bottom-pinned clue bar, bigger letters,
   and our own on-screen keyboard replacing iOS's. Patterned after NYT
   Games' phone layout so the grid + current clue + keyboard fit
   together with minimal chrome.
   Targets any coarse-pointer device — iPhone AND iPad. Desktop with a
   mouse (pointer: fine) keeps the classic grid + side clue list +
   physical keyboard flow. */
@media (pointer: coarse) {
  :root {
    /* Width-based sizing with a cap so iPad cells don't get gigantic.
       46px cap means an iPad portrait grid ~708px wide; iPhone
       portrait is bounded by the (100vw - 32)/15 term. A height
       clamp (100dvh - 360px) keeps landscape phones from
       overflowing vertically. */
    --cell-size: min(
      46px,
      calc((100vw - 32px) / 15),
      calc((100dvh - 360px) / 15)
    );
    /* Fixed height of the custom keyboard, used to anchor the clue
       bar directly above it. 3 rows * 42px + 2 * 6px gaps +
       8px top padding + 4px bottom padding + safe-area. */
    --custom-kbd-height: calc(144px + env(safe-area-inset-bottom));
  }

  body {
    background: #1a1a1a;
    color: #e8e3dc;
    /* Phones: treat the page like an app. No scroll at all — content
       already fits in the viewport (grid + header + fixed clue bar +
       fixed keyboard) so there's nothing to scroll to. No scroll
       means no pull-to-refresh, no URL-bar reveal from swiping on
       the background / title / space around the board. */
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    padding-bottom: 0;
    /* No text selection, no long-press context menu, no drag-select —
       those interactions interfere with tapping cells. */
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
  }

  html {
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }

  /* Menu button on phone: lighter color, same 44x44 tap target. */
  .menu-btn {
    color: #e8e3dc;
    opacity: 0.9;
  }
  .menu-btn:hover,
  .menu-btn:active {
    background: rgba(255,255,255,0.1);
  }
  .menu-dropdown {
    left: 8px;
    top: 54px;
  }

  /* Compact header — grid is the star, title shrinks, subtitle hides. */
  .page-header { padding: 8px 8px 2px; align-items: center; }
  .page-header h1 {
    font-size: 1.05em;
    color: #e8e3dc;
    letter-spacing: 0;
    font-weight: 700;
  }
  .page-header .subtitle { display: none; }
  .page-header .divider { display: none; }

  .timer-bar {
    padding: 2px 0 6px;
    font-size: 0.75em;
    color: #8b8680;
  }

  /* Touch-device layout: single column, grid centered horizontally.
     Without this, iPad landscape (>900px, so the max-width:900 tablet
     rule doesn't fire) keeps the row layout and the grid sticks to
     the left side of the viewport. */
  .container {
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
    gap: 12px;
  }

  /* Clue list lives in the bottom clue bar's Prev/Next on phones.
     Hiding it also neutralizes scrollIntoView() on the active clue —
     there's no visible list to scroll to, so the grid stays put. */
  .clues-panel { display: none; }

  /* Action buttons (Check / Reveal / Start Over) and the tribute
     footer are hidden on phones to keep the screen focused on the
     grid + clue + keyboard. They're still there on tablet/desktop. */
  .actions { display: none; }
  .footer { display: none; }

  /* Grid — white cells over a dark bezel so they pop. */
  .crossword-grid {
    background: #3a3530;
    border-color: #3a3530;
  }
  .cell.black { background: #1a1a1a; }
  .cell input {
    font-size: 18px;
    font-weight: 700;
    color: #1a1a1a;
  }
  .cell .cell-number {
    font-size: 8px;
    top: 1px;
    left: 2px;
    color: #6a6560;
  }

  /* Highlight: the whole active word gets a soft blue wash. */
  .cell.highlighted input {
    background: #c6e2f9;
  }
  /* Selected: the cursor cell is bright yellow (overrides highlighted). */
  .cell.selected input,
  .cell.highlighted.selected input {
    background: #f7d94c;
  }

  .cell.correct input { color: #2d7a4f; }
  .cell.revealed input { color: #6a6560; }
  /* Wrong letter: cell goes red. The compound selectors below beat
     .cell.highlighted/.selected's blue/yellow backgrounds so the red
     is visible the instant a wrong letter is typed, even while the
     cell is part of the currently-highlighted word or is the cursor. */
  .cell.incorrect { background: #d94545; }
  .cell.incorrect input,
  .cell.incorrect.highlighted input,
  .cell.incorrect.selected input,
  .cell.incorrect.selected.highlighted input {
    color: #fff;
    background: transparent;
  }

  /* Clue list: readable on dark background, more tappable rows. */
  .clue-section h3 {
    color: #e8e3dc;
    border-bottom-color: #3a3530;
  }
  .clue-list li {
    color: #d4cfc7;
    padding: 12px 12px;
    font-size: 0.95em;
  }
  .clue-list li:hover { background: #2d2926; }
  .clue-list li.active {
    background: #2d2926;
    color: #f7d94c;
    font-weight: 600;
  }
  .clue-list li.solved { color: #6a6560; }

  /* Action buttons on dark background. */
  .actions { padding: 14px 10px; gap: 8px; }
  .btn {
    background: #2d2926;
    border-color: #555;
    color: #e8e3dc;
    padding: 12px 14px;
    font-size: 0.88em;
    flex: 1 1 40%;
    min-width: 140px;
  }
  .btn:hover { background: #3a3530; color: #faf7f2; }
  .btn.primary {
    background: #c9a96e;
    border-color: #c9a96e;
    color: #1a1a1a;
  }
  .btn.primary:hover { background: #b8953a; border-color: #b8953a; color: #1a1a1a; }

  .footer { color: #6a6560; padding: 12px 20px; }

  /* Clue bar: sits directly above our custom on-screen keyboard.
     display: flex here is REQUIRED because iPad landscape doesn't
     trigger the `@media (max-width: 900px)` block that normally
     reveals the bar — it's still hidden by the base `display: none`
     until we turn it on for any coarse-pointer device. */
  .active-clue-bar {
    display: flex;
    position: fixed;
    top: auto;
    bottom: var(--custom-kbd-height);
    left: 0;
    right: 0;
    z-index: 50;
    padding: 12px 10px;
    border-top: 1px solid rgba(201, 169, 110, 0.25);
    font-size: 1em;
  }
  /* The keyboard's presence handles "padding" for us; when it's down the
     bar already has safe-area padding. Keep content from hiding behind
     it (padding-bottom on body above handles this). */

  .active-clue-bar .ac-arrow {
    font-size: 1.4em;
    padding: 10px 18px;
  }

  /* Hide the explicit Switch button on phones — tapping the selected
     cell already toggles direction (discoverable, matches NYT). */
  .active-clue-bar .ac-switch { display: none; }

  /* Win card padding stays gentle on small phones. */
  .win-card { padding: 32px 22px; margin: 20px; background: #2d2926; color: #e8e3dc; }
  .win-card h2 { font-size: 1.5em; color: #e8e3dc; }
  .win-card p { color: #a29b90; }

  /* Custom QWERTY keyboard, fixed at the bottom. Replaces iOS's
     (suppressed via inputmode="none" on the hidden textarea) so we
     control exactly what's on-screen below the grid — no password
     autofill chips, no QuickType bar, and room for a dedicated
     swap-direction key. */
  .custom-keyboard {
    display: flex;
    flex-direction: column;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 100;
    background: #1f1f1f;
    padding: 8px 3px calc(4px + env(safe-area-inset-bottom));
    gap: 6px;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
  }

  .custom-keyboard .kbd-row {
    display: flex;
    justify-content: center;
    gap: 4px;
  }

  /* Offset the middle row so A-L sit between Q-P and swap/Z-M/back,
     matching the usual QWERTY visual rhythm. */
  .custom-keyboard .kbd-row-mid {
    padding: 0 5%;
  }

  .custom-keyboard .kbd-key {
    flex: 1 1 0;
    min-width: 0;
    /* Cap key width so the keyboard stays comfortable on iPad Pro
       landscape (~1200px wide). Phones never hit this cap. */
    max-width: 58px;
    height: 42px;
    border: none;
    border-radius: 6px;
    background: #3f3f3f;
    color: #fff;
    font-family: inherit;
    font-size: 17px;
    font-weight: 500;
    padding: 0;
    cursor: pointer;
    -webkit-appearance: none;
    appearance: none;
    transition: background-color 0.05s;
  }

  .custom-keyboard .kbd-key:active {
    background: #6a6a6a;
  }

  .custom-keyboard .kbd-key.kbd-wide {
    flex: 1.6 1 0;
    max-width: 84px;
    background: #2a2a2a;
    font-size: 20px;
  }
}

/* Footer */
.footer {
  text-align: center;
  padding: 20px;
  font-size: 0.8em;
  color: #b0a696;
  font-family: 'Source Serif 4', serif;
  font-style: italic;
}
