/* ==========================================================================
   Kern It — dark, tool-like typography game
   Tokens first, then ambience, chrome, field, states.
   ========================================================================== */

:root {
  /* palette */
  --canvas: #05070a;
  --surface: #0b0d11;
  --elevated: #0e1116;
  --hairline: rgba(255, 255, 255, 0.08);
  --hairline-2: rgba(255, 255, 255, 0.16);
  --hairline-3: rgba(255, 255, 255, 0.28);
  --ink: #ffffff;
  --muted: rgba(255, 255, 255, 0.55);
  --muted-2: rgba(255, 255, 255, 0.5);
  --accent: #c7ff5b;
  --accent-hi: #d6ff87;
  --accent-ink: #0b0d11;
  --warm: #e8a87c;

  /* type */
  --font-sans: "Inter", "Inter Variable", "Segoe UI Variable Text", "Segoe UI", system-ui,
    -apple-system, "Helvetica Neue", Arial, sans-serif;
  --font-mono: "Geist Mono", "Cascadia Mono", Consolas, ui-monospace, "SFMono-Regular", Menlo,
    monospace;
  --track-eyebrow: 0.18em;

  /* geometry */
  --r-sm: 4px;
  --r-md: 10px;
  --r-pill: 999px;

  /* spacing — 4pt base, 8pt rhythm */
  --s-1: 4px;
  --s-2: 8px;
  --s-3: 12px;
  --s-4: 16px;
  --s-5: 24px;
  --s-6: 32px;
  --s-7: 48px;
  --s-8: 64px;

  /* layout */
  --gut: 56px;
  --hud-h: 64px;
  --foot-h: 64px;
  --glyph-size: clamp(96px, 12.5vw, 180px);
  --glyph-min: 72px;
  --glyph-max: 180px;
  --guide-bleed-top: 40px;
  --guide-bleed-bottom: 22px;
  --word-lift: -18px;
  --under-gap: 34px;

  --ease-out: cubic-bezier(0.2, 0.8, 0.2, 1);
}

@media (max-width: 899px) {
  :root {
    --gut: 20px;
    --hud-h: 52px;
    --foot-h: 44px;
    --glyph-size: clamp(96px, 26vw, 140px);
    --glyph-min: 44px;
    --glyph-max: 132px;
    --guide-bleed-top: 26px;
    --guide-bleed-bottom: 16px;
    --word-lift: -14px;
    --under-gap: 26px;
  }
}

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

[hidden] {
  display: none !important;
}

html {
  height: 100%;
}

body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--canvas);
  color: var(--ink);
  font-family: var(--font-sans);
  font-weight: 400;
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

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

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
  border-radius: var(--r-sm);
}

/* --------------------------------------------------------------------------
   Ambience — mesh 3–6%, grain, vignette .46
   -------------------------------------------------------------------------- */

.ambience {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
}

.ambience__mesh {
  position: absolute;
  inset: 0;
  opacity: 0.5;
  background:
    radial-gradient(46% 52% at 22% 18%, rgba(76, 96, 140, 0.14) 0%, rgba(5, 7, 10, 0) 72%),
    radial-gradient(42% 46% at 82% 78%, rgba(122, 108, 84, 0.1) 0%, rgba(5, 7, 10, 0) 70%),
    radial-gradient(70% 60% at 50% 120%, rgba(199, 255, 91, 0.035) 0%, rgba(5, 7, 10, 0) 70%);
}

.ambience__grain {
  position: absolute;
  inset: 0;
  opacity: 0.05;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  background-size: 180px 180px;
}

.ambience__vignette {
  position: absolute;
  inset: 0;
  opacity: 0.46;
  background: radial-gradient(
    ellipse 80% 80% at 50% 50%,
    rgba(5, 7, 10, 0) 38%,
    #000000 100%
  );
}

/* --------------------------------------------------------------------------
   Shell
   -------------------------------------------------------------------------- */

.app {
  position: fixed;
  inset: 0;
  z-index: 1;
  display: flex;
  flex-direction: column;
}

/* HUD — hairline separated, never boxed */
.hud {
  flex: 0 0 auto;
  height: var(--hud-h);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: var(--s-4);
  padding: 0 var(--gut);
  border-bottom: 1px solid var(--hairline);
}

.hud__side {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: var(--s-2);
}

.hud__side--end {
  justify-content: flex-end;
}

.hud__brand {
  text-align: center;
  white-space: nowrap;
}

.eyebrow {
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 400;
  letter-spacing: var(--track-eyebrow);
  text-transform: uppercase;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.eyebrow--ink {
  color: var(--ink);
}

/* Timer — 2px track, linear drain */
.timer {
  flex: 0 0 auto;
  position: relative;
  height: 2px;
  background: var(--hairline);
  overflow: hidden;
}

.timer__fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: 100%;
  background: var(--ink);
  transform-origin: left center;
}

.timer--urgent .timer__fill {
  background: var(--accent);
}

/* --------------------------------------------------------------------------
   Field
   -------------------------------------------------------------------------- */

.field {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
}

.rule {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 1px;
  background: var(--hairline);
  pointer-events: none;
}

.rule--l {
  left: 200px;
}

.rule--r {
  right: 200px;
}

/* outer-margin readings */
.metas {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--gut);
  pointer-events: none;
  transition: opacity 200ms ease-out;
}

.meta {
  display: flex;
  flex-direction: column;
  gap: var(--s-1);
  width: 168px;
}

.meta--dev {
  align-items: flex-start;
}

.meta--tol {
  align-items: flex-end;
  text-align: right;
}

.meta__value {
  font-family: var(--font-mono);
  font-size: 30px;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}

.meta__label,
.meta__unit {
  font-size: 10px;
}

/* stage */
.stage {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--under-gap);
  transition: opacity 240ms ease-out;
}

.pair {
  position: relative;
  display: flex;
  align-items: flex-start;
  font-kerning: none;
  font-variant-ligatures: none;
  transform: translate(var(--pair-shift, 0px), var(--pair-lift, 0px));
  transition: transform 320ms var(--ease-out);
}

/* the kern is pushed up as its verdict lands underneath it */
[data-phase="reveal"] .pair {
  --pair-lift: var(--word-lift);
}

.glyph {
  display: block;
  font-family: var(--font-sans);
  font-weight: 800;
  font-size: var(--glyph-size);
  line-height: 0.95;
  letter-spacing: 0;
  color: var(--ink);
  user-select: none;
  -webkit-user-select: none;
}

/* the word: everything up to the kerned pair, then the tail that moves */
.word {
  display: flex;
  align-items: flex-start;
}

.word--tail {
  translate: var(--offset-px, 0px) 0;
  transform: translateY(0);
  transition: transform 120ms ease-out;
  cursor: ew-resize;
  touch-action: none;
  background: none;
  border: 0;
  padding: 0;
  margin: 0;
  -webkit-tap-highlight-color: transparent;
}

[data-dragging="true"] .word--tail {
  transform: translateY(-2px);
}

/* the draggable letter gets a boxed ring: it reads as "this is the thing you move" */
.word--tail:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 6px;
  border-radius: 6px;
}

.is-dragging,
.is-dragging * {
  cursor: ew-resize !important;
}

/* gap guides — only while dragging (then held through the freeze + reveal) */
.guides {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.guide {
  position: absolute;
  top: calc(var(--guide-bleed-top) * -1);
  height: calc(100% + var(--guide-bleed-top) + var(--guide-bleed-bottom));
  width: 1px;
  background: var(--hairline-3);
  opacity: 0;
  transition: opacity 180ms ease-out;
}

.guide--a {
  left: var(--guide-a-x, 50%);
}

.guide--b {
  left: var(--guide-b-x, 50%);
}

[data-dragging="true"] .guide,
[data-phase="freeze"] .guide,
[data-phase="reveal"] .guide {
  opacity: 1;
}

[data-outcome="perfect"] .guide {
  background: var(--accent);
  opacity: 1;
  animation: guide-snap 90ms ease-out both;
}

@keyframes guide-snap {
  from {
    opacity: 0.45;
  }
  to {
    opacity: 1;
  }
}

/* warm deviation zone — miss only, never red */
.zone {
  position: absolute;
  top: calc(var(--guide-bleed-top) * -1);
  height: calc(100% + var(--guide-bleed-top) + var(--guide-bleed-bottom));
  left: var(--zone-x, 0px);
  width: var(--zone-w, 0px);
  background: rgba(232, 168, 124, 0.18);
  border-left: 1px solid rgba(232, 168, 124, 0.42);
  border-right: 1px solid rgba(232, 168, 124, 0.42);
  opacity: 0;
  transition: opacity 200ms ease-out;
  pointer-events: none;
}

[data-phase="reveal"][data-outcome="miss"] .zone {
  opacity: 1;
}

/* the result hangs off the kern itself, not a separate block */
.reveal {
  position: absolute;
  top: calc(100% + var(--under-gap));
  left: 50%;
  transform: translateX(calc(-50% - var(--pair-shift, 0px)));
  width: max-content;
  max-width: calc(100vw - var(--gut) * 2);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--s-3);
}

/* slot under the pair: LOCK IN while playing, the verdict once locked */
.stage__slot {
  position: relative;
  display: grid;
  grid-template-rows: 0fr;
  justify-items: center;
  width: 100%;
  min-height: 66px;
  transition: grid-template-rows 420ms var(--ease-out);
}

.slot__lock {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  transition: opacity 200ms ease-out;
}

.slot__result {
  min-height: 0;
  overflow: hidden;
  width: 100%;
}

.result__inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  opacity: 0;
  transform: translateY(6px);
  transition: opacity 260ms var(--ease-out) 120ms, transform 420ms var(--ease-out);
}

.result {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: var(--s-4);
  text-align: center;
}

/* the slot grows, which is what pushes the word up */
.app[data-screen="round"] .stage__slot,
.app[data-screen="over"] .stage__slot {
  grid-template-rows: 1fr;
}

.app[data-screen="round"] .result--round,
.app[data-screen="over"] .result--over {
  display: flex;
}

.app[data-screen="round"] .result__inner,
.app[data-screen="over"] .result__inner {
  opacity: 1;
  transform: none;
}

.app[data-phase="freeze"] .slot__lock,
.app[data-phase="reveal"] .slot__lock,
.app[data-screen="round"] .slot__lock,
.app[data-screen="over"] .slot__lock {
  opacity: 0;
  pointer-events: none;
  visibility: hidden;
}

.reveal__row {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  width: 100%;
  gap: var(--s-4);
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-radius: var(--r-pill);
  background: var(--elevated);
  box-shadow: inset 0 0 0 1px var(--hairline);
  white-space: nowrap;
}

.chip__label {
  font-size: 10px;
}

.chip__value {
  font-family: var(--font-mono);
  font-size: 14px;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}

.chip--perfect {
  background: var(--accent);
  color: var(--accent-ink);
  box-shadow: none;
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: var(--track-eyebrow);
  text-transform: uppercase;
  padding: 6px 12px;
  animation: chip-snap 90ms ease-out both;
}

.chip--best {
  background: var(--elevated);
  color: var(--ink);
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: var(--track-eyebrow);
  text-transform: uppercase;
  padding: 8px 14px;
}

.chip--best[data-beat="false"] {
  color: var(--muted);
}

@keyframes chip-snap {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.delta {
  display: inline-flex;
  align-items: center;
  gap: var(--s-2);
  color: var(--muted);
}

.delta__arrow--flip {
  transform: scaleX(-1);
}

.delta__value {
  font-family: var(--font-mono);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}

/* the two chips slide together */
[data-phase="reveal"] .chip--yours {
  animation: slide-in-left 400ms var(--ease-out) both;
}

[data-phase="reveal"] .chip--metric {
  animation: slide-in-right 400ms var(--ease-out) both;
}

[data-phase="reveal"] .delta {
  animation: fade-in 400ms var(--ease-out) 160ms both;
}

@keyframes slide-in-left {
  from {
    transform: translateX(-112px);
    opacity: 0;
  }
  to {
    transform: none;
    opacity: 1;
  }
}

@keyframes slide-in-right {
  from {
    transform: translateX(112px);
    opacity: 0;
  }
  to {
    transform: none;
    opacity: 1;
  }
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

/* --------------------------------------------------------------------------
   Buttons
   -------------------------------------------------------------------------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: var(--s-4) 36px;
  border: 0;
  border-radius: var(--r-sm);
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 500;
  letter-spacing: var(--track-eyebrow);
  text-transform: uppercase;
  cursor: pointer;
  transition: background-color 180ms ease-out, color 180ms ease-out,
    box-shadow 180ms ease-out;
}

.btn--accent {
  background: var(--accent);
  color: var(--accent-ink);
}

.btn--accent:hover {
  background: var(--accent-hi);
}

.btn--ghost {
  background: transparent;
  color: var(--ink);
  box-shadow: inset 0 0 0 1px var(--hairline-2);
}

.btn--ghost:hover {
  background: rgba(255, 255, 255, 0.04);
  box-shadow: inset 0 0 0 1px var(--hairline-3);
}

/* tertiary action: reads as text until you reach for it */
.btn--quiet {
  min-height: 32px;
  padding: var(--s-2) var(--s-4);
  background: transparent;
  color: var(--muted);
  box-shadow: none;
  font-weight: 400;
}

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

.result__actions {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--s-2);
}

.mini {
  padding: 2px 0;
  background: none;
  border: 0;
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: var(--track-eyebrow);
  text-transform: uppercase;
  cursor: pointer;
  transition: color 180ms ease-out;
}

.mini:hover,
.mini[aria-pressed="true"] {
  color: var(--ink);
}

/* --------------------------------------------------------------------------
   Screens — idle / round complete / game over
   -------------------------------------------------------------------------- */

.screen {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: none;
  place-items: center;
  padding: var(--s-6) var(--gut);
}

.panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.panel--idle {
  gap: var(--s-4);
}

.display {
  margin: 0;
  font-family: var(--font-sans);
  font-weight: 800;
  font-size: clamp(48px, 13vw, 144px);
  line-height: 0.95;
  letter-spacing: -0.03em;
}

.display--num {
  font-family: var(--font-mono);
  font-weight: 500;
  font-size: clamp(44px, 6.5vw, 84px);
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}

.sub {
  margin: 0;
  max-width: 34ch;
  font-size: 18px;
  color: var(--muted);
}

.howto {
  margin: 0;
  max-width: 62ch;
  font-family: var(--font-mono);
  font-size: 12px;
  letter-spacing: 0.02em;
  color: var(--muted);
}

.divider {
  width: 64px;
  height: 1px;
  background: var(--hairline);
}

.mono-note {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: var(--track-eyebrow);
  text-transform: uppercase;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.mono-note--line {
  text-transform: none;
  letter-spacing: 0.02em;
  color: var(--muted);
}

.result__hint {
  font-size: 10px;
  color: var(--muted-2);
}

.ticks {
  display: flex;
  align-items: flex-end;
  gap: var(--s-2);
  height: 24px;
}

.tick {
  width: 2px;
  height: 14px;
  background: var(--hairline-2);
  border-radius: 1px;
}

.tick--current {
  background: var(--muted-2);
}

.tick--scored {
  height: 20px;
  background: rgba(255, 255, 255, 0.72);
}

.tick--perfect {
  height: 24px;
  background: var(--accent);
}

/* --------------------------------------------------------------------------
   Footer
   -------------------------------------------------------------------------- */

.foot {
  flex: 0 0 auto;
  height: var(--foot-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-4);
  padding: 0 var(--gut);
  border-top: 1px solid var(--hairline);
}

.foot__right {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: var(--s-4);
}

.foot .eyebrow {
  font-size: 11px;
  white-space: nowrap;
}

.foot__left {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.foot__left--prose {
  text-transform: none;
}

.mini {
  white-space: nowrap;
}

/* --------------------------------------------------------------------------
   Screen states
   -------------------------------------------------------------------------- */

.app[data-screen="idle"] .metas,
.app[data-screen="idle"] .stage {
  opacity: 0;
  pointer-events: none;
}

.app[data-screen="idle"] .stage {
  visibility: hidden;
}

.app[data-screen="idle"] .screen--idle {
  display: grid;
}

.app[data-screen="round"] .metas,
.app[data-screen="over"] .metas {
  opacity: 0;
}

/* --------------------------------------------------------------------------
   Compact layout
   -------------------------------------------------------------------------- */

@media (max-width: 899px) {
  .rule {
    display: none;
  }

  .field {
    display: flex;
    flex-direction: column;
  }

  .metas {
    position: static;
    order: 0;
    padding: var(--s-3) var(--gut);
    border-bottom: 1px solid var(--hairline);
  }

  .meta {
    width: auto;
    gap: 2px;
  }

  .meta__value {
    font-size: 22px;
  }

  .meta__label,
  .meta__unit {
    font-size: 9px;
  }

  .stage {
    position: relative;
    inset: auto;
    flex: 1 1 auto;
    order: 1;
    padding: var(--s-5) var(--gut) var(--s-6);
  }

  .stage__slot {
    min-height: 60px;
  }

  .screen {
    padding: var(--s-5) var(--gut);
  }

  .panel--idle {
    gap: var(--s-3);
  }

  .sub {
    font-size: 15px;
  }

  .howto {
    font-size: 11px;
  }

  .btn {
    min-width: 200px;
  }

  .btn--quiet {
    min-width: 0;
  }

  .reveal__row {
    gap: var(--s-2);
  }

  .chip {
    gap: 6px;
    padding: 6px 9px;
  }

  .chip__label {
    font-size: 9px;
    letter-spacing: 0.12em;
  }

  .chip__value {
    font-size: 11px;
  }

  .delta {
    gap: 6px;
  }

  .delta__value {
    font-size: 11px;
  }

  .delta__arrow {
    width: 12px;
    height: 12px;
  }

  .foot__right {
    gap: var(--s-2);
  }

  .foot .eyebrow {
    font-size: 9px;
    letter-spacing: 0.12em;
  }

  .mini {
    font-size: 9px;
    letter-spacing: 0.12em;
  }
}

/* --------------------------------------------------------------------------
   Reduced motion — guides fade, nothing slides
   -------------------------------------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  .pair,
  .stage__slot,
  .result__inner,
  .slot__lock {
    transition: none;
  }

  [data-phase="reveal"] .pair {
    --pair-lift: 0px;
  }

  [data-phase="reveal"] .chip--yours,
  [data-phase="reveal"] .chip--metric,
  [data-phase="reveal"] .delta,
  .chip--perfect,
  [data-outcome="perfect"] .guide {
    animation: fade-in 180ms ease-out both;
  }

  .chip--perfect {
    animation: fade-in 180ms ease-out both;
  }

  [data-dragging="true"] .word--tail {
    transform: none;
  }
}
