:root {
  --bg: #f6f8fc;
  --surface: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --indigo: #4f46e5;
  --indigo-dark: #4338ca;
  --indigo-soft: #eef2ff;
  --vocab: #0284c7;
  --vocab-soft: #f0f9ff;
  --grammar: #7c3aed;
  --grammar-soft: #f5f3ff;
  --emerald: #059669;
  --emerald-soft: #ecfdf5;
  --amber: #d97706;
  --amber-soft: #fffbeb;
  --rose: #e11d48;
  --rose-soft: #fff1f2;
}

*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  max-width: 100%;
  overflow-x: hidden;
  font-family: "Inter", "PingFang SC", "Noto Sans SC", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}

button, input, select, textarea {
  font-family: inherit;
  touch-action: manipulation;
}

button { cursor: pointer; }
button:disabled { opacity: 0.55; cursor: not-allowed; }

.hidden { display: none !important; }
.antialiased { -webkit-font-smoothing: antialiased; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }

.code-tag {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
  background: #f1f5f9;
  color: #334155;
  padding: 2px 6px;
  border-radius: 6px;
  border: 1px solid #e2e8f0;
}

@keyframes livePulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.45; transform: scale(1.25); }
}
.live-dot {
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: #10b981;
  display: inline-block;
  flex-shrink: 0;
  animation: livePulse 1.8s infinite ease-in-out;
}
.live-dot.offline {
  background: #94a3b8;
  animation: none;
}

/* Login Screen */
.login-screen {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
  background: linear-gradient(135deg, #f8fafc 0%, #eef2ff 50%, #f0f9ff 100%);
}
.login-card {
  width: 100%;
  max-width: 440px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 24px;
  box-shadow: 0 20px 40px rgba(15, 23, 42, 0.08);
  padding: 28px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.login-brand { display: flex; align-items: center; gap: 14px; }
.brand-mark {
  width: 44px; height: 44px; border-radius: 14px;
  background: linear-gradient(135deg, #4f46e5, #0284c7, #7c3aed);
  color: #fff; font-weight: 800; font-size: 18px;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
  box-shadow: 0 4px 12px rgba(79, 70, 229, 0.25);
}
.brand-mark.sm { width: 36px; height: 36px; border-radius: 12px; font-size: 14px; }
.login-title { margin: 0; font-size: 18px; font-weight: 800; }
.login-sub { margin: 2px 0 0; font-size: 12px; color: var(--muted); }

.rbac-notice {
  border-radius: 12px;
  background: var(--amber-soft);
  border: 1px solid #fde68a;
  padding: 10px 12px;
  font-size: 11px;
  font-weight: 600;
  color: #78350f;
  line-height: 1.5;
}
.error-box {
  border-radius: 12px;
  background: var(--rose-soft);
  border: 1px solid #fecdd3;
  padding: 10px 12px;
  font-size: 12px;
  font-weight: 700;
  color: #be123c;
}
.form-stack { display: flex; flex-direction: column; gap: 14px; }
.field-group { display: flex; flex-direction: column; gap: 6px; }
.field-label { font-size: 12px; font-weight: 700; color: #334155; }
.input-ctrl {
  width: 100%;
  min-height: 40px;
  padding: 8px 12px;
  border-radius: 12px;
  border: 1px solid #cbd5e1;
  font-size: 14px;
  background: #fff;
  color: var(--ink);
}
.input-ctrl:focus {
  outline: none;
  border-color: var(--indigo);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.16);
}
.btn-primary {
  width: 100%;
  min-height: 42px;
  padding: 9px 16px;
  border-radius: 12px;
  border: none;
  background: var(--indigo);
  color: #fff;
  font-size: 13px;
  font-weight: 800;
  transition: background 0.15s ease;
}
.btn-primary:hover:not(:disabled) { background: var(--indigo-dark); }
.btn-primary.auto-w { width: auto; min-height: 34px; padding: 7px 14px; font-size: 12px; }
.btn-secondary {
  min-height: 34px;
  padding: 7px 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #fff;
  color: #334155;
  font-size: 12px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}
.btn-secondary:hover:not(:disabled) { background: #f8fafc; }
.btn-soft-indigo {
  border: 1px solid #c7d2fe; background: var(--indigo-soft); color: var(--indigo-dark);
  border-radius: 10px; padding: 6px 12px; font-size: 12px; font-weight: 800;
  display: inline-flex; align-items: center; gap: 5px;
}
.btn-soft-sky {
  border: 1px solid #bae6fd; background: var(--vocab-soft); color: #0369a1;
  border-radius: 10px; padding: 6px 12px; font-size: 12px; font-weight: 800;
  display: inline-flex; align-items: center; gap: 5px;
}
.btn-soft-violet {
  border: 1px solid #ddd6fe; background: var(--grammar-soft); color: #6d28d9;
  border-radius: 10px; padding: 6px 12px; font-size: 12px; font-weight: 800;
  display: inline-flex; align-items: center; gap: 5px;
}
.btn-soft-rose {
  border: 1px solid #fecdd3; background: var(--rose-soft); color: #be123c;
  border-radius: 10px; padding: 6px 12px; font-size: 12px; font-weight: 800;
  display: inline-flex; align-items: center; gap: 5px;
}
.btn-danger {
  border: none; background: var(--rose); color: #fff;
  border-radius: 10px; padding: 8px 14px; font-size: 12px; font-weight: 800;
}

/* App Shell & Header */
.app-shell { min-height: 100dvh; display: flex; flex-direction: column; }
.top-header {
  position: sticky; top: 0; z-index: 30;
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.header-inner {
  max-width: 1280px;
  margin: 0 auto;
  padding: 10px 14px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.brand-wrap { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1 1 auto; }
.brand-text { min-width: 0; }
.brand-title-row { display: flex; align-items: center; gap: 8px; }
.brand-name { font-size: 15px; font-weight: 800; color: var(--ink); }
.pill-badge {
  display: inline-flex; align-items: center; padding: 2px 8px; border-radius: 999px;
  font-size: 10px; font-weight: 800; background: var(--indigo-soft); color: var(--indigo-dark);
  border: 1px solid #c7d2fe;
}
.live-badge {
  display: inline-flex; align-items: center; gap: 6px; margin-top: 2px;
  font-size: 11px; font-weight: 700; color: #047857;
}
.live-badge.offline { color: var(--muted); }

.portal-switch {
  display: inline-flex;
  background: #f1f5f9;
  padding: 4px;
  border-radius: 12px;
  border: 1px solid var(--line);
  gap: 4px;
}
.portal-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  padding: 6px 14px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 800;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.portal-btn.active {
  background: #fff;
  color: var(--indigo-dark);
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
}

.header-right { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.student-picker {
  display: flex; align-items: center; gap: 6px;
  background: #f8fafc; border: 1px solid var(--line); border-radius: 12px; padding: 5px 10px;
}
.student-select {
  border: none; background: transparent; font-size: 12px; font-weight: 800; color: var(--ink);
  cursor: pointer; max-width: 150px;
}
.student-select:focus { outline: none; }

/* Main Containers */
.main-wrap {
  max-width: 1280px;
  width: 100%;
  margin: 0 auto;
  padding: 16px 14px 40px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  flex: 1;
}

.toolbar-card {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 10px 12px;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.03);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.seg-group {
  display: inline-flex;
  background: #f1f5f9;
  padding: 4px;
  border-radius: 12px;
  gap: 4px;
  flex-wrap: wrap;
}
.seg-btn {
  border: none;
  background: transparent;
  color: #475569;
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 800;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.seg-btn.active-indigo { background: var(--indigo); color: #fff; }
.seg-btn.active-sky { background: var(--vocab); color: #fff; }
.seg-btn.active-violet { background: var(--grammar); color: #fff; }

.period-group {
  display: inline-flex;
  background: var(--indigo-soft);
  padding: 4px;
  border-radius: 12px;
  gap: 4px;
}
.period-btn {
  border: none;
  background: transparent;
  color: var(--indigo-dark);
  padding: 5px 11px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 800;
}
.period-btn.active { background: var(--indigo); color: #fff; }

.course-group {
  display: inline-flex;
  background: var(--vocab-soft);
  border: 1px solid #bae6fd;
  padding: 3px;
  border-radius: 10px;
  gap: 3px;
}
.course-btn {
  border: none;
  background: transparent;
  color: #0369a1;
  padding: 4px 9px;
  border-radius: 7px;
  font-size: 11px;
  font-weight: 800;
}
.course-btn.active { background: #fff; box-shadow: 0 1px 2px rgba(2, 132, 199, 0.15); }

/* Banners & Cards */
.alert-banner {
  border-radius: 16px;
  padding: 12px 16px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--amber-soft);
  border: 1px solid #fde68a;
  color: #78350f;
  font-size: 12px;
  font-weight: 700;
}
.verdict-banner {
  border-radius: 18px;
  padding: 14px 18px;
  border: 1px solid #c7d2fe;
  background: linear-gradient(90deg, #eef2ff, #f8fafc);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.verdict-banner.warn { border-color: #fecdd3; background: linear-gradient(90deg, #fff1f2, #fffbeb); }
.verdict-banner.good { border-color: #a7f3d0; background: linear-gradient(90deg, #ecfdf5, #f8fafc); }
.verdict-tag { font-size: 11px; font-weight: 800; color: var(--indigo-dark); }
.verdict-meta { font-size: 11px; color: var(--muted); }
.verdict-text { margin: 4px 0 0; font-size: 14px; font-weight: 700; color: var(--ink); line-height: 1.5; }

.card {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 16px 18px;
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.03);
}
.card-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}
.card-title { margin: 0; font-size: 15px; font-weight: 800; color: var(--ink); }
.card-sub { margin: 2px 0 0; font-size: 12px; color: var(--muted); }

/* Grids */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}
@media (min-width: 960px) {
  .kpi-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}
.kpi-card {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.kpi-label { font-size: 12px; font-weight: 700; color: var(--muted); }
.kpi-val { font-size: 26px; font-weight: 800; line-height: 1.15; }
.kpi-val.indigo { color: var(--indigo); }
.kpi-val.sky { color: var(--vocab); }
.kpi-val.violet { color: var(--grammar); }
.kpi-val.emerald { color: var(--emerald); }
.kpi-val.rose { color: var(--rose); }
.kpi-foot { font-size: 11px; color: var(--muted); }

.grid-2 { display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; }
@media (min-width: 960px) {
  .grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-2.asymmetric { grid-template-columns: 7fr 5fr; }
}

.chart-box { position: relative; width: 100%; height: 220px; }
canvas { display: block; width: 100%; height: 100%; }

/* Lists & Rows */
.list-stack { display: flex; flex-direction: column; gap: 8px; }
.item-row {
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 10px 12px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 12px;
}
.item-row.warn { background: var(--rose-soft); border-color: #fecdd3; }
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 10px;
  font-weight: 800;
  white-space: nowrap;
}
.badge.vocab { background: #e0f2fe; color: #0369a1; }
.badge.grammar { background: #ede9fe; color: #6d28d9; }
.badge.good { background: #d1fae5; color: #065f46; }
.badge.warn { background: #fef3c7; color: #92400e; }
.badge.danger { background: #ffe4e6; color: #9f1239; }
.badge.muted { background: #e2e8f0; color: #475569; }

.blur-word { filter: blur(6px); user-select: none; transition: filter 0.15s ease; }

/* Grammar Heatmap */
.heat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 10px;
}
.heat-card {
  border-radius: 14px;
  border: 2px solid var(--line);
  background: #f8fafc;
  padding: 12px;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
}
.heat-card:hover { box-shadow: 0 4px 12px rgba(15, 23, 42, 0.08); }
.heat-card.MASTERED { background: var(--emerald-soft); border-color: #6ee7b7; }
.heat-card.CONSOLIDATING { background: var(--amber-soft); border-color: #fcd34d; }
.heat-card.WEAK { background: var(--rose-soft); border-color: #fda4af; }
.progress-bar { width: 100%; height: 6px; border-radius: 999px; background: #e2e8f0; overflow: hidden; }
.progress-bar > span { display: block; height: 100%; background: var(--indigo); }
.progress-bar.MASTERED > span { background: var(--emerald); }
.progress-bar.CONSOLIDATING > span { background: var(--amber); }
.progress-bar.WEAK > span { background: var(--rose); }

/* Admin Layout */
.admin-layout {
  display: flex;
  flex-direction: column;
  gap: 16px;
}
@media (min-width: 980px) {
  .admin-layout { flex-direction: row; align-items: flex-start; }
  .admin-sidebar { width: 256px; flex-shrink: 0; position: sticky; top: 76px; }
}
.admin-sidebar {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.admin-nav {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}
@media (min-width: 980px) {
  .admin-nav { grid-template-columns: 1fr; }
}
.admin-nav-btn {
  border: none;
  background: transparent;
  color: #475569;
  padding: 9px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 800;
  display: flex;
  align-items: center;
  gap: 8px;
  text-align: left;
}
.admin-nav-btn:hover { background: #f1f5f9; }
.admin-nav-btn.active { background: var(--indigo); color: #fff; }
.admin-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 16px; }

/* Tables */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 14px; }
.data-table { width: 100%; border-collapse: collapse; font-size: 12px; min-width: 640px; }
.data-table th {
  text-align: left; padding: 10px 12px; background: #f8fafc;
  border-bottom: 1px solid var(--line); font-weight: 800; color: #475569;
}
.data-table td { padding: 10px 12px; border-bottom: 1px solid #f1f5f9; vertical-align: middle; }
.data-table tr:last-child td { border-bottom: none; }

/* Modals & Drawers */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(15, 23, 42, 0.5);
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
}
.modal-card {
  width: 100%; max-width: 480px; background: #fff; border-radius: 20px;
  border: 1px solid var(--line); box-shadow: 0 24px 48px rgba(15, 23, 42, 0.22);
  overflow: hidden; max-height: 90vh; display: flex; flex-direction: column;
}
.modal-card.wide { max-width: 860px; }
.modal-head {
  padding: 14px 18px; border-bottom: 1px solid #f1f5f9;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
}
.modal-title { margin: 0; font-size: 15px; font-weight: 800; }
.modal-body { padding: 18px; overflow-y: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding-top: 12px; }
.close-x {
  width: 30px; height: 30px; border-radius: 10px; border: 1px solid var(--line);
  background: #f8fafc; color: var(--muted); font-weight: 800;
}

.drawer-backdrop {
  position: fixed; inset: 0; z-index: 45;
  background: rgba(15, 23, 42, 0.45);
  display: flex; justify-content: flex-end;
}
.drawer-panel {
  width: 100%; max-width: 500px; height: 100%; background: #fff;
  box-shadow: -8px 0 32px rgba(15, 23, 42, 0.16);
  display: flex; flex-direction: column;
}

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  z-index: 60; background: #0f172a; color: #fff;
  padding: 9px 16px; border-radius: 12px; font-size: 12px; font-weight: 800;
  box-shadow: 0 10px 24px rgba(15, 23, 42, 0.25);
  opacity: 0; pointer-events: none; transition: opacity 0.2s ease;
}
.toast.show { opacity: 1; }

.print-sheet {
  background: #fff; padding: 20px; border: 1px solid var(--line); border-radius: 14px;
}

@media print {
  body * { visibility: hidden !important; }
  #printModal, #printSheetArea, #printSheetArea * { visibility: visible !important; }
  #printModal { position: static !important; background: #fff !important; padding: 0 !important; }
  .no-print { display: none !important; }
}
