:root {
    color-scheme: dark;
    --bg: #17110d;
    --panel: rgba(43, 31, 23, .94);
    --panel-2: #37271d;
    --line: #72533c;
    --sand: #e7c78b;
    --muted: #bda98a;
    --ink: #fff5df;
    --copper: #d97745;
    --azure: #49a6bf;
    --danger: #e65945;
    --ok: #7fbd6a;
    --shadow: 0 18px 60px rgba(0,0,0,.38);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html { min-height: 100%; background: var(--bg); -webkit-text-size-adjust: 100%; }
body { min-height: 100vh; min-height: 100dvh; margin: 0; color: var(--ink); background: radial-gradient(circle at 50% -20%, #785431 0, #2b1e16 38%, #17110d 75%); overscroll-behavior-y: none; }
button, input, select { font: inherit; }
button { min-height: 44px; border: 1px solid var(--line); border-radius: 10px; color: var(--ink); background: #4a3425; cursor: pointer; touch-action: manipulation; }
button:hover { filter: brightness(1.12); }
button:focus-visible, input:focus-visible, select:focus-visible { outline: 3px solid var(--sand); outline-offset: 2px; }
button:disabled { cursor: not-allowed; opacity: .42; filter: none; }

.sand-haze { position: fixed; inset: 0; pointer-events: none; opacity: .12; background-image: repeating-linear-gradient(101deg, transparent 0 28px, rgba(255,222,164,.14) 29px 30px); }
.app { width: min(1500px, 100%); min-height: 100vh; min-height: 100dvh; margin: 0 auto; padding: max(clamp(14px, 2vw, 28px), env(safe-area-inset-top)) max(clamp(14px, 2vw, 28px), env(safe-area-inset-right)) max(clamp(14px, 2vw, 28px), env(safe-area-inset-bottom)) max(clamp(14px, 2vw, 28px), env(safe-area-inset-left)); }
.panel { border: 1px solid rgba(231,199,139,.28); border-radius: 18px; background: var(--panel); box-shadow: var(--shadow); }
.landing { max-width: 980px; margin: clamp(20px, 8vh, 90px) auto; padding: clamp(22px, 5vw, 56px); }
.eyebrow { margin: 0; color: var(--copper); font-size: .78rem; font-weight: 800; letter-spacing: .18em; text-transform: uppercase; }
h1 { margin: .1em 0; font-family: Georgia, serif; font-size: clamp(3rem, 10vw, 6.7rem); line-height: .95; letter-spacing: -.05em; }
h2, h3 { margin-top: 0; font-family: Georgia, serif; }
.lead { max-width: 720px; color: var(--muted); font-size: clamp(1rem, 2vw, 1.25rem); line-height: 1.55; }
.landing-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 18px; margin-top: 34px; }
.form-card { display: grid; gap: 15px; padding: 22px; border: 1px solid var(--line); border-radius: 14px; background: rgba(20,13,9,.35); }
label { display: grid; gap: 7px; color: var(--muted); font-size: .9rem; font-weight: 650; }
input, select { width: 100%; min-height: 44px; padding: 9px 11px; border: 1px solid var(--line); border-radius: 9px; color: var(--ink); background: #211710; }
fieldset { padding: 0; border: 0; }
legend { margin-bottom: 8px; color: var(--muted); font-size: .9rem; font-weight: 650; }
.choice-row { display: flex; gap: 8px; }
.choice { display: flex; flex: 1; grid-template-columns: auto 1fr; align-items: center; padding: 10px; border: 1px solid var(--line); border-radius: 9px; }
.choice input { width: auto; min-height: auto; }
.choice.kupfer:has(input:checked) { border-color: var(--copper); background: rgba(217,119,69,.15); }
.choice.azur:has(input:checked) { border-color: var(--azure); background: rgba(73,166,191,.15); }
.primary { border-color: #e3955d; background: #a64f2d; font-weight: 800; }
.secondary { border-color: #77bdce; background: #26768b; font-weight: 800; }
.danger { border-color: var(--danger); color: #ffd8d2; background: rgba(230,89,69,.15); }
.ghost { background: transparent; }
.footnote, .muted { color: var(--muted); }

.game-chrome { position: sticky; z-index: 40; top: 0; margin-bottom: 16px; border: 1px solid var(--line); border-radius: 14px; background: rgba(29,20,14,.97); box-shadow: 0 8px 30px rgba(0,0,0,.25); backdrop-filter: blur(14px); }
.game-header { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 10px 14px; }
.brand-block { display: flex; align-items: center; gap: 8px; }
.brand { font: 800 1.25rem Georgia, serif; }
.room-code { padding: 5px 9px; border: 1px dashed var(--sand); border-radius: 8px; color: var(--sand); letter-spacing: .12em; }
.crew-identity { display: flex; align-items: center; gap: 8px; min-width: 0; }
.player-name { overflow: hidden; max-width: 180px; color: var(--muted); font-size: .82rem; text-overflow: ellipsis; white-space: nowrap; }
.connection { display: inline-flex; align-items: center; gap: 7px; color: var(--muted); font-size: .8rem; }
.connection::before { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); content: ""; }
.connection.offline::before { background: var(--danger); }
.header-actions { display: flex; align-items: center; gap: 8px; }
.header-actions button { min-height: 36px; }
.leave-short { display: none; font-size: 1.3rem; line-height: 1; }
.role-tabs { display: flex; gap: 6px; padding: 0 8px 8px; overflow-x: auto; scrollbar-width: none; scroll-snap-type: x proximity; }
.role-tabs::-webkit-scrollbar { display: none; }
.role-tab { position: relative; display: inline-flex; flex: 1 0 130px; align-items: center; justify-content: center; gap: 7px; min-height: 48px; padding: 8px 12px; border-color: transparent; color: var(--muted); background: rgba(255,255,255,.025); scroll-snap-align: center; }
.role-tab:hover { border-color: var(--line); }
.role-tab.active { border-color: var(--sand); color: var(--ink); background: rgba(231,199,139,.12); box-shadow: inset 0 -3px 0 var(--sand); }
.role-symbol { color: var(--sand); font-size: 1.05rem; }
.role-label-short { display: none; }
.action-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--danger); box-shadow: 0 0 0 4px rgba(230,89,69,.16); }
.role-tab.needs-action:not(.active) { border-color: rgba(230,89,69,.65); color: #ffd0c8; }

.lobby { padding: clamp(18px, 4vw, 38px); }
.lobby-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 20px; }
.team-columns { display: grid; grid-template-columns: repeat(2, 1fr); gap: 18px; margin-top: 24px; }
.team-card { padding: 20px; border: 1px solid var(--line); border-radius: 14px; background: rgba(20,13,9,.3); }
.team-card.kupfer { border-top: 4px solid var(--copper); }
.team-card.azur { border-top: 4px solid var(--azure); }
.role-list { display: grid; gap: 9px; }
.role { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: center; padding: 12px; border: 1px solid #5d4331; border-radius: 10px; background: #2c1e16; }
.role strong, .role small { display: block; }
.role small { margin-top: 3px; color: var(--muted); }
.role .occupied { color: var(--sand); font-size: .86rem; }
.briefing { margin-top: 18px; padding: 14px; border-left: 3px solid var(--sand); color: var(--muted); background: rgba(231,199,139,.06); }

.dashboard { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 16px; }
.column { display: grid; align-content: start; gap: 16px; }
.role-workspace { min-width: 0; scroll-margin-top: 132px; }
.role-stack { display: grid; gap: 16px; min-width: 0; }
.navigator-view { grid-template-columns: minmax(0, 1.35fr) minmax(300px, .65fr); align-items: start; }
.tracker-view { grid-template-columns: minmax(260px, .55fr) minmax(0, 1.45fr); align-items: start; }
.setup-layout { max-width: 1050px; margin: 0 auto; }
.module { padding: 17px; border: 1px solid rgba(231,199,139,.22); border-radius: 14px; background: var(--panel); box-shadow: 0 10px 30px rgba(0,0,0,.18); }
.module-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.module-head h2, .module-head h3 { margin: 0; }
.badges { display: flex; flex-wrap: wrap; gap: 7px; }
.badge { padding: 5px 8px; border-radius: 999px; color: var(--muted); background: #241812; font-size: .78rem; }
.badge.kupfer { color: #ffc09b; background: rgba(217,119,69,.18); }
.badge.azur { color: #aeefff; background: rgba(73,166,191,.18); }
.status-module { padding-block: 12px; }
.status-module .module-head { margin-bottom: 8px; }
.status-module .module-head h2 { font-size: 1.05rem; }
.hulls { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.hull { display: grid; grid-template-columns: auto 1fr; align-items: center; gap: 5px 10px; padding: 9px 11px; border: 1px solid var(--line); border-radius: 10px; }
.hull .badge { justify-self: end; }
.hull-track { display: flex; gap: 5px; margin-top: 8px; }
.hull .hull-track { grid-column: 1 / -1; margin-top: 2px; }
.hull-dot { width: 22px; height: 10px; border-radius: 4px; background: #5d4636; }
.hull-dot.hit { background: var(--danger); }

.map-wrap { overflow: auto hidden; border-radius: 10px; overscroll-behavior-x: contain; -webkit-overflow-scrolling: touch; scrollbar-color: var(--line) transparent; }
.map { display: grid; grid-template-columns: 25px repeat(15, minmax(24px, 1fr)); min-width: 640px; overflow: hidden; border: 1px solid var(--line); border-radius: 10px; background: #d8b879; color: #2a1b11; }
.map-label { display: grid; min-height: 25px; place-items: center; color: #6a4a2f; font-size: .65rem; font-weight: 800; }
.cell { position: relative; min-height: 36px; padding: 0; border: 0; border-right: 1px solid rgba(76,49,28,.24); border-bottom: 1px solid rgba(76,49,28,.24); border-radius: 0; color: #2a1b11; background: rgba(255,245,208,.2); font-size: .65rem; }
.cell:nth-child(5n) { background-color: rgba(120,77,42,.05); }
.cell.sector-right { border-right: 3px solid rgba(76,49,28,.5); }
.cell.sector-bottom { border-bottom: 3px solid rgba(76,49,28,.5); }
.cell.obstacle { background: #76533a; }
.cell.obstacle::after { content: "◆"; color: #2b1b12; }
.cell.route { background: rgba(161,69,37,.33); }
.cell.route::after { content: "·"; font-size: 1.2rem; }
.cell.current { z-index: 2; background: var(--copper); box-shadow: inset 0 0 0 3px #fff1ce; }
.team-azur .cell.current { background: var(--azure); }
.cell.mine::before { position: absolute; content: "✦"; color: #801f16; font-size: 1rem; }
.cell.marked { background: rgba(51,117,133,.42); box-shadow: inset 0 0 0 2px #24677a; }
.cell.selectable:hover { box-shadow: inset 0 0 0 3px #fff3b5; }
.cell.selectable:active { box-shadow: inset 0 0 0 4px #fff3b5; filter: brightness(1.12); }
.map-hint { margin: 8px 0 0; color: var(--muted); font-size: .82rem; }

.direction-pad { display: grid; grid-template-columns: repeat(3, 58px); grid-template-rows: repeat(3, 48px); justify-content: center; gap: 6px; }
.direction-pad button { font-size: 1.3rem; font-weight: 900; }
.direction-pad [data-dir="N"] { grid-column: 2; }
.direction-pad [data-dir="W"] { grid-column: 1; grid-row: 2; }
.direction-pad .center { display: grid; grid-column: 2; grid-row: 2; place-items: center; color: var(--muted); }
.direction-pad [data-dir="E"] { grid-column: 3; grid-row: 2; }
.direction-pad [data-dir="S"] { grid-column: 2; grid-row: 3; }
.pending { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 12px; padding: 10px 13px; border: 1px solid #b88a52; border-radius: 10px; background: rgba(184,138,82,.1); }
.pending h3, .pending p { margin: 0; }
.action-grid, .system-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(145px, 1fr)); gap: 8px; }
.system { min-height: 70px; padding: 10px; text-align: left; }
.system strong, .system span { display: block; }
.system span { margin-top: 4px; color: var(--muted); font-size: .78rem; }
.system.ready { border-color: var(--ok); }
.system.blocked { border-color: var(--danger); }
.inline-controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: end; margin-top: 10px; }
.inline-controls label { min-width: 100px; flex: 1; }
.inline-controls select { min-height: 40px; }

.gauge-list { display: grid; gap: 9px; }
.gauge { display: grid; grid-template-columns: minmax(100px, 1fr) auto; gap: 10px; align-items: center; }
.pips { display: flex; gap: 5px; }
.pip { width: 18px; height: 18px; border: 1px solid #876348; border-radius: 50%; background: #211710; }
.pip.filled { background: var(--sand); box-shadow: 0 0 10px rgba(231,199,139,.4); }
.defect-board { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.defect-column { padding: 8px; border: 1px solid var(--line); border-radius: 9px; }
.defect-column h4 { margin: 0 0 7px; text-align: center; }
.defect-slot { width: 100%; min-height: 35px; margin-top: 5px; padding: 5px; font-size: .72rem; }
.defect-slot.broken { border-color: var(--danger); color: #ffd5ce; background: rgba(230,89,69,.22); }
.defect-slot.available { border-color: var(--sand); }

.events { max-height: 420px; overflow-y: auto; }
.comms-panel { padding: 0; overflow: hidden; }
.comms-panel summary { display: grid; gap: 3px; padding: 14px 16px; cursor: pointer; color: var(--sand); font: 800 1.05rem Georgia, serif; list-style-position: inside; }
.comms-panel summary small { overflow: hidden; color: var(--muted); font: 400 .74rem Inter, sans-serif; text-overflow: ellipsis; white-space: nowrap; }
.comms-panel .events { padding: 0 16px 8px; }
.event { padding: 10px 2px; border-bottom: 1px solid rgba(114,83,60,.4); }
.event:last-child { border: 0; }
.event time { display: block; margin-bottom: 3px; color: var(--muted); font-size: .7rem; }
.event.movement { color: #ffe1ac; }
.event.damage, .event.finish { color: #ff9e8e; font-weight: 800; }
.empty { padding: 20px; color: var(--muted); text-align: center; }
.rules { margin-top: 16px; }
.rules summary { cursor: pointer; color: var(--sand); font-family: Georgia, serif; font-size: 1.15rem; font-weight: 800; }
.rules-body { color: var(--muted); line-height: 1.45; }
.rules-body strong { color: var(--ink); }
.surface-timer { color: var(--danger); font-weight: 800; }
.victory { padding: 30px; text-align: center; }
.victory h2 { font-size: clamp(2rem, 5vw, 4rem); }
.toast { position: fixed; z-index: 100; right: 18px; bottom: 18px; max-width: min(420px, calc(100vw - 36px)); padding: 12px 16px; border: 1px solid var(--danger); border-radius: 10px; background: #3b1914; box-shadow: var(--shadow); opacity: 0; transform: translateY(15px); transition: .2s; pointer-events: none; }
.toast.show { opacity: 1; transform: translateY(0); }

@media (max-width: 860px) {
    .landing-grid, .team-columns, .dashboard { grid-template-columns: minmax(0, 1fr); }
    .navigator-view, .tracker-view { grid-template-columns: minmax(0, 1fr); }
    .secondary-column { gap: 10px; }
    .secondary-column .rules { margin-top: 0; }
    .comms-panel:not([open]) summary { border: 0; }
}

@media (min-width: 861px) and (max-width: 1180px) {
    .dashboard { grid-template-columns: minmax(0, 1fr) 300px; }
    .navigator-view, .tracker-view { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 599px) {
    .app { padding: max(7px, env(safe-area-inset-top)) max(7px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(7px, env(safe-area-inset-left)); }
    .landing { margin: 8px auto; padding: 20px 16px; }
    .choice-row, .lobby-top { flex-direction: column; }
    .choice { width: 100%; }
    .game-chrome { margin: -1px -1px 10px; border-radius: 10px; }
    .game-header { gap: 8px; padding: 7px 9px 6px; }
    .brand { font-size: 1.05rem; }
    .room-code { min-height: 34px; padding: 3px 6px; font-size: .72rem; }
    .crew-identity .player-name { display: none; }
    .crew-identity .badge { max-width: 96px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .connection { width: 9px; overflow: hidden; gap: 0; color: transparent; }
    .connection::before { flex: 0 0 9px; }
    .leave-wide { display: none; }
    .leave-short { display: inline; }
    .leave-button { width: 36px; min-height: 36px; padding: 0; }
    .role-tabs { gap: 4px; padding: 0 5px 5px; }
    .role-tab { flex-basis: 70px; min-width: 68px; min-height: 50px; flex-direction: column; gap: 1px; padding: 4px 5px; font-size: .71rem; }
    .role-symbol { font-size: 1rem; line-height: 1; }
    .role-label-full { display: none; }
    .role-label-short { display: inline; }
    .action-dot { position: absolute; top: 5px; right: 7px; width: 8px; height: 8px; }
    .team-card, .module { padding: 12px; }
    .role { grid-template-columns: 1fr; }
    .defect-board { grid-template-columns: repeat(2, 1fr); }
    .status-module { padding: 9px; }
    .status-module .module-head { margin-bottom: 6px; }
    .hulls { gap: 6px; }
    .hull { grid-template-columns: 1fr; gap: 4px; padding: 7px; font-size: .78rem; }
    .hull .badge { display: none; }
    .hull .hull-track { grid-column: 1; }
    .hull-dot { width: min(22px, 5vw); height: 9px; }
    .pending { grid-template-columns: 1fr; gap: 6px; }
    .pending .badges { gap: 4px; }
    .module-head { align-items: flex-start; }
    .module-head h2 { font-size: 1.25rem; }
    .map { grid-template-columns: 22px repeat(15, 31px); min-width: 487px; }
    .map-label { min-height: 22px; font-size: .58rem; }
    .cell { min-height: 31px; }
    .direction-pad { grid-template-columns: repeat(3, 62px); grid-template-rows: repeat(3, 52px); }
    .direction-pad button { min-height: 52px; }
    .system-grid { grid-template-columns: 1fr 1fr; }
    .system { min-height: 66px; padding: 8px; }
    .inline-controls { align-items: stretch; }
    .inline-controls label { min-width: 42%; }
    .inline-controls button { flex: 1 0 42%; }
    .gauge { grid-template-columns: minmax(0, 1fr) 92px; }
    .gauge button { min-height: 48px; }
    .defect-slot { min-height: 44px; }
    .events { max-height: 48dvh; }
    .toast { right: max(8px, env(safe-area-inset-right)); bottom: max(8px, env(safe-area-inset-bottom)); max-width: calc(100vw - 16px); }
}

@media (min-width: 600px) and (max-width: 979px) {
    .role-tab { flex-basis: 120px; }
    .map { min-width: 620px; }
    .defect-slot { min-height: 42px; }
}

@media (orientation: landscape) and (max-height: 620px) and (min-width: 700px) {
    .app { padding-top: 6px; padding-bottom: 6px; }
    .game-chrome { margin-bottom: 8px; }
    .game-header { padding-block: 5px; }
    .role-tab { min-height: 40px; padding-block: 4px; }
    .dashboard { gap: 8px; }
    .navigator-view, .tracker-view { grid-template-columns: minmax(0, 1.1fr) minmax(290px, .9fr); gap: 8px; }
    .module { padding: 10px; }
}

@media (pointer: coarse) {
    button, input, select { min-height: 48px; }
    .cell { touch-action: manipulation; }
}

@media (prefers-reduced-motion: no-preference) {
    .role-tab.needs-action .action-dot { animation: signal 1.5s ease-in-out infinite; }
}

@keyframes signal {
    50% { box-shadow: 0 0 0 7px rgba(230,89,69,0); }
}
