:root {
    --bg-graphite: #121316;
    --bg-panel: #1a1c22;
    --bg-card-front: #23262f;
    --bg-card-back: #1f222b;
    --gold: #F5B301;
    --gold-bright: #ffd147;
    --gold-dim: rgba(245, 179, 1, 0.16);
    --ivory: #F7F7F7;
    --muted-text: #a5acb8;
    --border-subtle: #2e323d;
    --gap: clamp(4px, 2vmin, 10px);
    --pad: clamp(5px, 2vmin, 10px);
    --board-size: min(calc(100vw - 24px), calc(100vh - 100px), 430px);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
    -webkit-user-select: none;
}

html, body {
    width: 100%;
    height: 100%;
    background-color: var(--bg-graphite);
    color: var(--ivory);
    font-family: system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    overflow: hidden;
    touch-action: manipulation;
}

body {
    display: flex;
    justify-content: center;
    align-items: center;
}

.game-shell {
    width: 100%;
    max-width: 520px;
    height: 100%;
    max-height: 100vh;
    padding: 10px 12px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    gap: 6px;
}

/* HUD Header */
.hud {
    width: min(100%, 430px);
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

.brand {
    display: flex;
    flex-direction: column;
}

.brand-tag {
    font-size: 9px;
    letter-spacing: 0.14em;
    color: var(--gold);
    font-weight: 700;
}

.game-title {
    font-size: clamp(16px, 4.2vw, 22px);
    font-weight: 800;
    letter-spacing: 0.02em;
    color: var(--ivory);
    line-height: 1.1;
}

.game-title span {
    color: var(--gold);
}

.hud-right {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.stats-bar {
    display: flex;
    gap: 5px;
}

.stat-box {
    background: var(--bg-panel);
    border: 1px solid var(--border-subtle);
    border-radius: 6px;
    padding: 3px 8px;
    min-width: 56px;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.stat-label {
    font-size: 9px;
    font-weight: 700;
    letter-spacing: 0.07em;
    color: var(--muted-text);
}

.stat-val {
    font-size: 13px;
    font-weight: 800;
    color: var(--gold);
    font-variant-numeric: tabular-nums;
}

.hud-actions {
    display: flex;
    gap: 5px;
}

/* Buttons & Keyboard Focus */
button {
    font-family: inherit;
    cursor: pointer;
    border: none;
    background: none;
    color: var(--ivory);
}

button:focus,
button:focus-visible {
    outline: 3px solid var(--gold);
    outline-offset: 2px;
}

.ctrl-btn {
    background: var(--bg-panel);
    color: var(--ivory);
    border: 1px solid var(--border-subtle);
    border-radius: 6px;
    padding: 6px 9px;
    font-size: 12px;
    font-weight: 600;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    transition: background-color 0.15s, border-color 0.15s;
}

.ctrl-btn:hover {
    border-color: var(--gold);
    background: #23262f;
}

.ctrl-btn[aria-pressed="true"] {
    border-color: var(--gold);
    color: var(--gold);
}

.ctrl-btn-accent {
    background: var(--gold-dim);
    border-color: var(--gold);
    color: var(--gold);
}

.ctrl-btn-accent:hover {
    background: var(--gold);
    color: var(--bg-graphite);
}

/* Board & 4x4 Card Grid */
.board-stage {
    display: flex;
    justify-content: center;
    align-items: center;
    flex: 1 1 0;
    width: 100%;
    min-height: 0;
    min-width: 0;
}

.board-container {
    position: relative;
    width: var(--board-size);
    height: var(--board-size);
    background: var(--bg-panel);
    border: 2px solid var(--border-subtle);
    border-radius: 12px;
    padding: var(--pad);
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.55), inset 0 0 0 1px rgba(245, 179, 1, 0.08);
    overflow: hidden;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.card-grid {
    width: 100%;
    height: 100%;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: var(--gap);
}

.card-grid:focus {
    outline: none;
}

.card-btn {
    position: relative;
    width: 100%;
    height: 100%;
    perspective: 700px;
    border-radius: 8px;
    padding: 0;
    background: transparent;
}

.card-btn:focus,
.card-btn:focus-visible {
    outline: 3px solid var(--gold);
    outline-offset: 2px;
    z-index: 5;
}

.card-inner {
    position: relative;
    width: 100%;
    height: 100%;
    border-radius: 8px;
    transform-style: preserve-3d;
    transition: transform 240ms cubic-bezier(0.22, 1, 0.36, 1);
}

.card-btn.is-flipped .card-inner,
.card-btn.is-matched .card-inner {
    transform: rotateY(180deg);
}

.card-face {
    position: absolute;
    inset: 0;
    border-radius: 8px;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.card-front {
    background: var(--bg-card-front);
    border: 1.5px solid var(--border-subtle);
    transition: border-color 0.15s, background-color 0.15s;
}

.card-btn:hover:not(.is-flipped):not(.is-matched) .card-front {
    border-color: rgba(245, 179, 1, 0.65);
    background: #282c37;
}

/* Subtle repair-shop emblem on face-down cards */
.card-front::before {
    content: "";
    width: clamp(12px, calc(var(--board-size) * 0.052), 22px);
    height: clamp(12px, calc(var(--board-size) * 0.052), 22px);
    border: 2px solid rgba(245, 179, 1, 0.35);
    border-radius: 50%;
    box-shadow: inset 0 0 0 4px var(--bg-card-front), inset 0 0 0 6px rgba(247, 247, 247, 0.18);
}

.card-back {
    background: var(--bg-card-back);
    border: 1.5px solid var(--gold);
    transform: rotateY(180deg);
    padding: clamp(2px, calc(var(--board-size) * 0.01), 4px);
    gap: clamp(1px, calc(var(--board-size) * 0.007), 3px);
}

.card-btn.is-matched .card-back {
    background: rgba(245, 179, 1, 0.14);
    border-color: var(--gold-bright);
    box-shadow: 0 0 12px rgba(245, 179, 1, 0.3);
}

.part-svg {
    width: clamp(14px, calc(var(--board-size) * 0.098), 42px);
    height: clamp(14px, calc(var(--board-size) * 0.098), 42px);
    display: block;
    overflow: visible;
}

.part-name {
    font-size: clamp(7px, calc(var(--board-size) * 0.025), 10.5px);
    font-weight: 700;
    color: var(--ivory);
    text-align: center;
    line-height: 1.1;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.card-btn.is-matched .part-name {
    color: var(--gold-bright);
}

/* Overlay Panel */
.overlay {
    position: absolute;
    inset: 0;
    background: rgba(18, 19, 22, 0.92);
    backdrop-filter: blur(4px);
    display: flex;
    justify-content: center;
    align-items: center;
    padding: clamp(4px, calc(var(--board-size) * 0.033), 14px);
    overflow-y: auto;
    overscroll-behavior: contain;
    z-index: 20;
}

.overlay.hidden,
.hidden {
    display: none !important;
}

.overlay-panel {
    width: 100%;
    max-width: 345px;
    margin: auto;
    background: var(--bg-panel);
    border: 1.5px solid var(--gold);
    border-radius: 10px;
    padding: clamp(6px, calc(var(--board-size) * 0.033), 14px) clamp(6px, calc(var(--board-size) * 0.038), 16px);
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: clamp(3px, calc(var(--board-size) * 0.019), 8px);
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.7);
}

.overlay-kicker {
    font-size: clamp(8px, calc(var(--board-size) * 0.024), 10px);
    font-weight: 700;
    letter-spacing: 0.14em;
    color: var(--gold);
}

.overlay-panel h2 {
    font-size: clamp(12px, calc(var(--board-size) * 0.054), 23px);
    line-height: 1.1;
    color: var(--ivory);
    font-weight: 800;
}

.overlay-panel p {
    font-size: clamp(9px, calc(var(--board-size) * 0.029), 12.5px);
    line-height: 1.28;
    color: var(--muted-text);
}

.overlay-panel p strong {
    color: var(--gold);
}

.overlay-stats {
    display: flex;
    gap: clamp(4px, calc(var(--board-size) * 0.019), 8px);
    width: 100%;
    justify-content: center;
    margin: 2px 0;
}

.stat-pill {
    flex: 1;
    background: var(--bg-card-front);
    border: 1px solid var(--border-subtle);
    border-radius: 6px;
    padding: clamp(2px, calc(var(--board-size) * 0.012), 5px) clamp(3px, calc(var(--board-size) * 0.014), 6px);
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.stat-pill span {
    font-size: clamp(8px, calc(var(--board-size) * 0.024), 10px);
    color: var(--muted-text);
}

.stat-pill strong {
    font-size: clamp(11px, calc(var(--board-size) * 0.035), 15px);
    color: var(--gold);
}

.overlay-buttons {
    display: flex;
    gap: clamp(4px, calc(var(--board-size) * 0.019), 8px);
    justify-content: center;
    margin-top: 2px;
}

.primary-btn {
    background: var(--gold);
    color: var(--bg-graphite);
    font-weight: 800;
    font-size: clamp(10px, calc(var(--board-size) * 0.031), 13px);
    padding: clamp(4px, calc(var(--board-size) * 0.019), 8px) clamp(8px, calc(var(--board-size) * 0.042), 18px);
    border-radius: 6px;
    transition: background-color 0.15s;
}

.primary-btn:hover {
    background: var(--gold-bright);
}

/* Footer */
.footer-bar {
    width: min(100%, 430px);
    text-align: center;
    flex-shrink: 0;
}

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

@media (max-width: 400px) {
    :root {
        --gap: clamp(4px, 2vmin, 7px);
        --pad: clamp(5px, 2vmin, 8px);
        --board-size: min(calc(100vw - 20px), calc(100vh - 96px), 340px);
    }
    .ctrl-btn {
        padding: 5px 7px;
        font-size: 11px;
    }
    .stat-box {
        padding: 2px 6px;
        min-width: 48px;
    }
    .hint-text {
        font-size: 10px;
    }
}

@media (max-height: 440px) {
    .game-shell {
        padding: 6px 10px;
        gap: 4px;
    }
    .brand-tag {
        display: none;
    }
    .stat-box {
        padding: 2px 6px;
        min-width: 48px;
    }
    .stat-val {
        font-size: 12px;
    }
    .ctrl-btn {
        padding: 4px 8px;
        font-size: 11px;
    }
}

@media (max-height: 280px) {
    :root {
        --board-size: min(calc(100vw - 16px), calc(100vh - 48px), 340px);
    }
    .game-shell {
        padding: 4px 8px;
        gap: 2px;
    }
    .hud {
        gap: 4px;
    }
    .game-title {
        font-size: 12px;
    }
    #mute-label {
        display: none;
    }
    .stat-box {
        padding: 1px 4px;
        min-width: 38px;
    }
    .stat-label {
        font-size: 7.5px;
    }
    .stat-val {
        font-size: 10.5px;
    }
    .ctrl-btn {
        padding: 3px 6px;
        font-size: 10px;
    }
    .overlay-kicker {
        display: none;
    }
    .hint-text {
        font-size: 8.5px;
        line-height: 1.1;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
}

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

    .card-inner,
    .card-btn.is-flipped .card-inner,
    .card-btn.is-matched .card-inner {
        transform: none !important;
    }

    .card-back {
        transform: none !important;
        opacity: 0;
    }

    .card-btn.is-flipped .card-front,
    .card-btn.is-matched .card-front {
        opacity: 0;
    }

    .card-btn.is-flipped .card-back,
    .card-btn.is-matched .card-back {
        opacity: 1;
    }
}
