:root {
  --bg: #0b0f17;
  --panel: #131a26;
  --panel-2: #1b2433;
  --text: #e8edf5;
  --muted: #8a97ad;
  --accent: #ffd23f;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.hidden { display: none !important; }

.logo {
  margin: 0;
  font-size: 48px;
  font-weight: 900;
  letter-spacing: 2px;
  font-style: italic;
}
.logo span { color: var(--accent); margin-left: 6px; }
.logo.small { font-size: 26px; }

.btn, button {
  display: inline-block;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: #1a1400;
  font: inherit;
  font-weight: 800;
  padding: 12px 20px;
  cursor: pointer;
  text-decoration: none;
  transition: filter 0.15s, background 0.15s;
}
.btn:hover, button:hover:not(:disabled) { filter: brightness(1.1); }
.btn:focus-visible, button:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

input:not([type="checkbox"]):not([type="radio"]) {
  font: inherit;
  font-size: 22px;
  padding: 10px 14px;
  border-radius: 10px;
  border: 2px solid #2c3850;
  background: #0e1420;
  color: var(--text);
  width: 100%;
}

/* ---------- landing ---------- */
.landing main { max-width: 820px; margin: 0 auto; padding: 48px 16px; }
.landing .tag { color: var(--muted); font-size: 18px; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px; margin-top: 32px; }
.card {
  display: flex; flex-direction: column; gap: 12px;
  background: var(--panel); border-radius: 16px; padding: 24px;
  color: inherit; text-decoration: none;
}
.card h2 { margin: 0; }
.card p { margin: 0; color: var(--muted); flex: 1; }
.card input { text-transform: uppercase; letter-spacing: 6px; text-align: center; }
a.card:hover { outline: 2px solid var(--accent); }

/* ---------- host ---------- */
.host { overflow: hidden; }
#app { display: grid; grid-template-columns: 1fr 300px; height: 100vh; }
#stage { position: relative; min-width: 0; min-height: 0; }
#game { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

#lobby {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
  background: rgba(11, 15, 23, 0.82);
  text-align: center;
}
#lobby .qr { width: min(46vh, 420px); background: #fff; padding: 12px; border-radius: 16px; margin-top: 16px; }
#lobby .big { font-size: 34px; font-weight: 800; margin: 12px 0 0; }
#lobby .url { color: var(--muted); font-size: 20px; margin: 0; word-break: break-all; }
#lobby .hint { color: var(--muted); font-size: 20px; margin: 0; }
#lobby .code { color: var(--accent); letter-spacing: 4px; }

#side {
  background: var(--panel);
  display: flex; flex-direction: column; gap: 12px;
  padding: 16px; min-height: 0; overflow-y: auto; /* fallback only: the layout is sized to fit */
}
#side > * { flex: none; }
/* QR on the left, room code on the right. The QR is the one flexible piece: it grows on
   tall screens and shrinks on short ones so the rest of the sidebar always fits. */
.join {
  background: var(--panel-2); border-radius: 14px; padding: 10px;
  display: grid; grid-template-columns: auto 1fr; grid-template-rows: auto auto; column-gap: 12px; align-items: center;
}
.join .qr {
  grid-row: 1 / 3; width: clamp(88px, 100vh - 660px, 150px); height: auto;
  background: #fff; border-radius: 8px; padding: 6px; display: block;
}
.join .room { align-self: end; font-size: 14px; color: var(--muted); display: flex; flex-direction: column; }
.join .code { color: var(--accent); font-size: 26px; letter-spacing: 3px; }
.join .url { align-self: start; margin-top: 4px; font-size: 11px; color: var(--muted); word-break: break-all; }

/* Manage players button + popup */
.duo { display: flex; gap: 8px; flex: none; }
#manage, #settings {
  display: flex; align-items: center; justify-content: center; gap: 8px; flex: 1;
  font-size: 14px; padding: 10px 12px; background: var(--panel-2); color: var(--text); border: 1px solid #2c3850;
}
#manage { justify-content: space-between; }
#fullscreen {
  flex: none; width: 42px; padding: 0; font-size: 18px;
  background: var(--panel-2); color: var(--text); border: 1px solid #2c3850;
}
#manage:hover, #settings:hover, #fullscreen:hover { background: #24304a; filter: none; }
/* Reset / End are hidden during setup: drop the empty row so it takes no space */
#foot .row:not(:has(button:not(.hidden))) { display: none; }
#manage b { background: var(--accent); color: #1a1400; border-radius: 999px; min-width: 26px; padding: 2px 8px; font-size: 13px; }
.dlg {
  width: min(560px, calc(100vw - 32px)); max-height: min(80vh, 760px); padding: 0;
  background: var(--panel); color: var(--text); border: 1px solid #2c3850; border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
}
#settingsDlg { width: min(420px, calc(100vw - 32px)); }
.dlg[open] { display: flex; flex-direction: column; }
.dlg::backdrop { background: rgba(5, 8, 14, 0.6); }
.dlg header { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 10px; }
.dlg h2 { margin: 0; font-size: 18px; }
.dlg h2 span { color: var(--muted); font-weight: 600; font-size: 14px; }
.dlg header button { font-size: 14px; padding: 6px 10px; background: var(--panel-2); color: var(--text); border: 1px solid #2c3850; }
.dlgbody { padding: 4px 16px 18px; display: flex; flex-direction: column; gap: 16px; overflow-y: auto; }
#board { list-style: none; margin: 0; padding: 0 16px 16px; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; }
#board li {
  display: grid; grid-template-columns: 14px 1fr auto auto; align-items: center; column-gap: 10px;
  background: var(--panel-2); border-radius: 10px; padding: 8px 12px;
}
#board .swatch { width: 14px; height: 14px; border-radius: 4px; }
#board .name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#board .score { font-size: 22px; font-weight: 900; }
#board .kick { font-size: 12px; padding: 6px 10px; background: transparent; color: #ff9a9a; border: 1px solid #5a2a33; }
#board .kick:hover { background: #3a1c22; filter: none; }
#board .meta { grid-column: 2 / 5; display: flex; justify-content: space-between; gap: 8px; font-size: 12px; color: var(--muted); }
#board .bottag { grid-column: 2 / 5; font-size: 12px; font-style: italic; color: var(--muted); }
#board .bottag:empty { display: none; }
#board .botopts { grid-column: 2 / 5; display: flex; gap: 6px; margin-top: 6px; }
#board .botopts select { flex: 1 1 0; min-width: 0; padding: 5px 26px 5px 8px; font-size: 12px; }
#board .hearts { color: #ff5d6c; letter-spacing: 2px; }
#board .off { opacity: 0.45; }
#board .badge { font-size: 10px; padding: 1px 6px; border-radius: 6px; background: #2a3549; }
#board .badge.p2p { background: #1f4d2f; color: #8ff0a4; }
#board .empty { color: var(--muted); background: none; display: block; text-align: center; padding: 20px; }

#foot { display: flex; flex-direction: column; gap: 10px; font-size: 12px; color: var(--muted); }
#foot .row { display: flex; gap: 6px; flex-wrap: wrap; }
/* Secondary buttons */
#foot button, #menu button {
  font-size: 12px; padding: 7px 12px; border-radius: 8px;
  background: var(--panel-2); color: var(--text); border: 1px solid #2c3850;
}
#foot button:hover, #menu button:hover { background: #24304a; filter: none; }
#foot #end { color: #ff9a9a; border-color: #5a2a33; }
.aibots { display: flex; align-items: center; gap: 8px; color: var(--text); font-size: 13px; }
.aibots span { flex: 1; font-weight: 700; }
.aibots b { min-width: 22px; text-align: center; font-size: 16px; }
#foot .aibots button { width: 32px; padding: 4px 0; font-size: 16px; font-weight: 900; }
.aiskill { display: flex; align-items: center; justify-content: space-between; gap: 8px; color: var(--text); font-size: 13px; font-weight: 700; }
.aiskill select { flex: 0 0 120px; width: 120px; padding: 6px 28px 6px 10px; }
.skillslider input[type="range"] { flex: 1; min-width: 0; margin: 0; accent-color: var(--accent); cursor: pointer; }
.skillslider output { flex: 0 0 56px; color: var(--accent); text-align: right; }
/* Settings popup: tight rows, label left, control right. */
.dlgbody.compact { gap: 10px; padding-bottom: 14px; }
.dlgbody.compact .toggle { align-items: center; justify-content: space-between; }
.dlgbody.compact .toggle small { margin-top: 1px; }
.dlgbody.compact hr { width: 100%; margin: 2px 0; border: 0; border-top: 1px solid #2c3850; }
#board .badge.ai { background: #3b2a5c; color: #d8c4ff; }
#board .badge.pad { background: #4d3a1f; color: #ffd28f; }
#board .badge.remote { background: #1f3d5c; color: #9fd0ff; }
#board li.remotehost { display: flex; justify-content: space-between; align-items: center; color: #9fd0ff; }
#board li.remotehost button { font-size: 12px; padding: 6px 10px; background: transparent; color: #ff9a9a; border: 1px solid #5a2a33; }
.modepick { display: flex; flex-direction: column; gap: 4px; }

/* Toggle switch */
.toggle { display: flex; gap: 10px; align-items: flex-start; color: var(--text); cursor: pointer; line-height: 1.3; }
.toggle b { display: block; font-size: 13px; }
.toggle small { display: block; color: var(--muted); font-size: 11px; margin-top: 2px; }
.switch {
  appearance: none; -webkit-appearance: none; margin: 0; flex: none;
  width: 36px; height: 20px; border-radius: 999px;
  background: #2c3850; position: relative; cursor: pointer; transition: background 0.15s;
}
.switch::after {
  content: ''; position: absolute; top: 3px; left: 3px; width: 14px; height: 14px; border-radius: 50%;
  background: var(--text); transition: transform 0.15s, background 0.15s;
}
.switch:checked { background: var(--accent); }
.switch:checked::after { transform: translateX(16px); background: #1a1400; }

/* Powerup on/off chips in Settings */
.pusection { color: var(--text); line-height: 1.3; }
.pusection b { display: block; font-size: 13px; }
.pusection small { display: block; color: var(--muted); font-size: 11px; margin-top: 1px; }
.pufreq { margin-top: 8px; font-size: 12px; }
.pufreq output { flex-basis: 64px; }
.putoggles { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.puchip {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px 4px 8px;
  border-radius: 999px; border: 1px solid #2c3850; background: var(--panel-2);
  color: var(--muted); font-size: 12px; cursor: pointer; user-select: none;
  transition: color 0.15s, border-color 0.15s;
}
.puchip input {
  appearance: none; -webkit-appearance: none; margin: 0; flex: none; cursor: pointer;
  width: 10px; height: 10px; border-radius: 50%; border: 2px solid var(--c);
}
.puchip input:checked { background: var(--c); }
.puchip:has(input:checked) { color: var(--text); border-color: var(--c); }
.puchip:has(input:not(:checked)) { text-decoration: line-through; }
.puchip:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Checkboxes */
.modechecks input {
  appearance: none; -webkit-appearance: none; margin: 0; flex: none;
  width: 18px; height: 18px; border-radius: 5px; border: 2px solid #3a4660; background: #0e1420;
  display: grid; place-items: center; cursor: pointer; transition: background 0.15s, border-color 0.15s;
}
.modechecks input:checked { background: var(--accent); border-color: var(--accent); }
.modechecks input:checked::after {
  content: ''; width: 4px; height: 9px; margin-top: -2px;
  border: solid #1a1400; border-width: 0 2px 2px 0; transform: rotate(45deg);
}

/* Dropdowns */
select {
  appearance: none; -webkit-appearance: none; width: 100%;
  font: inherit; font-size: 13px; padding: 8px 30px 8px 10px; border-radius: 8px;
  background-color: #0e1420; color: var(--text); border: 1px solid #2c3850; cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%23ffd23f' stroke-width='2'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center;
}
select option { background: #131a26; color: var(--text); }

/* Setup panel */
.panel { background: var(--panel-2); border-radius: 14px; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.panel h2 { margin: 0; font-size: 16px; }
.panel .hint { margin: 0; font-size: 12px; color: var(--muted); }
.seg { display: grid; grid-template-columns: repeat(4, 1fr); gap: 3px; background: #0e1420; border-radius: 10px; padding: 3px; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; text-align: center; padding: 7px 0; border-radius: 8px; font-size: 13px; font-weight: 700; color: var(--muted); cursor: pointer; }
.seg input:checked + span { background: var(--accent); color: #1a1400; }
.seg input:focus-visible + span { outline: 2px solid var(--accent); }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
#roundsField { flex-direction: row; align-items: center; justify-content: space-between; }
#roundsField select { width: 90px; padding-top: 6px; padding-bottom: 6px; }
/* Game modes: compact wrapping chips instead of a tall checklist */
.modechecks { display: flex; flex-wrap: wrap; gap: 4px; }
.modechecks label {
  display: flex; gap: 5px; align-items: center; color: var(--muted); font-size: 11.5px; line-height: 1.2; cursor: pointer;
  padding: 3px 8px 3px 5px; border-radius: 999px; border: 1px solid #2c3850; background: #0e1420; white-space: nowrap;
}
.modechecks label:has(input:checked) { color: var(--text); border-color: #5a4c1a; }
.modechecks label input { width: 14px; height: 14px; border-radius: 4px; }
.modechecks label input:checked::after { width: 3px; height: 7px; }
#start { width: 100%; }
#start:disabled { opacity: 0.4; cursor: not-allowed; }

/* Match / Game: arena takes the whole screen */
body.immersive #app { grid-template-columns: 1fr; }
body.immersive #side { display: none; }
#menu {
  position: absolute; top: 12px; right: 12px; display: flex; gap: 8px;
  opacity: 0; transition: opacity 0.3s; pointer-events: none;
}
#menu.show { opacity: 1; pointer-events: auto; }

@media (max-aspect-ratio: 1/1) {
  #app { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  #side { max-height: 40vh; flex-direction: row; flex-wrap: wrap; }
  #side .logo { display: none; }
  .join { width: 260px; }
}

/* ---------- controller ---------- */
.play {
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
}
#joinform {
  max-width: 420px; margin: 0 auto; padding: 32px 16px;
  display: flex; flex-direction: column; gap: 14px;
  touch-action: manipulation;
}
#joinform label { display: flex; flex-direction: column; gap: 6px; color: var(--muted); font-weight: 600; }
#joinform #room { text-transform: uppercase; letter-spacing: 6px; }
#joinform .btn { font-size: 22px; padding: 16px; }
#joinform .err { color: #ff7b7b; min-height: 1.2em; margin: 0; }
#pad { position: fixed; inset: 0; }
#pad canvas { width: 100%; height: 100%; display: block; touch-action: none; }
#fsbtn {
  position: absolute; top: 30px; left: 10px;
  font-size: 13px; padding: 8px 12px; border-radius: 10px;
  background: rgba(19, 26, 38, 0.85); color: var(--text); border: 1px solid #2c3850;
  touch-action: manipulation;
}
/* Emote (talk bubble) buttons, bottom centre between the sticks */
#emotes {
  position: absolute; left: 50%; bottom: max(10px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  display: flex; gap: 6px; padding: 5px; border-radius: 999px;
  background: rgba(19, 26, 38, 0.75); border: 1px solid #2c3850;
}
#emotes button {
  width: 42px; height: 42px; padding: 0; border-radius: 50%;
  background: #1b2433; font-size: 24px; line-height: 42px; text-align: center;
  touch-action: manipulation;
}
#emotes button:active { background: #2c3850; }
#emotes button.pop { animation: emote-pop 0.35s ease-out; }
@keyframes emote-pop { 0% { transform: scale(1); } 40% { transform: scale(1.3); } 100% { transform: scale(1); } }
/* Two-step confirm buttons (first click arms them) */
#foot #end.armed, #menu #menuEnd.armed { background: #c0392b; color: #fff; border-color: #c0392b; }

/* ---------- tank customization (controller) ---------- */
#czbtn {
  position: absolute; top: 34px; right: 10px;
  font-size: 15px; padding: 9px 14px; border-radius: 999px;
  touch-action: manipulation; box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
  animation: cz-nudge 2.4s ease-in-out infinite;
}
@keyframes cz-nudge { 0%, 80%, 100% { transform: scale(1); } 88% { transform: scale(1.08); } }
@media (prefers-reduced-motion: reduce) { #czbtn { animation: none; } }
#custom {
  position: fixed; inset: 0; z-index: 10; background: var(--bg);
  display: flex; gap: 12px;
  padding: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
}
#custom.hidden { display: none; }
.cz-stage { position: relative; flex: 1 1 50%; min-width: 0; min-height: 0; border-radius: 16px; background: var(--panel); overflow: hidden; }
#czc { width: 100%; height: 100%; display: block; touch-action: none; }
.cz-side { flex: 1 1 50%; min-width: 0; overflow-y: auto; touch-action: pan-y; display: flex; flex-direction: column; gap: 8px; padding-right: 2px; }
.cz-side header { display: flex; align-items: center; justify-content: space-between; gap: 8px; position: sticky; top: 0; background: var(--bg); padding-bottom: 4px; z-index: 1; }
.cz-side header b { font-size: 18px; margin-right: auto; }
.cz-side header button { padding: 8px 18px; touch-action: manipulation; }
.cz-side header .cz-ghost { background: var(--panel-2); color: var(--text); padding: 8px 12px; }
.cz-side h3 { margin: 4px 0 0; font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.cz-swatches { display: grid; grid-template-columns: repeat(8, 1fr); gap: 6px; }
.cz-swatches button {
  aspect-ratio: 1; padding: 0; border-radius: 50%; border: 3px solid transparent; position: relative; touch-action: manipulation; min-width: 0;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.18);
}
.cz-swatches button.on { border-color: #fff; box-shadow: 0 0 0 2px var(--bg) inset; }
.cz-swatches button.taken { opacity: 0.25; }
.cz-swatches button.taken::after { content: '✕'; position: absolute; inset: 0; display: grid; place-items: center; color: #000; font-size: 14px; }
.cz-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.cz-chips button { padding: 7px 12px; font-size: 13px; background: var(--panel-2); color: var(--text); border: 2px solid transparent; touch-action: manipulation; }
.cz-chips button.on { border-color: var(--accent); }
@media (orientation: portrait) {
  #custom { flex-direction: column; }
  .cz-stage { flex: 0 0 38%; }
  .cz-side { flex: 1 1 auto; }
}
#toast {
  position: fixed; left: 50%; top: 40%; transform: translate(-50%, -50%); z-index: 20;
  background: var(--accent); color: #1a1400; font-weight: 800; font-size: 18px;
  padding: 14px 22px; border-radius: 14px; box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5); text-align: center;
  pointer-events: none;
}

/* Remote hosts: the approval prompt on the main host, and the join screen on the remote. */
.approve-row { display: flex; gap: 10px; }
.approve-row button { flex: 1; }
button.ghost { background: transparent; color: var(--text); border: 1px solid #2c3850; }
#remoteJoin {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 10px; background: var(--bg); text-align: center; padding: 16px; z-index: 5;
}
#remoteJoin .big { font-size: 34px; font-weight: 800; margin: 0; }
#remoteJoin .hint { color: var(--muted); font-size: 20px; margin: 0; max-width: 640px; }
#remoteJoin .code { color: var(--accent); letter-spacing: 4px; }
#remoteForm { display: flex; gap: 10px; margin-top: 12px; flex-wrap: wrap; justify-content: center; }
#remoteForm input { font-size: 20px; padding: 12px 14px; border-radius: 10px; border: 1px solid #2c3850; background: var(--panel-2); color: var(--text); }
#remoteForm .btn { font-size: 20px; padding: 12px 20px; }
#remoteJoin.hidden { display: none; }
/* A remote host only shows the game: no setup, settings, bots or kicking. */
body.remote #setup, body.remote #settings, body.remote #liveMode, body.remote #reset, body.remote #end,
body.remote #menuEnd, body.remote .aibots, body.remote #board button, body.remote #board select { display: none !important; }
