:root {
    --bg: #14141f;
    --panel: #1e1e2e;
    --text: #e6e6f0;
    --muted: #8a8aa3;
    --accent: #ff9e3d;
    --accent-2: #5ec2ff;
    --error: #ff6b81;
}

body {
    font-family: system-ui, sans-serif;
    margin: 0;
    background: var(--bg);
    color: var(--text);
}

a {
    color: var(--accent-2);
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

h1 {
    color: var(--accent);
}

main {
    padding: 1.5rem 2rem;
    max-width: 1100px;
    margin: 0 auto;
}

.topbar {
    display: flex;
    flex-wrap: wrap;
    gap: 0 1.5rem;
    align-items: baseline;
    padding: 4px 12px;
    background: var(--panel);
}

.topbar a {
    display: inline-flex;
    align-items: center;
    min-height: 44px;
    touch-action: manipulation;
}

.topbar .brand {
    font-weight: 800;
    font-size: 1.4rem;
    color: var(--accent);
    letter-spacing: 0.05em;
}

/* Sits just after the brand, closer than the gap between site links. */
.topbar .tagline {
    margin-left: -1rem;
    font-size: 0.85rem;
    font-style: italic;
    opacity: 0.75;
}

/* Account control: right of the site links, with its sign-in dialog
   dropping down from it. */
.account {
    position: relative;
    margin-left: auto;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: flex-end;
    gap: 0.5rem;
}

.account-name {
    font-weight: 700;
}

.account-button {
    font: inherit;
    font-weight: 700;
    min-height: 44px;
    padding: 0.4rem 0.9rem;
    border: 2px solid var(--accent);
    border-radius: 6px;
    background: var(--panel);
    color: var(--text);
    cursor: pointer;
    touch-action: manipulation;
}

.account-button:disabled {
    opacity: 0.45;
    cursor: wait;
}

.account-primary {
    background: var(--accent);
    color: var(--bg);
}

@media (hover: hover) {
    .account-button:not(:disabled):hover {
        background: var(--accent);
        color: var(--bg);
    }
}

.account-dialog {
    position: absolute;
    top: calc(100% + 0.5rem);
    right: 0;
    z-index: 10;
    width: min(22rem, calc(100vw - 2rem));
    padding: 1rem;
    background: var(--panel);
    border: 2px solid var(--accent);
    border-radius: 8px;
    box-shadow: 0 18px 60px #0006;
}

.account-dialog label {
    display: block;
    color: var(--muted);
    font-size: 0.85rem;
}

.account-dialog input {
    box-sizing: border-box;
    width: 100%;
    min-height: 44px;
    margin: 0.35rem 0 0.75rem;
    padding: 0.4rem 0.6rem;
    font: inherit;
    color: var(--text);
    background: var(--bg);
    border: 1px solid var(--muted);
    border-radius: 6px;
}

.account-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.account-reason {
    margin: 0 0 0.75rem;
    font-weight: 700;
}

.account-error {
    color: var(--error);
}

.account-status {
    color: var(--accent-2);
    font-size: 0.85rem;
}

.account-help,
.account-notice {
    color: var(--muted);
    font-size: 0.85rem;
    line-height: 1.5;
}

.account-notice {
    flex-basis: 100%;
    margin: 0;
    text-align: right;
}

.muted {
    color: var(--muted);
}

.error {
    color: var(--error);
}

.n-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(3.5rem, 1fr));
    gap: 0.5rem;
    max-width: 40rem;
}

.n-button,
.button {
    display: inline-block;
    padding: 0.6rem 0.9rem;
    text-align: center;
    background: var(--panel);
    border: 2px solid var(--accent);
    border-radius: 6px;
    color: var(--text);
    font-weight: 700;
    min-height: 44px;
    line-height: 1.6;
    touch-action: manipulation;
}

@media (hover: hover) {
    .n-button:hover,
    .button:hover {
        background: var(--accent);
        color: var(--bg);
        text-decoration: none;
    }
}

table {
    border-collapse: collapse;
    margin-top: 1rem;
    width: 100%;
}

/* Wide leaderboard tables scroll sideways on narrow screens. */
.leaderboard {
    overflow-x: auto;
}

th,
td {
    padding: 0.4rem 0.8rem;
    text-align: left;
    border-bottom: 1px solid var(--panel);
    font-variant-numeric: tabular-nums;
}

th {
    color: var(--muted);
    font-weight: 600;
}

.long-expr {
    cursor: help;
}

/* A name typed before accounts, so not a verified username. */
.legacy {
    color: var(--muted);
    cursor: help;
}

.badge.legacy-badge {
    background: var(--muted);
}

.badge {
    margin-left: 0.5rem;
    padding: 0.05rem 0.4rem;
    border-radius: 4px;
    font-size: 0.75rem;
    background: var(--accent-2);
    color: var(--bg);
}

svg.arrangement {
    width: min(90vw, 480px);
    height: auto;
    display: block;
    margin: 1rem 0;
}

svg.arrangement .container {
    fill: var(--panel);
    stroke: var(--muted);
    stroke-width: 0.02;
}

svg.arrangement .square {
    stroke: var(--bg);
    stroke-width: 0.02;
}

/* Play screen */

.packing-game {
    --ink: #e9edf5;
    --pg-muted: #a1adc2;
    --pg-accent: #c7f36b;
    color: var(--ink);
    --board-width: min(100%, calc(100svh - 176px));
    margin: auto;
    padding: 0;
}

.packing-game * {
    box-sizing: border-box;
}

.pg-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
}

.pg-sub {
    color: var(--pg-muted);
    font-size: 14px;
}

.pg-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    justify-items: center;
    gap: 16px;
    margin-top: 4px;
}

.pg-board {
    position: relative;
    background: #121823;
    border: 1px solid #303c50;
    border-radius: 14px;
    padding: 6px;
    box-shadow: 0 18px 60px #0003;
}

.pg-board canvas {
    width: 100%;
    aspect-ratio: 1;
    display: block;
    touch-action: none;
    outline: none;
    border-radius: 12px;
}

.pg-board canvas:focus-visible {
    outline: 2px solid var(--pg-accent);
}

.pg-board-footer {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    color: var(--pg-muted);
    font-size: 13px;
    padding: 12px 4px 0;
}

.pg-panel {
    background: #1c2432;
    border: 1px solid #303c50;
    border-radius: 16px;
    padding: 18px;
    margin-bottom: 14px;
}

.pg-panel h2 {
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.13em;
    color: var(--pg-muted);
    margin: 0 0 18px;
}

.pg-stat {
    font-size: 30px;
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.04em;
}

.pg-stat small {
    font-size: 12px;
    color: var(--pg-muted);
    letter-spacing: 0;
}

.pg-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin: 15px 0;
    font-size: 13px;
}

.packing-game input[type="range"] {
    width: 100%;
    height: 32px;
    accent-color: var(--pg-accent);
    touch-action: manipulation;
}

.packing-game input[type="checkbox"] {
    accent-color: var(--pg-accent);
    width: 24px;
    height: 24px;
}

.packing-game button,
.packing-game input[type="text"],
.packing-game input[type="number"] {
    font: inherit;
    border: 1px solid #445067;
    border-radius: 9px;
    background: #273348;
    color: var(--ink);
    padding: 10px 14px;
    min-height: 44px;
    touch-action: manipulation;
}

.packing-game button {
    cursor: pointer;
    font-size: 14px;
    font-weight: 700;
}

@media (hover: hover) {
    .packing-game button:hover {
        background: #35455e;
    }
}

.pg-turn {
    display: inline-flex;
    gap: 8px;
}

.packing-game .pg-turn button {
    min-width: 44px;
    font-size: 20px;
    line-height: 1;
}

/* Mouse hints by default; touch devices get touch instructions instead. */
.pg-hint-touch {
    display: none;
}

@media (pointer: coarse) {
    .pg-hint-mouse {
        display: none;
    }

    .pg-hint-touch {
        display: inline;
    }

    /* Native range thumbs are too small to grab with a finger; the thumb can
       only be resized once the native appearance is replaced. */
    .packing-game input[type="range"] {
        -webkit-appearance: none;
        appearance: none;
        height: 44px;
        background: transparent;
    }

    .packing-game input[type="range"]::-webkit-slider-runnable-track {
        height: 6px;
        border-radius: 3px;
        background: #344159;
    }

    .packing-game input[type="range"]::-webkit-slider-thumb {
        -webkit-appearance: none;
        width: 28px;
        height: 28px;
        margin-top: -11px;
        border-radius: 50%;
        border: 2px solid #17200d;
        background: var(--pg-accent);
    }

    .packing-game input[type="range"]::-moz-range-track {
        height: 6px;
        border-radius: 3px;
        background: #344159;
    }

    .packing-game input[type="range"]::-moz-range-thumb {
        width: 28px;
        height: 28px;
        border-radius: 50%;
        border: 2px solid #17200d;
        background: var(--pg-accent);
    }
}

.packing-game button:disabled {
    opacity: 0.45;
    cursor: wait;
}

.packing-game .pg-primary {
    background: var(--pg-accent);
    color: #17200d;
    border-color: var(--pg-accent);
}

.pg-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.pg-force-actions {
    margin-top: 18px;
}

/* The Squeeze slider beside Settle: held, it presses the band in; let go,
   the box springs back out. */
.pg-squeeze .pg-row {
    margin: 12px 0 0;
    font-weight: 700;
}

.pg-squeeze output {
    font-variant-numeric: tabular-nums;
}

.pg-squeeze .pg-help {
    margin: 0 0 12px;
}

.pg-wide {
    width: 100%;
}

.pg-help {
    font-size: 12px;
    line-height: 1.65;
    color: var(--pg-muted);
}

.pg-status {
    font-size: 12px;
    line-height: 1.6;
    min-height: 42px;
    color: var(--pg-accent);
}

.pg-invalid {
    color: #ff9c9c;
}

/* A Share that finally failed: an alert that stands out from status text. */
.pg-share-error {
    margin: 8px 0;
    padding: 10px 12px;
    border: 1px solid #ff9c9c;
    border-radius: 9px;
    background: #3a1f27;
    color: #ffd6d6;
    font-size: 13px;
}

/* Share or Submit waiting on a sign-in, or dropped because none came. */
.pg-sign-in {
    margin: 8px 0;
    padding: 10px 12px;
    border: 1px solid var(--pg-accent);
    border-radius: 9px;
    background: #26301b;
    color: var(--ink);
    font-size: 13px;
}

.pg-field {
    width: 100%;
    margin: 8px 0;
}

.packing-game a {
    color: var(--pg-accent);
}

.pg-meter {
    height: 5px;
    background: #344159;
    border-radius: 4px;
    margin: 12px 0;
    overflow: hidden;
}

.pg-meter span {
    display: block;
    height: 100%;
    background: var(--pg-accent);
}

.pg-count {
    width: 75px;
}

/* Benchmark bubble: side as a percentage of the literature reference */

.pg-benchmark {
    display: inline-flex;
    align-items: center;
    gap: 12px;
    padding: 8px 18px 8px 14px;
    margin: 12px 0;
    border-radius: 999px;
    background: #1c2432;
    border: 1px solid #303c50;
    box-shadow: 0 8px 30px #0004;
    color: #e9edf5;
}

.pg-benchmark-pct {
    font-size: 22px;
    font-weight: 800;
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.03em;
}

.pg-benchmark-text {
    display: flex;
    flex-direction: column;
    line-height: 1.3;
}

.pg-benchmark-text strong {
    font-size: 13px;
}

.pg-benchmark-text small {
    font-size: 11px;
    color: var(--muted);
}

.pg-benchmark.is-near .pg-benchmark-pct {
    color: #5ec2ff;
}

.pg-benchmark.is-match {
    border-color: #c7f36b;
}

.pg-benchmark.is-match .pg-benchmark-pct {
    color: #c7f36b;
}

.pg-benchmark.is-record {
    border-color: #ffcf4d;
    box-shadow: 0 0 0 2px #ffcf4d55, 0 8px 30px #0004;
}

.pg-benchmark.is-record .pg-benchmark-pct {
    color: #ffcf4d;
}

.pg-benchmark.is-suspect {
    border-color: #ff9c9c;
}

.pg-benchmark.is-suspect .pg-benchmark-pct {
    color: #ff9c9c;
}

.pg-benchmark.is-missing .pg-benchmark-pct {
    color: var(--muted);
}

.pg-details {
    margin-top: 16px;
    font-size: 12px;
    line-height: 1.6;
    color: var(--pg-muted);
}

/* Everything beyond drag and Settle, collapsed by default. */
.pg-advanced summary {
    cursor: pointer;
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.13em;
    color: var(--pg-muted);
    min-height: 44px;
    display: flex;
    align-items: center;
}

.pg-advanced[open] summary {
    margin-bottom: 8px;
}

/* The playfield gets the viewport; supporting panels follow below it. */
main:has(.packing-game) {
    max-width: none;
    padding: 8px;
}

.pg-stage {
    width: 100%;
    margin-inline: auto;
}

.pg-top {
    width: min(100%, max(360px, calc(100svh - 176px)));
    margin-inline: auto;
    flex-wrap: nowrap;
    gap: 8px;
    min-height: 52px;
}

.pg-top .pg-benchmark {
    padding: 0;
    margin: 0;
    border: 0;
    border-radius: 0;
    background: transparent;
    box-shadow: none;
    gap: 8px;
}

.pg-top .pg-benchmark-pct {
    font-size: clamp(24px, 3vw, 32px);
}

.pg-top > label {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

.pg-board-footer {
    padding: 0 4px;
    flex-wrap: nowrap;
    gap: 12px;
}

.pg-squeeze {
    display: flex;
    align-items: center;
    flex: 1;
    min-width: 0;
    gap: 10px;
}

.pg-squeeze .pg-row {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 0;
    margin: 0;
    font-size: 12px;
}

.pg-squeeze output {
    color: var(--pg-muted);
    font-variant-numeric: tabular-nums;
}

.pg-squeeze input[type="range"] {
    min-width: 0;
    height: 44px;
    margin: 0;
}

.pg-sidebar {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-items: start;
    gap: 12px;
    width: min(100%, 960px);
}

.pg-sidebar .pg-panel {
    margin: 0;
}

.pg-instructions {
    grid-column: 1 / -1;
}

.pg-board-notes {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 12px;
    color: var(--pg-muted);
    font-size: 12px;
}

@media (max-width: 600px) {
    .topbar {
        gap: 0 12px;
    }

    .topbar .tagline {
        display: none;
    }

    .pg-sidebar {
        grid-template-columns: minmax(0, 1fr);
    }

    .pg-benchmark-text strong {
        font-size: 11px;
    }

    .pg-benchmark-text small {
        font-size: 10px;
    }
}

/* Native top-layer dialog: the page behind it is inert. */
.account-modal {
    padding: 0;
    margin: auto;
    width: min(34rem, calc(100vw - 2rem));
    max-height: calc(100dvh - 2rem);
    overflow: auto;
    border: 1px solid #565f89;
    border-radius: 1rem;
    background: #1a1b26;
    color: #c0caf5;
    box-shadow: 0 1rem 4rem #0008;
}
.account-modal::backdrop { background: #090d18b8; }
.account-modal .account-dialog { position: static; width: auto; margin: 0; border: 0; padding: 1.5rem; box-shadow: none; }
.account-modal h2 { margin-top: 0; }
.account-choices { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.account-choices button { min-height: 7rem; display: flex; flex-direction: column; justify-content: center; gap: .6rem; white-space: normal; }
.account-choices strong { font-size: 1.3rem; }
.account-availability { font-size: .85rem; min-height: 2.5em; }
.account-modal button { min-height: 44px; }
@media (max-width: 450px) { .account-choices { grid-template-columns: 1fr; } }

/* Corner controls sit on the live outer square, inside the canvas frame. */
.pg-canvas-frame { position: relative; }
.packing-game .pg-corner {
    position: absolute;
    transform: translate(-50%, -50%);
    width: 44px;
    height: 44px;
    padding: 0;
    border: 1px solid #c7f36b88;
    border-radius: 50%;
    color: var(--pg-accent);
    background: #17231fe8;
    font-size: 24px;
    cursor: grab;
    touch-action: none;
    user-select: none;
}
.packing-game .pg-corner:active { cursor: grabbing; background: #33452b; }
.pg-corner:focus-visible { outline: 2px solid var(--pg-accent); outline-offset: 2px; }
.pg-corner-hint { font-size: 12px; }

/* Use the spare horizontal room only when the screen is landscape. */
@media (min-aspect-ratio: 1/1) {
    .pg-layout {
        grid-template-columns: minmax(0, 1fr) clamp(240px, 26vw, 340px);
        align-items: start;
        gap: 12px;
    }
    .pg-sidebar {
        display: block;
        width: 100%;
    }
    .pg-sidebar .pg-panel { margin-bottom: 12px; }
}

.pg-board { width: var(--board-width); margin-inline: auto; }
@media (max-height: 500px) and (min-aspect-ratio: 1/1) {
    .pg-top .pg-benchmark-text strong { display: none; }
    .pg-corner-hint { display: none; }
    .pg-board-footer { justify-content: center; }
}

/* Shape and count stay together above the primary play actions. */
.pg-picker { display: grid; gap: 8px; }

/* Direct manipulation must not start browser text selection (#50). Keep
   editable fields and useful prose/link output selectable. */
.packing-game {
    -webkit-user-select: none;
    user-select: none;
}
.pg-canvas-frame, .pg-corner, .pg-game-picker {
    -webkit-touch-callout: none;
}
.packing-game input, .packing-game textarea, .packing-game a, .pg-share-link, .pg-help, .pg-notes {
    -webkit-user-select: text;
    user-select: text;
}
.pg-game-picker { justify-self:start; width:fit-content; display:flex; align-items:center; gap:1rem; min-height:56px; padding:.3rem .9rem; }
.pg-game-picker strong { font-size:1.5rem; }
.pg-game-picker svg { width:48px; height:48px; }
.pg-game-picker .enclosing { fill:none; stroke:currentColor; stroke-width:2; }
.pg-game-picker .enclosed { fill:#c7f36b; }
.pg-picker-modal { color:var(--text, #e2e8ef); background:#17202d; border:1px solid #52647a; border-radius:1rem; padding:1.25rem; width:min(480px, calc(100vw - 2rem)); max-height:calc(100dvh - 2rem); overflow:auto; }
.pg-picker-modal::backdrop { background:rgb(0 0 0 / .65); }
.pg-picker-modal form { display:grid; gap:1rem; }
.pg-picker-modal fieldset { border:0; padding:0; min-width:0; }
.pg-picker-modal legend { margin-bottom:.5rem; }
.pg-shape-options { display:grid; grid-template-columns:repeat(4,minmax(0,1fr)); gap:.4rem; }
.pg-shape-options button { display:flex; align-items:center; flex-direction:column; min-width:0; min-height:64px; padding:.4rem; font-size:.8rem; }
.pg-shape-options svg { width:32px; height:32px; fill:none; stroke:currentColor; stroke-width:3; }
.pg-shape-options [aria-pressed="true"] { border-color:#c7f36b; color:#c7f36b; background:#27332b; }

.pg-picker-confirm { background:#c7f36b; color:#101722; font-weight:700; }
