/* =============================================================================
   Quartets
   Dark, saturated, geometric — deliberately not NYT Connections' cream page and
   pastel tiles (decision #2, decision #24). Difficulty 4 stays in the violet
   family so "purple first" (bible section 5) still means something.
   ========================================================================== */

:root {
  color-scheme: dark;

  --ink:        #101119;
  --ink-raised: #171927;
  --tile:       #232739;
  --tile-press: #2C3148;
  --tile-sel:   #4C5474;

  --text:       #F4F2EC;
  --text-dim:   #9297AE;
  --hairline:   #2A2E42;

  /* Difficulty ramp: easiest to hardest. */
  --d1: #E8A33D;  /* amber  */
  --d2: #2FB6A0;  /* teal   */
  --d3: #EE6A5F;  /* coral  */
  --d4: #8A7BF0;  /* violet */

  /* Solved banners sit on a dark ink so the colour reads as a band, not a
     pastel block. Text on them is the ink colour, which keeps contrast high. */
  --on-colour: #14151F;

  --radius:  10px;
  --gap:     8px;
  --pad:     14px;

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
          "Helvetica Neue", Arial, sans-serif;

  --ease: cubic-bezier(0.2, 0.8, 0.3, 1);
  /* --ease front-loads ~93% of its motion into the first half of its
     duration — snappy for a 120ms tap highlight, but on a longer motion it
     reads as "moves fast, then sits still": by the time a 450ms transition
     using --ease is 150ms in, it has already covered ~80% of the distance,
     leaving the remaining 300ms visually almost static. --ease-glide is the
     standard symmetric ease-in-out curve, chosen for the solve animation's
     two longer phases (js/app.js animateSolve()) specifically because it
     keeps progress-vs-time close to even across the whole duration. */
  --ease-glide: cubic-bezier(0.42, 0, 0.58, 1);
}

*, *::before, *::after { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;   /* iOS Safari must not resize our tile text */
  text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--ink);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.4;
  /* No rubber-band scroll behind a page that never scrolls. */
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
}

.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* -----------------------------------------------------------------------------
   Shell
   100dvh, not 100vh: iOS Safari's collapsing toolbar makes 100vh taller than
   the visible area, which pushes the Submit button under the browser chrome.
   -------------------------------------------------------------------------- */

.app {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 100vh;
  min-height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  padding:
    calc(env(safe-area-inset-top) + 12px)
    calc(env(safe-area-inset-right) + var(--pad))
    calc(env(safe-area-inset-bottom) + 14px)
    calc(env(safe-area-inset-left) + var(--pad));
}

/* -----------------------------------------------------------------------------
   Masthead
   -------------------------------------------------------------------------- */

.masthead { text-align: center; }

.wordmark {
  margin: 0;
  font-size: clamp(1.5rem, 7vw, 2rem);
  font-weight: 800;
  letter-spacing: -0.035em;
  color: var(--text);
}

/* The four-colour tick under the wordmark is the whole brand mark for now. */
.wordmark::after {
  content: "";
  display: block;
  width: 68px;
  height: 4px;
  margin: 8px auto 0;
  border-radius: 2px;
  background: linear-gradient(
    to right,
    var(--d1) 0 25%, var(--d2) 25% 50%, var(--d3) 50% 75%, var(--d4) 75% 100%
  );
}

.tagline {
  margin: 10px 0 0;
  font-size: 0.85rem;
  color: var(--text-dim);
}

/* -----------------------------------------------------------------------------
   Board
   -------------------------------------------------------------------------- */

.board {
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  /* Takes the slack so the controls stay pinned near the bottom. */
  flex: 1 1 auto;
  justify-content: center;
}

.solved {
  display: flex;
  flex-direction: column;
  gap: var(--gap);
}

.solved:empty { display: none; }

.loading {
  margin: 0;
  padding: 24px 12px;
  text-align: center;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-dim);
}

.loading[hidden] { display: none; }

.solved__group {
  padding: 10px 12px;
  border-radius: var(--radius);
  color: var(--on-colour);
  text-align: center;
  min-height: 62px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
  animation: pop 260ms var(--ease) both;
}

.solved__group[data-difficulty="1"] { background: var(--d1); }
.solved__group[data-difficulty="2"] { background: var(--d2); }
.solved__group[data-difficulty="3"] { background: var(--d3); }
.solved__group[data-difficulty="4"] { background: var(--d4); }

/* Held invisible while the solved tiles are still flying up to meet it (see
   animateSolve() in app.js). Two classes beat one, so removing is-incoming
   hands straight back to the plain .solved__group rule above — including its
   `animation: pop`, which restarts on that computed-value change and doubles
   as the arrival flourish for free. */
.solved__group.is-incoming {
  animation: none;
  opacity: 0;
  transform: scale(0.96);
}

.solved__name {
  font-size: 0.8rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.solved__words {
  font-size: 0.85rem;
  font-weight: 600;
  opacity: 0.85;
}

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

.grid {
  display: grid;
  /* minmax(0, 1fr), not 1fr: a grid track's implicit min-width is `auto`, so a
     long word like GARIBALDI would widen its column past the viewport and push
     the fourth tile and the Submit button off the right edge. */
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: var(--gap);
}

.grid:empty { display: none; }

.tile {
  /* Slightly wider than tall — gives long words like DIGESTIVE more room
     without making the board scroll on a small phone. */
  aspect-ratio: 1 / 0.82;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2px;
  min-width: 0;
  overflow: hidden;

  background: var(--tile);
  color: var(--text);
  border: 0;
  border-radius: var(--radius);
  font-family: inherit;
  font-weight: 700;
  font-size: clamp(0.85rem, 3.8vw, 1.2rem);
  letter-spacing: 0.01em;
  text-transform: uppercase;
  text-align: center;
  line-height: 1.05;

  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  /* Kills the 300ms double-tap-to-zoom delay and the grey flash on iOS. */
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;

  transition: background-color 120ms var(--ease), transform 120ms var(--ease);
}

/* Long words step the type down rather than wrapping or overflowing. */
.tile.is-long  { font-size: clamp(0.75rem, 3.3vw, 1.05rem); letter-spacing: 0; }
.tile.is-xlong { font-size: clamp(0.68rem, 3vw, 0.95rem); letter-spacing: -0.01em; }

.tile:active { background: var(--tile-press); }

.tile.is-selected {
  background: var(--tile-sel);
  transform: scale(0.96);
}

.tile:focus-visible {
  outline: 2px solid var(--text);
  outline-offset: 2px;
}

.tile.is-wrong { animation: shake 380ms var(--ease); }

/* Phase A of a correct guess ("settling into place"): recolour and pop
   slightly in-grid, before phase B (the flight overlay below) lifts the tile
   and slides it up into the solved row. Needs no transition of its own — the
   base .tile rule above already transitions background-color and transform. */
.tile.is-settling { transform: scale(1.05); }
.tile.is-settling[data-difficulty="1"] { background: var(--d1); color: var(--on-colour); }
.tile.is-settling[data-difficulty="2"] { background: var(--d2); color: var(--on-colour); }
.tile.is-settling[data-difficulty="3"] { background: var(--d3); color: var(--on-colour); }
.tile.is-settling[data-difficulty="4"] { background: var(--d4); color: var(--on-colour); }

/* Phase B: a fixed-position layer the four real tile buttons are lifted into
   (not clones) so they can fly from their grid position to the new banner's
   position via a FLIP transform, independent of the grid's own layout. */
.tile-flight {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 5;
}

.tile-flight .tile {
  pointer-events: none;
  will-change: transform;
}

/* -----------------------------------------------------------------------------
   Status line: toast above, lives below
   -------------------------------------------------------------------------- */

.status {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  /* Reserved height, so a toast appearing never shifts the grid. */
  min-height: 46px;
}

.toast {
  margin: 0;
  min-height: 20px;
  font-size: 0.85rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--text);
  opacity: 0;
  transition: opacity 160ms var(--ease);
}

.toast.is-visible { opacity: 1; }

.lives {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.75rem;
  color: var(--text-dim);
}

.lives__dots { display: inline-flex; gap: 6px; }

.dot {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: var(--text);
  transition: background-color 200ms var(--ease), transform 200ms var(--ease);
}

.dot.is-spent {
  background: transparent;
  box-shadow: inset 0 0 0 1.5px var(--tile-sel);
  transform: scale(0.8);
}

/* -----------------------------------------------------------------------------
   Controls
   -------------------------------------------------------------------------- */

.controls {
  display: flex;
  gap: var(--gap);
  justify-content: center;
}

/* Needed because `display: flex` above outranks the UA stylesheet's
   [hidden] { display: none }, which would otherwise leave three dead buttons
   sitting above the result panel. */
.controls[hidden] { display: none; }

.btn {
  flex: 1 1 0;
  min-height: 46px;              /* comfortably above the 44pt touch target */
  padding: 0 10px;
  border-radius: 999px;
  border: 1.5px solid var(--hairline);
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: opacity 120ms var(--ease), background-color 120ms var(--ease);
}

.btn:active:not(:disabled) { background: var(--ink-raised); }

.btn:disabled {
  opacity: 0.35;
  cursor: default;
}

.btn--primary {
  background: var(--text);
  border-color: var(--text);
  color: var(--ink);
}

.btn--primary:active:not(:disabled) { background: #D9D6CE; }

.btn--ghost { flex: 0 0 auto; padding: 0 22px; }

/* -----------------------------------------------------------------------------
   Result
   -------------------------------------------------------------------------- */

.result {
  background: var(--ink-raised);
  border: 1px solid var(--hairline);
  border-radius: 14px;
  padding: 16px;
  text-align: center;
  animation: pop 260ms var(--ease) both;
}

.result[hidden] { display: none; }

.result__title {
  margin: 0 0 4px;
  font-size: 1.15rem;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.result__line {
  margin: 0 0 12px;
  font-size: 0.85rem;
  color: var(--text-dim);
}

.result__grid {
  margin: 0 0 12px;
  font-family: inherit;
  font-size: 1.2rem;
  line-height: 1.35;
  letter-spacing: 0.08em;
}

.result__note {
  margin: 0 0 14px;
  font-size: 0.7rem;
  color: var(--text-dim);
}

/* -----------------------------------------------------------------------------
   Motion
   -------------------------------------------------------------------------- */

@keyframes pop {
  from { opacity: 0; transform: scale(0.96); }
  to   { opacity: 1; transform: scale(1); }
}

@keyframes shake {
  10%, 90% { transform: translateX(-2px); }
  20%, 80% { transform: translateX(4px); }
  30%, 50%, 70% { transform: translateX(-6px); }
  40%, 60% { transform: translateX(6px); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  .tile.is-selected { transform: none; }
}

/* Short screens (iPhone SE, or Safari with the toolbar expanded): trim the
   furniture rather than let the grid scroll out of reach. */
@media (max-height: 700px) {
  .app { gap: 8px; }
  .tagline { display: none; }
  .wordmark::after { margin-top: 6px; }
  .solved__group { min-height: 54px; padding: 8px 10px; }
  .status { min-height: 40px; }
}
