/* ==========================================================================
   Glitchdle
   Look: old computer graphics. True black, the 16-color EGA palette,
   hard pixel edges, no rounded corners, no blur.
   ========================================================================== */

:root {
    /* EGA palette */
    --black: #000000;
    --white: #ffffff;
    --light: #aaaaaa;
    --mid: #555555;
    --green: #55ff55;
    --yellow: #ffff55;
    --magenta: #ff55ff;   /* glitch: a tile that was wrong */
    --cyan: #55ffff;      /* detector: a tile that was checked */

    /* two near-blacks for quiet surfaces */
    --dark: #2a2a2a;
    --well: #1a1a1a;

    /* board sizing. --tile must be a multiple of 4 so the dither lines up */
    --tile: 56px;
    --tile-px: 5px;       /* size of one pixel of a tile's letter */
    --gap: 6px;

    --mono: ui-monospace, "SF Mono", "Cascadia Mono", "Roboto Mono", Menlo, Consolas, monospace;
}

*, *::before, *::after {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

html, body {
    height: 100%;
}

body {
    margin: 0;
    color: var(--white);
    font: 13px/1.45 var(--mono);
    /* faint dot grid */
    background: var(--black) radial-gradient(circle at 1px 1px, #202020 1px, transparent 1.5px) 0 0 / 14px 14px;
}

button {
    font: inherit;
    color: inherit;
    border-radius: 0;
}

:focus-visible {
    outline: 2px solid var(--cyan);
    outline-offset: 2px;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
}


/* Pixel lettering ----------------------------------------------------------
   .ptext is a string drawn from the 5x7 font; --u is the size of one pixel.
   .glyph is a single letter whose pixels can be switched individually. */

.ptext {
    display: block;
    flex: none;
    width: calc(var(--cols) * var(--u, 2px));
    height: calc(7 * var(--u, 2px));
    fill: currentColor;
    shape-rendering: crispEdges;
    overflow: visible;
}

.glyph {
    display: block;
    width: calc(5 * var(--tile-px));
    height: calc(7 * var(--tile-px));
    shape-rendering: crispEdges;
}

.glyph rect {
    fill: currentColor;
    visibility: hidden;
}

.glyph rect.on {
    visibility: visible;
}


/* Layout ------------------------------------------------------------------- */

.app {
    height: 100vh;
    height: 100dvh;
    max-width: 520px;
    margin: 0 auto;
}

/* the game screen: header, HUD, status line, board (scrolls), keyboard */
.game {
    display: flex;
    flex-direction: column;
    gap: 12px;
    height: 100%;
    padding: 16px 12px 12px;
}

.masthead {
    display: flex;
    align-items: center;
    gap: 12px;
}

.masthead-actions {
    display: flex;
    gap: 8px;
    margin-left: auto;
}


/* Wordmark: white letters with a cyan and a magenta copy peeking out */

.wordmark {
    --u: 5px;
    --split: 2px;
    position: relative;
    flex: none;
    margin: 0;
    width: calc(53 * var(--u));
    height: calc(7 * var(--u));
}

.wordmark-layer {
    position: absolute;
    top: 0;
    left: 0;
}

.wordmark-cyan {
    color: var(--cyan);
    transform: translateX(var(--split));
}

.wordmark-magenta {
    color: var(--magenta);
    transform: translateX(calc(-1 * var(--split)));
}

.wordmark-white {
    color: var(--white);
}

.wordmark.glitching {
    --split: 5px;
}

/* the big one on the start screen */
.wordmark-big {
    --u: 7px;
    --split: 3px;
}

.wordmark-big.glitching {
    --split: 7px;
}


/* Buttons ------------------------------------------------------------------ */

.pbtn {
    --u: 2px;
    display: inline-grid;
    place-items: center;
    flex: none;
    min-width: 36px;
    height: 36px;
    padding: 0 10px;
    background: var(--black);
    color: var(--white);
    border: 2px solid var(--mid);
    cursor: pointer;
}

.pbtn:hover {
    border-color: var(--white);
}

.pbtn:active {
    background: var(--white);
    color: var(--black);
}

.pbtn-solid {
    background: var(--white);
    border-color: var(--white);
    color: var(--black);
}

.pbtn-solid:hover,
.pbtn-solid:active {
    background: var(--cyan);
    border-color: var(--cyan);
    color: var(--black);
}

.pbtn-big {
    --u: 3px;
    height: 52px;
    padding: 0 18px;
}

.pbtn:disabled {
    background: var(--black);
    border-color: var(--dark);
    color: var(--mid);
    cursor: default;
}


/* Start screen --------------------------------------------------------------- */

.start {
    display: flex;
    flex-direction: column;
    gap: 20px;
    height: 100%;
    padding: 24px 12px;
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: var(--mid) transparent;
}

/* center the block vertically when there is room, scroll when there isn't */
.start::before,
.start::after {
    content: "";
    flex: 1 0 0px;
}

.start > * {
    flex: none;
}

.start .wordmark {
    align-self: center;
}

.tagline {
    max-width: 44ch;
    margin: 0 auto;
    color: var(--light);
    text-align: center;
}

.start-demo {
    display: flex;
    justify-content: center;
}

.start-demo .row-num {
    visibility: hidden;
}

/* DAILY | PRACTICE */
.kind-switch {
    display: flex;
}

.kind-tab {
    --u: 2px;
    display: grid;
    place-items: center;
    flex: 1 1 0;
    height: 36px;
    padding: 0;
    background: var(--black);
    color: var(--light);
    border: 2px solid var(--mid);
    cursor: pointer;
}

.kind-tab + .kind-tab {
    margin-left: -2px;
}

.kind-tab:hover {
    color: var(--white);
}

.kind-tab[aria-checked="true"] {
    position: relative;
    background: var(--white);
    border-color: var(--white);
    color: var(--black);
}

.mode-picker {
    display: grid;
    gap: 8px;
}

.mode-card {
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: center;
    gap: 8px 12px;
    padding: 14px 14px 14px 22px;
    background: var(--black);
    color: var(--light);
    border: 2px solid var(--mid);
    text-align: left;
    cursor: pointer;
}

.mode-card:hover {
    border-color: var(--light);
}

.mode-card[aria-checked="true"] {
    border-color: var(--white);
    box-shadow: inset 8px 0 0 var(--white);   /* solid bar down the left edge */
    color: var(--white);
}

.mode-name {
    --u: 3px;
}

/* difficulty: one to three magenta blocks */
.mode-level {
    display: flex;
    gap: 3px;
}

.level-pip {
    width: 8px;
    height: 14px;
    box-shadow: inset 0 0 0 2px var(--mid);
}

.level-pip.on {
    background: var(--magenta);
    box-shadow: none;
}

.mode-rule,
.mode-record {
    grid-column: 1 / -1;
}

.mode-record {
    color: var(--light);
}

.mode-record:empty {
    display: none;
}

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

.start-actions .pbtn-solid {
    flex: 1 1 auto;
}

/* the secondary button is lettered smaller so START always has room */
.start-actions .pbtn:not(.pbtn-solid) {
    --u: 2px;
}

.start-error {
    margin: 0;
    color: var(--yellow);
}


/* HUD: mode, guess counter, scans left ---------------------------------------- */

.hud {
    --u: 2px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    min-height: 28px;
    color: var(--light);
}

.hud-count,
.hud-meter,
.hud-left {
    display: flex;
    align-items: center;
    gap: 8px;
}

.hud-left {
    gap: 14px;
}

.hud-mode {
    color: var(--white);
}

.pips {
    display: flex;
    gap: 3px;
}

.pip {
    width: 8px;
    height: 14px;
    background: var(--cyan);
}

.pip.spent {
    background: none;
    box-shadow: inset 0 0 0 2px var(--mid);
}


/* Status line -------------------------------------------------------------- */

.status {
    display: flex;
    align-items: center;
    gap: 12px;
    min-height: 38px;
    color: var(--light);
}

.status-text {
    flex: 1 1 auto;
    padding-left: 2ch;
    text-indent: -2ch;
}

.status-text::before {
    content: "> ";
    color: var(--mid);
}

.status[data-tone="info"],
.status[data-tone="error"] { color: var(--white); }
.status[data-tone="glitch"] { color: var(--magenta); }
.status[data-tone="clear"]  { color: var(--cyan); }
.status[data-tone="win"]    { color: var(--green); }
.status[data-tone="lose"]   { color: var(--yellow); }


/* Board -------------------------------------------------------------------- */

#board-wrap {
    display: flex;
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: var(--mid) transparent;
}

#game-board {
    display: grid;
    gap: var(--gap);
    margin: auto;
    padding: 6px 0;
}

.row {
    display: grid;
    grid-template-columns: 22px auto 22px;
    align-items: center;
    gap: 8px;
    scroll-margin: 8px 0;
}

.row-num {
    --u: 2px;
    justify-self: end;
    color: var(--dark);
}

.row[data-state="completed"] .row-num { color: var(--mid); }
.row[data-state="input"] .row-num     { color: var(--white); }

.row-tiles {
    display: grid;
    grid-template-columns: repeat(5, var(--tile));
    gap: var(--gap);
}

.row-flag {
    --u: 2px;
    display: grid;
    justify-items: start;
}

.row[data-flag="lie"] .row-flag   { color: var(--magenta); }
.row[data-flag="clear"] .row-flag { color: var(--cyan); }


/* Tiles -------------------------------------------------------------------- */

.tile {
    position: relative;
    display: grid;
    place-items: center;
    width: var(--tile);
    height: var(--tile);
    background: var(--black);
    color: var(--white);
    border: 2px solid var(--dark);
    user-select: none;
    -webkit-user-select: none;
    --frame: 6px;         /* width of the dithered edge on yellow tiles */
}

.row[data-state="input"] .tile        { border-color: var(--mid); }
.row[data-state="input"] .tile.filled { border-color: var(--white); }

/* blinking block where the next letter will go */
.tile.cursor::before {
    content: "";
    position: absolute;
    left: 50%;
    bottom: calc(var(--tile-px) * 2);
    width: calc(var(--tile-px) * 5);
    height: var(--tile-px);
    margin-left: calc(var(--tile-px) * -2.5);
    background: var(--white);
    animation: blink 1.06s steps(1) infinite;
}

/* The three answers. Shared by tiles, keyboard keys and the legend.
   Color is never the only signal: green is solid, yellow has a dithered
   edge, gray is a dark block. */

.tile[data-status="green"],
.key[data-status="green"],
.swatch[data-status="green"] {
    background: var(--green);
    border-color: var(--green);
    color: var(--black);
}

.tile[data-status="yellow"],
.key[data-status="yellow"],
.swatch[data-status="yellow"] {
    background:
        linear-gradient(var(--yellow), var(--yellow)) center / calc(100% - 2 * var(--frame)) calc(100% - 2 * var(--frame)) no-repeat,
        repeating-conic-gradient(var(--yellow) 0 25%, var(--black) 0 50%) 0 0 / 4px 4px;
    background-origin: border-box;
    border-color: transparent;
    color: var(--black);
}

.tile[data-status="gray"],
.swatch[data-status="gray"] {
    background: var(--well);
    border-color: var(--well);
    color: var(--light);
}

/* corner marks */
[data-mark]::after {
    content: "";
    position: absolute;
    top: -5px;
    right: -5px;
    width: 12px;
    height: 12px;
    border: 2px solid var(--black);
}

[data-mark="lie"]::after { background: var(--magenta); }
[data-mark="ok"]::after  { background: var(--cyan); }

/* tiles that can be scanned with a detector */
#game-board.can-scan .row[data-state="completed"] .tile[role="button"] {
    cursor: crosshair;
}

#game-board.can-scan .row[data-state="completed"] .tile[role="button"]:hover {
    outline: 2px solid var(--cyan);
    outline-offset: 2px;
}


/* Tile animation ------------------------------------------------------------
   Everything moves in hard steps, never smooth easing. */

.tile.pop {
    animation: pop 100ms steps(1);
}

.tile.decoding {
    border-color: var(--white);
    color: var(--white);
}

.tile.lock {
    animation: lock 160ms steps(1);
}

.tile.repair {
    z-index: 2;
    animation: repair 135ms steps(1) infinite;
}

.tile.scan {
    outline: 2px solid var(--cyan);
    outline-offset: 2px;
}

.tile.scan::before {
    content: "";
    position: absolute;
    z-index: 3;
    top: -2px;
    left: -2px;
    right: -2px;
    height: 4px;
    background: var(--cyan);
    animation: scan 380ms steps(10) forwards;
}

.tile.win {
    z-index: 2;
    animation: win 480ms steps(1);
}

.row.reject .row-tiles {
    animation: reject 300ms steps(1);
}

@keyframes blink {
    50% { opacity: 0; }
}

@keyframes pop {
    0%   { transform: scale(1.1); }
    60%  { transform: scale(1.04); }
    100% { transform: none; }
}

@keyframes lock {
    0%   { transform: translateX(-4px); clip-path: inset(0 0 58% 0); }
    34%  { transform: translateX(3px);  clip-path: inset(42% 0 0 0); }
    67%  { transform: translateX(-1px); clip-path: none; }
    100% { transform: none; }
}

@keyframes repair {
    0%   { transform: translate(2px, 0);    box-shadow: -5px 0 0 var(--magenta), 5px 0 0 var(--cyan); }
    34%  { transform: translate(-2px, 1px); box-shadow: 5px 0 0 var(--magenta), -5px 0 0 var(--cyan); }
    67%  { transform: translate(1px, -1px); box-shadow: -3px 0 0 var(--cyan), 3px 0 0 var(--magenta); }
}

@keyframes scan {
    to { top: calc(100% - 2px); }
}

@keyframes win {
    0%   { transform: translateY(-10px); box-shadow: -4px 0 0 var(--magenta), 4px 0 0 var(--cyan); }
    30%  { transform: translateY(-5px);  box-shadow: 3px 0 0 var(--magenta), -3px 0 0 var(--cyan); }
    60%  { transform: translateY(2px);   box-shadow: none; }
    80%  { transform: none; }
}

@keyframes reject {
    0%   { transform: translateX(-6px); filter: drop-shadow(4px 0 0 var(--magenta)) drop-shadow(-4px 0 0 var(--cyan)); }
    25%  { transform: translateX(5px);  filter: drop-shadow(-3px 0 0 var(--magenta)) drop-shadow(3px 0 0 var(--cyan)); }
    50%  { transform: translateX(-3px); filter: none; }
    75%  { transform: translateX(2px); }
    100% { transform: none; }
}


/* Keyboard ----------------------------------------------------------------- */

#keyboard {
    display: grid;
    gap: 6px;
    user-select: none;
    -webkit-user-select: none;
}

.kb-row {
    display: flex;
    justify-content: center;
    gap: 5px;
}

.key {
    --u: 3px;
    --frame: 5px;
    position: relative;
    display: grid;
    place-items: center;
    flex: 1 1 0;
    max-width: 44px;
    height: 52px;
    padding: 0;
    background: var(--dark);
    color: var(--white);
    border: 0 solid transparent;
    cursor: pointer;
    touch-action: manipulation;
}

.key.wide {
    flex: 1.5 1 0;
    max-width: 68px;
}

.key[data-status="gray"] {
    background: var(--black);
    box-shadow: inset 0 0 0 2px var(--dark);
    color: var(--mid);
}

.key:active,
.key.pressed {
    background: var(--white);
    box-shadow: none;
    color: var(--black);
}


/* How to play -------------------------------------------------------------- */

dialog {
    width: min(460px, calc(100vw - 24px));
    max-height: calc(100vh - 24px);
    padding: 20px;
    background: var(--black);
    color: var(--light);
    border: 2px solid var(--white);
    font: inherit;
}

dialog::backdrop {
    background: rgba(0, 0, 0, 0.85);
}

dialog h2 {
    --u: 2px;
    margin: 24px 0 10px;
    color: var(--white);
}

dialog p {
    margin: 0 0 10px;
}

.dialog-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 14px;
}

.dialog-head h2 {
    --u: 3px;
    margin: 0;
}

.legend {
    display: grid;
    gap: 10px;
    margin: 0;
    padding: 0;
    list-style: none;
}

.legend li {
    display: grid;
    grid-template-columns: 24px 1fr;
    align-items: center;
    gap: 12px;
}

.swatch {
    --frame: 4px;
    --u: 2px;
    position: relative;
    display: grid;
    place-items: center;
    width: 24px;
    height: 24px;
    border: 2px solid var(--dark);
}

.swatch-flag {
    border: 0;
    color: var(--magenta);
}

.mode-list {
    margin: 0 0 10px;
}

.mode-list dt {
    color: var(--white);
}

.mode-list dd {
    margin: 0 0 10px;
}


/* Small screens ------------------------------------------------------------ */

@media (max-width: 440px), (max-height: 780px) {
    :root {
        --tile: 48px;
        --tile-px: 4px;
        --gap: 5px;
    }

    .key {
        height: 46px;
    }
}

@media (max-width: 440px) {
    .game {
        gap: 10px;
        padding: 12px 10px 10px;
    }

    .masthead {
        gap: 10px;
    }

    .wordmark {
        --u: 3px;
    }

    .wordmark-big {
        --u: 6px;
    }

    .start {
        gap: 16px;
        padding: 20px 10px;
    }

    /* the counter keeps its numbers and drops the word GUESS */
    .hud-count .hud-label {
        display: none;
    }

    .kb-row {
        gap: 4px;
    }
}

@media (max-width: 360px) {
    :root {
        --tile: 44px;
    }

    .wordmark-big {
        --u: 5px;
    }
}

/* short windows (most laptops): tighten up so all 8 rows fit without scrolling */
@media (max-height: 780px) {
    :root {
        --tile: 44px;
    }

    .game {
        gap: 8px;
    }
}

@media (max-height: 640px) {
    .key {
        height: 40px;
    }
}


/* Reduced motion: the JS skips its effects too (see src/ui/effects.js) -------- */

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation: none !important;
    }
}
