* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary-color: #4f46e5;
    --primary-hover: #4338ca;
    --secondary-color: #6b7280;
    --success-color: #10b981;
    --error-color: #ef4444;
    --bg-color: #f9fafb;
    --card-bg: #ffffff;
    --text-primary: #111827;
    --text-secondary: #6b7280;
    --border-color: #e5e7eb;
    --shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
}

body {
    font-family: 'Assistant', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    padding: 20px;
    color: var(--text-primary);
    direction: rtl;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
}

header {
    text-align: center;
    color: white;
    margin-bottom: 40px;
    padding: 30px 0;
}

header h1 {
    font-size: 3rem;
    font-weight: 700;
    margin-bottom: 10px;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
}

.subtitle {
    font-size: 1.2rem;
    opacity: 0.95;
}

/* Live / Staging environment */
.app-main-header .app-header-row {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px 24px;
    text-align: right;
}

.app-main-header .app-header-row > div:first-child {
    flex: 1 1 240px;
}

.api-unreachable-banner {
    position: sticky;
    top: 0;
    z-index: 10000;
    width: 100%;
    padding: 12px 16px;
    text-align: center;
    font-weight: 600;
    font-size: 0.9rem;
    line-height: 1.45;
    color: #fff;
    background: #b91c1c;
    border-bottom: 3px solid #7f1d1d;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.staging-env-banner {
    position: sticky;
    top: 0;
    z-index: 9999;
    width: 100%;
    padding: 10px 16px;
    text-align: center;
    font-weight: 700;
    font-size: 0.95rem;
    letter-spacing: 0.04em;
    color: #1c1917;
    background: linear-gradient(90deg, #fbbf24 0%, #f97316 50%, #fbbf24 100%);
    border-bottom: 3px solid #b45309;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.env-toggle {
    display: inline-flex;
    border-radius: 10px;
    overflow: hidden;
    border: 2px solid rgba(255, 255, 255, 0.85);
    background: rgba(0, 0, 0, 0.2);
    flex-shrink: 0;
}

.env-toggle--settings {
    border-color: var(--border-color);
    background: var(--bg-color);
}

.env-toggle-btn {
    padding: 8px 18px;
    font-family: inherit;
    font-size: 0.9rem;
    font-weight: 700;
    border: none;
    cursor: pointer;
    background: transparent;
    color: rgba(255, 255, 255, 0.9);
    transition: background 0.15s, color 0.15s;
}

.env-toggle--settings .env-toggle-btn {
    color: var(--text-secondary);
}

.env-toggle-btn--active {
    background: #fff;
    color: var(--primary-color);
}

.env-toggle--settings .env-toggle-btn--active {
    background: var(--primary-color);
    color: #fff;
}

.env-toggle-btn:hover:not(.env-toggle-btn--active) {
    background: rgba(255, 255, 255, 0.15);
}

/* Staging visual theme */
html[data-env="staging"] {
    --primary-color: #d97706;
    --primary-hover: #b45309;
    --bg-color: #fffbeb;
    --card-bg: #fffef7;
    --border-color: #fcd34d;
}

html[data-env="staging"] body {
    background: linear-gradient(135deg, #f59e0b 0%, #ea580c 45%, #78716c 100%);
}

html[data-env="staging"] .section {
    border: 2px solid #fbbf24;
}

html[data-env="staging"] header h1::after {
    content: ' · STAGING';
    font-size: 0.45em;
    font-weight: 600;
    opacity: 0.95;
    vertical-align: middle;
}

html[data-env="staging"] .env-toggle-btn--active {
    background: #1c1917;
    color: #fbbf24;
}

.section {
    background: var(--card-bg);
    border-radius: 12px;
    padding: 30px;
    box-shadow: var(--shadow-lg);
    margin-bottom: 30px;
}

/* Auth Section */
.auth-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 30px;
}

.auth-box {
    padding: 25px;
    border: 2px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-color);
}

.auth-box h2 {
    margin-bottom: 20px;
    color: var(--primary-color);
    font-size: 1.5rem;
}

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 8px;
    font-weight: 600;
    color: var(--text-primary);
}

.form-group input {
    width: 100%;
    padding: 12px;
    border: 2px solid var(--border-color);
    border-radius: 6px;
    font-size: 1rem;
    transition: border-color 0.3s;
    font-family: inherit;
}

.form-group input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

/* Buttons */
.btn {
    padding: 12px 24px;
    border: none;
    border-radius: 6px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    font-family: inherit;
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.btn-primary {
    background: var(--primary-color);
    color: white;
    width: 100%;
    justify-content: center;
}

.btn-primary:hover:not(:disabled) {
    background: var(--primary-hover);
    transform: translateY(-1px);
    box-shadow: var(--shadow-lg);
}

.btn-secondary {
    background: var(--secondary-color);
    color: white;
}

.btn-secondary:hover {
    background: #4b5563;
}

.btn-small {
    padding: 8px 16px;
    font-size: 0.875rem;
}

.btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* Messages */
.message {
    margin-top: 15px;
    padding: 12px;
    border-radius: 6px;
    font-size: 0.9rem;
    display: none;
}

.message.success {
    background: #d1fae5;
    color: #065f46;
    border: 1px solid #10b981;
    display: block;
}

.message.error {
    background: #fee2e2;
    color: #991b1b;
    border: 1px solid #ef4444;
    display: block;
}

/* User Info */
.user-info {
    background: var(--bg-color);
    padding: 15px 20px;
    border-radius: 8px;
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.user-info span {
    font-weight: 600;
    color: var(--text-primary);
}

.user-info > div {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
}

.staging-header-nav {
    display: inline-flex;
    flex-wrap: nowrap;
    align-items: center;
    gap: 8px;
}

.staging-header-nav .btn {
    width: auto;
    flex: 0 0 auto;
    margin: 0;
}

.staging-header-nav .staging-nav-btn {
    min-width: 5.25rem;
    justify-content: center;
    background: var(--secondary-color);
    color: white;
}

.staging-header-nav .staging-nav-btn.is-active {
    background: var(--primary-color);
    color: white;
}

.staging-header-nav .staging-nav-btn:hover {
    filter: brightness(0.95);
}

/* Cards */
.card {
    background: var(--card-bg);
    border-radius: 12px;
    padding: 25px;
    box-shadow: var(--shadow);
    margin-bottom: 20px;
}

.card h2 {
    margin-bottom: 20px;
    color: var(--text-primary);
    font-size: 1.5rem;
}

/* Books List */
.books-list {
    margin-top: 20px;
}

.book-item {
    padding: 20px;
    border: 2px solid var(--border-color);
    border-radius: 8px;
    margin-bottom: 15px;
    cursor: pointer;
    transition: all 0.3s;
    background: var(--bg-color);
}

.book-item:hover {
    border-color: var(--primary-color);
    transform: translateX(-5px);
    box-shadow: var(--shadow);
}

.book-item h3 {
    color: var(--primary-color);
    margin-bottom: 10px;
}

.book-item .meta {
    color: var(--text-secondary);
    font-size: 0.9rem;
    margin-top: 8px;
}

.empty-state {
    text-align: center;
    color: var(--text-secondary);
    padding: 40px;
    font-size: 1.1rem;
}

/* Segments List */
.segments-list {
    margin-top: 20px;
}

.segment-item {
    padding: 20px;
    border: 2px solid var(--border-color);
    border-radius: 8px;
    margin-bottom: 15px;
    background: var(--bg-color);
    transition: all 0.3s ease;
}

.segment-item:hover {
    border-color: var(--primary-color);
    box-shadow: var(--shadow-lg);
}

.segment-item.expanded {
    border-color: var(--primary-color);
    box-shadow: var(--shadow-lg);
    background: var(--card-bg);
}

.segment-item h4 {
    color: var(--primary-color);
    margin-bottom: 10px;
}

/* LangGraph agent segment status (staging) */
.agent-seg-status-badge {
    font-size: 0.78rem;
    font-weight: 600;
    padding: 2px 8px;
    border-radius: 4px;
    margin-inline-start: 4px;
}
.agent-seg-status-badge.agent-st-ok,
.agent-seg-badge.agent-st-ok { background: rgba(34, 197, 94, 0.2); color: #166534; }
.agent-seg-status-badge.agent-st-running,
.agent-seg-badge.agent-st-running { background: rgba(59, 130, 246, 0.25); color: #1e40af; }
.agent-seg-status-badge.agent-st-qa,
.agent-seg-badge.agent-st-qa { background: rgba(168, 85, 247, 0.2); color: #6b21a8; }
.agent-seg-status-badge.agent-st-pending,
.agent-seg-badge.agent-st-pending { background: rgba(148, 163, 184, 0.25); color: var(--text-secondary); }
.agent-seg-status-badge.agent-st-fail,
.agent-seg-badge.agent-st-fail { background: rgba(239, 68, 68, 0.2); color: #991b1b; }
.agent-seg-status-badge.agent-st-human,
.agent-seg-badge.agent-st-human { background: rgba(245, 158, 11, 0.25); color: #92400e; }
.agent-seg-status-badge.agent-st-audio,
.agent-seg-badge.agent-st-audio { background: rgba(14, 165, 233, 0.2); color: #0c4a6e; }
.agent-segments-table td { padding: 4px 6px; border-bottom: 1px solid var(--border-color); vertical-align: top; }
.agent-segments-table .agent-seg-preview { color: var(--text-secondary); max-width: 55%; }
.agent-seg-row.agent-st-running,
tr.agent-seg-row-current { background: rgba(59, 130, 246, 0.08); }

/* LangGraph pipeline diagrams (staging agent) */
.agent-pipeline-flow {
    display: flex;
    flex-wrap: wrap;
    align-items: stretch;
    gap: 4px;
    margin: 8px 0;
    direction: ltr;
    justify-content: flex-start;
}
.agent-pipe-step {
    display: flex;
    flex-direction: column;
    min-width: 72px;
    max-width: 110px;
    padding: 6px 8px;
    border-radius: 8px;
    border: 1px solid var(--border-color);
    background: var(--card-bg, #fff);
    font-size: 0.72rem;
    line-height: 1.25;
    text-align: center;
}
.agent-pipe-step .agent-pipe-label { font-weight: 600; color: var(--text-color); }
.agent-pipe-step .agent-pipe-model { color: var(--text-secondary); font-size: 0.68rem; margin-top: 2px; }
.agent-pipe-step .agent-pipe-result { margin-top: 4px; font-size: 0.65rem; color: var(--text-secondary); word-break: break-word; }
.agent-pipe-step.agent-pipe-ok { border-color: rgba(34, 197, 94, 0.5); background: rgba(34, 197, 94, 0.08); }
.agent-pipe-step.agent-pipe-fail { border-color: rgba(239, 68, 68, 0.55); background: rgba(239, 68, 68, 0.1); }
.agent-pipe-step.agent-pipe-warn { border-color: rgba(245, 158, 11, 0.55); background: rgba(245, 158, 11, 0.1); }
.agent-pipe-step.agent-pipe-running { border-color: rgba(59, 130, 246, 0.55); background: rgba(59, 130, 246, 0.12); }
.agent-pipe-step.agent-pipe-pending { opacity: 0.55; }
.agent-pipe-step.agent-pipe-skip { opacity: 0.45; border-style: dashed; }

/* ── Agent execution log viewer ─────────────────────────────────── */
.agent-log-section { margin-top: 14px; }
.agent-log-toggle {
    background: none; border: 1px solid var(--border-color); border-radius: 6px;
    padding: 5px 12px; font-size: 0.82rem; cursor: pointer; color: var(--text-secondary);
    display: flex; align-items: center; gap: 6px;
}
.agent-log-toggle:hover { background: var(--hover-bg); color: var(--text-color); }
.agent-log-body {
    margin-top: 8px; border: 1px solid var(--border-color); border-radius: 8px;
    max-height: 520px; overflow-y: auto; font-size: 0.8rem;
}
.agent-log-entry {
    display: flex; align-items: flex-start; gap: 10px;
    padding: 7px 12px; border-bottom: 1px solid var(--border-color);
    cursor: pointer;
}
.agent-log-entry:last-child { border-bottom: none; }
.agent-log-entry:hover { background: var(--hover-bg); }
.agent-log-entry.expanded { background: var(--hover-bg); }
.agent-log-icon { font-size: 1rem; flex-shrink: 0; line-height: 1.4; }
.agent-log-main { flex: 1; min-width: 0; }
.agent-log-node { font-weight: 600; color: var(--text-color); font-size: 0.78rem; }
.agent-log-summary { color: var(--text-secondary); margin-top: 1px; word-break: break-word; }
.agent-log-ts { font-size: 0.7rem; color: var(--text-secondary); white-space: nowrap; flex-shrink: 0; }
.agent-log-details {
    margin: 6px 0 4px 28px; background: var(--code-bg, rgba(0,0,0,0.04));
    border-radius: 4px; padding: 8px; font-family: monospace;
    font-size: 0.72rem; white-space: pre-wrap; word-break: break-all;
    border: 1px solid var(--border-color);
}
.agent-log-entry-segment .agent-log-node { color: var(--secondary-color); }
.agent-log-entry-ok .agent-log-icon::before { content: '✓'; color: var(--success-color); }
.agent-log-entry-fail .agent-log-icon::before { content: '✗'; color: var(--error-color); }
.agent-log-entry-node .agent-log-icon::before { content: '▸'; color: var(--text-secondary); }
.agent-pipe-arrow { align-self: center; color: var(--text-secondary); font-size: 0.85rem; padding: 0 2px; }
.agent-pipeline-book-wrap {
    margin-top: 10px;
    padding: 10px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: rgba(148, 163, 184, 0.06);
}
.agent-pipeline-book-wrap > p { margin: 0 0 8px; font-size: 0.88rem; }

.agent-whisper-details {
    margin-top: 8px;
    padding: 8px 10px;
    border-radius: 6px;
    border: 1px solid var(--border-color);
    background: rgba(59, 130, 246, 0.06);
    font-size: 0.8rem;
}
.agent-whisper-label { margin: 6px 0 4px; font-size: 0.78rem; color: var(--text-secondary); }
.agent-whisper-label:first-child { margin-top: 0; }
.agent-whisper-transcript,
.agent-whisper-source {
    padding: 6px 8px;
    border-radius: 4px;
    background: var(--card-bg, rgba(0, 0, 0, 0.04));
    line-height: 1.65;
    word-break: break-word;
    white-space: pre-wrap;
    max-height: 140px;
    overflow-y: auto;
}
.agent-whisper-source.hg-text-collapsed,
.agent-whisper-transcript.hg-text-collapsed {
    max-height: 140px;
    overflow-y: hidden;
}
.agent-whisper-source.hg-text-expanded,
.agent-whisper-transcript.hg-text-expanded {
    max-height: none;
    overflow: visible;
}
.hg-text-expand-btn {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    margin: 0 0 6px;
    padding: 2px 0;
    border: none;
    background: transparent;
    color: var(--text-secondary, #6b7280);
    font-size: 0.8rem;
    font-weight: 500;
    cursor: pointer;
}
.hg-text-expand-btn:hover,
.hg-text-expand-btn:focus {
    color: var(--primary-color, #4f46e5);
    outline: none;
}
.agent-whisper-word-mismatch {
    cursor: pointer;
    color: #b91c1c;
    background: rgba(220, 38, 38, 0.14);
    border-radius: 3px;
    padding: 1px 4px;
    box-shadow: 0 0 0 1px rgba(220, 38, 38, 0.28);
    font-weight: 600;
    transition: background-color 0.15s;
}
.agent-whisper-word-mismatch:hover,
.agent-whisper-word-mismatch:focus {
    background: rgba(220, 38, 38, 0.24);
    outline: none;
}
.agent-whisper-word-mismatch.agent-whisper-word-open {
    background: rgba(220, 38, 38, 0.32);
    box-shadow: 0 0 0 2px rgba(220, 38, 38, 0.45);
}
.agent-whisper-word-replaced {
    color: #92400e;
    background: rgba(234, 179, 8, 0.22);
    border-radius: 3px;
    padding: 1px 4px;
    box-shadow: 0 0 0 1px rgba(202, 138, 4, 0.35);
    font-weight: 600;
}
.agent-qa-whisper-summary {
    margin: 0 0 10px;
    font-size: 0.9rem;
    line-height: 1.5;
}
.agent-qa-whisper-section {
    margin: 10px 0;
    padding: 8px 10px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background: rgba(148, 163, 184, 0.06);
}
.agent-qa-whisper-section h4 {
    margin: 0 0 6px;
    font-size: 0.82rem;
    color: var(--text-secondary);
}
.agent-qa-whisper-variant-row {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: 8px;
}
.agent-qa-whisper-variant-row:last-child { margin-bottom: 0; }
.admin-metrics-card canvas {
    width: 100% !important;
    max-height: 220px;
    margin-top: 8px;
}
.agent-whisper-stats { margin: 6px 0 0; font-size: 0.72rem; color: var(--text-secondary); }
.agent-whisper-err { margin: 0 0 6px; color: #dc2626; font-size: 0.78rem; }

/* Homograph reading picker — live preview blink when operator changes a reading */
@keyframes hg-reading-pulse {
    0%, 100% {
        opacity: 1;
        box-shadow: 0 0 0 0 rgba(124, 58, 237, 0.35);
    }
    50% {
        opacity: 0.45;
        box-shadow: 0 0 8px 2px rgba(124, 58, 237, 0.55);
    }
}
mark.hg-word-mark {
    position: relative;
    padding: 2px 5px;
    border-radius: 4px;
    font-weight: 700;
    cursor: pointer;
    transition: box-shadow 0.15s ease;
    display: inline;
    white-space: normal;
    touch-action: manipulation;
    -webkit-touch-callout: none;
    user-select: none;
    /* Always visible even if inline background is stripped / pale */
    background-color: #facc15 !important;
    box-shadow: inset 0 0 0 1px rgba(161, 98, 7, 0.45);
    color: inherit;
}
mark.hg-word-mark .hg-word-num {
    font-size: 0.65em;
    font-weight: 900;
    margin-inline-start: 2px;
    color: #92400e;
}
mark.hg-word-mark.hg-changed .hg-surface {
    animation: hg-reading-pulse 1s ease-in-out infinite;
}
mark.hg-word-mark .hg-surface {
    font-weight: 700;
}
mark.hg-word-mark.hg-has-niqqud .hg-surface {
    color: var(--primary-color, #4f46e5);
}
mark.hg-word-mark.hg-word-open {
    box-shadow: 0 0 0 2px rgba(79, 70, 229, 0.45), inset 0 0 0 1px rgba(161, 98, 7, 0.45);
    z-index: 2;
    background-color: #fde047 !important;
}
.hg-pending-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin: 0 0 8px;
}
.hg-pending-chip {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 10px;
    border-radius: 999px;
    background: #facc15 !important;
    border: 1px solid rgba(161, 98, 7, 0.45);
    font-weight: 700;
    cursor: pointer;
    font-size: 0.88rem;
}
.hg-pending-chip:hover,
.hg-pending-chip:focus {
    background: #fde047 !important;
    outline: none;
}
.hg-word-controls {
    display: none;
    position: absolute;
    top: calc(100% + 4px);
    right: 0;
    z-index: 20;
    min-width: 11rem;
    padding: 6px 8px;
    background: var(--card-bg, #fff);
    border: 1px solid var(--border-color, #e5e7eb);
    border-radius: 8px;
    box-shadow: var(--shadow-lg, 0 8px 20px rgba(0, 0, 0, 0.12));
    direction: rtl;
    text-align: right;
    white-space: nowrap;
}
mark.hg-word-mark.hg-word-open .hg-word-controls {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
}
.hg-inline-select {
    width: 100%;
    font-size: 0.88rem;
    direction: rtl;
    padding: 4px 6px;
    border-radius: 4px;
    border: 1px solid var(--border-color, #d1d5db);
    font-family: inherit;
}
.hg-confirm-label {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 0.78rem;
    color: var(--text-secondary);
    cursor: pointer;
}
.hg-confirm-label.hg-confirm-hidden {
    display: none !important;
}
.hg-reason {
    align-self: flex-start;
    font-size: 0.82rem;
    color: var(--text-secondary);
    cursor: help;
}
.hg-annotated-text {
    line-height: 2.1;
}

.agent-seg-pipeline-cell { padding: 8px 4px !important; min-width: 280px; }
.agent-seg-pipeline-wrap { margin-top: 6px; }
.agent-seg-pipeline-wrap > p { margin: 0 0 4px; font-size: 0.78rem; color: var(--text-secondary); }

.segment-item .text-preview {
    color: var(--text-secondary);
    margin: 10px 0;
    font-size: 0.9rem;
    line-height: 1.6;
}

.segment-item .text-section {
    margin-bottom: 10px;
}

/* Word Selection Styles */
.word-container {
    display: inline;
    word-wrap: break-word;
    line-height: 1.8;
}

.word {
    cursor: pointer;
    padding: 2px 4px;
    border-radius: 3px;
    transition: background-color 0.2s, color 0.2s;
    display: inline-block;
    margin: 0 1px;
}

.word:hover {
    background-color: rgba(79, 70, 229, 0.15);
}

.word.selected {
    background-color: var(--primary-color);
    color: white;
    font-weight: 600;
}

.word.word-narration-error {
    box-shadow: inset 0 -2px 0 #c0392b;
}

.narration-error-menu {
    position: fixed;
    z-index: 4000;
    min-width: 220px;
    max-width: 320px;
    padding: 6px;
    background: #fff;
    color: #171719;
    border: 1px solid rgba(23, 23, 25, 0.12);
    border-radius: 10px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.16);
    direction: rtl;
}

.narration-error-menu button {
    display: block;
    width: 100%;
    margin: 0;
    padding: 8px 10px;
    border: 0;
    border-radius: 6px;
    background: transparent;
    color: inherit;
    font: inherit;
    text-align: right;
    cursor: pointer;
}

.narration-error-menu button:hover,
.narration-error-menu button.is-current {
    background: rgba(192, 57, 43, 0.12);
}

.narration-error-summary {
    margin: 0 0 14px;
    padding: 10px 12px;
    border-radius: 8px;
    background: rgba(192, 57, 43, 0.06);
    font-size: 0.88rem;
    line-height: 1.55;
}

.narration-error-summary p {
    margin: 0 0 4px;
}

.segment-narration-error-line {
    margin: 8px 0 0;
    font-size: 0.88rem;
    color: var(--text-color);
}

.word.is-reading {
    background-color: #ffd54f;
    color: #1f2937;
    box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.05);
    transition: background-color 80ms linear, color 80ms linear;
}

.word.is-reading.selected {
    background-color: var(--primary-color);
    color: white;
    box-shadow: 0 0 0 2px #ffd54f inset;
}

/* Word is auto-replaced by pronunciation lexicon (override/auto_replace). */
.word.word-lexicon-auto-replace:not(.selected):not(.is-reading) {
    background-color: rgba(185, 28, 28, 0.14);
    box-shadow: 0 0 0 1px rgba(185, 28, 28, 0.18);
}

.word.word-lexicon-auto-replace:hover:not(.selected):not(.is-reading) {
    background-color: rgba(185, 28, 28, 0.22);
}

/* מילה שהוחלף קליפ מילון באודיו הקטע (לפי lexicon_auto_replacement_events) — בולט בטקסט המקורי */
.word.word-lexicon-segment-replaced:not(.selected):not(.is-reading) {
    background-color: rgba(124, 58, 237, 0.16);
    box-shadow: 0 0 0 2px rgba(109, 40, 217, 0.38);
    border-radius: 3px;
}

.word.word-lexicon-segment-replaced:hover:not(.selected):not(.is-reading) {
    background-color: rgba(124, 58, 237, 0.24);
}

.word.word-lexicon-segment-replaced.is-reading:not(.selected) {
    box-shadow: 0 0 0 2px rgba(109, 40, 217, 0.55);
}

/* Lexicon replacement panel + word actions: visible only when segment card is expanded */
.segment-item:not(.expanded) .segment-expanded-only {
    display: none !important;
}

.segment-item.expanded .segment-expanded-only {
    display: block;
}

.word-audio {
    display: inline-block;
    margin-right: 5px;
    vertical-align: middle;
}

.word-audio audio {
    width: 120px;
    height: 25px;
    vertical-align: middle;
}

.word-actions {
    margin-top: 10px;
    padding: 10px;
    background-color: rgba(79, 70, 229, 0.1);
    border-radius: 6px;
    border: 1px solid var(--primary-color);
}

.word-context-menu {
    position: fixed;
    z-index: 1000;
    background: var(--card-bg, #fff);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    padding: 6px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    display: flex;
    gap: 6px;
}

.replacements-list .replacement-item {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}

/* Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}

/* Nested dialogs (e.g. «סיבת ההחלפה» over «החלפות למילה») must stack above .modal */
.modal.modal-nested {
    z-index: 11000;
}

.modal.hidden {
    display: none;
}

.modal-content {
    background: white;
    border-radius: 10px;
    padding: 20px;
    width: min(600px, 90%);
    box-shadow: var(--shadow-lg);
}

.modal-content h3 {
    margin-bottom: 15px;
    color: var(--primary-color);
}

.modal .form-group {
    margin-bottom: 15px;
}

.modal textarea {
    width: 100%;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    padding: 10px;
    resize: vertical;
    font-family: inherit;
}

.edit-original-display {
    width: 100%;
    min-height: 120px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    padding: 10px;
    font-family: inherit;
    background-color: #f3f4f6;
    color: var(--text-secondary);
    white-space: pre-wrap;
}

.modal-actions {
    display: flex;
    gap: 10px;
    margin-top: 10px;
}

.audio-player {
    margin-top: 15px;
    width: 100%;
}

.audio-editor-modal-content {
    width: min(1200px, 95vw);
    max-height: 92vh;
    overflow: auto;
}

.audio-editor-standalone-body {
    min-height: 100vh;
    background: var(--bg-color, #f9fafb);
}

.audio-editor-standalone-container {
    max-width: 1240px;
}

.audio-editor-standalone-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 16px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}

.audio-editor-page-root {
    width: 100%;
    max-height: none;
}

.audio-editor-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
}

.audio-editor-grid {
    display: grid;
    grid-template-columns: 1fr 1.4fr;
    gap: 14px;
}

.audio-editor-grid-single-graph {
    grid-template-columns: 1fr;
}

@media (min-width: 900px) {
    .audio-editor-grid-single-graph {
        grid-template-columns: minmax(240px, 0.9fr) minmax(0, 1.5fr);
    }
}

/* ללא גרף: עמודה אחתה — טקסט מלא רוחב, מתחתיו פאנל נגינה מלא רוחב (כמו הקלטות) */
#audioEditorPageRoot.audio-editor-no-graphs .audio-editor-grid-single-graph {
    grid-template-columns: 1fr !important;
}

#audioEditorPageRoot.audio-editor-no-graphs #audioEditorGraphsColumn {
    display: none !important;
}

#audioEditorPageRoot.audio-editor-no-graphs .audio-editor-text-panel {
    min-width: 0;
}

.audio-editor-segment-playback-panel {
    margin-top: 14px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 10px 12px;
    background: #fafafa;
}

.audio-editor-graphs-column {
    min-width: 0;
}

#audioEditorPageRoot.audio-editor-no-graphs .audio-editor-playback-actions {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
}

#audioEditorPageRoot.audio-editor-no-graphs .audio-editor-play-segment-row {
    display: block;
}

#audioEditorPageRoot.audio-editor-no-graphs .audio-editor-play-segment-row .btn {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
}

#audioEditorPageRoot.audio-editor-no-graphs .audio-editor-main-player {
    width: 100%;
    max-width: 100%;
    margin-top: 8px;
    display: block;
    box-sizing: border-box;
}

#audioEditorPageRoot.audio-editor-no-graphs .audio-editor-selection-info {
    margin-top: 8px;
}

.audio-editor-text-panel,
.audio-editor-wave-panel {
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 10px;
    background: #fafafa;
}

.audio-editor-main-player {
    margin-top: 12px;
}

.audio-editor-selection-info {
    margin-top: 8px;
    color: var(--text-secondary);
    font-size: 0.9rem;
}

.audio-editor-lex-auto-panel {
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 12px 14px;
    margin-top: 14px;
    background: rgba(185, 28, 28, 0.05);
    border-color: rgba(185, 28, 28, 0.22);
}

.audio-editor-lex-auto-title {
    margin: 0 0 10px;
    font-weight: 600;
    font-size: 0.95rem;
}

.audio-editor-lex-auto-empty {
    margin: 0;
    font-size: 0.88rem;
    color: var(--text-secondary);
}

.audio-editor-lex-auto-list {
    margin: 0;
    padding-right: 20px;
    list-style-position: outside;
}

.audio-editor-lex-auto-item {
    margin: 10px 0;
    padding: 8px 10px;
    border-radius: 6px;
    background: rgba(255, 255, 255, 0.65);
    border: 1px solid rgba(185, 28, 28, 0.15);
    font-size: 0.9rem;
}

.audio-editor-lex-auto-when {
    display: block;
    margin-top: 4px;
    font-size: 0.78rem;
    color: var(--text-secondary);
}

.audio-editor-words {
    min-height: 180px;
    max-height: 420px;
    overflow: auto;
    line-height: 1.9;
}

.audio-editor-word {
    display: inline-block;
    margin: 0 2px;
    padding: 2px 5px;
    border-radius: 4px;
    cursor: pointer;
}

.audio-editor-word:hover {
    background: rgba(79, 70, 229, 0.14);
}

.audio-editor-word.selected {
    background: var(--primary-color);
    color: white;
}

.audio-editor-word.is-reading:not(.selected) {
    background-color: #ffd54f !important;
    color: #1f2937 !important;
    box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.05);
    transition: background-color 80ms linear, color 80ms linear;
}

.audio-editor-word.is-reading.selected {
    background-color: var(--primary-color);
    color: white;
    box-shadow: 0 0 0 2px #ffd54f inset;
}

.audio-editor-word.audio-editor-word-repl-toggle {
    cursor: pointer;
}

.audio-editor-word.audio-editor-word-lexicon-ambiguous:not(.selected) {
    background: rgba(217, 119, 6, 0.28);
    border-bottom: 2px solid #d97706;
}

.audio-editor-word.audio-editor-word-lexicon-picked:not(.selected) {
    box-shadow: 0 0 0 2px rgba(13, 148, 136, 0.45);
}

/* Lexicon policy: word should be auto-replaced (override) */
.audio-editor-word.audio-editor-word-lexicon-override:not(.selected) {
    background: rgba(190, 18, 60, 0.12);
    border-bottom: 2px solid rgba(190, 18, 60, 0.75);
}

@keyframes audioEditorApprovePulse {
    0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(59, 130, 246, 0.35); }
    50% { transform: scale(1.04); box-shadow: 0 0 0 6px rgba(59, 130, 246, 0.08); }
    100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(59, 130, 246, 0.0); }
}

.audio-editor-word.audio-editor-word-approve-active {
    background: rgba(59, 130, 246, 0.22) !important;
    border: 1px solid rgba(59, 130, 246, 0.5);
    animation: audioEditorApprovePulse 0.55s ease-in-out infinite;
}

.audio-editor-word.audio-editor-word-approve-added {
    background: rgba(22, 163, 74, 0.22) !important;
    border: 1px solid rgba(22, 163, 74, 0.45);
}

.audio-editor-word.audio-editor-word-approve-already_matched {
    background: rgba(22, 163, 74, 0.18) !important;
    border: 1px solid rgba(22, 163, 74, 0.4);
}

.audio-editor-word.audio-editor-word-approve-skipped {
    background: rgba(220, 38, 38, 0.16) !important;
    border: 1px solid rgba(220, 38, 38, 0.36);
}

.audio-editor-word.audio-editor-word-approve-error {
    background: rgba(220, 38, 38, 0.2) !important;
    border: 1px solid rgba(220, 38, 38, 0.45);
}

.audio-editor-lexicon-picker {
    position: fixed;
    inset: 0;
    z-index: 10000;
    background: rgba(0, 0, 0, 0.45);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
}

.audio-editor-lexicon-picker.hidden {
    display: none;
}

.audio-editor-lexicon-picker-inner {
    background: var(--bg-color, #fff);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 16px;
    max-width: 440px;
    width: 100%;
    max-height: 85vh;
    overflow: auto;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.18);
}

.audio-editor-lexicon-picker-word {
    font-weight: 700;
    margin: 0 0 8px;
    font-size: 1.1rem;
}

.audio-editor-lexicon-hint {
    font-size: 0.82rem;
    color: var(--text-secondary);
    margin: 0 0 12px;
}

.audio-editor-wave-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
    margin-bottom: 8px;
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.audio-editor-wave-toolbar-label {
    font-weight: 600;
    color: var(--text-color);
}

.audio-editor-wave-toolbar-sep {
    width: 1px;
    height: 18px;
    background: var(--border-color);
    margin: 0 4px;
}

.audio-editor-wave-zoom-hint {
    margin-inline-start: 8px;
    font-size: 0.8rem;
}

.audio-editor-toggle-hint {
    margin-top: 8px;
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.audio-editor-toggle-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px;
    margin-top: 8px;
    min-height: 0;
}

.audio-editor-repl-toggle {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 2px solid rgba(0, 0, 0, 0.15);
    cursor: pointer;
    flex-shrink: 0;
    padding: 0;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}

.audio-editor-repl-toggle.off {
    opacity: 0.35;
    filter: grayscale(0.4);
}

.audio-editor-repl-toggle-label {
    font-size: 0.8rem;
    color: var(--text-secondary);
    margin-inline-end: 4px;
}

.audio-editor-wave-shell {
    position: relative;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    overflow: hidden;
    background: white;
}

#audioEditorWaveCanvas {
    width: 100%;
    height: 110px;
    display: block;
    cursor: crosshair;
    user-select: none;
    -webkit-user-select: none;
}

#audioEditorMasterLaneCanvas {
    width: 100%;
    height: 48px;
    display: block;
}

canvas.audio-editor-repl-lane-canvas {
    width: 100%;
    height: 44px;
    display: block;
}

#audioEditorWaveCanvas.audio-editor-wave-pan {
    cursor: grab;
}

.audio-editor-wave-playhead {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 2px;
    margin-left: -1px;
    background: #e11d48;
    box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.85), 0 0 4px rgba(0, 0, 0, 0.25);
    z-index: 4;
    pointer-events: none;
}

.audio-editor-wave-selection {
    position: absolute;
    top: 0;
    height: 100%;
    z-index: 2;
    background: rgba(79, 70, 229, 0.2);
    border-right: 2px solid var(--primary-color);
    border-left: 2px solid var(--primary-color);
    pointer-events: none;
}

.audio-editor-recording-panel {
    display: block;
    margin-top: 12px;
    border-top: 1px solid var(--border-color);
    padding-top: 12px;
}

.audio-editor-recordings-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px;
    row-gap: 10px;
}

.audio-editor-replacements-panel {
    margin-top: 16px;
    padding-top: 14px;
    border-top: 1px solid var(--border-color);
}

.audio-editor-replacements-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.audio-editor-replacement-row {
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 10px 12px;
    background: var(--bg-color, #fafafa);
}

.audio-editor-replacement-row-head {
    font-weight: 600;
    margin-bottom: 8px;
    word-break: break-word;
}

.audio-editor-replacement-row-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
}

.audio-editor-replacement-row .btn-small {
    white-space: nowrap;
}

.audio-editor-replacement-row-status {
    font-size: 0.82rem;
    color: var(--text-secondary);
    margin-top: 6px;
}

.audio-editor-toolbar-group {
    display: inline-flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
}

.audio-editor-voice-select {
    min-width: 200px;
    max-width: 320px;
    padding: 8px;
    border-radius: 4px;
    border: 1px solid var(--border-color);
    background: var(--bg-color);
    color: var(--text-color);
}

.audio-editor-replace-hint {
    font-size: 0.85rem;
    color: var(--text-secondary);
    white-space: nowrap;
}

.audio-editor-s2s-lexicon-row {
    flex-basis: 100%;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
    padding-top: 10px;
    margin-top: 2px;
    border-top: 1px dashed var(--border-color);
}

.audio-editor-s2s-lexicon-hint {
    display: block;
    font-size: 0.82rem;
    color: var(--text-secondary);
    line-height: 1.35;
    max-width: 640px;
}

.audio-editor-s2s-lexicon-actions {
    flex-wrap: wrap;
}

.audio-editor-lexicon-override-btn {
    background: #b91c1c;
    border-color: #991b1b;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* Loading */
.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    color: white;
}

.loading-overlay p {
    margin-top: 20px;
    font-size: 1.2rem;
}

/* Spinner */
.spinner {
    border: 3px solid rgba(255, 255, 255, 0.3);
    border-top: 3px solid white;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    animation: spin 1s linear infinite;
    display: inline-block;
}

.spinner-large {
    border: 4px solid rgba(255, 255, 255, 0.3);
    border-top: 4px solid white;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Audio progress bar */
.audio-progress-bar {
    margin-top: 12px;
}

.progress-bar-container {
    width: 100%;
    height: 8px;
    background: var(--border-color);
    border-radius: 4px;
    overflow: hidden;
}

.progress-bar-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--primary-color), var(--success-color));
    border-radius: 4px;
    transition: width 0.3s ease;
}

.progress-bar-text {
    display: block;
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-top: 4px;
}

/* Parallel TTS job visualization (הקרא קטעים) */
.audio-parallel-viz {
    margin-top: 12px;
    padding: 12px 14px;
    background: linear-gradient(145deg, #f8fafc 0%, #eef2ff 100%);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    max-height: 280px;
    overflow: hidden;
    box-shadow: var(--shadow);
}

.audio-parallel-viz-inner {
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-height: 260px;
}

.audio-parallel-viz-title {
    font-size: 0.88rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin: 0 0 4px 0;
}

.audio-parallel-rows {
    display: flex;
    flex-direction: column;
    gap: 6px;
    overflow-y: auto;
    padding-left: 4px;
}

.audio-parallel-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 8px 10px;
    border-radius: 8px;
    font-size: 0.9rem;
    transition: background 0.25s ease, transform 0.2s ease;
}

.audio-parallel-row.aps-queued {
    background: #f1f5f9;
    color: var(--text-secondary);
}

.audio-parallel-row.aps-active {
    background: linear-gradient(90deg, #e0e7ff 0%, #dbeafe 100%);
    color: var(--primary-color);
    font-weight: 600;
    box-shadow: 0 0 0 2px rgba(79, 70, 229, 0.25);
    animation: aps-row-pulse 1.1s ease-in-out infinite;
}

.audio-parallel-row.aps-ok {
    background: #ecfdf5;
    color: #047857;
}

.audio-parallel-row.aps-fail {
    background: #fef2f2;
    color: #b91c1c;
}

.aps-num {
    flex: 1;
    text-align: right;
}

.aps-badge {
    font-size: 0.82rem;
    min-width: 4.5em;
    text-align: center;
}

.aps-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #cbd5e1;
    flex-shrink: 0;
}

.aps-dot-pulse {
    background: var(--primary-color);
    animation: aps-dot-breathe 0.9s ease-in-out infinite;
}

@keyframes aps-row-pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.01); }
}

@keyframes aps-dot-breathe {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.55; transform: scale(1.35); }
}

/* Responsive */
@media (max-width: 768px) {
    header h1 {
        font-size: 2rem;
    }

    .auth-container {
        grid-template-columns: 1fr;
    }

    .section {
        padding: 20px;
    }

    .card {
        padding: 20px;
    }

    .audio-editor-grid {
        grid-template-columns: 1fr;
    }
}

/* Admin metrics dashboard (staging) */
.admin-metrics-panel h3 {
    margin: 0 0 8px;
    font-size: 1rem;
}
.admin-metrics-header {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}
.admin-metrics-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 12px;
}
.admin-metrics-chip {
    display: inline-block;
    padding: 4px 10px;
    border-radius: 999px;
    font-size: 0.82rem;
    background: rgba(127, 127, 127, 0.12);
    border: 1px solid var(--border-color);
}
.admin-metrics-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 12px;
}
.admin-metrics-card {
    padding: 12px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: rgba(127, 127, 127, 0.04);
    font-size: 0.88rem;
    line-height: 1.5;
}
.admin-metrics-row {
    padding: 4px 0;
    border-bottom: 1px solid rgba(127, 127, 127, 0.15);
}
.admin-metrics-job {
    border: 1px solid var(--border-color);
    border-radius: 6px;
    margin-bottom: 8px;
    overflow: hidden;
}
.admin-metrics-job-stale {
    border-color: #c62828;
    background: rgba(198, 40, 40, 0.06);
}
.admin-metrics-job-head {
    padding: 8px 10px;
    cursor: pointer;
    font-size: 0.85rem;
}
.admin-metrics-job-head:hover {
    background: rgba(127, 127, 127, 0.08);
}
.admin-metrics-job-detail {
    padding: 8px 10px 12px;
    border-top: 1px solid var(--border-color);
    font-size: 0.82rem;
}
.admin-metrics-logs {
    margin-top: 8px;
    max-height: 200px;
    overflow: auto;
    font-size: 0.75rem;
    background: #1e1e1e;
    color: #d4d4d4;
    padding: 8px;
    border-radius: 4px;
}

