:root {
    --cell: min(8.6vw, 38px);
    --gap: 3px;
    --r: 10px;
    --r-lg: 20px;
    --shadow: 0 2px 16px rgba(0,0,0,.08);
    --shadow-lg: 0 6px 32px rgba(0,0,0,.12);

    --ocean: #0d2137;
    --ocean-mid: #1a3a5c;
    --water-cell: #c8e6f5;
    --water-hover: #90caf9;
    --ship-a: #7fa8be;
    --ship-b: #4f7d91;
    --hit: #f4511e;
    --miss-cell: #b0cdd8;
    --sunk-a: #c62828;
    --sunk-b: #7b1111;
    --ok: #43a047;
    --bad: #e53935;

    --bg: #dceef8;
    --surface: #fff;
    --surface-2: #f5f8fb;
    --border: rgba(0,0,0,.1);
    --text: #1a2938;
    --muted: #607d8b;
    --accent: #1565c0;
    --accent-h: #0d47a1;
    --danger: #c62828;
}

*,*::before,*::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: 'Segoe UI', system-ui, sans-serif;
    font-size: 15px;
    color: var(--text);
    background: linear-gradient(150deg, #e3f0fb 0%, #cfe4f4 60%, #c4ddf0 100%);
    min-height: 100vh;
}
body.sidebar-opened { overflow: hidden; }

/* ── Topbar ─────────────────────────────── */
.topbar {
    background: var(--ocean);
    color: #fff;
    height: 54px;
    padding: 0 20px;
    display: flex;
    align-items: center;
    position: sticky;
    top: 0;
    z-index: 100;
}
.topbar-inner {
    width: 100%;
    max-width: 1320px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    gap: 12px;
}
.brand { font-size: 1.2rem; font-weight: 800; flex: 1; letter-spacing: -.02em; }
.topbar-right { display: flex; align-items: center; gap: 8px; }
.nick-input {
    background: rgba(255,255,255,.1);
    border: 1px solid rgba(255,255,255,.22);
    color: #fff;
    border-radius: 8px;
    padding: 6px 12px;
    font: inherit;
    font-size: .88rem;
    width: 175px;
    outline: none;
    transition: background .15s;
}
.nick-input::placeholder { color: rgba(255,255,255,.4); }
.nick-input:focus { background: rgba(255,255,255,.18); }

/* ── Buttons ────────────────────────────── */
.btn {
    display: inline-flex; align-items: center; gap: 5px;
    padding: 9px 18px; border-radius: 99px; border: none;
    font: inherit; font-weight: 600; font-size: .88rem;
    cursor: pointer; white-space: nowrap;
    transition: transform .12s, opacity .15s, background .15s;
}
.btn:hover:not(:disabled) { transform: translateY(-1px); }
.btn:active:not(:disabled) { transform: none; }
.btn:disabled { opacity: .42; cursor: not-allowed; }

.btn-primary   { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-h); }
.btn-secondary { background: #dbeeff; color: var(--accent-h); }
.btn-ghost     { background: rgba(255,255,255,.88); color: var(--text); border: 1px solid var(--border); }
.btn-danger    { background: rgba(198,40,40,.1); color: var(--danger); border: 1px solid rgba(198,40,40,.22); }
.btn-sm  { padding: 6px 13px; font-size: .82rem; }
.btn-lg  { padding: 13px 28px; font-size: 1rem; }
.btn-full { width: 100%; justify-content: center; }

@keyframes spin-once {
    from { transform: rotate(0deg) scale(1); }
    50%  { transform: rotate(180deg) scale(1.3); }
    to   { transform: rotate(360deg) scale(1); }
}
#randomNickButton.spin { animation: spin-once .4s ease-out; }

.topbar .btn-ghost {
    background: rgba(255,255,255,.12);
    color: #fff;
    border-color: rgba(255,255,255,.22);
}
.topbar .btn-ghost:hover:not(:disabled) { background: rgba(255,255,255,.22); }

/* ── Layout ─────────────────────────────── */
.app-layout {
    max-width: 1320px;
    margin: 0 auto;
    padding: 20px 16px 48px;
    display: block;
}
.panel {
    background: var(--surface);
    border-radius: var(--r-lg);
    box-shadow: var(--shadow-lg);
    border: 1px solid rgba(255,255,255,.7);
    padding: 20px;
}

/* ── Sidebar ────────────────────────────── */
.sidebar {
    position: fixed;
    top: 66px;
    left: 12px;
    width: min(320px, calc(100vw - 24px));
    max-height: calc(100vh - 78px);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 14px;
    z-index: 150;
    transform: translateX(-115%);
    opacity: 0;
    pointer-events: none;
    transition: transform .22s ease, opacity .2s ease;
}
.app-layout.sidebar-open .sidebar {
    transform: translateX(0);
    opacity: 1;
    pointer-events: auto;
}
.sidebar-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(10, 20, 34, .42);
    backdrop-filter: blur(2px);
    z-index: 140;
    opacity: 0;
    pointer-events: none;
    transition: opacity .18s ease;
}
.sidebar-backdrop.active {
    opacity: 1;
    pointer-events: auto;
}
.status-banner {
    font-size: .83rem;
    color: var(--muted);
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--r);
    padding: 10px 14px;
}
.lobby-section { display: flex; flex-direction: column; gap: 8px; }
.lobby-section-head {
    display: flex; justify-content: space-between; align-items: center;
    font-size: .78rem; font-weight: 700; text-transform: uppercase;
    letter-spacing: .06em; color: var(--muted);
}
.player-card {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--r);
    padding: 11px 14px;
    display: flex; justify-content: space-between; align-items: center; gap: 8px;
}
.player-name { font-weight: 600; font-size: .88rem; }
.invite-card {
    background: rgba(21,101,192,.05);
    border: 1px solid rgba(21,101,192,.2);
    border-radius: var(--r);
    padding: 12px 14px;
    display: flex; flex-direction: column; gap: 8px;
}
.invite-from { font-weight: 700; font-size: .9rem; }
.invite-actions { display: flex; gap: 8px; }
.text-muted { color: var(--muted); }

/* ── Game Panel ─────────────────────────── */
.game-panel { display: flex; flex-direction: column; gap: 18px; }
.game-header {
    display: flex; justify-content: space-between; align-items: flex-start; gap: 12px;
    flex-wrap: wrap;
}
.game-header h2 { font-size: 1.3rem; }
.game-header .text-muted { font-size: .88rem; margin-top: 3px; }
.fighter-name { font-weight: 800; letter-spacing: .01em; }
.vs-badge {
    display: inline-block;
    margin: 0 8px;
    padding: 2px 10px;
    border-radius: 999px;
    background: linear-gradient(135deg, #d32f2f, #b71c1c);
    color: #fff;
    font-size: .8em;
    font-weight: 900;
    letter-spacing: .08em;
    vertical-align: middle;
}
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }

/* ── Result Panel ───────────────────────── */
.result-panel {
    background: linear-gradient(135deg, #f6fbff, #eef6ff);
    border: 1px solid rgba(21,101,192,.18);
    border-radius: var(--r-lg);
    padding: 14px 18px;
    display: flex;
    align-items: center;
    gap: 8px;
    position: sticky;
    top: 62px;
    z-index: 60;
}
.result-panel > div {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
}
.result-panel h3 {
    font-size: 1.02rem;
    margin: 0;
    white-space: nowrap;
}
.result-panel .text-muted {
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.result-panel.turn-mine {
    background: linear-gradient(135deg, #edf9f1, #e4f8ef);
    border-color: rgba(67, 160, 71, .35);
}
.result-panel.turn-enemy {
    background: linear-gradient(135deg, #fff8eb, #fff1dd);
    border-color: rgba(245, 124, 0, .35);
}
.result-panel.turn-system {
    background: linear-gradient(135deg, #f4f8fc, #edf4fb);
    border-color: rgba(96, 125, 139, .3);
}
.result-panel.turn-win {
    background: linear-gradient(135deg, #e9f9ef, #def4e8);
    border-color: rgba(46, 125, 50, .35);
}
.result-panel.turn-lose {
    background: linear-gradient(135deg, #fff0f0, #ffe7e7);
    border-color: rgba(198, 40, 40, .35);
}

/* ── Setup Layout ───────────────────────── */
.setup-layout {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 24px;
    align-items: start;
}
.setup-sidebar {
    display: flex; flex-direction: column; gap: 12px;
    min-width: 180px; max-width: 240px;
}
.setup-sidebar h3 { font-size: .95rem; }
.hint-text { font-size: .83rem; line-height: 1.45; }
.setup-actions {
    display: flex; flex-direction: column; gap: 8px;
    margin-top: 8px; padding-top: 14px; border-top: 1px solid var(--border);
}
.setup-btn-row { display: flex; gap: 6px; }

/* ── Fleet Picker ───────────────────────── */
.fleet-picker { display: flex; flex-direction: column; gap: 10px; }
.fleet-row { display: flex; flex-direction: column; gap: 5px; }
.fleet-row-label {
    font-size: .72rem; font-weight: 700; text-transform: uppercase;
    letter-spacing: .05em; color: var(--muted);
}
.fleet-ships-wrap { display: flex; flex-wrap: wrap; gap: 5px; }

.fleet-ship {
    display: flex;
    cursor: grab;
    border-radius: 4px;
    overflow: hidden;
    touch-action: none;
    user-select: none;
    transition: transform .12s, opacity .15s, box-shadow .12s;
    box-shadow: 0 2px 6px rgba(0,0,0,.18);
    position: relative;
}
.fleet-ship:hover:not(.placed) { box-shadow: 0 5px 14px rgba(0,0,0,.22); }
.fleet-ship.placed { opacity: .28; cursor: default; pointer-events: none; }
.fleet-ship.orient-v { flex-direction: column; }

.ship-block {
    width: calc(var(--cell) * .82);
    height: calc(var(--cell) * .82);
    background: linear-gradient(145deg, var(--ship-a), var(--ship-b));
    border-right: 1px solid rgba(0,0,0,.14);
    flex-shrink: 0;
}
.fleet-ship:not(.orient-v) .ship-block:last-child { border-right: none; }
.fleet-ship.orient-v .ship-block { border-right: none; border-bottom: 1px solid rgba(0,0,0,.14); }
.fleet-ship.orient-v .ship-block:last-child { border-bottom: none; }

/* ── Game Board ─────────────────────────── */
.board {
    display: grid;
    grid-template-columns: repeat(11, var(--cell));
    grid-auto-rows: var(--cell);
    gap: var(--gap);
}
.board-axis {
    display: flex; align-items: center; justify-content: center;
    font-size: .7rem; font-weight: 700; color: var(--muted);
}
.board-cell {
    border-radius: 5px;
    display: flex; align-items: center; justify-content: center;
    font-weight: 900; font-size: 1rem;
    border: none; cursor: default;
    transition: transform .1s, background .1s;
    position: relative;
}
.board-cell.water,
.board-cell.unknown { background: var(--water-cell); }
.board-cell.ship    { background: linear-gradient(145deg, var(--ship-a), var(--ship-b)); }
.board-cell.miss    { background: var(--miss-cell); color: rgba(255,255,255,.8); }
.board-cell.hit {
    background: linear-gradient(145deg, #ffbd59, #f57c00);
    color: rgba(255,255,255,.95);
    box-shadow: inset 0 0 0 2px rgba(255,255,255,.22);
}
.board-cell.sunk {
    background: linear-gradient(145deg, #a11212, #5b0808);
    color: rgba(255,255,255,.92);
    box-shadow: inset 0 0 0 2px rgba(255,255,255,.1);
}

.board-cell.attackable { cursor: crosshair; }
.board-cell.attackable:hover {
    transform: scale(1.1);
    background: linear-gradient(145deg, #81d4fa, #4fc3f7);
    z-index: 2;
}
.board-cell.preview-ok  { background: rgba(67,160,71,.55) !important; outline: 2px solid var(--ok); outline-offset: -1px; }
.board-cell.preview-bad { background: rgba(229,57,53,.45) !important; outline: 2px solid var(--bad); outline-offset: -1px; }

/* Hit/miss content via pseudo */
.board-cell.miss::after  { content: '•'; line-height: 1; }
.board-cell.hit::after   { content: '✹'; font-size: .85em; text-shadow: 0 0 8px rgba(255,189,89,.8); }
.board-cell.sunk::after  { content: '✖'; font-size: .92em; }
.board-cell.hint-cell::after {
    content: '✕';
    color: rgba(70, 84, 95, .5);
    font-size: .9em;
}

/* ── Active game panel ──────────────────── */
#activeGamePanel { display: flex; flex-direction: column; gap: 14px; }
#activeGamePanel .game-summary { margin-top: 2px; }

/* ── Tabs ───────────────────────────────── */
.tabs {
    display: inline-flex; gap: 6px; padding: 5px;
    background: var(--surface-2); border: 1px solid var(--border); border-radius: 99px;
    align-self: flex-start;
}
.tab-button {
    padding: 8px 18px; border: none; border-radius: 99px;
    background: transparent; font: inherit; font-weight: 600; font-size: .88rem;
    color: var(--muted); cursor: pointer; transition: all .15s;
}
.tab-button.active { background: var(--ocean); color: #fff; }
.tab-button:disabled { opacity: .38; cursor: not-allowed; }

/* ── Board Stage ────────────────────────── */
.board-stage.dual-boards {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 14px;
}
.board-panel { display: none; }
.board-panel.active { display: block; }
.board-panel.dual-mode {
    display: block;
    overflow-x: auto;
}

/* ── Game Summary ───────────────────────── */
.game-summary { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.summary-card {
    background: var(--surface-2); border: 1px solid var(--border);
    border-radius: var(--r); padding: 14px 16px;
}
.summary-card h3 { font-size: .88rem; margin-bottom: 10px; }
.summary-row { font-size: .83rem; color: var(--muted); margin: 3px 0; }
.summary-row strong { color: var(--text); }
.fleet-bar { display: flex; gap: 3px; flex-wrap: wrap; margin-top: 10px; }
.fleet-bar-dot { width: 9px; height: 9px; border-radius: 2px; background: var(--border); }
.fleet-bar-dot.active { background: var(--hit); }
.setup-summary { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* ── Battle Log ─────────────────────────── */
.battle-log {
    max-height: 190px; overflow-y: auto;
    display: flex; flex-direction: column; gap: 5px;
}
.log-entry {
    font-size: .83rem; padding: 7px 12px;
    background: var(--surface-2); border-radius: 8px;
    border: 1px solid var(--border);
}
.log-entry.log-me {
    background: rgba(67, 160, 71, .08);
    border-color: rgba(67, 160, 71, .25);
}
.log-entry.log-enemy {
    background: rgba(245, 124, 0, .1);
    border-color: rgba(245, 124, 0, .28);
}
.log-entry.log-system {
    background: rgba(96, 125, 139, .08);
    border-color: rgba(96, 125, 139, .2);
}
.log-time { font-size: .75rem; color: var(--muted); margin-right: 8px; }

/* ── Drag Ghost ─────────────────────────── */
.drag-ghost {
    position: fixed; pointer-events: none; z-index: 9999;
    display: flex; opacity: .88;
    filter: drop-shadow(0 4px 12px rgba(0,0,0,.35));
}
.drag-ghost.orient-v { flex-direction: column; }
.drag-ghost-cell {
    width: var(--cell); height: var(--cell);
    background: linear-gradient(145deg, var(--ship-a), var(--ship-b));
    border-right: 2px solid rgba(0,0,0,.15);
}
.drag-ghost-cell:first-child { border-radius: 6px 0 0 6px; }
.drag-ghost-cell:last-child  { border-radius: 0 6px 6px 0; border-right: none; }
.drag-ghost.orient-v .drag-ghost-cell { border-right: none; border-bottom: 2px solid rgba(0,0,0,.15); }
.drag-ghost.orient-v .drag-ghost-cell:first-child { border-radius: 6px 6px 0 0; }
.drag-ghost.orient-v .drag-ghost-cell:last-child  { border-radius: 0 0 6px 6px; border-bottom: none; }
.drag-ghost.valid   .drag-ghost-cell { background: linear-gradient(145deg, #81c784, #388e3c); }
.drag-ghost.invalid .drag-ghost-cell { background: linear-gradient(145deg, #e57373, #c62828); }

/* ── Difficulty selector ────────────────── */
.difficulty-row {
    display: flex; gap: 6px; flex-wrap: wrap;
}
.diff-btn {
    flex: 1 1 110px;
    min-width: 110px;
    justify-content: center;
}
.diff-btn.active {
    background: var(--ocean); color: #fff;
    border-color: var(--ocean);
}
.difficulty-hint {
    margin-top: 2px;
    padding: 8px 10px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--muted);
    font-size: .79rem;
    line-height: 1.35;
}

/* ── Tabs row (tabs + auto-play) ─────────── */
.tabs-row {
    display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
}

/* ── Auto-play button active state ──────── */
.btn.auto-on {
    background: var(--accent); color: #fff;
    border-color: var(--accent);
}

/* ── Enemy shot animation ───────────────── */
@keyframes enemy-shot {
    0%   { transform: scale(0.85); opacity: 0; }
    22%  { transform: scale(1.06); opacity: 1; }
    100% { transform: scale(1.2); opacity: 0; }
}
.cell-enemy-shot {
    animation: enemy-shot 1.55s ease-out forwards;
    z-index: 5;
    position: relative;
}
.cell-enemy-shot::before {
    content: '✸';
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #ffd54f;
    text-shadow: 0 0 10px rgba(255, 213, 79, .9), 0 0 24px rgba(244, 67, 54, .55);
    font-size: 1.05em;
    pointer-events: none;
}
.cell-enemy-shot::after {
    content: '';
    position: absolute;
    inset: 2px;
    border-radius: 4px;
    border: 2px solid rgba(255, 213, 79, .85);
    box-shadow: 0 0 20px rgba(255, 82, 82, .6);
    pointer-events: none;
}

/* ── Responsive ─────────────────────────── */
@media (max-width: 940px) {
    .setup-layout { grid-template-columns: 1fr; }
    .setup-sidebar { max-width: none; }
    .setup-btn-row { flex-wrap: wrap; }
}
@media (max-width: 600px) {
    :root { --cell: min(9.2vw, 34px); }
    .topbar { padding: 0 12px; height: 48px; }
    .nick-input { width: 120px; font-size: .8rem; }
    .brand { font-size: 1rem; }
    .app-layout { padding: 10px 8px 28px; }
    .sidebar {
        top: 56px;
        left: 8px;
        width: calc(100vw - 16px);
        max-height: calc(100vh - 64px);
    }
    .panel { padding: 12px; border-radius: 16px; }
    .result-panel {
        flex-direction: row;
        top: 56px;
    }
    .tabs-row { flex-wrap: wrap; }
    .tabs { flex: 1; }
    .tab-button { flex: 1; text-align: center; padding: 8px 10px; font-size: .82rem; }
    .board-stage.dual-boards { grid-template-columns: 1fr; }
    .game-summary { grid-template-columns: 1fr 1fr; }
    .difficulty-row { gap: 4px; }
    .diff-btn { font-size: .78rem; padding: 5px 8px; }
    #autoPlayButton { font-size: .82rem; }
    .setup-board-col { overflow-x: auto; }
}
