:root {
    --green: #00ff41;
    --green-dim: #00b32c;
    --green-faint: rgb(0 255 65 / 14%);
    --amber: #ffcc00;
    --orange: #ff8c00;
    --red: #ff3b3b;
    --panel: rgb(0 8 2 / 94%);
    --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
}

* {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

html,
body {
    height: 100%;
}

body {
    margin: 0;
    overflow: hidden;
    background: #000;
    color: var(--green);
    font-family: var(--mono);
    -webkit-tap-highlight-color: transparent;
}

#matrix-canvas {
    position: fixed;
    inset: 0;
    display: block;
    touch-action: none; /* dragging a finger moves the effect instead of scrolling */
}

button {
    font: inherit;
    color: inherit;
    cursor: pointer;
}

/* ---------- launcher ---------- */

.cb-launch {
    position: fixed;
    left: 50%;
    bottom: max(28px, env(safe-area-inset-bottom));
    transform: translateX(-50%);
    padding: 12px 20px;
    background: rgb(0 0 0 / 70%);
    border: 1px solid var(--green-dim);
    border-radius: 4px;
    font-size: 0.95rem;
    letter-spacing: 0.12em;
    text-shadow: 0 0 8px var(--green);
    box-shadow: 0 0 18px rgb(0 255 65 / 25%), inset 0 0 12px rgb(0 255 65 / 10%);
    white-space: nowrap;
}

.cb-launch:hover,
.cb-launch:focus-visible {
    background: rgb(0 40 10 / 80%);
    outline: none;
}

.cb-launch .cursor {
    animation: blink 1s steps(1) infinite;
}

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

/* ---------- panel ---------- */

.cb-panel {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: max(12px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom));
    pointer-events: none; /* the rain stays interactive around the box */
}

.cb-box {
    position: relative;
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: 420px;
    max-height: 100%;
    padding: 22px 18px 18px;
    background: var(--panel);
    border: 1px solid var(--green-dim);
    border-radius: 6px;
    box-shadow: 0 0 40px rgb(0 255 65 / 20%), inset 0 0 30px rgb(0 255 65 / 6%);
    contain: layout paint; /* changes inside the box never repaint the page around it */
    pointer-events: auto;
    animation: boot .35s ease-out;
    /* scrolls if a screen is too short to show everything */
    overflow-y: auto;
    overscroll-behavior: contain;
    /* scanlines */
    background: repeating-linear-gradient(0deg, rgb(0 0 0 / 18%) 0 1px, transparent 1px 3px), var(--panel);
}

@keyframes boot {
    from {
        opacity: 0;
        transform: scale(0.96);
        filter: brightness(2);
    }
}

.cb-close {
    position: absolute;
    top: 6px;
    right: 6px;
    z-index: 1;
    width: 44px;
    height: 44px;
    background: none;
    border: 0;
    color: var(--green-dim);
    font-size: 1.1rem;
}

.cb-close:hover {
    color: var(--green);
}

.cb-title {
    min-height: 1.3em;
    margin: 4px 0 6px;
    font-size: 1.7rem;
    letter-spacing: 0.08em;
    text-align: center;
    text-shadow: 0 0 10px var(--green);
}

.cb-intro {
    margin: 0 0 14px;
    color: var(--green-dim);
    text-align: center;
    font-size: 0.9rem;
}

.cb-rules {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin: 0 auto 18px;
    padding: 0;
    list-style: none;
    font-size: 0.85rem;
    color: var(--green-dim);
}

.cb-rules li {
    display: flex;
    align-items: center;
    gap: 10px;
}

.cb-levels {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.cb-level {
    display: grid;
    gap: 2px;
    padding: 12px 14px;
    text-align: left;
    background: var(--green-faint);
    border: 1px solid var(--green-dim);
    border-radius: 4px;
    transition: background .15s, box-shadow .15s;
}

.cb-level:hover,
.cb-level:focus-visible {
    background: rgb(0 255 65 / 24%);
    box-shadow: 0 0 14px rgb(0 255 65 / 30%);
    outline: none;
}

.cb-level strong {
    font-size: 1.05rem;
    letter-spacing: 0.1em;
    text-transform: uppercase;
}

.cb-level span {
    font-size: 0.8rem;
    color: var(--green-dim);
}

.cb-level small {
    font-size: 0.75rem;
    color: var(--amber);
}

/* ---------- game ---------- */

.cb-head {
    display: flex;
    align-items: baseline;
    gap: 14px;
    margin: 0 40px 12px 0;
    letter-spacing: 0.12em;
    font-size: 0.85rem;
}

.cb-head #cb-title {
    flex: 1;
    font-weight: 700;
    text-shadow: 0 0 8px var(--green);
}

.cb-stat {
    color: var(--green-dim);
    font-variant-numeric: tabular-nums;
}

.cb-stat b {
    color: var(--green);
}

.cb-history {
    flex: 1 1 auto;
    min-height: 0;
    margin: 0 0 10px;
    padding: 0;
    list-style: none;
    overflow-y: auto;
}

.cb-row {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 5px 0;
    border-bottom: 1px dashed rgb(0 255 65 / 15%);
    animation: row-in .25s ease-out;
}

@keyframes row-in {
    from {
        opacity: 0;
        transform: translateX(-8px);
    }
}

.cb-n {
    width: 1.6em;
    color: var(--green-dim);
    font-size: 0.75rem;
    text-align: right;
}

.cb-digits {
    display: flex;
    gap: 5px;
}

.cb-row .cb-cell {
    width: 30px;
    height: 34px;
    font-size: 1.05rem;
}

.cb-row .cb-cell {
    animation: flip .35s ease-out backwards;
}

@keyframes flip {
    from {
        transform: rotateX(90deg);
    }
}

/* Wordle-style colours: solid green, solid orange, grey */
.cb-cell.filled.exact {
    color: #000;
    background: var(--green);
    border-color: var(--green);
    box-shadow: 0 0 8px rgb(0 255 65 / 50%);
    text-shadow: none;
}

.cb-cell.filled.partial {
    color: #000;
    background: var(--orange);
    border-color: var(--orange);
    box-shadow: 0 0 8px rgb(255 140 0 / 45%);
    text-shadow: none;
}

.cb-cell.filled.none {
    color: rgb(255 255 255 / 45%);
    background: rgb(255 255 255 / 6%);
    border-color: rgb(255 255 255 / 18%);
    text-shadow: none;
}

.cb-rules .cb-cell {
    width: 26px;
    height: 28px;
    font-size: 0.95rem;
    animation: none;
}

.cb-current {
    display: flex;
    justify-content: center;
    gap: 8px;
    margin: 4px 0 14px;
}

.cb-cell {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 46px;
    height: 54px;
    font-size: 1.6rem;
    font-weight: 700;
    border: 1px solid rgb(0 255 65 / 35%);
    border-radius: 4px;
    background: rgb(0 0 0 / 45%);
    text-shadow: 0 0 8px var(--green);
}

.cb-cell.filled {
    border-color: var(--green);
    background: var(--green-faint);
}

.cb-cell.cursor {
    border-color: var(--green);
    box-shadow: inset 0 -3px 0 var(--green);
    animation: cursor 1s steps(1) infinite;
}

@keyframes cursor {
    50% {
        box-shadow: none;
    }
}

.cb-keypad {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
}

.cb-keypad button {
    min-height: 52px;
    font-size: 1.35rem;
    font-weight: 700;
    background: var(--green-faint);
    border: 1px solid rgb(0 255 65 / 40%);
    border-radius: 4px;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
}

.cb-keypad button:hover {
    background: rgb(0 255 65 / 22%);
}

.cb-keypad button:active,
.cb-keypad button.pressed {
    background: rgb(0 255 65 / 40%);
    transform: scale(0.95);
}

.cb-keypad .cb-enter {
    font-size: 1rem;
    letter-spacing: 0.12em;
    background: rgb(0 255 65 / 28%);
    border-color: var(--green);
}

.cb-keypad .cb-enter.wide {
    grid-column: span 2;
}

.cb-keypad button.exact {
    color: #000;
    background: var(--green);
    border-color: var(--green);
}

.cb-keypad button.partial {
    color: #000;
    background: var(--orange);
    border-color: var(--orange);
}

.cb-keypad button.none {
    color: rgb(255 255 255 / 35%);
    background: rgb(255 255 255 / 5%);
    border-color: rgb(255 255 255 / 12%);
}

.cb-keypad button:disabled {
    opacity: 0.25;
    cursor: default;
}

/* ---------- result ---------- */

.cb-result {
    margin: 6px 0 2px;
    text-align: center;
    animation: boot .4s ease-out;
}

.cb-result h2 {
    margin: 0 0 6px;
    font-size: 1.6rem;
    letter-spacing: 0.08em;
}

.cb-result.won h2 {
    color: #c8ffd4;
    text-shadow: 0 0 14px var(--green), 0 0 30px var(--green);
}

.cb-result.lost h2 {
    color: var(--red);
    text-shadow: 0 0 14px var(--red);
}

.cb-result p {
    margin: 0 0 12px;
    color: var(--green-dim);
}

.cb-result p b {
    color: var(--amber);
}

.cb-result .cb-digits {
    justify-content: center;
    margin-bottom: 16px;
}

.cb-result.lost .cb-cell {
    color: var(--red);
    border-color: var(--red);
    background: rgb(255 59 59 / 12%);
    text-shadow: 0 0 8px var(--red);
}

.cb-actions {
    display: flex;
    gap: 10px;
}

.cb-btn {
    flex: 1;
    min-height: 48px;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    background: var(--green-faint);
    border: 1px solid var(--green-dim);
    border-radius: 4px;
}

.cb-btn.primary {
    background: rgb(0 255 65 / 28%);
    border-color: var(--green);
}

/* glitch effect for headings */
.glitch {
    position: relative;
}

.glitch::before,
.glitch::after {
    content: attr(data-text);
    position: absolute;
    inset: 0;
    opacity: 0.7;
    pointer-events: none;
}

.glitch::before {
    color: #0ff;
    animation: glitch 2.4s infinite steps(1);
    clip-path: inset(0 0 60% 0);
}

.glitch::after {
    color: #f0f;
    animation: glitch 2.4s infinite reverse steps(1);
    clip-path: inset(55% 0 0 0);
}

@keyframes glitch {
    0%, 86%, 100% { transform: none; opacity: 0; }
    88% { transform: translate(-2px, 1px); opacity: 0.7; }
    92% { transform: translate(2px, -1px); opacity: 0.7; }
    96% { transform: translate(-1px, 0); opacity: 0.7; }
}

/* Phones held sideways: history on the left, input and keypad on the right */
@media (orientation: landscape) and (max-height: 520px) {
    .cb-box {
        max-width: 780px;
        padding: 14px 16px;
    }

    .cb-box.game {
        display: grid;
        grid-template-columns: 1fr 1fr;
        grid-template-rows: auto minmax(0, 1fr) auto;
        column-gap: 20px;
        height: 100%;
        overflow: hidden;
    }

    .cb-head {
        grid-column: 1 / -1;
        margin-bottom: 8px;
    }

    .cb-history {
        grid-column: 1;
        grid-row: 2 / 4;
        margin: 0;
    }

    .cb-current {
        grid-column: 2;
        grid-row: 2;
        align-self: end;
        margin: 0 0 10px;
    }

    .cb-keypad {
        grid-column: 2;
        grid-row: 3;
        gap: 6px;
    }

    .cb-keypad button {
        min-height: 44px;
    }

    .cb-box.game .cb-result {
        grid-column: 2;
        grid-row: 2 / 4;
        align-self: center;
    }

    .cb-cell {
        width: 42px;
        height: 48px;
        font-size: 1.4rem;
    }

    .cb-title {
        font-size: 1.3rem;
        margin: 0 0 4px;
    }

    .cb-intro {
        margin-bottom: 8px;
    }

    .cb-rules {
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: center;
        gap: 4px 16px;
        margin-bottom: 10px;
    }

    .cb-levels {
        flex-direction: row;
    }

    .cb-level {
        flex: 1;
    }
}

@media (max-height: 640px) {
    .cb-cell { width: 40px; height: 46px; font-size: 1.35rem; }
    .cb-keypad button { min-height: 46px; }
    .cb-title { font-size: 1.4rem; }
}

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