/* ══════════════════════════════════════════════════════════════
   Route 81 — Dark Arcade Theme
   Deep navy base · Teal glow accents · Pixel-game typography
   ══════════════════════════════════════════════════════════════ */

:root {
  --bg-deep:      #08080a;
  --bg-1:         #0f0e12;
  --bg-2:         #141317;
  --ink:          #e5ddd3;
  --ink-soft:     #5e5c6b;
  --accent:       #cc3022;
  --accent-2:     #f59e0b;
  --line:         #1d1c22;
  --line-strong:  #292730;
  --card:         rgba(18, 17, 21, 0.97);
  --card-hover:   rgba(25, 24, 30, 0.99);
  --glass:        rgba(14, 13, 16, 0.88);
  --ok:           #3aaf75;
  --warn:         #f59e0b;
  --err:          #e9584a;
  --pixel-font:   "Silkscreen", "IBM Plex Mono", monospace;
  --glow-accent:  0 0 18px rgba(204, 48, 34, 0.32);
  --glow-ok:      0 0 14px rgba(58, 175, 117, 0.25);
  --glow-err:     0 0 14px rgba(233, 88, 74, 0.22);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "IBM Plex Mono", monospace;
  color: var(--ink);
  background:
    var(--bg-1);
  min-height: 100vh;
  background-attachment: fixed;
}

/* Atmospheric glow orbs */
.bg-orb {
  position: fixed;
  width: 30rem;
  height: 30rem;
  border-radius: 50%;
  filter: blur(70px);
  opacity: 0.12;
  pointer-events: none;
}
.orb-a { background: #cc3022; top: -12rem; left: -10rem; }
.orb-b { background: #f59e0b; right: -12rem; bottom: -14rem; }

/* ── Shell ───────────────────────────────────────────────── */
.app-shell {
  max-width: 1480px;
  margin: 0 auto;
  padding: 1.1rem clamp(0.6rem, 2vw, 1.5rem) 2rem;
  position: relative;
}

/* ── Header ──────────────────────────────────────────────── */
.app-header {
  display: grid;
  gap: 0.7rem;
  margin-bottom: 1rem;
}

.hero-copy { max-width: 800px; }

.eyebrow {
  font-family: var(--pixel-font);
  letter-spacing: 0.18em;
  text-transform: uppercase;
  font-size: 0.65rem;
  color: var(--accent);
  margin: 0 0 0.2rem;
  opacity: 0.75;
}

h1 {
  margin: 0 0 0.2rem;
  font-family: var(--pixel-font);
  font-size: clamp(1.8rem, 3.5vw, 3.2rem);
  letter-spacing: 0.02em;
  line-height: 1;
  background: #e04d40;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: none;
}

.subtitle {
  margin: 0;
  font-family: "IBM Plex Mono", monospace;
  font-size: 0.78rem;
  color: var(--ink-soft);
  letter-spacing: 0.04em;
}

/* ── Buttons ─────────────────────────────────────────────── */
input, select, button { font: inherit; }

input, select {
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  padding: 0.55rem 0.65rem;
  background: var(--card);
  color: var(--ink);
}

button {
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  padding: 0.56rem 1rem;
  font-family: var(--pixel-font);
  font-weight: 700;
  font-size: 0.82rem;
  letter-spacing: 0.04em;
  cursor: pointer;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.45);
  transition: background 120ms ease, transform 80ms ease, box-shadow 80ms ease;
}

button:hover {
  background: var(--accent-hi, var(--accent));
}

button:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.45);
}

button:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: 2px solid var(--focus, var(--accent));
  outline-offset: 2px;
}

button.ghost {
  background: var(--card);
  color: var(--ink);
  border: 2px solid var(--line-strong);
}

button.ghost:hover {
  background: var(--card-hover);
  border-color: var(--line-strong);
  color: var(--ink);
}

.player-actions {
  display: flex;
  gap: 0.55rem;
  flex-wrap: wrap;
}

/* ── Status row ──────────────────────────────────────────── */
.status-row {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.6rem;
  align-items: baseline;
  font-weight: 500;
  font-size: 0.82rem;
  background: var(--glass);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.45rem 0.8rem;
  font-family: "IBM Plex Mono", monospace;
}

.message { color: var(--ink-soft); }

/* ── Legend ──────────────────────────────────────────────── */
.legend {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
  margin: 0.3rem 0 0.75rem;
  color: var(--ink-soft);
  font-size: 0.78rem;
  font-family: "IBM Plex Mono", monospace;
}

.dot {
  display: inline-block;
  width: 0.55rem;
  height: 0.55rem;
  border-radius: 50%;
  margin-right: 0.3rem;
}
.dot.ok   { background: var(--ok); box-shadow: none; }
.dot.warn { background: var(--warn); }
.dot.err  { background: var(--err); }
.dot.hint { background: #56bab0; box-shadow: none; }

/* ── Board wrap ──────────────────────────────────────────── */
.board-wrap {
  overflow: auto;
  border: 1px solid var(--line-strong);
  border-radius: 18px;
  background:
    rgba(13, 28, 44, 0.95);
  padding: 0.55rem;
  box-shadow:
    0 24px 60px rgba(0, 0, 0, 0.6), inset 0 1px 0 rgba(204, 48, 34, 0.08), inset 0 0 0 1px rgba(204, 48, 34, 0.04);
}

/* ── Board grid ──────────────────────────────────────────── */
.board-grid {
  display: grid;
  --board-header-min: 102px;
  --board-header-max: 118px;
  --board-col-min: 102px;
  grid-template-columns: minmax(var(--board-header-min), var(--board-header-max)) repeat(var(--board-cols, 9), minmax(var(--board-col-min), 1fr));
  gap: 0.28rem;
  min-width: calc(var(--board-header-min) + var(--board-cols, 9) * var(--board-col-min));
}

/* ── Rule / row headers ──────────────────────────────────── */
.rule-head,
.row-head {
  background: rgba(0, 196, 176, 0.05);
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  padding: 0.3rem 0.18rem;   /* tighter horizontal so badge has more room */
  font-family: var(--pixel-font);
  font-weight: 700;
  font-size: 0.60rem;        /* was 0.72rem — "EARTHQUAKE" fits at 0.64rem */
  letter-spacing: 0.04em;
  display: grid;
  place-items: center;
  text-align: center;
  min-height: 2.8rem;
  line-height: 1.15;
  white-space: normal;
  overflow-wrap: break-word;   /* break only at spaces, never mid-character */
  word-break: normal;
  hyphens: none;
  color: var(--ink);
  transition: border-color 200ms ease, background 200ms ease;
  overflow: hidden;
}

.rule-head:hover,
.row-head:hover {
  border-color: rgba(204, 48, 34, 0.40);
  background: rgba(204, 48, 34, 0.09);
}

.rule-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.26rem 0.42rem;
  border-radius: 8px;
  border: 1px solid rgba(204, 48, 34, 0.22);
  background: rgba(204, 48, 34, 0.06);
  max-width: 100%;
}

.rule-badge.text {
  font-size: 0.78rem;
  line-height: 1.05;
  color: var(--ink);
  text-transform: uppercase;
}

/* ── Legacy type-badge img — hidden, no longer used ──────────────────── */
.type-badge { display: none !important; }

/* ── Custom type badge — pure CSS, no external images ───────────────────
   NOTE: background + color set via inline style.cssText in JS (belt+suspenders
   so the rule-badge class can never override the type color).                  */
.type-badge-custom {
  display: inline-flex !important;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 7px;
  /* bg/color come from inline style set in buildTypeBadge() */
  font-family: var(--pixel-font);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  white-space: nowrap;
  max-width: 100%;
  box-shadow: inset 0 -2px 0 rgba(0,0,0,0.25);
  border: none !important;
}

/* Pixel icon (type-icons.css). Sized in whole multiples of its 9px grid so
   each icon pixel lands on whole screen pixels and stays crisp. */
.tbi {
  width: 13.5px;
  height: 13.5px;
  flex-shrink: 0;
}

.tbn {
  font-family: var(--pixel-font);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  line-height: 1;
}

@media (max-width: 767px) {
  .type-badge-custom { padding: 3px 7px; gap: 4px; }
  .tbi { width: 9px; height: 9px; }
  .tbn { font-size: 0.62rem; }
}

.corner { background: transparent; border: 0; }

/* ── Cells ───────────────────────────────────────────────── */
.cell {
  overflow: hidden;           /* clip long names/sprites to cell boundary */
  border: 1px solid var(--line-strong);
  background: var(--card);
  border-radius: 10px;
  padding: 0.38rem;
  display: grid;
  gap: 0.2rem;
  align-content: start;
  aspect-ratio: 1 / 1;
  min-height: 104px;
  overflow: hidden;
  position: relative;
  transform-origin: center;
  transition: transform 140ms ease, box-shadow 140ms ease, border-color 140ms ease, background 140ms ease;
  animation: cell-rise 280ms ease both;
  animation-delay: calc(var(--cell-index, 0) * 7ms);
  cursor: pointer;
}

.cell:hover,
.cell:focus,
.cell:focus-within,
.cell.active {
  transform: scale(1.18);
  z-index: 8;
  border-color: var(--accent);
  background: var(--card-hover);
  box-shadow: var(--glow-accent), 0 12px 32px rgba(0, 0, 0, 0.5);
}

.cell.ok {
  background: rgba(58, 175, 117, 0.1);
  border-color: rgba(58, 175, 117, 0.35);
}

.cell.err {
  background: rgba(233, 88, 74, 0.08);
  border-color: rgba(233, 88, 74, 0.28);
}

/* Prevent locked cells from reopening editor */
.cell.locked .cell-editor,
.cell.locked:hover .cell-editor,
.cell.locked:focus-within .cell-editor,
.cell.locked.active .cell-editor {
  max-height: 0 !important;
  opacity: 0 !important;
  pointer-events: none !important;
}

.cell.locked:hover,
.cell.locked:focus,
.cell.locked.active {
  transform: scale(1.06);
  z-index: 4;
  box-shadow: var(--glow-ok);
}

/* ── Cell summary ────────────────────────────────────────── */
.cell-summary {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  align-content: start;
  align-items: center;
  height: 100%;
}

.summary-sprite {
  width: min(74%, 88px);
  aspect-ratio: 1 / 1;
  border-radius: 0;
  border: 0;
  object-fit: cover;
  background: transparent;
  filter: drop-shadow(0 6px 12px rgba(0, 0, 0, 0.5));
}

.summary-sprite.hidden { display: none; }

.qual-line {
  margin: 0;
  font-family: "IBM Plex Mono", monospace;
  font-size: 0.63rem;
  line-height: 1.2;
  color: var(--ink-soft);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.qual-stack {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}

.picked-name-row {
  margin: 0;
  font-family: var(--pixel-font);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.3rem;
  width: 100%;
}

.picked-name {
  margin: 0;
  font-size: clamp(0.58rem, 0.75vw, 0.82rem); /* slightly smaller to fit more chars */
  font-weight: 700;
  line-height: 1.2;
  color: var(--ink-soft);
  min-height: 0;           /* don't reserve space when empty */
  overflow: hidden;
  /* Allow wrapping so long names like "Kyogre Primal" don't overflow */
  white-space: normal;
  word-break: break-word;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  text-align: center;
  max-width: 100%;
}

.result-mark {
  display: inline-grid;
  place-items: center;
  width: 1.05rem;
  height: 1.05rem;
  border-radius: 50%;
  font-size: 0.72rem;
  font-weight: 800;
  color: transparent;
  border: 1px solid transparent;
  background: transparent;
  flex: 0 0 auto;
}

/* ── Cell editor ─────────────────────────────────────────── */
.cell-editor {
  display: grid;
  gap: 0.3rem;
  max-height: 0;
  opacity: 0;
  overflow: hidden;
  pointer-events: none;
  position: absolute;
  inset: 0.24rem;
  border-radius: 8px;
  background: rgba(10, 22, 36, 0.98);
  border: 1px solid rgba(204, 48, 34, 0.30);
  padding: 0.38rem;
  transition: max-height 140ms ease, opacity 140ms ease;
  box-shadow: none;
}

.cell:hover .cell-editor,
.cell:focus-within .cell-editor,
.cell.active .cell-editor {
  max-height: 220px;
  opacity: 1;
  pointer-events: auto;
}

.cell-actions {
  display: flex;
  gap: 0.35rem;
  width: 100%;
}

.ok-btn,
.clear-btn {
  flex: 1;
  min-width: 0;
  width: 100%;
  border-radius: 8px;
  font-size: 0.78rem;
  padding: 0.4rem 0.5rem;
}

.name-input {
  width: 100%;
  min-width: 0;
  padding: 0.38rem 0.5rem;
  font-size: 0.78rem;
  background: rgba(204, 48, 34, 0.06);
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  color: var(--ink);
}

.name-input:focus {
  border-color: var(--accent);
  outline: none;
  box-shadow: none;
}

.cell-status {
  margin: 0;
  font-size: 0.65rem;
  color: var(--ink-soft);
  line-height: 1.2;
  font-family: "IBM Plex Mono", monospace;
}

/* ── Cell state colours ──────────────────────────────────── */
.cell.ok  { border-color: rgba(58, 175, 117, 0.4); }
.cell.err { border-color: rgba(233, 88, 74, 0.35); }
.cell.warn{ border-color: rgba(245, 158, 11, 0.3);  }

.cell.ok  .picked-name { color: var(--ok); }
.cell.ok  .result-mark {
  color: var(--ok);
  border-color: rgba(58, 175, 117, 0.45);
  background: rgba(58, 175, 117, 0.12);
}

.cell.err .picked-name { color: var(--err); }
.cell.err .result-mark {
  color: var(--err);
  border-color: rgba(233, 88, 74, 0.4);
  background: rgba(233, 88, 74, 0.1);
}

/* Hint cell — cyan to distinguish from amber "not submitted" */
.cell.hint-cell {
  border-color: rgba(86, 186, 176, 0.45) !important;
  background: linear-gradient(160deg, rgba(86, 186, 176,0.07), rgba(13,24,38,0.97)) !important;
}
.cell.hint-cell .picked-name { color: #56bab0 !important; }

/* Board locked (game over) */
.board-wrap.board-locked {
  opacity: 0.4;
  pointer-events: none;
  filter: grayscale(0.5) brightness(0.7);
  transition: opacity 400ms ease, filter 400ms ease;
}

/* ── Animations ──────────────────────────────────────────── */
@keyframes cell-rise {
  from { opacity: 0; transform: translateY(7px) scale(0.97); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

/* ── Accessibility ───────────────────────────────────────── */
.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}


/* ════════════════════════════════════════════════════════════
   Mobile & Responsive — Full Overhaul
   ════════════════════════════════════════════════════════════ */

/* ── Board is always horizontally scrollable ─────────────── */
.board-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* ── Tablet (≤1024px) ────────────────────────────────────── */
@media (max-width: 1024px) {
  .board-grid {
    --board-header-min: 92px;
    --board-header-max: 105px;
    --board-col-min: 88px;
    grid-template-columns: minmax(var(--board-header-min), var(--board-header-max)) repeat(var(--board-cols, 9), minmax(var(--board-col-min), 1fr));
    min-width: calc(var(--board-header-min) + var(--board-cols, 9) * var(--board-col-min));
  }
}

/* ── Mobile (≤767px) — core overhaul ─────────────────────── */
@media (max-width: 767px) {

  /* ── Page shell ── */
  .app-shell { padding: 0.8rem 0.8rem 3rem; }
  h1 { font-size: clamp(1.7rem, 8vw, 2.8rem); }
  .eyebrow { font-size: 0.62rem; }
  .subtitle { font-size: 0.74rem; line-height: 1.5; margin-top: 0.3rem; }
  .player-actions { gap: 0.4rem; width: 100%; }
  .player-actions button { font-size: 0.8rem; padding: 0.55rem 0.9rem; flex: 1; }

  /* ── Status row ── */
  .status-row { font-size: 0.75rem; padding: 0.4rem 0.75rem; flex-wrap: wrap; gap: 0.25rem; }

  /* ── Legend ── */
  .legend { font-size: 0.72rem; gap: 0.6rem; flex-wrap: wrap; }

  /* ── Game alerts ── */
  .hud-alert { font-size: 0.82rem; padding: 0.5rem 0.75rem; }
  .hud-alert-icon { font-size: 1.25rem; }

  /* ── Board container ── */
  .board-wrap { padding: 0.3rem; border-radius: 12px; }

  /* ── Grid layout ──
     Sticky first column (row-heads) so users always know which row
     they are on while scrolling horizontally. */
  .board-grid {
    --board-header-min: 78px;
    --board-header-max: 88px;
    --board-col-min: 72px;
    grid-template-columns: minmax(var(--board-header-min), var(--board-header-max)) repeat(var(--board-cols, 9), minmax(var(--board-col-min), 1fr));
    gap: 0.17rem;
    min-width: calc(var(--board-header-min) + var(--board-cols, 9) * var(--board-col-min));
  }

  /* Column rule headers */
  .rule-head {
    font-size: 0.6rem;
    padding: 0.2rem 0.14rem;
    min-height: 3.4rem;
    border-radius: 8px;
    line-height: 1.25;
    overflow-wrap: break-word;  /* only break at spaces — no more "EVOLVE S:" */
    word-break: normal;
    hyphens: none;
    overflow: hidden;           /* clip content so nothing bleeds into adjacent cells */
  }

  /* Rule badge — block so it fills column width and text wraps properly */
  .rule-head .rule-badge,
  .row-head .rule-badge {
    display: block;           /* block not flex — text wraps naturally */
    width: 100%;
    text-align: center;
    box-sizing: border-box;
    overflow: visible;        /* let text breathe; clipped by cell/head container */
    padding: 0.15rem 0.08rem;
    white-space: normal;
    word-break: normal;
    overflow-wrap: break-word;
    hyphens: none;
  }

  .rule-head .rule-badge.text {
    font-size: 0.6rem;
    word-break: normal;
    overflow-wrap: break-word;
    hyphens: none;
    line-height: 1.25;
    text-align: center;
    white-space: normal;
  }

  /* Type badge images — constrain to column width so they don't overflow */
  .rule-head .type-badge {
    height: 20px;
    max-width: 90%;
    width: auto;
    object-fit: contain;
  }

  /* Row rule headers — sticky left, must have z-index ABOVE cell hover (z-index:8)
     so cells never bleed through the sticky column when scrolling */
  .row-head {
    font-size: 0.6rem;
    padding: 0.18rem 0.14rem;
    min-height: 3.4rem;
    border-radius: 8px;
    line-height: 1.25;
    overflow-wrap: break-word;
    word-break: normal;
    hyphens: none;
    overflow: hidden;
    position: sticky;
    left: 0;
    z-index: 30;                            /* above cell hover z-index: 8 */
    background: #09090a;                    /* fully opaque — no cell bleed-through */
    box-shadow: 3px 0 10px rgba(0,0,0,0.7); /* hard right-edge separator */
  }

  /* Corner cell — sticky, above row-heads */
  .rule-head.corner {
    position: sticky;
    left: 0;
    z-index: 31;
    background: #09090a;
    box-shadow: 3px 0 10px rgba(0,0,0,0.7);
    overflow: hidden;
  }

  /* ── Cells ── fixed height, no aspect-ratio, no hover scale on touch */
  .cell {
    aspect-ratio: unset;
    height: 84px;
    min-height: 84px;
    border-radius: 8px;
    padding: 0.28rem 0.22rem;
  }

  /* Disable hover scale on mobile — touch devices don't hover,
     and scale(1.18) + z-index:8 would render cells above sticky headers */
  .cell:hover, .cell:focus, .cell:focus-within, .cell.active {
    transform: none;
    z-index: 1;
    box-shadow: none;
  }
  .cell.locked:hover, .cell.locked:focus, .cell.locked.active {
    transform: none;
    z-index: 1;
  }

  /* Sprite in cells */
  .summary-sprite {
    width: 50px;
    height: 50px;
    min-width: unset;
    object-fit: contain;
  }

  /* Pokémon name in cells — keep readable */
  .picked-name { font-size: 0.7rem; }

  /* "Tap to enter" placeholder — visible but secondary */
  .cell:not(.ok):not(.hint-cell):not(.err):not(.drafted):not(.warn) .picked-name {
    font-size: 0.54rem;
    opacity: 0.42;
  }

  /* Rule labels (q-row / q-col) inside cells */
  .q-row { font-size: 0.64rem !important; }
  .q-col { font-size: 0.58rem !important; }
  .cell:hover .q-row, .cell:focus-within .q-row { font-size: 0.74rem !important; }
  .cell:hover .q-col, .cell:focus-within .q-col { font-size: 0.68rem !important; }
}

/* ── Small phones (≤420px) ───────────────────────────────── */
@media (max-width: 420px) {
  .board-grid {
    --board-header-min: 70px;
    --board-header-max: 78px;
    --board-col-min: 64px;
    grid-template-columns: minmax(var(--board-header-min), var(--board-header-max)) repeat(var(--board-cols, 9), minmax(var(--board-col-min), 1fr));
    gap: 0.13rem;
    min-width: calc(var(--board-header-min) + var(--board-cols, 9) * var(--board-col-min));
  }

  .cell { height: 76px; min-height: 76px; padding: 0.22rem 0.18rem; }
  .summary-sprite { width: 44px; height: 44px; }
  .picked-name { font-size: 0.64rem; }
  .rule-head, .row-head { font-size: 0.56rem; min-height: 3.0rem; overflow-wrap: break-word; word-break: normal; hyphens: none; }
}

/* ── Touch devices: tap target sizing ───────────────────── */
@media (pointer: coarse) {
  button { min-height: 44px; }
  .pip-hint {
    padding: 0.28rem;
    min-width: 38px;
    min-height: 38px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
}

/* ── Locked cells: hide rule labels (sprite+name are enough) ─ */
.cell.ok .qual-combo,
.cell.hint-cell .qual-combo {
  display: none;
}
/* Fix: cap name width inside cell so result-mark stays inside */
.picked-name-row .picked-name {
  flex: 1 1 0;
  min-width: 0;
}
.picked-name-row .result-mark { flex-shrink: 0; }