/* ChemQuest design system: tokens first, then layout, then components. */

:root {
  color-scheme: light;
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #eef1f8;
  --text: #172033;
  --muted: #586378;
  --border: #dbe1ec;
  --primary: #5b3df5;
  --primary-hover: #4a2ee0;
  --primary-ink: #ffffff;
  --primary-soft: #ece8ff;
  --accent: #0f9d8f;
  --success: #157f3d;
  --success-soft: #e3f6ea;
  --warn: #a35a00;
  --warn-soft: #fff1dc;
  --danger: #c62828;
  --danger-soft: #fde8e8;
  --info: #1d5fc4;
  --info-soft: #e5efff;
  --hero-from: #5b3df5;
  --hero-to: #0f9d8f;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 8px 24px rgba(16, 24, 40, .06);
  --radius: 16px;
  --radius-sm: 10px;
  --font: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "Cascadia Mono", Consolas, monospace;
  --sidebar-w: 264px;
  --topbar-h: 64px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0d1220;
    --surface: #151c2e;
    --surface-2: #1d2539;
    --text: #e8ecf5;
    --muted: #a0aac0;
    --border: #2a3450;
    --primary: #9b8cff;
    --primary-hover: #b0a4ff;
    --primary-ink: #120d2b;
    --primary-soft: #26204a;
    --accent: #3dd6c4;
    --success: #5fd38d;
    --success-soft: #133222;
    --warn: #f4b55a;
    --warn-soft: #3a2a12;
    --danger: #ff8a80;
    --danger-soft: #3d1a1a;
    --info: #8ab6ff;
    --info-soft: #162a4a;
    --hero-from: #3b2a9e;
    --hero-to: #0b6e65;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .25);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d1220;
  --surface: #151c2e;
  --surface-2: #1d2539;
  --text: #e8ecf5;
  --muted: #a0aac0;
  --border: #2a3450;
  --primary: #9b8cff;
  --primary-hover: #b0a4ff;
  --primary-ink: #120d2b;
  --primary-soft: #26204a;
  --accent: #3dd6c4;
  --success: #5fd38d;
  --success-soft: #133222;
  --warn: #f4b55a;
  --warn-soft: #3a2a12;
  --danger: #ff8a80;
  --danger-soft: #3d1a1a;
  --info: #8ab6ff;
  --info-soft: #162a4a;
  --hero-from: #3b2a9e;
  --hero-to: #0b6e65;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .25);
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 var(--font);
}
a { color: var(--primary); text-underline-offset: 2px; }
a:hover { color: var(--primary-hover); }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5rem; }
h1 { font-size: 1.6rem; letter-spacing: -.01em; }
code, .mono { font-family: var(--mono); font-size: .92em; }
img, svg { max-width: 100%; }
:focus-visible { outline: 3px solid var(--primary); outline-offset: 2px; border-radius: 4px; }

.skip-link { position: absolute; left: -999px; top: 8px; z-index: 100; background: var(--surface); padding: 8px 12px; border-radius: 8px; }
.skip-link:focus { left: 8px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: .85rem; margin: .35rem 0 0; }
.small { font-size: .85rem; }
.nowrap { white-space: nowrap; }
.optional { color: var(--muted); font-weight: 400; }
.eyebrow { text-transform: uppercase; letter-spacing: .08em; font-size: .75rem; color: var(--muted); margin: 0 0 .25rem; font-weight: 600; }
.subhead { font-size: 1rem; margin-top: 1rem; }

/* ---------- App shell ---------- */
.nav-toggle { position: absolute; opacity: 0; pointer-events: none; }
.topbar {
  position: sticky; top: 0; z-index: 30;
  height: var(--topbar-h);
  display: flex; align-items: center; gap: 12px;
  padding: 0 20px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--text); font-size: 1.2rem; }
.brand b { color: var(--primary); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 12px; }
.icon-btn {
  display: inline-grid; place-items: center;
  width: 40px; height: 40px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.i-moon { display: none; }
:root[data-theme="dark"] .i-sun { display: none; }
:root[data-theme="dark"] .i-moon { display: block; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .i-sun { display: none; }
  :root:not([data-theme="light"]) .i-moon { display: block; }
}
.nav-burger { display: none; }
.user-chip { display: flex; align-items: center; gap: 10px; }
.avatar {
  width: 36px; height: 36px; border-radius: 50%;
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--hero-from), var(--hero-to)); color: #fff; font-weight: 700;
}
.user-chip-text { display: flex; flex-direction: column; line-height: 1.2; }
.user-chip-name { font-weight: 600; max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-chip-role { font-size: .78rem; color: var(--muted); }

.sidebar {
  position: fixed; top: var(--topbar-h); bottom: 0; left: 0;
  width: var(--sidebar-w);
  overflow-y: auto;
  padding: 16px 12px 24px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  z-index: 20;
}
.nav-group { margin-bottom: 4px; }
.nav-group summary {
  list-style: none; cursor: pointer;
  padding: 8px 12px; border-radius: var(--radius-sm);
  font-size: .75rem; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--muted);
}
.nav-group summary::-webkit-details-marker { display: none; }
.nav-group summary::after { content: "▸"; float: right; transition: transform .15s; }
.nav-group[open] summary::after { transform: rotate(90deg); }
.nav-group ul { list-style: none; margin: 0 0 8px; padding: 0; }
.nav-group a, .nav-soon {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px; border-radius: var(--radius-sm);
  color: var(--text); text-decoration: none; font-size: .93rem;
}
.nav-group a:hover { background: var(--surface-2); }
.nav-group a[aria-current="page"] { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.nav-soon { color: var(--muted); cursor: default; }
.nav-soon small { font-size: .68rem; border: 1px solid var(--border); border-radius: 999px; padding: 0 6px; }
.nav-logout { margin-top: 12px; padding: 0 4px; }
.nav-scrim { display: none; }

.main { margin-left: var(--sidebar-w); padding: 28px 32px 48px; max-width: calc(1200px + var(--sidebar-w)); }

@media (max-width: 900px) {
  .nav-burger { display: inline-grid; }
  .sidebar { transform: translateX(-100%); transition: transform .2s ease; box-shadow: var(--shadow); }
  .nav-toggle:checked ~ .sidebar { transform: none; }
  .nav-toggle:checked ~ .nav-scrim { display: block; position: fixed; inset: var(--topbar-h) 0 0 0; background: rgba(0, 0, 0, .35); z-index: 15; }
  .main { margin-left: 0; padding: 20px 16px 40px; }
  .user-chip-text { display: none; }
  .topbar { padding: 0 12px; }
}

/* ---------- Page structure ---------- */
.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 12px; margin-bottom: 20px; }
.page-head p { margin: 0; }
.page-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.back-link { display: inline-block; margin-bottom: 6px; font-size: .88rem; text-decoration: none; }
.grid { display: grid; gap: 20px; margin-bottom: 20px; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(min(100%, 380px), 1fr)); }
.grid-tight { gap: 12px; margin-bottom: 0; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 22px;
  margin-bottom: 20px;
  min-width: 0;
}
.grid > .card { margin-bottom: 0; }
.card-title { font-size: 1.05rem; margin-bottom: 14px; }
.card-narrow { max-width: 560px; }
.card-flush { padding: 0; overflow: hidden; }
.card-feature { border-top: 4px solid var(--primary); }
.empty { padding: 28px; text-align: center; color: var(--muted); margin: 0; }

.hero-banner {
  display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 20px;
  padding: 28px;
  margin-bottom: 20px;
  border-radius: var(--radius);
  color: #fff;
  background:
    radial-gradient(circle at 85% 20%, rgba(255, 255, 255, .18), transparent 40%),
    linear-gradient(120deg, var(--hero-from), var(--hero-to));
  box-shadow: var(--shadow);
}
.hero-banner h1 { font-size: 1.7rem; margin: 0 0 6px; }
.hero-kicker { margin: 0 0 4px; opacity: .85; font-size: .85rem; text-transform: uppercase; letter-spacing: .08em; }
.hero-meta { margin: 0; display: flex; flex-wrap: wrap; gap: 8px; }
.hero-meta span { background: rgba(255, 255, 255, .18); padding: 2px 10px; border-radius: 999px; font-size: .88rem; }
.hero-level { display: flex; flex-direction: column; align-items: flex-end; text-align: right; }
.hero-level-label { font-size: .8rem; opacity: .85; text-transform: uppercase; letter-spacing: .08em; }
.hero-level-value { font-size: 2rem; font-weight: 700; }
.hint-on-dark { font-size: .8rem; opacity: .85; }

.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 170px), 1fr)); gap: 16px; margin-bottom: 20px; }
.stat {
  display: flex; flex-direction: column; gap: 4px;
  padding: 16px 18px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow);
  color: var(--text); text-decoration: none;
}
.stat-link:hover { border-color: var(--primary); color: var(--text); }
.stat-label { color: var(--muted); font-size: .82rem; }
.stat-value { font-size: 1.7rem; font-weight: 700; font-variant-numeric: tabular-nums; }

.progress { display: block; height: 10px; border-radius: 999px; background: var(--surface-2); overflow: hidden; margin: 14px 0 6px; }
.progress > span { display: block; height: 100%; width: 0; border-radius: inherit; background: linear-gradient(90deg, var(--primary), var(--accent)); transition: width .4s ease; }
.progress-sm { height: 6px; margin: 8px 0 2px; }

.class-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.class-tile {
  display: flex; flex-direction: column; padding: 14px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  text-decoration: none; color: var(--text); background: var(--surface);
}
.class-tile:hover { border-color: var(--primary); color: var(--text); }
.class-tile-code { font-size: 1.15rem; font-weight: 700; }

/* ---------- Lists & details ---------- */
.list { list-style: none; padding: 0; margin: 0 0 14px; }
.list li { padding: 8px 0; border-bottom: 1px solid var(--border); }
.list li:last-child { border-bottom: 0; }
.list-bullets { list-style: disc; padding-left: 20px; }
.list-bullets li { border: 0; padding: 3px 0; }
.details { display: grid; grid-template-columns: minmax(120px, 40%) 1fr; gap: 8px 16px; margin: 0 0 14px; }
.details dt { color: var(--muted); font-size: .9rem; }
.details dd { margin: 0; overflow-wrap: anywhere; }
.chip-list { list-style: none; padding: 0; margin: 0 0 14px; display: flex; flex-wrap: wrap; gap: 8px; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 4px 6px 4px 12px; border-radius: 999px; background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.chip form { margin: 0; }
.chip-remove { border: 0; background: transparent; color: inherit; cursor: pointer; font-size: 1.1rem; line-height: 1; width: 24px; height: 24px; border-radius: 50%; }
.chip-remove:hover { background: rgba(0, 0, 0, .08); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.table th, .table td { padding: 12px 16px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
.table th { font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); background: var(--surface-2); font-weight: 600; }
.table tbody tr:hover { background: color-mix(in srgb, var(--surface-2) 55%, transparent); }
.table-compact th, .table-compact td { padding: 8px 10px; }
.card:not(.card-flush) .table th:first-child, .card:not(.card-flush) .table td:first-child { padding-left: 0; }
.card:not(.card-flush) .table th { background: transparent; }
.details-cell { max-width: 320px; overflow-wrap: anywhere; font-family: var(--mono); font-size: .8rem; color: var(--muted); }

/* ---------- Badges & alerts ---------- */
.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: .76rem; font-weight: 600; white-space: nowrap; background: var(--surface-2); color: var(--muted); }
.badge-success { background: var(--success-soft); color: var(--success); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-muted { background: var(--surface-2); color: var(--muted); }
.badge-role-admin { background: var(--danger-soft); color: var(--danger); }
.badge-role-lecturer { background: var(--info-soft); color: var(--info); }
.badge-role-student { background: var(--primary-soft); color: var(--primary); }

.flashes { display: grid; gap: 8px; margin-bottom: 18px; }
.alert { padding: 12px 16px; border-radius: var(--radius-sm); border: 1px solid transparent; font-size: .93rem; }
.alert-success { background: var(--success-soft); color: var(--success); border-color: color-mix(in srgb, var(--success) 25%, transparent); }
.alert-error { background: var(--danger-soft); color: var(--danger); border-color: color-mix(in srgb, var(--danger) 25%, transparent); }
.alert-info { background: var(--info-soft); color: var(--info); border-color: color-mix(in srgb, var(--info) 25%, transparent); }
.alert-credential { background: var(--warn-soft); color: var(--text); border: 2px dashed var(--warn); font-family: var(--mono); }

/* ---------- Forms ---------- */
.form { display: grid; gap: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; margin: 0; min-width: 0; }
fieldset.field { border: 0; padding: 0; }
legend, label { font-weight: 600; font-size: .9rem; }
.radio { display: inline-flex; align-items: center; gap: 8px; font-weight: 500; margin-right: 16px; }
input[type="text"], input[type="email"], input[type="password"], input[type="search"], input[type="tel"], input[type="file"], select {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  font: inherit; color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
input[readonly] { background: var(--surface-2); color: var(--muted); }
input:focus, select:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.password-wrap { position: relative; }
.password-wrap input { padding-right: 72px; }
.password-toggle {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  border: 0; background: var(--surface-2); color: var(--text);
  padding: 6px 10px; border-radius: 8px; font: inherit; font-size: .8rem; cursor: pointer;
}
.input-code { font-family: var(--mono); font-size: 1.4rem !important; letter-spacing: .3em; text-align: center; }
.form-row-between { display: flex; justify-content: flex-end; font-size: .9rem; }

.filters { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px; margin-bottom: 16px; }
.filters .field { width: 160px; }
.filters .field-grow { flex: 1 1 260px; width: auto; }
.filters .field-sm { width: 110px; }
.inline-form { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 0; }
.inline-form input, .inline-form select { width: auto; flex: 1 1 160px; }
.action-row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 8px; }
.action-row form { margin: 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 42px; padding: 9px 18px;
  font: inherit; font-weight: 600; font-size: .92rem;
  border-radius: var(--radius-sm); border: 1px solid transparent;
  cursor: pointer; text-decoration: none;
  transition: background .15s, border-color .15s;
}
.btn:disabled { opacity: .6; cursor: progress; }
.btn-primary { background: var(--primary); color: var(--primary-ink); }
.btn-primary:hover { background: var(--primary-hover); color: var(--primary-ink); }
.btn-secondary { background: var(--primary-soft); color: var(--primary); }
.btn-secondary:hover { border-color: var(--primary); color: var(--primary); }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-block { width: 100%; }
.btn-lg { min-height: 50px; font-size: 1rem; }
.link-btn { border: 0; background: none; padding: 0; color: var(--primary); font: inherit; text-decoration: underline; cursor: pointer; }

.segmented { display: inline-flex; padding: 4px; background: var(--surface-2); border-radius: 12px; }
.segmented a { padding: 6px 14px; border-radius: 9px; text-decoration: none; color: var(--muted); font-weight: 600; }
.segmented a[aria-current="page"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.pager { display: flex; align-items: center; justify-content: center; gap: 16px; }

/* ---------- Auth pages ---------- */
.auth-shell { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); min-height: 100vh; }
.auth-hero {
  position: relative; overflow: hidden;
  display: flex; align-items: center; padding: 48px;
  color: #fff;
  background:
    radial-gradient(circle at 15% 15%, rgba(255, 255, 255, .16), transparent 35%),
    linear-gradient(140deg, var(--hero-from), var(--hero-to));
}
.auth-hero-inner { position: relative; z-index: 1; max-width: 460px; }
.auth-hero-kicker { text-transform: uppercase; letter-spacing: .12em; font-size: .8rem; opacity: .85; margin: 24px 0 8px; }
.auth-hero-title { font-size: clamp(2rem, 3.5vw, 2.8rem); font-weight: 800; line-height: 1.1; margin: 0 0 12px; letter-spacing: -.01em; }
.auth-hero-sub { font-size: 1.15rem; opacity: .92; margin: 0 0 28px; }
.auth-hero-points { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
.auth-hero-points li::before { content: "⚗"; margin-right: 10px; }
.auth-molecule { position: absolute; right: -60px; bottom: -60px; width: 420px; color: #fff; }
.auth-main { position: relative; display: grid; place-items: center; padding: 32px 16px; }
.auth-theme { position: absolute; top: 16px; right: 16px; }
.auth-card {
  width: 100%; max-width: 440px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 20px;
  padding: 32px; box-shadow: var(--shadow);
}
.auth-title { font-size: 1.5rem; font-weight: 800; letter-spacing: -.01em; }
.auth-sub { color: var(--muted); margin: 0 0 22px; }
.auth-alt { margin-top: 18px; text-align: center; font-size: .9rem; color: var(--muted); }
.auth-help { margin-top: 20px; font-size: .9rem; color: var(--muted); }
.auth-help summary { cursor: pointer; color: var(--primary); font-weight: 600; }
.secret-box { display: grid; gap: 6px; padding: 16px; margin-bottom: 18px; border-radius: var(--radius-sm); background: var(--surface-2); text-align: center; }
.secret { font-family: var(--mono); font-size: 1.15rem; letter-spacing: .06em; word-break: break-all; color: var(--text); }
.secret-sm { font-size: .78rem; letter-spacing: 0; display: block; }
.error-code { font-size: 3rem; font-weight: 800; margin: 0; color: var(--primary); }

@media (max-width: 860px) {
  .auth-shell { grid-template-columns: 1fr; }
  .auth-hero { padding: 28px 20px; min-height: 0; }
  .auth-hero-points, .auth-molecule, .auth-hero-inner > img { display: none; }
  .auth-hero-kicker { margin-top: 0; }
  .auth-hero-sub { margin-bottom: 0; font-size: 1rem; }
  .auth-card { padding: 24px 20px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; scroll-behavior: auto !important; }
}
