:root {
    color-scheme: light;
    /* ink-on-paper palette */
    --paper: #f6f2e9;
    --paper-edge: #efe9da;
    --line: #ddd5c2;
    --ink: #2c3a46;
    --ink-soft: #8a9aa6;
    --number-idle: #b3aa95;
    --number-strong: #2c3a46;
    --muted-text: #8c8470;
    --illegal: #c0492f;
    --given-tint: rgba(44, 58, 70, 0.06);
    --ink-given: #41525f;
    --glow: #cdbf95;
    --hover: rgba(255, 255, 255, 0.5);
    --line-hover: #cfc6b0;
    --board-highlight: rgba(255, 255, 255, 0.9);
    --board-shadow: rgba(0, 0, 0, 0.55);
    --cell: clamp(34px, 12vw, 46px);
    --ship-inset: 20%;
    --ship-overlap: -1px;
    --ship-round: 999px;
}

[data-theme="dark"] {
    color-scheme: dark;
    /* ink-on-slate: light marks on a dark ground */
    --paper: #14181d;
    --paper-edge: #1b2229;
    --line: #2d3742;
    --ink: #ccd7e0;
    --ink-soft: #5f6f7d;
    --number-idle: #5a6573;
    --number-strong: #ccd7e0;
    --muted-text: #76838f;
    --illegal: #e2674f;
    --given-tint: rgba(204, 215, 224, 0.06);
    --ink-given: #7e8e9b;
    --glow: #4f6172;
    --hover: rgba(255, 255, 255, 0.06);
    --line-hover: #455160;
    --board-highlight: rgba(255, 255, 255, 0.04);
    --board-shadow: rgba(0, 0, 0, 0.5);
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    height: 100%;
    margin: 0;
}

body {
    background: var(--paper);
    color: var(--ink);
    font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 22px;
    padding: 24px;
    overscroll-behavior: none;
}

header {
    text-align: center;
    line-height: 1.3;
}

header h1 {
    margin: 0;
    font-size: 15px;
    font-weight: 600;
    letter-spacing: 0.22em;
    text-transform: uppercase;
}

header .caption {
    margin: 4px 0 0;
    min-height: 16px;
    font-size: 12px;
    letter-spacing: 0.04em;
    font-variant-numeric: tabular-nums;
    color: var(--muted-text);
}

.board {
    display: grid;
    grid-template-columns: repeat(var(--size), var(--cell)) auto;
    grid-template-rows: repeat(var(--size), var(--cell)) auto;
    background: var(--paper-edge);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 10px;
    touch-action: none;
    user-select: none;
    box-shadow: 0 1px 0 var(--board-highlight) inset, 0 6px 24px -18px var(--board-shadow);
}

.cell {
    width: var(--cell);
    height: var(--cell);
    border-right: 1px solid var(--line);
    border-bottom: 1px solid var(--line);
    display: grid;
    place-items: center;
    position: relative;
    cursor: pointer;
}

.cell[data-column="0"] {
    border-left: 1px solid var(--line);
}

.cell[data-row="0"] {
    border-top: 1px solid var(--line);
}

.cell.given {
    background: var(--given-tint);
    cursor: default;
}

.board.placeholder .cell {
    cursor: default;
}

.glyph {
    width: 78%;
    height: 78%;
    display: grid;
    place-items: center;
    transition: transform 0.08s ease, opacity 0.12s ease;
}

/* water = a small pair of waves, drawn as a mask so it tints with the theme */
.cell.water .glyph {
    --wave: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cg fill='none' stroke='%23000' stroke-width='1.7' stroke-linecap='round'%3E%3Cpath d='M3 9q3-3 6 0t6 0 6 0'/%3E%3Cpath d='M3 15q3-3 6 0t6 0 6 0'/%3E%3C/g%3E%3C/svg%3E");
    -webkit-mask: var(--wave) center / 72% no-repeat;
    mask: var(--wave) center / 72% no-repeat;
    background-color: var(--ink-soft);
    opacity: 0.55;
}

/* ship = up to two ink bars of equal thickness: a horizontal one drawn by
   ::before, a vertical one by ::after. Each bar reaches the tile edge on a
   connected side and rounds off on an open side, so straight runs, ends,
   lone submarines, bends and crosses all keep the same thickness. ::before
   doubles as the lone-submarine circle when nothing connects. */
.cell.ship .glyph {
    position: absolute;
    inset: 0;
    width: auto;
    height: auto;
}

.cell.ship .glyph::before,
.cell.ship .glyph::after {
    content: "";
    position: absolute;
    top: var(--ship-inset);
    right: var(--ship-inset);
    bottom: var(--ship-inset);
    left: var(--ship-inset);
    background: var(--ink);
    border-radius: var(--ship-round);
}

/* the vertical bar exists only when the ship runs vertically */
.cell.ship .glyph::after {
    display: none;
}

.cell.ship.connect-up .glyph::after,
.cell.ship.connect-down .glyph::after {
    display: block;
}

.cell.ship.connect-left .glyph::before {
    left: var(--ship-overlap);
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;
}

.cell.ship.connect-right .glyph::before {
    right: var(--ship-overlap);
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
}

.cell.ship.connect-up .glyph::after {
    top: var(--ship-overlap);
    border-top-left-radius: 0;
    border-top-right-radius: 0;
}

.cell.ship.connect-down .glyph::after {
    bottom: var(--ship-overlap);
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
}

.cell.ship.illegal .glyph::before,
.cell.ship.illegal .glyph::after {
    background: var(--illegal);
}

.cell.given.ship .glyph::before,
.cell.given.ship .glyph::after {
    background: var(--ink-given);
}

.cell.press .glyph {
    transform: scale(0.86);
}

.number {
    display: grid;
    place-items: center;
    font-variant-numeric: tabular-nums;
    font-weight: 600;
    font-size: 15px;
    color: var(--number-strong);
    transition: color 0.2s ease;
}

.number.row-number {
    width: 30px;
}

.number.column-number {
    height: 30px;
}

.number.done {
    color: var(--number-idle);
    font-weight: 500;
}

.generator {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    justify-content: center;
    gap: 12px;
    margin-bottom: 14px;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 10px;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--muted-text);
}

.field input,
.field select {
    font: inherit;
    font-size: 13px;
    letter-spacing: normal;
    text-transform: none;
    color: var(--ink);
    background: transparent;
    border: 1px solid var(--line);
    border-radius: 8px;
    height: 34px;
    padding: 0 10px;
}

.field input[type="number"] {
    width: 60px;
}

.stats {
    margin: 0;
    min-height: 16px;
    text-align: center;
    font-size: 12px;
    letter-spacing: 0.04em;
    font-variant-numeric: tabular-nums;
    color: var(--muted-text);
}

button.busy {
    animation: pulse 1s ease-in-out infinite;
}

@keyframes pulse {
    50% {
        opacity: 0.35;
    }
}

.fleet {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 16px;
    min-height: 14px;
}

.fleet-group {
    display: flex;
    align-items: center;
    gap: 7px;
}

/* one fused capsule per ship, matching the board: a circle for a submarine,
   a rounded bar for anything longer. The parent clips the row of segments to
   a pill, and thin gaps between segments show the part count. */
.fleet-ship {
    --fleet-unit: 13px;
    display: flex;
    gap: 1.5px;
    height: var(--fleet-unit);
    border-radius: 999px;
    overflow: hidden;
    opacity: 0.22;
    transition: opacity 0.2s ease;
}

.fleet-ship.filled {
    opacity: 1;
}

.fleet-segment {
    width: var(--fleet-unit);
    height: 100%;
    background: var(--ink);
}

.controls {
    display: flex;
    gap: 10px;
}

button {
    font: inherit;
    font-size: 13px;
    letter-spacing: 0.04em;
    color: var(--ink);
    background: transparent;
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 8px 16px;
    cursor: pointer;
    transition: background 0.15s ease, border-color 0.15s ease;
}

button:hover {
    background: var(--hover);
    border-color: var(--line-hover);
}

button:active {
    transform: translateY(1px);
}

.board.solved {
    animation: glow 1.4s ease;
}

@keyframes glow {
    50% {
        box-shadow: 0 1px 0 var(--board-highlight) inset, 0 0 0 3px var(--glow);
    }
}

.toolbar {
    position: fixed;
    top: 14px;
    right: 14px;
    z-index: 5;
    display: flex;
    gap: 8px;
}

.icon-button {
    width: 36px;
    height: 36px;
    padding: 0;
    display: grid;
    place-items: center;
    font-size: 15px;
    line-height: 1;
}

.modal {
    position: fixed;
    inset: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    background: rgba(0, 0, 0, 0.4);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s ease, visibility 0s linear 0.2s;
}

.modal.open {
    opacity: 1;
    visibility: visible;
    transition: opacity 0.2s ease;
}

.modal-card {
    width: 100%;
    max-width: 380px;
    max-height: 100%;
    overflow-y: auto;
    background: var(--paper-edge);
    color: var(--ink);
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 24px 26px;
    box-shadow: 0 14px 44px -14px var(--board-shadow);
    transform: scale(0.97);
    transition: transform 0.2s ease;
}

.modal.open .modal-card {
    transform: scale(1);
}

@media (prefers-reduced-motion: reduce) {
    .modal,
    .modal-card {
        transition: none;
    }
}

.modal-card h2 {
    margin: 0 0 12px;
    font-size: 14px;
    font-weight: 600;
    letter-spacing: 0.18em;
    text-transform: uppercase;
    text-align: center;
}

.modal-card p {
    margin: 0 0 12px;
    font-size: 13px;
    line-height: 1.55;
}

.modal-subhead {
    font-size: 10px;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--muted-text);
}

.modal-card ul {
    margin: 0 0 16px;
    padding-left: 18px;
}

.modal-card li {
    font-size: 13px;
    line-height: 1.55;
    margin-bottom: 7px;
}

.modal-close {
    display: block;
    margin: 4px auto 0;
}

body {
    transition: background-color 0.25s ease, color 0.25s ease;
}
