:root {
  --primary: #4F8EF7;
  --primary-dark: #2E6FD8;
  --success: #27ae60;
  --warning: #f39c12;
  --danger: #e74c3c;
  --bg: #f0f4ff;
  --bg-card: #ffffff;
  --bg-secondary: #f8faff;
  --border: #dde5f0;
  --text: #2c3e50;
  --text-muted: #6c7a8d;
  --accent: #4F8EF7;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 2px 12px rgba(0,0,0,0.08);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: var(--bg); color: var(--text); min-height: 100vh; }

/* ─── Header ─── */
header {
  background: linear-gradient(135deg, #4F8EF7 0%, #7C3AED 100%);
  color: white;
  padding: 1rem 1.5rem 0;
  box-shadow: 0 4px 16px rgba(79,142,247,0.3);
}
.header-content { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem; }
.header-content h1 { font-size: 1.5rem; font-weight: 700; }
.header-content .subtitle { font-size: 0.85rem; opacity: 0.85; margin-top: 0.2rem; }
.user-area { display: flex; align-items: center; gap: 0.5rem; }
.user-name { font-size: 0.85rem; font-weight: 600; }

.modal-overlay {
  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: 10000;
}
.modal-box {
  background: white; border-radius: var(--radius); padding: 1.5rem; width: 90%; max-width: 380px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.2);
}
.modal-box h2 { font-size: 1.1rem; margin-bottom: 1rem; text-align: center; }
.modal-box .form-group { margin-bottom: 0.75rem; }
.modal-box .form-group label { display: block; font-size: 0.82rem; font-weight: 600; margin-bottom: 0.25rem; }
.modal-box .form-group input {
  width: 100%; padding: 0.5rem 0.6rem; border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 0.85rem;
}

.tabs { display: flex; gap: 0.25rem; }
.tab {
  background: rgba(255,255,255,0.15);
  border: none;
  color: rgba(255,255,255,0.8);
  padding: 0.6rem 1rem;
  border-radius: var(--radius) var(--radius) 0 0;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 500;
  transition: all 0.2s;
}
.tab.active { background: white; color: var(--primary); font-weight: 700; }
.tab:hover:not(.active) { background: rgba(255,255,255,0.25); color: white; }

/* ─── Main ─── */
main { padding: 1.5rem; max-width: 1100px; margin: 0 auto; }

.tab-content { display: none; }
.tab-content.active { display: block; }

/* ─── Card ─── */
.card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  margin-bottom: 1.25rem;
}
.card-title { font-size: 1rem; font-weight: 700; color: var(--text); margin-bottom: 1rem; }
.card-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem; }
.card-header .card-title { margin-bottom: 0; }
.card-actions { display: flex; gap: 0.5rem; flex-wrap: wrap; }

/* ─── Upload Zone ─── */
.upload-zone {
  border: 2px dashed var(--primary);
  border-radius: var(--radius);
  padding: 2.5rem;
  text-align: center;
  cursor: pointer;
  background: var(--bg-secondary);
  transition: all 0.2s;
}
.upload-zone:hover, .upload-zone.drag-over { background: #eef3ff; border-color: var(--primary-dark); }
.upload-icon { font-size: 3rem; display: block; margin-bottom: 0.75rem; }
.upload-placeholder p { color: var(--text-muted); margin-bottom: 0.25rem; }
.upload-hint { font-size: 0.8rem; }

.upload-info {
  display: flex; gap: 1rem; align-items: center;
  padding: 0.75rem 1rem;
  background: #eef6ff;
  border-radius: var(--radius-sm);
  margin-top: 0.75rem;
  font-size: 0.9rem;
  color: var(--primary);
  font-weight: 600;
}

/* ─── Progress ─── */
.progress-bar {
  width: 100%;
  height: 10px;
  background: var(--border);
  border-radius: 99px;
  overflow: hidden;
  margin-bottom: 0.5rem;
}
.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), #7C3AED);
  border-radius: 99px;
  transition: width 0.4s ease;
  width: 0%;
}
.progress-text { font-size: 0.85rem; color: var(--text-muted); text-align: center; }

/* ─── OCR Pages ─── */
.ocr-page {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin-bottom: 1rem;
  overflow: hidden;
}
.ocr-page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.6rem 1rem;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.ocr-page-header span { font-weight: 600; font-size: 0.9rem; }
.ocr-page-body { display: none; padding: 1rem; }
.ocr-page-body.open { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.ocr-page-img { width: 100%; border-radius: var(--radius-sm); border: 1px solid var(--border); }
.ocr-textarea {
  width: 100%;
  min-height: 200px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.75rem;
  font-size: 0.8rem;
  font-family: monospace;
  resize: vertical;
  color: var(--text);
  background: var(--bg-secondary);
}
.ocr-status-dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.ocr-status-dot.done { background: var(--success); }
.ocr-status-dot.pending { background: var(--border); }
.ocr-status-dot.running { background: var(--warning); animation: pulse 1s infinite; }

@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.4; } }

/* ─── Lessons Layout ─── */
.lessons-layout { display: grid; grid-template-columns: 260px 1fr; gap: 1.25rem; align-items: start; }
.lesson-sidebar {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  position: sticky;
  top: 1rem;
}
.lesson-sidebar h3 { font-size: 0.9rem; font-weight: 700; margin-bottom: 0.75rem; color: var(--primary); }

.unit-item { margin-bottom: 0.5rem; }
.unit-header {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text);
  padding: 0.5rem 0.6rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  background: var(--bg-secondary);
}
.unit-header:hover { background: #eef3ff; }
.unit-lessons { padding-left: 0.75rem; margin-top: 0.25rem; }
.lesson-item {
  font-size: 0.8rem;
  padding: 0.4rem 0.6rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 0.4rem;
  transition: all 0.2s;
}
.lesson-item:hover { background: #eef3ff; color: var(--primary); }
.lesson-item.active { background: var(--primary); color: white; }
.lesson-badge {
  font-size: 0.7rem;
  padding: 0.1rem 0.4rem;
  border-radius: 99px;
  background: rgba(255,255,255,0.2);
  font-weight: 600;
}

.lesson-detail { background: var(--bg-card); border-radius: var(--radius); padding: 1.25rem; box-shadow: var(--shadow); border: 1px solid var(--border); }
.lesson-detail-header { margin-bottom: 1rem; }
.lesson-detail-header h2 { font-size: 1.1rem; font-weight: 700; }
.lesson-detail-header p { font-size: 0.85rem; color: var(--text-muted); margin-top: 0.25rem; }
.lesson-content-section { margin-bottom: 1rem; }
.lesson-content-section h4 { font-size: 0.85rem; font-weight: 700; color: var(--primary); margin-bottom: 0.4rem; }
.vocab-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 0.4rem; }
.vocab-card {
  background: #f0f7ff;
  border: 1px solid #d0e4ff;
  border-radius: var(--radius-sm);
  padding: 0.4rem 0.6rem;
  font-size: 0.8rem;
  text-align: center;
}
.vocab-word { font-weight: 700; color: var(--primary); }
.vocab-vn { color: var(--text-muted); font-size: 0.75rem; }
.chant-box {
  background: #fff8e1;
  border: 1px solid #ffe082;
  border-radius: var(--radius-sm);
  padding: 0.75rem;
  font-size: 0.85rem;
  white-space: pre-wrap;
  line-height: 1.6;
}
.dialogue-line { padding: 0.35rem 0.6rem; font-size: 0.85rem; border-left: 3px solid var(--primary); margin-bottom: 0.25rem; background: var(--bg-secondary); }

.gen-config { display: flex; gap: 0.75rem; align-items: center; flex-wrap: wrap; margin-bottom: 1rem; padding: 0.75rem; background: var(--bg-secondary); border-radius: var(--radius-sm); }
.gen-config label { font-size: 0.8rem; font-weight: 600; }
.gen-config select { padding: 0.35rem 0.5rem; border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: 0.85rem; }

.char-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-bottom: 0.3rem;
}
.char-row input[type="checkbox"] { width: 18px; height: 18px; cursor: pointer; }
.char-role { font-size: 0.82rem; font-weight: 600; white-space: nowrap; min-width: 80px; }
.char-name {
  padding: 0.3rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.82rem;
  width: 100px;
}
.char-role-input {
  padding: 0.3rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.82rem;
  width: 160px;
}

.admin-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}
.admin-table th, .admin-table td {
  padding: 0.5rem 0.6rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
}
.admin-table th {
  background: var(--bg);
  font-weight: 700;
  font-size: 0.78rem;
  text-transform: uppercase;
  color: var(--muted);
}
.admin-table tr:hover { background: var(--bg); }

/* Admin mobile: card layout */
@media (max-width: 768px) {
  .admin-table, .admin-table thead, .admin-table tbody, .admin-table th, .admin-table td, .admin-table tr {
    display: block;
  }
  .admin-table thead { display: none; }
  .admin-table tr {
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    margin-bottom: 0.75rem;
    padding: 0.6rem;
    background: white;
  }
  .admin-table td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.3rem 0;
    border-bottom: none;
    font-size: 0.82rem;
  }
  .admin-table td::before {
    content: attr(data-label);
    font-weight: 700;
    font-size: 0.75rem;
    color: var(--muted);
    text-transform: uppercase;
    flex-shrink: 0;
    margin-right: 0.5rem;
  }
  .admin-table td:last-child {
    justify-content: flex-end;
    gap: 0.3rem;
    flex-wrap: wrap;
    padding-top: 0.4rem;
    border-top: 1px solid var(--border);
    margin-top: 0.2rem;
  }
}

/* ─── Script View ─── */
.script-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem; flex-wrap: wrap; gap: 0.5rem; }
.script-actions { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.segment-tabs { display: flex; gap: 0.4rem; flex-wrap: wrap; margin-bottom: 1rem; }
.seg-tab {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 99px;
  padding: 0.35rem 0.75rem;
  font-size: 0.8rem;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.2s;
}
.seg-tab.active { background: var(--primary); color: white; border-color: var(--primary); }
.seg-tab:hover:not(.active) { background: #eef3ff; border-color: var(--primary); }

.segment-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 1rem;
  margin-bottom: 0.75rem;
}
.seg-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.75rem; }
.seg-header h3 { font-size: 0.9rem; font-weight: 700; color: var(--primary); }
.seg-block {
  background: white;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.75rem;
  margin-bottom: 0.5rem;
}
.seg-block-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.5rem; font-size: 0.82rem; font-weight: 600; color: var(--accent); }
.prompt-text { font-size: 0.78rem; line-height: 1.6; white-space: pre-wrap; color: var(--text); }
.seg-bgm { font-size: 0.8rem; color: var(--text-muted); display: flex; gap: 1rem; }

.edit-textarea {
  width: 100%;
  min-height: 120px;
  border: 2px solid var(--primary);
  border-radius: var(--radius-sm);
  padding: 0.6rem;
  font-size: 0.78rem;
  font-family: monospace;
  line-height: 1.5;
  resize: vertical;
  color: var(--text);
  background: #fffff5;
}
.edit-textarea.edit-small { min-height: 60px; }
.edit-actions { display: flex; gap: 0.4rem; margin-top: 0.4rem; }

.caption-section, .combined-audio-section { margin-top: 1rem; }
.caption-section h3, .combined-audio-section h3 { font-size: 0.9rem; font-weight: 700; margin-bottom: 0.5rem; }
.section-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.5rem; }
.caption-text, .combined-audio-text {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.75rem;
  font-size: 0.85rem;
  line-height: 1.6;
  white-space: pre-wrap;
}
.hashtag-text { font-size: 0.85rem; color: var(--primary); margin-top: 0.4rem; font-weight: 500; }

/* ─── History ─── */
.history-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin-bottom: 0.5rem;
  background: var(--bg-secondary);
}
.history-info .name { font-weight: 600; font-size: 0.9rem; }
.history-info .meta { font-size: 0.78rem; color: var(--text-muted); margin-top: 0.15rem; }
.history-status {
  padding: 0.2rem 0.6rem;
  border-radius: 99px;
  font-size: 0.75rem;
  font-weight: 600;
}
.status-uploaded { background: #e3f0ff; color: #2e6fd8; }
.status-ocr_done { background: #e8f5e9; color: #2e7d32; }
.status-extracted { background: #f3e5f5; color: #7b1fa2; }

/* ─── Buttons ─── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.55rem 1rem;
  border-radius: 8px;
  border: none;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); }
.btn-success { background: var(--success); color: white; }
.btn-success:hover:not(:disabled) { background: #219a52; }
.btn-sm { padding: 0.35rem 0.65rem; font-size: 0.78rem; }
.btn-copy { background: var(--bg-secondary); border: 1px solid var(--border); color: var(--text-muted); }
.btn-copy:hover { background: #eef3ff; color: var(--primary); border-color: var(--primary); }

/* ─── Empty State ─── */
.empty-state { text-align: center; padding: 3rem 1rem; color: var(--text-muted); }
.empty-icon { font-size: 3rem; display: block; margin-bottom: 0.75rem; }
.empty-state p { font-size: 0.9rem; }

/* ─── Toast ─── */
.toast {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  background: var(--text);
  color: white;
  padding: 0.75rem 1.25rem;
  border-radius: var(--radius);
  font-size: 0.85rem;
  font-weight: 500;
  box-shadow: 0 4px 16px rgba(0,0,0,0.2);
  transform: translateY(100px);
  opacity: 0;
  transition: all 0.3s;
  z-index: 9999;
  max-width: 320px;
}
.toast.show { transform: translateY(0); opacity: 1; }
.toast.error { background: var(--danger); }

/* ─── Responsive ─── */
@media (max-width: 768px) {
  .lessons-layout { grid-template-columns: 1fr; }
  .lesson-sidebar { position: static; }
  .ocr-page-body.open { grid-template-columns: 1fr; }
  header { padding: 1rem; }
  .header-content { flex-direction: column; align-items: flex-start; gap: 0.5rem; }
  .user-area { width: 100%; justify-content: flex-end; }
  .tabs { overflow-x: auto; }
  .tab { padding: 0.5rem 0.75rem; font-size: 0.78rem; white-space: nowrap; }
  .gen-config { flex-direction: column !important; align-items: stretch !important; }
  .char-row { flex-wrap: wrap; }
  .char-name, .char-role-input { width: 100% !important; min-width: 0; }
}
