:root {
    --blue-deep: #0b3d91;
    --blue-mid: #1a63c9;
    --blue-light: #4a9be8;
    --gold: #e0b43a;
    --gold-dark: #9c6f14;
    --cream: #fbf6e6;
    --card-back: #a9865a;
    --card-back-dark: #6e5333;
    --card-face: #f7f3ea;
    --text-red: #c0252a;
    --shadow: rgba(0, 0, 0, 0.35);
    --font: "Trebuchet MS", "Helvetica Neue", Arial, sans-serif;
    --header-panels-width: 220px;
}

* {
    box-sizing: border-box;
    /* Quick successive taps must flip cards, never double-tap zoom. */
    touch-action: manipulation;
}

/* The home and game-over screens are ordinary scrolling pages, so Safari's
   bars recede and content slides under them. While playing, the document is
   locked: mobile Safari only paints page pixels behind its bars when the
   document sits at a non-zero scroll position, so on touch devices the body
   carries a bleed above and below the app and the page is parked on it. */
:root {
    --top-bleed: 0px;
    --bottom-bleed: 0px;
}

@media (pointer: coarse) {
    html.playing {
        --top-bleed: 80px;
        --bottom-bleed: 160px;
    }
}

/* The pattern lives on html, fixed to the viewport and tiling: WebKit only
   extends the root background beyond the page (under Safari's bars, into the
   bounce areas) when it is a fixed, repeating image; otherwise just the colour.
   The gradient tile is three viewports tall and centred, so its repeats never
   come into view. */
html {
    margin: 0;
    min-height: 100%;
    background-color: var(--blue-mid);
    background-image:
        radial-gradient(circle at 20% 30%, rgba(255, 255, 255, 0.10) 0 6px, transparent 7px),
        radial-gradient(circle at 70% 60%, rgba(255, 255, 255, 0.08) 0 9px, transparent 10px),
        radial-gradient(circle at 45% 80%, rgba(255, 255, 255, 0.06) 0 4px, transparent 5px),
        radial-gradient(circle at 85% 20%, rgba(255, 255, 255, 0.07) 0 5px, transparent 6px),
        linear-gradient(160deg, var(--blue-light) 0 36%, var(--blue-mid) 50%, var(--blue-deep) 64% 100%);
    background-size: 160px 160px, 220px 220px, 130px 130px, 190px 190px, 100% 300%;
    background-position: 0 0, 0 0, 0 0, 0 0, center;
    background-repeat: repeat;
    background-attachment: fixed;
}

body {
    margin: 0;
    padding-top: var(--top-bleed);
    padding-bottom: var(--bottom-bleed);
    min-height: 100dvh;
    box-sizing: content-box;
    font-family: var(--font);
    color: var(--cream);
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

#app {
    min-height: 100dvh;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

html.playing #app {
    height: 100dvh;
    overflow: hidden;
    /* The app shell swallows pan gestures so the document stays put on its bleed. */
    touch-action: none;
}

.screen {
    min-height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 20px;
    padding: 16px;
}

.hidden {
    display: none !important;
}

.title {
    margin: 0;
    font-size: clamp(32px, 7vw, 64px);
    font-weight: 900;
    letter-spacing: 2px;
    text-transform: uppercase;
    color: var(--gold);
    text-shadow: 2px 3px 0 var(--gold-dark), 0 6px 14px var(--shadow);
}

/* Category screen: scrolls, themes above the leaderboard on phones and
   side by side on wide screens */

#category-screen {
    justify-content: flex-start;
    padding-top: max(16px, 4vh);
}

.home-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 24px;
    width: 100%;
}

.home-themes {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    width: 100%;
}

@media (min-aspect-ratio: 1 / 1) {
    .home-content {
        flex-direction: row;
        align-items: flex-start;
        justify-content: center;
        gap: 40px;
    }

    .home-themes {
        width: min(100%, 640px);
    }

    #home-leaderboard {
        width: 320px;
        flex-shrink: 0;
    }
}

.category-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: clamp(10px, 2.5vw, 28px);
    width: min(100%, 760px);
}

.category-tile {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    padding: 0;
    border: none;
    background: none;
    color: var(--cream);
    font: inherit;
    cursor: pointer;
}

.category-tile .frame {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    aspect-ratio: 1;
    border: 6px solid var(--gold);
    border-radius: 4px;
    background: linear-gradient(145deg, #1e4f9e, #0a2a66);
    box-shadow: inset 0 0 0 2px var(--gold-dark), 0 6px 14px var(--shadow);
    transition: transform 0.15s;
}

.category-tile:active .frame,
.category-tile:hover .frame {
    transform: scale(1.05);
}

.category-tile .sample {
    font-size: clamp(32px, 8vw, 72px);
    line-height: 1;
}

.category-name {
    font-size: 15px;
    letter-spacing: 1px;
    text-transform: uppercase;
}

/* Game screen */

#game-screen {
    position: relative;
    height: 100%;
    justify-content: flex-start;
    gap: 12px;
    padding: 16px;
}

.game-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    width: 100%;
    flex-shrink: 0;
}

.header-panels {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    width: var(--header-panels-width);
}

/* The board is centered on the whole screen, with the header laid over it.
   Side padding keeps the board clear of the panel column. */
.board-area {
    position: absolute;
    inset: 16px;
    padding: 0 calc(var(--header-panels-width) + 16px);
    display: flex;
    align-items: center;
    justify-content: center;
    container-type: size;
}

.game-header {
    position: relative;
    z-index: 1;
    pointer-events: none;
}

.board {
    --columns: 4;
    --rows: 3;
    --aspect: 1;
    display: grid;
    grid-template-columns: repeat(var(--columns), minmax(0, 1fr));
    gap: 2cqmin;
    width: min(100cqw, calc(100cqh * var(--aspect)));
    aspect-ratio: var(--aspect);
}

.card {
    position: relative;
    padding: 0;
    border: none;
    background: none;
    aspect-ratio: 3 / 4;
    perspective: 800px;
    cursor: pointer;
    font: inherit;
}

/* Like the original: cards open and close instantly, no flip animation. */
.card-inner {
    position: absolute;
    inset: 0;
    transform-style: preserve-3d;
}

.card.up .card-inner {
    transform: rotateY(180deg);
}

.card-face {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6%;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    box-shadow: 0 4px 10px var(--shadow);
}

.card-back {
    background: linear-gradient(160deg, #c5a378, var(--card-back) 50%, #8a6a44);
    border: 3px solid #d8c3a0;
    box-shadow: inset 0 0 0 2px var(--card-back-dark), 0 4px 10px var(--shadow);
}

.monogram {
    font-size: 11cqmin;
    font-weight: 900;
    font-family: Georgia, "Times New Roman", serif;
    color: #7a5a36;
    text-shadow: 1px 1px 0 #d8c3a0, -1px -1px 0 #5a3f22;
}

.monogram small {
    font-size: 0.6em;
    margin-left: -0.45em;
    vertical-align: -0.1em;
}

.card-front {
    background: linear-gradient(160deg, #ffffff, var(--card-face));
    border: 3px solid #dcd3c0;
    transform: rotateY(180deg);
}

.card.trap .card-front {
    background: radial-gradient(circle, #ff6a5a, #9e1b14 75%);
    border-color: #ffb3a7;
}

.picture {
    font-size: 15cqmin;
    line-height: 1;
}

.card.matched .card-front {
    border-color: var(--gold);
    box-shadow: 0 0 0 2px var(--gold-dark), 0 4px 10px var(--shadow);
}

.card.matched {
    cursor: default;
}

/* Header panels */

.panel {
    padding: 8px 12px;
    border-radius: 6px;
    background: linear-gradient(160deg, #ffffff, var(--cream));
    border: 4px solid var(--gold);
    box-shadow: inset 0 0 0 2px var(--gold-dark), 0 6px 14px var(--shadow);
    color: #222;
}


.panel-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 12px;
}

.label {
    color: var(--text-red);
    font-weight: 700;
    font-size: 15px;
}

.value {
    font-size: 22px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.reveal-mode {
    margin-top: 4px;
    padding-top: 4px;
    border-top: 2px solid rgba(156, 111, 20, 0.25);
    font-size: 13px;
    font-weight: 700;
    color: #555;
    text-align: center;
}

.clock-panel {
    padding: 8px;
}

.clock-track {
    height: 18px;
    border-radius: 4px;
    background: #f1ece0;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3);
    overflow: hidden;
}

/* The gradient spans the whole track and is clipped from the left as time
   drains, so the colours stay put and only the red end survives. */
.clock-fill {
    --drained: 0%;
    height: 100%;
    width: 100%;
    background: linear-gradient(90deg, #2ecc40, #ffb000 55%, #e0241c);
    clip-path: inset(0 0 0 var(--drained));
}

.logo {
    display: flex;
    align-items: flex-start;
    font-family: Georgia, "Times New Roman", serif;
    font-weight: 900;
    line-height: 0.72;
    color: #f4e6ee;
    text-shadow: 2px 2px 0 #7a4a66, 0 6px 12px var(--shadow);
}

.logo-letter {
    font-size: 64px;
}

.logo-digit {
    font-size: 48px;
    margin-left: -0.4em;
}

.quit-button {
    position: absolute;
    right: 16px;
    bottom: 16px;
    z-index: 3;
    width: 36px;
    height: 36px;
    border: 2px solid rgba(255, 255, 255, 0.4);
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.25);
    color: var(--cream);
    font: inherit;
    cursor: pointer;
}

/* Popups */

.overlay {
    position: absolute;
    inset: 0;
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    visibility: hidden;
    transition: visibility 0s linear 0.2s;
}

.overlay.open {
    visibility: visible;
    transition: visibility 0s;
}

.popup {
    width: min(80%, 420px);
    padding: 22px 26px;
    border-radius: 8px;
    border: 4px solid #8fd1ff;
    background: linear-gradient(160deg, rgba(36, 118, 196, 0.96), rgba(16, 72, 140, 0.96));
    box-shadow: 0 10px 30px var(--shadow);
    text-align: center;
    opacity: 0;
    transform: scale(0.8);
    transition:
        opacity 0.2s ease,
        transform 0.2s ease-in;
}

.overlay.open .popup {
    opacity: 1;
    transform: scale(1);
    transition:
        opacity 0.2s ease,
        transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.popup h2 {
    margin: 0 0 8px;
    font-size: clamp(28px, 5vw, 44px);
    line-height: 1.1;
    color: #ffe14d;
    text-shadow: 2px 2px 0 var(--gold-dark);
}

.popup p {
    margin: 0;
    font-size: 18px;
}

.popup .tap-hint {
    margin-top: 14px;
    font-size: 14px;
    letter-spacing: 2px;
    text-transform: uppercase;
    opacity: 0.75;
    animation: pulse 1.2s ease-in-out infinite alternate;
}

@keyframes pulse {
    to {
        opacity: 0.35;
    }
}

.bonus-line {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 10px 0;
}

.bonus-line + .bonus-line {
    border-top: 2px solid rgba(255, 255, 255, 0.25);
}

.bonus-line span {
    font-size: 20px;
    font-weight: 700;
    color: #ffe14d;
    text-shadow: 1px 1px 0 var(--gold-dark);
}

.bonus-line strong {
    font-size: 30px;
    font-variant-numeric: tabular-nums;
}

/* Game over */

#gameover-screen {
    justify-content: flex-start;
    padding-top: max(16px, 6vh);
}

.result-panel {
    width: min(100%, 320px);
    padding: 16px 20px;
    border-radius: 6px;
    background: linear-gradient(160deg, #ffffff, var(--cream));
    border: 4px solid var(--gold);
    box-shadow: inset 0 0 0 2px var(--gold-dark), 0 6px 14px var(--shadow);
    color: #222;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.score-form {
    display: flex;
    gap: 10px;
    width: min(100%, 360px);
}

.name-input {
    flex: 1;
    min-width: 0;
    padding: 10px 12px;
    border: 4px solid var(--gold);
    border-radius: 8px;
    background: #fff;
    box-shadow: inset 0 0 0 2px var(--gold-dark);
    color: #222;
    font: inherit;
    font-size: 18px;
    font-weight: 700;
}

.name-input:focus {
    outline: none;
    border-color: #fff;
}

.name-input.invalid {
    border-color: var(--text-red);
}

.primary-button:disabled {
    opacity: 0.5;
    cursor: default;
}

.leaderboard {
    width: min(100%, 420px);
    padding: 12px 16px;
    border-radius: 6px;
    background: linear-gradient(160deg, #ffffff, var(--cream));
    border: 4px solid var(--gold);
    box-shadow: inset 0 0 0 2px var(--gold-dark), 0 6px 14px var(--shadow);
    color: #222;
}

.leaderboard-title {
    margin: 0 0 8px;
    font-size: 16px;
    letter-spacing: 2px;
    text-transform: uppercase;
    color: var(--text-red);
    text-align: center;
}

.leaderboard-list {
    margin: 0;
    padding: 0 0 0 28px;
    font-variant-numeric: tabular-nums;
}

.leaderboard-list li {
    display: grid;
    grid-template-columns: 1fr auto;
    column-gap: 12px;
    padding: 4px 6px;
    border-radius: 4px;
}

.leaderboard-list li.own {
    background: rgba(224, 180, 58, 0.3);
    font-weight: 700;
}

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

.entry-detail {
    grid-column: 1;
    font-size: 12px;
    color: #666;
}

.entry-score {
    grid-row: 1 / span 2;
    grid-column: 2;
    align-self: center;
    font-size: 18px;
    font-weight: 700;
}

.rank-line {
    margin: 8px 0 0;
    text-align: center;
    font-weight: 700;
    color: var(--text-red);
}

.rank-line:empty {
    display: none;
}

.primary-button {
    padding: 12px 28px;
    border: 4px solid var(--gold);
    border-radius: 8px;
    background: linear-gradient(160deg, #ffffff, var(--cream));
    box-shadow: inset 0 0 0 2px var(--gold-dark), 0 6px 14px var(--shadow);
    color: var(--text-red);
    font: inherit;
    font-size: 20px;
    font-weight: 700;
    cursor: pointer;
}

/* Portrait phones: the same header, with the board pushed down into thumb
   reach and kept clear of the close button in the corner. */

@media (max-aspect-ratio: 1 / 1) {
    .board-area {
        position: relative;
        inset: auto;
        padding: 0 0 56px;
        flex: 1;
        width: 100%;
        min-height: 0;
        align-items: flex-end;
    }

    .category-grid {
        grid-template-columns: repeat(3, minmax(0, 1fr));
        width: min(100%, 480px);
    }
}
