/* Design Tokens */
:root {
    /* Primary Colors - Vibrant Indigo */
    --color-primary: #6366f1;
    --color-primary-dark: #4f46e5;
    --color-primary-light: #818cf8;
    --color-primary-glow: rgba(99, 102, 241, 0.3);
    
    /* Secondary/Accent - Warm Amber */
    --color-accent: #f59e0b;
    --color-accent-dark: #d97706;
    --color-accent-light: #fbbf24;
    
    /* Status Colors */
    --color-success: #10b981;
    --color-success-light: #34d399;
    --color-error: #ef4444;
    --color-warning: #f59e0b;
    
    /* Neutrals */
    --color-bg: #f8fafc;
    --color-surface: #ffffff;
    --color-surface-hover: #fafbfc;
    --color-text: #1e293b;
    --color-text-muted: #64748b;
    --color-border: #e2e8f0;
    --color-border-hover: #cbd5e1;
    
    /* Gradients */
    --gradient-primary: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
    --gradient-success: linear-gradient(135deg, #10b981 0%, #059669 100%);
    --gradient-warm: linear-gradient(135deg, #f59e0b 0%, #ef4444 100%);
    --gradient-accent: linear-gradient(135deg, #f59e0b 0%, #fb923c 100%);
    
    /* Shadows */
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.08);
    --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.12);
    --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.16);
    --shadow-xl: 0 12px 48px rgba(0, 0, 0, 0.2);
    --shadow-colored: 0 8px 24px var(--color-primary-glow);
    
    /* Spacing */
    --space-xs: 4px;
    --space-sm: 8px;
    --space-md: 16px;
    --space-lg: 24px;
    --space-xl: 32px;
    --space-2xl: 48px;
    --space-3xl: 64px;
    
    /* Border Radius */
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 24px;
    --radius-full: 9999px;
    
    /* Typography */
    --font-display: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    
    /* Motion */
    --easing-standard: cubic-bezier(0.4, 0, 0.2, 1);
    --easing-bounce: cubic-bezier(0.68, -0.55, 0.265, 1.55);
    --duration-fast: 0.15s;
    --duration-med: 0.25s;
    --duration-slow: 0.4s;
}

/* ============================================================================
   Global Styles
   ============================================================================ */

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: var(--font-display);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    background: var(--color-bg);
    margin: 0;
    color: var(--color-text);
    line-height: 1.6;
}

/* ============================================================================
   Top Navigation Bar
   ============================================================================ */

.topbar {
    background: var(--gradient-primary);
    color: #ffffff;
    padding: var(--space-md) clamp(16px, 3vw, 24px);
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    box-shadow: var(--shadow-lg);
    position: sticky;
    top: 0;
    z-index: 100;
}

.brand {
    font-weight: 800;
    letter-spacing: -0.5px;
    font-size: 28px;
    text-align: center;
}

.left-side,
.right-side {
    display: flex;
    gap: var(--space-md);
    align-items: center;
}

.right-side {
    justify-content: flex-end;
}

/* Navigation chip for '한글' */
.nav-chip {
    background: var(--color-surface);
    color: var(--color-primary);
    border: 2px solid rgba(255, 255, 255, 0.3);
    padding: var(--space-sm) var(--space-lg);
    border-radius: var(--radius-full);
    cursor: pointer;
    text-decoration: none;
    font-size: 18px;
    font-weight: 700;
    transition: all var(--duration-med) var(--easing-standard);
}

.nav-chip:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
    border-color: rgba(255, 255, 255, 0.6);
}

.nav-chip:focus-visible {
    outline: 3px solid rgba(255, 255, 255, 0.8);
    outline-offset: 2px;
}

/* Login button */
.login-btn {
    background: rgba(255, 255, 255, 0.2);
    backdrop-filter: blur(10px);
    color: #ffffff;
    border: 2px solid rgba(255, 255, 255, 0.3);
    padding: var(--space-sm) var(--space-lg);
    border-radius: var(--radius-full);
    font-weight: 600;
    font-size: 16px;
    cursor: pointer;
    transition: all var(--duration-med) var(--easing-standard);
}

.login-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    border-color: rgba(255, 255, 255, 0.5);
    transform: translateY(-2px);
}

.login-btn:focus-visible {
    outline: 3px solid rgba(255, 255, 255, 0.8);
    outline-offset: 2px;
}

/* ============================================================================
   Content Area
   ============================================================================ */

.content {
    padding: var(--space-xl) clamp(16px, 4vw, 28px);
    max-width: 1200px;
    margin: 0 auto;
}

/* ============================================================================
   Modal & Overlay
   ============================================================================ */

.overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    animation: fade-in var(--duration-slow) var(--easing-standard) both;
    backdrop-filter: blur(4px);
}

.modal {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: var(--color-surface);
    border: 3px solid var(--color-primary-light);
    border-radius: var(--radius-xl);
    padding: var(--space-xl);
    width: 400px;
    max-width: calc(100% - 32px);
    z-index: 1001;
    box-shadow: var(--shadow-xl);
    animation: pop-in var(--duration-slow) var(--easing-bounce) both;
}

.modal-close {
    position: absolute;
    top: var(--space-md);
    right: var(--space-md);
    background: transparent;
    border: none;
    color: var(--color-text-muted);
    font-size: 24px;
    cursor: pointer;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    transition: all var(--duration-fast);
}

.modal-close:hover {
    background: var(--color-bg);
    color: var(--color-text);
}

.modal-title {
    font-weight: 700;
    font-size: 1.5rem;
    margin-bottom: var(--space-lg);
    color: var(--color-text);
    padding-right: 32px;
}

.calibration-prompt {
    text-align: center;
    font-size: 1.25rem;
    font-weight: 700;
    margin-bottom: var(--space-lg);
    color: var(--color-text);
}

#calibrationModal .modal-title {
    text-align: center;
}

.modal-body {
    display: flex;
    flex-direction: column;
    gap: var(--space-md);
}

.modal-body label {
    display: block;
    margin-bottom: var(--space-xs);
    font-weight: 600;
    color: var(--color-text);
}

.input {
    width: 100%;
    padding: var(--space-md);
    border: 2px solid var(--color-border);
    border-radius: var(--radius-md);
    background: var(--color-surface);
    font-size: 1rem;
    transition: all var(--duration-fast);
}

.input:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px var(--color-primary-glow);
}

.modal-actions {
    display: flex;
    gap: var(--space-md);
    margin-top: var(--space-md);
}

.modal-btn {
    flex: 1;
    background: var(--gradient-primary);
    color: #fff;
    border: none;
    padding: var(--space-md) var(--space-lg);
    border-radius: var(--radius-md);
    cursor: pointer;
    font-weight: 700;
    font-size: 1rem;
    transition: all var(--duration-med) var(--easing-standard);
    box-shadow: var(--shadow-sm);
}

.modal-btn:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-colored);
}

.modal-btn.secondary {
    background: var(--color-surface);
    color: var(--color-primary);
    border: 2px solid var(--color-primary);
    box-shadow: none;
}

.modal-btn.secondary:hover {
    background: var(--color-bg);
    box-shadow: var(--shadow-sm);
}

/* Calibration modal - no close button */
#calibrationModal .modal-close {
    display: none;
}

#calibrationOverlay {
    cursor: default;
}

#calibrationModal .modal-body {
    align-items: center;
}

/* ============================================================================
   Sound Groups & Grid
   ============================================================================ */

.group {
    margin: var(--space-2xl) 0;
}

.group-title {
    font-weight: 700;
    font-size: 1.5rem;
    margin-bottom: var(--space-lg);
    color: var(--color-text);
}

.button-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
    gap: var(--space-md);
}

/* Hangul sound buttons */
.hangul-btn {
    aspect-ratio: 1;
    background: var(--color-surface);
    border: 3px solid var(--color-border);
    border-radius: var(--radius-lg);
    padding: var(--space-md) 0;
    cursor: pointer;
    font-size: 2rem;
    font-weight: 700;
    text-align: center;
    text-decoration: none;
    color: var(--color-text);
    box-shadow: var(--shadow-sm);
    transition: all var(--duration-med) var(--easing-standard);
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

.hangul-btn[style*="--progress"] {
    /* New purple border */
    border-color: #A78BFA; /* Tailwind's violet-400 or a similar shade */
    /* Maintain the original text-shadow for better contrast against the fill */
    text-shadow: 0 1px 3px rgba(255, 255, 255, 0.8),
                 0 0 8px rgba(255, 255, 255, 0.6);
}

/* Progress indicator - fills from bottom */
.hangul-btn::before {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: var(--progress, 0%);
    background: linear-gradient(135deg, rgba(166, 83, 255, 0.7) 0%, rgba(255, 102, 219, 0.8) 100%);
    transition: height var(--duration-slow) var(--easing-standard);
    z-index: 0;
}

.hangul-btn {
    position: relative;
    z-index: 1;
}

.hangul-btn[style*="--progress"] {
    text-shadow: 0 1px 3px rgba(255, 255, 255, 0.8),
                 0 0 8px rgba(255, 255, 255, 0.6);
}

.hangul-btn span,
.hangul-btn::after {
    position: relative;
    z-index: 1;
}

.hangul-btn:hover {
    transform: translateY(-8px) scale(1.02);
    box-shadow: var(--shadow-lg);
    border-color: var(--color-primary-light);
}

.hangul-btn:focus-visible {
    outline: 3px solid var(--color-primary-glow);
    outline-offset: 2px;
}

/* 100% completion styling */
.hangul-btn.complete {
    background: var(--gradient-success);
    color: #ffffff;
    border-color: var(--color-success);
    animation: celebration var(--duration-slow) var(--easing-bounce);
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.hangul-btn.complete::before {
    display: none;
}

@keyframes celebration {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.05); }
}

/* ============================================================================
   Sound Practice Page
   ============================================================================ */

/* Description box */
.desc-box {
    background: var(--color-surface);
    border: 3px solid var(--color-primary-light);
    border-radius: var(--radius-xl);
    padding: var(--space-xl);
    margin-bottom: var(--space-xl);
    box-shadow: var(--shadow-md);
}

.desc-title {
    font-weight: 700;
    font-size: 1.5rem;
    margin-bottom: var(--space-md);
    text-align: center;
    color: var(--color-text);
}

.desc-title #soundSymbol {
    display: block;
    font-size: 4rem;
    line-height: 1.1;
    margin-top: var(--space-sm);
    background: var(--gradient-primary);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.desc-text {
    font-size: 1.1rem;
    text-align: center;
    color: var(--color-text-muted);
    line-height: 1.7;
}

/* Record button */
.record-btn {
    background: var(--gradient-primary);
    color: #ffffff;
    border: none;
    width: 160px;
    height: 160px;
    border-radius: 50%;
    cursor: pointer;
    box-shadow: var(--shadow-colored);
    transition: all var(--duration-med) var(--easing-standard);
    display: block;
    margin: var(--space-2xl) auto;
    font-size: 1.25rem;
    font-weight: 700;
    letter-spacing: 0.5px;
}

.record-btn:hover {
    transform: scale(1.05);
    box-shadow: 0 12px 40px var(--color-primary-glow);
}

.record-btn:focus-visible {
    outline: 4px solid var(--color-primary-glow);
    outline-offset: 4px;
}

.record-btn.recording {
    animation: pulse-record 1.5s infinite;
    background: var(--gradient-warm);
}

@keyframes pulse-record {
    0%, 100% {
        box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.7);
    }
    50% {
        box-shadow: 0 0 0 30px rgba(239, 68, 68, 0);
    }
}

/* Analysis status and feedback */
.analysis-status {
    text-align: center;
    margin: var(--space-6) auto;
    padding: var(--space-4) var(--space-6);
    background: var(--color-surface-tint);
    border-radius: var(--radius-md);
    font-weight: 500;
    max-width: 600px;
    width: auto;
    display: block;
    animation: statusPulse var(--duration-med) var(--easing-standard);
}

#analysisStatus {
    text-align: center !important;
    margin: var(--space-6) auto !important;
    padding: var(--space-4) var(--space-6);
    background: var(--color-surface-tint);
    border-radius: var(--radius-md);
    font-weight: 500;
    max-width: 600px;
    width: auto !important;
    display: block;
    margin-left: auto !important;
    margin-right: auto !important;
}

@keyframes statusPulse {
    0%, 100% {
        opacity: 1;
    }
    50% {
        opacity: 0.7;
    }
}

#analysisStatus:empty,
.analysis-status:empty {
    display: none;
}

.analysis-feedback {
    margin: var(--space-xl) auto;
    text-align: left;
    color: var(--color-text);
    font-weight: 500;
    background: var(--color-surface);
    border: 2px solid var(--color-border);
    border-radius: var(--radius-lg);
    padding: var(--space-lg);
    box-shadow: var(--shadow-sm);
    max-width: 680px;
}

.analysis-feedback .feedback-list {
    list-style: none;
    margin: 0;
    padding: 0;
}

.analysis-feedback .feedback-list li {
    margin: var(--space-md) 0;
    padding-left: var(--space-xl);
    position: relative;
}

.analysis-feedback .feedback-list li::before {
    content: '✓';
    position: absolute;
    left: 0;
    color: var(--color-success);
    font-weight: 700;
    font-size: 1.2rem;
}

/* Hide unused record-status */
.record-status {
    display: none;
}

/* ============================================================================
   Analysis Cards
   ============================================================================ */

.cards {
    display: grid;
    grid-template-columns: repeat(5, minmax(120px, 1fr));
    grid-auto-rows: minmax(140px, auto);
    justify-content: center;
    gap: var(--space-md);
    max-width: 800px;
    margin: var(--space-2xl) auto;
}

.card {
    background: var(--color-surface);
    border: 2px solid var(--color-border);
    border-radius: var(--radius-lg);
    padding: var(--space-md);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-sm);
    transition: all var(--duration-med) var(--easing-standard);
    animation: fade-in-up var(--duration-slow) var(--easing-standard) both;
}

.card:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-md);
    border-color: var(--color-primary-light);
}

.img-ph {
    width: 56px;
    height: 56px;
    background: var(--gradient-primary);
    border-radius: var(--radius-md);
    margin-bottom: var(--space-md);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.75rem;
}

.card-title {
    font-size: 0.85rem;
    font-weight: 700;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    width: 100%;
    text-align: center;
    color: var(--color-text);
    margin-bottom: var(--space-sm);
}

.card-body {
    font-size: 0.9rem;
    text-align: center;
    color: var(--color-text-muted);
    line-height: 1.4;
}

/* Total score card - positioned in bottom middle */
.card.total {
    grid-column: 3;
    grid-row: 2;
    border: 3px solid var(--color-primary);
    background: linear-gradient(135deg, rgba(99, 102, 241, 0.05) 0%, rgba(139, 92, 246, 0.05) 100%);
}

.card.total .percent {
    font-weight: 900;
    font-size: 2.5rem;
    background: var(--gradient-primary);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    animation: score-appear var(--duration-slow) var(--easing-bounce);
}

.card.total .card-title {
    font-size: 1rem;
    margin-top: var(--space-sm);
}

@keyframes score-appear {
    from {
        opacity: 0;
        transform: scale(0.5);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

/* ============================================================================
   Analysis Plot - Dual Canvas Layout
   ============================================================================ */

.analysis-plot-container {
    margin: var(--space-2xl) auto;
    max-width: 900px;
    text-align: center;
}

.dual-plot-wrapper {
    display: flex;
    gap: var(--space-md);
    justify-content: center;
    flex-wrap: wrap;
}

.plot-panel {
    background: var(--color-surface);
    border: 2px solid var(--color-border);
    border-radius: var(--radius-lg);
    padding: var(--space-sm);
    box-shadow: var(--shadow-md);
    transition: all var(--duration-med) var(--easing-standard);
}

.plot-panel:hover {
    border-color: var(--color-primary-light);
    box-shadow: var(--shadow-lg);
}

.plot-panel canvas {
    display: block;
    border-radius: var(--radius-sm);
}

.analysis-plot-caption {
    font-size: 1rem;
    color: var(--color-text-muted);
    font-weight: 500;
    margin-top: var(--space-md);
}

/* Legacy single plot support */
.analysis-plot {
    margin: var(--space-2xl) auto;
    max-width: 600px;
    text-align: center;
}

.analysis-plot img {
    width: 100%;
    border-radius: var(--radius-lg);
    border: 2px solid var(--color-border);
    box-shadow: var(--shadow-md);
    margin-bottom: var(--space-md);
}

/* Responsive: Stack vertically on narrow screens */
@media (max-width: 900px) {
    .dual-plot-wrapper {
        flex-direction: column;
        align-items: center;
    }

    .plot-panel {
        width: 100%;
        max-width: 450px;
    }

    .plot-panel canvas {
        width: 100%;
        height: auto;
    }
}

/* ============================================================================
   Banners
   ============================================================================ */

.first-visit-banner {
    position: fixed;
    left: 50%;
    bottom: var(--space-lg);
    transform: translateX(-50%);
    width: min(860px, calc(100% - 32px));
    background: var(--color-surface);
    color: var(--color-text);
    border: 3px solid var(--color-primary-light);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-xl);
    padding: var(--space-lg) var(--space-xl);
    z-index: 1200;
    animation: slide-up var(--duration-slow) var(--easing-bounce) both;
}

.first-visit-banner .banner-text {
    margin-right: 32px;
    font-size: 1rem;
    line-height: 1.6;
}

#userCreatedBanner {
    width: auto;
    min-width: 240px;
    padding: var(--space-md) var(--space-xl);
}

#userCreatedBanner .banner-text {
    margin-right: 0;
    text-align: center;
    font-weight: 600;
    font-size: 1.1rem;
}

.banner-close {
    position: absolute;
    top: var(--space-md);
    right: var(--space-md);
    background: transparent;
    border: none;
    color: var(--color-text-muted);
    font-size: 20px;
    cursor: pointer;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    transition: all var(--duration-fast);
}

.banner-close:hover {
    background: var(--color-bg);
    color: var(--color-text);
}

/* ============================================================================
   Utility Classes
   ============================================================================ */

.note {
    font-size: 0.9rem;
    color: var(--color-text-muted);
}

/* ============================================================================
   Animations
   ============================================================================ */

@keyframes fade-in {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@keyframes fade-in-up {
    from {
        opacity: 0;
        transform: translateY(16px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes slide-up {
    from {
        opacity: 0;
        transform: translate(-50%, 20px);
    }
    to {
        opacity: 1;
        transform: translate(-50%, 0);
    }
}

@keyframes pop-in {
    from {
        opacity: 0;
        transform: translate(-50%, -45%) scale(0.9);
    }
    to {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1);
    }
}

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

@media (max-width: 768px) {
    .topbar {
        grid-template-columns: auto 1fr auto;
        gap: var(--space-md);
    }
    
    .brand {
        font-size: 1.5rem;
    }
    
    .button-grid {
        grid-template-columns: repeat(auto-fill, minmax(70px, 1fr));
        gap: var(--space-sm);
    }
    
    .hangul-btn {
        font-size: 1.5rem;
    }

    .hangul-btn[style*="--progress"] {
        text-shadow: 0 1px 4px rgba(255, 255, 255, 0.9),
                     0 0 10px rgba(255, 255, 255, 0.7);
    }

    .analysis-status,
    #analysisStatus {
        margin: var(--space-4) auto;
        padding: var(--space-3) var(--space-4);
        font-size: 0.95rem;
    }
    
    .cards {
        grid-template-columns: repeat(3, 1fr);
        gap: var(--space-sm);
    }
    
    .card.total {
        grid-column: 2;
        grid-row: 2;
    }
    
    .modal {
        width: calc(100% - 32px);
        padding: var(--space-lg);
    }
    
    .record-btn {
        width: 140px;
        height: 140px;
        font-size: 1.1rem;
    }
}

@media (max-width: 480px) {

    .hangul-btn {
        font-size: 1.25rem;
    }

    .content {
        padding: var(--space-lg) var(--space-md);
    }
    
    .group-title {
        font-size: 1.25rem;
    }
    
    .cards {
        grid-template-columns: repeat(2, 1fr);
    }
    
    .card.total {
        grid-column: 1 / -1;
        grid-row: auto;
    }

    .analysis-status,
    #analysisStatus {
        margin: var(--space-3) var(--space-md);
        width: calc(100% - var(--space-lg));
    }
}

/* ============================================================================
   Accessibility
   ============================================================================ */

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* Focus visible for keyboard navigation */
*:focus-visible {
    outline: 3px solid var(--color-primary-glow);
    outline-offset: 2px;
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    .hangul-btn,
    .card,
    .modal {
        border-width: 3px;
    }
}