:root {
  --bg: #12141a;
  --panel: rgba(22, 24, 32, 0.86);
  --line: rgba(255, 255, 255, 0.09);
  --text: #e9e6df;
  --muted: #9a9caa;
  --accent: #e0b45c;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif; overflow: hidden; }
#scene { position: fixed; inset: 0; }
#scene canvas { display: block; touch-action: none; }

#panel {
  position: fixed; top: 16px; left: 16px; bottom: 16px; width: 320px;
  display: flex; flex-direction: column; gap: 14px;
  padding: 18px; border: 1px solid var(--line); border-radius: 14px;
  background: var(--panel); backdrop-filter: blur(10px);
  transition: transform .25s ease, opacity .25s ease;
}
body.panel-hidden #panel { transform: translateX(-110%); opacity: 0; }
.panel-toggle {
  position: fixed; top: 16px; right: 16px; z-index: 5;
  width: 40px; height: 40px; border-radius: 10px; font-size: 18px;
}

h1 { margin: 0; font-size: 22px; letter-spacing: .02em; }
h2 { margin: 0 0 8px; font-size: 11px; text-transform: uppercase; letter-spacing: .12em; color: var(--muted); font-weight: 600; }
.sub { margin: 2px 0 0; color: var(--muted); font-size: 12px; }

.turn { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-radius: 10px; background: rgba(255,255,255,.05); font-weight: 600; font-size: 15px; }
.turn em { font-weight: 400; color: var(--muted); font-style: normal; }

.dot { display: inline-block; width: 12px; height: 12px; border-radius: 50%; flex: none; box-shadow: 0 0 0 2px rgba(0,0,0,.35); }

#players { display: flex; flex-direction: column; gap: 6px; }
.player { display: grid; grid-template-columns: 14px 1fr auto auto 72px; align-items: center; gap: 8px; padding: 7px 10px; border-radius: 9px; border: 1px solid transparent; }
.player.active { border-color: var(--accent); background: rgba(224,180,92,.08); }
.player.out { opacity: .45; }
.player.out .pname { text-decoration: line-through; }
.pname { font-weight: 600; }
.status { font-size: 12px; color: var(--muted); }
.status.warn { color: #ff6b6b; font-weight: 600; }
.score { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }

button, select {
  font: inherit; color: var(--text); background: rgba(255,255,255,.07);
  border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; cursor: pointer;
}
select { padding: 4px 6px; font-size: 12px; }
button:hover, select:hover { background: rgba(255,255,255,.13); }
.buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.check { grid-column: 1 / -1; display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); cursor: pointer; }

.log-wrap { flex: 1; min-height: 0; display: flex; flex-direction: column; }
#log { flex: 1; overflow-y: auto; margin: 0; padding: 0 4px 0 26px; font-size: 12.5px; font-variant-numeric: tabular-nums; color: #cfccc4; }
#log li { padding: 2px 0; }
#log li .dot { width: 8px; height: 8px; margin-right: 6px; }
#log li.sys { color: #ffb36b; font-weight: 600; }
footer { font-size: 11px; color: var(--muted); }

.toast {
  position: fixed; top: 22px; left: 50%; transform: translate(-50%, -20px); opacity: 0;
  padding: 10px 18px; border-radius: 10px; background: rgba(20,22,30,.92); border: 1px solid var(--line);
  font-weight: 600; pointer-events: none; transition: all .25s ease; z-index: 10;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

dialog {
  max-width: 560px; width: calc(100% - 32px); color: var(--text); background: #1b1d26;
  border: 1px solid var(--line); border-top: 4px solid var(--accent); border-radius: 14px; padding: 22px 24px;
}
dialog::backdrop { background: rgba(0,0,0,.55); }
dialog h2 { font-size: 18px; color: var(--text); text-transform: none; letter-spacing: 0; }
dialog ul { padding-left: 18px; }
dialog li { margin: 4px 0; }
dialog .row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; }
.c0 { color: #f1e7d0; } .c1 { color: #e5475a; } .c2 { color: #4f93ff; }

@media (max-width: 700px) {
  #panel { top: auto; left: 16px; right: 16px; bottom: 16px; width: auto; height: 40%; padding: 14px; gap: 10px; overflow-y: auto; }
  #panel header .sub { display: none; }
  body.panel-hidden #panel { transform: translateY(110%); }

}

/* ---- Modes ---------------------------------------------------------------- */
[hidden] { display: none !important; }
body:not([data-mode="local"]):not([data-mode="online"]) #panel,
body:not([data-mode="local"]):not([data-mode="online"]) .panel-toggle { display: none; }
body[data-mode="online"] .local-only { display: none; }
body:not([data-mode="online"]) .online-only { display: none; }
.buttons .wide { grid-column: 1 / -1; }

/* ---- Menu + lobby screens ---------------------------------------------------- */
.screen {
  position: fixed; inset: 0; z-index: 4; display: grid; place-items: center; padding: 16px;
  background: radial-gradient(ellipse at center, rgba(18,20,26,.35), rgba(18,20,26,.8));
  overflow-y: auto;
}
.card {
  width: min(460px, 100%); display: flex; flex-direction: column; gap: 14px;
  padding: 22px; border-radius: 16px; border: 1px solid var(--line);
  background: rgba(24, 26, 35, .92); backdrop-filter: blur(12px);
  box-shadow: 0 20px 60px rgba(0,0,0,.45);
}
.card h1 { font-size: 26px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: var(--muted); }
input[type="text"], input:not([type]) {
  font: inherit; color: var(--text); background: rgba(0,0,0,.3); border: 1px solid var(--line);
  border-radius: 8px; padding: 9px 11px; min-width: 0;
}
input:focus-visible, button:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.primary { background: var(--accent); color: #1a1408; border-color: transparent; font-weight: 600; }
.primary:hover { background: #ecc574; }
.join { display: flex; gap: 8px; }
.join input { flex: 1; text-transform: uppercase; letter-spacing: .25em; font-weight: 600; }
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; max-height: 220px; overflow-y: auto; }
.list li { display: grid; grid-template-columns: 1fr auto auto; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 9px; background: rgba(255,255,255,.04); }
.list li.empty { display: block; color: var(--muted); font-size: 13px; text-align: center; padding: 14px; }
.muted { color: var(--muted); }
.small { font-size: 12px; margin: 0; }
.menu-foot { display: flex; justify-content: space-between; align-items: center; gap: 8px; font-size: 12px; }
.link { background: none; border: none; color: var(--muted); padding: 4px 6px; text-decoration: underline; text-underline-offset: 3px; }
.link:hover { background: none; color: var(--text); }
.conn { color: var(--muted); }
.conn::before { content: ''; display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 6px; background: #e0b45c; vertical-align: 1px; }
.conn[data-status="online"]::before { background: #4cd07d; }
.conn[data-status="offline"]::before, .conn[data-status="stopped"]::before { background: #ff5a5a; }

.lobby-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; }
.code { font-size: 38px; font-weight: 700; letter-spacing: .22em; font-variant-numeric: tabular-nums; line-height: 1.1; user-select: all; }
#seats { display: flex; flex-direction: column; gap: 6px; }
.seat { display: grid; grid-template-columns: 14px 64px 1fr auto; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 10px; background: rgba(255,255,255,.04); border: 1px solid transparent; }
.seat.mine { border-color: var(--accent); }
.seat .army { font-size: 12px; color: var(--muted); }
.seat .who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.seat .acts { display: flex; gap: 6px; }
.seat .acts button { padding: 4px 9px; font-size: 12px; }
.card .row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.tag { display: inline-block; margin-left: 6px; padding: 0 6px; border-radius: 6px; font-size: 10.5px; font-weight: 600; vertical-align: 1px; }
.tag.you { background: rgba(224,180,92,.18); color: var(--accent); }
.tag.off { background: rgba(255,90,90,.15); color: #ff7b7b; }

.turn.yours { background: rgba(224,180,92,.16); box-shadow: inset 0 0 0 1px var(--accent); }
.player.online { grid-template-columns: 14px 1fr auto auto; }
.player .pname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---- Chat -------------------------------------------------------------------- */
.chat { display: flex; flex-direction: column; gap: 6px; }
#chat-log { list-style: none; margin: 0; padding: 8px 10px; height: 120px; overflow-y: auto; border-radius: 9px; background: rgba(0,0,0,.25); font-size: 12.5px; overflow-wrap: anywhere; }
#chat-log li { padding: 1px 0; }
#chat-log li.sys { color: var(--muted); font-style: italic; }
#chat-form { display: flex; gap: 6px; }
#chat-form input { flex: 1; padding: 6px 9px; }
#panel .log-wrap { flex: 1 1 0; }
#panel-chat-slot #chat-log { height: 110px; }

@media (max-width: 700px) {
  .grid2 { grid-template-columns: 1fr; }
  .code { font-size: 30px; }
  .lobby-head { flex-wrap: wrap; }
  .seat { grid-template-columns: 14px 1fr auto; }
  .seat .army { display: none; }
  #panel-chat-slot #chat-log { height: 70px; }
}
.conn.clickable { cursor: pointer; color: var(--accent); text-decoration: underline; text-underline-offset: 3px; }
.conn[data-status="replaced"]::before { background: #e0b45c; }
