:root {
  --bg: #11161f;
  --panel: rgba(17, 23, 33, 0.94);
  --panel2: rgba(30, 38, 52, 0.96);
  --line: rgba(255, 255, 255, 0.12);
  --text: #eef2f7;
  --muted: #98a6b8;
  --accent: #f2c14e;
  --good: #5fe08a;
  --bad: #ff6b6b;
  --warn: #ffb547;
  --blue: #6fb6ff;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --top-h: 64px;
  --bottom-h: 110px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 14px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; overflow: hidden; overscroll-behavior: none; }
body { position: fixed; inset: 0; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
#game { position: absolute; inset: 0; display: block; touch-action: none; }
.hidden { display: none !important; }
button { font: inherit; color: var(--text); background: rgba(255,255,255,0.07); border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; cursor: pointer; touch-action: manipulation; }
button:active { background: rgba(255,255,255,0.18); }
button:disabled { opacity: 0.4; }
button.primary { background: #2f8f4a; border-color: #48c06a; }
button.danger { background: #8f2f2f; border-color: #c04848; }
button.on { background: #2e4766; border-color: var(--blue); }
.muted { color: var(--muted); }
.good { color: var(--good); } .bad { color: var(--bad); } .warn { color: var(--warn); }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 6px 24px rgba(0,0,0,0.35); }
img.ri { vertical-align: -2px; flex: 0 0 auto; }

/* ---------------------------------------------------------------- HUD */
#hud { position: absolute; inset: 0; pointer-events: none; }
#hud > * { pointer-events: auto; }
#top { position: absolute; top: 0; left: 0; right: 0; padding: calc(4px + var(--sat)) calc(6px + var(--sar)) 4px calc(6px + var(--sal)); background: linear-gradient(rgba(8,11,17,0.96), rgba(8,11,17,0.78)); display: grid; grid-template-columns: 1fr auto; grid-template-areas: "res right" "wave wave"; gap: 4px 6px; align-items: center; }
#res-bar { grid-area: res; display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; min-width: 0; cursor: pointer; }
#res-bar::-webkit-scrollbar { display: none; }
.chip { display: flex; align-items: center; gap: 3px; white-space: nowrap; background: rgba(0,0,0,0.38); border-radius: 8px; padding: 3px 6px; font-size: 13px; font-variant-numeric: tabular-nums; }
.chip small { color: var(--muted); font-size: 10px; }
.chip.bad b { color: var(--bad); } .chip.full b { color: var(--warn); }
#top-right { grid-area: right; display: flex; align-items: center; gap: 6px; }
#clock { text-align: center; white-space: nowrap; font-weight: 600; font-size: 13px; }
#daybar { width: 70px; height: 4px; background: rgba(255,255,255,0.14); border-radius: 2px; margin: 3px auto 0; overflow: hidden; }
#daybar div { height: 100%; width: 0; background: var(--accent); }
#speed { display: flex; gap: 2px; }
#speed button { padding: 4px 7px; font-size: 12px; border-radius: 6px; min-width: 30px; }
.ib { padding: 4px 10px; font-size: 16px; }
#wave-bar { grid-area: wave; justify-self: center; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; border-radius: 8px; padding: 2px 10px; background: rgba(20,30,45,0.9); border: 1px solid var(--line); color: var(--muted); }
#wave-bar.near { background: rgba(110,60,10,0.9); border-color: var(--warn); color: #ffe3b0; }
#wave-bar.live { background: rgba(120,20,20,0.92); border-color: var(--bad); color: #fff; }
#wave-bar:empty { display: none; }
#res-detail { position: absolute; top: calc(var(--top-h) + 4px); left: calc(6px + var(--sal)); width: min(420px, calc(100vw - 12px)); padding: 10px 12px; z-index: 5; }
#res-detail table { width: 100%; border-collapse: collapse; font-size: 13px; }
#res-detail td { padding: 3px 4px; border-bottom: 1px solid var(--line); }
#res-detail td:not(:first-child) { text-align: right; font-variant-numeric: tabular-nums; }

#toasts { position: absolute; top: calc(var(--top-h) + 6px); left: calc(70px + var(--sal)); right: calc(116px + var(--sar)); max-width: 460px; margin: 0 auto; display: flex; flex-direction: column; gap: 5px; pointer-events: none; z-index: 3; }
.toast { background: var(--panel); border: 1px solid var(--line); border-left: 4px solid var(--blue); border-radius: 8px; padding: 6px 9px; font-size: 13px; animation: tin 0.22s ease-out; transition: opacity 0.6s; }
.toast.warn { border-left-color: var(--warn); }
.toast.danger { border-left-color: var(--bad); background: rgba(84,16,16,0.94); }
.toast.good { border-left-color: var(--good); }
.toast.jump { pointer-events: auto; cursor: pointer; }
.toast.fade { opacity: 0; }
@keyframes tin { from { transform: translateY(-6px); opacity: 0; } }

#left-rail { position: absolute; left: calc(6px + var(--sal)); top: calc(var(--top-h) + 6px); display: flex; flex-direction: column; gap: 5px; z-index: 2; }
#right-rail { position: absolute; right: calc(6px + var(--sar)); top: calc(var(--top-h) + 6px); display: flex; flex-direction: column; align-items: flex-end; gap: 5px; z-index: 2; }
.rail { width: 54px; height: 50px; padding: 2px; display: flex; flex-direction: column; align-items: center; justify-content: center; background: var(--panel); line-height: 1; }
.rail span { font-size: 18px; } .rail small { font-size: 10px; color: var(--muted); margin-top: 3px; }
.rail.small { height: 42px; }
.rail.on { border-color: var(--accent); background: rgba(242,193,78,0.2); }
#btn-alert.hot { border-color: var(--bad); background: rgba(160,30,30,0.6); animation: pulse 1s infinite; }
@keyframes pulse { 50% { background: rgba(160,30,30,0.25); } }
#groups { display: flex; flex-direction: column; gap: 4px; }
#groups { display: grid; grid-template-columns: 1fr 1fr; gap: 3px; }
#groups button { width: auto; height: 28px; padding: 0; font-size: 12px; background: var(--panel); }
#groups button.set { border-color: var(--blue); }
#minimap { width: 110px; height: 110px; border: 1px solid var(--line); border-radius: 8px; background: #000; touch-action: none; }

#bottom { position: absolute; left: 0; right: 0; bottom: 0; padding: 0 calc(6px + var(--sar)) calc(6px + var(--sab)) calc(6px + var(--sal)); background: linear-gradient(rgba(8,11,17,0.35), rgba(8,11,17,0.96) 30%); }
#tabs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; padding: 4px 0; }
#tabs::-webkit-scrollbar, #tray::-webkit-scrollbar { display: none; }
#tabs button { flex: 0 0 auto; padding: 4px 10px; font-size: 12px; border-radius: 14px; }
#tabs button.on { background: rgba(242,193,78,0.22); border-color: var(--accent); }
#tray { display: flex; gap: 5px; overflow-x: auto; scrollbar-width: none; min-height: 72px; align-items: stretch; }
.card { flex: 0 0 auto; width: 74px; padding: 4px 3px; display: flex; flex-direction: column; align-items: center; justify-content: flex-start; gap: 1px; background: var(--panel); font-size: 10.5px; line-height: 1.15; position: relative; }
.card canvas.ic { width: 34px; height: 34px; }
.card .nm { font-weight: 600; font-size: 11px; text-align: center; }
.card .cs { color: var(--muted); font-size: 9.5px; text-align: center; }
.card.off { opacity: 0.42; }
.card.locked { opacity: 0.3; filter: grayscale(1); }
.card.on { border-color: var(--accent); background: rgba(242,193,78,0.2); }
.card .badge { position: absolute; top: 2px; right: 3px; font-size: 10px; background: var(--blue); color: #000; border-radius: 8px; padding: 0 4px; }
.cmd { flex: 0 0 auto; min-width: 64px; padding: 6px 8px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; background: var(--panel); font-size: 11px; }
.cmd span { font-size: 18px; }
.cmd.on { border-color: var(--accent); background: rgba(242,193,78,0.22); }

#context { position: absolute; left: 50%; transform: translateX(-50%); bottom: calc(var(--bottom-h) + 4px); width: min(470px, calc(100vw - 12px)); max-height: 42vh; overflow-y: auto; padding: 10px 12px; font-size: 13px; z-index: 4; }
#context h3 { margin: 0 0 4px; font-size: 15px; display: flex; align-items: center; gap: 8px; }
#context h3 .x { padding: 2px 9px; }
#context h3 .x.info { margin-left: auto; }
#context h3 .x.info.on { border-color: var(--blue); }
#context .bar { height: 6px; background: rgba(255,255,255,0.14); border-radius: 3px; overflow: hidden; margin: 4px 0; }
#context .bar div { height: 100%; background: var(--good); }
#context .bar.blue div { background: var(--blue); }
#context .bar.gold div { background: var(--accent); }
#context .row { display: flex; gap: 5px; flex-wrap: wrap; margin-top: 6px; }
#context .row button { font-size: 12px; padding: 6px 9px; }
#context .stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 2px 10px; margin: 4px 0; font-size: 12px; }
#context .stats div { display: flex; justify-content: space-between; border-bottom: 1px dotted var(--line); }
#context .units { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
#context .units .u { width: 38px; text-align: center; font-size: 10px; }
#context .units canvas, #context .units img { width: 30px; height: 30px; }

#place-bar { position: absolute; left: 50%; transform: translateX(-50%); bottom: calc(var(--bottom-h) + 4px); width: min(470px, calc(100vw - 12px)); padding: 8px 10px; font-size: 13px; border-color: var(--accent); z-index: 4; }
#place-bar .acts { display: flex; gap: 6px; margin-top: 6px; }
#place-bar .acts button { flex: 1; padding: 10px; font-size: 15px; }
#place-bar .acts .x { flex: 0 0 56px; }
#place-bar .acts .opt { flex: 0 0 auto; font-size: 13px; }

/* ----------------------------------------------------------- overlays */
#overlay { position: absolute; inset: 0; z-index: 20; background: radial-gradient(ellipse at 50% 35%, rgba(36,46,64,0.97), rgba(8,10,15,0.99)); display: flex; align-items: center; justify-content: center; padding: calc(14px + var(--sat)) 14px calc(14px + var(--sab)); overflow-y: auto; }
#overlay.hidden { display: none; }
.screen { width: 100%; max-width: 620px; text-align: center; margin: auto; }
.logo { font-size: clamp(40px, 12vw, 76px); letter-spacing: 0.12em; margin: 0 0 4px; color: var(--accent); text-shadow: 0 4px 26px rgba(242,193,78,0.35); }
.sub { color: var(--muted); margin: 0 0 18px; }
.tiny { color: var(--muted); font-size: 12px; margin-top: 16px; }
.stack { display: flex; flex-direction: column; align-items: center; gap: 8px; }
button.big { width: min(320px, 82vw); padding: 13px; font-size: 17px; }
.row-btns { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; margin-top: 12px; }
.row-btns button.big { width: auto; min-width: 130px; flex: 1; max-width: 260px; }
.progress { width: min(320px, 80vw); height: 8px; margin: 18px auto 8px; background: rgba(255,255,255,0.12); border-radius: 4px; overflow: hidden; }
.progress div { height: 100%; width: 0; background: var(--accent); transition: width 0.15s; }
.scroll { max-height: 62vh; overflow-y: auto; text-align: left; background: rgba(0,0,0,0.25); border-radius: 12px; padding: 6px 14px; }
.scroll h3 { margin: 14px 0 6px; font-size: 15px; color: var(--accent); }
.scroll p, .scroll li { font-size: 14px; }
.scroll table { width: 100%; border-collapse: collapse; font-size: 13px; }
.scroll td, .scroll th { padding: 4px; border-bottom: 1px solid var(--line); text-align: left; }
.opts { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; margin: 6px 0 12px; }
.opts button { flex: 1 1 120px; max-width: 200px; text-align: left; padding: 8px 10px; }
.opts button small { display: block; color: var(--muted); font-size: 11px; }
.opts button.on { border-color: var(--accent); background: rgba(242,193,78,0.18); }
.setup-label { text-align: left; font-weight: 600; margin: 10px 4px 2px; }
.mult { font-size: 22px; font-weight: 700; color: var(--accent); margin: 10px 0 0; }

#modal { position: absolute; inset: 0; z-index: 30; background: rgba(0,0,0,0.55); display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal-card { background: var(--panel2); border: 1px solid var(--line); border-radius: 14px; padding: 16px; width: min(560px, 100%); max-height: 86vh; overflow-y: auto; }
.modal-card h2 { margin-top: 0; }

/* research tree */
.tech-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 6px; margin: 6px 0; }
.tech { text-align: left; padding: 7px 8px; font-size: 12px; }
.tech b { display: block; font-size: 13px; }
.tech small { color: var(--muted); display: block; }
.tech.done { border-color: var(--good); background: rgba(60,160,90,0.18); }
.tech.busy { border-color: var(--blue); background: rgba(70,120,200,0.2); }
.tech.lock { opacity: 0.45; }

@media (max-width: 640px) {
  :root { --top-h: 60px; --bottom-h: 104px; }
  #top { grid-template-columns: 1fr; grid-template-areas: "res" "right" "wave"; }
  #res-bar { display: grid; grid-template-columns: repeat(5, 1fr); gap: 3px; overflow: visible; }
  #top-right { justify-content: space-between; }
  #clock { font-size: 12px; }
  .chip { font-size: 11.5px; padding: 2px 4px; justify-content: center; }
  .chip small { display: none; }
  #speed button { padding: 3px 5px; min-width: 26px; }
  #minimap { width: 92px; height: 92px; }
  .rail { width: 48px; height: 44px; }

  #toasts { left: calc(60px + var(--sal)); right: calc(100px + var(--sar)); }
  .card { width: 64px; padding: 3px 2px; }
  .card canvas.ic { width: 28px; height: 28px; }
  .card .nm { font-size: 10.5px; }
  .card .cs { font-size: 9px; display: flex; flex-wrap: wrap; justify-content: center; gap: 0 2px; }
  .card .cs .cost { margin-right: 0; }
  #tray { min-height: 60px; }
  #tabs button { padding: 3px 9px; }
  #place-bar { padding: 6px 8px; }
  #place-bar .desc { display: none; }
  #place-bar .acts button { padding: 7px; font-size: 14px; }
  .cmd { min-width: 54px; padding: 4px 6px; font-size: 10.5px; }
  .cmd span { font-size: 16px; }
}
@media (orientation: landscape) and (max-height: 500px) {
  :root { --bottom-h: 84px; }
  /* Short screens: one top row, horizontal cards, and rails that never reach the tray. */
  #top { grid-template-columns: 1fr auto; grid-template-areas: "res right" "wave wave"; padding-top: calc(2px + var(--sat)); padding-bottom: 2px; gap: 2px 6px; }
  #res-bar { display: flex; overflow-x: auto; }
  #top-right { justify-content: flex-end; }
  .chip { font-size: 12px; padding: 2px 5px; }
  .chip small { display: none; }
  #wave-bar { padding: 1px 10px; font-size: 11px; }
  #daybar { width: 54px; }
  #speed button { padding: 3px 5px; min-width: 26px; }
  .ib { padding: 3px 8px; }
  #left-rail { display: grid; grid-template-columns: repeat(4, 40px); gap: 4px; top: calc(var(--top-h) + 4px); }
  #left-rail #groups { grid-column: 1 / -1; grid-template-columns: repeat(5, 1fr); }
  .rail { width: 40px; height: 34px; } .rail small { display: none; }
  #groups button { height: 24px; }
  #right-rail { flex-direction: row; align-items: flex-start; top: calc(var(--top-h) + 4px); }
  #minimap { width: 84px; height: 84px; }
  #toasts { top: calc(var(--top-h) + 72px); left: calc(8px + var(--sal)); right: calc(140px + var(--sar)); }
  #tabs { padding: 2px 0; }
  #tabs button { padding: 2px 9px; font-size: 11.5px; }
  #tray { min-height: 46px; }
  .card { width: 122px; display: grid; grid-template-columns: 26px 1fr; grid-template-rows: auto 1fr; column-gap: 4px; align-items: start; justify-items: start; text-align: left; padding: 3px 4px; }
  .card > :first-child { grid-row: 1 / 3; width: 26px !important; height: 26px !important; align-self: center; }
  .card .nm { text-align: left; font-size: 10.5px; }
  .card .cs { text-align: left; display: flex; flex-wrap: wrap; gap: 0 2px; font-size: 9px; }
  .card .cs .cost { margin-right: 0; }
  #context, #place-bar { max-height: calc(100% - var(--top-h) - var(--bottom-h) - 10px); }
  #context { width: min(470px, calc(100vw - 280px)); }
  #place-bar .desc { display: none; }
  #place-bar .acts button { padding: 6px; font-size: 14px; }
}

/* ------------------------------------------------------------ panels */
.small { font-size: 12px; }
.cost { display: inline-flex; align-items: center; gap: 2px; margin-right: 4px; white-space: nowrap; }
.cost.bad { color: var(--bad); }
.section { margin: 10px 0 4px; font-weight: 600; color: var(--accent); font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; }
.unitbtn { display: flex; flex-direction: column; align-items: center; gap: 2px; min-width: 76px; padding: 6px; font-size: 11px; }
.unitbtn small { color: var(--muted); font-size: 10px; }
.queue { display: flex; gap: 4px; margin-top: 6px; flex-wrap: wrap; }
.qitem { padding: 2px; position: relative; display: flex; flex-direction: column; align-items: center; }
.bar.mini { height: 3px; width: 28px; margin: 2px 0 0; }
table.trade { width: 100%; border-collapse: collapse; }
table.trade td { padding: 3px; }
table.trade button { width: 100%; font-size: 12px; padding: 6px; }
.mayors { display: flex; flex-wrap: wrap; gap: 8px; }
.mayor { flex: 1 1 200px; display: flex; flex-direction: column; align-items: center; gap: 6px; text-align: center; padding: 12px; }
.mayor span { color: var(--muted); font-size: 13px; }
.save-row { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.save-row > div { flex: 1; }
.setting { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.setting span { display: flex; flex-direction: column; }
.setting input[type=checkbox] { width: 22px; height: 22px; }
.setting input[type=range] { flex: 1; }
.ach { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 6px; }
.ach-row { display: flex; flex-direction: column; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; opacity: 0.6; }
.ach-row.got { opacity: 1; border-color: var(--accent); }
table.score td:last-child { text-align: right; font-variant-numeric: tabular-nums; }
table.score tr.total td { font-weight: 700; font-size: 16px; color: var(--accent); border-top: 2px solid var(--line); }
.sel-sum { font-size: 12px; color: var(--muted); padding: 4px 6px; white-space: nowrap; }
#setup-body input[type=text] { width: 100%; padding: 10px; border-radius: 8px; border: 1px solid var(--line); background: rgba(0,0,0,0.3); color: var(--text); font: inherit; }
#setup-body { max-height: 66vh; overflow-y: auto; }
#context h3 small { font-weight: 400; }
#context .row button { display: inline-flex; align-items: center; gap: 4px; }
#context .units .u { padding: 2px; display: flex; flex-direction: column; align-items: center; }
#place-bar .muted { margin: 2px 0; }
.list { text-align: left; }
