/* style.css — The 8th Legion. Mobile-first dark theme.
   Palette theory: everything hue-driven off --h; one slider reskins all. */

:root {
  --h: 197;                                /* blue-steel default (~55% of the hue slider) */
  --bg:        hsl(var(--h) 24% 9%);
  --panel:     hsl(var(--h) 22% 14%);
  --panel2:    hsl(var(--h) 20% 18%);
  --row:       hsl(var(--h) 18% 22%);
  --btn:       hsl(calc(var(--h) + 15) 28% 28%);
  --btn-hi:    hsl(calc(var(--h) + 15) 32% 36%);
  --text:      hsl(var(--h) 18% 92%);
  --dim:       hsl(var(--h) 12% 62%);
  --accent:    hsl(190 55% 62%);           /* faded cyan, fixed */
  --gold:      #c9a227;
  --danger:    hsl(8 65% 58%);
  --ok:        hsl(140 40% 55%);
  --sand:      #E4C88F;                    /* light army: warm sand */
  --sand-edge: #6E5233;
  --wood:      #3B2814;                    /* dark army: deep dark wood */
  --wood-edge: #E4C88F;
  --tile-a:    hsl(var(--h) 22% 89%);        /* board light tile: near-white, hue-tinted */
  --tile-b:    hsl(var(--h) 26% 34%);        /* board dark tile: deep hue slate */
  --corr:      hsl(var(--h) 16% 60%);      /* corridor squares */
  --edge:      hsl(var(--h) 20% 24%);
  --homeline:  #9aa0aa;                    /* grey home-rank demarcation */
}

/* stone_colors: pieces are immune to the theme hue; two fixed palettes */
body.stones-stone {
  --sand:      #D9D9DE;                    /* light army: light grey */
  --sand-edge: #54545C;
  --wood:      #3A3A42;                    /* dark army: dark grey */
  --wood-edge: #D9D9DE;
}

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

html, body {
  height: 100%;
  margin: 0;
  overscroll-behavior: none;
  touch-action: manipulation;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.35;
}

/* ---- App shell ---- */
#app { display: flex; flex-direction: column; height: 100%; height: 100dvh; } /* 100% fallback for older webviews */

#windows { flex: 1 1 auto; position: relative; min-height: 0; }

.window {
  position: absolute; inset: 0;
  overflow-y: auto;
  display: none;
  -webkit-overflow-scrolling: touch;
}
.window.active { display: flex; flex-direction: column; }

/* ---- Navbar ---- */
#navbar {
  flex: 0 0 56px;
  height: 56px;
  display: flex;
  flex-wrap: nowrap;
  background: var(--panel);
  border-top: 1px solid var(--edge);
}
#navbar .nav-btn {
  flex: 1 1 calc(100% / 7);
  min-width: 0;
  display: flex; align-items: center; justify-content: center;
  background: none; border: none; color: var(--dim);
  font: inherit; padding: 0; cursor: pointer;
  box-shadow: inset 0 -2px 0 transparent;
}
#navbar .nav-btn.active {
  color: var(--text);
  background: var(--btn-hi);
  box-shadow: inset 0 -2px 0 var(--accent);
}
#navbar .nav-btn svg { width: 24px; height: 24px; }

/* ---- Buttons & controls ---- */
.btn {
  border: none; border-radius: 10px;
  padding: 8px 10px;
  background: var(--btn); color: var(--text);
  font: inherit; white-space: nowrap; cursor: pointer;
  text-align: center;
}
.btn:active { transform: scale(.98); background: var(--btn-hi); }
.btn.active { background: var(--btn-hi); box-shadow: inset 0 -2px 0 var(--accent); }
.btn.wide { width: 100%; padding: 12px; font-weight: 700; }
.btn.gold { background: var(--gold); color: hsl(40 30% 12%); }
.btn.danger { background: var(--danger); color: #fff; }
.btn:disabled { opacity: .45; }

.seg { display: flex; gap: 6px; padding: 8px; }
.seg .btn { flex: 1 1 0; }

.panel {
  background: var(--panel);
  border-radius: 12px;
  padding: 10px;
  margin: 8px;
}
.panel h3 { margin: 0 0 8px; font-size: 14px; color: var(--dim); letter-spacing: .06em; text-transform: uppercase; }

.row {
  display: flex; align-items: center; gap: 8px;
  background: var(--row);
  border-radius: 10px;
  padding: 8px 10px;
  margin: 6px 0;
}
.row .grow { flex: 1 1 auto; min-width: 0; }
.row .right { margin-left: auto; }

input, select, output {
  font: inherit; color: var(--text);
  background: var(--panel2);
  border: 1px solid var(--edge);
  border-radius: 8px;
  padding: 6px 8px;
  min-width: 0;
}
select { min-width: 72px; }                 /* keep the chosen value visible on phones */
input[type="range"] { padding: 0; accent-color: var(--accent); border: none; background: transparent; }
input[type="checkbox"] { width: 20px; height: 20px; accent-color: var(--accent); }
label { color: var(--dim); }

/* ---- Singletons ---- */
.tooltip {
  position: fixed; z-index: 300;
  background: var(--panel); opacity: .9;
  border: 1px solid var(--edge); border-radius: 8px;
  padding: 6px 8px; font-size: 12px;
  max-width: 60vw; max-height: 20vh; overflow: hidden;
  pointer-events: none;
  transition: opacity .1s;
  color: var(--text);
}

.dropdown {
  position: fixed; z-index: 200;
  background: var(--panel);
  border: 1px solid var(--edge); border-radius: 8px;
  padding: 4px;
  max-width: 50vw; max-height: 40vh; overflow-y: auto;
  color: var(--text);
}
.dropdown .dd-item { display: block; width: 100%; text-align: left; border: none; background: none; color: var(--text); font: inherit; padding: 8px 10px; border-radius: 6px; white-space: nowrap; }
.dropdown .dd-item:active { background: var(--btn-hi); }

.toast {
  position: fixed; bottom: 70px; left: 50%; transform: translateX(-50%);
  z-index: 400;
  background: var(--panel2);
  border: 1px solid var(--gold); border-radius: 10px;
  padding: 8px 14px;
  color: var(--text);
  max-width: 80vw; text-align: center;
  box-shadow: 0 4px 14px rgba(0,0,0,.5);
}

.overlay {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center;
}
.overlay .sheet {
  background: var(--panel);
  border: 1px solid var(--edge); border-radius: 14px;
  padding: 14px;
  width: min(92vw, 420px);
  max-height: 84vh; max-height: 84dvh; overflow-y: auto; overflow-x: hidden;
  display: flex; flex-direction: column; gap: 8px;
}

[hidden] { display: none !important; }

/* ---- Battle window ---- */
#window-battle { overflow-x: hidden; }
#window-battle > * { flex: 0 0 auto; }      /* never squash: scroll instead */
#board-host { position: relative; width: calc(100% + 12px); margin: 0 -6px; } /* board gets less margin -> slightly larger */
svg#board { width: 100%; height: auto; max-height: 55dvh; display: block; touch-action: none; }

.menubar {
  display: flex; gap: 6px; padding: 8px; overflow-x: auto;
  position: sticky; top: 0; z-index: 6;
  background: var(--panel); border-bottom: 1px solid var(--edge);
}
.menubar .btn { flex: 0 0 auto; }

.nameplate {
  display: flex; align-items: center; gap: 8px;
  background: var(--panel);
  border-radius: 10px; margin: 6px 8px; padding: 6px 10px;
}
.nameplate input { flex: 1 1 auto; background: transparent; border: none; padding: 4px; }
.nameplate .neo-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--dim); flex: 0 0 auto; }
.nameplate .neo-dot.on { background: var(--ok); box-shadow: 0 0 6px var(--ok); }

.tracker {
  align-self: center;
  display: flex; gap: 10px; align-items: center;
  background: var(--panel2);
  border: 1px solid var(--edge);
  border-radius: 999px;
  padding: 4px 14px; margin: 2px auto;
  font-size: 13px; color: var(--dim); white-space: nowrap;
}
.tracker b { color: var(--text); }

.bin {
  background: var(--panel);
  border-radius: 10px; padding: 4px 8px; margin: 3px 8px;
  min-height: 34px;
  display: flex; flex-wrap: wrap; gap: 6px; align-items: center;
  font-size: 12px; color: var(--dim);
}
.bin .stack { position: relative; display: inline-flex; align-items: center; }
.bin .stack .cnt {
  position: absolute; right: -4px; bottom: -3px;
  background: var(--panel2); border: 1px solid var(--edge); border-radius: 8px;
  font-size: 10px; line-height: 1; padding: 1px 4px; color: var(--text);
}

.collapse { margin: 6px 8px; background: var(--panel); border-radius: 10px; }
.collapse > .collapse-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 10px; color: var(--dim); cursor: pointer; font-size: 14px;
}
.collapse > .collapse-head::after { content: "▸"; transition: transform .15s; color: var(--accent); }
.collapse.open > .collapse-head::after { content: "▾"; }
.collapse > .collapse-body { display: none; padding: 0 10px 10px; font-size: 13px; }
.collapse.open > .collapse-body { display: block; }

/* Setup overlay: three modular bars over a translucent board — no Commence;
   every tap applies live and the board stays visible behind. */
.overlay.see-through { background: rgba(4, 8, 14, .22); }
.sheet.bare {
  background: transparent; border: none; box-shadow: none;
  width: 100%; max-width: 560px; height: 100%; max-height: 100%;
  padding: 6px 10px; justify-content: space-between; pointer-events: none;
  overflow: hidden;
}
.setup-bar {
  pointer-events: auto;
  background: hsl(var(--h) 22% 14% / .93);
  border: 1px solid var(--edge); border-radius: 12px;
  padding: 8px; display: flex; flex-direction: column; gap: 6px;
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
  box-shadow: 0 4px 18px rgba(0,0,0,.45);
}
.setup-bar .side-label { text-align: center; color: var(--dim); font-size: 12px; letter-spacing: .08em; text-transform: uppercase; }
.setup-bar .formation-row { display: flex; justify-content: space-between; align-items: stretch; gap: 6px; }
.setup-bar .formation-row .form-btn { flex: 0 0 36%; display: flex; flex-direction: column; gap: 4px; align-items: center; padding: 8px 6px; }
.setup-bar .formation-row .form-mid { flex: 0 0 22%; display: flex; flex-direction: column; gap: 6px; align-items: center; justify-content: center; }
.setup-bar .formation-row .form-mid .btn { width: 100%; padding: 8px 4px; }
.setup-bar .form-btn svg, .setup-bar .form-mid svg { width: 24px; height: 24px; }
.setup-bar .size-row { display: flex; justify-content: space-between; gap: 6px; }
.setup-bar .size-row .btn {
  flex: 1 1 0; font-size: 26px; font-weight: 700; color: var(--accent);
  text-shadow: 0 2px 6px rgba(0,0,0,.6); padding: 4px 0;
}
.setup-hint { pointer-events: none; text-align: center; color: var(--text); font-size: 12px;
  text-shadow: 0 1px 4px rgba(0,0,0,.9); opacity: .85; }

/* ---- Generic shop / slots / bars (invest, genes, stats, lounge) ---- */
.shop-item {
  display: flex; align-items: center; gap: 10px;
  background: var(--row);
  border-radius: 10px; padding: 8px 10px; margin: 6px 0;
}
.shop-item .si-icon { flex: 0 0 32px; width: 32px; height: 32px; display: flex; align-items: center; justify-content: center; color: var(--gold); }
.shop-item .si-body { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; }
.shop-item .si-name { color: var(--text); font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.shop-item .si-fx { color: var(--dim); font-size: 12px; }
.shop-item .si-price { flex: 0 0 auto; color: var(--gold); font-weight: 700; white-space: nowrap; }
.shop-item.owned .si-price { color: var(--ok); }

.slot {
  border: 2px dashed var(--edge);
  border-radius: 10px;
  min-height: 48px;
  display: flex; align-items: center; justify-content: center;
  color: var(--dim); font-size: 12px;
  background: var(--panel2);
}
.slot.filled { border-style: solid; color: var(--text); }

.bars { display: flex; align-items: flex-end; gap: 4px; height: 90px; padding: 6px 2px 0; }
.bars .bar {
  flex: 1 1 0; min-height: 2px;
  background: var(--btn-hi);
  border-radius: 3px 3px 0 0;
}
.bars .bar.gold { background: var(--gold); }
.bars .bar.accent { background: var(--accent); }
.bar-labels { display: flex; gap: 4px; font-size: 10px; color: var(--dim); }
.bar-labels span { flex: 1 1 0; text-align: center; overflow: hidden; }

/* ---- Lounge ---- */
.lounge-stage {
  position: relative; width: calc(100% - 16px); margin: 8px;
  border-radius: 14px; overflow: hidden;
  border: 1px solid var(--edge);
  box-shadow: 0 6px 24px rgba(0,0,0,.4);
}
.lounge-stage svg { width: 100%; height: auto; display: block; }
.emperor { animation: bob 3.2s ease-in-out infinite; transform-origin: 50% 100%; }
@keyframes bob { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-2.5%); } }
.flame { animation: flick 1.6s ease-in-out infinite; transform-origin: 50% 100%; }
@keyframes flick { 0%,100% { transform: scaleY(1); opacity: .95; } 40% { transform: scaleY(1.14) skewX(-2deg); opacity: 1; } 70% { transform: scaleY(.92) skewX(2deg); opacity: .85; } }
.mote { animation: drift 9s linear infinite; }
@keyframes drift { 0% { transform: translateY(0); opacity: 0; } 15% { opacity: .5; } 80% { opacity: .35; } 100% { transform: translateY(-46px); opacity: 0; } }
.npc-mover { transition: transform .45s steps(5); } /* paper-mario hop between rest spots */
.tex-row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.tex-sw { background: var(--row); border: 2px solid var(--edge); border-radius: 8px; padding: 3px 5px; cursor: pointer; }
.tex-sw.on { border-color: var(--gold); box-shadow: 0 0 0 1px var(--gold); }

/* ---- Splash ---- */
#splash {
  position: fixed; inset: 0; z-index: 500;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  background: radial-gradient(120% 90% at 50% 20%, hsl(var(--h) 30% 20%), hsl(var(--h) 26% 8%));
  transition: opacity .45s ease; cursor: pointer;
}
#splash.hide { opacity: 0; pointer-events: none; }
#splash .sp-title { font-size: 30px; font-weight: 800; letter-spacing: .14em; color: var(--sand); text-shadow: 0 2px 14px rgba(0,0,0,.7); }
#splash .sp-sub { color: var(--dim); letter-spacing: .3em; font-size: 12px; text-transform: uppercase; }
#splash .sp-ring {
  width: 92px; height: 92px; border-radius: 50%;
  border: 2px solid var(--gold);
  display: flex; align-items: center; justify-content: center;
  color: var(--gold); font-size: 34px; font-weight: 800;
  box-shadow: 0 0 30px hsl(45 80% 50% / .25), inset 0 0 18px hsl(45 80% 50% / .15);
  animation: spPulse 2.2s ease-in-out infinite;
}
@keyframes spPulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.05); } }

/* ---- tiny disclosure button (#16) ---- */
.tiny-btn {
  display: block; margin: 10px auto 2px; padding: 4px 10px;
  background: none; border: none; color: var(--dim);
  font-size: 10px; letter-spacing: .08em; opacity: .5; cursor: pointer;
}
.tiny-btn:active { opacity: 1; }

/* ---- Small helpers ---- */
.dim { color: var(--dim); }
.gold { color: var(--gold); }
.ok { color: var(--ok); }
.danger-text { color: var(--danger); }
.center { text-align: center; }
.spacer { flex: 1 1 auto; }
.hr { height: 1px; background: var(--edge); margin: 8px 0; border: none; }

/* ---- Stat charts: compact horizontal bars, vertical bars, pies, lines ---- */
.hbar { display: flex; align-items: center; gap: 10px; margin: 7px 0; font-size: 12px; }
.hbar .lbl { flex: 0 0 88px; color: var(--dim); text-align: right; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar .trk { flex: 1 1 auto; height: 11px; background: var(--row); border-radius: 5px; overflow: hidden; display: flex; flex-direction: column; justify-content: center; gap: 2px; padding: 1px 0; }
.hbar .fil { display: block; height: 2.5px; background: var(--sand); border-radius: 2px; min-width: 0; transition: width .3s; }
.hbar .fil.b { background: var(--gold); }
.hbar .val { flex: 0 0 auto; min-width: 44px; text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; background: var(--row); border-radius: 6px; padding: 1px 6px; }
.hbar .val .b, .hbar .val.b { color: var(--gold); font-weight: 400; }

.vbars { display: flex; align-items: flex-end; gap: 6px; height: 110px; padding: 6px 2px 2px; }
.vbars .vb { flex: 1 1 0; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; gap: 3px; height: 100%; min-width: 0; }
.vbars .vb i { display: block; width: 72%; background: var(--accent); border-radius: 3px 3px 0 0; min-height: 2px; }
.vbars .vb span { font-size: 9px; color: var(--dim); max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.vbars .vb b { font-size: 10px; }

.pie-wrap { display: flex; align-items: center; gap: 14px; padding: 4px 0; }
.pie { width: 108px; height: 108px; border-radius: 50%; flex: 0 0 auto; box-shadow: inset 0 0 0 2px rgba(0,0,0,.25); }
.pie-legend { font-size: 12px; line-height: 1.7; }
.pie-legend .sw { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; }
.lchart { width: 100%; height: auto; display: block; }
.stat-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.stat-table td, .stat-table th { padding: 3px 6px; border-bottom: 1px solid var(--row); text-align: right; }
.stat-table td:first-child, .stat-table th:first-child { text-align: left; color: var(--dim); }

/* ---- Tournament bracket: horizontal snap-scroll columns (mobile pattern) ---- */
.bracket { display: flex; gap: 16px; overflow-x: auto; padding: 8px 2px 10px; scroll-snap-type: x proximity; -webkit-overflow-scrolling: touch; }
.br-round { flex: 0 0 auto; min-width: 118px; scroll-snap-align: start; display: flex; flex-direction: column; justify-content: flex-start; gap: 5px; }
.br-round h4 { margin: 0 0 2px; font-size: 10px; color: var(--dim); text-transform: uppercase; letter-spacing: .08em; text-align: center; }
.br-match { background: var(--row); border-radius: 7px; padding: 4px 7px; font-size: 10.5px; position: relative; }
.br-match::after { content: ""; position: absolute; right: -9px; top: 50%; width: 9px; height: 1px; background: var(--edge); }
.br-round:last-child .br-match::after { display: none; }
.br-match .p { display: flex; justify-content: space-between; gap: 6px; padding: 1px 0; white-space: nowrap; }
.br-match .p .nm { overflow: hidden; text-overflow: ellipsis; max-width: 86px; }
.br-match .p.winner { color: var(--gold); font-weight: 700; }
.br-match .p .sc { color: var(--dim); font-variant-numeric: tabular-nums; }
.br-match .p.winner .sc { color: var(--gold); }
.br-hint { font-size: 11px; color: var(--dim); text-align: center; margin: 2px 0 0; }

/* ---- Game log: bracketed record + copy ---- */
.log-head { display: flex; align-items: center; gap: 6px; }
.log-turn { font-size: 12px; line-height: 1.6; margin: 3px 0; }
.log-turn .tno { color: var(--dim); font-variant-numeric: tabular-nums; margin-right: 5px; }
.log-turn .side-l { color: var(--sand); }
.log-turn .side-d { color: var(--accent); }
.log-turn code { background: var(--row); border-radius: 5px; padding: 1px 5px; margin: 1px 2px; display: inline-block; font-size: 11px; }

/* ---- Attack FX overlays (drawn on top of the board svg) ---- */
#fx-layer { position: absolute; inset: 0; pointer-events: none; z-index: 3; }
#board-host .fx-arrow { position: absolute; pointer-events: none; opacity: .3; color: var(--danger); }
#board-host .fx-x { position: absolute; pointer-events: none; opacity: .3; color: var(--danger); font-weight: 800; line-height: 1; text-align: center; }

/* ---- Board focus mode (#focus): tracker button expands the board ---- */
.focus-btn { flex: 0 0 auto; padding: 2px 7px; border-radius: 999px; line-height: 0; }
.focus-btn svg { width: 16px; height: 16px; vertical-align: middle; }

body.board-focus { overflow: hidden; }
/* desktop: a 40% dark-grey box over everything not game-related */
body.board-focus::after {
  content: ""; position: fixed; inset: 0; z-index: 150;
  background: rgba(66, 66, 72, .4); pointer-events: none;
}
body.board-focus #window-battle { position: relative; z-index: 160; }
body.board-focus #window-battle .menubar,
body.board-focus #window-battle .collapse { display: none; }
body.board-focus svg#board { max-height: none; }
/* tablet: immersive like phone but keeps the nameplates (room to spare) */
body.board-focus.tablet #window-battle { position: fixed; inset: 0; background: var(--bg); overflow-y: auto; }
/* phone: only bins + remaining actions + escape + board; everything else gone */
body.board-focus.phone::after { display: none; }
body.board-focus.phone #window-battle { position: fixed; inset: 0; background: var(--bg); overflow-y: auto; }
body.board-focus.phone #window-battle .nameplate { display: none; }
body.board-focus.phone svg#board { max-height: calc(100vh - 220px); max-height: calc(100dvh - 220px); }
body.board-focus.tablet svg#board { max-height: calc(100vh - 260px); max-height: calc(100dvh - 260px); }

/* ---- Lesson pill: gentle nudge under the tracker until you raise an engine ---- */
.lesson-pill {
  align-self: center; display: flex; gap: 8px; align-items: center;
  background: hsl(var(--h) 30% 20%); border: 1px solid var(--accent);
  border-radius: 999px; padding: 3px 6px 3px 12px; margin: 2px auto;
  font-size: 12px; color: var(--text); white-space: nowrap;
}
.lesson-pill .btn { padding: 3px 10px; border-radius: 999px; font-size: 12px; }

/* ---- Achievements / economy ---- */
.achv { display: flex; align-items: center; gap: 8px; background: var(--row); border-radius: 9px; padding: 7px 10px; margin: 6px 0; font-size: 13px; }
.achv .ic { flex: 0 0 auto; color: var(--gold); }
.achv .locked { opacity: .45; }
.hint-card { background: hsl(var(--h) 30% 20%); border: 1px solid var(--accent); border-radius: 10px; padding: 9px 12px; margin: 8px; font-size: 13px; }

/* ---- on-board turn controls: 60% opacity pair that appears once you've acted ---- */
.turn-float { position: absolute; left: 50%; bottom: 8px; transform: translateX(-50%); display: flex; gap: 8px; z-index: 4; }
.turn-float .tf-btn { opacity: .6; border-radius: 999px; padding: 6px 14px; font: inherit; font-size: 13px;
  background: var(--btn); color: var(--text); border: 1px solid var(--edge); cursor: pointer; }
.turn-float .tf-btn:hover, .turn-float .tf-btn:focus-visible { opacity: 1; }
.turn-float .tf-btn:disabled { opacity: .3; cursor: default; }
