:root {
  color-scheme: light dark;
  --bg: #f5f2ea;
  --surface: #fffdf8;
  --surface-2: #efebdf;
  --ink: #1d2a2c;
  --muted: #5d6a69;
  --line: #ddd7c7;
  --accent: #1c5e50;
  --accent-ink: #ffffff;
  --accent-soft: #dcebe4;
  --side: #17352f;
  --side-ink: #d7e4dd;
  --side-active: #2a5147;
  --good: #1f6b45;  --good-bg: #dcefe2;
  --warn: #85560a;  --warn-bg: #f8e9c8;
  --bad: #a3302a;   --bad-bg: #f9dfdb;
  --info: #2c5a85;  --info-bg: #dfeaf5;
  --bar: #2f8270;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121a1a;
    --surface: #1a2424;
    --surface-2: #233030;
    --ink: #e6ece8;
    --muted: #9fadaa;
    --line: #314140;
    --accent: #6fc5ae;
    --accent-ink: #10201c;
    --accent-soft: #24423b;
    --side: #0e1616;
    --side-ink: #c5d4ce;
    --side-active: #223a35;
    --good: #84d6a5;  --good-bg: #1d3b2b;
    --warn: #e9c077;  --warn-bg: #43341a;
    --bad: #f2a29b;   --bad-bg: #4a2522;
    --info: #9cc5ea;  --info-bg: #1f3549;
    --bar: #5fb8a2;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.5 var(--sans); }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; line-height: 1.2; margin: 0; }
h1 { font-size: 1.75rem; }
h2 { font-size: 1.15rem; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.nowrap { white-space: nowrap; }
.hidden { display: none !important; }

/* Shell */
.shell { display: grid; grid-template-columns: 224px minmax(0, 1fr); min-height: 100vh; }
.side { background: var(--side); color: var(--side-ink); padding: 22px 12px; display: flex; flex-direction: column; gap: 22px; position: sticky; top: 0; height: 100vh; overflow-y: auto; overscroll-behavior: contain; }
.brand { font: 600 1.3rem var(--serif); color: #fff; padding: 0 10px; }
.side nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.side nav a { color: inherit; text-decoration: none; padding: 8px 10px; border-radius: 6px; }
.side nav a:hover { background: var(--side-active); }
.side nav a[aria-current="page"] { background: var(--side-active); color: #fff; font-weight: 600; }
.who { padding: 0 10px; font-size: .85rem; }
.who strong { display: block; color: #fff; }
.who .links { display: flex; gap: 12px; margin-top: 6px; }
.who button, .who a { background: none; border: 0; padding: 0; font: inherit; color: var(--side-ink); text-decoration: underline; cursor: pointer; }
main { padding: 30px 34px 60px; max-width: 1180px; width: 100%; }
main:focus { outline: none; }

.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 20px; }
.toolbar { display: flex; flex-wrap: wrap; align-items: end; gap: 10px; margin-bottom: 16px; }
.toolbar label { display: grid; gap: 3px; font-size: .8rem; color: var(--muted); }
.spacer { flex: 1; }

/* Controls */
button, .btn { font: inherit; }
.btn { display: inline-block; background: var(--surface); color: var(--ink); border: 1px solid var(--line); border-radius: 6px; padding: 7px 14px; cursor: pointer; text-decoration: none; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.1); }
.btn.link { background: none; border: 0; padding: 2px 4px; color: var(--accent); text-decoration: underline; }
.btn.danger { color: var(--bad); }
.btn[disabled] { opacity: .55; cursor: default; }
input, select, textarea { font: inherit; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: 6px; padding: 7px 9px; width: 100%; min-width: 0; }
.toolbar input, .toolbar select { width: auto; min-width: 170px; max-width: 100%; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* Cards and tables */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 18px 20px; }
.card > h2 { margin-bottom: 12px; }
.table-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; overflow-x: auto; }
.card .table-wrap { border: 0; border-radius: 0; background: none; }
table { border-collapse: collapse; width: 100%; }
th { text-align: left; font-size: .78rem; font-weight: 600; color: var(--muted); letter-spacing: .02em; }
th, td { padding: 9px 14px; border-bottom: 1px solid var(--line); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
th.num { text-align: right; }
td.actions { text-align: right; white-space: nowrap; }
.empty { padding: 28px 16px; text-align: center; color: var(--muted); }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 14px; margin-bottom: 18px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 14px 18px; }
.stat .label { font-size: .82rem; color: var(--muted); }
.stat .value { font: 600 1.85rem/1.2 var(--serif); font-variant-numeric: tabular-nums; }
.stat .note { font-size: .8rem; color: var(--muted); }
.cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 340px), 1fr)); gap: 16px; align-items: start; }
.stack { display: grid; gap: 16px; }

.badge { display: inline-block; padding: 1px 9px; border-radius: 99px; font-size: .78rem; font-weight: 600; white-space: nowrap; }
.badge.good { color: var(--good); background: var(--good-bg); }
.badge.warn { color: var(--warn); background: var(--warn-bg); }
.badge.bad { color: var(--bad); background: var(--bad-bg); }
.badge.info { color: var(--info); background: var(--info-bg); }
.badge.plain { color: var(--muted); background: var(--surface-2); }

.list { list-style: none; margin: 0; padding: 0; }
.list li { padding: 9px 0; border-bottom: 1px solid var(--line); }
.list li:last-child { border-bottom: 0; }
.note-title { font-weight: 600; }
.note-body { white-space: pre-wrap; overflow-wrap: anywhere; }
.period { display: inline-block; min-width: 26px; font-weight: 600; color: var(--accent); }

/* Chart */
.chart { width: 100%; min-height: 200px; }
.chart svg { display: block; width: 100%; }
.chart text { fill: var(--muted); font: 12px var(--sans); }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .bar { fill: var(--bar); }
.chart .hit { fill: transparent; }
.chart .val { fill: var(--ink); font-weight: 600; opacity: 0; }
.chart g:hover .val, .chart g:focus .val, .chart g.latest .val { opacity: 1; }
.chart g:hover .hit, .chart g:focus .hit { fill: var(--surface-2); fill-opacity: .6; }
.chart g:focus { outline: none; }
details summary { cursor: pointer; color: var(--muted); font-size: .85rem; margin-top: 8px; }

/* Attendance */
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.seg label { cursor: pointer; }
.seg input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.seg span { display: block; padding: 4px 10px; font-size: .82rem; border-left: 1px solid var(--line); }
.seg label:first-child span { border-left: 0; }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }
.seg input:checked + span { font-weight: 700; }
.seg input[value="present"]:checked + span { background: var(--good-bg); color: var(--good); }
.seg input[value="late"]:checked + span { background: var(--warn-bg); color: var(--warn); }
.seg input[value="absent"]:checked + span { background: var(--bad-bg); color: var(--bad); }
.seg input[value="excused"]:checked + span { background: var(--info-bg); color: var(--info); }
.score { width: 92px; text-align: right; }
.savebar { display: flex; align-items: center; justify-content: flex-end; gap: 14px; margin-top: 14px; }

/* Timetable */
.tt { min-width: 700px; table-layout: fixed; }
.tt th, .tt td { border: 1px solid var(--line); padding: 6px; }
.tt th { text-align: center; }
.tt th:first-child { width: 58px; }
.tt td { height: 62px; vertical-align: top; position: relative; font-size: .85rem; line-height: 1.3; }
.tt .subj { font-weight: 600; display: block; padding-right: 14px; }
.tt .x { position: absolute; top: 2px; right: 3px; background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 1rem; line-height: 1; padding: 2px 4px; }
.tt .x:hover { color: var(--bad); }
.tt .add { display: block; width: 100%; height: 100%; min-height: 48px; background: none; border: 1px dashed transparent; border-radius: 4px; color: var(--muted); cursor: pointer; }
.tt .add:hover, .tt .add:focus-visible { border-color: var(--line); background: var(--surface-2); }

/* Dialog */
dialog { border: 1px solid var(--line); border-radius: 12px; background: var(--surface); color: var(--ink); padding: 22px 24px; width: min(540px, calc(100vw - 32px)); max-height: calc(100vh - 40px); }
dialog::backdrop { background: rgb(10 20 20 / .5); }
dialog h2 { margin-bottom: 14px; }
.fields { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field { display: grid; gap: 3px; align-content: start; font-size: .85rem; }
.field.wide { grid-column: 1 / -1; }
.field .help { color: var(--muted); font-size: .78rem; }
.req { color: var(--bad); }
.form-error { background: var(--bad-bg); color: var(--bad); border-radius: 6px; padding: 8px 12px; margin-top: 12px; font-size: .9rem; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }

/* Toast */
#toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 9px 18px; border-radius: 8px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 50; max-width: calc(100vw - 32px); }
#toast.show { opacity: 1; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login form { width: min(380px, 100%); display: grid; gap: 12px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 30px 28px; }
.login h1 { font-size: 2rem; }
.login .field { font-size: .9rem; }

@media (max-width: 800px) {
  .shell { display: block; }
  .side { position: static; height: auto; padding: 12px 16px; gap: 8px; }
  .side nav { flex-direction: row; overflow-x: auto; margin: 0 -4px; padding-bottom: 2px; }
  .side nav a { white-space: nowrap; }
  .who { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 0; }
  .brand { padding: 0; }
  main { padding: 20px 16px 50px; }
  .fields { grid-template-columns: 1fr; }
  .toolbar label, .toolbar input, .toolbar select { width: 100%; }
  th, td { padding: 8px 10px; }
  .hide-sm { display: none; }
  .seg span { padding: 4px 6px; font-size: .78rem; }
}
@media (prefers-reduced-motion: reduce) { #toast { transition: none; } }
@media print {
  .side, .toolbar, .page-head .btn, .savebar { display: none !important; }
  .shell { display: block; }
  main { padding: 0; max-width: none; }
}

/* login role tabs */
.login-tabs { display: flex; flex-wrap: wrap; gap: 6px; }
.login-tabs button { flex: 1 1 auto; font: inherit; font-size: .85rem; padding: 6px 10px; border: 1px solid var(--line); border-radius: 99px; background: var(--surface); color: var(--ink); cursor: pointer; }
.login-tabs button[aria-selected="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
