/* ==========================================================================
   1. デザイントークン
   ========================================================================== */
:root {
    --bg: #f4f7f9;
    --surface: #ffffff;
    --surface-alt: #f9fafb;
    --border: #e6ebf0;
    --border-light: #f1f4f7;
    --text: #1f2937;
    --text-sub: #5b6b7a;
    --text-muted: #8a96a3;

    --accent: #3b82f6;
    --accent-rgb: 59, 130, 246;
    --accent-hover: #2563eb;
    --accent-soft: rgba(59, 130, 246, 0.10);

    --success: #10b981;
    --danger: #ef4444;
    --warning: #f59e0b;
    --purple: #8b5cf6;

    --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
    --shadow-md: 0 6px 16px rgba(15, 23, 42, 0.06);
    --shadow-lg: 0 16px 32px rgba(15, 23, 42, 0.10);

    --radius: 14px;
    --radius-sm: 8px;
    --transition: 0.18s ease;

    --font-base: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Hiragino Kaku Gothic ProN', 'Hiragino Sans', Meiryo, sans-serif;
    --font-mono: 'SFMono-Regular', Consolas, 'Courier New', monospace;

    --background-color: #1a2029;
    --go-color: #e74c3c;
    --target-size: 60px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f1419;
        --surface: #1a2029;
        --surface-alt: #131820;
        --border: #2a3441;
        --border-light: #232b36;
        --text: #e6edf3;
        --text-sub: #a8b3bd;
        --text-muted: #6c7986;
        --accent-soft: rgba(59, 130, 246, 0.18);
        --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
        --shadow-md: 0 6px 16px rgba(0, 0, 0, 0.35);
        --shadow-lg: 0 16px 32px rgba(0, 0, 0, 0.45);
    }
}

*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
    font-family: var(--font-base);
    background: var(--bg);
    color: var(--text);
    line-height: 1.65;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}
.hidden { display: none !important; }

/* ==========================================================================
   2. ヒーロー
   ========================================================================== */
.hero {
    background: linear-gradient(135deg, rgba(59, 130, 246, 0.08), rgba(139, 92, 246, 0.08));
    border-bottom: 1px solid var(--border);
    padding: 32px 24px 28px;
}
.hero-inner { max-width: 880px; margin: 0 auto; }
.breadcrumb { font-size: 0.85rem; margin-bottom: 12px; color: var(--text-muted); }
.breadcrumb a { color: var(--text-sub); text-decoration: none; transition: color var(--transition); }
.breadcrumb a:hover { color: var(--accent); }
.breadcrumb .sep { margin: 0 6px; color: var(--text-muted); }
.hero h1 { margin: 0 0 8px; font-size: 1.85rem; font-weight: 800; letter-spacing: -0.02em; color: var(--text); }
.hero-desc { margin: 0; color: var(--text-sub); font-size: 0.95rem; }

/* ==========================================================================
   3. アプリコンテナ
   ========================================================================== */
#app-container {
    flex-grow: 1;
    width: 100%;
    max-width: 880px;
    margin: 28px auto;
    padding: 0 20px;
    display: flex;
    flex-direction: column;
    gap: 20px;
    text-align: center;
}
#message-area {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 22px 24px;
    box-shadow: var(--shadow-sm);
}
#message-area p { margin: 0; color: var(--text-sub); font-size: 1rem; }
#message-area p:not(:last-child) { margin-bottom: 6px; }
#color-name-display { color: var(--go-color); font-weight: 700; }
.sub-instruction { font-size: 0.88rem !important; color: var(--text-muted) !important; margin-top: 8px !important; }

/* ==========================================================================
   4. プレイエリア
   ========================================================================== */
#play-area {
    position: relative;
    width: 100%;
    height: 520px;
    max-height: 70vh;
    border-radius: var(--radius);
    background: var(--background-color);
    border: 4px solid transparent;
    overflow: hidden;
    cursor: crosshair;
    user-select: none;
    box-shadow: var(--shadow-md);
    transition: border-color 0.2s, box-shadow var(--transition);
    background-image:
        linear-gradient(rgba(255, 255, 255, 0.03) 1px, transparent 1px),
        linear-gradient(90deg, rgba(255, 255, 255, 0.03) 1px, transparent 1px);
    background-size: 40px 40px;
}
#play-area:hover { box-shadow: var(--shadow-lg); }
#play-area.mistake { border-color: var(--danger); }
#play-area.shake { animation: shake 0.4s ease-in-out; }
@keyframes shake {
    0%, 100% { transform: translateX(0); }
    10%, 30%, 50%, 70%, 90% { transform: translateX(-6px); }
    20%, 40%, 60%, 80% { transform: translateX(6px); }
}

#start-prompt {
    position: absolute;
    top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    color: #fff;
    font-size: 1.4rem;
    font-weight: 700;
    padding: 14px 32px;
    background: rgba(59, 130, 246, 0.85);
    border-radius: 999px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
    letter-spacing: 0.02em;
    pointer-events: none;
    animation: prompt-pulse 1.4s ease-in-out infinite;
}
@keyframes prompt-pulse {
    0%, 100% { transform: translate(-50%, -50%) scale(1); }
    50% { transform: translate(-50%, -50%) scale(1.04); }
}

#target {
    position: absolute;
    width: var(--target-size);
    height: var(--target-size);
    border-radius: 50%;
    background: var(--go-color);
    box-shadow:
        0 0 0 3px rgba(255, 255, 255, 0.4),
        0 0 24px rgba(231, 76, 60, 0.6),
        0 8px 28px rgba(0, 0, 0, 0.4);
    cursor: crosshair;
    animation: target-spawn 0.18s ease-out;
    will-change: transform;
}
#target::after {
    content: "";
    position: absolute;
    inset: 25%;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.55);
}
@keyframes target-spawn {
    0%   { transform: scale(0.4); opacity: 0; }
    100% { transform: scale(1);   opacity: 1; }
}

#hit-marker {
    position: absolute;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    pointer-events: none;
    transform: translate(-50%, -50%);
    animation: hit-marker-pop 0.6s ease-out forwards;
    z-index: 5;
}
#hit-marker.hit  { background: rgba(16, 185, 129, 0.6);  box-shadow: 0 0 18px rgba(16, 185, 129, 0.8); }
#hit-marker.miss { background: rgba(239, 68, 68, 0.6);  box-shadow: 0 0 18px rgba(239, 68, 68, 0.8); }
@keyframes hit-marker-pop {
    0%   { transform: translate(-50%, -50%) scale(0.4); opacity: 1; }
    100% { transform: translate(-50%, -50%) scale(2.4); opacity: 0; }
}

#play-area .hud {
    position: absolute;
    top: 12px;
    left: 16px;
    color: rgba(255, 255, 255, 0.7);
    font-family: var(--font-mono);
    font-size: 0.85rem;
    pointer-events: none;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
}
#play-area .hud span { color: #fff; font-weight: 700; }

/* ==========================================================================
   5. 結果エリア
   ========================================================================== */
#result-area {
    background: linear-gradient(180deg, var(--surface) 0%, var(--surface-alt) 100%);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 28px 24px 24px;
    box-shadow: var(--shadow-lg);
    display: flex;
    flex-direction: column;
    gap: 18px;
    position: relative;
    overflow: hidden;
    animation: result-card-in 0.6s cubic-bezier(0.16, 1, 0.3, 1) both;
}
#result-area::before {
    content: "";
    position: absolute;
    inset: 0 0 auto 0;
    height: 4px;
    background: linear-gradient(90deg, var(--accent), var(--purple), var(--warning));
}
@keyframes result-card-in {
    0%   { opacity: 0; transform: translateY(20px) scale(0.96); }
    100% { opacity: 1; transform: translateY(0) scale(1); }
}

#rank-display {
    text-align: center;
    padding: 14px 16px;
    background: var(--surface);
    border: 1px dashed var(--accent);
    border-radius: var(--radius-sm);
}
#rank-display .rank-title {
    margin: 0;
    font-size: 0.78rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    font-weight: 600;
}
#rank-display .rank-name {
    margin: 8px 0 4px;
    font-size: 2.2rem;
    font-weight: 800;
    color: var(--accent);
    letter-spacing: -0.01em;
    line-height: 1.1;
    animation: rank-bounce 0.7s cubic-bezier(0.34, 1.56, 0.64, 1) 0.2s both;
}
#rank-display .rank-comment { margin: 0; font-size: 0.95rem; color: var(--text-sub); }
@keyframes rank-bounce {
    0%   { opacity: 0; transform: scale(0.4); }
    60%  { transform: scale(1.15); }
    100% { opacity: 1; transform: scale(1); }
}

#stat-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
}
.stat-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 14px 10px;
    background: var(--surface);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-sm);
    transition: transform var(--transition);
}
.stat-item:hover { transform: translateY(-2px); }
.stat-item .stat-label {
    font-size: 0.72rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    font-weight: 600;
}
.stat-item .stat-value {
    font-size: 1.35rem;
    font-weight: 800;
    color: var(--text);
    font-variant-numeric: tabular-nums;
    line-height: 1.1;
}
.stat-item:nth-child(1) .stat-value { color: var(--accent); }
.stat-item:nth-child(2) .stat-value { color: var(--success); }
.stat-item:nth-child(3) .stat-value { color: var(--purple); }
.stat-item:nth-child(4) .stat-value { color: var(--warning); }

#best-record-display:not(:empty) {
    padding: 12px 16px;
    background: linear-gradient(135deg, rgba(245, 158, 11, 0.10), rgba(139, 92, 246, 0.08));
    border: 1px solid rgba(245, 158, 11, 0.4);
    border-radius: var(--radius-sm);
    font-size: 0.88rem;
    color: var(--text-sub);
    text-align: center;
    line-height: 1.55;
}
#best-record-display strong { font-size: 1.1rem; color: var(--text); font-variant-numeric: tabular-nums; }
#best-record-display .best-meta { font-size: 0.78rem; color: var(--text-muted); margin-left: 4px; }
.new-record-badge {
    display: inline-block;
    margin-left: 6px;
    padding: 2px 9px;
    font-size: 0.7rem;
    font-weight: 700;
    color: #fff;
    background: linear-gradient(45deg, var(--danger), var(--warning));
    border-radius: 999px;
    letter-spacing: 0.1em;
    animation: badge-glow 1.4s ease-in-out infinite;
}
@keyframes badge-glow {
    0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.5); }
    50% { box-shadow: 0 0 0 6px rgba(239, 68, 68, 0); }
}

#chart-container {
    width: 100%;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 14px;
    position: relative;
}
#reaction-time-chart { width: 100% !important; height: auto !important; max-height: 320px; }
.chart-controls { display: flex; gap: 8px; justify-content: center; align-items: center; margin-top: 12px; }
.chart-controls button {
    font-weight: 700;
    width: 36px;
    height: 36px;
    padding: 0;
    border-radius: 50%;
    background: var(--surface);
    color: var(--text-sub);
    border: 1px solid var(--border);
    cursor: pointer;
    transition: all var(--transition);
}
.chart-controls button:hover {
    color: var(--accent);
    border-color: var(--accent);
    background: var(--accent-soft);
}
#reset-zoom-button {
    width: auto !important;
    padding: 0 16px !important;
    border-radius: 18px !important;
    font-size: 0.85rem !important;
}

#action-area { padding-top: 16px; border-top: 1px solid var(--border); }
.button-group {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}
.button-group #share-button { grid-column: 1 / -1; }
#share-button, #copy-button, #retry-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 12px 20px;
    border-radius: 999px;
    border: none;
    cursor: pointer;
    font-size: 0.95rem;
    font-weight: 600;
    font-family: inherit;
    transition: all var(--transition);
}
#share-button {
    background: linear-gradient(135deg, var(--accent), var(--purple));
    color: #fff;
    box-shadow: 0 6px 18px rgba(var(--accent-rgb), 0.3);
}
#share-button:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 24px rgba(var(--accent-rgb), 0.4);
    filter: brightness(1.05);
}
#copy-button {
    background: var(--surface);
    color: var(--text-sub);
    border: 1px solid var(--border);
}
#copy-button:hover { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }
#retry-button {
    background: var(--surface-alt);
    color: var(--text-sub);
    border: 1px solid var(--border);
}
#retry-button:hover { color: var(--text); background: var(--surface); border-color: var(--text-muted); }

/* ==========================================================================
   6. 設定パネル
   ========================================================================== */
#settings {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0 22px;
    box-shadow: var(--shadow-sm);
}
#settings summary {
    font-weight: 700;
    cursor: pointer;
    padding: 16px 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    list-style: none;
    color: var(--text);
    font-size: 0.95rem;
}
#settings summary::-webkit-details-marker { display: none; }
#settings[open] > #controls { padding: 18px 0 22px; border-top: 1px solid var(--border); }
.disclosure-arrow { transition: transform var(--transition); color: var(--text-muted); }
#settings[open] .disclosure-arrow { transform: rotate(90deg); color: var(--accent); }

.control-group { margin-bottom: 16px; text-align: left; }
.control-group:last-child { margin-bottom: 0; }
.control-group label { display: block; margin-bottom: 8px; font-size: 0.88rem; font-weight: 600; color: var(--text-sub); }
.control-group input[type="color"] {
    width: 60px; height: 40px; padding: 2px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    background: var(--surface);
}
.control-group input[type="range"] { width: 100%; accent-color: var(--accent); height: 30px; }
.control-group select {
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    font-size: 0.95rem;
    cursor: pointer;
    font-family: inherit;
}
.control-group select:focus { outline: none; border-color: var(--accent); }

#color-palettes { display: flex; gap: 8px; flex-wrap: wrap; }
.palette-button {
    flex: 1;
    min-width: 90px;
    background: var(--surface-alt);
    border: 1px solid var(--border);
    color: var(--text-sub);
    padding: 8px 14px;
    font-size: 0.88rem;
    font-weight: 500;
    border-radius: 999px;
    cursor: pointer;
    transition: all var(--transition);
    font-family: inherit;
}
.palette-button:hover {
    color: var(--accent);
    border-color: var(--accent);
    background: var(--accent-soft);
}

/* ==========================================================================
   7. フッター
   ========================================================================== */
footer {
    text-align: center;
    padding: 24px 20px;
    background: var(--surface);
    border-top: 1px solid var(--border);
    color: var(--text-muted);
    font-size: 0.85rem;
    width: 100%;
}
footer a { color: var(--text-sub); text-decoration: none; transition: color var(--transition); }
footer a:hover { color: var(--accent); }

/* ==========================================================================
   8. レスポンシブ
   ========================================================================== */
@media (max-width: 720px) {
    .hero { padding: 24px 18px 22px; }
    .hero h1 { font-size: 1.5rem; }
    .hero-desc { font-size: 0.88rem; }

    #app-container { padding: 0 14px; margin: 22px auto; gap: 16px; }
    #message-area { padding: 18px 18px; }
    #play-area { height: 60vh; min-height: 360px; }
    #start-prompt { font-size: 1.1rem; padding: 12px 22px; }
    #result-area { padding: 22px 18px 20px; }
    #rank-display .rank-name { font-size: 1.8rem; }

    #stat-grid { grid-template-columns: repeat(2, 1fr); }
    .stat-item .stat-value { font-size: 1.2rem; }

    .button-group { grid-template-columns: 1fr; }
    .button-group #share-button { grid-column: auto; }
    #settings { padding: 0 18px; }
}
