:root {
  color-scheme: light dark;
  --bg: light-dark(#f3f4f7, #0e1116);
  --surface: light-dark(#ffffff, #161a21);
  --surface-2: light-dark(#f6f7f9, #1c212a);
  --border: light-dark(#e2e5eb, #272d38);
  --border-strong: light-dark(#cfd4dd, #353c49);
  --text: light-dark(#161b26, #e7e9ee);
  --muted: light-dark(#5b6475, #9aa3b2);
  --faint: light-dark(#98a0ad, #5d6676);
  --accent: light-dark(#2563eb, #6d9eff);
  --accent-strong: light-dark(#1d4ed8, #8fb4ff);
  --accent-soft: light-dark(#e9f0ff, #1a2640);
  --ok: light-dark(#16a34a, #4ade80);
  --warn: light-dark(#d97706, #fbbf24);
  --error: light-dark(#dc2626, #f87171);
  --shadow: light-dark(0 1px 2px rgb(16 24 40 / .05), 0 1px 2px rgb(0 0 0 / .4));

  --lk-bg: light-dark(#e7efff, #1a2949);   --lk-fg: light-dark(#1d4ed8, #93b6ff);
  --pz-bg: light-dark(#e2f6e9, #12301d);   --pz-fg: light-dark(#15803d, #7ad797);
  --lb-bg: light-dark(#f2e9ff, #2a1f44);   --lb-fg: light-dark(#7e22ce, #c6a4ff);
  --kons-bg: light-dark(#fff2d9, #382910); --kons-fg: light-dark(#b45309, #f5c26b);
  --zal-bg: light-dark(#ddf4f4, #0f3034);  --zal-fg: light-dark(#0f766e, #6dd5cd);
  --exam-bg: light-dark(#fde6e6, #3b1719); --exam-fg: light-dark(#c62828, #ff9d9d);
  --other-bg: light-dark(#eef0f3, #252a33); --other-fg: light-dark(#4b5563, #b4bcc8);

  --radius: 14px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] { color-scheme: dark; }

*, *::before, *::after { box-sizing: border-box; }
html { background: var(--bg); }
body { margin: 0; color: var(--text); background: var(--bg); min-height: 100dvh; }
h1, h2, h3, p { margin: 0; }
button { font: inherit; color: inherit; }
a { color: var(--accent); }

.icon { width: 18px; height: 18px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- top bar ---------- */
.topbar { border-bottom: 1px solid var(--border); background: var(--surface); }
.topbar__inner {
  max-width: 1240px; margin: 0 auto; padding: 12px max(16px, env(safe-area-inset-right)) 12px max(16px, env(safe-area-inset-left));
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.title { display: flex; align-items: center; gap: 10px; font-size: 18px; font-weight: 650; letter-spacing: -.01em; }
.title__logo { width: 28px; height: 28px; }
.title__group { color: var(--accent); }

.icon-btn {
  display: inline-grid; place-items: center; width: 36px; height: 36px; padding: 0;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface); color: var(--muted); cursor: pointer;
}
.icon-btn:hover { color: var(--text); background: var(--surface-2); }
.icon--moon { display: none; }
:root[data-theme="dark"] .icon--sun { display: none; }
:root[data-theme="dark"] .icon--moon { display: block; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .icon--sun { display: none; }
  :root:not([data-theme="light"]) .icon--moon { display: block; }
}

/* ---------- layout ---------- */
.layout {
  max-width: 1240px; margin: 0 auto; padding: 20px max(16px, env(safe-area-inset-right)) 40px max(16px, env(safe-area-inset-left));
  display: grid; gap: 20px; align-items: start;
}
@media (min-width: 980px) {
  .layout { grid-template-columns: minmax(0, 1fr) 300px; }
  .side { position: sticky; top: 20px; }
}

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 38px; padding: 0 14px; border: 1px solid var(--border-strong); border-radius: 10px;
  background: var(--surface); font-size: 14px; font-weight: 550; cursor: pointer; white-space: nowrap;
  box-shadow: var(--shadow); transition: background-color .15s, border-color .15s;
}
.btn:hover:not(:disabled) { background: var(--surface-2); border-color: var(--faint); }
.btn:disabled { opacity: .45; cursor: default; box-shadow: none; }
.btn:focus-visible, .icon-btn:focus-visible, .cal__day:focus-visible, .cal-panel__toggle:focus-visible, .cal__today-link:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.btn--ghost { box-shadow: none; background: transparent; border-color: var(--border); color: var(--muted); }
.btn--ghost:hover:not(:disabled) { color: var(--text); }
.btn--nav { font-size: 14.5px; }
.btn--nav [aria-hidden] { font-size: 20px; line-height: 1; margin-top: -2px; }
.is-busy .btn--refresh .icon { animation: spin .8s linear infinite; }

/* ---------- toolbar ---------- */
.toolbar {
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 12px;
  padding: 14px 16px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.toolbar__range { text-align: center; min-width: 0; }
.toolbar__range h2 { font-size: 20px; font-weight: 700; letter-spacing: -.01em; font-variant-numeric: tabular-nums; }
.toolbar__sem { color: var(--muted); font-size: 13.5px; margin-top: 1px; }

.statusbar { display: flex; align-items: center; justify-content: space-between; gap: 10px 16px; flex-wrap: wrap; margin: 12px 2px 16px; }
.statusbar__actions { display: flex; gap: 8px; margin-left: auto; }
.status { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13.5px; min-width: 0; }
.status__dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); flex: none; }
.status[data-kind="loading"] .status__dot { background: var(--accent); animation: pulse 1s ease-in-out infinite; }
.status[data-kind="warn"] { color: light-dark(#92400e, #fcd34d); }
.status[data-kind="warn"] .status__dot { background: var(--warn); }
.status[data-kind="error"] { color: var(--error); }
.status[data-kind="error"] .status__dot { background: var(--error); }

/* thin progress bar while a request is in flight */
.week-host { position: relative; min-width: 0; }
.week-host.is-busy::before {
  content: ""; position: absolute; z-index: 2; left: 0; right: 0; top: -10px; height: 3px; border-radius: 3px;
  background: linear-gradient(90deg, transparent, var(--accent), transparent); background-size: 40% 100%; background-repeat: no-repeat;
  animation: progress 1.1s ease-in-out infinite;
}
.week-host.is-switching .days, .week-host.is-switching .error-panel { opacity: .45; transition: opacity .15s; }

/* ---------- days ---------- */
.days {
  list-style: none; margin: 0; padding: 0;
  display: grid; gap: 14px; align-items: start; grid-template-columns: repeat(auto-fill, minmax(min(100%, 330px), 1fr));
}
.day {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 14px 12px; box-shadow: var(--shadow); min-width: 0;
}
.day.is-today { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent), var(--shadow); }
.day__head { display: flex; align-items: baseline; gap: 8px; margin: 0 2px 10px; }
.day__name { font-size: 16px; font-weight: 650; }
.day__date { color: var(--muted); font-size: 13.5px; }
.day__today {
  display: none; margin-left: auto; align-self: center; padding: 2px 9px; border-radius: 999px;
  background: var(--accent); color: light-dark(#fff, #0e1116); font-size: 12px; font-weight: 650;
}
.day.is-today .day__today { display: inline-block; }
.day.is-today .day__name { color: var(--accent-strong); }
.day.is-past .day__name, .day.is-past .day__date { color: var(--faint); }
.day.is-past .lessons { opacity: .62; }
.day__empty { color: var(--faint); font-size: 14px; padding: 14px 2px 8px; text-align: center; border-top: 1px dashed var(--border); }

.lessons { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.lesson {
  --c-bg: var(--other-bg); --c-fg: var(--other-fg);
  display: grid; grid-template-columns: 52px minmax(0, 1fr); gap: 12px;
  padding: 10px 12px 10px 10px; border-radius: 10px; background: var(--surface-2);
  border-left: 3px solid var(--c-fg);
}
.lesson--lk { --c-bg: var(--lk-bg); --c-fg: var(--lk-fg); }
.lesson--pz { --c-bg: var(--pz-bg); --c-fg: var(--pz-fg); }
.lesson--lb { --c-bg: var(--lb-bg); --c-fg: var(--lb-fg); }
.lesson--kons { --c-bg: var(--kons-bg); --c-fg: var(--kons-fg); }
.lesson--zal { --c-bg: var(--zal-bg); --c-fg: var(--zal-fg); }
.lesson--exam { --c-bg: var(--exam-bg); --c-fg: var(--exam-fg); }

.lesson__time { display: flex; flex-direction: column; font-variant-numeric: tabular-nums; line-height: 1.25; }
.lesson__start { font-weight: 700; font-size: 15px; }
.lesson__end { color: var(--muted); font-size: 13px; }
.lesson__pair { color: var(--faint); font-size: 11.5px; margin-top: 4px; white-space: nowrap; }

.lesson__head { display: flex; align-items: center; flex-wrap: wrap; gap: 6px; }
.lesson__short { font-weight: 700; font-size: 15px; }
.badge {
  display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 650; line-height: 18px;
  background: var(--c-bg); color: var(--c-fg);
}
.chip {
  display: inline-block; padding: 0 7px; border: 1px solid var(--border-strong); border-radius: 999px;
  color: var(--muted); font-size: 11.5px; line-height: 18px;
}
.lesson__subject { margin-top: 4px; font-size: 13.5px; color: var(--text); overflow-wrap: anywhere; }
.lesson__meta { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-top: 6px; color: var(--muted); font-size: 13px; }
.lesson__meta:empty { display: none; }
.meta { display: inline-flex; align-items: center; gap: 5px; }
.meta .icon { width: 14px; height: 14px; }

/* ---------- error / skeleton ---------- */
.error-panel {
  padding: 20px; border-radius: var(--radius); border: 1px solid color-mix(in srgb, var(--error) 40%, transparent);
  background: color-mix(in srgb, var(--error) 8%, var(--surface)); display: grid; gap: 8px; justify-items: start;
}
.error-panel h3 { font-size: 16px; color: var(--error); }
.error-panel p { color: var(--text); }
.error-panel .btn { margin-top: 6px; }

.days--skeleton .day { display: grid; gap: 10px; }
.sk { display: block; border-radius: 8px; background: linear-gradient(90deg, var(--surface-2) 25%, var(--border) 50%, var(--surface-2) 75%); background-size: 200% 100%; animation: shimmer 1.4s ease-in-out infinite; }
.sk--title { width: 45%; height: 20px; }
.sk--lesson { height: 64px; }

.week__source { margin: 18px 2px 0; font-size: 13px; }
.week__source a { color: var(--muted); text-decoration: none; }
.week__source a:hover { color: var(--accent); text-decoration: underline; }

/* ---------- calendar ---------- */
.cal-panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.cal-panel__toggle { display: none; }
.cal { padding: 14px 12px 12px; }
.cal__head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.cal__title { font-size: 15.5px; font-weight: 650; }
.cal__head .icon-btn { width: 32px; height: 32px; border-color: transparent; }
.cal__grid { width: 100%; border-collapse: separate; border-spacing: 0 2px; table-layout: fixed; }
.cal__grid th { color: var(--faint); font-size: 11.5px; font-weight: 600; padding: 2px 0 6px; text-transform: lowercase; }
.cal__grid td { padding: 0; text-align: center; }
.cal__wk { width: 30px; color: var(--faint); font-size: 11px; font-variant-numeric: tabular-nums; }
.cal__day {
  display: block; width: 100%; height: 34px; padding: 0; border: 0; border-radius: 8px; background: transparent;
  font-size: 13.5px; font-variant-numeric: tabular-nums; cursor: pointer;
}
.cal__day:hover { background: var(--surface-2); }
.cal__day.is-other { color: var(--faint); }
.cal__day.is-today { color: var(--accent-strong); font-weight: 750; box-shadow: inset 0 0 0 1.5px var(--accent); }
tr.is-selected td:not(.cal__wk) { background: var(--accent-soft); }
tr.is-selected td:nth-child(2) { border-radius: 8px 0 0 8px; }
tr.is-selected td:last-child { border-radius: 0 8px 8px 0; }
tr.is-selected .cal__wk { color: var(--accent); font-weight: 700; }
tr.is-selected .cal__day:hover { background: color-mix(in srgb, var(--accent) 14%, transparent); }
.cal__today-link {
  display: block; margin: 8px auto 0; padding: 4px 8px; border: 0; background: none; color: var(--accent); font-size: 13px; cursor: pointer; border-radius: 6px;
}
.cal__today-link:hover { text-decoration: underline; }

/* ---------- calendar sync ---------- */
.topbar__actions { display: flex; align-items: center; gap: 8px; }
.topbar__sync { min-height: 36px; padding: 0 12px; }
.btn--primary { background: var(--accent); border-color: var(--accent); color: light-dark(#fff, #0b0f17); }
.btn--primary:hover:not(:disabled) { background: var(--accent-strong); border-color: var(--accent-strong); }

.dialog {
  width: min(540px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); padding: 0; overflow: auto;
  border: 1px solid var(--border); border-radius: 18px; background: var(--surface); color: var(--text);
  box-shadow: 0 24px 60px rgb(0 0 0 / .28);
}
.dialog::backdrop { background: rgb(8 12 20 / .5); backdrop-filter: blur(2px); }
.dialog[open] { animation: dialog-in .16s ease-out; }
.dialog__inner { display: grid; gap: 16px; padding: 20px 22px 22px; }
.dialog__head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.dialog__head h2 { font-size: 18px; font-weight: 700; letter-spacing: -.01em; }
.dialog__close { border-color: transparent; }
.dialog__lead { color: var(--muted); font-size: 14px; margin-top: -6px; }

.reminders { margin: 0; padding: 10px 14px 12px; border: 1px solid var(--border); border-radius: 12px; display: grid; gap: 10px; }
.reminders legend { padding: 0 6px; margin-left: -6px; color: var(--muted); font-size: 12.5px; font-weight: 650; text-transform: uppercase; letter-spacing: .04em; }
.check { display: flex; align-items: flex-start; gap: 10px; font-size: 14px; cursor: pointer; }
.check input { width: 18px; height: 18px; margin: 1px 0 0; accent-color: var(--accent); flex: none; cursor: pointer; }
.check b { font-weight: 650; }

.sync-actions { display: grid; gap: 8px; }
.sync-actions .btn { width: 100%; min-height: 42px; text-decoration: none; color: inherit; }
.sync-actions .btn--primary { color: light-dark(#fff, #0b0f17); }
.sync-actions .btn--ghost { color: var(--muted); }

.sync-url { display: grid; gap: 6px; }
.sync-url label { color: var(--muted); font-size: 13px; }
.sync-url__row { display: flex; gap: 8px; }
.sync-url input {
  flex: 1; min-width: 0; height: 38px; padding: 0 10px; border: 1px solid var(--border-strong); border-radius: 10px;
  background: var(--surface-2); color: var(--text); font: 12.5px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.sync-url input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.sync-url .btn { min-width: 118px; }

.sync-help { border-top: 1px solid var(--border); padding-top: 12px; }
.sync-help summary { cursor: pointer; color: var(--muted); font-size: 13.5px; font-weight: 550; }
.sync-help summary:hover { color: var(--text); }
.sync-help ul { margin: 10px 0 0; padding-left: 18px; display: grid; gap: 8px; color: var(--muted); font-size: 13.5px; }
.sync-help b { color: var(--text); font-weight: 600; }

@keyframes dialog-in { from { opacity: 0; transform: translateY(8px) scale(.98); } }

.noscript { max-width: 600px; margin: 20px auto; padding: 16px; text-align: center; color: var(--error); }

/* ---------- mobile ---------- */
@media (max-width: 979px) {
  .side { order: -1; }
  .cal-panel__toggle {
    display: flex; align-items: center; gap: 8px; width: 100%; padding: 10px 14px; border: 0; background: none;
    font-size: 14.5px; font-weight: 600; cursor: pointer; border-radius: var(--radius);
  }
  .cal-panel__chevron { margin-left: auto; transition: transform .2s; color: var(--muted); }
  .cal-panel.is-open .cal-panel__chevron { transform: rotate(180deg); }
  .cal-panel:not(.is-open) .cal { display: none; }
  .cal { padding-top: 4px; max-width: 420px; margin: 0 auto; }
  .layout { gap: 14px; padding-top: 14px; }
}
@media (max-width: 560px) {
  :root { font-size: 14.5px; }
  .topbar__inner { padding-top: 10px; padding-bottom: 10px; }
  .title { font-size: 16.5px; }
  .toolbar { padding: 10px; gap: 8px; }
  .toolbar__range h2 { font-size: 17px; }
  .btn--nav { width: 42px; padding: 0; }
  .btn--nav .btn__text, .topbar__sync .btn__text { display: none; }
  .topbar__sync { width: 36px; padding: 0; color: var(--muted); }
  .dialog__inner { padding: 16px; gap: 14px; }
  .statusbar { margin: 10px 0 12px; }
  .statusbar__actions { width: 100%; }
  .statusbar__actions .btn { flex: 1; }
  .day { padding: 12px 10px 10px; }
  .lesson { grid-template-columns: 46px minmax(0, 1fr); gap: 10px; }
}

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse { 50% { opacity: .35; } }
@keyframes shimmer { to { background-position: -200% 0; } }
@keyframes progress { from { background-position: -40% 0; } to { background-position: 140% 0; } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition: none !important; }
}
