:root {
    --bg: #0e1116;
    --panel: #141a22;
    --panel2: #10151c;
    --text: #e7edf7;
    --muted: #a8b3c7;
    --accent: #7aa2ff;
    --good: #6ee7b7;
    --bad: #fb7185;
    --border: rgba(255, 255, 255, .10);
    --shadow: 0 12px 30px rgba(0, 0, 0, .35);
}

* {
    box-sizing: border-box;
}

/* Under-finger preview: hidden by default (desktop) */
#under,
.underLabel {
    display: none;
}

body {
    margin: 0;
    background: radial-gradient(1200px 700px at 20% 0%, #152033 0%, var(--bg) 55%, #0b0d12 100%);
    color: var(--text);
    font: 14px/1.4 system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;

    /* App shell: header / main / footer fits viewport */
    height: 100dvh;
    min-height: 100dvh;
    display: grid;
    grid-template-rows: auto 1fr auto;

    /* On “proper size” screens: no page scroll */
    overflow: hidden;
}

header {
    padding: 14px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border);
    background: rgba(15, 19, 26, .65);
    backdrop-filter: blur(8px);
}

header .title {
    display: flex;
    gap: 10px;
    align-items: baseline;
    font-weight: 700;
    letter-spacing: .2px;
}

header .title small {
    font-weight: 600;
    color: var(--muted);
}

header .controls {
    display: flex;
    gap: 8px;
    align-items: center;
}

.topbar {
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
}

.headerInner {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}


.title {
    font-size: 14px;
    font-weight: 600;
    letter-spacing: .02em;
}


.subtitle {
    margin-top: 2px;
    font-size: 12px;
    color: var(--muted);
    text-align: center;
    line-height: 1.3;
}

button {
    appearance: none;
    border: 1px solid var(--border);
    background: linear-gradient(180deg, rgba(255, 255, 255, .06), rgba(255, 255, 255, .03));
    color: var(--text);
    padding: 8px 10px;
    border-radius: 10px;
    cursor: pointer;
    box-shadow: 0 1px 0 rgba(255, 255, 255, .06) inset;
}

button:hover {
    border-color: rgba(255, 255, 255, .18);
}

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

.wrap {
    min-height: 0;
    display: grid;

    /* left / center / right — side panels flush to screen edges */
    grid-template-columns: clamp(220px, 16vw, 280px) minmax(0, 1fr) clamp(220px, 18vw, 300px);

    gap: 14px;

    /* IMPORTANT: no outer padding / no centering */
    padding: 0;
    margin: 0;
    max-width: none;
    width: 100%;

    overflow: hidden;
}

.card {
    border: 1px solid var(--border);
    background: linear-gradient(180deg, rgba(255, 255, 255, .05), rgba(255, 255, 255, .02));
    border-radius: 16px;
    box-shadow: var(--shadow);
    overflow: hidden;
    min-height: 0;
}

.card h2 {
    margin: 0;
    padding: 12px 12px 10px;
    font-size: 12px;
    letter-spacing: .12em;
    text-transform: uppercase;
    color: var(--muted);
    border-bottom: 1px solid var(--border);
    background: rgba(0, 0, 0, .12);
}

/* Board card: header + content + footer; content stretches */
.wrap>section.card {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.boardRow {
    flex: 1 1 auto;
    min-height: 0;
}

/* Stage should fill the available height inside the board card */
#mainStage {
    height: 100%;
}

.boardRow .stage {
    height: 100%;
    min-height: 0;
}

.stage {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 12px;
    min-height: 0;
}

.boardRow {
    display: flex;
    align-items: flex-start;
    gap: 14px;
    padding: 12px;
}

.boardAssist {
    display: flex;
    flex-direction: column;
    gap: 12px;
    flex: 0 0 auto;
    padding-top: 0;
}

.assistBlock {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.assistLabel {
    font-size: 11px;
    letter-spacing: .10em;
    text-transform: uppercase;
    color: var(--muted);
    opacity: .85;
    padding-left: 4px;
}

/* Smaller assist canvases (less attention-grabbing) */
.boardAssist canvas {
    width: 120px;
    height: 120px;
    image-rendering: pixelated;
    background: #0b0f15;
    border-radius: 12px;
    border: 1px solid rgba(255, 255, 255, .06);
}

/* Under-finger preview uses the same styling as other assist canvases */
#under {
    image-rendering: pixelated;
}

/* Remove extra padding from stage when it's inside boardRow */
.boardRow .stage {
    padding: 0;
    flex: 1 1 auto;
    min-width: 0;

    /* Align board to top so it matches the patch preview top edge */
    align-items: flex-start;
    justify-content: center;

    /* same top offset that the assist block previously had */
    padding-top: 6px;
}

.hud {
    display: flex;
    align-items: flex-start;
    justify-content: center;
    gap: 14px;
    padding: 12px;
    border-bottom: 1px solid var(--border);
    background: rgba(0, 0, 0, .10);
    flex-wrap: wrap;
}

.patchCol {
    display: flex;
    flex-direction: column;
    gap: 8px;
    flex: 0 0 auto;
    align-items: flex-start;
}

.hudStats {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    align-items: flex-start;
    padding-top: 2px;
}

.hudLine {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    padding: 8px 10px;
    border-radius: 12px;
    background: rgba(0, 0, 0, .18);
    border: 1px solid rgba(255, 255, 255, .06);
    min-width: 130px;
}

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

.hudLine .v {
    font-weight: 700;
}

.boardFooter {
    display: flex;
    justify-content: center;
    padding: 12px;
    border-top: 1px solid var(--border);
    background: rgba(0, 0, 0, .10);

}

#main {
    image-rendering: pixelated;
    background: #0b0f15;
    border-radius: 14px;
    border: 1px solid rgba(255, 255, 255, .08);
}

.side {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.pad {
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    min-height: 0;
    overflow: auto;
}

#patch {
    image-rendering: pixelated;
    background: #0b0f15;
    border-radius: 14px;
    border: 1px solid rgba(255, 255, 255, .08);
    flex: 0 0 auto;
}

.stat {
    display: grid;
    gap: 8px;
    flex: 1;
    padding-top: 2px;
}

.stat .line {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    padding: 8px 10px;
    border-radius: 12px;
    background: rgba(0, 0, 0, .18);
    border: 1px solid rgba(255, 255, 255, .06);
}

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

.stat .v {
    font-weight: 700;
}

.hint {
    color: var(--muted);
    font-size: 12px;
    padding: 8px 10px 0;
    line-height: 1.35;
}

.pill {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    border-radius: 999px;
    background: rgba(0, 0, 0, .18);
    border: 1px solid rgba(255, 255, 255, .06);
    color: var(--muted);
    font-size: 12px;
}

.pill b {
    color: var(--text);
    font-weight: 700;
}

.nav .navControls {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.nav {
    max-height: calc(100vh - 80px);
    overflow-y: auto;
}

.navGroupTitle {
    font-size: 11px;
    letter-spacing: .12em;
    text-transform: uppercase;
    color: var(--muted);
    opacity: .85;
    padding-left: 2px;
    margin-bottom: 2px;
}

.navSep {
    height: 1px;
    background: rgba(255, 255, 255, .08);
    margin: 2px 0;
}

.navHelp {
    margin-top: 14px;
    padding-top: 10px;
    border-top: 1px dashed var(--border);
    font-size: 12px;
    color: var(--muted);
}


.navHelpTitle {
    margin-bottom: 6px;
    font-weight: 600;
    color: var(--text);
    letter-spacing: .04em;
    text-transform: uppercase;
    font-size: 11px;
}


.navHelp ul {
    margin: 0;
    padding-left: 16px;
}


.navHelp li {
    margin-bottom: 6px;
    line-height: 1.35;
}

.seoNote {
    margin-top: 12px;
    font-size: 12px;
    color: var(--muted);
    line-height: 1.4;
}

.seedRow {
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding: 10px 10px;
    border-radius: 14px;
    background: rgba(255, 255, 255, .04);
    border: 1px solid rgba(255, 255, 255, .06);
}

.seedActions {
    display: grid;
}

.seedActions>button {
    width: 100%;
}

.seedLabel {
    font-size: 11px;
    letter-spacing: .08em;
    text-transform: uppercase;
    color: var(--muted);
    opacity: .8;
    padding-left: 2px;
    margin-top: 2px;
}

.seedHint {
    font-size: 12px;
    line-height: 1.25;
    color: var(--muted);
    opacity: .78;
}

.seedInput {
    font-size: 14px;
    padding: 10px 10px;
    border-radius: 12px;
    border: 1px solid rgba(255, 255, 255, .12);
    background: rgba(0, 0, 0, .18);
    color: var(--text);
}

.seedInput:focus {
    border-color: rgba(255, 255, 255, .20);
}

.btnRow {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

.btnRow>button {
    flex: 1 1 0;
    width: auto;
}

.nav button {
    width: 100%;
}

.footer {
    border-top: 1px solid var(--border);
    background: rgba(15, 19, 26, .55);
    backdrop-filter: blur(8px);
    padding: 10px 16px;
}

.footerInner {

    max-width: none;
    margin: 0 auto;
    padding: 0 14px;

    display: flex;
    gap: 10px;
    align-items: center;
    justify-content: center;
    flex-wrap: wrap;

    color: var(--muted);
    font-size: 12px;
    text-align: center;
}

.footerInner .sep {
    opacity: 0.5;
}

.footerLink {
    color: var(--muted);
    text-decoration: none;
    border-bottom: 1px dotted rgba(255, 255, 255, .22);
    padding-bottom: 1px;
}


.footerLink:hover {
    color: var(--text);
    border-bottom-color: rgba(255, 255, 255, .38);
}

/* When panels touch the screen edges, remove outer corner rounding */
.wrap>.nav.card {
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;
}

.wrap>.side.card {
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
}

@media (max-width: 980px) {

    /* Order: Board -> Stats -> Menu */
    .wrap>section.card {
        order: 0;
    }

    /* board */
    .side {
        order: 1;
    }

    /* stats */
    .nav {
        order: 2;
    }

    /* menu */

    .wrap {
        grid-template-columns: 1fr;
    }

    .card>h2 {
        display: none;
    }

    .card {
        padding-top: 0;
    }

    .boardAssist canvas {
        width: 72px;
        height: 72px;
        border-radius: 10px;
    }

    .assistLabel {
        font-size: 10px;
        padding-left: 0;
        text-align: left;
    }

    body {
        overflow: auto;
        /* mobile/tablet: allow scrolling */
    }

    /* Let the page scroll naturally on narrow screens */
    body {
        height: auto;
        min-height: 100vh;
        display: block;
        overflow: auto;
    }

    /* Don't clip the page content */
    .wrap {
        overflow: visible;
        padding: 10px;
        gap: 10px;
    }

    /* Disable inner panel scrolling on mobile; use page scroll instead */
    .pad {
        overflow: visible;
    }

    .nav .pad {
        gap: 10px;
    }

    .btnRow {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
    }

    .nav button {
        width: 100%;
        padding: 10px 10px;
        border-radius: 12px;
    }

    .nav .pill {
        width: 100%;
        justify-content: center;
        font-size: 12px;
        padding: 8px 10px;
    }

    .boardRow {
        gap: 8px;
        /* was 14 */
        padding: 8px;
        /* was 12 */
    }

    .boardAssist {
        gap: 8px;
        /* was 12 */
    }

    .boardRow .stage {
        padding-top: 0;
        /* remove the extra separation */
    }
}

@media (max-width: 720px) {
    header {
        gap: 10px;
        flex-wrap: wrap;
    }

    .card>h2 {
        display: none;
    }

    .card {
        padding-top: 0;
    }

    .wrap>section.card {
        order: 0;
    }

    .side {
        order: 1;
    }

    .nav {
        order: 2;
    }

    header .controls {
        width: 100%;
        flex-wrap: wrap;
        justify-content: flex-start;
    }

    .wrap {
        padding: 10px;
        gap: 10px;
    }

    /* Stack patch above board on mobile */
    .boardRow {
        flex-direction: column;
        align-items: center;
        gap: 10px;
        padding: 10px;
    }

    .boardAssist {
        padding-top: 0;
        width: 100%;
        align-items: center;
    }

    .assistBlock {
        align-items: center;
    }

    /* Make patch preview larger on mobile */
    .boardAssist canvas {
        width: 64px;
        height: 64px;
        border-radius: 12px;
    }

    .stage {
        width: 100%;
        padding: 0;
    }

    /* Ensure main canvas can shrink nicely */
    #main {
        max-width: 100%;
        height: auto;
        touch-action: none;
        /* important: allow custom touch input */
    }

    .boardFooter {
        padding: 10px;
    }

    .hudLine {
        min-width: 110px;
    }

    /* Put "Patch to find" and "Under finger" next to each other */
    .boardAssist {
        flex-direction: row;
        align-items: flex-start;
        justify-content: center;
        flex-wrap: nowrap;
        gap: 10px;
    }

    .assistBlock {
        align-items: flex-start;
    }
}

/* Low-height / landscape: ensure the full board is visible without page scroll */
@media (max-height: 780px) and (min-width: 721px) {

    /* Make the central board card fill available height */
    .wrap>section.card {
        min-height: 0;
    }

    /* Constrain stage by viewport: header + some breathing room */
    #mainStage {
        height: min(68vh, 560px);
    }

    /* Remove extra vertical padding that wastes height */
    .boardRow {
        padding: 4px;
    }

    .hud {
        padding: 10px;
    }

    .boardFooter {
        padding: 10px;
    }

    .stage {
        padding: 4px;
    }

    header {
        display: none;
    }
}

/* --- Game Over overlay --- */
.overlay {
    position: fixed;
    inset: 0;
    display: none;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, .55);
    backdrop-filter: blur(6px);
    z-index: 50;
}

.overlay.show {
    display: flex;
}

.overlayCard {
    width: min(520px, calc(100vw - 28px));
    border: 1px solid var(--border);
    background: linear-gradient(180deg, rgba(255, 255, 255, .06), rgba(255, 255, 255, .02));
    border-radius: 18px;
    box-shadow: var(--shadow);
    padding: 16px 16px 14px;
    text-align: center;
}

.overlayTitle {
    font-size: 18px;
    font-weight: 800;
    letter-spacing: .4px;
    margin-bottom: 8px;
}

.overlaySub {
    color: var(--muted);
    margin-bottom: 14px;
    line-height: 1.35;
}

.overlayActions {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin-bottom: 10px;
}

.overlayHint {
    color: var(--muted);
    font-size: 12px;
    opacity: .9;
}

/* --- Overlay badges row --- */
.overlayBadges {
    display: flex;
    justify-content: center;
    gap: 10px;
    flex-wrap: wrap;
    margin: 0 0 12px;
}

.badge {
    display: inline-flex;
    align-items: center;
    padding: 6px 10px;
    border-radius: 999px;
    background: rgba(0, 0, 0, .18);
    border: 1px solid rgba(255, 255, 255, .08);
    color: var(--text);
    font-size: 12px;
    font-weight: 650;
}

.badge em {
    font-style: normal;
    color: var(--muted);
    font-weight: 600;
    margin-right: 6px;
}

/* --- Share box (Game Over overlay) --- */
.btnGhost {
    background: rgba(0, 0, 0, .18);
}

.shareWrap {
    margin-top: 10px;
    text-align: left;
}

.shareText {
    width: 100%;
    resize: none;
    border-radius: 14px;
    border: 1px solid rgba(255, 255, 255, .10);
    background: rgba(0, 0, 0, .22);
    color: var(--text);
    padding: 10px 10px;
    font: 12px/1.35 ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    outline: none;
}

/* -------- Flow mode: hide performance-pressure stats -------- */
body.flowMode #lineStreak,
body.flowMode #lineBestStreak,
body.flowMode #lineAvgTime,
body.flowMode #lineLastTime {
    display: none;
}


/* Flow-only stats lines: hidden by default, shown in Flow mode */
#lineFlowDepth,
#lineFlowComfort,
#lineFlowBoards,
#lineFlowPerfectHits {
    display: none;
}

body.flowMode #lineFlowDepth,
body.flowMode #lineFlowComfort,
body.flowMode #lineFlowBoards,
body.flowMode #lineFlowPerfectHits {
    display: flex;
}

/* Show under-finger preview ONLY on touch devices */
@media (hover: none) and (pointer: coarse) {

    #under,
    .underLabel {
        display: block;
    }
}