:root {
    --bg-dark: #0b0d12;
    --bg-darker: #070910;
    --surface: rgba(255, 255, 255, 0.06);
    --surface-2: rgba(255, 255, 255, 0.09);
    --stroke: rgba(255, 255, 255, 0.12);
    --text-primary: #ffffff;
    --text-secondary: rgba(255, 255, 255, 0.6);
    --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    /* Fluid sizing across phones/tablets/desktop */
    --slider-thickness: clamp(48px, 12vw, 92px);
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --shadow-1: 0 18px 50px rgba(0,0,0,0.55);
    --shadow-2: 0 10px 26px rgba(0,0,0,0.35);
    --radius-xl: 28px;
    --radius-lg: 20px;
    --radius-md: 14px;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: var(--font-family);
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

button:focus-visible,
input[type="range"]:focus-visible {
    outline: 3px solid rgba(255, 255, 255, 0.9);
    outline-offset: 4px;
}

body {
    background-color: var(--bg-dark);
    color: var(--text-primary);
    overflow: hidden;
    height: 100dvh;
    width: 100vw;
}

.app-container::before {
    content: "";
    position: absolute;
    inset: -80px;
    background:
        radial-gradient(900px 520px at 15% 15%, rgba(255, 138, 0, 0.14), transparent 60%),
        radial-gradient(820px 520px at 85% 20%, rgba(229, 46, 113, 0.14), transparent 62%),
        radial-gradient(700px 520px at 60% 85%, rgba(155, 45, 235, 0.12), transparent 60%),
        radial-gradient(900px 700px at 50% 55%, rgba(255, 255, 255, 0.05), transparent 65%),
        linear-gradient(180deg, var(--bg-dark), var(--bg-darker));
    filter: blur(0px);
    pointer-events: none;
    z-index: 0;
}

.app-container::after {
    content: "";
    position: absolute;
    inset: 0;
    background-image:
        radial-gradient(rgba(255,255,255,0.05) 1px, transparent 1px);
    background-size: 3px 3px;
    opacity: 0.05;
    mix-blend-mode: overlay;
    pointer-events: none;
    z-index: 0;
}

.app-container {
    width: 100vw;
    height: 100dvh;
    background-color: var(--bg-dark);
    position: relative;
    overflow: hidden;
}

.stage {
    display: none;
    height: 100%;
    width: 100%;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 1; /* ensure above background layers */
}

.stage.active {
    display: flex;
    flex-direction: column;
    animation: stageFadeIn 0.3s ease-out forwards;
}

/* Common utilities */
.header-row {
    display: flex;
    justify-content: space-between;
    padding: clamp(16px, 2.6vw, 28px);
    width: 100%;
    z-index: 10;
}

.round-text {
    font-size: clamp(12px, 1.6vw, 14px);
    font-weight: 500;
    letter-spacing: 1px;
}

.brand-text {
    font-size: clamp(12px, 1.6vw, 14px);
    font-weight: 500;
    color: var(--text-secondary);
}

/* Stage 0: Start */
#stage-start {
    justify-content: center;
    align-items: center;
    padding: clamp(16px, 3vw, 28px);
}

.start-content {
    display: flex;
    flex-direction: column;
    gap: 32px;
    width: 100%;
    align-items: center;
    text-align: center;
    max-width: min(560px, 92vw);
    padding: clamp(18px, 3vw, 30px);
    border-radius: var(--radius-xl);
    background: linear-gradient(180deg, rgba(255,255,255,0.08), rgba(255,255,255,0.04));
    border: 1px solid var(--stroke);
    box-shadow: var(--shadow-1);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    position: relative;
    z-index: 1;
}

.start-content h1 {
    font-size: clamp(40px, 7vw, 84px);
    font-weight: 800;
    letter-spacing: -1px;
    background: linear-gradient(90deg, #ff8a00, #e52e71, #ff8a00);
    background-size: 200% auto;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    animation: gradientShine 3s linear infinite, fadeInUp 0.8s ease-out forwards, floating 3s ease-in-out infinite 0.8s;
    opacity: 0;
    transform: translateY(20px);
}

.subtitle {
    color: var(--text-secondary);
    font-size: clamp(15px, 2.6vw, 18px);
    margin-top: -24px;
    font-weight: 500;
    animation: fadeInUp 0.8s ease-out 0.2s forwards;
    opacity: 0;
    transform: translateY(20px);
}

.difficulty-selector {
    display: flex;
    gap: 8px;
    width: 100%;
    background: rgba(0,0,0,0.18);
    padding: 8px;
    border-radius: clamp(14px, 2.2vw, 18px);
    border: 1px solid rgba(255,255,255,0.08);
    margin-bottom: 16px;
    animation: fadeInUp 0.8s ease-out 0.4s forwards;
    opacity: 0;
    transform: translateY(20px);
}

.diff-btn {
    flex: 1;
    padding: 14px 0;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    border-radius: clamp(10px, 1.8vw, 12px);
    font-weight: 600;
    font-size: clamp(14px, 2vw, 16px);
    cursor: pointer;
    transition: transform 0.15s ease, background 0.2s ease, color 0.2s ease, box-shadow 0.2s ease;
}

.diff-btn:hover {
    background: rgba(255,255,255,0.06);
    color: rgba(255,255,255,0.9);
}

.diff-btn.active {
    background: white;
    color: black;
    box-shadow: 0 10px 24px rgba(0,0,0,0.35);
}

.btn {
    padding: clamp(14px, 2.6vw, 18px);
    border-radius: 30px;
    font-size: clamp(16px, 2.2vw, 18px);
    font-weight: 600;
    cursor: pointer;
    text-align: center;
    width: 100%;
    transition: transform 0.18s ease, box-shadow 0.18s ease, filter 0.18s ease;
}

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

#btn-start {
    animation: fadeInUp 0.8s ease-out 0.6s forwards;
    opacity: 0;
    transform: translateY(20px);
}

#btn-start:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 24px rgba(255, 255, 255, 0.2);
}

.btn.outline {
    background: transparent;
    border: 1px solid rgba(255,255,255,0.5);
    color: white;
}

.btn.solid {
    background: white;
    border: none;
    color: black;
}

.btn.solid:hover {
    filter: brightness(1.03);
    box-shadow: 0 16px 34px rgba(255,255,255,0.12), 0 10px 26px rgba(0,0,0,0.35);
}

.btn.outline:hover {
    background: rgba(255,255,255,0.06);
    box-shadow: var(--shadow-2);
    transform: translateY(-1px);
}

/* Stage 1: Memorize */
#stage-memorize {
    background-color: var(--bg-dark);
    justify-content: space-between;
}

.timer-container {
    text-align: right;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

.timer-numbers {
    display: flex;
    align-items: baseline;
}

.timer-large {
    font-size: clamp(86px, 14vw, 180px);
    font-weight: 700;
    line-height: 1;
    letter-spacing: -2px;
}

.timer-small {
    font-size: clamp(86px, 14vw, 180px);
    font-weight: 700;
    line-height: 1;
    color: var(--text-secondary);
    letter-spacing: -2px;
}

.timer-label {
    font-size: clamp(14px, 2.2vw, 16px);
    font-weight: 600;
    margin-top: -10px;
}

.footer-row {
    padding: 24px;
    text-align: right;
    width: 100%;
}

/* Stage 2: Play */
.play-content {
    position: relative;
    width: 100%;
    height: 100%;
    background-color: #333;
    transition: background-color 0.1s ease;
}

.play-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 10;
}

.play-overlay .header-row, .play-overlay .icon-btn {
    pointer-events: auto;
}

.sliders-container {
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    display: flex;
    background: transparent;
    z-index: 2;
    pointer-events: none;
}

.slider-wrapper {
    width: var(--slider-thickness);
    height: 100%;
    position: relative;
    pointer-events: auto;
}

/* Vertical Sliders styling */
.vertical-slider {
    -webkit-appearance: none;
    appearance: none;
    height: var(--slider-thickness);
    background: transparent;
    transform: rotate(-90deg);
    transform-origin: left top;
    position: absolute;
    top: 100%;
    left: 0;
    margin: 0;
    outline: none;
}

/* Specific slider tracks */
.hue-slider {
    background: linear-gradient(to right, #f00 0%, #ff0 17%, #0f0 33%, #0ff 50%, #00f 67%, #f0f 83%, #f00 100%);
}

.sat-slider {
    background: linear-gradient(to right, #808080, #ff00ff);
}

.lit-slider {
    background: linear-gradient(to right, #000, #808080, #fff);
}

/* Slider thumbs */
.vertical-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: clamp(22px, 3.8vw, 32px);
    height: clamp(22px, 3.8vw, 32px);
    border-radius: 50%;
    background: white;
    cursor: pointer;
    box-shadow: 0 2px 6px rgba(0,0,0,0.4);
    border: none;
    transition: transform 0.2s, box-shadow 0.2s;
}

.vertical-slider::-webkit-slider-thumb:active {
    transform: scale(1.3);
    box-shadow: 0 0 15px rgba(255,255,255,0.8);
}

.vertical-slider::-moz-range-thumb {
    width: clamp(22px, 3.8vw, 32px);
    height: clamp(22px, 3.8vw, 32px);
    border-radius: 50%;
    background: white;
    cursor: pointer;
    box-shadow: 0 2px 6px rgba(0,0,0,0.4);
    border: none;
    transition: transform 0.2s, box-shadow 0.2s;
}

.vertical-slider::-moz-range-thumb:active {
    transform: scale(1.3);
    box-shadow: 0 0 15px rgba(255,255,255,0.8);
}

.icon-btn {
    width: clamp(56px, 9vw, 72px);
    height: clamp(56px, 9vw, 72px);
    border-radius: 50%;
    background: white;
    border: none;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    position: absolute;
    box-shadow: 0 12px 28px rgba(0,0,0,0.35);
    transition: transform 0.12s ease, box-shadow 0.12s ease, filter 0.12s ease;
}

.icon-btn:active {
    transform: scale(0.95);
}

.icon-btn:hover {
    transform: translateY(-1px);
    filter: brightness(1.03);
    box-shadow: 0 18px 40px rgba(0,0,0,0.45);
}

.bottom-right {
    bottom: calc(24px + var(--safe-bottom));
    right: calc(24px + var(--safe-right));
}

/* Stage 3: Result */
.split-top, .split-bottom {
    flex: 1;
    position: relative;
    width: 100%;
}

.split-top {
    color: white; 
}

#stage-result.active .split-top {
    animation: ninjaCutTop 0.6s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}
#stage-result.active .split-bottom {
    animation: ninjaCutBottom 0.6s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

.split-content {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 24px;
}

.score-container {
    text-align: right;
    max-width: 60%;
}

.score-large {
    font-size: clamp(64px, 10vw, 120px);
    font-weight: 700;
    line-height: 1;
    letter-spacing: -2px;
}

.feedback-text {
    font-size: 16px;
    color: rgba(255,255,255,0.8);
    line-height: 1.2;
    margin-top: 8px;
    font-weight: 500;
    text-shadow: 0 1px 4px rgba(0,0,0,0.5);
}

.bottom-left-info {
    display: flex;
    flex-direction: column;
    gap: 4px;
    position: absolute;
    bottom: 24px;
    left: 24px;
}

.bottom-left-info .label {
    font-size: 14px;
    color: rgba(255,255,255,0.8);
    font-weight: 500;
    text-shadow: 0 1px 3px rgba(0,0,0,0.5);
}

.bottom-left-info .value {
    font-size: 16px;
    font-weight: 700;
    letter-spacing: 0.5px;
    text-shadow: 0 1px 3px rgba(0,0,0,0.5);
}

/* Stage 4: Final Summary */
#stage-final {
    background-color: transparent;
    padding: 32px 24px;
    justify-content: center;
    align-items: center;
}

.final-content {
    display: flex;
    flex-direction: column;
    gap: 22px;
    width: 100%;
    max-width: 600px;
    text-align: center;
    border-radius: var(--radius-xl);
    background:
        radial-gradient(900px 260px at 50% 0%, rgba(255,255,255,0.10), transparent 55%),
        linear-gradient(180deg, rgba(255,255,255,0.07), rgba(255,255,255,0.03));
    border: 1px solid rgba(255,255,255,0.12);
    box-shadow: var(--shadow-1);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    padding: 28px 22px;
    position: relative;
    z-index: 1;
}

.final-content::before {
    content: "";
    position: absolute;
    inset: -1px;
    border-radius: inherit;
    padding: 1px;
    background: linear-gradient(135deg, rgba(255,138,0,0.45), rgba(229,46,113,0.35), rgba(155,45,235,0.35));
    -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
    -webkit-mask-composite: xor;
    mask-composite: exclude;
    pointer-events: none;
    opacity: 0.55;
}

.final-content > * {
    opacity: 0;
    transform: translateY(20px);
}

#stage-final.active .final-header {
    animation: fadeInUp 0.6s ease-out forwards;
}

#stage-final.active .rank-row {
    animation: fadeInUp 0.6s ease-out 0.1s forwards;
}

#stage-final.active .rounds-bar {
    animation: fadeInUp 0.6s ease-out 0.2s forwards;
}

#stage-final.active .action-buttons {
    animation: fadeInUp 0.6s ease-out 0.3s forwards;
}

.final-header {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 8px;
    position: relative;
    min-height: 48px;
}

#final-title {
    font-size: clamp(22px, 3.6vw, 30px);
    font-weight: 700;
    line-height: 1.3;
    padding: 0 48px;
    letter-spacing: -0.2px;
}

.badge {
    position: absolute;
    left: 0;
    top: 2px;
    padding: 8px 10px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 800;
    letter-spacing: 0.5px;
    color: rgba(0,0,0,0.9);
    background: linear-gradient(90deg, #ffd700, #ff8a00);
    box-shadow: 0 14px 30px rgba(255, 191, 0, 0.10), 0 10px 24px rgba(0,0,0,0.35);
    transform: translateY(0);
    animation: popIn 0.35s cubic-bezier(0.16, 1, 0.3, 1) both;
}

.badge[hidden] {
    display: none !important;
}

.close-btn {
    background: rgba(255,255,255,0.10);
    border: 1px solid rgba(255,255,255,0.14);
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    position: absolute;
    right: 0;
    top: 0;
    transition: transform 0.15s ease, background 0.15s ease, border-color 0.15s ease;
}

.close-btn:hover {
    background: rgba(255,255,255,0.14);
    border-color: rgba(255,255,255,0.22);
}

.close-btn:active {
    transform: scale(0.9);
    background: rgba(255,255,255,0.16);
}

.rank-row {
    width: 100%;
    display: flex;
    justify-content: center;
}

.stats-grid {
    width: 100%;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.stat {
    text-align: left;
    padding: 12px 12px;
    border-radius: 16px;
    background: rgba(0,0,0,0.18);
    border: 1px solid rgba(255,255,255,0.10);
    box-shadow: 0 10px 20px rgba(0,0,0,0.18);
}

.stat.wide {
    grid-column: 1 / -1;
}

.stat-label {
    font-size: 13px;
    font-weight: 600;
    color: rgba(255,255,255,0.65);
    letter-spacing: 0.4px;
}

.stat-value {
    margin-top: 6px;
    font-size: clamp(18px, 3.2vw, 28px);
    font-weight: 800;
    letter-spacing: -0.4px;
}

.stat-suffix {
    font-size: 0.65em;
    font-weight: 700;
    color: rgba(255,255,255,0.7);
    margin-left: 2px;
}

.rounds-bar {
    display: flex;
    height: clamp(64px, 10vw, 84px);
    overflow: visible;
    margin: 6px 0 10px;
    border-radius: 14px;
    gap: 10px;
    padding: 10px;
    background: rgba(0,0,0,0.18);
    border: 1px solid rgba(255,255,255,0.10);
}

.round-block {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    font-weight: 600;
    color: white;
    border-radius: 12px;
    border: 1px solid rgba(255,255,255,0.16);
    box-shadow: 0 10px 22px rgba(0,0,0,0.35);
    position: relative;
    overflow: hidden;
    transition: transform 0.12s ease, box-shadow 0.12s ease, filter 0.12s ease;
}

.round-block:hover {
    transform: translateY(-2px);
    filter: brightness(1.03);
    box-shadow: 0 16px 32px rgba(0,0,0,0.45);
}

.round-block::after {
    content: "";
    position: absolute;
    inset: 0;
    background: linear-gradient(180deg, rgba(255,255,255,0.18), transparent 55%);
    opacity: 0.35;
    pointer-events: none;
}

.action-buttons {
    display: flex;
    gap: 16px;
    margin-top: 2px;
}

.action-buttons .btn {
    flex: 1;
    font-size: 16px;
    padding: 16px;
    border-radius: 999px;
}

/* Desktop Responsiveness */
@media (min-width: 768px) {
    :root {
        --slider-thickness: 84px;
    }
    #stage-result.active {
        flex-direction: row;
    }
    .split-top, .split-bottom {
        height: 100%;
        width: 50%;
    }
    .start-content h1 {
        font-size: 80px;
    }
    .timer-large, .timer-small {
        font-size: 180px;
    }
    .score-large {
        font-size: 120px;
    }
    .final-content {
        max-width: 600px;
    }
    .difficulty-selector {
        max-width: 400px;
    }
    #btn-start {
        max-width: 400px;
    }
}

/* Small screens: keep spacing tight */
@media (max-width: 520px) {
    :root {
        --slider-thickness: 46px;
    }

    .header-row {
        padding: calc(18px + var(--safe-top)) calc(18px + var(--safe-right)) 18px calc(18px + var(--safe-left));
    }

    #stage-start {
        padding: 18px;
    }

    .start-content {
        padding: 22px 18px;
        gap: 24px;
        border-radius: 22px;
    }

    .start-content h1 {
        font-size: 44px;
    }

    .subtitle {
        font-size: 16px;
    }

    /* Make the sliders feel like a floating control rail */
    .sliders-container {
        left: calc(10px + var(--safe-left));
        top: calc(10px + var(--safe-top));
        height: calc(100% - 20px - var(--safe-top) - var(--safe-bottom));
        padding: 8px 8px;
        border-radius: 18px;
        background: rgba(0, 0, 0, 0.18);
        border: 1px solid rgba(255,255,255,0.10);
        backdrop-filter: blur(12px);
        -webkit-backdrop-filter: blur(12px);
        box-shadow: 0 16px 40px rgba(0,0,0,0.45);
    }

    .slider-wrapper {
        height: 100%;
    }

    .vertical-slider {
        height: 48px;
    }

    .final-content {
        padding: 22px 16px;
        border-radius: 22px;
    }

    #final-title {
        padding: 0 44px;
    }

    .rounds-bar {
        gap: 8px;
        padding: 8px;
        border-radius: 14px;
    }
}

@media (max-width: 380px) {
    :root {
        --slider-thickness: 42px;
    }
    .difficulty-selector {
        border-radius: 16px;
    }

    .diff-btn {
        font-size: 15px;
        padding: 12px 0;
    }
}

/* Tablet portrait: add breathing room */
@media (min-width: 600px) and (max-width: 1024px) and (orientation: portrait) {
    .start-content {
        max-width: 620px;
    }
    .difficulty-selector,
    #btn-start {
        max-width: 520px;
    }
}

/* Landscape phones: keep controls reachable */
@media (max-height: 520px) and (orientation: landscape) {
    .bottom-right {
        bottom: calc(14px + var(--safe-bottom));
        right: calc(14px + var(--safe-right));
    }
    .header-row {
        padding: calc(12px + var(--safe-top)) calc(14px + var(--safe-right)) 12px calc(14px + var(--safe-left));
    }
    .timer-large,
    .timer-small {
        font-size: clamp(64px, 12vh, 120px);
    }
}

/* Ultra-wide: prevent overly stretched content */
@media (min-width: 1200px) {
    .start-content {
        max-width: 680px;
    }
    .final-content {
        max-width: 760px;
    }
}

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

/* Animations */
@keyframes gradientShine {
    0% { background-position: 0% center; }
    100% { background-position: 200% center; }
}

@keyframes fadeInUp {
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes floating {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-10px); }
}

@keyframes stageFadeIn {
    0% { opacity: 0; transform: translateX(15px); }
    100% { opacity: 1; transform: translateX(0); }
}

@keyframes ninjaCutTop {
    0% { clip-path: polygon(0 0, 0 0, 0 0); opacity: 0; transform: translate(-10px, -10px); }
    100% { clip-path: polygon(0 0, 200% 0, 0 200%); opacity: 1; transform: translate(0, 0); }
}

@keyframes ninjaCutBottom {
    0% { clip-path: polygon(100% 100%, 100% 100%, 100% 100%); opacity: 0; transform: translate(10px, 10px); }
    100% { clip-path: polygon(100% 100%, -100% 100%, 100% -100%); opacity: 1; transform: translate(0, 0); }
}

.timer-alert {
    color: #ff3333 !important;
    animation: shake 0.4s infinite;
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-5px); }
    50% { transform: translateX(5px); }
    75% { transform: translateX(-5px); }
}

@keyframes popIn {
    0% { opacity: 0; transform: translateY(-6px) scale(0.96); }
    100% { opacity: 1; transform: translateY(0) scale(1); }
}
