:root {
    --vh: 1vh;
    --bg: #050509;
    --fg: #f5f5f5;
    --muted: rgba(245, 245, 245, 0.6);
    --border: rgba(255, 255, 255, 0.1);
    --accent: #ffcc55;
    --glass: rgba(255, 255, 255, 0.05);
    --left-color: #55ddff;
    --right-color: #ff6688;
    --left-ob: rgba(120, 220, 255, 0.85);
    --right-ob: rgba(255, 160, 190, 0.88);
}

/* Colorblind palette overrides */
.cb-blue-orange {
    --left-color: #4488ff;
    --right-color: #ff8800;
    --left-ob: rgba(68, 136, 255, 0.85);
    --right-ob: rgba(255, 136, 0, 0.88);
}

.cb-green-purple {
    --left-color: #44cc66;
    --right-color: #bb66ff;
    --left-ob: rgba(68, 204, 102, 0.85);
    --right-ob: rgba(187, 102, 255, 0.88);
}

html,
body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--fg);
    font-family: 'Outfit', system-ui, -apple-system, sans-serif;
    overflow: hidden;
    height: 100%;
    -webkit-tap-highlight-color: transparent;
}

/* Scrollable body for dashboard/settings overlays */
body.scrollable {
    overflow: auto;
}

#gameContainer {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    height: calc(var(--vh) * 100);
    padding: 10px;
    box-sizing: border-box;
    gap: 8px;
}

#ui {
    width: min(640px, 96vw);
    text-align: center;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.06), rgba(255, 255, 255, 0.02));
    box-shadow: 0 12px 30px rgba(0, 0, 0, 0.35);
}

#ui h1 {
    margin: 6px 0 2px;
    font-size: 18px;
    letter-spacing: 0.8px;
}

#ui p {
    margin: 2px 0;
    font-size: 13px;
    color: var(--muted);
    line-height: 1.25;
}

#topRow {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin-top: 6px;
    flex-wrap: wrap;
}

#scoreRow {
    display: flex;
    justify-content: center;
    gap: 14px;
    font-size: 14px;
    padding: 4px 8px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: rgba(0, 0, 0, 0.18);
}

#btnRow {
    display: flex;
    gap: 8px;
    align-items: center;
    justify-content: center;
}

button {
    appearance: none;
    border: 1px solid var(--border);
    background: rgba(0, 0, 0, 0.4);
    color: var(--fg);
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 13px;
    cursor: pointer;
    transition: all 0.15s ease;
    user-select: none;
    touch-action: manipulation;
    font-family: inherit;
}

button:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.3);
}

button:active {
    transform: scale(0.96);
}

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

.btn-accent {
    background: rgba(255, 204, 85, 0.15);
    border-color: rgba(255, 204, 85, 0.4);
    color: #ffda80;
}

.btn-coffee {
    background: #FFDD00;
    color: #000;
    border: none;
    font-weight: 600;
}

/* Game Over Overlay */
#gameOverOverlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(5, 5, 9, 0.9);
    backdrop-filter: blur(12px);
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 100;
    opacity: 0;
    transition: opacity 0.3s;
    overflow-y: auto;
}

#gameOverOverlay.visible {
    opacity: 1;
}

.stats-card {
    background: linear-gradient(145deg, rgba(30, 30, 45, 0.9), rgba(20, 20, 30, 0.95));
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-top: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 20px;
    padding: 24px;
    width: min(380px, 90%);
    max-height: 85vh;
    overflow-y: auto;
    text-align: center;
    box-shadow: 0 25px 60px rgba(0, 0, 0, 0.6), inset 0 1px 0 rgba(255, 255, 255, 0.1);
    display: flex;
    flex-direction: column;
    gap: 14px;
    transform: translateY(10px);
    animation: slideUp 0.4s cubic-bezier(0.18, 0.89, 0.32, 1.28) forwards;
}

@keyframes slideUp {
    to {
        transform: translateY(0);
    }
}

.stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin: 4px 0;
}

.stats-grid.three-col {
    grid-template-columns: 1fr 1fr 1fr;
}

.stat-item {
    background: rgba(0, 0, 0, 0.25);
    padding: 10px 8px;
    border-radius: 12px;
    border: 1px solid rgba(255, 255, 255, 0.03);
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.stat-val {
    font-size: 22px;
    font-weight: 800;
    color: #fff;
    text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}

.stat-label {
    font-size: 10px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 1px;
    font-weight: 600;
    margin-top: 2px;
}

/* Left/Right breakdown section */
.lr-breakdown {
    display: flex;
    gap: 8px;
    justify-content: center;
}

.lr-side {
    flex: 1;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 12px;
    padding: 10px;
    border: 1px solid rgba(255, 255, 255, 0.04);
}

.lr-side h4 {
    margin: 0 0 6px;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 1px;
}

.lr-side.left h4 {
    color: var(--left-color);
}

.lr-side.right h4 {
    color: var(--right-color);
}

.lr-row {
    display: flex;
    justify-content: space-between;
    font-size: 12px;
    padding: 3px 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.03);
}

.lr-row:last-child {
    border-bottom: none;
}

.lr-row .lbl {
    color: var(--muted);
}

.lr-row .val {
    font-weight: 600;
}

/* AAI gauge */
.aai-gauge {
    position: relative;
    height: 24px;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 12px;
    overflow: hidden;
    margin: 4px 0;
}

.aai-gauge .aai-fill {
    position: absolute;
    top: 2px;
    bottom: 2px;
    width: 8px;
    border-radius: 4px;
    background: var(--accent);
    transition: left 0.3s ease;
}

.aai-gauge .aai-center {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 50%;
    width: 2px;
    background: rgba(255, 255, 255, 0.3);
    transform: translateX(-50%);
}

.aai-labels {
    display: flex;
    justify-content: space-between;
    font-size: 10px;
    color: var(--muted);
}

/* Timeline canvas */
#timelineCanvas {
    width: 100%;
    height: 60px;
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.2);
}

/* Insights */
.insights {
    text-align: left;
    font-size: 12px;
    line-height: 1.5;
    color: rgba(255, 255, 255, 0.8);
}

.insight-item {
    padding: 6px 8px;
    margin: 3px 0;
    background: rgba(255, 204, 85, 0.06);
    border-left: 3px solid var(--accent);
    border-radius: 0 6px 6px 0;
}

/* Section headers in results */
.section-label {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 1.5px;
    color: var(--muted);
    margin: 4px 0 2px;
    font-weight: 600;
}

.share-row {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 8px;
    margin-top: 6px;
}

.share-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 44px;
    border-radius: 10px;
    border: none;
    font-size: 20px;
    color: white;
    transition: transform 0.1s;
}

.btn-x {
    background: #000;
    border: 1px solid rgba(255, 255, 255, 0.2);
}

.btn-wa {
    background: #25D366;
}

.btn-copy {
    background: rgba(255, 255, 255, 0.1);
}

.share-btn:active {
    transform: scale(0.94);
}

#shareMsg {
    grid-column: span 3;
    font-size: 11px;
    color: var(--muted);
    margin-top: -6px;
}

/* Footer */
#footer {
    margin-top: 12px;
    display: flex;
    gap: 10px;
    justify-content: center;
    flex-wrap: wrap;
}

#message {
    margin-top: 6px;
    font-size: 14px;
    color: var(--accent);
    min-height: 18px;
}

.orientHint {
    margin: 8px auto 0;
    width: fit-content;
    max-width: 100%;
    padding: 8px 10px;
    border-radius: 10px;
    border: 1px solid rgba(255, 204, 85, 0.35);
    background: rgba(255, 204, 85, 0.10);
    color: rgba(255, 240, 210, 0.95);
    font-size: 12.5px;
    line-height: 1.25;
}

.orientHint b {
    color: rgba(255, 230, 170, 1);
}

#history {
    margin-top: 6px;
    font-size: 13px;
    color: var(--muted);
    display: flex;
    flex-direction: column;
    gap: 4px;
    align-items: center;
}

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

#historyList {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 6px;
}

.chip {
    border: 1px solid var(--border);
    background: rgba(0, 0, 0, 0.18);
    border-radius: 999px;
    padding: 4px 10px;
    font-size: 12px;
    color: rgba(245, 245, 245, 0.85);
}

#gameCanvas {
    width: min(640px, 96vw);
    height: min(78vh, 860px);
    border: 2px solid rgba(255, 255, 255, 0.18);
    border-radius: 14px;
    background: radial-gradient(circle at top, #181830 0, #050509 60%);
    box-shadow: 0 18px 40px rgba(0, 0, 0, 0.45);
    touch-action: none;
    display: block;
}

/* High contrast mode */
.high-contrast #gameCanvas {
    border-width: 3px;
    border-color: rgba(255, 255, 255, 0.5);
}

/* Inline difficulty select */
#difficultySelect {
    appearance: none;
    -webkit-appearance: none;
    border: 1px solid var(--border);
    background: rgba(0, 0, 0, 0.4);
    color: var(--fg);
    padding: 8px 28px 8px 10px;
    border-radius: 8px;
    font-size: 13px;
    font-family: inherit;
    cursor: pointer;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='rgba(245,245,245,0.6)'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 8px center;
}

#difficultySelect option {
    background: #1a1a2e;
    color: var(--fg);
}

/* Difficulty selector */
.mode-selector {
    display: flex;
    gap: 6px;
    justify-content: center;
    flex-wrap: wrap;
    margin: 6px 0;
}

.mode-btn {
    padding: 6px 14px;
    font-size: 12px;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: rgba(0, 0, 0, 0.3);
    color: var(--muted);
    cursor: pointer;
    transition: all 0.15s;
}

.mode-btn.active {
    background: rgba(255, 204, 85, 0.15);
    border-color: rgba(255, 204, 85, 0.5);
    color: var(--accent);
    font-weight: 600;
}

/* Settings panel */
#settingsPanel {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(5, 5, 9, 0.95);
    backdrop-filter: blur(12px);
    z-index: 200;
    overflow-y: auto;
    padding: 20px;
    box-sizing: border-box;
}

#settingsPanel.visible {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.settings-content {
    width: min(440px, 94%);
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.settings-section {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 16px;
}

.settings-section h3 {
    margin: 0 0 10px;
    font-size: 14px;
    color: var(--accent);
}

.setting-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.03);
    font-size: 13px;
}

.setting-row:last-child {
    border-bottom: none;
}

.toggle-switch {
    position: relative;
    width: 40px;
    height: 22px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 11px;
    cursor: pointer;
    transition: background 0.2s;
}

.toggle-switch.on {
    background: rgba(255, 204, 85, 0.4);
}

.toggle-switch::after {
    content: '';
    position: absolute;
    top: 2px;
    left: 2px;
    width: 18px;
    height: 18px;
    background: white;
    border-radius: 50%;
    transition: transform 0.2s;
}

.toggle-switch.on::after {
    transform: translateX(18px);
}

/* Dashboard overlay */
#dashboardPanel {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(5, 5, 9, 0.95);
    backdrop-filter: blur(12px);
    z-index: 200;
    overflow-y: auto;
    padding: 20px;
    box-sizing: border-box;
}

#dashboardPanel.visible {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.dashboard-content {
    width: min(500px, 94%);
    display: flex;
    flex-direction: column;
    gap: 16px;
    padding-bottom: 30px;
}

.dashboard-card {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 16px;
}

.dashboard-card h3 {
    margin: 0 0 10px;
    font-size: 14px;
    color: var(--accent);
}

.dashboard-card canvas {
    width: 100%;
    height: 120px;
    border-radius: 8px;
}

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

.record-item {
    text-align: center;
    padding: 10px;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 10px;
}

.record-val {
    font-size: 20px;
    font-weight: 800;
    color: var(--accent);
}

.record-label {
    font-size: 10px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-top: 2px;
}

/* Tutorial overlay */
#tutorialOverlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(5, 5, 9, 0.92);
    backdrop-filter: blur(8px);
    z-index: 300;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 20px;
    box-sizing: border-box;
}

#tutorialOverlay.visible {
    display: flex;
}

.tutorial-box {
    max-width: 360px;
    width: 90%;
}

.tutorial-step {
    font-size: 16px;
    line-height: 1.5;
    margin-bottom: 12px;
}

.tutorial-step .key {
    display: inline-block;
    background: rgba(255, 204, 85, 0.2);
    border: 1px solid rgba(255, 204, 85, 0.4);
    border-radius: 6px;
    padding: 2px 10px;
    font-weight: 700;
    color: var(--accent);
    font-size: 18px;
}

.tutorial-progress {
    display: flex;
    gap: 6px;
    justify-content: center;
    margin-top: 12px;
}

.tutorial-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.2);
}

.tutorial-dot.active {
    background: var(--accent);
}

/* Achievement toast */
.achievement-toast {
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translateX(-50%) translateY(-100px);
    background: linear-gradient(135deg, rgba(40, 35, 10, 0.95), rgba(30, 25, 8, 0.95));
    border: 1px solid rgba(255, 204, 85, 0.4);
    border-radius: 12px;
    padding: 12px 20px;
    z-index: 400;
    display: flex;
    align-items: center;
    gap: 10px;
    transition: transform 0.4s cubic-bezier(0.18, 0.89, 0.32, 1.28);
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}

.achievement-toast.show {
    transform: translateX(-50%) translateY(0);
}

.achievement-toast .ach-icon {
    font-size: 24px;
}

.achievement-toast .ach-text {
    font-size: 13px;
}

.achievement-toast .ach-name {
    font-weight: 700;
    color: var(--accent);
}

/* Training panel overlay */
#trainingPanel {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(5, 5, 9, 0.95);
    backdrop-filter: blur(12px);
    z-index: 200;
    overflow-y: auto;
    padding: 20px;
    box-sizing: border-box;
}

#trainingPanel.visible {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.training-content {
    width: min(440px, 94%);
    display: flex;
    flex-direction: column;
    gap: 12px;
}

/* Training session selector */
.training-selector {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin: 8px 0;
}

.training-option {
    background: rgba(0, 0, 0, 0.25);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px;
    cursor: pointer;
    text-align: left;
    transition: all 0.15s;
}

.training-option:hover {
    border-color: rgba(255, 204, 85, 0.3);
    background: rgba(255, 204, 85, 0.05);
}

.training-option h4 {
    margin: 0 0 3px;
    font-size: 13px;
    color: var(--fg);
}

.training-option p {
    margin: 0;
    font-size: 11px;
    color: var(--muted);
}

/* Reduced motion */
.reduced-motion * {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
}

@media (max-width: 420px) {
    #ui h1 {
        font-size: 16px;
    }

    #scoreRow {
        font-size: 13px;
    }

    button {
        padding: 10px 12px;
    }

    .stats-card {
        padding: 18px;
        gap: 10px;
    }

    .stat-val {
        font-size: 18px;
    }
}
