:root {
    --bg: #f4f7fa;
    --panel: #ffffff;
    --text: #1b2733;
    --muted: #5d6b78;
    --border: #c9d4de;
    --water: #dbe9f4;
    --water-hover: #c3dbee;
    --ship: #5b6b7a;
    --hit: #d9483b;
    --sunk: #3d0f0b;
    --sunk-mark: #ff7b6b;
    --miss: #9fb3c4;
    --ring: #e8eef3;
    --ok: #3a9d5d;
    --bad: #d9483b;
    --accent: #1f6fb2;
    --wait: #c26a12;
    --last: #e0a800;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #10161c;
        --panel: #18212a;
        --text: #e3e9ef;
        --muted: #93a2b0;
        --border: #2c3a47;
        --water: #1d3346;
        --water-hover: #264560;
        --ship: #8796a4;
        --hit: #ef5a4c;
        --sunk: #050709;
        --sunk-mark: #ff6b5c;
        --miss: #4d6377;
        --ring: #16232f;
        --ok: #4cbf75;
        --bad: #ef5a4c;
        --accent: #4b9be0;
        --wait: #b8620f;
        --last: #ffd23f;
    }
}

* { box-sizing: border-box; }

body {
    margin: 0;
    padding: 16px;
    background: var(--bg);
    color: var(--text);
    font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

header { text-align: center; }
h1 { margin: 0 0 4px; font-size: 1.8rem; }
h2 { margin: 0 0 8px; font-size: 1.1rem; text-align: center; }

.status { min-height: 1.5em; margin: 0 0 12px; font-weight: 600; }
.status.win { color: var(--ok); }
.status.lose { color: var(--bad); }

.controls {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
    margin-bottom: 16px;
}

button {
    font: inherit;
    padding: 8px 14px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--panel);
    color: var(--text);
    cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: 0.5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }

.boards {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 24px;
}

.board-panel {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 12px;
    width: 100%;
    max-width: 400px;
}

.board {
    display: grid;
    grid-template-columns: repeat(11, 1fr);
    gap: 2px;
    user-select: none;
}

.label {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    color: var(--muted);
    aspect-ratio: 1;
}

.cell {
    aspect-ratio: 1;
    padding: 0;
    border: none;
    border-radius: 3px;
    background: var(--water);
    position: relative;
    cursor: default;
}
.cell:hover:not(:disabled) { border: none; }

.cell.ship { background: var(--ship); }
.cell.ring { background: var(--ring); }
.cell.miss::after,
.cell.ring::after {
    content: "";
    position: absolute;
    inset: 38%;
    border-radius: 50%;
    background: var(--miss);
}
.cell.hit { background: var(--hit); }
.cell.sunk { background: var(--sunk); }
.cell.hit::after,
.cell.sunk::after {
    content: "✕";
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 0.8rem;
    font-weight: 700;
}

/* Et sænket skib tegnes som ét samlet skrog: afrundede ender, og
   mellemrummet mellem felterne fyldes med en skygge (join-r/join-d). Skibe
   rører aldrig hinanden, så nabofelter med '#' hører altid til samme skib. */
.cell.sunk { border-radius: 40%; }
.cell.sunk::after { color: var(--sunk-mark); font-size: 1rem; }
.cell.sunk.join-l { border-top-left-radius: 0; border-bottom-left-radius: 0; }
.cell.sunk.join-r { border-top-right-radius: 0; border-bottom-right-radius: 0; box-shadow: 2px 0 0 var(--sunk); }
.cell.sunk.join-u { border-top-left-radius: 0; border-top-right-radius: 0; }
.cell.sunk.join-d { border-bottom-left-radius: 0; border-bottom-right-radius: 0; box-shadow: 0 2px 0 var(--sunk); }

.cell.just-sunk { animation: sink 1.2s ease-out; z-index: 1; }
@keyframes sink {
    0% { transform: scale(1.3); background: var(--hit); }
    100% { transform: scale(1); background: var(--sunk); }
}
@media (prefers-reduced-motion: reduce) {
    .cell.just-sunk { animation: none; }
}

.cell.preview { background: var(--ok); }
.cell.preview-bad { background: var(--bad); opacity: 0.7; }

.board.targetable .cell.open { cursor: crosshair; }
.board.targetable .cell.open:hover { background: var(--water-hover); }
.board.placing .cell { cursor: pointer; }

.fleet {
    list-style: none;
    padding: 0;
    margin: 10px 0 0;
    display: flex;
    flex-wrap: wrap;
    gap: 4px 12px;
    font-size: 0.85rem;
    color: var(--muted);
}
.fleet li { display: inline-flex; align-items: center; gap: 6px; }
.fleet .pips { display: inline-flex; gap: 2px; }
.fleet .pip { width: 8px; height: 8px; border-radius: 2px; background: var(--ship); }
.fleet li.sunk { text-decoration: line-through; color: var(--bad); font-weight: 600; }
.fleet li.sunk .pip { background: var(--hit); }
.fleet li.current { color: var(--accent); font-weight: 600; }

.stats { text-align: center; color: var(--muted); margin-top: 8px; }
.last-event {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin: 0 auto 16px;
    min-height: 1.8em;
    font-size: 1.05rem;
}
.last-event:empty { display: none; }
.badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 0.8rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    color: #fff;
}
.badge.MISS { background: var(--miss); color: var(--text); }
.badge.HIT { background: var(--hit); }
.badge.SUNK { background: var(--sunk); color: var(--sunk-mark); outline: 1px solid var(--sunk-mark); }

/* Det seneste skud på hvert bræt: en gul ramme, og en ring der pulserer
   kort, når skuddet lige er landet. */
.cell.last-shot { outline: 3px solid var(--last); outline-offset: 1px; z-index: 2; }
.cell.new-shot::before {
    content: "";
    position: absolute;
    inset: -4px;
    border-radius: 50%;
    border: 3px solid var(--last);
    animation: ping 1.2s ease-out 2;
    pointer-events: none;
}
@keyframes ping {
    0% { transform: scale(0.6); opacity: 1; }
    100% { transform: scale(2.2); opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
    .cell.new-shot::before { animation: none; opacity: 0; }
}
.muted { color: var(--muted); }
[hidden] { display: none !important; }

.error {
    max-width: 480px;
    margin: 0 auto 12px;
    padding: 8px 12px;
    border-radius: 6px;
    background: color-mix(in srgb, var(--bad) 15%, var(--panel));
    color: var(--bad);
}

.lobby {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
}

.card {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 16px;
    width: 100%;
    max-width: 480px;
}

.card label { display: block; font-weight: 600; margin-bottom: 4px; }

.card input[type=text] {
    width: 100%;
    font: inherit;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg);
    color: var(--text);
}

.lobby-buttons { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; align-items: center; }
.lobby-buttons p { margin: 0; width: 100%; }
.busy { margin: 0; font-weight: 600; }

table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th, td { text-align: left; padding: 6px 4px; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 600; }

/* Tur-markering: banner + det bræt der bliver skudt på får en kraftig ramme,
   det andet tones ned. */
.turn-banner {
    max-width: 824px;
    margin: 0 auto 16px;
    padding: 12px 16px;
    border-radius: 8px;
    text-align: center;
    font-size: 1.4rem;
    font-weight: 700;
    color: #fff;
}
.turn-banner.yours { background: var(--accent); animation: pulse 1.6s ease-in-out infinite; }
.turn-banner.theirs { background: var(--wait); }

@keyframes pulse {
    0%, 100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--accent) 60%, transparent); }
    50% { box-shadow: 0 0 0 8px color-mix(in srgb, var(--accent) 0%, transparent); }
}
@media (prefers-reduced-motion: reduce) {
    .turn-banner.yours { animation: none; }
}

.board-panel { transition: opacity 0.2s, box-shadow 0.2s; }
.board-panel.active-target { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent); }
.board-panel.under-fire { border-color: var(--wait); box-shadow: 0 0 0 3px var(--wait); }
.board-panel.inactive { opacity: 0.55; }
