/* ===================================
   RESET & BASE STYLES
   =================================== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Noto Sans SC', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: #333;
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ===================================
   CONTAINER & LAYOUT
   =================================== */
.container {
    width: 100%;
    height: 450px;
    max-width: 100%;
    margin: 0 auto;
    background: #ffffff;
    display: flex;
    flex-direction: column;
    position: relative;
    overflow: hidden;
}

/* Detect if opened in new tab (not iframe) */
@media screen and (min-height: 600px) {
    body:not(.in-iframe) .container {
        height: 90vh;
        max-height: 90vh;
    }
}

/* ===================================
   TOP BAR
   =================================== */
.top-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 16px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    z-index: 10;
    min-height: 44px;
}

.progress-indicator {
    font-size: 14px;
    font-weight: 600;
    letter-spacing: 0.5px;
}

.settings-container {
    display: flex;
    gap: 8px;
}

.icon-btn {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    border-radius: 8px;
    padding: 8px;
    cursor: pointer;
    color: white;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 40px;
    min-height: 40px;
}

.icon-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: scale(1.05);
}

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

/* ===================================
   MODE INDICATOR
   =================================== */
.mode-indicator {
    background: #f0f4ff;
    padding: 6px 12px;
    text-align: center;
    font-size: 12px;
    font-weight: 600;
    color: #667eea;
    border-bottom: 2px solid #e0e7ff;
}

.mode-indicator.test-mode {
    background: #fff4e6;
    color: #f59e0b;
    border-bottom-color: #fed7aa;
}

/* ===================================
   CANVAS CONTAINER (Tian Zi Ge)
   =================================== */
.canvas-container {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    position: relative;
    background: #fafafa;
    min-height: 0;
}

.character-target {
    width: 100%;
    max-width: 320px;
    aspect-ratio: 1;
    position: relative;
    background: white;
    border: 3px solid #333;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
    border-radius: 8px;
}

/* Ensure HanziWriter SVG sits above the CSS grid overlay */
.character-target svg {
    position: relative;
    z-index: 1;
}

/* Tian Zi Ge Grid Lines */
.character-target::before,
.character-target::after {
    content: '';
    position: absolute;
    background: #e0e0e0;
    pointer-events: none; /* Must not intercept mouse/touch events destined for HanziWriter SVG */
    z-index: 0;
}

.character-target::before {
    width: 1px;
    height: 100%;
    left: 50%;
    top: 0;
    transform: translateX(-50%);
}

.character-target::after {
    width: 100%;
    height: 1px;
    top: 50%;
    left: 0;
    transform: translateY(-50%);
}

/* Diagonal lines for Tian Zi Ge */
.character-target {
    background-image: 
        linear-gradient(45deg, transparent 49.5%, #f0f0f0 49.5%, #f0f0f0 50.5%, transparent 50.5%),
        linear-gradient(-45deg, transparent 49.5%, #f0f0f0 49.5%, #f0f0f0 50.5%, transparent 50.5%);
}

/* ===================================
   COMPLETION MESSAGE
   =================================== */
.completion-message {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0);
    background: white;
    padding: 32px;
    border-radius: 16px;
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.2);
    text-align: center;
    z-index: 100;
    opacity: 0;
    transition: all 0.4s cubic-bezier(0.68, -0.55, 0.265, 1.55);
    max-width: 90%;
}

.completion-message.show {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
}

.message-content h2 {
    color: #10b981;
    font-size: 24px;
    margin: 12px 0 8px 0;
    font-weight: 700;
}

.message-content p {
    color: #666;
    font-size: 14px;
    line-height: 1.6;
}

.success-icon {
    color: #10b981;
    animation: checkmark 0.6s ease-in-out;
}

@keyframes checkmark {
    0% {
        transform: scale(0) rotate(-45deg);
        opacity: 0;
    }
    50% {
        transform: scale(1.2) rotate(10deg);
    }
    100% {
        transform: scale(1) rotate(0deg);
        opacity: 1;
    }
}

/* ===================================
   ACTION DOCK
   =================================== */
.action-dock {
    display: flex;
    gap: 8px;
    padding: 12px 16px;
    background: #f8f9fa;
    border-top: 2px solid #e9ecef;
    box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.05);
}

.action-btn {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 16px;
    border: none;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    color: white;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    min-height: 48px;
    white-space: nowrap;
}

.watch-btn {
    background: linear-gradient(135deg, #3b82f6, #2563eb);
}

.watch-btn:hover {
    background: linear-gradient(135deg, #2563eb, #1d4ed8);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(37, 99, 235, 0.4);
}

.reset-btn {
    background: linear-gradient(135deg, #ef4444, #dc2626);
}

.reset-btn:hover {
    background: linear-gradient(135deg, #dc2626, #b91c1c);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(220, 38, 38, 0.4);
}

.next-btn {
    background: linear-gradient(135deg, #10b981, #059669);
}

.next-btn:hover {
    background: linear-gradient(135deg, #059669, #047857);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(5, 150, 105, 0.4);
}

.action-btn:active {
    transform: translateY(0);
}

.action-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

/* ===================================
   FEEDBACK PANEL
   =================================== */
.feedback-panel {
    background: linear-gradient(135deg, #f0f9ff, #e0f2fe);
    padding: 12px 16px;
    border-top: 2px solid #bae6fd;
    opacity: 0;
    max-height: 0;
    overflow: hidden;
    transition: all 0.4s ease;
}

.feedback-panel.show {
    opacity: 1;
    max-height: 100px;
}

.feedback-content {
    display: flex;
    flex-direction: column;
    gap: 6px;
    align-items: center;
}

.pinyin {
    font-size: 18px;
    font-weight: 600;
    color: #0369a1;
    letter-spacing: 1px;
}

.english {
    font-size: 14px;
    color: #075985;
    font-weight: 500;
}

/* ===================================
   TOOLTIP
   =================================== */
.tooltip {
    position: fixed;
    background: rgba(0, 0, 0, 0.9);
    color: white;
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 12px;
    pointer-events: none;
    z-index: 1000;
    opacity: 0;
    transition: opacity 0.2s ease;
    max-width: 200px;
    text-align: center;
    white-space: nowrap;
}

.tooltip.show {
    opacity: 1;
}

/* ===================================
   ANIMATIONS
   =================================== */
@keyframes shake {
    0%, 100% { transform: translateX(0); }
    10%, 30%, 50%, 70%, 90% { transform: translateX(-4px); }
    20%, 40%, 60%, 80% { transform: translateX(4px); }
}

.shake {
    animation: shake 0.5s ease-in-out;
}

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

.pulse {
    animation: pulse 0.4s ease-in-out;
}

/* ===================================
   RESPONSIVE DESIGN
   =================================== */
@media (max-width: 480px) {
    .action-btn {
        font-size: 11px;
        padding: 10px 8px;
        gap: 4px;
    }
    
    .action-btn svg {
        width: 16px;
        height: 16px;
    }
    
    .character-target {
        max-width: 280px;
    }
    
    .top-bar {
        padding: 6px 12px;
    }
    
    .progress-indicator {
        font-size: 12px;
    }
}

/* ===================================
   ANALYTICS PANEL
   =================================== */
.analytics-panel {
    background: #1e1e2e;
    border-top: 2px solid #444;
    color: #cdd6f4;
    font-family: 'Courier New', Consolas, monospace;
    font-size: 11px;
    max-height: 160px;
    display: flex;
    flex-direction: column;
    transition: max-height 0.3s ease;
    flex-shrink: 0;
}

.analytics-panel.collapsed {
    max-height: 30px;
}

.analytics-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 4px 10px;
    background: #181825;
    border-bottom: 1px solid #444;
    min-height: 28px;
    flex-shrink: 0;
}

.analytics-title {
    font-size: 11px;
    font-weight: 700;
    color: #89b4fa;
    letter-spacing: 0.5px;
}

.analytics-controls {
    display: flex;
    gap: 4px;
    align-items: center;
}

.analytics-btn {
    background: #313244;
    border: 1px solid #555;
    color: #cdd6f4;
    border-radius: 4px;
    padding: 2px 8px;
    font-size: 10px;
    cursor: pointer;
    font-family: inherit;
    transition: background 0.2s;
}

.analytics-btn:hover {
    background: #45475a;
}

.analytics-toggle-btn {
    min-width: 24px;
    text-align: center;
    padding: 2px 6px;
}

.analytics-panel.collapsed .analytics-toggle-btn {
    transform: rotate(-90deg);
}

.analytics-body {
    overflow: hidden;
    flex: 1;
    display: flex;
    flex-direction: column;
}

.analytics-panel.collapsed .analytics-body {
    display: none;
}

.analytics-log {
    overflow-y: auto;
    flex: 1;
    padding: 4px 8px;
}

.analytics-log::-webkit-scrollbar {
    width: 4px;
}

.analytics-log::-webkit-scrollbar-track {
    background: #1e1e2e;
}

.analytics-log::-webkit-scrollbar-thumb {
    background: #45475a;
    border-radius: 2px;
}

.analytics-empty {
    color: #585b70;
    font-style: italic;
    padding: 6px 0;
}

.log-entry {
    display: flex;
    gap: 8px;
    padding: 2px 0;
    border-bottom: 1px solid #2a2a3e;
    line-height: 1.5;
    align-items: baseline;
}

.log-time {
    color: #6c7086;
    white-space: nowrap;
    flex-shrink: 0;
    min-width: 52px;
}

.log-icon {
    flex-shrink: 0;
    width: 16px;
    text-align: center;
}

.log-action {
    color: #cdd6f4;
    flex: 1;
    word-break: break-word;
}

.log-state {
    color: #a6e3a1;
    white-space: nowrap;
    flex-shrink: 0;
}

.log-entry.log-correct { border-left: 2px solid #a6e3a1; padding-left: 4px; }
.log-entry.log-mistake { border-left: 2px solid #f38ba8; padding-left: 4px; }
.log-entry.log-complete { border-left: 2px solid #fab387; padding-left: 4px; }
.log-entry.log-nav { border-left: 2px solid #89b4fa; padding-left: 4px; }
.log-entry.log-control { border-left: 2px solid #cba6f7; padding-left: 4px; }

/* ===================================
   ACCESSIBILITY
   =================================== */
button:focus-visible {
    outline: 3px solid #3b82f6;
    outline-offset: 2px;
}

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