:root {
  --bg: #f6f5f1;
  --card: #ffffff;
  --text: #1d1d1f;
  --muted: #6b6b70;
  --line: #e4e2dc;
  --accent: #2f6b4f;
  --accent-text: #ffffff;
  --accent-soft: #e3efe8;
  --warn: #9a5b00;
  --warn-soft: #fbefd9;
  --error: #b42318;
  --radius: 14px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16171a;
    --card: #212226;
    --text: #ececef;
    --muted: #9a9aa2;
    --line: #2f3036;
    --accent: #5fbf8c;
    --accent-text: #0e1a13;
    --accent-soft: #1f3329;
    --warn: #f0b45a;
    --warn-soft: #3a2e1b;
    --error: #ff8a80;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
main {
  max-width: 720px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 16px) 16px calc(env(safe-area-inset-bottom) + 48px);
}
h1 { font-size: 1.6rem; margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 0 0 8px; display: flex; align-items: center; gap: 8px; }
.count { background: var(--line); color: var(--text); border-radius: 99px; padding: 0 8px; font-size: 0.75rem; letter-spacing: 0; }
.muted { color: var(--muted); }
.empty { margin: 4px 0 0; }
.empty.big { text-align: center; padding: 48px 0; }

button {
  font: inherit;
  border: 0;
  border-radius: 10px;
  padding: 8px 14px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  cursor: pointer;
  min-height: 40px;
}
button:disabled { opacity: 0.5; cursor: default; }
button.secondary { background: var(--accent-soft); color: var(--accent); }
button.small { min-height: 32px; padding: 4px 10px; font-size: 0.85rem; }
button.link { background: none; color: var(--accent); padding: 4px 6px; min-height: 32px; font-weight: 500; }
button.icon { background: var(--card); color: var(--text); width: 40px; padding: 0; border: 1px solid var(--line); }
button:focus-visible, input:focus-visible, summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

input {
  font: inherit;
  color: var(--text);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 10px;
  min-height: 40px;
}

header { margin-bottom: 16px; }
.title-row { display: flex; align-items: center; justify-content: space-between; }
.icons { display: flex; gap: 8px; }
.sync { display: flex; align-items: center; gap: 4px; color: var(--muted); font-size: 0.9rem; margin-top: 2px; }
.sync.error { color: var(--error); }

.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 16px; }
.chip { background: var(--card); color: var(--text); border: 1px solid var(--line); border-radius: 99px; font-weight: 500; white-space: nowrap; }
.chip.active { background: var(--text); color: var(--bg); border-color: var(--text); }

.totals { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 24px; }
.total-card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; display: flex; flex-direction: column; gap: 2px; align-items: flex-start; }
.total-name { font-weight: 600; }
.total-sum { font-size: 1.7rem; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--warn); }
.total-card.zero .total-sum { color: var(--accent); }
.total-sub { color: var(--muted); font-size: 0.85rem; margin-bottom: 6px; }

.group { margin-bottom: 28px; }
.exams { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.exam {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  display: grid;
  grid-template-columns: 48px 1fr;
  gap: 4px 12px;
  align-items: start;
}
.exam-main { min-width: 0; }
.exam-meta { color: var(--muted); font-size: 0.8rem; }
.exam-title { font-weight: 600; }
.exam-name { color: var(--text); overflow-wrap: anywhere; }
.exam-verbal { margin: 4px 0 0; font-size: 0.9rem; color: var(--muted); white-space: pre-line; }
.exam-info { margin-top: 4px; font-size: 0.9rem; }
.exam-info summary { color: var(--accent); cursor: pointer; }
.exam-info p { white-space: pre-line; margin: 4px 0 0; color: var(--muted); }
.exam-actions { grid-column: 2; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-top: 6px; }

.grade, .date-badge {
  width: 48px; height: 48px; border-radius: 12px;
  display: grid; place-items: center;
  font-weight: 700; font-size: 1.15rem;
  background: var(--warn-soft); color: var(--warn);
}
.grade.empty { color: var(--muted); background: var(--line); }
.done .grade { background: var(--accent-soft); color: var(--accent); }
.date-badge { background: var(--bg); color: var(--text); border: 1px solid var(--line); }
.date-badge span { display: flex; flex-direction: column; align-items: center; line-height: 1; }
.date-badge small { font-size: 0.7rem; font-weight: 500; color: var(--muted); margin-top: 2px; }
.waiting .date-badge { color: var(--muted); }

.amount-wrap { display: flex; align-items: center; gap: 4px; }
.amount { width: 72px; text-align: right; font-variant-numeric: tabular-nums; }
.paid-label { display: flex; flex-direction: column; font-weight: 600; color: var(--accent); }
.paid-label small { font-weight: 400; color: var(--muted); font-size: 0.75rem; }
.done.skipped .paid-label { color: var(--muted); }
.done.skipped { opacity: 0.75; }

.done-group summary { list-style: none; cursor: pointer; }
.done-group summary::-webkit-details-marker { display: none; }
.done-group summary h2::after { content: "▸"; margin-left: 2px; }
.done-group[open] summary h2::after { content: "▾"; }

.login { max-width: 320px; margin: 15vh auto 0; display: flex; flex-direction: column; gap: 10px; }
.login h1 { margin-bottom: 12px; }
.login label { font-size: 0.9rem; color: var(--muted); }
.logo { width: 56px; height: 56px; border-radius: 16px; background: var(--accent); color: var(--accent-text); display: grid; place-items: center; font-size: 28px; }
.error-text { color: var(--error); margin: 0; }

dialog { border: 0; border-radius: 18px; padding: 20px; background: var(--card); color: var(--text); width: min(420px, calc(100vw - 32px)); }
dialog::backdrop { background: rgb(0 0 0 / 0.4); }
dialog h2 { font-size: 1.15rem; text-transform: none; letter-spacing: 0; color: var(--text); }
dialog p { font-size: 0.9rem; margin: 0 0 12px; }
.rate-row { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.rate-row label { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 0.9rem; }
.rate-row input { width: 64px; text-align: right; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

#toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 20px);
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 12px; font-size: 0.95rem;
  transition: opacity 0.2s, transform 0.2s; max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { background: var(--error); color: #fff; }

@media (min-width: 560px) {
  .exam { grid-template-columns: 48px 1fr auto; }
  .exam-actions { grid-column: 3; margin-top: 0; justify-content: flex-end; }
}
@media (prefers-reduced-motion: reduce) { #toast { transition: none; } }

/* WhatsApp dialog */
#wa-dialog { width: min(460px, calc(100vw - 32px)); }
.wa-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.wa-head h2 { margin: 0; }
.wa p { font-size: 0.92rem; margin: 8px 0; }
.wa-status { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.wa-status .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); }
.wa-status.open .dot { background: var(--accent); }
.wa-status.linking .dot, .wa-status.connecting .dot { background: var(--warn); }
.wa-qr { background: #fff; border-radius: 12px; padding: 8px; width: min(280px, 100%); margin: 8px auto; }
.wa-qr svg { display: block; width: 100%; height: auto; }
.wa-pair { margin-top: 8px; font-size: 0.92rem; }
.wa-pair summary { color: var(--accent); cursor: pointer; }
.wa-pair .row { display: flex; gap: 8px; }
.wa-pair input { flex: 1; min-width: 0; }
.wa-code { font: 700 1.8rem ui-monospace, monospace; letter-spacing: 0.12em; text-align: center; margin: 12px 0; }
.wa-form { display: flex; flex-direction: column; gap: 12px; margin-top: 12px; }
.wa-form .field { display: flex; flex-direction: column; gap: 4px; font-size: 0.9rem; color: var(--muted); }
.wa-form select { font: inherit; color: var(--text); background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; min-height: 40px; }
.wa-form fieldset { border: 1px solid var(--line); border-radius: 12px; padding: 8px 12px 12px; margin: 0; display: flex; flex-direction: column; gap: 6px; }
.wa-form legend { font-size: 0.9rem; color: var(--muted); padding: 0 4px; }
.check { display: flex; align-items: center; gap: 8px; min-height: 32px; }
.check input { width: 20px; height: 20px; min-height: 0; accent-color: var(--accent); }
.check-row { display: flex; align-items: center; gap: 6px; }
.hour { width: 64px; }
button.link.danger { color: var(--error); margin-top: 8px; padding-left: 0; }

/* Exam timeline: Lisätty → Koe → Arvosana → Maksettu */
.timeline { list-style: none; margin: 6px 0 0; padding: 0; display: flex; flex-wrap: wrap; gap: 2px 0; font-size: 0.8rem; font-variant-numeric: tabular-nums; }
.timeline .step { display: inline-flex; gap: 4px; align-items: baseline; color: var(--text); }
.timeline .step + .step::before { content: "→"; color: var(--muted); margin: 0 6px; }
.timeline .step-label { color: var(--muted); }
.timeline .step.muted { color: var(--muted); }
.timeline .step[title] { cursor: help; }

/* --- Wilima ------------------------------------------------------------------ */
:root { --good: #2f6b4f; --bad: #b42318; --absent: #6b5bb5; --imp: #b54708; --imp-soft: #fdf0e3; }
@media (prefers-color-scheme: dark) {
  :root { --good: #5fbf8c; --bad: #ff8a80; --absent: #a99cf0; --imp: #f5a962; --imp-soft: #3a2a1b; }
}
h1 { display: flex; align-items: center; gap: 10px; }
.brand { width: 32px; height: 32px; border-radius: 8px; }
.login .logo { width: 64px; height: 64px; border-radius: 16px; background: none; }

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(6, 1fr);
  background: var(--card); border-top: 1px solid var(--line);
  padding: 4px 4px calc(env(safe-area-inset-bottom) + 4px);
}
.tab {
  background: none; color: var(--muted); border-radius: 10px; padding: 4px 0; min-height: 48px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0;
  font-weight: 500; font-size: 0.7rem; position: relative;
}
.tab-icon { font-size: 1.25rem; line-height: 1.2; }
.tab.active { color: var(--accent); background: var(--accent-soft); }
.tab .badge {
  position: absolute; top: 2px; right: calc(50% - 22px);
  background: var(--imp); color: #fff; border-radius: 99px; font-size: 0.65rem; min-width: 18px; padding: 0 5px; line-height: 18px;
}
main { padding-bottom: calc(env(safe-area-inset-bottom) + 96px); }
@media (min-width: 760px) {
  main { max-width: 860px; padding-bottom: 48px; }
  .tabs { position: static; display: flex; gap: 4px; background: none; border: 0; padding: 0; margin-bottom: 14px; }
  .tab { flex-direction: row; gap: 6px; font-size: 0.95rem; padding: 6px 14px; min-height: 40px; }
  .tab-icon { font-size: 1rem; }
  .tab .badge { position: static; }
}

.group h2 .more { margin-left: auto; text-transform: none; letter-spacing: 0; font-size: 0.85rem; min-height: 24px; }
.note { font-size: 0.9rem; margin: -8px 0 16px; }

.kids { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 10px; margin-bottom: 24px; }
.kid-card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; cursor: pointer; display: flex; flex-direction: column; gap: 4px; }
.kid-card:hover { border-color: var(--accent); }
.kid-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.kid-name { font-weight: 700; font-size: 1.15rem; }
.kid-class { background: var(--accent-soft); color: var(--accent); font-weight: 700; border-radius: 8px; padding: 0 8px; }
.kid-sub { color: var(--muted); font-size: 0.85rem; }
.kid-day { font-size: 0.95rem; margin-top: 4px; }
.kid-day-label { font-weight: 600; }
.kid-subjects { display: flex; flex-wrap: wrap; gap: 4px; }
.kid-subjects span { background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 0 6px; font-size: 0.8rem; }
.kid-exam { color: var(--warn); background: var(--warn-soft); border-radius: 8px; padding: 2px 8px; font-weight: 600; font-size: 0.9rem; margin-top: 4px; }
.kid { color: var(--muted); font-size: 0.85em; font-weight: 500; }

.rows, .msgs { list-style: none; margin: 0; padding: 0; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.row { display: flex; gap: 12px; padding: 10px 12px; align-items: baseline; }
.row + .row, .msg + .msg { border-top: 1px solid var(--line); }
.row.clickable { cursor: pointer; }
.row-date { color: var(--muted); font-size: 0.85rem; min-width: 74px; flex-shrink: 0; }
.row-date.urgent, .due.urgent { color: var(--bad); font-weight: 600; }
.row-main { min-width: 0; overflow-wrap: anywhere; }

.money { display: flex; align-items: baseline; gap: 10px; width: 100%; background: var(--warn-soft); color: var(--text); border-radius: var(--radius); padding: 14px; font-weight: 500; margin-bottom: 28px; }
.money b { color: var(--warn); font-size: 1.3rem; margin-left: auto; }
.money small { color: var(--muted); }

.search { width: 100%; margin-bottom: 16px; }
.msg { padding: 12px; cursor: pointer; border-left: 4px solid transparent; }
.msg:hover { background: var(--bg); }
.msg.imp { border-left-color: var(--imp); }
.msg.info { border-left-color: var(--line); }
.msg.low { opacity: 0.8; }
.msg.unread .msg-subject { font-weight: 700; }
.msg-top { display: flex; gap: 8px; justify-content: space-between; align-items: baseline; }
.msg-subject { font-weight: 500; overflow-wrap: anywhere; }
.msg-date { color: var(--muted); font-size: 0.8rem; white-space: nowrap; }
.msg-meta { color: var(--muted); font-size: 0.85rem; display: flex; flex-wrap: wrap; gap: 4px 6px; align-items: baseline; }
.msg-summary { margin: 6px 0 0; font-size: 0.95rem; }
.cat { background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 0 6px; font-size: 0.75rem; color: var(--muted); }
.actions { margin: 6px 0 0; padding-left: 20px; font-size: 0.9rem; }
.actions li { margin: 2px 0; }
.due { margin-left: 6px; font-size: 0.8rem; color: var(--warn); font-weight: 600; white-space: nowrap; }
.group.imp h2 { color: var(--imp); }
.group.todo .msg { border-left-color: var(--imp); }
button.wide { width: 100%; margin-top: 8px; }
.fold summary { list-style: none; cursor: pointer; }
.fold summary::-webkit-details-marker { display: none; }
.fold summary h2::after { content: '▸'; }
.fold[open] summary h2::after { content: '▾'; }

#msg-dialog { width: min(680px, 100vw - 16px); max-height: calc(100dvh - 16px); }
.msg-title { font-size: 1.2rem; text-transform: none; letter-spacing: 0; color: var(--text); margin: 4px 0; }
.ai-box { background: var(--accent-soft); border-radius: 12px; padding: 10px 12px; margin: 12px 0; }
.ai-box.imp { background: var(--imp-soft); }
.ai-box p { margin: 6px 0 0; }
.ai-head { display: flex; gap: 6px; align-items: center; }
.pill { font-weight: 700; font-size: 0.8rem; }
.ai-box.imp .pill { color: var(--imp); }
.ai-by { margin-left: auto; color: var(--muted); font-size: 0.75rem; }
.msg-html { overflow-wrap: anywhere; line-height: 1.5; }
.msg-html p { margin: 0 0 0.7em; }
.msg-html table { border-collapse: collapse; max-width: 100%; display: block; overflow-x: auto; }
.msg-html td, .msg-html th { border: 1px solid var(--line); padding: 4px 6px; }
.msg-html a { color: var(--accent); }
.reply { border-top: 1px solid var(--line); margin-top: 12px; padding-top: 10px; }

.day-nav { display: flex; gap: 6px; align-items: center; margin-bottom: 16px; }
.days { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; flex: 1; }
.day { background: var(--card); color: var(--text); border: 1px solid var(--line); display: flex; flex-direction: column; align-items: center; padding: 4px 0; min-height: 48px; font-weight: 600; }
.day small { color: var(--muted); font-weight: 400; font-size: 0.75rem; }
.day.today { border-color: var(--accent); }
.day.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.day.active small { color: inherit; }
.sched h2 { text-transform: none; letter-spacing: 0; font-size: 1.05rem; color: var(--text); }
.sched-span { margin-left: auto; color: var(--muted); font-size: 0.85rem; font-weight: 500; }
.lessons { list-style: none; margin: 0; padding: 0; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.lesson { display: grid; grid-template-columns: 56px 1fr; gap: 12px; padding: 10px 12px; }
.lesson + .lesson { border-top: 1px solid var(--line); }
.lesson.past { opacity: 0.55; }
.lesson.live { background: var(--accent-soft); box-shadow: inset 4px 0 0 var(--accent); }
.lesson-time { font-weight: 600; font-variant-numeric: tabular-nums; display: flex; flex-direction: column; }
.lesson-time small { color: var(--muted); font-weight: 400; }
.lesson-subject { font-weight: 600; }
.lesson-subject small { color: var(--muted); font-weight: 400; margin-left: 6px; }
.lesson-meta { color: var(--muted); font-size: 0.85rem; }

.hw { flex-direction: column; gap: 2px; }
.hw-head { display: flex; gap: 6px; align-items: baseline; width: 100%; }
.hw-date { margin-left: auto; color: var(--muted); font-size: 0.8rem; white-space: nowrap; }
.hw-text { white-space: pre-line; overflow-wrap: anywhere; }
.lesson-note .hw-text { color: var(--muted); }
.lesson-note b::after { content: ' · tunnilla'; font-weight: 400; color: var(--muted); }
.toggle { margin-bottom: 16px; }

.mark { align-items: flex-start; }
.mark-dot { width: 10px; height: 10px; border-radius: 50%; margin-top: 6px; flex-shrink: 0; background: var(--line); }
.mark.good .mark-dot { background: var(--good); }
.mark.bad .mark-dot { background: var(--bad); }
.mark.absent .mark-dot { background: var(--absent); }
.mark-note { font-size: 0.9rem; }
.mark-meta { color: var(--muted); font-size: 0.8rem; }
.mark-sum .total-name small { color: var(--muted); font-weight: 400; }
.mark-counts { display: flex; flex-direction: column; font-size: 0.9rem; }
.mark-counts b { font-variant-numeric: tabular-nums; }
.mark-counts .good b { color: var(--good); }
.mark-counts .bad b { color: var(--bad); }
.mark-counts .absent b { color: var(--absent); }

/* --- accounts ------------------------------------------------------------------ */
.login .center { text-align: center; margin: 0 0 8px; }
.suffix-input { display: flex; align-items: center; gap: 4px; }
.suffix-input input { flex: 1; min-width: 0; }
.suffix-input span { color: var(--muted); }
.block { display: block; }
.login .check { align-items: flex-start; font-size: 0.95rem; margin: 6px 0; }
.login .check input { margin-top: 2px; flex-shrink: 0; }
.privacy { font-size: 0.85rem; color: var(--muted); margin-top: 12px; }
.privacy summary { cursor: pointer; color: var(--accent); }
.privacy p { margin: 6px 0; }
#account-dialog { width: min(560px, 100vw - 16px); max-height: calc(100dvh - 16px); }
.acc-section { border-top: 1px solid var(--line); padding: 12px 0; display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.acc-section:first-child { border-top: 0; padding-top: 0; }
.acc-section h3 { margin: 4px 0 0; font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.acc-section p { margin: 0; }
.acc-me { font-size: 1.05rem; }
.acc-me .pill { color: var(--accent); }
.plain { list-style: none; margin: 0; padding: 0; width: 100%; display: flex; flex-direction: column; gap: 6px; }
.invite { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
.invite code { font-size: 1rem; background: var(--bg); padding: 0 6px; border-radius: 6px; }
.row-actions { display: flex; gap: 4px; }
.acc-section .row { display: flex; gap: 6px; width: 100%; }
.acc-section .row input { flex: 1; min-width: 0; }
.acc-more summary { cursor: pointer; color: var(--accent); }
.acc-more p { margin: 6px 0; }
.small-text { font-size: 0.8rem; }
.acc-actions { flex-direction: row; flex-wrap: wrap; }
#pw-dialog { width: min(420px, 100vw - 16px); }
.pw-form { display: flex; flex-direction: column; gap: 10px; }
.pw-form h2 { text-transform: none; letter-spacing: 0; font-size: 1.1rem; color: var(--text); margin: 0; }
