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

:root {
    --bg: #faf9f6;
    --surface: #ffffff;
    --text: #2d2d2d;
    --text-light: #6b6b6b;
    --primary: #4a7c59;
    --primary-light: #e8f0eb;
    --accent: #e8913a;
    --accent-light: #fef3e6;
    --error: #c0392b;
    --error-light: #fdecea;
    --correct: #27ae60;
    --correct-light: #e8f8ef;
    --border: #e0ddd8;
    --radius: 12px;
    --shadow: 0 2px 8px rgba(0,0,0,0.06);
    --shadow-lg: 0 4px 20px rgba(0,0,0,0.1);
}

body {
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
}

/* ===== Nav ===== */
.top-nav {
    display: flex;
    align-items: center;
    gap: 24px;
    padding: 12px 24px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow);
}
.nav-brand {
    font-size: 18px;
    font-weight: 700;
    color: var(--primary);
}
.nav-badge {
    font-size: 10px;
    font-weight: 600;
    background: var(--accent-light);
    color: var(--accent);
    padding: 2px 8px;
    border-radius: 99px;
    vertical-align: middle;
    margin-left: 6px;
}
.nav-links { display: flex; gap: 4px; flex: 1; }
.nav-links a {
    text-decoration: none;
    color: var(--text-light);
    padding: 6px 14px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    transition: all 0.15s;
}
.nav-links a:hover { background: var(--primary-light); color: var(--primary); }
.nav-links a.active { background: var(--primary-light); color: var(--primary); font-weight: 600; }
.nav-status { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-light); }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: #ccc; }
.status-dot.green { background: var(--correct); }
.status-dot.yellow { background: var(--accent); }
.status-dot.red { background: var(--error); }

main { max-width: 1100px; margin: 0 auto; padding: 24px; }

/* ===== Cards ===== */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 24px;
    box-shadow: var(--shadow);
}
.card + .card { margin-top: 16px; }
.card h2 { font-size: 16px; font-weight: 600; margin-bottom: 12px; color: var(--text); }
.card h3 { font-size: 14px; font-weight: 600; margin-bottom: 8px; color: var(--text-light); }

/* ===== Index Page ===== */
.hero {
    text-align: center;
    padding: 48px 24px;
}
.hero h1 { font-size: 28px; font-weight: 700; color: var(--primary); margin-bottom: 8px; }
.hero p { font-size: 16px; color: var(--text-light); max-width: 500px; margin: 0 auto 32px; }
.hero-actions { display: flex; gap: 12px; justify-content: center; }
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 10px 20px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    text-decoration: none;
    border: none;
    cursor: pointer;
    transition: all 0.15s;
}
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: #3d6a4a; }
.btn-outline { background: var(--surface); color: var(--primary); border: 1.5px solid var(--primary); }
.btn-outline:hover { background: var(--primary-light); }
.btn-accent { background: var(--accent); color: white; }
.btn-accent:hover { background: #d07e2f; }
.btn-sm { padding: 6px 12px; font-size: 12px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ===== Reader Page ===== */
.reader-layout {
    display: grid;
    grid-template-columns: 260px 1fr;
    gap: 16px;
    align-items: start;
}

/* Sidebar: book list */
.book-list { display: flex; flex-direction: column; gap: 8px; }
.book-card {
    padding: 14px;
    border: 1.5px solid var(--border);
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.15s;
    background: var(--surface);
}
.book-card:hover { border-color: var(--primary); box-shadow: var(--shadow); }
.book-card.selected { border-color: var(--primary); background: var(--primary-light); }
.book-card .book-title { font-size: 14px; font-weight: 600; }
.book-card .book-meta { font-size: 11px; color: var(--text-light); margin-top: 2px; }
.band-tag {
    display: inline-block;
    font-size: 10px;
    font-weight: 600;
    padding: 1px 6px;
    border-radius: 4px;
    background: var(--accent-light);
    color: var(--accent);
}

/* Reading area */
.reading-area {
    display: flex;
    flex-direction: column;
    gap: 16px;
}
.page-display {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 40px;
    min-height: 200px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow);
}
.page-text {
    font-size: 32px;
    line-height: 1.8;
    font-family: 'Georgia', serif;
    text-align: center;
    max-width: 600px;
}
.page-text .word {
    display: inline-block;
    padding: 2px 4px;
    border-radius: 4px;
    transition: all 0.2s;
    cursor: default;
    margin: 0 2px;
}
.page-text .word.current {
    background: var(--accent-light);
    color: var(--accent);
    font-weight: 700;
}
.page-text .word.correct {
    background: var(--correct-light);
    color: var(--correct);
}
.page-text .word.error {
    background: var(--error-light);
    color: var(--error);
    text-decoration: underline wavy var(--error);
}
.page-text .word.recognized {
    background: #e8edf3;
    color: #4a6785;
}
.page-text .word.upcoming {
    color: var(--text-light);
}

.page-placeholder {
    color: var(--text-light);
    font-size: 16px;
    text-align: center;
}

/* Controls */
.controls-bar {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
}
.provider-select {
    padding: 6px 12px;
    border: 1.5px solid var(--border);
    border-radius: 8px;
    font-size: 13px;
    background: var(--surface);
    cursor: pointer;
}
.page-nav { display: flex; gap: 6px; align-items: center; margin-left: auto; }
.page-indicator { font-size: 13px; color: var(--text-light); min-width: 80px; text-align: center; }

/* Mic indicator */
.mic-indicator {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    font-weight: 600;
}
.mic-indicator .mic-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: #ccc;
}
.mic-indicator.recording .mic-dot {
    background: var(--error);
    animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse {
    0%, 100% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.3); opacity: 0.7; }
}

/* ===== Event Log ===== */
.event-log {
    max-height: 300px;
    overflow-y: auto;
    font-family: 'SF Mono', 'Consolas', monospace;
    font-size: 12px;
    padding: 12px;
    background: #1e1e1e;
    color: #d4d4d4;
    border-radius: 8px;
    line-height: 1.7;
}
.event-log .ev-time { color: #6a9955; }
.event-log .ev-type { font-weight: 600; }
.event-log .ev-highlight { color: #569cd6; }
.event-log .ev-error { color: #f44747; }
.event-log .ev-intervention { color: #dcdcaa; }
.event-log .ev-system { color: #808080; }

/* ===== Intervention Overlay ===== */
.intervention-overlay {
    display: none;
    position: fixed;
    bottom: 24px;
    right: 24px;
    background: var(--surface);
    border: 2px solid var(--accent);
    border-radius: var(--radius);
    padding: 16px 20px;
    box-shadow: var(--shadow-lg);
    z-index: 100;
    max-width: 300px;
    animation: slideUp 0.3s ease;
}
.intervention-overlay.visible { display: block; }
.intervention-overlay h4 { font-size: 13px; color: var(--accent); margin-bottom: 6px; }
.intervention-overlay p { font-size: 14px; }
.intervention-overlay .phonemes {
    font-size: 20px;
    font-family: 'Georgia', serif;
    font-weight: 700;
    letter-spacing: 4px;
    margin-top: 8px;
    color: var(--primary);
}
@keyframes slideUp {
    from { transform: translateY(20px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

/* ===== Session Summary ===== */
.summary-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 12px;
}
.summary-stat {
    text-align: center;
    padding: 16px;
    background: var(--bg);
    border-radius: 8px;
}
.summary-stat .stat-value {
    font-size: 28px;
    font-weight: 700;
    color: var(--primary);
}
.summary-stat .stat-label {
    font-size: 11px;
    color: var(--text-light);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-top: 4px;
}

/* ===== Dashboard ===== */
.dashboard-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}
.skill-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}
.skill-bar .skill-name { font-size: 12px; min-width: 120px; }
.skill-bar .bar-bg {
    flex: 1;
    height: 8px;
    background: var(--bg);
    border-radius: 4px;
    overflow: hidden;
}
.skill-bar .bar-fill {
    height: 100%;
    border-radius: 4px;
    transition: width 0.5s ease;
}
.skill-bar .bar-fill.emerging { background: var(--error); }
.skill-bar .bar-fill.approaching { background: var(--accent); }
.skill-bar .bar-fill.proficient { background: var(--correct); }

/* ===== Feedback Mode Toggle ===== */
.toggle-group { display: flex; flex-direction: column; gap: 6px; }
.toggle-label {
    display: flex; align-items: center; gap: 8px;
    font-size: 13px; cursor: pointer; padding: 6px 8px;
    border-radius: 6px; transition: background 0.15s;
}
.toggle-label:hover { background: var(--bg); }
.toggle-label input { accent-color: var(--primary); }

/* ===== Voice Selector ===== */
.voice-list { max-height: 260px; overflow-y: auto; }
.voice-item {
    padding: 4px 6px; border-radius: 6px; cursor: pointer;
    border: 1.5px solid transparent; white-space: nowrap; line-height: 24px;
}
.voice-item:hover { background: var(--bg); }
.voice-item.selected { border-color: var(--primary); background: var(--primary-light); }
.voice-item .voice-play {
    display: inline-block; width: 20px; height: 20px; border-radius: 50%;
    border: 1.5px solid var(--border); background: var(--surface);
    text-align: center; line-height: 18px; font-size: 8px; cursor: pointer;
    vertical-align: middle; margin-right: 6px;
}
.voice-item .voice-play:hover { border-color: var(--primary); background: var(--primary-light); }
.voice-item .voice-play.playing { border-color: var(--accent); background: var(--accent-light); }
.voice-name { font-size: 12px; font-weight: 600; color: var(--text); vertical-align: middle; }
.voice-tag {
    font-size: 9px; font-weight: 600; padding: 1px 4px; border-radius: 3px;
    background: var(--bg); color: var(--text-light); vertical-align: middle; margin-left: 4px;
}

/* ===== Tutor Chat ===== */
.tutor-chat {
    display: flex; gap: 12px; align-items: flex-start;
    padding: 16px; background: var(--surface);
    border: 2px solid var(--primary); border-radius: var(--radius);
    box-shadow: var(--shadow-lg); animation: slideUp 0.3s ease;
}
.tutor-avatar {
    width: 40px; height: 40px; border-radius: 50%;
    background: var(--primary); color: white;
    display: flex; align-items: center; justify-content: center;
    font-weight: 700; font-size: 18px; flex-shrink: 0;
}
.tutor-bubble { flex: 1; }
.tutor-text { font-size: 16px; line-height: 1.5; color: var(--text); }
.tutor-loading { font-size: 13px; color: var(--text-light); font-style: italic; }

/* ===== Toast Notifications ===== */
.toast-container {
    position: fixed; top: 20px; left: 50%; transform: translateX(-50%);
    z-index: 200; display: flex; flex-direction: column; gap: 8px; align-items: center;
    pointer-events: none;
}
.toast {
    padding: 10px 24px; border-radius: 10px; font-size: 15px; font-weight: 600;
    box-shadow: 0 4px 16px rgba(0,0,0,0.15); animation: toastIn 0.3s ease, toastOut 0.3s ease 2.2s forwards;
    pointer-events: auto;
}
.toast.success { background: var(--correct); color: #fff; }
.toast.error { background: var(--error-light); color: var(--error); border: 1.5px solid var(--error); }
.toast.info { background: var(--surface); color: var(--text); border: 1.5px solid var(--border); }
@keyframes toastIn { from { opacity: 0; transform: translateY(-10px); } to { opacity: 1; transform: translateY(0); } }
@keyframes toastOut { from { opacity: 1; } to { opacity: 0; transform: translateY(-10px); } }

/* ===== Live Metrics Dashboard ===== */
.metrics-panel {
    background: #1a1a2e; border-radius: var(--radius); overflow: hidden;
    box-shadow: var(--shadow-lg); font-family: 'SF Mono', 'Consolas', monospace;
}
.metrics-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 8px 14px; background: #16213e; color: #a8b2d1; font-size: 11px;
    font-weight: 600; text-transform: uppercase; letter-spacing: 1px; cursor: pointer;
    user-select: none;
}
.metrics-header:hover { background: #1a2744; }
.metrics-toggle { font-size: 10px; transition: transform 0.2s; }
.metrics-body.collapsed { display: none; }
.metrics-body.collapsed + .metrics-header .metrics-toggle { transform: rotate(-90deg); }
.metrics-body { padding: 10px; }
.metrics-row { display: flex; gap: 8px; margin-bottom: 8px; }
.metrics-row:last-child { margin-bottom: 0; }
.metric-card {
    flex: 1; background: #0f3460; border-radius: 8px; padding: 10px 12px;
    min-width: 0;
}
.metric-card.metric-wide { flex: 1; }
.metric-value {
    font-size: 22px; font-weight: 700; color: #e94560; line-height: 1.1;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.metric-label { font-size: 9px; color: #a8b2d1; text-transform: uppercase; letter-spacing: 0.5px; margin-top: 2px; }
.metric-sub { font-size: 10px; color: #5a6a8a; margin-top: 2px; }

/* Hesitation bar */
.hesitation-bar-container { position: relative; height: 14px; background: #16213e; border-radius: 4px; overflow: hidden; }
.hesitation-bar {
    height: 100%; width: 0%; border-radius: 4px; transition: background 0.3s;
    background: #27ae60;
}
.hesitation-bar.warning { background: #e8913a; }
.hesitation-bar.danger { background: #e94560; }
.hesitation-marks {
    position: absolute; top: 0; left: 0; right: 0; height: 100%;
    display: flex; justify-content: space-between; padding: 0 2px;
    font-size: 7px; color: #5a6a8a; align-items: center; pointer-events: none;
}

/* Pipeline viz */
.pipeline-viz {
    display: flex; align-items: center; gap: 3px;
}
.pipe-segment {
    padding: 3px 6px; border-radius: 4px; font-size: 8px; font-weight: 700;
    text-align: center; color: #fff; min-width: 36px; position: relative;
}
.pipe-segment::after {
    content: attr(data-ms); display: block; font-size: 9px; font-weight: 400; color: #a8b2d1;
}
.pipe-audio { background: #2d6a4f; }
.pipe-network { background: #1d3557; }
.pipe-asr { background: #6a2c70; }
.pipe-match { background: #b55400; }
.pipe-arrow { color: #5a6a8a; font-size: 10px; }
.pipe-segment.active { box-shadow: 0 0 8px rgba(233,69,96,0.5); }

/* Counters row */
.metrics-counters {
    display: flex; gap: 4px; flex-wrap: wrap;
}
.counter {
    flex: 1; text-align: center; background: #16213e; border-radius: 6px; padding: 6px 4px;
    min-width: 50px;
}
.counter span { display: block; font-size: 16px; font-weight: 700; color: #e94560; }
.counter label { font-size: 8px; color: #5a6a8a; text-transform: uppercase; letter-spacing: 0.3px; }

/* ===== Responsive ===== */
@media (max-width: 768px) {
    .reader-layout { grid-template-columns: 1fr; }
    .dashboard-grid { grid-template-columns: 1fr; }
    .page-text { font-size: 24px; }
}
