/* ===================================
   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;
    width: 100%;
    /* Detect if in iframe or standalone */
    height: 450px;
    position: relative;
}

/* If opened in new tab, use 90vh */
body.standalone {
    height: 90vh;
}

/* ===================================
   STICKY HEADER
   =================================== */
.header {
    position: sticky;
    top: 0;
    background: rgba(255, 255, 255, 0.98);
    backdrop-filter: blur(10px);
    padding: 8px 16px;
    display: flex;
    justify-content: space-around;
    align-items: center;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    z-index: 100;
    border-bottom: 3px solid #667eea;
}

.stat-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
}

.stat-label {
    font-size: 11px;
    color: #666;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.stat-value {
    font-size: 18px;
    font-weight: 700;
    color: #667eea;
}

/* ===================================
   MAIN CONTAINER
   =================================== */
.main-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 12px 16px;
    gap: 12px;
    max-width: 600px;
    margin: 0 auto;
}

/* ===================================
   INFO BOX
   =================================== */
.info-box {
    background: white;
    border-radius: 12px;
    padding: 12px 20px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 12px;
    transition: transform 0.2s;
}

.info-box:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
}

.character-display {
    font-size: 48px;
    font-weight: 700;
    color: #333;
    min-width: 60px;
    text-align: center;
}

.pinyin-toggle,
.english-toggle {
    display: flex;
    align-items: center;
    gap: 6px;
}

.toggle-label {
    font-size: 12px;
    color: #666;
    font-weight: 600;
}

/* Toggle Switch */
.toggle-switch {
    position: relative;
    display: inline-block;
    width: 40px;
    height: 20px;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    transition: 0.3s;
    border-radius: 20px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 14px;
    width: 14px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: 0.3s;
    border-radius: 50%;
}

input:checked + .slider {
    background-color: #667eea;
}

input:checked + .slider:before {
    transform: translateX(20px);
}

.pinyin,
.english {
    font-size: 14px;
    color: #555;
    padding: 4px 12px;
    background: #f0f0f0;
    border-radius: 6px;
    transition: opacity 0.3s;
}

.pinyin {
    font-style: italic;
    font-weight: 600;
}

.pinyin.hidden,
.english.hidden {
    display: none;
}

/* ===================================
   CANVAS CONTAINER (TIAN ZI GE)
   =================================== */
.canvas-container {
    position: relative;
    width: 100%;
    max-width: 450px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}

.tian-zi-ge {
    width: 100%;
    aspect-ratio: 1;
    max-width: 450px;
    max-height: 450px;
    background: white;
    border: 3px solid #333;
    border-radius: 8px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
    position: relative;
    overflow: hidden;
    touch-action: none; /* Optimize for touch/stylus */
}

/* Shake animation for incorrect strokes */
@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-10px); }
    75% { transform: translateX(10px); }
}

.tian-zi-ge.shake {
    animation: shake 0.3s;
}

/* Flash red for incorrect */
@keyframes flashRed {
    0%, 100% { background-color: white; }
    50% { background-color: rgba(255, 0, 0, 0.2); }
}

.tian-zi-ge.flash-red {
    animation: flashRed 0.5s;
}

.canvas-hint {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.9);
    text-align: center;
    font-weight: 500;
}

/* ===================================
   CONTROL DOCK
   =================================== */
.control-dock {
    display: flex;
    gap: 8px;
    width: 100%;
    max-width: 450px;
    flex-wrap: wrap;
    justify-content: center;
}

.control-btn {
    flex: 1;
    min-width: 140px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 16px;
    background: white;
    border: 2px solid #667eea;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 600;
    color: #667eea;
    cursor: pointer;
    transition: all 0.2s;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    touch-action: manipulation; /* Optimize for touch */
}

.control-btn:hover {
    background: #667eea;
    color: white;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.control-btn:active {
    transform: translateY(0);
    box-shadow: 0 2px 6px rgba(102, 126, 234, 0.2);
}

.btn-icon {
    font-size: 16px;
}

.btn-text {
    white-space: nowrap;
}

/* ===================================
   SCORE OVERLAY
   =================================== */
.score-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    animation: fadeIn 0.3s;
}

.score-overlay.show {
    display: flex;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.score-card {
    background: white;
    border-radius: 16px;
    padding: 24px 32px;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3);
    text-align: center;
    max-width: 90%;
    animation: slideUp 0.4s;
}

@keyframes slideUp {
    from {
        transform: translateY(50px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.score-title {
    font-size: 18px;
    font-weight: 700;
    color: #333;
    margin-bottom: 12px;
}

.score-character {
    font-size: 64px;
    font-weight: 700;
    color: #667eea;
    margin: 12px 0;
}

.score-percentage {
    font-size: 48px;
    font-weight: 700;
    color: #2ecc71;
    margin: 8px 0;
}

.score-percentage.medium {
    color: #f39c12;
}

.score-percentage.low {
    color: #e74c3c;
}

.score-feedback {
    font-size: 20px;
    font-weight: 600;
    color: #555;
    margin: 12px 0;
}

.score-details {
    font-size: 14px;
    color: #777;
    margin: 8px 0;
}

.continue-btn {
    margin-top: 16px;
    padding: 12px 32px;
    background: #667eea;
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.continue-btn:hover {
    background: #5568d3;
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(102, 126, 234, 0.4);
}

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

/* ===================================
   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;
    position: relative;
    z-index: 10;
}

.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; }

/* ===================================
   RESPONSIVE DESIGN
   =================================== */
@media (max-width: 500px) {
    .header {
        padding: 6px 12px;
    }
    
    .stat-label {
        font-size: 10px;
    }
    
    .stat-value {
        font-size: 16px;
    }
    
    .character-display {
        font-size: 36px;
    }
    
    .control-btn {
        min-width: 100px;
        padding: 8px 12px;
        font-size: 12px;
    }
    
    .btn-text {
        font-size: 11px;
    }
    
    .score-character {
        font-size: 48px;
    }
    
    .score-percentage {
        font-size: 36px;
    }
}

/* ===================================
   ACCESSIBILITY & TOUCH OPTIMIZATION
   =================================== */
button:focus,
input:focus {
    outline: 2px solid #667eea;
    outline-offset: 2px;
}

/* Ensure minimum touch target size of 44x44px */
.control-btn,
.continue-btn,
.toggle-switch {
    min-height: 44px;
}

/* High contrast for better readability */
@media (prefers-contrast: high) {
    .header {
        border-bottom-width: 4px;
    }
    
    .control-btn {
        border-width: 3px;
    }
}