/* Study Hub v4: base design system. Apple style: system fonts, materials, restrained colour, 44px targets. */

:root {
  color-scheme: light dark;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", "Segoe UI", system-ui, Roboto, sans-serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, monospace;

  --bg: #f2f2f7;
  --surface: #ffffff;
  --surface-2: #f7f7fa;
  --text: #1c1c1e;
  --text-2: #5c5c63;
  --sep: rgba(60, 60, 67, 0.18);
  --separator: var(--sep);
  --accent: #0066d6;
  --accent-text: #ffffff;
  --accent-soft: rgba(0, 102, 214, 0.1);
  --good: #1a7f37;
  --good-soft: rgba(26, 127, 55, 0.12);
  --warn: #9a5b00;
  --warn-soft: rgba(255, 159, 10, 0.16);
  --bad: #c62828;
  --bad-soft: rgba(198, 40, 40, 0.1);
  --material: rgba(249, 249, 251, 0.78);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 6px 24px rgba(0, 0, 0, 0.06);

  --r-s: 8px;
  --r-m: 12px;
  --r-l: 16px;
  --radius: var(--r-m);
  --tap: 44px;
  --nav-h: 56px;
  --side-w: 232px;
  --ease: cubic-bezier(0.32, 0.72, 0, 1);

  --band-HD: #1a7f37;
  --band-D: #0066d6;
  --band-C: #9a5b00;
  --band-P: #8a4b9e;
  --band-N: #c62828;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #000000;
    --surface: #1c1c1e;
    --surface-2: #2c2c2e;
    --text: #f5f5f7;
    --text-2: #a1a1a8;
    --sep: rgba(235, 235, 245, 0.2);
    --accent: #4da3ff;
    --accent-text: #00152e;
    --accent-soft: rgba(77, 163, 255, 0.16);
    --good: #4cd964;
    --good-soft: rgba(76, 217, 100, 0.16);
    --warn: #ffb340;
    --warn-soft: rgba(255, 179, 64, 0.16);
    --bad: #ff6b6b;
    --bad-soft: rgba(255, 107, 107, 0.16);
    --material: rgba(28, 28, 30, 0.78);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
    --band-HD: #4cd964; --band-D: #4da3ff; --band-C: #ffb340; --band-P: #d59bea; --band-N: #ff6b6b;
  }
}
:root[data-theme="dark"] {
  --bg: #000000;
  --surface: #1c1c1e;
  --surface-2: #2c2c2e;
  --text: #f5f5f7;
  --text-2: #a1a1a8;
  --sep: rgba(235, 235, 245, 0.2);
  --accent: #4da3ff;
  --accent-text: #00152e;
  --accent-soft: rgba(77, 163, 255, 0.16);
  --good: #4cd964;
  --good-soft: rgba(76, 217, 100, 0.16);
  --warn: #ffb340;
  --warn-soft: rgba(255, 179, 64, 0.16);
  --bad: #ff6b6b;
  --bad-soft: rgba(255, 107, 107, 0.16);
  --material: rgba(28, 28, 30, 0.78);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
  --band-HD: #4cd964; --band-D: #4da3ff; --band-C: #ffb340; --band-P: #d59bea; --band-N: #ff6b6b;
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
h1, h2, h3 { margin: 0; line-height: 1.2; letter-spacing: -0.01em; }
h1 { font-size: 1.9rem; font-weight: 700; letter-spacing: -0.02em; }
h2 { font-size: 1.25rem; font-weight: 650; }
h3 { font-size: 1.05rem; font-weight: 600; }
p { margin: 0 0 0.5em; }
.muted { color: var(--text-2); }
.num { font-variant-numeric: tabular-nums; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

:focus { outline: none; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
#view:focus { outline: none; }

.skip-link {
  position: absolute; left: 8px; top: -60px; z-index: 100;
  background: var(--surface); color: var(--text); padding: 10px 14px; border-radius: var(--r-s);
}
.skip-link:focus { top: 8px; }

/* ---------- shell ---------- */
.app-shell { min-height: 100vh; min-height: 100dvh; }
.main {
  padding: calc(16px + env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    calc(var(--nav-h) + 24px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  max-width: 1100px;
  margin: 0 auto;
}

.nav {
  position: fixed; z-index: 40; left: 0; right: 0; bottom: 0;
  background: var(--material);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-top: 1px solid var(--sep);
  padding-bottom: env(safe-area-inset-bottom);
}
.brand { display: none; }
.nav-list { display: flex; justify-content: space-around; height: var(--nav-h); }
.nav-item {
  flex: 1; min-width: var(--tap); min-height: var(--tap);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--text-2); text-decoration: none; font-size: 0.6875rem; font-weight: 500;
  transition: color 0.15s, transform 0.12s var(--ease);
}
.nav-item:active { transform: scale(0.94); }
.nav-item[aria-current="page"] { color: var(--accent); }

@media (min-width: 768px) {
  .nav {
    top: 0; right: auto; width: var(--side-w); bottom: 0;
    border-top: 0; border-right: 1px solid var(--sep);
    padding: calc(20px + env(safe-area-inset-top)) 12px 20px calc(12px + env(safe-area-inset-left));
  }
  .brand { display: block; font-size: 1.15rem; font-weight: 700; padding: 4px 12px 16px; letter-spacing: -0.01em; }
  .nav-list { flex-direction: column; justify-content: flex-start; height: auto; gap: 2px; }
  .nav-item {
    flex: none; flex-direction: row; justify-content: flex-start; gap: 12px;
    padding: 0 12px; border-radius: var(--r-s); font-size: 0.95rem;
  }
  .nav-item:hover { background: var(--accent-soft); }
  .nav-item[aria-current="page"] { background: var(--accent-soft); font-weight: 600; }
  .main {
    margin-left: var(--side-w);
    max-width: none;
    padding: calc(28px + env(safe-area-inset-top)) 32px 48px;
  }
  .main > * { max-width: 1060px; }
}

/* ---------- layout primitives ---------- */
.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 20px; }
.page-head .sub { color: var(--text-2); margin: 4px 0 0; }
.stack { display: grid; gap: 16px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.row.between { justify-content: space-between; }
.grid-cards { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }

.card {
  background: var(--surface);
  border-radius: var(--r-l);
  padding: 16px;
  box-shadow: var(--shadow);
}
.card > h2, .card > .card-head { margin-bottom: 12px; }
.card-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; }
.empty { text-align: center; padding: 28px 16px; color: var(--text-2); }
.empty h3 { color: var(--text); margin-bottom: 6px; }
.empty .row { justify-content: center; margin-top: 12px; }

/* ---------- controls ---------- */
.btn {
  appearance: none; -webkit-appearance: none;
  min-height: var(--tap); min-width: var(--tap);
  padding: 0 16px; border: 0; border-radius: var(--r-m);
  background: var(--surface-2); color: var(--text);
  font: inherit; font-weight: 550; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  transition: transform 0.12s var(--ease), background 0.15s, opacity 0.15s;
}
.btn:active { transform: scale(0.97); }
.btn:hover { background: var(--accent-soft); }
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { background: var(--bad); color: #fff; }
.btn.ghost { background: transparent; }
.btn.ghost:hover { background: var(--accent-soft); }
.btn.icon-only { padding: 0; }
.btn[disabled] { opacity: 0.45; cursor: not-allowed; transform: none; }

input, select, textarea {
  font: inherit; color: var(--text);
  min-height: var(--tap);
  width: 100%;
  padding: 0 12px;
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: var(--r-s);
  transition: border-color 0.15s, background 0.15s;
}
input[type="color"] { padding: 4px; }
input[type="checkbox"], input[type="radio"] { width: 24px; min-height: 24px; accent-color: var(--accent); }
input:hover, select:hover { border-color: var(--sep); }
input:focus-visible, select:focus-visible { outline-offset: 0; border-color: var(--accent); }
input[aria-invalid="true"] { border-color: var(--bad); background: var(--bad-soft); }
input::placeholder { color: var(--text-2); opacity: 0.8; }

.field { display: grid; gap: 4px; margin-bottom: 12px; }
.field > label, legend { font-size: 0.85rem; font-weight: 600; color: var(--text-2); }
.field-check { display: flex; align-items: center; gap: 10px; }
.field-check > label { font-size: 1rem; color: var(--text); }
.hint { font-size: 0.82rem; color: var(--text-2); margin: 0; }
.form-error, .field-error { color: var(--bad); font-size: 0.85rem; margin: 4px 0 0; min-height: 0; }
.form-error:empty, .field-error:empty { display: none; }
fieldset { border: 0; padding: 0; margin: 0 0 12px; }

.seg { display: inline-flex; background: var(--surface-2); border-radius: var(--r-m); padding: 3px; gap: 2px; }
.seg label {
  position: relative; min-height: 44px; min-width: var(--tap); padding: 0 16px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 9px; cursor: pointer; font-weight: 550; color: var(--text);
  transition: background 0.2s var(--ease);
}
.seg input { position: absolute; opacity: 0; inset: 0; width: 100%; min-height: 100%; margin: 0; cursor: pointer; }
.seg label:has(input:checked) { background: var(--surface); box-shadow: var(--shadow); color: var(--accent); }
.seg label:has(input:focus-visible) { outline: 3px solid var(--accent); outline-offset: 1px; }

/* ---------- chips, badges ---------- */
.chip, .badge {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 26px; padding: 0 10px; border-radius: 999px;
  font-size: 0.8rem; font-weight: 600; white-space: nowrap;
  background: var(--surface-2); color: var(--text-2);
}
.chip-good { background: var(--good-soft); color: var(--good); }
.chip-warn { background: var(--warn-soft); color: var(--warn); }
.chip-bad { background: var(--bad-soft); color: var(--bad); }
.chip-accent { background: var(--accent-soft); color: var(--accent); }
.chip .dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.badge { color: #fff; }
.band-HD { background: var(--band-HD); }
.band-D { background: var(--band-D); }
.band-C { background: var(--band-C); }
.band-P { background: var(--band-P); }
.band-N { background: var(--band-N); }
:root[data-theme="dark"] .badge { color: #00110a; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .badge { color: #00110a; } }

.callout { border-radius: var(--r-m); padding: 10px 14px; background: var(--warn-soft); color: var(--text); border-left: 4px solid var(--warn); }
.callout.info { background: var(--accent-soft); border-left-color: var(--accent); }

/* ---------- toasts ---------- */
.toast-host {
  position: fixed; z-index: 90; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom));
  display: grid; gap: 8px; width: min(92vw, 420px); pointer-events: none;
}
@media (min-width: 768px) { .toast-host { bottom: 24px; left: calc(50% + var(--side-w) / 2); } }
.toast {
  pointer-events: auto; padding: 12px 16px; border-radius: var(--r-m);
  background: var(--material); color: var(--text);
  -webkit-backdrop-filter: saturate(180%) blur(20px); backdrop-filter: saturate(180%) blur(20px);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.22); border: 1px solid var(--sep);
  animation: toast-in 0.35s var(--ease);
  transition: opacity 0.25s, transform 0.25s var(--ease);
}
.toast-error { border-color: var(--bad); }
.toast-success { border-color: var(--good); }
.toast.leaving { opacity: 0; transform: translateY(8px); }
@keyframes toast-in { from { opacity: 0; transform: translateY(12px) scale(0.96); } }

/* ---------- dialog ---------- */
.dlg {
  border: 0; padding: 0; border-radius: var(--r-l);
  background: var(--surface); color: var(--text);
  width: min(92vw, 460px); max-height: 90dvh; overflow: auto;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}
.dlg::backdrop { background: rgba(0, 0, 0, 0.4); -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px); }
.dlg[open] { animation: dlg-in 0.35s var(--ease); }
@keyframes dlg-in { from { opacity: 0; transform: scale(0.94); } }
.dlg-form { padding: 20px; display: grid; gap: 4px; }
.dlg-form h2 { margin-bottom: 8px; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }

.error-panel { background: var(--bad-soft); border: 1px solid var(--bad); border-radius: var(--r-l); padding: 20px; }
.error-panel h2 { margin-bottom: 8px; }

/* ---------- assessments view ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: end; margin-bottom: 16px; }
.toolbar .field { margin: 0; min-width: 200px; flex: 1 1 200px; max-width: 360px; }
.subj { border-top: 4px solid var(--subj, var(--accent)); margin-bottom: 20px; padding: 0; overflow: hidden; }
.subj-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; padding: 16px 16px 8px; }
.subj-title { display: flex; align-items: center; gap: 10px; min-width: 0; }
.subj-title h2 { overflow-wrap: anywhere; }
.swatch { width: 14px; height: 14px; border-radius: 50%; background: var(--subj, var(--accent)); flex: none; }

.summary { margin: 8px 16px 16px; padding: 14px; background: var(--surface-2); border-radius: var(--r-m); display: grid; gap: 12px; }
.summary-stats { display: grid; gap: 12px 20px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); margin: 0; }
.summary-stats div { min-width: 0; }
.summary-stats dt { font-size: 0.78rem; color: var(--text-2); font-weight: 600; }
.summary-stats dd { margin: 2px 0 0; font-size: 1.15rem; font-weight: 650; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.summary-stats dd small { font-weight: 450; color: var(--text-2); font-size: 0.8rem; display: block; }
.need { margin: 0; font-size: 1.02rem; font-weight: 550; }
.target-row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.target-row label { font-weight: 600; font-size: 0.9rem; color: var(--text-2); }
.target-row select { width: auto; min-width: 150px; }

.table-wrap { overflow-x: auto; }
.as-table { width: 100%; border-collapse: collapse; }
.as-table th { text-align: left; font-size: 0.75rem; font-weight: 650; color: var(--text-2); padding: 6px 8px; border-bottom: 1px solid var(--sep); white-space: nowrap; }
.as-table td { padding: 8px; border-bottom: 1px solid var(--sep); vertical-align: top; }
.as-table tr:last-child td { border-bottom: 0; }
.as-table td.col-title { min-width: 220px; }
.as-table td.col-num { width: 104px; }
.as-table td.col-sel { width: 150px; }
.as-table td.col-date { width: 160px; }
.as-table td.col-actions { width: 100px; white-space: nowrap; }
.contrib { display: block; padding-top: 11px; font-variant-numeric: tabular-nums; }
.contrib.awaiting { color: var(--text-2); font-style: italic; }
details.more { margin-top: 6px; }
details.more > summary {
  cursor: pointer; color: var(--accent); font-size: 0.85rem; font-weight: 600;
  min-height: 32px; display: inline-flex; align-items: center;
}
details.more .more-grid { display: grid; gap: 8px; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); padding: 6px 0; }
details.more .field { margin: 0; }
.as-table .chips { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 4px; }

@media (max-width: 1240px) {
  .as-table, .as-table tbody, .as-table tr, .as-table td { display: block; }
  .as-table thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0, 0, 0, 0); }
  .as-table tr {
    display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px;
    padding: 14px 16px; border-bottom: 1px solid var(--sep);
  }
  .as-table tr:last-child { border-bottom: 0; }
  .as-table td { border: 0; padding: 0; width: auto !important; min-width: 0 !important; }
  .as-table td::before {
    content: attr(data-label); display: block;
    font-size: 0.75rem; font-weight: 650; color: var(--text-2); margin-bottom: 2px;
  }
  .as-table td.col-title, .as-table td.col-actions { grid-column: 1 / -1; }
  .as-table td.col-actions::before { content: none; }
  .as-table td.col-actions { display: flex; gap: 8px; }
  .contrib { padding-top: 11px; }
}

/* ---------- today view ---------- */
.due-list { list-style: none; margin: 0; padding: 0; display: grid; }
.due-item {
  display: grid; grid-template-columns: auto 1fr auto; gap: 4px 12px; align-items: center;
  padding: 12px 0; border-bottom: 1px solid var(--sep); min-height: var(--tap);
}
.due-item:last-child { border-bottom: 0; }
.due-item .when { text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }
.due-item .meta { grid-column: 2 / -1; color: var(--text-2); font-size: 0.85rem; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.proj-card { display: grid; gap: 6px; border-top: 4px solid var(--subj, var(--accent)); }
.proj-card .big { font-size: 2rem; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.proj-card a { color: inherit; text-decoration: none; }
.proj-card a:hover h3 { text-decoration: underline; }
.section-label { font-size: 0.8rem; font-weight: 650; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-2); margin: 16px 0 4px; }

/* ---------- settings view ---------- */
.settings-grid { display: grid; gap: 16px; }
.band-grid { display: grid; gap: 8px 12px; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); }
.sync-status { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 8px 0 12px; }
.data-row { display: grid; gap: 8px; padding: 12px 0; border-bottom: 1px solid var(--sep); }
.data-row:last-child { border-bottom: 0; }
.data-row input[type="file"] { padding: 8px 12px; }

/* ---------- motion ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
@media (forced-colors: active) {
  .btn, .chip, .badge { border: 1px solid ButtonText; }
  .seg label:has(input:checked) { outline: 2px solid Highlight; }
}
@media print { .nav, .toast-host { display: none; } .main { margin: 0; padding: 0; } }

/* ---------- v4 audit fixes ---------- */
a.chip { min-height: var(--tap); }
.sync-chip-host .chip { min-height: var(--tap); }
.update-banner {
  position: fixed; left: 12px; right: 12px; bottom: calc(var(--nav-h) + 12px + env(safe-area-inset-bottom, 0px)); z-index: 60;
  display: flex; gap: 12px; align-items: center; justify-content: space-between;
  padding: 10px 14px; border-radius: var(--r-m); background: var(--surface); color: var(--text);
  border: 1px solid var(--sep); box-shadow: 0 8px 30px rgba(0, 0, 0, 0.22);
}
.update-banner.warn { border-left: 4px solid var(--warn); }
@media (min-width: 900px) { .update-banner { left: auto; right: 24px; bottom: 24px; max-width: 420px; } }
.filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.filters .field { min-width: 140px; flex: 1 1 140px; }
.wam-card .big { font-size: 2rem; font-weight: 700; }
