/* Colour Flood Duel
   Layout: board-first, with a slim control rail beside it. On phones the rail stacks under the board.
   In embed mode (inside a game portal's frame) everything fits the window with no scrolling. */

@font-face {
    font-family: "Dela Gothic One";
    src: url("fonts/dela-gothic-one-latin-400-normal.woff2") format("woff2");
    font-weight: 400;
    font-display: swap;
}
@font-face {
    font-family: "DM Sans";
    src: url("fonts/dm-sans-latin-400-normal.woff2") format("woff2");
    font-weight: 400;
    font-display: swap;
}
@font-face {
    font-family: "DM Sans";
    src: url("fonts/dm-sans-latin-500-normal.woff2") format("woff2");
    font-weight: 500;
    font-display: swap;
}
@font-face {
    font-family: "DM Sans";
    src: url("fonts/dm-sans-latin-700-normal.woff2") format("woff2");
    font-weight: 700;
    font-display: swap;
}

:root {
    --bg: #eceef2;
    --surface: #f8f9fb;
    --fg: #1b1e26;
    --muted: #5b6274;
    --line: #d2d6df;
    --well: #262a33;
    --track: #8b909c;
    --focus: #2f8fb8;
    --scrim: rgba(10, 11, 14, 0.55);
    --tip-bg: #fff1c7;
    --tip-line: #e9c96a;
    --p1: #f5f0e6;
    --p2: #15171c;
    --display: "Dela Gothic One", "Arial Black", Impact, sans-serif;
    --body: "DM Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #13151a;
        --surface: #1c1f26;
        --fg: #eef0f4;
        --muted: #9aa1b2;
        --line: #2e323c;
        --well: #0b0c0f;
        --track: #5d6270;
        --tip-bg: #33301f;
        --tip-line: #6b5d2a;
        color-scheme: dark;
    }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
[hidden] { display: none !important; }

body {
    background: var(--bg);
    color: var(--fg);
    font-family: var(--body);
    font-size: 15px;
    line-height: 1.5;
}

/* ---------- Page grid ---------- */
.app {
    max-width: 1040px;
    margin: 0 auto;
    padding-block: 24px 40px;
    padding-left: max(16px, env(safe-area-inset-left, 0px));
    padding-right: max(16px, env(safe-area-inset-right, 0px));
    display: grid;
    grid-template-columns: minmax(0, 1fr) 320px;
    grid-template-areas:
        "head  head"
        "tools tools"
        "board side";
    column-gap: 24px;
    row-gap: 18px;
    align-items: start;
}
.top { grid-area: head; }
.toolbar { grid-area: tools; }
.board-wrap { grid-area: board; }
.side { grid-area: side; }

@media (max-width: 780px) {
    .app {
        grid-template-columns: minmax(0, 1fr);
        grid-template-areas: "head" "tools" "board" "side";
    }
}

/* ---------- Header ---------- */
h1 {
    font-family: var(--display);
    font-weight: 400;
    font-size: clamp(1.9rem, 5vw, 2.8rem);
    line-height: 1;
    margin: 0;
    letter-spacing: -0.01em;
    text-wrap: balance;
}
h1 .drip { display: inline-flex; gap: 4px; vertical-align: middle; margin-left: 10px; transform: translateY(-4px); }
h1 .drip i { width: 10px; height: 10px; border-radius: 3px; display: block; }
h1 .drip i:nth-child(1) { background: #e8553d; }
h1 .drip i:nth-child(2) { background: #f2b33d; }
h1 .drip i:nth-child(3) { background: #4fa36a; }
h1 .drip i:nth-child(4) { background: #2f8fb8; }
.sub { margin: 6px 0 0; color: var(--muted); }

/* ---------- Toolbar ---------- */
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px; }
select, button { font: inherit; color: inherit; }

.seg {
    display: inline-flex;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 3px;
}
.seg button {
    border: 0;
    background: none;
    padding: 6px 12px;
    border-radius: 7px;
    font-weight: 700;
    cursor: pointer;
    color: var(--muted);
}
.seg button[aria-pressed="true"] { background: var(--fg); color: var(--bg); }

.field { display: inline-flex; align-items: center; gap: 8px; }
.field span { font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); font-weight: 700; }
select {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 7px 10px;
    cursor: pointer;
}

.actions { display: flex; gap: 8px; margin-left: auto; }
.btn {
    background: var(--fg);
    color: var(--bg);
    border: 1px solid var(--fg);
    border-radius: 8px;
    padding: 8px 14px;
    font-weight: 700;
    cursor: pointer;
}
.btn.ghost { background: var(--surface); color: var(--fg); border-color: var(--line); }
.btn:hover { opacity: 0.88; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
#rulesBtn { display: none; }

@media (max-width: 780px) {
    .actions { margin-left: 0; }
}

/* ---------- Board ---------- */
.board-wrap {
    position: relative;
    width: 100%;
    max-width: 640px;
    justify-self: center;
    background: var(--well);
    border-radius: 14px;
    padding: 10px;
}
canvas {
    display: block;
    width: 100%;
    max-width: 100%;
    aspect-ratio: 1;
    border-radius: 6px;
    cursor: pointer;
    touch-action: manipulation;
}
.board-wrap.waiting canvas { cursor: wait; }

.overlay, .modal {
    display: grid;
    place-items: center;
    background: var(--scrim);
    backdrop-filter: blur(2px);
    padding: 16px;
}
.overlay { position: absolute; inset: 0; border-radius: 14px; }
.modal { position: fixed; inset: 0; z-index: 20; }

.card {
    background: var(--surface);
    color: var(--fg);
    border-radius: 14px;
    padding: 22px 26px;
    max-width: 340px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.35);
}
.overlay .card { text-align: center; }
.eyebrow { margin: 0; font-size: 12px; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); font-weight: 700; }
.card h2 { font-family: var(--display); font-weight: 400; font-size: 1.8rem; margin: 6px 0 4px; line-height: 1.1; text-wrap: balance; }
.overlay .res-sub { margin: 0 0 16px; color: var(--muted); font-variant-numeric: tabular-nums; }

.modal .card { max-width: 440px; max-height: calc(100% - 16px); overflow: auto; display: grid; gap: 12px; }
.modal h2 { font-size: 1.4rem; margin: 0; }
.modal ul { margin: 0; padding-left: 18px; display: grid; gap: 6px; font-size: 14px; }
.modal .controls-line { margin: 0; font-size: 13px; color: var(--muted); }
.modal .btn { justify-self: start; }

/* ---------- Side rail ---------- */
.side { display: grid; gap: 18px; min-width: 0; }
.panel {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 16px;
    display: grid;
    gap: 12px;
}

.player {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    border-radius: 10px;
    border: 2px solid transparent;
    transition: border-color 0.2s, background 0.2s;
}
.player.active { border-color: var(--fg); background: var(--bg); }
.chip {
    width: 26px;
    height: 26px;
    flex: none;
    border-radius: 50%;
    display: inline-grid;
    place-items: center;
    font-family: var(--display);
    font-size: 13px;
    box-shadow: 0 0 0 2px var(--line);
}
.chip.p1 { background: var(--p1); color: #15171c; }
.chip.p2 { background: var(--p2); color: #f5f0e6; }
.pname { font-weight: 700; }
.pmeta { font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.pscore { font-family: var(--display); font-size: 1.6rem; font-variant-numeric: tabular-nums; min-width: 2.5ch; text-align: right; }

.tug { display: flex; height: 12px; border-radius: 6px; overflow: hidden; background: var(--track); }
.tug span { display: block; height: 100%; transition: width 0.35s ease; }
#t1 { background: var(--p1); box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.15); }
#t2 { background: var(--p2); }
.tugmeta { display: flex; justify-content: space-between; font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; }

.status { margin: 0; font-weight: 700; display: flex; align-items: center; gap: 8px; min-height: 26px; }
.note { margin: -4px 0 0; font-size: 13px; color: var(--muted); }
.note.tip {
    margin: 0;
    display: flex;
    align-items: flex-start;
    gap: 8px;
    padding: 6px 6px 6px 10px;
    background: var(--tip-bg);
    border: 1px solid var(--tip-line);
    border-radius: 8px;
    color: var(--fg);
    font-weight: 500;
    line-height: 1.35;
}
.note.tip span { flex: 1; min-width: 0; }
.tip-x {
    flex: none;
    border: 0;
    background: none;
    color: var(--muted);
    font-size: 16px;
    line-height: 1;
    padding: 2px 4px;
    cursor: pointer;
}
.sw.pulse .blob { animation: pulse 1.1s ease-in-out infinite; }
@keyframes pulse {
    0%, 100% { box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.18), 0 0 0 0 var(--fg); transform: translateY(0); }
    50% { box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.18), 0 0 0 3px var(--fg); transform: translateY(-3px); }
}

.dots { display: inline-flex; gap: 3px; }
.dots i { width: 5px; height: 5px; border-radius: 50%; background: var(--muted); animation: blink 1s infinite; }
.dots i:nth-child(2) { animation-delay: 0.15s; }
.dots i:nth-child(3) { animation-delay: 0.3s; }
@keyframes blink { 0%, 100% { opacity: 0.25; } 50% { opacity: 1; } }

.palette { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 8px; }
.sw {
    background: none;
    border: 0;
    padding: 0;
    cursor: pointer;
    display: grid;
    gap: 4px;
    justify-items: center;
    border-radius: 10px;
}
.sw .blob {
    position: relative;
    width: 100%;
    aspect-ratio: 1;
    border-radius: 10px;
    background: var(--c);
    box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.18);
    transition: transform 0.12s ease;
}
.sw .key {
    position: absolute;
    top: 4px;
    left: 6px;
    font-size: 11px;
    font-weight: 700;
    color: rgba(255, 255, 255, 0.92);
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
}
.sw .gain { font-size: 12.5px; font-weight: 700; font-variant-numeric: tabular-nums; }
.sw:not(:disabled):hover .blob, .sw.hot .blob { transform: translateY(-3px); }
.sw:disabled { cursor: not-allowed; }
.sw:disabled .blob {
    opacity: 0.35;
    background-image: repeating-linear-gradient(135deg, rgba(255, 255, 255, 0.45) 0 4px, transparent 4px 9px);
}
.sw.wait:disabled .blob { opacity: 0.6; background-image: none; }
.sw:disabled .gain { color: var(--muted); font-weight: 500; }
.hint { margin: 0; font-size: 12.5px; color: var(--muted); }

.legend { display: grid; gap: 6px; font-size: 12.5px; color: var(--muted); }
.lg { display: flex; align-items: center; gap: 8px; min-width: 0; }
.lg b { color: var(--fg); font-weight: 700; }
.ico { width: 22px; height: 22px; flex: none; display: block; }

.rules h3 { margin: 0; font-size: 12px; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); }
.rules ul { margin: 0; padding-left: 18px; display: grid; gap: 6px; font-size: 14px; }

@media (max-width: 780px) {
    .palette { gap: 10px; }
    .pick { order: -1; }
}

/* =========================================================
   Embed mode: fit the whole game in the frame, no scrolling.
   Tuned for 16:9 frames from 800×450 up to 1920×1080.
   ========================================================= */
html.embed, html.embed body { height: 100%; overflow: hidden; }
html.embed body { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }

html.embed .app {
    --pad: clamp(8px, 2.4vh, 24px);
    --gap: clamp(12px, 2.8vh, 32px);
    --rail: clamp(250px, 32vw, 440px);
    height: 100%;
    max-width: none;
    margin: 0;
    padding: var(--pad);
    grid-template-columns: auto var(--rail);
    grid-template-rows: auto auto minmax(0, 1fr);
    grid-template-areas:
        "board head"
        "board tools"
        "board side";
    column-gap: var(--gap);
    row-gap: clamp(6px, 1.5vh, 14px);
    justify-content: center;
    align-content: center;
    align-items: start;
}
html.embed .board-wrap {
    width: min(calc(100vh - 2 * var(--pad)), calc(100vw - var(--rail) - var(--gap) - 2 * var(--pad)));
    max-width: none;
    align-self: center;
    padding: clamp(6px, 1.2vh, 12px);
}
html.embed h1 { font-size: clamp(1.15rem, 4.6vh, 2.3rem); }
html.embed h1 .drip { display: none; }
html.embed .sub, html.embed .hint, html.embed .rules { display: none; }
html.embed #rulesBtn { display: inline-block; }

html.embed .toolbar { gap: clamp(6px, 1.2vh, 10px) 8px; font-size: clamp(12px, 2.6vh, 15px); }
html.embed .seg button { padding: clamp(3px, 0.8vh, 6px) 10px; }
html.embed select { padding: clamp(3px, 0.8vh, 7px) 8px; }
html.embed .btn { padding: clamp(4px, 1vh, 8px) 12px; }
html.embed .field span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
html.embed .actions { margin-left: 0; }

html.embed .side { gap: clamp(6px, 1.5vh, 14px); align-self: stretch; align-content: start; overflow: auto; min-height: 0; }
html.embed .panel { padding: clamp(8px, 1.8vh, 16px); gap: clamp(6px, 1.3vh, 12px); }
html.embed .player { padding: clamp(4px, 0.9vh, 10px) 10px; gap: 10px; }
html.embed .pname { font-size: clamp(13px, 2.8vh, 15px); line-height: 1.25; }
html.embed .pmeta { font-size: 12px; line-height: 1.3; }
html.embed .pscore { font-size: clamp(1.1rem, 4.4vh, 1.6rem); }
html.embed .status { font-size: clamp(13px, 2.8vh, 15px); min-height: 0; }
html.embed .note { font-size: 12px; line-height: 1.35; }
html.embed .tug { height: clamp(8px, 1.6vh, 12px); }
html.embed .tugmeta { font-size: 12px; line-height: 1.3; }
html.embed .palette { gap: clamp(5px, 1.2vh, 8px); }
html.embed .sw .gain { font-size: 12px; line-height: 1.2; }
html.embed .pick { order: -1; }
/* short frames (phones in landscape, small desktop frames): tighten the score panel */
@media (max-height: 560px) {
    html.embed .pmeta, html.embed .tugmeta { display: none; }
    /* the two scores sit side by side on one line */
    html.embed .score { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 6px; padding: 6px; }
    html.embed .score .tug { grid-column: 1 / -1; height: 8px; }
    html.embed .player { padding: 2px 5px; gap: 4px; }
    html.embed .player .chip { width: 18px; height: 18px; font-size: 10px; }
    html.embed .pname { font-size: 12px; letter-spacing: -0.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    html.embed .pscore { font-size: 1.05rem; min-width: 0; }
    html.embed .note { font-size: 11.5px; }
    html.embed .note.tip { padding: 4px 4px 4px 8px; }
}
/* very short frames (browser zoom, small laptops): the portal already shows the title */
@media (max-height: 420px) {
    html.embed .top { display: none; }
    html.embed .legend, html.embed .score .tug { display: none; }
}
html.embed .legend { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 12px; }
html.embed .lg-d { display: none; }
html.embed .ico { width: clamp(14px, 3vh, 20px); height: clamp(14px, 3vh, 20px); }

/* Portrait frames: board on top, rail below, scrolling allowed */
@media (max-aspect-ratio: 1/1) {
    html.embed, html.embed body { overflow: auto; height: auto; }
    html.embed .app {
        height: auto;
        grid-template-columns: minmax(0, 1fr);
        grid-template-rows: none;
        grid-template-areas: "head" "tools" "board" "side";
        --rail: 0px;
    }
    html.embed .board-wrap { width: min(100%, 70vh); justify-self: center; }
    html.embed .side { overflow: visible; }
    html.embed .palette { grid-template-columns: repeat(6, minmax(0, 84px)); justify-content: space-between; }
    html.embed .pick { order: -1; }
}

@media (prefers-reduced-motion: reduce) {
    .tug span, .sw .blob, .player { transition: none; }
    .dots i { animation: none; opacity: 0.7; }
    .sw.pulse .blob { animation: none; box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.18), 0 0 0 3px var(--fg); }
}
