:root {
    --bg: #0d2236;
    --panel: rgba(13, 34, 54, 0.92);
    --line: rgba(160, 200, 230, 0.22);
    --ink: #e8f1f8;
    --muted: #9db4c7;
    --gold: #e5b648;
    --gold-dark: #b4862a;
    --blue: #5fb4ff;
    --red: #ff6b6b;
    --green: #7dffa8;
    --font: 'Barlow', system-ui, sans-serif;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
html { touch-action: manipulation; }
body {
    background: var(--bg); color: var(--ink); font-family: var(--font); font-size: 16px;
    -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; overflow: hidden;
}
input, select, button { font: inherit; }
.screen { position: fixed; inset: 0; overflow-y: auto; }

/* Buttons */
.btn {
    color: var(--ink); background: rgba(255, 255, 255, 0.08); border: 1px solid var(--line); border-radius: 10px;
    padding: 10px 14px; font-weight: 700; cursor: pointer; transition: transform .08s, background .15s;
}
.btn:hover { background: rgba(255, 255, 255, 0.14); }
.btn:active { transform: translateY(1px); }
.btn.gold { background: linear-gradient(180deg, #f2c75c, var(--gold-dark)); color: #1c1305; border-color: #f7d98c; }
.btn.ghost { background: transparent; }
.btn.small { padding: 6px 10px; font-size: 14px; }
.btn.wide { width: 100%; margin-top: 16px; padding: 14px; font-size: 18px; }
.btn.big { padding: 16px 12px; font-size: 19px; text-align: left; }
.btn.big small { display: block; font-size: 13px; font-weight: 500; color: var(--muted); margin-top: 2px; }
.btn.gold.big small { color: #3b2a07; }
.btn:disabled { opacity: .45; cursor: default; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.hint { color: var(--muted); font-size: 14px; }
.msg { color: var(--red); min-height: 1.2em; }
input, select {
    color: var(--ink); background: rgba(0, 0, 0, 0.3); border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px;
}
input { -webkit-user-select: text; user-select: text; }

/* Menu */
.menu-bg {
    position: fixed; inset: 0; background: url('art/title.webp?v=1') center / cover no-repeat;
    filter: saturate(1.05) brightness(.7);
}
.menu-bg::after { content: ''; position: absolute; inset: 0; background: linear-gradient(180deg, rgba(8, 20, 34, .2), rgba(8, 20, 34, .85) 70%); }
.menu-box { position: relative; max-width: 560px; margin: 0 auto; padding: max(28px, env(safe-area-inset-top)) 16px 24px; }
.logo { font-family: 'Black Ops One', var(--font); font-weight: 400; font-size: 54px; line-height: .95; margin: 8px 0 0; letter-spacing: 2px; text-shadow: 0 4px 18px rgba(0, 0, 0, .6); }
.logo span { display: block; color: var(--gold); }
.tag { color: #cfe0ee; margin: 6px 0 18px; font-weight: 600; }
.field { display: block; font-weight: 700; font-size: 14px; color: var(--muted); }
.field input { display: block; width: 100%; margin-top: 4px; }
.menu-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 14px; }

/* Ship picker (menu, setup, lobby) */
.ship-pick {
    display: flex; align-items: center; gap: 12px; margin-top: 12px; padding: 10px 12px;
    background: var(--panel); border: 1px solid var(--line); border-radius: 12px; cursor: pointer;
}
.ship-pick img { height: 26px; transform: rotate(90deg); transform-origin: center; width: auto; margin: 0 18px; }
.ship-pick b { display: block; }
.ship-pick small { color: var(--muted); }
.ship-pick .chg { margin-left: auto; color: var(--gold); font-weight: 700; font-size: 14px; }

/* Bars and panels */
.bar {
    position: sticky; top: 0; z-index: 2; display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 10px;
    padding: max(10px, env(safe-area-inset-top)) 16px 10px; background: rgba(8, 20, 34, .95); border-bottom: 1px solid var(--line);
}
.bar h2 { margin: 0; text-align: center; font-size: 20px; }
.panel { max-width: 640px; margin: 16px auto; padding: 16px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; }
.panel h3 { margin: 16px 0 8px; font-size: 15px; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; }
.panel h3:first-child { margin-top: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { padding: 8px 14px; border-radius: 999px; border: 1px solid var(--line); background: transparent; color: var(--ink); font-weight: 700; cursor: pointer; }
.chip[aria-pressed="true"] { background: var(--gold); color: #1c1305; border-color: var(--gold); }

/* Shipyard */
.yard { display: grid; grid-template-columns: minmax(240px, 340px) 1fr; gap: 16px; max-width: 1060px; margin: 0 auto; padding: 16px; }
.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.tab { flex: 1; padding: 8px 6px; border-radius: 8px; border: 1px solid var(--line); background: transparent; color: var(--ink); font-weight: 700; cursor: pointer; white-space: nowrap; }
.tab[aria-selected="true"] { background: rgba(229, 182, 72, .2); border-color: var(--gold); color: var(--gold); }
.preview-wrap { background: radial-gradient(circle at 50% 40%, #2a6f97, #123a58); border-radius: 14px; border: 1px solid var(--line); height: min(56vh, 460px); }
#preview { width: 100%; height: 100%; display: block; }
.perk { color: var(--gold); font-size: 14px; font-weight: 600; }
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 8px; }
.stat { background: rgba(0, 0, 0, .25); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; }
.stat small { display: block; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .5px; }
.stat b { font-size: 19px; }
.weight { display: flex; align-items: center; gap: 10px; margin: 12px 0; }
.weight-bar { flex: 1; height: 10px; border-radius: 999px; background: rgba(0, 0, 0, .35); overflow: hidden; }
.weight-bar i { display: block; height: 100%; background: linear-gradient(90deg, #6fd18f, var(--gold)); border-radius: 999px; transition: width .2s; }
.weight-bar i.full { background: var(--red); }
#weight-text { font-weight: 700; font-size: 14px; white-space: nowrap; }
.mounts { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.mount { display: grid; grid-template-columns: 56px 1fr auto; gap: 10px; align-items: center; background: rgba(0, 0, 0, .22); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; }
.mount .slot { font-weight: 800; font-size: 13px; color: var(--muted); text-align: center; line-height: 1.1; }
.mount .slot b { display: block; font-size: 18px; color: var(--ink); }
.mount select { width: 100%; }
.mount .wstats { grid-column: 2 / 4; font-size: 12.5px; color: var(--muted); }
.variants { display: flex; gap: 4px; }
.variants button { width: 32px; padding: 6px 0; border-radius: 6px; border: 1px solid var(--line); background: transparent; color: var(--ink); font-weight: 800; cursor: pointer; }
.variants button[aria-pressed="true"] { background: var(--gold); color: #1c1305; }

/* Lobby */
.room { font-weight: 700; color: var(--gold); text-align: right; }
.teams { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.team { border-radius: 12px; padding: 10px; border: 1px solid var(--line); }
.team.t0 { background: rgba(95, 180, 255, .1); }
.team.t1 { background: rgba(255, 107, 107, .1); }
.team h3 { margin: 0 0 6px; }
.team ul { list-style: none; padding: 0; margin: 0 0 8px; min-height: 40px; font-size: 14px; }
.team li { padding: 4px 0; border-bottom: 1px solid var(--line); }
.team li small { color: var(--muted); }
.bots-row label { font-size: 14px; color: var(--muted); }

/* Battle */
#battle { overflow: hidden; background: #1b5479; }
#world { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
.hud { position: absolute; inset: 0; pointer-events: none; }
.hud > *, .hud button { pointer-events: auto; }
.minimap {
    position: absolute; left: max(10px, env(safe-area-inset-left)); top: max(10px, env(safe-area-inset-top));
    width: 124px; height: 124px; border-radius: 10px; border: 1px solid var(--line); box-shadow: 0 4px 14px rgba(0, 0, 0, .35);
}
.top-right { position: absolute; right: max(10px, env(safe-area-inset-right)); top: max(10px, env(safe-area-inset-top)); display: flex; gap: 8px; align-items: center; }
.pill { background: rgba(8, 20, 34, .7); border: 1px solid var(--line); border-radius: 999px; padding: 6px 12px; font-weight: 700; font-size: 14px; pointer-events: none; }
.banner {
    position: absolute; left: 50%; top: 22%; transform: translateX(-50%); pointer-events: none;
    font-family: 'Black Ops One', var(--font); font-size: 34px; color: var(--gold); text-shadow: 0 3px 14px rgba(0, 0, 0, .7);
    animation: banner 2.6s ease forwards; white-space: nowrap;
}
@keyframes banner { 0% { opacity: 0; transform: translate(-50%, 10px) scale(.9); } 12%, 75% { opacity: 1; transform: translate(-50%, 0) scale(1); } 100% { opacity: 0; } }

.left-ctl { position: absolute; left: max(14px, env(safe-area-inset-left)); bottom: max(14px, env(safe-area-inset-bottom)); display: grid; grid-template-columns: auto auto; grid-template-rows: auto auto; gap: 8px 10px; align-items: end; }
.telegraph { grid-row: 1 / 3; display: flex; flex-direction: column; gap: 4px; }
.telegraph button {
    width: 52px; padding: 6px 0; border-radius: 8px; font-weight: 800; font-size: 13px; color: var(--ink);
    background: rgba(8, 20, 34, .62); border: 1px solid var(--line); cursor: pointer;
}
.telegraph button[aria-pressed="true"] { background: var(--gold); color: #1c1305; border-color: var(--gold); }
.telegraph button.flank[aria-pressed="true"] { background: #ff8a3d; border-color: #ff8a3d; }
.speed { font-weight: 700; font-size: 14px; color: #dbe9f5; text-shadow: 0 1px 4px #000; }
.speed b { font-size: 22px; }
.heat { width: 120px; height: 5px; border-radius: 999px; background: rgba(0, 0, 0, .4); margin-top: 3px; overflow: hidden; }
.heat i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, #ffd34d, #ff4d2e); }
.joy {
    position: relative; width: 132px; height: 132px; border-radius: 50%; touch-action: none;
    background: radial-gradient(circle, rgba(255, 255, 255, .06), rgba(255, 255, 255, .14)); border: 2px solid rgba(255, 255, 255, .28);
}
.joy::before { content: 'N'; position: absolute; top: 3px; left: 50%; transform: translateX(-50%); font-size: 11px; font-weight: 800; color: rgba(255, 255, 255, .5); }
.joy-knob {
    position: absolute; left: 50%; top: 50%; width: 54px; height: 54px; margin: -27px 0 0 -27px; border-radius: 50%;
    background: radial-gradient(circle at 35% 30%, #f6f9fc, #9fb3c4); box-shadow: 0 3px 10px rgba(0, 0, 0, .45); pointer-events: none;
}
.right-ctl {
    position: absolute; right: max(14px, env(safe-area-inset-right)); bottom: max(14px, env(safe-area-inset-bottom));
    display: grid; grid-template-columns: auto auto; grid-template-areas: "spread repair" "weapon fire"; gap: 10px; align-items: end; justify-items: center;
}
.ctl { border: none; cursor: pointer; color: #fff; font-weight: 800; touch-action: none; }
.fire {
    grid-area: fire; width: 104px; height: 104px; border-radius: 50%; font-size: 20px; letter-spacing: 1px; position: relative;
    background: radial-gradient(circle at 40% 35%, #ff7b5c, #c4271b); box-shadow: 0 5px 0 #7d150d, 0 8px 18px rgba(0, 0, 0, .45);
}
.fire::before {
    content: ''; position: absolute; inset: -7px; border-radius: 50%;
    background: conic-gradient(#ffe08a calc(var(--ready, 1) * 360deg), rgba(255, 255, 255, .15) 0);
    -webkit-mask: radial-gradient(circle, transparent 58px, #000 59px); mask: radial-gradient(circle, transparent 58px, #000 59px);
}
.fire span { position: relative; }
.fire:active, .fire.down { transform: translateY(3px); box-shadow: 0 2px 0 #7d150d; }
.weapon {
    grid-area: weapon; width: 74px; height: 74px; border-radius: 18px; background: rgba(8, 20, 34, .75); border: 2px solid var(--gold);
    display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: 28px;
}
.weapon small { font-size: 12px; color: var(--gold); }
.weapon.torp { border-color: #7fd8ff; }
.weapon.torp small { color: #7fd8ff; }
.repair {
    grid-area: repair; width: 58px; height: 58px; border-radius: 50%; font-size: 24px; position: relative;
    background: rgba(8, 20, 34, .75); border: 2px solid #7dffa8;
}
.repair i { position: absolute; inset: 0; border-radius: 50%; background: conic-gradient(rgba(0, 0, 0, .65) calc(var(--cd, 0) * 360deg), transparent 0); }
.repair.cooling { border-color: var(--line); }
.spread { grid-area: spread; padding: 8px 10px; border-radius: 10px; background: rgba(8, 20, 34, .75); border: 1px solid #7fd8ff; color: #7fd8ff; font-size: 13px; }
.mounts-strip {
    position: absolute; left: 50%; bottom: max(12px, env(safe-area-inset-bottom)); transform: translateX(-50%);
    display: flex; gap: 4px; padding: 5px 7px; border-radius: 10px; background: rgba(8, 20, 34, .62); pointer-events: none;
}
.mt { position: relative; width: 22px; height: 26px; border-radius: 5px; background: rgba(255, 255, 255, .1); overflow: hidden; font-size: 10px; font-weight: 800; display: grid; place-items: center; }
.mt i { position: absolute; left: 0; right: 0; bottom: 0; background: rgba(255, 224, 138, .45); }
.mt.ok { background: rgba(125, 255, 168, .35); }
.mt.off { opacity: .35; }
.mt.torp { color: #7fd8ff; }
.keys { position: absolute; left: 50%; top: max(10px, env(safe-area-inset-top)); transform: translateX(-50%); margin: 0; font-size: 12px; color: rgba(255, 255, 255, .55); pointer-events: none; white-space: nowrap; }
@media (pointer: coarse) { .keys { display: none; } }
@media (max-height: 460px) {
    .joy { width: 112px; height: 112px; }
    .telegraph button { padding: 4px 0; font-size: 12px; }
    .fire { width: 88px; height: 88px; }
    .fire::before { -webkit-mask: radial-gradient(circle, transparent 50px, #000 51px); mask: radial-gradient(circle, transparent 50px, #000 51px); }
    .weapon { width: 62px; height: 62px; font-size: 24px; }
    .minimap { width: 100px; height: 100px; }
}
/* Portrait phones: right-hand buttons in one column so they clear the joystick; mount strip moves up. */
@media (max-width: 600px) and (orientation: portrait) {
    .right-ctl { grid-template-columns: auto; grid-template-areas: "repair" "spread" "weapon" "fire"; gap: 8px; }
    .fire { width: 92px; height: 92px; }
    .fire::before { -webkit-mask: radial-gradient(circle, transparent 52px, #000 53px); mask: radial-gradient(circle, transparent 52px, #000 53px); }
    .weapon { width: 64px; height: 64px; font-size: 24px; }
    .joy { width: 118px; height: 118px; }
    .mounts-strip { bottom: auto; top: calc(max(10px, env(safe-area-inset-top)) + 134px); left: max(10px, env(safe-area-inset-left)); transform: none; }
}

.over { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(5, 14, 24, .55); padding: 16px; }
.over-box { width: min(520px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 18px; text-align: center; }
.over-box h2 { font-family: 'Black Ops One', var(--font); font-weight: 400; font-size: 38px; margin: 0; color: var(--gold); }
.score { width: 100%; border-collapse: collapse; margin: 10px 0 14px; font-size: 14px; }
.score th { color: var(--muted); font-weight: 700; text-align: right; padding: 4px; }
.score th:first-child, .score td:first-child { text-align: left; }
.score td { text-align: right; padding: 5px 4px; border-top: 1px solid var(--line); }
.score tr.t0 td:first-child { color: var(--blue); }
.score tr.t1 td:first-child { color: var(--red); }
.score tr.me td { font-weight: 800; }
.over-box .row { justify-content: center; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 20; background: #000c; padding: 10px 16px; border-radius: 999px; font-weight: 700; max-width: calc(100vw - 32px); }

@media (max-width: 760px) {
    .yard { grid-template-columns: 1fr; }
    .preview-wrap { height: 220px; }
    .menu-grid { grid-template-columns: 1fr; }
}
