:root {
  --primary: #3457c9;
  --on-primary: #fff;
  --primary-container: #dce2ff;
  --on-primary-container: #0f1d5c;
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #eef0f7;
  --text: #1a1c22;
  --text-2: #5b5f6d;
  --border: #e1e3ec;
  --danger: #c62828;
  --danger-container: #fde3e3;
  --warn: #a15c00;
  --warn-container: #fff0d6;
  --ok: #1b7f3b;
  --ok-container: #ddf3e3;
  --shadow: 0 1px 2px rgba(20, 25, 50, .06), 0 2px 8px rgba(20, 25, 50, .05);
  --radius: 16px;
  --tabbar-h: 58px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

:root[data-theme="dark"] {
  --primary: #b4c3ff;
  --on-primary: #10226b;
  --primary-container: #2b3f94;
  --on-primary-container: #dce2ff;
  --bg: #111318;
  --surface: #1b1d24;
  --surface-2: #252832;
  --text: #e4e5ec;
  --text-2: #a2a6b5;
  --border: #2e3140;
  --danger: #ff8a80;
  --danger-container: #4a1c1c;
  --warn: #ffc46b;
  --warn-container: #45320f;
  --ok: #7fd89a;
  --ok-container: #173822;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

body { min-height: 100dvh; overscroll-behavior-y: none; }

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--primary); }

/* ── layout ─────────────────────────────────────────── */

.topbar {
  position: sticky; top: 0; z-index: 20;
  padding: calc(var(--safe-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.6) blur(14px);
  -webkit-backdrop-filter: saturate(1.6) blur(14px);
  display: flex; align-items: center; gap: 8px;
}
.topbar h1 { font-size: 26px; font-weight: 700; margin: 0; flex: 1; letter-spacing: -.3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .back { font-size: 17px; font-weight: 600; flex: none; }

.icon-btn {
  border: 0; background: var(--surface-2); color: var(--text);
  width: 38px; height: 38px; border-radius: 50%;
  display: inline-grid; place-items: center; flex: none;
}
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn.spin svg { animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

main {
  padding: 4px 16px calc(var(--tabbar-h) + var(--safe-bottom) + 24px);
  max-width: 760px; margin: 0 auto;
}

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  display: flex;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(1.6) blur(14px);
  -webkit-backdrop-filter: saturate(1.6) blur(14px);
  border-top: 1px solid var(--border);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--text-2); text-decoration: none; font-size: 11px; font-weight: 500;
}
.tabbar a svg { width: 24px; height: 24px; }
.tabbar a.active { color: var(--primary); }
.tabbar a.active .pill { background: var(--primary-container); }
.tabbar .pill { padding: 2px 16px; border-radius: 14px; display: grid; place-items: center; transition: background .2s; }
:root[data-theme="dark"] .tabbar a.active { color: var(--on-primary-container); }

/* ── components ─────────────────────────────────────── */

.card {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px 16px; margin: 0 0 12px;
}
.card.tap { cursor: pointer; }
.card.tap:active { transform: scale(.99); }

.section-title {
  font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .6px;
  color: var(--text-2); margin: 20px 4px 8px;
  display: flex; justify-content: space-between; align-items: baseline;
}
.section-title a { text-transform: none; letter-spacing: 0; font-weight: 500; text-decoration: none; }

.list { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; margin-bottom: 12px; }
.row {
  display: flex; gap: 12px; align-items: center; padding: 12px 16px;
  border-bottom: 1px solid var(--border); text-decoration: none; color: inherit;
}
.row:last-child { border-bottom: 0; }
.row.tap { cursor: pointer; }
.row.tap:active { background: var(--surface-2); }
.row .main { flex: 1; min-width: 0; }
.row .title { font-weight: 600; }
.row .sub { color: var(--text-2); font-size: 14px; }
.row .trail { color: var(--text-2); font-size: 14px; text-align: right; flex: none; }
.row .chev { color: var(--text-2); opacity: .6; flex: none; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.clamp2 { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

.muted { color: var(--text-2); }
.small { font-size: 13px; }
.center { text-align: center; }
.empty { text-align: center; color: var(--text-2); padding: 32px 16px; }
.empty .big { font-size: 40px; margin-bottom: 6px; }

.chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 10px; border-radius: 999px; font-size: 13px; font-weight: 600;
  background: var(--surface-2); color: var(--text);
}
.chip.primary { background: var(--primary-container); color: var(--on-primary-container); }
.chip.danger { background: var(--danger-container); color: var(--danger); }
.chip.warn { background: var(--warn-container); color: var(--warn); }
.chip.ok { background: var(--ok-container); color: var(--ok); }

.segmented {
  display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; gap: 3px; margin: 4px 0 12px;
  overflow-x: auto; scrollbar-width: none;
}
.segmented::-webkit-scrollbar { display: none; }
.segmented button {
  flex: 1; border: 0; background: transparent; padding: 7px 10px; border-radius: 9px;
  font-size: 14px; font-weight: 600; color: var(--text-2); white-space: nowrap;
}
.segmented.tight button { font-size: 13px; padding: 7px 4px; }
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.btn {
  border: 0; border-radius: 14px; padding: 13px 18px; font-weight: 600;
  background: var(--primary); color: var(--on-primary); width: 100%;
}
.btn:disabled { opacity: .6; }
.btn.secondary { background: var(--surface-2); color: var(--text); }
.btn.danger { background: var(--danger-container); color: var(--danger); }

.field { margin-bottom: 12px; }
.field label { display: block; font-size: 14px; font-weight: 600; margin: 0 0 6px 2px; color: var(--text-2); }
.field input, .field select {
  width: 100%; padding: 13px 14px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); font-size: 16px; outline: none;
}
.field input:focus { border-color: var(--primary); }
.check { display: flex; gap: 10px; align-items: flex-start; font-size: 14px; color: var(--text-2); margin: 4px 2px 16px; }
.check input { width: 20px; height: 20px; margin: 0; flex: none; accent-color: var(--primary); }

.banner {
  border-radius: 12px; padding: 10px 14px; font-size: 14px; margin-bottom: 12px;
  background: var(--warn-container); color: var(--warn);
}
.banner.error { background: var(--danger-container); color: var(--danger); }
.banner.info { background: var(--primary-container); color: var(--on-primary-container); }

.skeleton { height: 64px; border-radius: var(--radius); margin-bottom: 12px; background: linear-gradient(90deg, var(--surface) 0%, var(--surface-2) 50%, var(--surface) 100%); background-size: 200% 100%; animation: sk 1.2s infinite; }
@keyframes sk { to { background-position: -200% 0; } }

/* ── grades ─────────────────────────────────────────── */

.grade {
  display: inline-grid; place-items: center; min-width: 34px; height: 34px; padding: 0 6px;
  border-radius: 10px; font-weight: 700; font-size: 15px;
  background: var(--surface-2); color: var(--text);
}
.grade.sm { min-width: 28px; height: 28px; font-size: 14px; border-radius: 8px; }
.grade.g6 { background: #1e7a46; color: #fff; }
.grade.g5 { background: #3aa35f; color: #fff; }
.grade.g4 { background: #8bc34a; color: #10300a; }
.grade.g3 { background: #f2c230; color: #3d2d00; }
.grade.g2 { background: #f28a30; color: #3a1800; }
.grade.g1 { background: #e0453a; color: #fff; }
.grades-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.avg { font-size: 22px; font-weight: 700; }
.subject-head { display: flex; align-items: center; gap: 12px; }
.subject-head .name { flex: 1; font-weight: 600; min-width: 0; }
.summary-chips { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }

.hero {
  background: var(--primary); color: var(--on-primary);
  border-radius: 22px; padding: 18px; margin-bottom: 12px; display: flex; gap: 14px; align-items: center;
}
:root[data-theme="dark"] .hero { background: var(--primary-container); color: var(--on-primary-container); }
.hero .name { font-size: 20px; font-weight: 700; }
.hero .meta { opacity: .85; font-size: 14px; }
.lucky {
  margin-left: auto; text-align: center; background: rgba(255,255,255,.18);
  border-radius: 16px; padding: 8px 12px; min-width: 70px;
}
.lucky { white-space: nowrap; flex: none; padding: 8px 10px; }
.hero > div:first-child { flex: 1; min-width: 0; }
.lucky .n { font-size: 26px; font-weight: 800; line-height: 1.1; }
.lucky .l { font-size: 10px; opacity: .9; text-transform: uppercase; letter-spacing: .4px; }

/* ── timetable ──────────────────────────────────────── */

.week-nav { display: flex; align-items: center; gap: 8px; margin: 4px 0 10px; }
.week-nav .label { flex: 1; text-align: center; font-weight: 600; }
.days { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; margin-bottom: 12px; }
.days button {
  border: 0; background: var(--surface); border-radius: 14px; padding: 8px 0;
  display: flex; flex-direction: column; align-items: center; box-shadow: var(--shadow);
}
.days button .dn { font-size: 12px; color: var(--text-2); font-weight: 600; }
.days button .dd { font-size: 18px; font-weight: 700; }
.days button.today .dd { color: var(--primary); }
.days button.active { background: var(--primary); color: var(--on-primary); }
.days button.active .dn { color: inherit; opacity: .85; }
.days button.active .dd { color: inherit; }
.days .dot { width: 5px; height: 5px; border-radius: 50%; background: var(--warn); margin-top: 2px; }
.days .dot.none { background: transparent; }

.lesson { display: flex; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--border); align-items: flex-start; }
.lesson:last-child { border-bottom: 0; }
.lesson .num {
  flex: none; width: 34px; text-align: center;
}
.lesson .num b { display: block; font-size: 18px; }
.lesson .num span { font-size: 11px; color: var(--text-2); }
.lesson .main { flex: 1; min-width: 0; }
.lesson .title { font-weight: 600; }
.lesson .sub { color: var(--text-2); font-size: 14px; }
.lesson.cancelled .title { text-decoration: line-through; color: var(--text-2); }
.lesson .change { font-size: 13px; margin-top: 4px; }
.lesson .room { flex: none; font-weight: 600; font-size: 14px; color: var(--text-2); text-align: right; }

/* ── attendance ─────────────────────────────────────── */

.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.stat { background: var(--surface); border-radius: var(--radius); padding: 12px; text-align: center; box-shadow: var(--shadow); }
.stat .v { font-size: 22px; font-weight: 700; }
.stat .l { font-size: 12px; color: var(--text-2); }
.bar { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; margin-top: 6px; }
.bar > div { height: 100%; background: var(--ok); border-radius: 3px; }
.dot-kind { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.k-present { background: var(--ok); }
.k-absent { background: var(--danger); }
.k-justified { background: #7e8bd8; }
.k-late { background: #f2a33a; }
.k-exempt { background: #9aa0ad; }
.k-other, .k-none { background: var(--border); }

/* ── sheet ──────────────────────────────────────────── */

.sheet-backdrop {
  position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.35);
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade .2s;
}
.sheet {
  background: var(--surface); width: 100%; max-width: 760px; max-height: 88dvh; overflow-y: auto;
  border-radius: 22px 22px 0 0; padding: 10px 18px calc(var(--safe-bottom) + 20px);
  animation: up .25s cubic-bezier(.2,.8,.2,1);
}
.sheet .grab { width: 38px; height: 5px; border-radius: 3px; background: var(--border); margin: 0 auto 12px; }
.sheet h2 { margin: 0 0 4px; font-size: 20px; }
.sheet .kv { display: flex; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.sheet .kv:last-child { border-bottom: 0; }
.sheet .kv span:first-child { color: var(--text-2); }
.sheet .kv span:last-child { text-align: right; font-weight: 500; }
@keyframes fade { from { opacity: 0; } }
@keyframes up { from { transform: translateY(40%); opacity: .5; } }

.html-content { overflow-wrap: anywhere; line-height: 1.5; }
.html-content img { max-width: 100%; height: auto; }
.html-content table { border-collapse: collapse; max-width: 100%; display: block; overflow-x: auto; }
.html-content td, .html-content th { border: 1px solid var(--border); padding: 4px 6px; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 14px); z-index: 60;
  background: #23252d; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px;
  box-shadow: 0 4px 16px rgba(0,0,0,.25); max-width: calc(100% - 32px); animation: fade .2s;
}

.ptr {
  height: 0; overflow: hidden; display: grid; place-items: center; color: var(--text-2);
  transition: height .2s;
}
.ptr svg { width: 22px; height: 22px; }

/* ── login ──────────────────────────────────────────── */

.login { max-width: 420px; margin: 0 auto; padding: calc(var(--safe-top) + 40px) 20px 40px; }
.login .logo { width: 76px; height: 76px; border-radius: 20px; display: block; margin: 0 auto 14px; box-shadow: var(--shadow); }
.login h1 { text-align: center; margin: 0 0 4px; font-size: 28px; }
.login .lead { text-align: center; color: var(--text-2); margin: 0 0 24px; }

.install-hint { display: flex; gap: 10px; align-items: center; }
.install-hint svg { flex: none; width: 22px; height: 22px; }

/* own activities in the timetable */
.lesson.custom { background: color-mix(in srgb, var(--primary-container) 35%, var(--surface)); cursor: pointer; }
.lesson.custom .num b { color: var(--primary); }

/* homework due today / tomorrow */
.row.due-tomorrow { background: color-mix(in srgb, var(--warn-container) 70%, var(--surface)); }
.row.due-today { background: color-mix(in srgb, var(--danger-container) 55%, var(--surface)); }

/* iOS gives time/date inputs a wide intrinsic size; keep them inside their column */
.time-pair { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; }
.field input[type="time"], .field input[type="date"] {
  -webkit-appearance: none; appearance: none; min-width: 0; max-width: 100%; display: block;
  min-height: 48px; text-align: left; background: var(--surface);
}
.field input::-webkit-date-and-time-value { text-align: left; }
.pill-select { flex: none; border: 1px solid var(--border); border-radius: 10px; padding: 6px 8px; background: var(--surface); font-size: 15px; }
