:root {
    --bg: #1d2433;
    --panel: #273046;
    --panel-2: #33405c;
    --fg: #f1ece1;
    --muted: #a9b3c9;
    --accent: #ffd54a;
    --accent-ink: #2b2100;
    --red: #e04848;
    --blue: #3f7fe0;
    --good: #38c172;
    --bad: #ff5a5a;
    --radius: 12px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
    background: var(--bg);
    color: var(--fg);
    font: 16px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
    -webkit-tap-highlight-color: transparent;
    overflow: hidden;
}
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.team-red { color: var(--red); font-weight: 700; }
.team-blue { color: var(--blue); font-weight: 700; }

input {
    font: inherit; color: var(--fg); background: #1a2030;
    border: 2px solid var(--panel-2); border-radius: 10px; padding: 12px; width: 100%;
}
input:focus { outline: none; border-color: var(--accent); }

.btn {
    font: inherit; font-weight: 700; color: var(--fg); background: var(--panel-2);
    border: none; border-radius: 10px; padding: 12px 16px; cursor: pointer;
}
.btn:active:not(:disabled) { transform: scale(0.97); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-ghost { background: transparent; border: 2px solid var(--panel-2); }
.btn-small { padding: 6px 10px; font-size: 0.85rem; }
.row { display: flex; gap: 10px; justify-content: center; }

/* ---------- setup + lobby ---------- */

.setup, .lobby {
    height: 100dvh; overflow-y: auto; max-width: 480px; margin: 0 auto;
    padding: max(20px, env(safe-area-inset-top)) 16px max(20px, env(safe-area-inset-bottom));
    display: flex; flex-direction: column; gap: 14px;
}
.lobby { justify-content: center; }
.brand { text-align: center; margin: 16px 0 4px; }
.brand-mark { font-size: 70px; line-height: 1; }
.brand h1 { margin: 8px 0 4px; font-size: 1.9rem; font-family: Georgia, "Times New Roman", serif; }
.tagline { margin: 0; color: var(--muted); }
.field { display: flex; flex-direction: column; gap: 6px; }
.field span { color: var(--muted); font-size: 0.9rem; }
.divider { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 0.85rem; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--panel-2); }
.code-row { display: flex; gap: 8px; }
.code-row input { text-transform: uppercase; letter-spacing: 0.2em; font-weight: 700; text-align: center; min-width: 0; }
.error { color: var(--bad); margin: 0; text-align: center; }
.panel { background: var(--panel); border-radius: var(--radius); padding: 18px; display: flex; flex-direction: column; gap: 12px; }
.panel h2 { margin: 0; }
.lobby-players { margin: 0; padding-left: 20px; }
.local-names { display: grid; gap: 10px; }

/* ---------- game ---------- */

.game {
    height: 100dvh; display: flex; flex-direction: column;
    padding: max(6px, env(safe-area-inset-top)) 10px max(6px, env(safe-area-inset-bottom));
    max-width: 720px; margin: 0 auto; gap: 6px;
}
.top { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.tabs { display: flex; border: 2px solid var(--panel-2); border-radius: 10px; overflow: hidden; flex: none; }
.tab { font: inherit; font-weight: 700; font-size: 0.85rem; color: var(--muted); background: transparent; border: none; padding: 7px 10px; cursor: pointer; }
.tab.active { background: var(--panel-2); color: var(--fg); }
.phases {
    display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 3px;
    background: var(--panel); border: 1px solid var(--panel-2); border-radius: 8px; padding: 4px; max-width: 62%;
}
.phases span { font-size: 0.7rem; font-weight: 700; padding: 2px 6px; border-radius: 5px; color: var(--muted); white-space: nowrap; }
.phases span.on { background: var(--accent); color: var(--accent-ink); }
.status { display: flex; align-items: center; gap: 8px; font-size: 0.85rem; }
.me-label { font-weight: 700; }
.score { color: var(--muted); flex: 1; }
.conn { color: var(--accent); font-weight: 700; }

.board-view { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 6px; }
.board-wrap { flex: 1; min-height: 0; display: flex; justify-content: center; }
.board { height: 100%; max-width: 100%; aspect-ratio: 10.2 / 30.2; touch-action: manipulation; user-select: none; -webkit-user-select: none; }
.hint { margin: 0; text-align: center; font-size: 0.85rem; color: var(--muted); min-height: 1.2em; }
.actions { display: flex; justify-content: center; gap: 8px; }
.actions:not(:has(button:not([hidden]))) { display: none; }

.tokenbar { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; }
.tok-btn {
    font: inherit; color: var(--fg); background: var(--panel); border: 2px solid var(--panel-2);
    border-radius: 10px; padding: 4px 2px; display: flex; flex-direction: column; align-items: center; gap: 1px; cursor: pointer;
}
.tok-btn.ready { border-color: var(--accent); }
.tok-btn.empty { opacity: 0.4; }
.tok-btn b { font-size: 0.8rem; }
.tok-btn small { font-size: 0.62rem; color: var(--muted); }
.disc { width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; }
.disc svg { width: 70%; height: 70%; }
.disc.red { background: var(--red); }
.disc.blue { background: var(--blue); }

/* Board SVG */
.board .zone-home { fill: #3a4a39; }
.board .zone-mid { fill: #3b3a33; }
.board .zone-enemy { fill: #463a38; }
.board .grid { stroke: rgba(0, 0, 0, 0.25); stroke-width: 0.03; fill: none; }
.board .castle { fill: #8b8272; stroke: #d9cfb8; stroke-width: 0.08; }
.board .castle-bricks { stroke: rgba(40, 30, 20, 0.35); stroke-width: 0.03; }
.board .castle-red { stroke: var(--red); }
.board .castle-blue { stroke: var(--blue); }
.board .hidden-cell { fill: #2e5a33; }
.board .hidden-dots { fill: #1e4424; }
.board .tunnel { fill: #1b1512; stroke: #c8a86a; stroke-width: 0.06; }
.board .tunnel-sym { fill: #f0d38a; }
.board .mist { fill: #9aa3ad; opacity: 0.92; }
.board .mist-edge { fill: url(#mistPattern); }
.board .target { fill: var(--accent); opacity: 0.85; cursor: pointer; }
.board .target-ring { fill: none; stroke: var(--accent); stroke-width: 0.06; }
.board .cellhit { fill: transparent; cursor: pointer; }
.board .lastmove { stroke: var(--accent); stroke-width: 0.06; fill: none; stroke-dasharray: 0.15 0.12; opacity: 0.7; }
.board .token { transition: transform 0.35s ease; cursor: pointer; }
.board .token .ring { fill: none; stroke: var(--accent); stroke-width: 0.08; opacity: 0; }
.board .token.selected .ring { opacity: 1; }
.board .token .body { transform-box: fill-box; transform-origin: center; }
.board .token.explode .body { animation: explode 0.9s ease-in forwards; }
.board .burst { transform-box: fill-box; transform-origin: center; animation: burst 0.9s ease-out forwards; }
@keyframes explode { 0% { transform: scale(1); opacity: 1; } 40% { transform: scale(1.5); opacity: 1; filter: brightness(2); } 100% { transform: scale(0.2); opacity: 0; } }
@keyframes burst { 0% { transform: scale(0.2); opacity: 1; } 100% { transform: scale(2.4); opacity: 0; } }

/* Inventory */
.inv-view { flex: 1; overflow-y: auto; }
.inv-list { display: grid; gap: 8px; }
.inv-item {
    display: flex; align-items: center; gap: 12px; text-align: left; font: inherit; color: var(--fg);
    background: var(--panel); border: 2px solid var(--panel-2); border-radius: var(--radius); padding: 10px; cursor: pointer;
}
.inv-item .disc { width: 42px; height: 42px; flex: none; }
.inv-item .inv-name { font-weight: 800; flex: 1; }
.inv-item .inv-count { font-size: 1.3rem; font-weight: 800; }

/* Modals */
.modal { position: fixed; inset: 0; background: rgba(8, 10, 16, 0.6); display: grid; place-items: center; z-index: 10; padding: 16px; }
.card { background: var(--panel); border: 1px solid var(--panel-2); border-radius: var(--radius); padding: 18px; width: min(420px, 100%); display: flex; flex-direction: column; gap: 12px; }
.card.small { width: min(280px, 100%); text-align: center; }
.card h3 { margin: 0; }
.deploy-token { display: flex; justify-content: center; align-items: center; gap: 10px; font-weight: 800; }
.info-head { display: flex; align-items: center; gap: 12px; }
.info-head .disc { width: 48px; height: 48px; }
.info-head h3 { margin: 0; }
.info-table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
.info-table th { text-align: left; color: var(--muted); font-weight: 600; padding: 5px 8px 5px 0; vertical-align: top; width: 34%; }
.info-table td { padding: 5px 0; }

.dice-card { text-align: center; }
.duel { display: flex; align-items: center; justify-content: center; gap: 12px; }
.side { display: flex; flex-direction: column; align-items: center; gap: 6px; min-width: 110px; }
.side .who { font-weight: 800; font-size: 0.9rem; }
.side .bonus { font-size: 0.8rem; color: var(--accent); min-height: 1.1em; }
.side .total { font-size: 0.8rem; color: var(--muted); min-height: 1.1em; }
.vs { color: var(--muted); font-weight: 800; }
.die {
    width: 64px; height: 64px; border-radius: 12px; background: #fdfbf5; box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.15);
    display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr); padding: 9px;
}
.die i { width: 11px; height: 11px; border-radius: 50%; background: #222; place-self: center; visibility: hidden; }
.die i.on { visibility: visible; }
.die.rolling { animation: shake 0.12s linear infinite; }
.die.red i { background: #b52020; }
.die.blue i { background: #1d55b0; }
@keyframes shake { 0% { transform: rotate(-8deg); } 50% { transform: rotate(8deg); } 100% { transform: rotate(-8deg); } }
.dice-result { margin: 0; font-weight: 800; min-height: 2.6em; }
.dice-result.good { color: var(--good); }
.dice-result.bad { color: var(--bad); }

.toast {
    position: fixed; left: 50%; top: 46%; transform: translate(-50%, -50%); z-index: 12; pointer-events: none;
    background: var(--panel); border: 2px solid var(--accent); border-radius: 999px; padding: 8px 16px;
    font-weight: 700; font-size: 0.9rem; display: flex; align-items: center; gap: 8px; max-width: calc(100% - 32px);
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
}
.toast .disc { width: 24px; height: 24px; }

.curtain { position: fixed; inset: 0; z-index: 20; background: var(--bg); display: grid; place-items: center; padding: 16px; }
.curtain-box { max-width: 420px; width: 100%; display: flex; flex-direction: column; gap: 14px; text-align: center; }
.curtain-icon { font-size: 60px; }
.curtain-box h2 { margin: 0; }
.curtain-box p { margin: 0; }

/* End screen */
.end { position: fixed; inset: 0; z-index: 15; pointer-events: none; }
.end.solid { background: rgba(8, 10, 16, 0.55); }
.fx { position: absolute; inset: 0; width: 100%; height: 100%; }
.end-box {
    position: absolute; left: 50%; top: 40%; transform: translate(-50%, -50%); width: min(460px, calc(100% - 32px));
    text-align: center; display: flex; flex-direction: column; gap: 14px; pointer-events: auto;
    background: rgba(29, 36, 51, 0.92); border-radius: var(--radius); padding: 20px; border: 1px solid var(--panel-2);
}
.end-win { color: var(--good); font-size: 1.35rem; font-weight: 800; margin: 0; }
.end-lose { color: var(--bad); font-size: 1.35rem; font-weight: 800; margin: 0; }
.end-sad { font-size: 2.2rem; animation: sad 2.4s ease-in-out infinite; }
@keyframes sad { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(6px); } }
