html, body {
    height: 100%;
    overflow: hidden;
}

/* Confidence pane on the right for the experimental layout */
body.experimental-layout #confidencePanel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 240px;
    overflow: hidden;
    background: linear-gradient(180deg, #ffffff 0%, #f8fafc 100%);
    border-left: 1px solid #e5e7eb;
    box-shadow: -6px 0 20px rgba(0, 0, 0, 0.05);
    padding: 0.65rem 0.85rem 0.85rem;
    margin-top: 0 !important;
    z-index: 40;
    font-size: 0.8rem;
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

body.experimental-layout #confidencePanel .space-y-1 { margin-top: 0.35rem; }
body.experimental-layout #confidencePanel .border-t { border-top: none !important; }
body.experimental-layout #confidencePanel .pt-4 { padding-top: 0 !important; }
body.experimental-layout #confidencePanel #confidenceList {
    flex: 1;
    overflow-y: auto;
    min-height: 0;
    padding-bottom: 0.5rem;
    max-height: none !important;
}

/* Padding is managed by JS based on whether confidence panel is visible */
body.experimental-layout .app-container {
    min-height: 100vh;
}

/* Main layout */
.app-container {
    display: flex;
    height: 100vh;
    width: 100vw;
    box-sizing: border-box;
}

/* Sidebar (match updated lesson pages) */
.sidebar {
    width: 16rem;
    flex-shrink: 0;
    background: #ffffff;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.06);
    padding: 1rem;
    overflow-y: auto;
}

.sidebar h2 {
    font-size: 1rem;
    font-weight: 700;
    color: #1f2937;
    margin-bottom: 0.75rem;
    margin-top: 2.5rem;
    text-transform: none;
    letter-spacing: 0;
}

.sidebar .mode-desc {
    font-size: 0.875rem;
    color: #9ca3af;
    margin-bottom: 0.75rem;
    line-height: 1.4;
}

.sidebar .mode-btn {
    display: block;
    width: 100%;
    padding: 0.35rem 0.6rem;
    margin-bottom: 0.2rem;
    border-radius: 0.5rem;
    border: 1.5px solid #d1d5db;
    background: #ffffff;
    text-align: left;
    font-size: 0.82rem;
    color: #374151;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.sidebar .mode-btn:hover { background: #f3f4f6; }

.sidebar .mode-btn.active {
    background: #3b82f6;
    color: white;
    border-color: #3b82f6;
}

/* Main content area */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    padding: 0.5rem 1rem;
    overflow: auto;
}

@media (min-width: 900px) {
    .main-content {
        padding: 0.75rem 1.5rem;
    }
}

/* Header with title */
.quiz-header {
    flex-shrink: 0;
    text-align: center;
    padding-bottom: 0.25rem;
}

.quiz-header h1 {
    font-size: 0.9rem;
    font-weight: 600;
    color: #374151;
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Quiz display area */
.quiz-display {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    min-height: 0;
    padding: 0.25rem 0;
}

#questionDisplay { width: 100%; }

/* Three-column layout tweaks */
.three-column-meaning-layout {
    margin: 0 auto;
    padding: 0.5rem;
    gap: 2rem;
}

.three-column-meaning-layout .column-char-large { font-size: clamp(56px, 12vw, 120px); }
.three-column-meaning-layout .column-char { font-size: clamp(28px, 5vw, 40px); }
.three-column-meaning-layout .column-card { min-height: auto; padding: 0.5rem; }

/* Input section */
.input-section {
    flex-shrink: 0;
    max-width: 32rem;
    width: 100%;
    margin: 0 auto;
    padding-top: 0.5rem;
}

.input-section input[type="text"] {
    width: 100%;
    padding: 0.75rem 1rem;
    border: 2px solid #d1d5db;
    border-radius: 0.5rem;
    font-size: 1rem;
    outline: none;
    transition: border-color 0.15s;
}

.input-section input[type="text"]:focus { border-color: #3b82f6; }

.choice-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.75rem;
}

.choice-grid button {
    padding: 0.875rem 1rem;
    border: 2px solid #e5e7eb;
    border-radius: 0.5rem;
    background: white;
    font-size: 0.9375rem;
    cursor: pointer;
    transition: all 0.15s;
}

.choice-grid button:hover {
    border-color: #3b82f6;
    background: #eff6ff;
}

/* Stats bar */
.stats-bar {
    flex-shrink: 0;
    display: flex;
    justify-content: center;
    gap: 1.5rem;
    padding: 0.2rem 0;
    font-size: 0.75rem;
    color: #6b7280;
    border-top: 1px solid #f3f4f6;
    margin-top: auto;
}

.stats-bar span { font-weight: 500; }

/* Feedback area */
#feedback {
    min-height: 2rem;
    text-align: center;
    font-size: 1.25rem;
    font-weight: 600;
    padding: 0.5rem 0;
}

/* Hide mode sections by default; JS toggles them */
#typeMode, #choiceMode, #fuzzyMode, #strokeOrderMode,
#handwritingMode, #drawCharMode, #studyMode, #radicalPracticeMode {
    display: none;
}

#typeMode.active, #choiceMode.active, #fuzzyMode.active,
#strokeOrderMode.active, #handwritingMode.active,
#drawCharMode.active, #studyMode.active, #radicalPracticeMode.active {
    display: block;
}

/* Home button */
.home-btn {
    position: fixed;
    top: 1rem;
    left: 1rem;
    padding: 0.375rem 0.75rem;
    background: white;
    border: 1px solid #e5e7eb;
    border-radius: 0.375rem;
    font-size: 0.875rem;
    color: #374151;
    text-decoration: none;
    z-index: 50;
    transition: background 0.15s;
}

.home-btn:hover { background: #f3f4f6; }

/* Audio button */
#audioSection button {
    padding: 0.75rem 1.5rem;
    background: #06b6d4;
    color: white;
    border: none;
    border-radius: 0.5rem;
    font-size: 1rem;
    cursor: pointer;
    transition: background 0.15s;
}

#audioSection button:hover { background: #0891b2; }

/* Study Mode Container - expand to fill available space */
.study-mode-container {
    width: 100%;
    max-width: none;
    padding: 0 1rem;
}

#studyMode.active {
    display: block;
    width: 100%;
}

/* When study mode is active, expand input-section to full width */
.input-section:has(#studyMode.active) {
    max-width: none;
    padding: 0;
}

/* Auto-hide sidebar at narrow widths */
@media (max-width: 780px) {
    .sidebar {
        position: fixed;
        left: -16rem;
        top: 0;
        bottom: 0;
        z-index: 50;
        transition: left 0.2s ease;
        box-shadow: none;
    }

    .sidebar.sidebar-open {
        left: 0;
        box-shadow: 4px 0 20px rgba(0,0,0,0.15);
    }

    .app-container {
        padding-left: 0 !important;
    }

    .sidebar-mobile-toggle {
        display: flex !important;
    }
}

.sidebar-mobile-toggle {
    display: none;
    position: fixed;
    top: 0.75rem;
    left: 0.75rem;
    z-index: 51;
    align-items: center;
    justify-content: center;
    width: 2rem;
    height: 2rem;
    background: white;
    border: 1px solid #e5e7eb;
    border-radius: 0.375rem;
    cursor: pointer;
    font-size: 1rem;
    color: #374151;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    transition: background 0.15s;
}

.sidebar-mobile-toggle:hover {
    background: #f3f4f6;
}

/* Overlay behind open mobile sidebar */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 49;
    background: rgba(0,0,0,0.25);
}

.sidebar-overlay.active {
    display: block;
}

/* Collapsible sidebar styles */
.sidebar-wrapper {
    position: relative;
    display: flex;
    flex-shrink: 0;
    transition: width 0.2s ease;
}

.sidebar-wrapper.collapsed {
    width: 0;
}

.sidebar-wrapper.collapsed .sidebar-content {
    display: none;
}

.sidebar-content {
    width: 16rem;
    background: #ffffff;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.06);
    padding: 1rem;
    overflow-y: auto;
    height: 100%;
}

.sidebar-toggle {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 1.5rem;
    height: 3rem;
    background: #ffffff;
    border: 1px solid #e5e7eb;
    border-radius: 0 0.375rem 0.375rem 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #6b7280;
    font-size: 0.75rem;
    z-index: 45;
    transition: background 0.15s, color 0.15s;
}

.sidebar-toggle:hover {
    background: #f3f4f6;
    color: #374151;
}

/* Left sidebar toggle positioning */
.sidebar-wrapper.left .sidebar-toggle {
    right: -1.5rem;
}

.sidebar-wrapper.left.collapsed .sidebar-toggle {
    right: auto;
    left: 0;
    border-radius: 0 0.375rem 0.375rem 0;
}

/* Right sidebar toggle positioning */
.sidebar-wrapper.right .sidebar-toggle {
    left: -1.5rem;
    border-radius: 0.375rem 0 0 0.375rem;
}

.sidebar-wrapper.right.collapsed .sidebar-toggle {
    left: auto;
    right: 0;
    border-radius: 0.375rem 0 0 0.375rem;
}

/* Dictation chat layout overrides */
body.dictation-chat-active .main-content {
    padding: 1rem;
}

body.dictation-chat-active .quiz-header {
    text-align: left;
}

body.dictation-chat-active .quiz-display {
    display: none;
}

body.dictation-chat-active .input-section {
    flex: 1;
    max-width: none;
    width: 100%;
    margin: 0;
    padding-top: 0;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

body.dictation-chat-active #dictationChatMode {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
}

body.dictation-chat-active #dictationChatMessages {
    flex: 1;
    height: auto;
    min-height: 0;
}

body.dictation-chat-active .stats-bar {
    display: none;
}

body.dictation-chat-active #feedback {
    display: none;
}

.dictation-chat-app {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 280px;
    gap: 1.25rem;
    height: 100%;
    min-height: 0;
}

.dictation-chat-main {
    display: flex;
    flex-direction: column;
    min-height: 0;
    background: #ffffff;
    border-radius: 22px;
    border: 1px solid #e5e7eb;
    box-shadow: 0 16px 30px rgba(15, 23, 42, 0.08);
    overflow: hidden;
}

.dictation-chat-header {
    padding: 1rem 1.25rem 0.75rem;
    border-bottom: 1px solid #f1f5f9;
    background: linear-gradient(180deg, #ffffff 0%, #f8fafc 100%);
}

.dictation-chat-title {
    font-size: 1.05rem;
    font-weight: 700;
    color: #0f172a;
}

.dictation-chat-subtitle {
    font-size: 0.85rem;
    color: #64748b;
    margin-top: 0.25rem;
}

.dictation-chat-messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 1.25rem;
    display: flex;
    flex-direction: column;
    gap: 0.85rem;
    background: #f5f7fb;
}

.dictation-chat-input {
    display: flex;
    gap: 0.75rem;
    padding: 0.85rem 1.1rem;
    border-top: 1px solid #e2e8f0;
    background: #f8fafc;
}

.dictation-chat-textarea {
    flex: 1;
    resize: none;
    border-radius: 16px;
    border: 1px solid #cbd5f5;
    padding: 0.7rem 0.9rem;
    font-size: 0.95rem;
    min-height: 48px;
    outline: none;
    background: #ffffff;
}

.dictation-chat-textarea:focus {
    border-color: #2563eb;
    box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.15);
}

.dictation-chat-send {
    border: none;
    border-radius: 16px;
    padding: 0.6rem 1.2rem;
    font-weight: 600;
    background: #2563eb;
    color: #ffffff;
    cursor: pointer;
    transition: background 0.15s ease;
}

.dictation-chat-send:hover {
    background: #1d4ed8;
}

.dictation-chat-hint {
    padding: 0 1.2rem 1rem;
    font-size: 0.75rem;
    color: #94a3b8;
}

.dictation-chat-aside {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    min-height: 0;
}

.dictation-chat-card {
    background: #ffffff;
    border: 1px solid #e5e7eb;
    border-radius: 18px;
    padding: 0.9rem;
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
    box-shadow: 0 12px 22px rgba(15, 23, 42, 0.06);
}

.dictation-chat-card-label {
    font-size: 0.65rem;
    letter-spacing: 0.3em;
    text-transform: uppercase;
    color: #94a3b8;
    font-weight: 700;
}

.dictation-chat-card-text {
    font-size: 0.95rem;
    color: #0f172a;
    font-weight: 600;
    line-height: 1.4;
}

.dictation-chat-toggle {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.dictation-chat-toggle-btn {
    border: 1px solid #cbd5f5;
    background: #f8fafc;
    color: #475569;
    padding: 0.35rem 0.75rem;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s ease;
}

.dictation-chat-toggle-btn.is-active {
    background: #2563eb;
    color: #ffffff;
    border-color: #2563eb;
}

.dictation-chat-audio-slot {
    display: flex;
    justify-content: center;
    padding: 0.4rem 0;
}

.dictation-chat-status {
    font-size: 0.9rem;
    font-weight: 600;
    color: #b45309;
}

.dictation-chat-status.is-pending {
    color: #b45309;
}

.dictation-chat-status.is-passed {
    color: #15803d;
}

.dictation-chat-actions {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.dictation-chat-action {
    border: 1px solid #cbd5f5;
    background: #ffffff;
    color: #475569;
    padding: 0.45rem 0.8rem;
    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s ease;
}

.dictation-chat-action-primary {
    background: #10b981;
    border-color: #10b981;
    color: #ffffff;
}

.dictation-chat-action:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.dictation-chat-mini-stats {
    font-size: 0.75rem;
    color: #64748b;
    display: grid;
    gap: 0.35rem;
}

.dictation-chat-bubble {
    max-width: min(70%, 520px);
    padding: 0.75rem 0.95rem;
    border-radius: 18px;
    font-size: 0.95rem;
    line-height: 1.5;
    position: relative;
}

.dictation-chat-bubble-label {
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.2em;
    color: rgba(71, 85, 105, 0.7);
    margin-bottom: 0.35rem;
}

.dictation-chat-bubble.is-user {
    align-self: flex-end;
    background: #2563eb;
    color: #ffffff;
    border-bottom-right-radius: 6px;
}

.dictation-chat-bubble.is-user .dictation-chat-bubble-label {
    color: rgba(226, 232, 240, 0.8);
    text-align: right;
}

.dictation-chat-bubble.is-assistant {
    align-self: flex-start;
    background: #ffffff;
    border: 1px solid #e5e7eb;
    color: #0f172a;
    border-bottom-left-radius: 6px;
}

.dictation-chat-bubble.is-typing {
    color: #475569;
}

.dictation-chat-bubble.is-assistant .md-line,
.dictation-chat-bubble.is-assistant .md-list,
.dictation-chat-bubble.is-assistant .md-quote {
    color: inherit;
}

@media (max-width: 1100px) {
    .dictation-chat-app {
        grid-template-columns: 1fr;
    }

    .dictation-chat-aside {
        order: -1;
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
        align-items: start;
    }
}


/* Dark mode (opt-in): add .theme-dark to <body> to enable. */

    body.theme-dark.experimental-layout #confidencePanel {
        background: linear-gradient(180deg, #0f172a 0%, #0b1020 100%);
        border-left-color: var(--dm-border);
        box-shadow: -6px 0 20px rgba(0, 0, 0, 0.45);
        color: var(--dm-text-soft);
    }

    body.theme-dark .sidebar,
    body.theme-dark .sidebar-content {
        background: var(--dm-surface);
        box-shadow: var(--dm-shadow);
        color: var(--dm-text);
    }

    body.theme-dark .sidebar h2 {
        color: var(--dm-text);
    }

    body.theme-dark .sidebar .mode-desc {
        color: var(--dm-text-soft);
    }

    body.theme-dark .sidebar .mode-btn {
        background: var(--dm-surface-2);
        border-color: var(--dm-border-strong);
        color: var(--dm-text-muted);
    }

    body.theme-dark .sidebar .mode-btn:hover {
        background: #1f2937;
    }

    body.theme-dark .sidebar .mode-btn.active {
        background: var(--dm-accent-strong);
        border-color: var(--dm-accent-strong);
        color: #f8fafc;
    }

    body.theme-dark .quiz-header h1 {
        color: var(--dm-text);
    }

    body.theme-dark .input-section input[type="text"] {
        background: var(--dm-surface-2);
        border-color: var(--dm-border-strong);
        color: var(--dm-text);
    }

    body.theme-dark .input-section input[type="text"]:focus {
        border-color: var(--dm-accent);
        box-shadow: 0 0 0 2px rgba(96, 165, 250, 0.2);
    }

    body.theme-dark .choice-grid button {
        background: var(--dm-surface-2);
        border-color: var(--dm-border);
        color: var(--dm-text);
    }

    body.theme-dark .choice-grid button:hover {
        border-color: var(--dm-accent);
        background: rgba(59, 130, 246, 0.14);
    }

    body.theme-dark .stats-bar {
        color: var(--dm-text-faint);
        border-top-color: var(--dm-border);
    }

    body.theme-dark .home-btn {
        background: var(--dm-surface);
        border-color: var(--dm-border);
        color: var(--dm-text-muted);
    }

    body.theme-dark .home-btn:hover {
        background: var(--dm-surface-2);
    }

    body.theme-dark .sidebar-toggle {
        background: var(--dm-surface);
        border-color: var(--dm-border);
        color: var(--dm-text-faint);
    }

    body.theme-dark .sidebar-toggle:hover {
        background: var(--dm-surface-2);
        color: var(--dm-text-muted);
    }

    body.theme-dark .dictation-chat-main {
        background: var(--dm-surface);
        border-color: var(--dm-border);
        box-shadow: var(--dm-shadow);
    }

    body.theme-dark .dictation-chat-header {
        border-bottom-color: var(--dm-border);
        background: linear-gradient(180deg, #0f172a 0%, #0b1020 100%);
    }

    body.theme-dark .dictation-chat-title {
        color: var(--dm-text);
    }

    body.theme-dark .dictation-chat-subtitle {
        color: var(--dm-text-soft);
    }

    body.theme-dark .dictation-chat-messages {
        background: #0b1020;
    }

    body.theme-dark .dictation-chat-input {
        border-top-color: var(--dm-border);
        background: var(--dm-surface-2);
    }

    body.theme-dark .dictation-chat-textarea {
        background: #0b1020;
        border-color: var(--dm-border-strong);
        color: var(--dm-text);
    }

    body.theme-dark .dictation-chat-textarea:focus {
        border-color: var(--dm-accent);
        box-shadow: 0 0 0 2px rgba(96, 165, 250, 0.2);
    }

    body.theme-dark .dictation-chat-hint {
        color: var(--dm-text-faint);
    }

    body.theme-dark .dictation-chat-card {
        background: var(--dm-surface);
        border-color: var(--dm-border);
        box-shadow: var(--dm-shadow);
    }

    body.theme-dark .dictation-chat-card-label {
        color: var(--dm-text-faint);
    }

    body.theme-dark .dictation-chat-card-text {
        color: var(--dm-text);
    }

    body.theme-dark .dictation-chat-toggle-btn {
        background: var(--dm-surface-2);
        border-color: var(--dm-border);
        color: var(--dm-text-soft);
    }

    body.theme-dark .dictation-chat-toggle-btn.is-active {
        background: var(--dm-accent-strong);
        border-color: var(--dm-accent-strong);
        color: #f8fafc;
    }

    body.theme-dark .dictation-chat-action {
        background: var(--dm-surface-2);
        border-color: var(--dm-border);
        color: var(--dm-text-soft);
    }

    body.theme-dark .dictation-chat-action-primary {
        background: #10b981;
        border-color: #10b981;
        color: #f0fdf4;
    }

    body.theme-dark .dictation-chat-mini-stats {
        color: var(--dm-text-faint);
    }

    body.theme-dark .dictation-chat-bubble-label {
        color: rgba(148, 163, 184, 0.7);
    }

    body.theme-dark .dictation-chat-bubble.is-assistant {
        background: var(--dm-surface-2);
        border-color: var(--dm-border);
        color: var(--dm-text);
    }

    body.theme-dark .dictation-chat-bubble.is-typing {
        color: var(--dm-text-soft);
    }
