/* ========== Reset & Base ========== */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --bg: #0f0f1a;
    --surface: #1a1a2e;
    --surface-2: #25253e;
    --text: #e0e0e0;
    --text-muted: #8888aa;
    --accent: #00e676;
    --danger: #ff5252;
    --warning: #ffab40;
    --blue: #448aff;
    --radius: 16px;
    --radius-sm: 10px;
    --transition: 0.25s ease;
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans SC", sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
}

body {
    display: flex;
    justify-content: center;
    min-height: 100vh;
    min-height: 100dvh;
    padding: 16px;
}

/* Pseudo-elements center content vertically when it fits,
   but shrink to 0 when content overflows — so the top is
   always scrollable. */
body::before,
body::after {
    content: '';
    flex: 1;
    min-height: 0;
}

.container {
    width: 100%;
    max-width: 520px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

/* ========== Header ========== */
header {
    text-align: center;
}

header h1 {
    font-size: 2rem;
    font-weight: 700;
    letter-spacing: -0.5px;
    background: linear-gradient(135deg, #00e676, #00bcd4);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.subtitle {
    color: var(--text-muted);
    font-size: 0.95rem;
    margin-top: 4px;
}

/* ========== Test Area ========== */
.test-area {
    position: relative;
    width: 100%;
    aspect-ratio: 1 / 1;
    max-height: 420px;
    border-radius: var(--radius);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.15s ease, transform 0.10s ease, box-shadow var(--transition);
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
}

.test-area:active {
    transform: scale(0.985);
}

/* --- States --- */
.state-idle {
    background: var(--surface-2);
    border: 2px dashed #3a3a5a;
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.3);
}

.state-idle:hover {
    border-color: #5a5a8a;
    box-shadow: 0 4px 32px rgba(0, 0, 0, 0.45);
}

.state-waiting {
    background: #b71c1c;
    border: 2px solid #d32f2f;
    box-shadow: 0 4px 32px rgba(183, 28, 28, 0.35);
}

.state-ready {
    background: #1b5e20;
    border: 2px solid #00e676;
    box-shadow: 0 4px 40px rgba(0, 230, 118, 0.3);
    animation: pulse-ready 0.6s ease-out;
}

.state-done {
    background: var(--surface-2);
    border: 2px solid var(--blue);
    box-shadow: 0 4px 24px rgba(68, 138, 255, 0.25);
}

.state-early {
    background: #4a148c;
    border: 2px solid #e040fb;
    box-shadow: 0 4px 32px rgba(224, 64, 251, 0.3);
    animation: shake 0.4s ease-out;
}

@keyframes pulse-ready {
    0% { transform: scale(1); }
    40% { transform: scale(1.03); }
    100% { transform: scale(1); }
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    20% { transform: translateX(-6px); }
    40% { transform: translateX(6px); }
    60% { transform: translateX(-4px); }
    80% { transform: translateX(4px); }
}

.test-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    pointer-events: none;
    transition: opacity 0.2s ease;
}

#test-icon {
    font-size: 3.5rem;
    transition: transform 0.3s ease;
}

#test-text {
    font-size: 1.15rem;
    font-weight: 600;
    color: var(--text);
    text-align: center;
    line-height: 1.5;
}

.state-waiting .test-content #test-icon,
.state-waiting .test-content #test-text {
    color: rgba(255,255,255,0.9);
}

.state-ready .test-content #test-icon,
.state-ready .test-content #test-text {
    color: rgba(255,255,255,0.95);
}

/* ========== Result Panel ========== */
.result-panel {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.result-panel.hidden { display: none; }

.result-card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 20px 24px;
    text-align: center;
    border: 1px solid #2a2a45;
}

.result-label {
    font-size: 0.85rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 8px;
}

.result-value {
    font-size: 3.5rem;
    font-weight: 800;
    font-variant-numeric: tabular-nums;
    letter-spacing: -1px;
}

.result-grade {
    margin-top: 8px;
    font-size: 1rem;
    font-weight: 600;
}

/* --- Grade Colors --- */
.grade-god { color: #ffd700; }
.grade-amazing { color: #00e676; }
.grade-great { color: #00bcd4; }
.grade-good { color: #448aff; }
.grade-ok { color: #ffab40; }
.grade-slow { color: #ff5252; }

/* ========== Stats Grid ========== */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
}

.stat-item {
    background: var(--surface);
    border-radius: var(--radius-sm);
    padding: 14px 10px;
    text-align: center;
    border: 1px solid #2a2a45;
}

.stat-label {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 4px;
}

.stat-value {
    font-size: 1.15rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

/* ========== History Panel ========== */
.history-panel.hidden { display: none; }

.history-panel h2 {
    font-size: 1.1rem;
    margin-bottom: 12px;
    font-weight: 600;
}

.history-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-height: 300px;
    overflow-y: auto;
    padding-right: 4px;
}

.history-list::-webkit-scrollbar {
    width: 4px;
}

.history-list::-webkit-scrollbar-track {
    background: transparent;
}

.history-list::-webkit-scrollbar-thumb {
    background: #3a3a5a;
    border-radius: 2px;
}

.history-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--surface);
    border-radius: var(--radius-sm);
    padding: 12px 16px;
    border: 1px solid #2a2a45;
    animation: slide-in 0.3s ease-out;
}

@keyframes slide-in {
    from { opacity: 0; transform: translateY(-8px); }
    to { opacity: 1; transform: translateY(0); }
}

.history-item .h-rank {
    font-weight: 700;
    font-size: 0.85rem;
    color: var(--text-muted);
    min-width: 28px;
}

.history-item .h-time {
    font-weight: 700;
    font-size: 1.1rem;
    font-variant-numeric: tabular-nums;
}

.history-item .h-grade {
    font-size: 0.8rem;
    font-weight: 600;
}

.history-item .h-date {
    font-size: 0.72rem;
    color: var(--text-muted);
}

.btn-clear {
    margin-top: 12px;
    padding: 8px 20px;
    background: transparent;
    border: 1px solid #3a3a5a;
    border-radius: 8px;
    color: var(--text-muted);
    font-size: 0.85rem;
    cursor: pointer;
    transition: all var(--transition);
}

.btn-clear:hover {
    border-color: var(--danger);
    color: var(--danger);
}

/* ========== Footer ========== */
footer {
    text-align: center;
    font-size: 0.8rem;
    color: var(--text-muted);
    line-height: 1.8;
}

.green-text { color: var(--accent); font-weight: 600; }
.tip { color: var(--warning); font-size: 0.78rem; }

/* ========== Toast ========== */
.toast {
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--surface-2);
    border: 1px solid #3a3a5a;
    border-radius: 12px;
    padding: 12px 24px;
    font-size: 0.95rem;
    font-weight: 600;
    box-shadow: 0 8px 32px rgba(0,0,0,0.5);
    z-index: 1000;
    transition: opacity 0.3s ease, transform 0.3s ease;
}

.toast.hidden {
    opacity: 0;
    transform: translateX(-50%) translateY(-20px);
    pointer-events: none;
}

.toast:not(.hidden) {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* ========== Back-to-top button ========== */
.btn-back-to-top {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--surface-2);
    border: 1px solid #3a3a5a;
    color: var(--text);
    font-size: 1.2rem;
    cursor: pointer;
    z-index: 999;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transform: translateY(16px);
    pointer-events: none;
    transition: opacity 0.3s ease, transform 0.3s ease, background var(--transition);
    box-shadow: 0 4px 16px rgba(0,0,0,0.4);
}

.btn-back-to-top.visible {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}

.btn-back-to-top:hover {
    background: var(--surface);
    border-color: #5a5a8a;
}

.btn-back-to-top:active {
    transform: translateY(0) scale(0.92);
}

/* ========== Responsive ========== */

/* Small phones */
@media (max-width: 380px) {
    .container {
        gap: 14px;
    }

    header h1 {
        font-size: 1.6rem;
    }

    .test-area {
        max-height: 320px;
    }

    #test-icon {
        font-size: 2.8rem;
    }

    #test-text {
        font-size: 1rem;
    }

    .result-value {
        font-size: 2.8rem;
    }

    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .stat-item:nth-child(5) {
        grid-column: span 2;
    }
}

/* Tablets */
@media (min-width: 600px) {
    .container {
        max-width: 560px;
    }

    .test-area {
        max-height: 460px;
    }

    .stats-grid {
        grid-template-columns: repeat(5, 1fr);
    }

    .stat-item:nth-child(5) {
        grid-column: auto;
    }
}

/* Desktop */
@media (min-width: 900px) {
    .container {
        max-width: 600px;
    }

    header h1 {
        font-size: 2.2rem;
    }

    .test-area {
        max-height: 480px;
    }

    #test-icon {
        font-size: 4rem;
    }
}

/* Landscape phones — reduce height */
@media (max-height: 500px) and (orientation: landscape) {
    body {
        align-items: flex-start;
        padding-top: 10px;
    }

    .container {
        gap: 10px;
    }

    .test-area {
        max-height: 220px;
    }

    #test-icon {
        font-size: 2.2rem;
    }

    #test-text {
        font-size: 0.9rem;
    }

    .result-panel {
        gap: 8px;
    }

    .result-value {
        font-size: 2rem;
    }

    footer {
        font-size: 0.7rem;
    }
}

/* Reduced motion preference */
@media (prefers-reduced-motion: reduce) {
    .test-area {
        transition: background-color 0.1s ease;
    }

    .state-ready {
        animation: none;
    }

    .state-early {
        animation: none;
    }

    .toast {
        transition: none;
    }
}
