:root {
  --bg: #0c100c;
  --panel: rgba(18, 24, 15, 0.86);
  --panel-solid: #141a11;
  --line: #3c4a30;
  --line-2: #6d7c52;
  --text: #d7decc;
  --muted: #8d987c;
  --mass: #b6e35a;
  --energy: #f2c94c;
  --accent: #b6ff57;
  --danger: #e15d4c;
  --btn: rgba(36, 48, 32, 0.92);
  --btn-on: #3c5726;
  --go: #31461f;
  --go-line: #9dcc62;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; }
body {
  font-family: Tahoma, Verdana, "Segoe UI", sans-serif;
  font-size: 14px;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
}
button, input, select { font-family: inherit; color: inherit; font-size: inherit; }
button { cursor: pointer; }

#view {
  position: fixed; inset: 0; width: 100vw; height: 100vh; height: 100dvh;
  display: block; touch-action: none; background: #070907;
}
body.menu #view { filter: brightness(0.35) blur(2px); }

/* ---- top bar ------------------------------------------------------------ */
#top {
  position: fixed; left: 0; right: 0; top: 0;
  display: flex; align-items: center; gap: 10px;
  padding: calc(4px + env(safe-area-inset-top)) calc(8px + env(safe-area-inset-right)) 4px calc(8px + env(safe-area-inset-left));
  background: linear-gradient(rgba(12, 16, 11, 0.92), rgba(12, 16, 11, 0.6));
  border-bottom: 1px solid rgba(60, 74, 48, 0.7);
  pointer-events: auto;
  z-index: 5;
}
.res { display: flex; align-items: center; gap: 6px; min-width: 0; }
.res > b {
  width: 22px; height: 22px; border-radius: 4px; display: grid; place-items: center;
  font-size: 12px; color: #10140e;
}
.mass > b { background: var(--mass); }
.energy > b { background: var(--energy); }
.meter { display: flex; flex-direction: column; gap: 2px; min-width: 92px; }
.meter .bar { height: 5px; background: #0a0d09; border: 1px solid #2b3624; border-radius: 3px; overflow: hidden; }
.meter .bar i { display: block; height: 100%; width: 0; }
.mass .bar i { background: var(--mass); }
.energy .bar i { background: var(--energy); }
.meter span { font-size: 12px; white-space: nowrap; }
.meter em { font-style: normal; font-weight: bold; }
.meter small { color: var(--muted); margin-left: 3px; }
.meter small.neg { color: var(--danger); }
.res.stall .bar { border-color: var(--danger); animation: blink 0.8s steps(2) infinite; }
@keyframes blink { 50% { border-color: transparent; } }
.spacer { flex: 1; }
.dim { color: var(--muted); font-size: 12px; }
#clock { font-variant-numeric: tabular-nums; color: var(--muted); }
#top button {
  min-width: 38px; height: 32px; border-radius: 5px;
  border: 1px solid var(--line); background: var(--btn);
}

/* ---- quick buttons, minimap ------------------------------------------- */
#quick {
  position: fixed; left: calc(6px + env(safe-area-inset-left)); top: calc(50px + env(safe-area-inset-top));
  display: flex; flex-direction: column; gap: 6px; z-index: 4;
}
#quick button, #cmds button {
  position: relative;
  min-width: 46px; height: 38px; padding: 0 8px;
  border-radius: 6px; border: 1px solid var(--line);
  background: var(--btn); font-size: 12px; letter-spacing: 0.04em;
}
#quick button.on, #cmds button.on { background: var(--btn-on); border-color: var(--go-line); color: #f3ffd8; }
#quick i {
  position: absolute; right: -5px; top: -5px; font-style: normal; font-size: 10px;
  background: var(--energy); color: #10140e; border-radius: 8px; padding: 1px 5px;
}
#quick i:empty { display: none; }
#minimap {
  position: fixed; right: calc(6px + env(safe-area-inset-right)); top: calc(46px + env(safe-area-inset-top));
  width: 140px; height: 140px; border: 1px solid var(--line-2); border-radius: 4px;
  background: #050605; touch-action: none; z-index: 4;
}
#alerts {
  position: fixed; left: 50%; top: calc(48px + env(safe-area-inset-top)); transform: translateX(-50%);
  display: flex; flex-direction: column; align-items: center; gap: 4px; pointer-events: none; z-index: 6;
}
#alerts div {
  background: rgba(20, 12, 10, 0.85); border: 1px solid #6b3a30; color: #ffd8c8;
  padding: 4px 12px; border-radius: 14px; font-size: 13px; transition: opacity 0.5s;
}
#alerts div.ok { border-color: var(--line-2); color: #e6f6cf; background: rgba(14, 20, 12, 0.85); }
#mode-hint {
  position: fixed; left: 50%; bottom: calc(50% + 40px); transform: translateX(-50%);
  background: rgba(14, 20, 12, 0.85); border: 1px solid var(--go-line);
  padding: 6px 14px; border-radius: 16px; font-size: 13px; pointer-events: none; z-index: 6;
}
#perf {
  position: fixed; left: calc(60px + env(safe-area-inset-left)); top: calc(46px + env(safe-area-inset-top));
  font: 11px/1.3 monospace; color: #b6ff57; background: rgba(0, 0, 0, 0.5); padding: 3px 6px; z-index: 6; pointer-events: none;
  white-space: pre;
}

/* ---- selection panel ---------------------------------------------------- */
#panel {
  position: fixed; left: 0; right: 0; bottom: 0;
  padding: 6px calc(8px + env(safe-area-inset-right)) calc(6px + env(safe-area-inset-bottom)) calc(8px + env(safe-area-inset-left));
  background: var(--panel); border-top: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 6px; z-index: 5;
  max-height: 46vh; overflow: hidden;
}
#info { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#info b { color: var(--text); }
#groups, #cmds, #queue, #build { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
#groups::-webkit-scrollbar, #cmds::-webkit-scrollbar, #queue::-webkit-scrollbar, #build::-webkit-scrollbar { display: none; }
#groups:empty, #queue:empty, #build:empty, #cmds:empty { display: none; }
.grp, .cam, .qi {
  flex: none; position: relative; border: 1px solid var(--line); border-radius: 6px; background: var(--btn);
  padding: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.grp { width: 44px; height: 44px; }
.grp img, .qi img { width: 34px; height: 34px; }
.grp span, .qi span {
  position: absolute; right: 2px; bottom: 1px; font-size: 11px; font-weight: bold;
  text-shadow: 0 0 3px #000, 0 0 3px #000;
}
.qi { width: 40px; height: 40px; }
.qi.cur { border-color: var(--energy); }
.qi .pr { position: absolute; left: 2px; right: 2px; bottom: 2px; height: 3px; background: #000; }
.qi .pr i { display: block; height: 100%; background: var(--energy); }
.cam { width: 74px; height: 78px; padding-top: 2px; }
.cam img { width: 42px; height: 42px; }
.cam b { font-size: 10px; font-weight: normal; line-height: 1.1; text-align: center; padding: 0 2px; max-height: 2.2em; overflow: hidden; }
.cam small { font-size: 10px; color: var(--mass); }
.cam small em { font-style: normal; color: var(--energy); margin-left: 3px; }
.cam .tier { position: absolute; left: 3px; top: 2px; font-size: 9px; color: var(--muted); }
.cam.lock { opacity: 0.4; }
.cam.on { border-color: var(--go-line); background: var(--btn-on); }
.tabs { display: flex; flex-direction: column; gap: 3px; flex: none; }
.tabs button {
  height: 24px; min-width: 40px; border-radius: 5px; border: 1px solid var(--line);
  background: var(--btn); font-size: 11px; padding: 0 6px;
}
.tabs button.on { background: var(--btn-on); border-color: var(--go-line); color: #f3ffd8; }
#cmds button.danger { border-color: #6b3a30; }

/* Landscape phones and wider: panel sits bottom-right, leaves the field open. */
@media (min-aspect-ratio: 4/3) {
  #panel {
    left: auto; width: min(560px, 62vw); border-left: 1px solid var(--line); border-top-left-radius: 8px;
    max-height: 60vh;
  }
}
@media (max-height: 430px) {
  #panel { gap: 4px; padding-top: 4px; }
  #cmds button { height: 30px; min-width: 40px; padding: 0 7px; font-size: 11px; }
  .grp { width: 36px; height: 36px; }
  .grp img { width: 28px; height: 28px; }
  .qi { width: 34px; height: 34px; }
  .qi img { width: 28px; height: 28px; }
  .cam { width: 58px; height: 58px; }
  .cam img { width: 28px; height: 28px; }
  .cam b { font-size: 9px; max-height: 1.1em; }
  .tabs button { height: 17px; font-size: 10px; min-width: 34px; }
  .card { padding: 14px; }
  h1 { font-size: 24px; }
  .tag { display: none; }
  #quick button { height: 32px; min-width: 42px; }
  #quick { gap: 4px; }
  #minimap { width: 112px; height: 112px; }
}
@media (max-width: 520px) {
  .meter { min-width: 70px; }
  #units { display: none; }
}

/* ---- setup and overlays -------------------------------------------------- */
#setup, .overlay {
  position: fixed; inset: 0; display: flex; z-index: 10;
  overflow-y: auto; padding: 16px;
}
#setup > .card, .overlay > .card { margin: auto; }
body:not(.menu) #setup { display: none; }
.overlay { background: rgba(5, 7, 5, 0.6); }
.card {
  width: min(560px, 100%); background: var(--panel-solid); border: 1px solid var(--line);
  border-radius: 10px; padding: 20px 20px 16px; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
}
.card.small { width: min(380px, 100%); display: flex; flex-direction: column; gap: 10px; }
.eyebrow { margin: 0; color: #a8c48c; font-size: 11px; letter-spacing: 0.18em; text-transform: uppercase; }
h1 { margin: 4px 0 6px; font-size: 32px; letter-spacing: 0.04em; }
h1 span { color: var(--accent); }
h2 { margin: 0 0 4px; }
.tag { margin: 0 0 14px; color: var(--muted); line-height: 1.4; }
.field label, .grid label { display: block; color: var(--muted); font-size: 12px; margin-bottom: 4px; }
.factions { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.card .factions button {
  height: auto; flex: 1 1 90px; display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 8px 4px; border: 1px solid var(--line); border-radius: 8px; background: var(--btn); font-size: 11px;
}
.factions button.on { border-color: var(--fac); box-shadow: 0 0 0 1px var(--fac) inset; }
.factions .emblem { width: 34px; height: 34px; display: block; flex: none; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.grid select, .grid input, .card select {
  width: 100%; height: 36px; margin-top: 3px; border-radius: 6px; border: 1px solid var(--line);
  background: #0f140d; padding: 0 8px;
}
.actions { display: flex; align-items: center; justify-content: space-between; margin-top: 16px; gap: 10px; }
.back { color: var(--muted); text-decoration: none; }
.card button { height: 40px; border-radius: 6px; border: 1px solid var(--line); background: var(--btn); padding: 0 16px; }
.card button.go { background: var(--go); border-color: var(--go-line); color: #f3ffd8; font-weight: bold; letter-spacing: 0.06em; min-width: 140px; }
.check { display: flex; align-items: center; gap: 8px; color: var(--text) !important; font-size: 13px !important; }
.help { margin-top: 14px; color: var(--muted); font-size: 13px; }
.help summary { cursor: pointer; color: var(--text); }
.help ul { padding-left: 18px; line-height: 1.45; }
#end-board { width: 100%; border-collapse: collapse; font-size: 12px; }
#end-board td, #end-board th { padding: 3px 4px; border-bottom: 1px solid #23301d; text-align: left; }
#end-board .emblem { width: 16px; height: 16px; vertical-align: -3px; margin-right: 4px; }

/* ---- online ------------------------------------------------------------------ */
.online { margin-top: 16px; padding-top: 12px; border-top: 1px solid var(--line); }
.online-title { margin: 0 0 8px; font-weight: bold; }
.online-row { display: flex; gap: 8px; margin-bottom: 8px; }
.online-row input {
  flex: 1; min-width: 0; height: 40px; border-radius: 6px; border: 1px solid var(--line);
  background: #0f140d; padding: 0 10px;
}
#opt-code { text-transform: uppercase; letter-spacing: 0.2em; }
.online-row button { flex: none; min-width: 120px; }
.hint { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.4; }
.hint.err { color: #ff8a70; }
#lobby-title { letter-spacing: 0.06em; }
#lobby-title b { color: var(--accent); letter-spacing: 0.2em; }
#lobby-seats { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
#lobby-seats li {
  display: flex; align-items: center; gap: 8px; padding: 6px 8px; border: 1px solid var(--line);
  border-radius: 6px; background: var(--btn); font-size: 14px;
}
#lobby-seats li.you { border-color: var(--accent); }
#lobby-seats .emblem { width: 22px; height: 22px; flex: none; }
#lobby-seats small { margin-left: auto; color: var(--muted); }
#net-wait {
  position: fixed; left: 50%; top: 64px; transform: translateX(-50%); z-index: 6;
  padding: 6px 12px; border-radius: 6px; background: rgba(10, 14, 9, 0.85); border: 1px solid var(--line);
  color: var(--text); font-size: 13px; pointer-events: none;
}
