:root {
  --bg: #f3f5f9;
  --surface: #ffffff;
  --surface-2: #eef2f8;
  --text: #17202e;
  --muted: #5d6b80;
  --border: #dbe1ea;
  --accent: #1f5fbf;
  --accent-text: #ffffff;
  --accent-soft: #e3ecfa;
  --warn: #9a5b00;
  --warn-soft: #fff1d6;
  --bad: #b3261e;
  --bad-soft: #fde4e1;
  --good: #1b7f3a;
  --g1: #1b7f3a;
  --g2: #4f9a2c;
  --g3: #b07d00;
  --g4: #d35400;
  --g5: #c0392b;
  --mark-other: #5d6b80;
  --shadow: 0 1px 2px rgba(23, 32, 46, .06), 0 2px 8px rgba(23, 32, 46, .04);
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141c;
    --surface: #18202b;
    --surface-2: #212b38;
    --text: #e6ebf2;
    --muted: #97a3b6;
    --border: #2b3645;
    --accent: #6ea8ff;
    --accent-text: #0b1220;
    --accent-soft: #1d2d47;
    --warn: #f0b44c;
    --warn-soft: #3a2c12;
    --bad: #ff8a80;
    --bad-soft: #3d1d1b;
    --good: #6fd38f;
    --g1: #2f9e55;
    --g2: #6aa83c;
    --g3: #c49212;
    --g4: #e0702a;
    --g5: #e0574a;
    --mark-other: #66748a;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2 { line-height: 1.2; margin: 0 0 12px; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.1rem; }
p { margin: 0 0 10px; }
p:last-child { margin-bottom: 0; }

.muted { color: var(--muted); font-weight: normal; }
.small { font-size: .85rem; }

/* --- header ------------------------------------------------------------ */

.top {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding-top: env(safe-area-inset-top);
}

.top-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  max-width: 1100px;
  margin: 0 auto;
  padding: 10px 16px 4px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  font-weight: 700;
  font-size: 1.1rem;
}
.brand:hover { text-decoration: none; }

.who {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}
.who-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: .9rem;
}
.who form { margin: 0; }

.link-button {
  background: none;
  border: 0;
  padding: 0;
  color: var(--accent);
  font: inherit;
  font-size: .9rem;
  cursor: pointer;
}
.link-button:hover { text-decoration: underline; }

.tabs {
  display: flex;
  gap: 4px;
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 12px;
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tabs a {
  flex: none;
  padding: 10px 10px 9px;
  color: var(--muted);
  font-weight: 600;
  font-size: .95rem;
  border-bottom: 3px solid transparent;
}
.tabs a:hover { color: var(--text); text-decoration: none; }
.tabs a[aria-current="page"] { color: var(--accent); border-bottom-color: var(--accent); }

/* --- layout ------------------------------------------------------------ */

.page {
  max-width: 1100px;
  margin: 0 auto;
  padding: 16px 16px calc(32px + env(safe-area-inset-bottom));
}

.card {
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 16px;
  min-width: 0;
}

.grid-2 { display: grid; gap: 0 16px; }
.stack { min-width: 0; }
@media (min-width: 820px) {
  .grid-2 { grid-template-columns: 1fr 1fr; }
}

.page-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 12px;
  margin-bottom: 12px;
}
.page-head h1 { margin: 0; }

.section-title { font-size: 1rem; color: var(--muted); margin: 20px 0 10px; }

.button-row { display: flex; flex-wrap: wrap; gap: 6px; }

.button {
  display: inline-block;
  padding: 10px 16px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
}
.button:hover { text-decoration: none; filter: brightness(1.05); }
.button.secondary {
  background: var(--surface-2);
  color: var(--text);
  padding: 7px 12px;
  font-weight: 500;
}

.pill {
  display: inline-block;
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: .75rem;
  font-weight: 600;
  vertical-align: 1px;
}

.notice { font-weight: 600; background: var(--accent-soft); border-color: transparent; }
.notice:hover { text-decoration: none; filter: brightness(.98); }

.alert {
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--bad-soft);
  color: var(--bad);
}

.plain-list { list-style: none; margin: 0 0 10px; padding: 0; }
.plain-list > li {
  display: flex;
  gap: 10px;
  align-items: baseline;
  padding: 8px 0;
  border-top: 1px solid var(--border);
}
.plain-list > li:first-child { border-top: 0; padding-top: 0; }

.clamp {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  min-width: 0;
}

.due { flex: none; color: var(--muted); font-size: .85rem; font-weight: 600; }
.due.soon { color: var(--bad); }

/* --- login ------------------------------------------------------------- */

.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 16px;
}
.login {
  width: 100%;
  max-width: 380px;
  display: grid;
  gap: 6px;
}
.login-head { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.login-head h1 { margin: 0; }
.login label { font-weight: 600; font-size: .9rem; margin-top: 8px; }
.login input, .login select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}
.login input:focus, .login select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.login .button { margin: 14px 0 6px; }
.school-name { margin: 0; }

/* --- marks ------------------------------------------------------------- */

.mark {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 2.1em;
  height: 2.1em;
  padding: 0 6px;
  border-radius: 8px;
  background: var(--mark-other);
  color: #fff;
  font-weight: 700;
  font-size: .9rem;
  line-height: 1;
}
.mark.g1 { background: var(--g1); }
.mark.g2 { background: var(--g2); }
.mark.g3 { background: var(--g3); }
.mark.g4 { background: var(--g4); }
.mark.g5 { background: var(--g5); }

.mark-row { align-items: center !important; }
.mark-info { display: flex; flex-direction: column; min-width: 0; }

details > summary { cursor: pointer; list-style: none; }
details > summary::-webkit-details-marker { display: none; }

.subject { padding: 0; }
.subject > summary {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "name avg" "marks marks";
  gap: 6px 12px;
  align-items: center;
  padding: 14px 16px;
}
.subject[open] > summary { border-bottom: 1px solid var(--border); }
.subject > :not(summary) { margin: 12px 16px; }
.subject-name { grid-area: name; font-weight: 600; }
.subject > summary > .muted { grid-column: 1 / -1; }
.subject-marks { grid-area: marks; display: flex; flex-wrap: wrap; gap: 4px; }
.subject-marks .mark { min-width: 1.9em; height: 1.9em; font-size: .8rem; }
.subject[open] .subject-marks { display: none; }
.average {
  grid-area: avg;
  font-weight: 700;
  font-size: 1.15rem;
  font-variant-numeric: tabular-nums;
}
@media (min-width: 820px) {
  .subject > summary { grid-template-columns: 16em 1fr auto; grid-template-areas: "name marks avg"; }
}

/* --- timetable --------------------------------------------------------- */

.lessons { list-style: none; margin: 0 0 10px; padding: 0; }
.lesson {
  display: flex;
  gap: 12px;
  padding: 8px 0;
  border-top: 1px solid var(--border);
}
.lesson:first-child { border-top: 0; padding-top: 0; }
.lesson-time { flex: none; width: 4.2em; color: var(--muted); font-size: .9rem; font-variant-numeric: tabular-nums; }
.lesson-time strong { color: var(--text); }
.lesson-main { display: flex; flex-direction: column; min-width: 0; }
.lesson-subject { font-weight: 600; }
.lesson-theme { color: var(--muted); font-style: italic; }
.change-note {
  margin-top: 2px;
  font-size: .85rem;
  color: var(--warn);
  font-weight: 600;
}
.lesson.canceled .lesson-subject { text-decoration: line-through; color: var(--muted); }
.lesson.canceled .change-note { color: var(--bad); }

.tt-wrap { padding: 0; overflow-x: auto; display: none; }
.tt { width: 100%; border-collapse: collapse; table-layout: fixed; }
.tt th, .tt td {
  border: 1px solid var(--border);
  padding: 6px;
  vertical-align: top;
  text-align: center;
}
.tt thead th { background: var(--surface-2); border-top: 0; }
.tt tr > :first-child { border-left: 0; width: 4.5em; }
.tt tr > :last-child { border-right: 0; }
.tt tbody tr:last-child > * { border-bottom: 0; }
.tt th span, .tt-lesson > * { display: block; }
.tt-hour { font-weight: 700; }
.tt-day { font-weight: 700; }
.tt tr.today th[scope="row"] { color: var(--accent); }
.tt tr.today td, .tt tr.today th[scope="row"] { background: var(--accent-soft); }
.tt-off { color: var(--muted); font-style: italic; vertical-align: middle !important; }
.tt-lesson { padding: 2px; border-radius: 6px; }
.tt-lesson + .tt-lesson { margin-top: 4px; border-top: 1px dashed var(--border); padding-top: 4px; }
.tt-lesson.changed { background: var(--warn-soft); }
.tt-lesson.canceled { background: var(--bad-soft); }
.tt-lesson.canceled strong { text-decoration: line-through; }

.tt-days .card.today { border-color: var(--accent); }

@media (min-width: 760px) {
  .tt-wrap { display: block; }
  .tt-days { display: none; }
}

/* --- homework ---------------------------------------------------------- */

.hw-head { display: flex; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.hw-content { margin-bottom: 10px; overflow-wrap: anywhere; }
.hw.done { opacity: .7; }
.hw.done .due { color: var(--good); }
.past > summary { cursor: pointer; }

.attachments { list-style: none; padding: 0; margin: 0 0 10px; display: grid; gap: 4px; }
.attachments a { overflow-wrap: anywhere; }

/* --- absence ----------------------------------------------------------- */

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 0 16px;
}
.stat { display: flex; flex-direction: column; }
.stat-value { font-size: 1.6rem; font-weight: 700; font-variant-numeric: tabular-nums; }

.table-wrap { overflow-x: auto; margin: 0 -16px; padding: 0 16px; }
.table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.table th, .table td { padding: 7px 8px; border-top: 1px solid var(--border); text-align: left; }
.table thead th { border-top: 0; color: var(--muted); font-weight: 600; font-size: .85rem; white-space: nowrap; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table tr.over td { color: var(--bad); font-weight: 600; }
.table td.warn { color: var(--warn); font-weight: 600; }
.table td.bad { color: var(--bad); font-weight: 600; }
.table meter { width: 56px; height: 10px; vertical-align: middle; margin-right: 4px; }

/* --- messages ---------------------------------------------------------- */

.message { padding: 0; }
.message > summary {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 14px 16px;
}
.message[open] > summary { border-bottom: 1px solid var(--border); }
.message > :not(summary) { margin: 12px 16px; }
.message-title { font-weight: 600; display: flex; align-items: center; gap: 8px; }
.message.unread .message-title { color: var(--accent); }
.dot { flex: none; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
.message-text { overflow-wrap: anywhere; }
.message-text img { max-width: 100%; height: auto; }
.message-text table { border-collapse: collapse; max-width: 100%; display: block; overflow-x: auto; }
.message-text td, .message-text th { border: 1px solid var(--border); padding: 4px 6px; }

/* --- predictors ---------------------------------------------------------- */

.predict {
  margin-top: 4px;
  padding: 12px;
  border-radius: 10px;
  background: var(--surface-2);
}
.predict h3 { font-size: .95rem; margin: 0 0 8px; }
.p-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.p-row label { display: inline-flex; align-items: center; gap: 6px; font-size: .9rem; }
.predict input, .predict select, .f-controls input, .forecast input {
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: .95rem;
}
.predict input, .forecast input { width: 4.2em; }
.predict .button.secondary { background: var(--surface); }
.chips { list-style: none; padding: 0; margin: 8px 0; display: flex; flex-wrap: wrap; gap: 6px; }
.chips li {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 4px 2px 10px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: .85rem;
  font-weight: 600;
}
.chip-x {
  border: 0;
  background: none;
  color: inherit;
  font-size: 1.1rem;
  line-height: 1;
  padding: 2px 6px;
  cursor: pointer;
}
.p-result { margin: 8px 0; }
.p-change.good { color: var(--good); }
.p-change.bad { color: var(--bad); }
.p-need { white-space: nowrap; }

.f-controls { display: flex; flex-wrap: wrap; align-items: end; gap: 10px 16px; margin-top: 12px; }
.f-controls label { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; font-weight: 600; }
.forecast td { vertical-align: middle; }
.forecast input { text-align: right; }
.forecast input:disabled { opacity: .4; }
.forecast tr.over td { color: var(--bad); font-weight: 600; }
.forecast .f-percent { display: inline-block; min-width: 3.6em; }
@media (max-width: 640px) {
  .forecast .hide-narrow, .forecast meter { display: none; }
  .forecast th, .forecast td { padding: 7px 4px; }
  .forecast th:first-child, .forecast td:first-child { padding-left: 0; }
  .forecast input { width: 2.8em; padding: 5px 6px; }
}
