:root {
  --bg: #f4f1ea;
  --surface: #fffdf8;
  --surface-2: #ebe6db;
  --ink: #1d1c19;
  --ink-2: #5e5a52;
  --line: #ddd6c8;
  --accent: #0d6b5f;
  --accent-ink: #ffffff;
  --accent-soft: #d6ebe6;
  --warn: #b4410c;
  --warn-soft: #fbe3d4;
  --danger: #b42318;
  --danger-soft: #fbe0dd;
  --ok: #1f7a3d;
  --ok-soft: #dcefe1;
  --grey-soft: #e6e2da;
  --shadow: 0 1px 2px rgba(40, 32, 20, .06), 0 4px 14px rgba(40, 32, 20, .05);
  --radius: 12px;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "Roboto Mono", Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141614; --surface: #1c1f1d; --surface-2: #262a27; --ink: #ecebe5; --ink-2: #a3a39b; --line: #2f3430;
    --accent: #5cc4b2; --accent-ink: #0d1f1b; --accent-soft: #1d3833; --warn: #f59a5c; --warn-soft: #3b2518;
    --danger: #f2766b; --danger-soft: #3d1d1a; --ok: #6fcf8d; --ok-soft: #183323; --grey-soft: #2a2d2b; --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body { background: var(--bg); color: var(--ink); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-font-smoothing: antialiased; }
[hidden] { display: none !important; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
input[type=text], input[type=password], input[type=number], input[type=date], input[type=time], select, textarea {
  background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px; color: var(--ink);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
textarea { width: 100%; resize: vertical; }
h1, h2, h3 { letter-spacing: -.01em; }
h3 { font-size: 16px; margin: 0 0 10px; }
.mt { margin-top: 26px; }
.muted { color: var(--ink-2); }
.small { font-size: 13px; }
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.r { text-align: right; }
.nowrap { white-space: nowrap; }
.pre { white-space: pre-wrap; margin: 0; }
.warn { color: var(--warn); font-weight: 700; }
.error { color: var(--danger); font-size: 14px; margin: 6px 0; }
.notice { background: var(--warn-soft); color: var(--warn); padding: 10px 14px; border-radius: 10px; }

/* ---------- кнопки ---------- */
.btn { border: 1px solid var(--line); background: var(--surface); border-radius: 8px; padding: 7px 14px; cursor: pointer; font-weight: 600; }
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn-ghost { border-color: transparent; background: transparent; color: var(--ink-2); }
.btn-small { padding: 4px 10px; font-size: 13px; }
.btn-on { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
.wide { width: 100%; padding: 11px; margin-top: 6px; }
.link-btn { border: 0; background: none; color: var(--ink-2); text-decoration: underline; padding: 4px 0; font-size: 13px; cursor: pointer; }

/* ---------- екрани входу ---------- */
.center-screen { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.spinner { width: 32px; height: 32px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.auth-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 26px 22px; box-shadow: var(--shadow); }
.auth-card img { display: block; margin-bottom: 12px; }
.auth-card h1 { margin: 0; font-size: 24px; }
.auth-card > .muted { margin: 4px 0 20px; }
.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: 13px; font-weight: 600; color: var(--ink-2); margin-bottom: 5px; }
.field input, .field select { width: 100%; padding: 10px 11px; font-size: 16px; }

/* ---------- оболонка ---------- */
.appbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 18px;
  padding: 6px 18px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--ink); font-weight: 700; font-size: 17px; }
.space-switch { display: flex; align-items: center; gap: 8px; margin-left: auto; }
.space-switch label { font-size: 13px; color: var(--ink-2); font-weight: 600; }
.space-switch select { min-width: 200px; font-weight: 600; }
.whoami { display: flex; align-items: center; gap: 8px; margin-left: auto; }
.space-switch:not([hidden]) + .whoami { margin-left: 18px; }
.who { font-weight: 600; }
.role-badge { font-size: 12px; background: var(--accent-soft); color: var(--accent); padding: 2px 8px; border-radius: 999px; font-weight: 700; }
.layout { display: grid; grid-template-columns: 220px 1fr; min-height: calc(100vh - 61px); }
.nav { border-right: 1px solid var(--line); padding: 14px 10px; position: sticky; top: 61px; align-self: start; max-height: calc(100vh - 61px); overflow: auto; }
.nav-group { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-2); margin: 14px 10px 6px; }
.nav-group:first-child { margin-top: 0; }
.nav-item { display: block; padding: 7px 10px; border-radius: 8px; color: var(--ink); text-decoration: none; }
.nav-item:hover { background: var(--surface-2); }
.nav-item.on { background: var(--accent-soft); color: var(--accent); font-weight: 700; }
.page { padding: 18px 26px 60px; min-width: 0; }
.page-title { margin: 0 0 14px; font-size: 24px; }
.empty { padding: 40px; text-align: center; color: var(--ink-2); background: var(--surface); border: 1px dashed var(--line); border-radius: var(--radius); }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 0 0 14px; }
.inl { display: inline-flex; align-items: center; gap: 6px; color: var(--ink-2); font-size: 14px; }
.cb { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.card-lite { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 14px; }
.narrow { max-width: 420px; }
.lbl { display: block; font-size: 12px; font-weight: 700; color: var(--ink-2); text-transform: uppercase; letter-spacing: .05em; margin-bottom: 6px; }
.cols { display: grid; grid-template-columns: minmax(300px, 1fr) minmax(360px, 1.3fr); gap: 22px; align-items: start; }
.col.wide { min-width: 0; }
.scroll-x { overflow-x: auto; }
.tag { font-size: 11px; background: var(--warn-soft); color: var(--warn); padding: 1px 6px; border-radius: 6px; font-weight: 700; }

/* ---------- таблиці ---------- */
.tbl { border-collapse: collapse; width: 100%; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.tbl th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-2); background: var(--surface-2); padding: 8px 10px; }
.tbl td { padding: 7px 10px; border-top: 1px solid var(--line); vertical-align: middle; }
.tbl tr.total td { font-weight: 700; background: var(--surface-2); }
.tbl tr.unmapped td { background: var(--warn-soft); }
.tbl tr.inactive td { color: var(--ink-2); }
.tbl.log td { vertical-align: top; }
.tbl .code { font-family: var(--mono); font-size: 12px; max-width: 280px; overflow-wrap: anywhere; color: var(--ink-2); }
.num-cell { font-weight: 700; color: var(--accent); width: 40px; }
.tpl-grid select { width: 100%; min-width: 110px; }
.plan-edit select { width: 100%; }
.w-num { width: 70px; }
.actions { display: inline-flex; flex-wrap: wrap; gap: 6px; }

/* ---------- календар ---------- */
.week { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 8px; margin-bottom: 18px; }
.wday { display: flex; flex-direction: column; gap: 6px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; color: var(--ink); text-decoration: none; min-height: 150px; }
.wday:hover { border-color: var(--accent); }
.wday.sel { outline: 2px solid var(--accent); }
.wday.today .wday-head b { color: var(--accent); }
.wday-head { display: flex; justify-content: space-between; }
.wday-list { margin: 0; padding: 0; list-style: none; font-size: 12.5px; }
.wday-list li { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.wday-fact { color: var(--ok); }
.day-panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; }
.panel-title { margin: 0 0 16px; font-size: 19px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.mw { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; margin-top: 4px; }
.fact-head { margin-bottom: 12px; }
.totals { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tot { display: flex; flex-direction: column; gap: 2px; background: var(--bg); border-radius: 10px; padding: 8px 10px; }
.tot-l { font-size: 11px; font-weight: 700; text-transform: uppercase; color: var(--ink-2); letter-spacing: .05em; }
.tot-v { font-size: 17px; font-weight: 600; }

/* стани дня */
.state { display: inline-block; font-size: 12px; font-weight: 700; padding: 2px 9px; border-radius: 999px; }
.st-grey { background: var(--grey-soft); color: var(--ink-2); }
.st-orange { background: var(--warn-soft); color: var(--warn); }
.st-green { background: var(--ok-soft); color: var(--ok); }
.st-red { background: var(--danger-soft); color: var(--danger); }
.st-off, .st-future { background: var(--surface-2); color: var(--ink-2); }

/* ---------- картка уроку (спільна з PWA) ---------- */
.lesson { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; margin-bottom: 8px; }
.lesson.incomplete { border-color: var(--danger); }
.lesson.complete { border-left: 4px solid var(--ok); }
.lesson-head { display: grid; grid-template-columns: 28px 1fr auto; gap: 10px; align-items: center; }
.slot { width: 28px; height: 28px; border-radius: 8px; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent); font-weight: 700; }
.lesson-title { display: flex; gap: 10px; align-items: baseline; flex-wrap: wrap; }
.lesson-status { min-width: 20px; text-align: center; font-weight: 700; }
.lesson-status.saved { color: var(--ok); }
.lesson-status.pending, .lesson-status.saving { color: var(--warn); }
.lesson-status.error { color: var(--danger); }
.lesson-fact { margin: 6px 0 0 38px; font-size: 14px; }
.lesson-body { margin: 8px 0 0 38px; display: grid; grid-template-columns: auto auto 1fr; gap: 8px 14px; align-items: center; }
.seg { display: inline-flex; background: var(--surface-2); border-radius: 8px; padding: 2px; gap: 2px; }
.seg-btn { border: 0; background: transparent; border-radius: 6px; padding: 4px 11px; font-weight: 600; font-size: 13px; color: var(--ink-2); cursor: pointer; }
.seg-btn.on { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.dur-inputs { display: flex; gap: 8px; border-radius: 8px; }
.inline-field { display: inline-flex; align-items: center; gap: 5px; }
.inline-field input.num { width: 80px; font-family: var(--mono); }
.suffix, .prefix { color: var(--ink-2); font-weight: 600; font-size: 13px; }
.dur-preview { font-family: var(--mono); }
.field-err { grid-column: 1 / -1; color: var(--danger); font-size: 13px; }
.field-err:empty { display: none; }
.chips-group { grid-column: 1 / -1; display: flex; align-items: center; gap: 10px; border-radius: 8px; }
.chips-group .lbl { margin: 0; min-width: 70px; }
.chips { display: flex; flex-wrap: wrap; gap: 5px; }
.chip-opt { border: 1px solid var(--line); background: var(--bg); border-radius: 999px; padding: 3px 11px; font-size: 13px; cursor: pointer; }
.chip-opt.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.missing { outline: 2px solid var(--danger); outline-offset: 3px; }
.lesson-actions { grid-column: 1 / -1; display: flex; justify-content: flex-end; margin-top: -6px; }

/* ---------- звіти ---------- */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-bottom: 18px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; display: flex; flex-direction: column; }
.kpi b { font-size: 24px; }
.rep { margin-bottom: 24px; }
.rep-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.rep-head h3 { margin: 0; }
.rep-head span { display: inline-flex; gap: 6px; }
.versions details { margin-bottom: 8px; }
.versions summary { cursor: pointer; font-weight: 600; margin-bottom: 6px; }

/* ---------- модальне вікно, toast ---------- */
.modal-back { position: fixed; inset: 0; background: rgba(0, 0, 0, .4); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { background: var(--surface); border-radius: 14px; padding: 20px 22px; width: 100%; max-width: 460px; box-shadow: 0 10px 40px rgba(0,0,0,.25); }
.modal h3 { font-size: 18px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.toast { position: fixed; right: 20px; bottom: 20px; background: var(--ink); color: var(--bg); padding: 11px 16px; border-radius: 10px; z-index: 60; max-width: 460px; box-shadow: var(--shadow); }
.toast.bad { background: var(--danger); color: #fff; }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .nav { position: static; max-height: none; border-right: 0; border-bottom: 1px solid var(--line); display: flex; flex-wrap: wrap; gap: 4px; }
  .nav-group { display: none; }
  .cols, .kpis { grid-template-columns: 1fr; }
  .week { grid-template-columns: repeat(2, 1fr); }
  .appbar { flex-wrap: wrap; }
  .page { padding: 14px; }
  .lesson-body { margin-left: 0; grid-template-columns: 1fr; }
}
