:root {
    --bg: #f3f6fb;
    --bg-elevated: #ffffff;
    --bg-card: #ffffff;
    --line: rgba(20, 36, 68, 0.12);
    --text: #101828;
    --muted: #667085;
    --accent: #00a884;
    --accent-2: #2563eb;
    --danger: #e11d48;
    --warn: #d97706;
    --shadow: 0 10px 28px rgba(16, 24, 40, 0.08);
    --radius: 6px;
    --font: "Syne", system-ui, -apple-system, sans-serif;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
}

*,
*::before,
*::after {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html,
body {
    margin: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background:
        linear-gradient(135deg, rgba(37, 99, 235, 0.06) 0%, transparent 42%),
        linear-gradient(315deg, rgba(0, 168, 132, 0.07) 0%, transparent 40%),
        #f0f3f8;
    color: var(--text);
    font-family: var(--font);
    letter-spacing: -0.02em;
}

body {
    touch-action: manipulation;
    user-select: none;
    display: flex;
    flex-direction: column;
    min-height: 100dvh;
}

/* Minimal SumoMath header */
.site-header {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: 8px;
    padding:
        calc(4px + var(--safe-top))
        calc(10px + var(--safe-right))
        4px
        calc(10px + var(--safe-left));
    background: #fff;
    border-bottom: 1px solid var(--line);
    z-index: 60;
}

.site-logo {
    display: inline-flex;
    align-items: center;
    line-height: 0;
    flex-shrink: 0;
}

.site-logo img {
    display: block;
    height: 32px;
    width: auto;
}

.site-nav {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    flex-wrap: nowrap;
    gap: 4px;
    margin-left: auto;
    min-width: 0;
    overflow: hidden;
}

.site-nav-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 1 auto;
    max-width: 100%;
    padding: 5px 7px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: #f5f7fb;
    color: var(--text);
    font-family: var(--font);
    font-size: clamp(0.55rem, 2.1vw, 0.72rem);
    font-weight: 800;
    letter-spacing: 0;
    text-decoration: none;
    white-space: nowrap;
    line-height: 1.2;
}

.site-nav-link.active {
    background: #fff;
    border-color: #101828;
    border-width: 2px;
    color: var(--text);
    padding: 4px 6px;
}

.site-nav-link:active {
    transform: scale(0.97);
}

#app {
    position: relative;
    width: 100%;
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding:
        6px
        calc(16px + var(--safe-right))
        calc(12px + var(--safe-bottom))
        calc(16px + var(--safe-left));
}

.screen {
    display: none;
    width: 100%;
    flex: 1;
    min-height: 0;
}

.screen.active {
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: clamp(10px, 2.5vh, 22px);
}

/* Settings: pin content to top instead of vertical center */
#settingsScreen.screen.active {
    justify-content: flex-start;
    gap: 12px;
    padding-top: 0;
    margin-top: 12px;
}

#app:has(#settingsScreen.active) {
    gap: 0;
}

/* Hide score / streak / stop on settings */
#app:has(#settingsScreen.active) .game-bar {
    display: none;
}

/* Allow scroll on settings so explanation text is reachable */
html:has(#settingsScreen.active),
html:has(#settingsScreen.active) body {
    height: auto;
    overflow: auto;
    min-height: 100dvh;
}

/* Explanation / SEO copy — settings screen only */
.seo-panel {
    display: none;
    max-width: 920px;
    width: 100%;
    margin: 0.5rem auto 2rem;
    padding: 0 calc(16px + var(--safe-right)) 0 calc(16px + var(--safe-left));
    box-sizing: border-box;
}
body:has(#settingsScreen.active) .seo-panel {
    display: block;
}
.seo-panel h2 {
    margin: 0 0 0.6rem;
    font-size: 1.15rem;
    font-weight: 800;
    letter-spacing: -0.02em;
}
.seo-panel h3 {
    margin: 1.25rem 0 0.4rem;
    font-size: 0.95rem;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--muted);
}
.seo-panel p,
.seo-panel li {
    margin: 0 0 0.65rem;
    color: var(--muted);
    font-size: 0.95rem;
    font-weight: 500;
    line-height: 1.55;
    user-select: text;
}
.seo-panel a {
    color: var(--accent-2);
    font-weight: 700;
    text-decoration: underline;
}
.seo-panel strong {
    color: var(--text);
    font-weight: 800;
}
.seo-panel ul {
    margin: 0 0 0.75rem;
    padding-left: 1.2rem;
}
.seo-card {
    background: var(--bg-card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: clamp(14px, 2.5vw, 22px);
    box-shadow: var(--shadow);
}

/* Desktop: restore larger space under header (stats bar used to fill this) */
@media (min-width: 768px) {
    #settingsScreen.screen.active {
        margin-top: 100px;
    }
}

#settingsScreen .brand {
    padding-right: 12px;
}

#settingsScreen .brand h1 {
    font-size: clamp(0.85rem, 4.2vw, 1.45rem);
    white-space: nowrap;
}

/* TOP BAR ------------------------------------------------- */
.game-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
    z-index: 50;
    position: relative;
}

.stat-pill {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    gap: 8px;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 8px 12px;
    font-weight: 700;
    font-size: clamp(0.9rem, 2.2vw, 1.05rem);
    box-shadow: var(--shadow);
    white-space: nowrap;
}

.stat-value {
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
}

.stat-label {
    color: var(--muted);
    font-size: 0.68rem;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    font-weight: 700;
    white-space: nowrap;
}

/* SETTINGS ------------------------------------------------ */
#settingsScreen {
    max-width: 920px;
    width: 100%;
    margin: 0 auto;
    padding: 0 4px;
    box-sizing: border-box;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    min-width: 0;
    padding-right: 12px;
    box-sizing: border-box;
}

.brand-mark {
    display: block;
    width: 14px;
    height: 14px;
    flex: 0 0 14px;
    border-radius: 2px;
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    box-shadow: 0 0 0 3px rgba(0, 168, 132, 0.15);
}

.brand h1 {
    margin: 0;
    font-size: clamp(0.85rem, 4.2vw, 1.45rem);
    font-weight: 800;
    letter-spacing: -0.03em;
    text-transform: uppercase;
    white-space: nowrap;
    line-height: 1.15;
}

.tagline {
    margin: 0 0 20px;
    color: var(--muted);
    font-size: clamp(0.9rem, 2.4vw, 1.05rem);
    font-weight: 500;
}

.settings-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: clamp(10px, 2vw, 18px);
}

.setting-card {
    background: var(--bg-card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: clamp(12px, 2vw, 18px);
    box-shadow: var(--shadow);
}

.setting-label {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: 12px;
}

.setting-label span {
    color: var(--muted);
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    font-weight: 700;
}

.chip-row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

#rodsChips {
    flex-wrap: nowrap;
}

.chip {
    appearance: none;
    border: 1px solid var(--line);
    background: #eef1f6;
    color: var(--text);
    border-radius: var(--radius);
    min-width: 52px;
    height: 40px;
    padding: 0 14px;
    font: inherit;
    font-weight: 700;
    font-size: 0.95rem;
    cursor: pointer;
    transition: transform 0.1s ease, background 0.1s ease, border-color 0.1s ease;
}

.chip:active {
    transform: scale(0.97);
}

.chip.active {
    background: #101828;
    border-color: #101828;
    color: #fff;
}

/* GAME ---------------------------------------------------- */
#gameScreen {
    gap: 0;
}

#stage {
    position: relative;
    flex: 1;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #ffffff;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow);
}

#canvas {
    display: block;
    max-width: 98%;
    max-height: 96%;
    width: auto;
    height: auto;
}

.flash-cue {
    position: absolute;
    top: 12px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 0.72rem;
    letter-spacing: 0.28em;
    font-weight: 800;
    color: var(--accent);
    opacity: 0;
    transition: opacity 0.15s ease;
    pointer-events: none;
}

.flash-cue.show {
    opacity: 1;
}

/* BUTTONS ------------------------------------------------- */
.btn {
    appearance: none;
    border: none;
    border-radius: var(--radius);
    font: inherit;
    font-weight: 800;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    cursor: pointer;
    transition: transform 0.1s ease, opacity 0.1s ease;
}

.btn:active {
    transform: scale(0.97);
}

.btn-primary {
    background: #101828;
    color: #fff;
    box-shadow: 0 8px 20px rgba(16, 24, 40, 0.2);
}

.btn-primary:hover {
    background: #1d2939;
}

.btn-xl {
    width: min(360px, 100%);
    height: 54px;
    font-size: 1rem;
    align-self: center;
    margin-top: 4px;
}

@media (min-width: 768px) {
    #settingsScreen #startBtn.btn-xl {
        margin-top: 24px;
    }
}

#againBtn {
    width: 100%;
    height: 58px;
    font-size: 1.1rem;
    margin-top: 4px;
}

.btn-stop {
    margin-left: auto;
    height: 42px;
    padding: 0 20px;
    background: #fff;
    color: var(--danger);
    border: 1px solid rgba(225, 29, 72, 0.4);
    box-shadow: var(--shadow);
}

.btn-stop:hover:not(:disabled),
.btn-stop:focus-visible:not(:disabled) {
    background: rgba(225, 29, 72, 0.08);
}

.btn-stop:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    transform: none;
}

.btn-ghost {
    background: #eef1f6;
    color: var(--text);
    border: 1px solid var(--line);
    height: 44px;
    padding: 0 22px;
    width: 100%;
    margin-top: 8px;
}

/* MODALS -------------------------------------------------- */
.modal {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 12px;
    padding-top: calc(58px + var(--safe-top));
    background: rgba(16, 24, 40, 0.28);
    backdrop-filter: blur(4px);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s ease;
    z-index: 40;
}

.modal.open {
    opacity: 1;
    pointer-events: auto;
}

.modal-card {
    width: min(420px, 94vw);
    background: var(--bg-elevated);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: clamp(14px, 2.5vw, 22px);
    box-shadow: 0 18px 40px rgba(16, 24, 40, 0.16);
    text-align: center;
}

.answer-card {
    width: min(380px, 92vw);
}

.modal-kicker {
    margin: 0 0 10px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.16em;
    font-size: 0.72rem;
    font-weight: 800;
}

.answer-display {
    width: 100%;
    height: 56px;
    border-radius: var(--radius);
    border: 2px solid #101828;
    background: #f8fafc;
    color: var(--text);
    font-size: clamp(1.8rem, 6vw, 2.3rem);
    font-weight: 800;
    letter-spacing: 0.08em;
    display: grid;
    place-items: center;
    margin-bottom: 12px;
}

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

.key {
    appearance: none;
    border: 1px solid var(--line);
    background: #eef1f6;
    color: var(--text);
    border-radius: var(--radius);
    height: clamp(44px, 12vh, 56px);
    font: inherit;
    font-size: 1.25rem;
    font-weight: 800;
    cursor: pointer;
    transition: transform 0.08s ease, background 0.08s ease;
}

.key:active {
    transform: scale(0.96);
    background: #dde3ec;
}

.key-action {
    background: #fff;
    color: var(--muted);
}

.key-go {
    background: #101828;
    color: #fff;
    border-color: #101828;
    font-size: 0.95rem;
    letter-spacing: 0.08em;
}

.key-go:active {
    background: #1d2939;
}

.feedback {
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid var(--line);
}

.feedback.hidden {
    display: none;
}

.feedback-icon {
    width: 40px;
    height: 40px;
    margin: 0 auto 8px;
    border-radius: 4px;
    display: grid;
    place-items: center;
    font-size: 1.25rem;
    font-weight: 800;
}

.feedback-icon.ok {
    background: rgba(0, 168, 132, 0.14);
    color: var(--accent);
}

.feedback-icon.bad {
    background: rgba(225, 29, 72, 0.1);
    color: var(--danger);
}

.feedback-text {
    margin: 0;
    font-size: 1.15rem;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.feedback-text.bad {
    color: var(--danger);
    font-size: clamp(1.35rem, 5vw, 1.7rem);
}

.feedback-text.ok {
    color: var(--accent);
}

.feedback-meta {
    margin: 10px 0 0;
    color: var(--muted);
    font-size: 0.95rem;
    font-weight: 600;
}

.feedback-answer {
    margin: 8px 0 0;
    color: var(--text);
    font-size: clamp(2.4rem, 12vw, 3.4rem);
    font-weight: 800;
    letter-spacing: 0.04em;
    line-height: 1.1;
}

.feedback-answer.bad {
    color: var(--danger);
}

.feedback-answer.ok {
    color: var(--accent);
}

.summary-card h2 {
    margin: 0 0 18px;
    font-size: clamp(1.4rem, 4vw, 1.9rem);
    letter-spacing: -0.03em;
    font-weight: 800;
    text-transform: uppercase;
}

.summary-stats {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
    margin-bottom: 20px;
    width: 100%;
}

.summary-stats div {
    background: #eef1f6;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 12px 6px;
    min-width: 0;
    overflow: hidden;
}

@media (max-width: 767px) {
    .summary-stats div {
        padding: 12px 10px;
    }

    .summary-stats .stat-label {
        display: block;
        padding-right: 4px;
        line-height: 1.25;
        letter-spacing: 0.04em;
    }
}

.summary-stats strong {
    display: block;
    margin-top: 6px;
    font-size: clamp(0.95rem, 3.8vw, 1.25rem);
    font-weight: 800;
    font-variant-numeric: tabular-nums;
    overflow-wrap: anywhere;
    word-break: break-word;
    line-height: 1.15;
}

.summary-times {
    margin-top: 10px;
}

.summary-times strong {
    font-size: clamp(0.85rem, 3.4vw, 1.1rem);
    letter-spacing: -0.02em;
}

.summary-times .stat-label {
    font-size: 0.62rem;
}

.feedback-time {
    margin: 10px 0 0;
    font-size: clamp(1.4rem, 6vw, 1.9rem);
    font-weight: 800;
    letter-spacing: 0.02em;
    font-variant-numeric: tabular-nums;
    color: #101828;
}

.modal-card.summary-card {
    width: min(420px, 92vw);
    max-width: 100%;
    overflow: hidden;
}

/* Portrait-first: nudge to rotate when landscape */
@media (orientation: landscape) {
    .settings-grid {
        grid-template-columns: 1fr 1fr;
    }
}

/* Mobile landscape only — hide on desktop */
@media (orientation: landscape) and (max-height: 500px) and (hover: none) {
    #app::after {
        content: "Rotate to portrait";
        position: fixed;
        bottom: calc(10px + var(--safe-bottom));
        left: 50%;
        transform: translateX(-50%);
        background: rgba(217, 119, 6, 0.1);
        color: var(--warn);
        border: 1px solid rgba(217, 119, 6, 0.3);
        border-radius: var(--radius);
        padding: 6px 12px;
        font-size: 0.75rem;
        font-weight: 700;
        pointer-events: none;
        z-index: 5;
    }
}

@media (orientation: portrait) and (max-width: 480px) {
    .key {
        height: clamp(48px, 9vh, 60px);
    }

    .answer-display {
        height: 60px;
    }
}
