/* Default = DARK mode */
:root, [data-theme="dark"] {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #334155;
  --border: #475569;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --primary-light: #1e3a5f;
  --success: #22c55e;
  --success-bg: rgba(34, 197, 94, 0.15);
  --danger: #ef4444;
  --danger-bg: rgba(239, 68, 68, 0.15);
  --warning: #f59e0b;
  --accent: #8b5cf6;
  --radius: 12px;
  --shadow: 0 4px 24px rgba(0,0,0,0.3);
  --sidebar-w: 260px;
  --auth-grad: linear-gradient(135deg, #0f172a 0%, #1e1b4b 100%);
}

[data-theme="light"] {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-light: #dbeafe;
  --success: #16a34a;
  --success-bg: #dcfce7;
  --danger: #dc2626;
  --danger-bg: #fee2e2;
  --warning: #d97706;
  --accent: #7c3aed;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 4px 12px rgba(0,0,0,.04);
  --auth-grad: linear-gradient(135deg, #eff6ff 0%, #f5f3ff 100%);
}

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

body {
  font-family: 'Inter', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
  transition: background .2s, color .2s;
}

.hidden { display: none !important; }

/* Theme toggle switch */
.theme-switch {
  display: flex; align-items: center; gap: 8px;
  cursor: pointer; user-select: none;
}
.theme-switch input { display: none; }
.theme-track {
  width: 44px; height: 24px; border-radius: 12px;
  background: var(--surface-2); border: 1px solid var(--border);
  position: relative; transition: background .2s;
}
.theme-track::after {
  content: ''; position: absolute; top: 2px; left: 2px;
  width: 18px; height: 18px; border-radius: 50%;
  background: var(--primary); transition: transform .2s;
}
.theme-switch input:checked + .theme-track::after { transform: translateX(20px); }
.theme-label { font-size: 12px; color: var(--text-muted); }

/* ========== AUTH ========== */
.auth-screen {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: var(--auth-grad); padding: 20px;
}
.auth-card {
  background: var(--surface); border-radius: 16px; padding: 40px 36px;
  width: 100%; max-width: 400px; box-shadow: var(--shadow);
  border: 1px solid var(--border);
}
.auth-logo { text-align: center; margin-bottom: 28px; }
.auth-logo h1 { font-size: 22px; margin-top: 12px; }
.auth-logo p { 
  /* color: #f3f4f6; */
  color: var(--text-muted); 
  font-size: 18px;
  margin: 15px 15px 0px 15px;
}
.auth-logo .slogan{
  color: var(--text-muted); 
  font-size: 14px; 
  margin: 0;
}
.logo-icon {
  width: 56px; height: 56px; margin: 0 auto;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  border-radius: 14px; display: flex; align-items: center; justify-content: center;
  font-weight: 700; color: #fff; font-size: 18px;
}
.logo-icon.sm { width: 40px; height: 40px; font-size: 14px; border-radius: 10px; margin: 0; }

.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; color: var(--text-muted); }
.form-group input, .form-group select, .form-group textarea {
  width: 100%; padding: 11px 14px; border: 1.5px solid var(--border);
  border-radius: 8px; font-size: 14px; font-family: inherit; outline: none;
  background: var(--surface-2); color: var(--text); transition: border .15s;
}
.form-group input:focus, .form-group select:focus { border-color: var(--primary); }

.btn {
  padding: 10px 18px; border-radius: 8px; font-size: 14px; font-weight: 600;
  cursor: pointer; border: none; font-family: inherit; transition: all .15s;
  display: inline-flex; align-items: center; gap: 6px; text-decoration: none; color: inherit;
}
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-outline { background: transparent; border: 1.5px solid var(--border); color: var(--text); }
.btn-outline:hover:not(:disabled) { background: var(--surface-2); }
.btn-success { background: var(--success); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.btn-block { width: 100%; justify-content: center; }
.btn-icon {
  background: none; border: none; font-size: 20px; cursor: pointer;
  color: var(--text-muted); padding: 6px; border-radius: 6px;
}
.btn-icon:hover { background: var(--surface-2); color: var(--text); }

.auth-link { text-align: center; margin-top: 16px; font-size: 13px; }
.auth-link a { color: var(--primary); text-decoration: none; }
.error-msg { color: var(--danger); font-size: 13px; margin-top: 12px; text-align: center; }

/* ========== APP SHELL ========== */
.app { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w); background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  position: fixed; top: 0; left: 0; bottom: 0; z-index: 50;
  transition: transform .25s ease;
}
.sidebar-header {
  display: flex; align-items: center; gap: 10px;
  padding: 16px 14px; border-bottom: 1px solid var(--border);
}
.brand strong { font-size: 14px; display: block; }
.brand span {
  font-size: 11px; background: var(--primary-light); color: var(--primary);
  padding: 2px 8px; border-radius: 20px; font-weight: 600;
}
.sidebar-close { display: none; margin-left: auto; }

.nav { flex: 1; overflow-y: auto; padding: 12px 10px; }
.nav-section-title {
  font-size: 11px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .04em; color: var(--text-muted); padding: 8px 10px 6px;
}
.nav-item {
  display: flex; align-items: center; gap: 10px;
  width: 100%; text-align: left; padding: 10px 12px;
  border: none; background: transparent; color: var(--text);
  border-radius: 8px; cursor: pointer; font-size: 14px; font-weight: 500;
  transition: all .15s; margin-bottom: 2px; text-decoration: none;
}
.nav-item:hover { background: var(--surface-2); }
.nav-item.active { background: var(--primary-light); color: var(--primary); }

.sidebar-user {
  display: flex; align-items: center; gap: 10px;
  padding: 14px; border-top: 1px solid var(--border);
}
.user-avatar {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--primary); color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 14px; flex-shrink: 0;
}
.user-info { flex: 1; min-width: 0; }
.user-info strong { display: block; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-info small { color: var(--text-muted); font-size: 11px; }

.sidebar-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,.35); z-index: 40;
}

.main-wrap {
  flex: 1; margin-left: var(--sidebar-w);
  display: flex; flex-direction: column; min-height: 100vh;
}
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 8px 15px; background: var(--surface);
  border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20;
}
.topbar h2 { font-size: 17px; font-weight: 700; flex: 1; min-width: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.menu-toggle { display: none; }
.topbar-right { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.topbar-user {
  display: none; flex-direction: column; align-items: flex-end; line-height: 1.2;
}
.topbar-user strong { font-size: 13px; }
.topbar-user small { font-size: 11px; color: var(--text-muted); }

.content { flex: 1; padding: 24px; }

/* Cards */
.cards-row {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px; margin-bottom: 24px;
}
.stat-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow);
}
.stat-card .label { font-size: 13px; color: var(--text-muted); font-weight: 500; }
.stat-card .value { font-size: 28px; font-weight: 700; margin-top: 4px; }
.stat-card .value.green { color: var(--success); }

.panel {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden;
}
.panel-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 20px; border-bottom: 1px solid var(--border);
}
.panel-header h3 { font-size: 15px; font-weight: 600; }
.panel-body { padding: 16px 20px; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); }
th { font-weight: 600; color: var(--text-muted); font-size: 12px; background: var(--surface-2); }
tr:hover td { background: var(--surface-2); }

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 20px;
  font-size: 11px; font-weight: 600;
}
.badge-green { background: var(--success-bg); color: var(--success); }
.badge-red { background: var(--danger-bg); color: var(--danger); }
.badge-blue { background: var(--primary-light); color: var(--primary); }

.page-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 20px; }

.exercise-header {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 20px; flex-wrap: wrap; gap: 10px;
}
.mode-tabs {
  display: flex; gap: 4px; background: var(--surface-2);
  padding: 4px; border-radius: 10px; border: 1px solid var(--border);
}
.mode-tab {
  padding: 7px 14px; border: none; background: transparent;
  color: var(--text-muted); border-radius: 8px; cursor: pointer;
  font-weight: 500; font-size: 13px;
}
.mode-tab.active { background: var(--primary); color: #fff; }

.columns-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  gap: 16px; margin-bottom: 24px;
}
.column-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px; text-align: center;
  box-shadow: var(--shadow);
}
.column-card h4 { font-size: 12px; color: var(--text-muted); margin-bottom: 10px; }
.number-list {
  font-family: 'JetBrains Mono', monospace; font-size: 18px;
  font-weight: 500; line-height: 1.7; min-height: 40px;
}
.number-list .neg, .neg { color: var(--danger); }
.answer-row { margin-top: 10px; padding-top: 10px; border-top: 2px solid var(--border); }
.answer-input, .problem-input, .expression-input {
  width: 100%; padding: 9px; font-family: 'JetBrains Mono', monospace;
  font-size: 16px; font-weight: 700; text-align: center;
  background: var(--surface-2); border: 2px solid var(--border);
  border-radius: 8px; color: var(--text); outline: none;
}
.answer-input:focus, .problem-input:focus, .expression-input:focus { border-color: var(--primary); }
.answer-input.correct, .problem-input.correct, .expression-input.correct {
  border-color: var(--success); background: var(--success-bg);
}
.answer-input.wrong, .problem-input.wrong, .expression-input.wrong {
  border-color: var(--danger); background: var(--danger-bg);
}

.problems-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 12px; margin-bottom: 24px;
}
.problems-grid-special {
  display: grid; 
  grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
  gap: 12px; 
  margin-bottom: 24px;
}
.problem-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 16px;
  display: flex; align-items: center; gap: 12px; box-shadow: var(--shadow);
}
.problem-text {
  font-family: 'JetBrains Mono', monospace; font-size: 16px;
  font-weight: 600; flex: 1;
}
.problem-input { width: 90px; }

.expression-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 36px; text-align: center;
  max-width: 480px; margin: 0 auto 24px; box-shadow: var(--shadow);
}
.expression-text {
  font-family: 'JetBrains Mono', monospace; font-size: 24px;
  font-weight: 700; margin-bottom: 20px;
}
.expression-input { width: 160px; font-size: 22px; padding: 12px; }

.score-bar {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 20px; margin-top: 12px;
  box-shadow: var(--shadow);
}
.score-text { font-size: 15px; font-weight: 600; }
.score-text span { color: var(--primary); }

/* Dictation */
.dictation-box {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 24px; text-align: center;
  max-width: 520px; margin: 0 auto 20px; box-shadow: var(--shadow);
}
.dictation-display {
  background: var(--bg); border-radius: 12px; padding: 36px 20px;
  margin-bottom: 16px; min-height: 110px;
  display: flex; align-items: center; justify-content: center;
}
.big-number { font-family: 'JetBrains Mono', monospace; font-size: 48px; font-weight: 700; }
.dictation-controls {
  display: flex; gap: 10px; justify-content: center; align-items: center;
  flex-wrap: wrap; margin-bottom: 12px;
}
.speed-control { font-size: 13px; color: var(--text-muted); display: flex; align-items: center; gap: 6px; }
.speed-control select {
  padding: 5px 8px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); font-family: inherit;
}
.dictation-hint { font-size: 12px; color: var(--text-muted); margin-top: 8px; }
.dictation-progress { font-size: 13px; color: var(--text-muted); margin-bottom: 8px; }
.hint { font-size: 13px; color: var(--text-muted); margin-bottom: 10px; }
.col-answer-row {
  display: flex; gap: 10px; align-items: center; justify-content: center;
  margin-top: 12px; flex-wrap: wrap;
}
.col-answer-row input { width: 120px; }

/* Responsive */
@media (max-width: 900px) {
  .sidebar { transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); }
  .sidebar-close { display: block; }
  .sidebar-overlay.open { display: block; }
  .main-wrap { margin-left: 0; }
  .menu-toggle { display: block; }
  .topbar-user { display: flex; }
  .columns-grid { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); }
}

.big-number { font-size: 72px !important; line-height: 1.1; }
.big-number.dict-problem { font-size: 42px !important; }
.big-number.dict-prompt { font-size: 28px !important; font-weight: 600; }
.dictation-display { min-height: 160px; padding: 48px 20px; }

.form-layout {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  align-items: start;
}
@media (max-width: 900px) {
  .form-layout { grid-template-columns: 1fr; }
  .big-number { font-size: 56px !important; }
}
.demo-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  position: sticky;
  top: 80px;
}
.demo-panel h3 { font-size: 15px; margin-bottom: 12px; }
.demo-panel pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  font-size: 11px;
  overflow-x: auto;
  font-family: 'JetBrains Mono', monospace;
  color: var(--text-muted);
  white-space: pre-wrap;
}
.demo-panel .demo-section { margin-bottom: 16px; }
.demo-panel .demo-section strong { font-size: 13px; display: block; margin-bottom: 6px; }

.brand-logo-sm {
  height: 50px; width: auto; max-width: 100px;
  object-fit: contain; border-radius: 4px;
  background: #f4f3f3;  padding: 2px 4px; flex-shrink: 0;
}
.branch-badge {
  display: inline-block;
  background: linear-gradient(135deg, #dc2626, #ef4444);
  color: #fff !important;
  font-size: 10px; font-weight: 700;
  padding: 2px 8px; border-radius: 10px;
  letter-spacing: 0.03em; text-transform: uppercase;
  width: fit-content;
}
.role-pill {
  display: inline-block;
  background: var(--primary-light); color: var(--primary);
  font-size: 10px; font-weight: 600;
  padding: 2px 8px; border-radius: 10px; margin-left: 4px;
}
.auth-logo-img {
  height: 60px; 
  width: auto; 
  max-width: 200px;
  object-fit: contain; 
  border-radius: 8px;
  background: #f4f3f3; 
  padding: 6px 12px; 
  margin: 0 auto 8px;
  display: block;
}
.auth-logo .branch-badge { margin: 8px auto 0; display: inline-block; }

.btn-generate {
  background: #7c3aed; color: #fff;
}
.btn-generate:hover:not(:disabled) { background: #6d28d9; color: #fff; }
.btn-check-save {
  background: #16a34a; color: #fff;
}
.btn-check-save:hover:not(:disabled) { background: #15803d; color: #fff; }
.answer-input.empty-error,
.problem-input.empty-error,
.expression-input.empty-error {
  border-color: var(--danger) !important;
  background: var(--danger-bg) !important;
  box-shadow: 0 0 0 2px rgba(239, 68, 68, 0.25);
}

/* Breadcrumbs */
.breadcrumbs {
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px;
  font-size: 13px; margin-bottom: 16px; color: var(--text-muted);
}
.breadcrumbs a { color: var(--primary); text-decoration: none; font-weight: 500; }
.breadcrumbs a:hover { text-decoration: underline; }
.bc-sep { color: var(--border); }
.bc-current { color: var(--text); font-weight: 600; }

/* Data table */
.data-table-wrap { width: 100%; }
.dt-toolbar {
  display: flex; flex-wrap: wrap; gap: 10px; align-items: center;
  justify-content: space-between; margin-bottom: 12px; padding: 0 4px;
}
.dt-filter {
  padding: 8px 12px; border: 1.5px solid var(--border); border-radius: 8px;
  background: var(--surface-2); color: var(--text); font-size: 13px;
  min-width: 180px; flex: 1; max-width: 320px; font-family: inherit;
}
.dt-filter:focus { outline: none; border-color: var(--primary); }
.dt-count { font-size: 12px; color: var(--text-muted); white-space: nowrap; }
.table-scroll {
  width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch;
  border-radius: 0 0 var(--radius) var(--radius);
}
.table-scroll table { min-width: 560px; }
@media (max-width: 600px) {
  .table-scroll table { min-width: 480px; font-size: 12px; }
  th, td { padding: 8px 10px; }
  .dt-filter { max-width: 100%; }
}

.topbar-brand {
  display: flex; align-items: center; gap: 5px; flex: 1; min-width: 0;
}
.topbar-brand strong {
  font-size: 16px; font-weight: 700; white-space: nowrap;
}

.pagination {
  display: flex; flex-wrap: wrap; gap: 10px; align-items: center;
  justify-content: space-between; margin-top: 16px;
  padding: 12px 0;
}
.pag-info { font-size: 13px; color: var(--text-muted); }
.pag-links { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.pag-ellipsis { color: var(--text-muted); padding: 0 4px; }
@media (max-width: 600px) {
  .pagination { flex-direction: column; align-items: flex-start; }
}

/* Toast */
.toast-container {
  position: fixed; top: 16px; right: 16px; z-index: 9999;
  display: flex; flex-direction: column; gap: 10px;
  max-width: min(360px, calc(100vw - 32px));
  pointer-events: none;
}
.toast {
  pointer-events: auto;
  padding: 14px 16px; border-radius: 10px;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); box-shadow: var(--shadow);
  font-size: 14px; font-weight: 500; line-height: 1.4;
  display: flex; align-items: flex-start; gap: 10px;
  animation: toastIn .25s ease;
}
.toast.success { border-left: 4px solid var(--success); }
.toast.error { border-left: 4px solid var(--danger); }
.toast.info { border-left: 4px solid var(--primary); }
.toast-close {
  background: none; border: none; color: var(--text-muted);
  cursor: pointer; font-size: 18px; line-height: 1; margin-left: auto; padding: 0 2px;
}
@keyframes toastIn {
  from { opacity: 0; transform: translateY(-8px); }
  to { opacity: 1; transform: translateY(0); }
}

.btn-check-save {
  font-size: 15px !important;
  padding: 12px 22px !important;
  font-weight: 700 !important;
  min-height: 44px;
}
.btn-check-save.btn-lg-block {
  width: 100%;
  max-width: 320px;
  display: inline-flex;
  justify-content: center;
}
.check-bottom {
  margin-top: 24px;
  text-align: center;
  padding: 16px 0 8px;
}

.exercise-header {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
}
.exercise-header .mode-tabs { flex: 1; min-width: 0; }
.exercise-header #worksheetActions { flex-shrink: 0; }

@media (max-width: 600px) {
  .exercise-header {
    flex-wrap: nowrap;
    gap: 6px;
  }
  .mode-tabs {
    flex: 1;
    min-width: 0;
    overflow-x: auto;
  }
  .mode-tab {
    padding: 6px 10px;
    font-size: 12px;
    white-space: nowrap;
  }
  .btn-check-save {
    font-size: 13px !important;
    padding: 10px 12px !important;
    white-space: nowrap;
  }
}
