:root {
  /* Surfaces */
  --bg: #0d0d10;
  --surface: #16161b;
  --surface-2: #1f1f27;
  --border: #2c2c36;
  --text: #f4f4f5;
  --text-dim: #b8b8c2;

  /* Tile states (all WCAG AA against their content color) */
  --tile-empty-bg: #16161b;
  --tile-empty-border: #3a3a47;

  --tile-filled-bg: #16161b;
  --tile-filled-border: #6c6c80;

  --tile-correct-bg: #2d7a3d;        /* white text -> 5.7:1 */
  --tile-correct-fg: #ffffff;

  --tile-present-bg: #c79018;        /* black text -> 6.6:1 */
  --tile-present-fg: #0a0a0a;

  --tile-absent-bg: #3d3d48;         /* white text -> 9.0:1 */
  --tile-absent-fg: #ffffff;

  --tile-danger-bg: #0a0a0a;         /* white text -> 19.5:1 */
  --tile-danger-fg: #ffffff;
  --tile-danger-border: #ff8c00;     /* orange on black -> 7.6:1 */

  /* Meter */
  --meter-low: #2d7a3d;
  --meter-mid: #c79018;
  --meter-high: #b8331b;
  --meter-trap: #ff8c00;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
}

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

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  background: var(--surface-2);
  color: var(--text);
  padding: .6rem 1rem;
  border-radius: 0 0 8px 0;
  z-index: 100;
}
.skip-link:focus { left: 0; outline: 3px solid var(--meter-trap); }

.topbar {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: .65rem 1rem;
  padding-top: max(.65rem, env(safe-area-inset-top));
  padding-left: max(1rem, env(safe-area-inset-left));
  padding-right: max(1rem, env(safe-area-inset-right));
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0;
  background: rgba(13,13,16,.92);
  backdrop-filter: blur(8px);
  z-index: 5;
}
.title {
  font-size: 1.25rem;
  letter-spacing: .04em;
  margin: 0;
  flex: 1;
  font-weight: 700;
}
.icon-btn, .btn {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .55rem .8rem;
  font-size: .9rem;
  cursor: pointer;
  font-weight: 600;
  /* Prevent iOS double-tap-zoom on rapid taps. */
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* Topbar buttons swap between full and compact labels by viewport width. */
.lbl-compact { display: none; }
@media (max-width: 480px) {
  .lbl-full    { display: none; }
  .lbl-compact { display: inline; }
}
.icon-btn:hover, .btn:hover { background: #2a2a35; }
.icon-btn:focus-visible, .btn:focus-visible {
  outline: 3px solid var(--meter-trap);
  outline-offset: 2px;
}
.btn.primary { background: var(--tile-correct-bg); border-color: transparent; }
.btn.primary:hover { background: #34924a; }

.howto {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 1rem;
}
.howto h2 { margin: 0 0 .5rem 0; font-size: 1rem; }
.howto ul { margin: 0; padding-left: 1.1rem; line-height: 1.55; color: var(--text-dim); }
.howto strong { color: var(--text); }
.legend {
  display: inline-block;
  padding: .05rem .4rem;
  border-radius: 4px;
  font-weight: 700;
  margin: 0 .1rem;
  font-size: .85em;
}
.legend.correct { background: var(--tile-correct-bg); color: var(--tile-correct-fg); }
.legend.present { background: var(--tile-present-bg); color: var(--tile-present-fg); }
.legend.absent  { background: var(--tile-absent-bg);  color: var(--tile-absent-fg); }

.game {
  max-width: 480px;
  margin: 0 auto;
  padding: 1rem .75rem 1.25rem;
  padding-bottom: max(1.25rem, calc(env(safe-area-inset-bottom) + .5rem));
  padding-left: max(.75rem, env(safe-area-inset-left));
  padding-right: max(.75rem, env(safe-area-inset-right));
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* Meter */
.meter { display: flex; flex-direction: column; gap: .35rem; }
.meter-row {
  display: flex; justify-content: space-between; align-items: baseline;
  font-size: .85rem; color: var(--text-dim);
  letter-spacing: .05em; text-transform: uppercase;
}
.meter-label { font-weight: 700; }
.meter-value { font-variant-numeric: tabular-nums; color: var(--text); }
.meter-bar {
  height: 12px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  overflow: hidden;
}
.meter-fill {
  height: 100%;
  width: 0%;
  background: var(--meter-low);
  transition: width 140ms ease, background-color 140ms ease;
}
.meter-fill.mid  { background: var(--meter-mid); }
.meter-fill.high { background: var(--meter-high); }
.meter-fill.trap {
  background: var(--meter-trap);
  animation: pulse 700ms ease-in-out infinite alternate;
}
@keyframes pulse {
  from { box-shadow: 0 0 0 0 rgba(255,140,0,.0); }
  to   { box-shadow: 0 0 12px 2px rgba(255,140,0,.55); }
}
@media (prefers-reduced-motion: reduce) {
  .meter-fill, .tile { transition: none !important; animation: none !important; }
}

/* Grid */
.grid {
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: 6px;
  width: 100%;
  aspect-ratio: 5 / 6;
  max-height: 56vh;
}
.row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}
.tile {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: clamp(1.4rem, 7vw, 2rem);
  text-transform: uppercase;
  background: var(--tile-empty-bg);
  color: var(--text);
  border: 2px solid var(--tile-empty-border);
  border-radius: 8px;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  transition: transform 140ms ease, background-color 200ms ease, border-color 200ms ease;
}
.tile.filled {
  border-color: var(--tile-filled-border);
  animation: pop 120ms ease-out;
}
@keyframes pop { 0% { transform: scale(.9); } 100% { transform: scale(1); } }

/* Per-tile danger styling intentionally disabled.
 * Letters in trap-not-in-target render visually identical to 'absent'
 * to avoid leaking the trap's letter set. The proximity signal lives
 * at the row level (.row.trap-proximity) and the danger meter. */
.tile.correct { background: var(--tile-correct-bg); color: var(--tile-correct-fg); border-color: var(--tile-correct-bg); }
.tile.present { background: var(--tile-present-bg); color: var(--tile-present-fg); border-color: var(--tile-present-bg); }
.tile.absent,
.tile.danger  { background: var(--tile-absent-bg);  color: var(--tile-absent-fg);  border-color: var(--tile-absent-bg); }

/* Row-level trap proximity warning (default mode only).
 * Fires when the submitted guess overlapped with the trap in a way
 * that pushed the player toward it — without revealing which letters. */
.row.trap-proximity {
  position: relative;
  box-shadow: 0 0 0 2px var(--tile-danger-border), 0 0 14px rgba(255,140,0,.35);
  border-radius: 10px;
}
.row.trap-proximity::after {
  content: "⚠ trap";
  position: absolute;
  right: -4px;
  top: -10px;
  background: var(--tile-danger-bg);
  color: var(--tile-danger-border);
  border: 1px solid var(--tile-danger-border);
  font-size: .65rem;
  font-weight: 700;
  letter-spacing: .08em;
  padding: 2px 6px;
  border-radius: 999px;
  text-transform: uppercase;
  pointer-events: none;
}

.row.invalid { animation: shake 320ms ease; }
@keyframes shake {
  0%,100% { transform: translateX(0); }
  20% { transform: translateX(-6px); } 40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); } 80% { transform: translateX(4px); }
}

.row.reveal .tile { animation: flip 500ms ease forwards; }
.row.reveal .tile:nth-child(1) { animation-delay: 0ms; }
.row.reveal .tile:nth-child(2) { animation-delay: 100ms; }
.row.reveal .tile:nth-child(3) { animation-delay: 200ms; }
.row.reveal .tile:nth-child(4) { animation-delay: 300ms; }
.row.reveal .tile:nth-child(5) { animation-delay: 400ms; }
@keyframes flip {
  0%   { transform: rotateX(0deg); }
  50%  { transform: rotateX(90deg); }
  100% { transform: rotateX(0deg); }
}

/* Status & alerts */
.status {
  min-height: 1.5rem;
  text-align: center;
  font-size: .95rem;
  color: var(--text-dim);
  letter-spacing: .02em;
}

/* Keyboard */
.keyboard { display: flex; flex-direction: column; gap: 6px; }
.kb-row { display: flex; gap: 6px; justify-content: center; }
.key {
  flex: 1 1 0;
  min-width: 0;
  height: 52px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-weight: 700;
  font-size: 1rem;
  text-transform: uppercase;
  cursor: pointer;
  padding: 0 .25rem;
  /* The big one: kill iOS double-tap-zoom on the keyboard. */
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}
.key.wide { flex: 1.6 1 0; font-size: .8rem; letter-spacing: .04em; }
.key:hover { background: #2a2a35; }
.key:focus-visible { outline: 3px solid var(--meter-trap); outline-offset: 2px; }
.key:active { transform: translateY(1px); }

.key.correct { background: var(--tile-correct-bg); color: var(--tile-correct-fg); border-color: var(--tile-correct-bg); }
.key.present { background: var(--tile-present-bg); color: var(--tile-present-fg); border-color: var(--tile-present-bg); }
/* Trap-only keys render as 'absent' so the keyboard never reveals the trap's letter set. */
.key.absent,
.key.danger  { background: var(--tile-absent-bg);  color: var(--tile-absent-fg);  border-color: var(--tile-absent-bg); }

/* Endgame dialog */
.endgame {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 12px;
  padding: 1.25rem;
  max-width: 360px;
  width: calc(100% - 2rem);
}
.endgame::backdrop { background: rgba(0,0,0,.6); }
.endgame h2 { margin: 0 0 .25rem 0; }
.endgame .reveal { color: var(--text-dim); }
.endgame .reveal strong { color: var(--text); letter-spacing: .12em; }
.endgame-actions {
  display: flex; gap: .5rem; margin-top: 1rem; justify-content: flex-end;
}

/* Mobile tweaks: phones in portrait. Compresses padding, gaps, and
 * tile/key sizes so the whole game fits without scrolling on iPhone. */
@media (max-width: 480px) {
  .topbar {
    gap: .45rem;
    padding: .6rem .85rem;
    padding-top: max(.5rem, env(safe-area-inset-top));
  }
  .title { font-size: 1.1rem; letter-spacing: .03em; }
  .icon-btn, .btn { padding: .45rem .65rem; font-size: .85rem; border-radius: 7px; }

  .game {
    padding: .8rem .65rem 1.1rem;
    padding-bottom: max(1.1rem, calc(env(safe-area-inset-bottom) + .45rem));
    gap: .85rem;
  }

  .meter-row { font-size: .78rem; }
  .meter-bar { height: 11px; }

  .grid {
    gap: 6px;
    /* Drop the aspect-ratio cap on small screens so the grid never
     * crowds out the keyboard. Use min() to bound by both width and
     * height. */
    aspect-ratio: auto;
    height: min(calc(100vw * 6 / 5 - 2rem), 52dvh);
  }
  .row { gap: 6px; }
  .tile { font-size: clamp(1.25rem, 7vw, 1.8rem); border-width: 2px; border-radius: 7px; }

  .keyboard { gap: 6px; }
  .kb-row { gap: 6px; }
  .key { height: 50px; font-size: 1rem; padding: 0 .2rem; border-radius: 7px; }
  .key.wide { font-size: .75rem; }

  .row.trap-proximity::after { font-size: .62rem; padding: 1px 6px; top: -9px; }
}

/* Very narrow phones (e.g. iPhone SE) — go a bit tighter */
@media (max-width: 360px) {
  .key { height: 44px; font-size: .9rem; }
  .tile { font-size: 1.2rem; }
  .icon-btn, .btn { padding: .4rem .5rem; font-size: .78rem; }
}

/* Short viewports (landscape phones, small windows): never let the
 * keyboard get pushed off-screen. */
@media (max-height: 700px) {
  .grid { max-height: 44dvh; }
  .key { height: 42px; }
}

/* Hard Mode: strip every trap-related signal except the loss condition.
 * No meter, no row-proximity warning, no preview hint. */
body.hard-mode .meter,
body.hard-mode .row.trap-proximity::after { display: none; }
body.hard-mode .row.trap-proximity { box-shadow: none; }

/* Hard-mode button visual state */
.icon-btn.hard-active {
  background: var(--meter-trap);
  color: #0a0a0a;
  border-color: var(--meter-trap);
}

/* High contrast / forced colors */
@media (forced-colors: active) {
  .tile, .key { forced-color-adjust: none; border: 2px solid CanvasText; }
  .tile.danger, .key.danger { outline: 3px solid Highlight; }
}
