/* Training Studio — only apply layout when active (don't override page-section display:none) */
#page-training-studio.active {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
}

.ts-container {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
    max-width: 1100px;
    margin: 0 auto;
    width: 100%;
}

/* Header */
.ts-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

/* Card panels */
.ts-card {
    padding: 16px;
    border-radius: 10px;
    background: var(--bg-secondary, #1e1e2e);
    margin-bottom: 16px;
}

/* Source panel */
.ts-source-panel { }
.ts-folder-input { display: flex; gap: 8px; }
.ts-folder-input input { flex: 1; }
.ts-file-list { list-style: none; padding: 0; margin-top: 12px; }
.ts-file-item { display: flex; align-items: center; gap: 8px; padding: 6px 12px; border-radius: 6px; background: var(--bg-secondary, #1e1e2e); margin-bottom: 4px; }
.ts-file-icon { font-size: 1.2em; }
.ts-file-name { flex: 1; font-size: 0.9em; }
.ts-file-type { font-size: 0.75em; color: var(--text-muted); text-transform: uppercase; }

/* File tree */
.ts-tree { list-style: none; padding-left: 0; margin: 8px 0; }
.ts-tree .ts-tree-children { list-style: none; padding-left: 20px; }
.ts-tree-node { margin: 1px 0; }
.ts-tree-row { display: flex; align-items: center; gap: 6px; padding: 3px 8px; border-radius: 4px; cursor: default; }
.ts-tree-row:hover { background: rgba(255,255,255,0.04); }
.ts-tree-toggle { width: 16px; text-align: center; font-size: 0.7em; cursor: pointer; color: var(--text-muted); transition: transform 0.15s; user-select: none; }
.ts-tree-toggle.open { transform: rotate(90deg); }
.ts-tree-toggle-spacer { width: 16px; }
.ts-tree-check { accent-color: var(--primary, #a855f7); cursor: pointer; }
.ts-tree-icon { font-size: 1em; }
.ts-tree-name { font-size: 0.85em; }
.ts-tree-loading { color: var(--text-muted); font-size: 0.8em; padding: 4px 8px 4px 42px; }
.ts-select-actions { display: flex; gap: 8px; margin: 8px 0; align-items: center; }
.ts-select-actions button { font-size: 0.8em; padding: 4px 12px; border-radius: 4px; border: 1px solid var(--border-color, #333); background: transparent; color: var(--text-primary); cursor: pointer; }
.ts-select-actions button:hover { background: rgba(255,255,255,0.06); }

/* Upload zone */
.ts-upload-zone {
    border: 2px dashed var(--border-color, #444);
    border-radius: 10px;
    padding: 24px 16px;
    text-align: center;
    cursor: pointer;
    transition: border-color 0.2s, background 0.2s;
    margin-bottom: 12px;
}
.ts-upload-zone:hover, .ts-upload-zone.dragover {
    border-color: var(--primary, #a855f7);
    background: rgba(168, 85, 247, 0.05);
}
.ts-upload-zone.dragover {
    background: rgba(168, 85, 247, 0.1);
}
.ts-uploaded-file {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    border-radius: 6px;
    background: var(--bg-secondary, #1e1e2e);
    margin-bottom: 4px;
}

/* Video player in completion */
#ts-video-player {
    display: block;
    margin: 16px auto;
}

/* Settings panel */
.ts-settings-panel { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 16px; }
.ts-setting-group { display: flex; flex-direction: column; gap: 6px; }
.ts-setting-group label { font-size: 0.85em; color: var(--text-muted); }
.ts-setting-group select, .ts-setting-group input { padding: 8px 12px; border-radius: 6px; border: 1px solid var(--border-color, #333); background: var(--bg-input, #1a1a2e); color: var(--text-primary, #e8eaed); }

/* Script editor */
.ts-script-editor { margin-top: 24px; }
.ts-section-card { background: var(--bg-secondary, #1e1e2e); border-radius: 10px; padding: 16px; margin-bottom: 16px; border: 1px solid var(--border-color, #2a2a3e); transition: border-color 0.3s; }
.ts-section-card.ts-section-done { border-left: 3px solid #00ff88; }
.ts-section-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.ts-section-title { font-weight: 600; font-size: 1em; }
.ts-section-source { font-size: 0.75em; color: var(--text-muted); }
.ts-section-body { display: flex; gap: 16px; }
.ts-slide-preview { width: 240px; height: 135px; border-radius: 6px; object-fit: cover; background: #111; flex-shrink: 0; }
.ts-script-textarea { flex: 1; min-height: 120px; resize: vertical; padding: 10px; border-radius: 6px; border: 1px solid var(--border-color, #333); background: var(--bg-input, #1a1a2e); color: var(--text-primary); font-family: inherit; font-size: 0.9em; line-height: 1.5; }
.ts-section-actions { display: flex; gap: 8px; margin-top: 10px; justify-content: flex-end; }
.ts-word-count { font-size: 0.75em; color: var(--text-muted); margin-top: 4px; }

/* Progress panel — sticky, always visible during processing */
.ts-progress-panel {
    position: sticky;
    top: 0;
    z-index: 50;
    background: var(--bg-secondary, #1e1e2e);
    border: 1px solid rgba(79, 195, 247, 0.3);
    border-radius: 10px;
    padding: 16px 20px;
    margin: 16px 0;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
}

/* Progress bar track */
.ts-progress-track {
    width: 100%;
    height: 40px;
    background: #1a1a2e;
    border-radius: 20px;
    overflow: hidden;
    position: relative;
    border: 1px solid #2a2a3e;
}

/* Progress bar fill */
.ts-progress-fill {
    height: 100%;
    min-width: 40px;
    background: linear-gradient(90deg, #4fc3f7, #7c4dff, #a855f7);
    border-radius: 20px;
    transition: width 0.4s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    animation: ts-progress-pulse 2s ease-in-out infinite;
}

/* Percentage text centered in the bar */
.ts-progress-pct {
    font-size: 0.95em;
    font-weight: 700;
    color: #fff;
    text-shadow: 0 1px 3px rgba(0,0,0,0.5);
    white-space: nowrap;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}

/* When bar is too narrow, show pct outside */
.ts-progress-track .ts-progress-pct {
    min-width: max-content;
}

@keyframes ts-progress-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.85; }
}

.ts-progress-stage {
    font-size: 0.9em;
    font-weight: 600;
    color: #4fc3f7;
    margin-top: 10px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.ts-progress-message {
    font-size: 0.85em;
    color: var(--text-muted);
    margin-top: 4px;
}

/* Stage indicators */
.ts-stages { display: flex; gap: 6px; margin-bottom: 14px; }
.ts-stage-dot {
    flex: 1;
    height: 24px;
    border-radius: 12px;
    background: var(--bg-tertiary, #252535);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7em;
    color: #555;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.3px;
    transition: all 0.3s;
}
.ts-stage-dot.active {
    background: rgba(79, 195, 247, 0.2);
    color: #4fc3f7;
    border: 1px solid rgba(79, 195, 247, 0.4);
}
.ts-stage-dot.done {
    background: rgba(102, 187, 106, 0.2);
    color: #66bb6a;
    border: 1px solid rgba(102, 187, 106, 0.3);
}

/* Action buttons */
.ts-actions { display: flex; gap: 12px; margin-top: 20px; }
.ts-btn-primary { padding: 10px 24px; border-radius: 8px; border: none; background: linear-gradient(135deg, #4fc3f7, #7c4dff); color: white; font-weight: 600; cursor: pointer; font-size: 0.95em; }
.ts-btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.ts-btn-secondary { padding: 10px 24px; border-radius: 8px; border: 1px solid var(--border-color, #333); background: transparent; color: var(--text-primary); cursor: pointer; }

/* Completion */
.ts-complete-panel { text-align: center; padding: 40px; }
.ts-complete-icon { font-size: 3em; margin-bottom: 16px; }
.ts-complete-title { font-size: 1.4em; font-weight: 700; margin-bottom: 8px; }

/* Project history */
.ts-history { margin-top: 32px; }
.ts-project-row { display: flex; align-items: center; padding: 10px 16px; background: var(--bg-secondary, #1e1e2e); border-radius: 8px; margin-bottom: 8px; cursor: pointer; }
.ts-project-row:hover { background: var(--bg-hover, #262640); }
.ts-project-title { flex: 1; font-weight: 500; }
.ts-project-status { font-size: 0.8em; padding: 2px 8px; border-radius: 10px; }
.ts-project-status.complete { background: #1b5e20; color: #a5d6a7; }
.ts-project-status.scripted { background: #0d47a1; color: #90caf9; }
.ts-project-status.failed { background: #b71c1c; color: #ef9a9a; }
.ts-project-status.cancelled { background: #4a4a00; color: #ffd54f; }

/* ── Mobile Responsive ── */
@media (max-width: 768px) {
    .ts-container {
        padding: 12px;
    }

    /* Header — stack title + history button */
    .ts-header {
        flex-direction: column;
        align-items: flex-start !important;
        gap: 8px;
        margin-bottom: 14px;
    }
    .ts-header .ts-btn-secondary {
        width: 100%;
    }
    #page-training-studio h2 {
        font-size: 1.3em;
    }

    /* Card panels — tighter padding on mobile */
    .ts-card {
        padding: 12px;
        border-radius: 8px;
        margin-bottom: 12px;
    }

    /* Upload zone — more compact, bigger tap target */
    .ts-upload-zone {
        padding: 20px 12px;
        min-height: 80px;
    }
    .ts-uploaded-file {
        padding: 8px 10px;
    }

    /* Folder input stacks vertically */
    .ts-folder-input {
        flex-direction: column;
    }
    .ts-folder-input input {
        font-size: 0.9em;
        padding: 10px 12px;
    }
    .ts-folder-input button {
        padding: 10px 16px;
        font-size: 0.9em;
        min-height: 44px;
    }

    /* Settings grid → single column */
    .ts-settings-panel {
        grid-template-columns: 1fr;
        gap: 12px;
    }
    /* Larger touch targets for inputs/selects */
    .ts-setting-group select, .ts-setting-group input[type="text"] {
        padding: 10px 12px;
        font-size: 0.9em;
        min-height: 44px;
    }
    .ts-setting-group label {
        font-size: 0.9em;
    }
    /* Range slider — larger thumb for touch */
    .ts-setting-group input[type="range"] {
        height: 32px;
        -webkit-appearance: none;
        appearance: none;
    }
    /* Checkbox touch target */
    .ts-setting-group input[type="checkbox"] {
        width: 22px;
        height: 22px;
    }

    /* Tree indentation smaller on mobile */
    .ts-tree .ts-tree-children {
        padding-left: 12px;
    }
    .ts-tree-name {
        font-size: 0.8em;
        word-break: break-word;
    }
    .ts-tree-row {
        padding: 6px 4px;
        gap: 4px;
        min-height: 40px;
    }
    .ts-tree-check {
        width: 20px;
        height: 20px;
    }

    /* Select actions wrap */
    .ts-select-actions {
        flex-wrap: wrap;
    }
    .ts-select-actions button {
        min-height: 36px;
        padding: 6px 14px;
    }

    /* Script editor cards */
    .ts-section-body {
        flex-direction: column;
    }
    .ts-slide-preview {
        width: 100%;
        height: auto;
        max-height: 200px;
    }
    .ts-script-textarea {
        min-height: 100px;
        font-size: 0.85em;
        width: 100%;
        box-sizing: border-box;
    }
    .ts-section-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 4px;
    }
    .ts-section-title {
        font-size: 0.9em;
    }
    .ts-section-actions {
        justify-content: stretch;
    }
    .ts-section-actions button {
        flex: 1;
        text-align: center;
        min-height: 44px;
        font-size: 0.9em;
    }

    /* Segment video/audio — full width on mobile */
    .ts-segment-video video,
    .ts-segment-audio audio {
        max-width: 100%;
    }

    /* Action buttons stack */
    .ts-actions {
        flex-direction: column;
        gap: 8px;
    }
    .ts-btn-primary, .ts-btn-secondary {
        width: 100%;
        text-align: center;
        padding: 14px 16px;
        min-height: 48px;
        font-size: 0.95em;
    }
    /* Clear All button — full width on mobile, no margin trick */
    #ts-clear-btn {
        margin-left: 0 !important;
    }

    /* Progress panel */
    .ts-progress-panel {
        padding: 12px 14px;
        margin: 12px 0;
        border-radius: 8px;
    }
    .ts-progress-track {
        height: 32px;
    }
    .ts-progress-pct {
        font-size: 0.85em;
    }
    .ts-progress-stage {
        font-size: 0.8em;
    }
    .ts-progress-message {
        font-size: 0.8em;
    }
    /* Stage dots — scrollable row if too many */
    .ts-stages {
        gap: 4px;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        flex-wrap: nowrap;
        padding-bottom: 4px;
    }
    .ts-stage-dot {
        font-size: 0.65em;
        min-width: 48px;
        padding: 0 4px;
        white-space: nowrap;
    }

    /* Per-file progress cards */
    .ts-file-progress {
        padding: 10px 12px;
        margin-bottom: 12px;
    }
    .ts-file-label {
        font-size: 0.85em;
    }
    .ts-file-stages {
        gap: 10px;
        font-size: 0.75em;
    }

    /* Pronunciation guide */
    #ts-pronunciation-body > div:last-child {
        flex-direction: column;
        gap: 6px;
    }
    #ts-pronunciation-body input {
        min-height: 40px;
        font-size: 0.9em !important;
    }

    /* Completion */
    .ts-complete-panel {
        padding: 20px 12px;
    }
    .ts-complete-title {
        font-size: 1.1em;
    }
    /* Video player responsive */
    #ts-video-player {
        max-width: 100% !important;
        width: 100% !important;
        border-radius: 8px !important;
    }
    .ts-complete-panel .ts-actions a,
    .ts-complete-panel .ts-actions button {
        font-size: 0.9em;
        padding: 12px 14px;
    }

    /* Multi-file result videos + actions */
    .ts-result-video {
        max-width: 100%;
    }
    .ts-result-actions {
        flex-direction: column;
    }
    .ts-result-link {
        width: 100%;
        text-align: center;
        padding: 12px 14px;
        box-sizing: border-box;
    }

    /* Project history */
    .ts-project-row {
        flex-wrap: wrap;
        gap: 6px;
        padding: 10px 12px;
        min-height: 44px;
    }
    .ts-project-title {
        width: 100%;
        font-size: 0.9em;
    }

    /* Voice preview layout */
    #ts-voice-preview {
        min-height: 40px;
        padding: 8px 14px !important;
    }
    #ts-voice-audio {
        width: 100%;
    }
}

/* Small phones */
@media (max-width: 400px) {
    .ts-container {
        padding: 8px;
    }
    .ts-tree .ts-tree-children {
        padding-left: 8px;
    }
    .ts-tree-name {
        font-size: 0.75em;
    }
    .ts-setting-group select, .ts-setting-group input {
        font-size: 0.85em;
        padding: 8px 8px;
    }
    .ts-stage-dot {
        font-size: 0.6em;
        min-width: 40px;
    }
    .ts-progress-track {
        height: 28px;
    }
    .ts-btn-primary, .ts-btn-secondary {
        padding: 12px 12px;
        font-size: 0.9em;
    }
    .ts-section-card {
        padding: 12px;
        margin-bottom: 12px;
    }
    .ts-upload-zone {
        padding: 16px 8px;
    }
}

/* Pronunciation play button spinner */
.ts-pron-spinner {
    display: inline-block;
    width: 12px;
    height: 12px;
    border: 2px solid rgba(255,255,255,0.2);
    border-top-color: #ffa726;
    border-radius: 50%;
    animation: ts-spin 0.6s linear infinite;
    vertical-align: middle;
}
@keyframes ts-spin {
    to { transform: rotate(360deg); }
}

/* Collapsible scripts section */
.ts-script-editor #ts-scripts-collapsible {
    display: none;
}
.ts-script-editor.ts-scripts-expanded #ts-scripts-collapsible {
    display: block;
}

/* Per-file progress cards */
.ts-file-progress {
    margin-bottom: 16px;
    padding: 14px 16px;
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 10px;
    background: rgba(255,255,255,0.03);
    transition: border-color 0.3s;
}
.ts-file-progress.ts-file-done {
    border-color: #00ff88;
    background: rgba(0,255,136,0.04);
}
.ts-file-label {
    font-weight: 600;
    font-size: 0.95em;
    color: #ccc;
    margin-bottom: 8px;
}
.ts-file-stages {
    display: flex;
    gap: 16px;
    margin-bottom: 8px;
    font-size: 0.8em;
}
.ts-file-stages .ts-stage-dot {
    color: #555;
    transition: color 0.2s;
}
.ts-file-stages .ts-stage-dot.active {
    color: #00d4ff;
    font-weight: 600;
}
.ts-file-stages .ts-stage-dot.done {
    color: #00ff88;
}
.ts-file-result {
    border-bottom: 1px solid rgba(255,255,255,0.08);
    padding-bottom: 20px;
    margin-bottom: 20px;
}
.ts-file-result:last-child {
    border-bottom: none;
    margin-bottom: 0;
}

/* Segment video/audio in script cards */
.ts-segment-video video {
    width: 100%;
    max-width: 640px;
    border-radius: 8px;
    background: #000;
    margin-top: 8px;
}
.ts-segment-audio audio {
    width: 100%;
    max-width: 640px;
    margin-top: 8px;
}

/* Multi-file result video/actions */
.ts-result-video {
    width: 100%;
    max-width: 700px;
    border-radius: 8px;
    margin: 8px 0;
    background: #000;
}
.ts-result-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin: 8px 0 0;
}
.ts-result-link {
    display: inline-block;
    padding: 8px 16px;
    border-radius: 6px;
    text-decoration: none;
}
