:root {
  color-scheme: light;
  --bg: #f4f4f1;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e1dc;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7c7b76;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --danger: #c93a39;
  --ok: #0e7a3a;
  --warn: #a86b00;
  --grid: #e8e7e2;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948;
  --seq-lo: #cde2fb; --seq-hi: #104281;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 2px 8px rgba(0,0,0,.04);
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --border: #33332f;
    --text: #ffffff; --text-2: #c3c2b7; --muted: #8f8e86; --accent: #3987e5;
    --danger: #e66767; --ok: #3fbf74; --warn: #e0a43a; --grid: #2a2a27;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --seq-lo: #1c2a3d; --seq-hi: #86b6ef;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --border: #33332f;
  --text: #ffffff; --text-2: #c3c2b7; --muted: #8f8e86; --accent: #3987e5;
  --danger: #e66767; --ok: #3fbf74; --warn: #e0a43a; --grid: #2a2a27;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --seq-lo: #1c2a3d; --seq-hi: #86b6ef;
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
}
a { color: var(--accent); }
h1 { font-size: 22px; margin: 0; font-weight: 650; }
h2 { font-size: 16px; margin: 0 0 12px; font-weight: 600; }
h3 { font-size: 15px; margin: 0; font-weight: 600; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; }
.num { font-variant-numeric: tabular-nums; text-align: right; }

/* ---------- кнопки та поля ---------- */
.btn {
  font: inherit; font-weight: 500; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); border-radius: 8px; padding: 7px 14px; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.small { padding: 4px 9px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 7px 10px; width: 100%;
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--text-2); }
label.inline { flex-direction: row; align-items: center; gap: 8px; }
label.inline input { width: auto; }

/* ---------- вхід ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 28px; display: flex; flex-direction: column; gap: 14px; box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; }
.brand.big { font-size: 20px; }
.logo { color: var(--accent); }

/* ---------- каркас ---------- */
.layout { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--surface); border-right: 1px solid var(--border); padding: 18px 12px;
  display: flex; flex-direction: column; gap: 18px; position: sticky; top: 0; height: 100vh;
}
#nav { display: flex; flex-direction: column; gap: 2px; }
#nav a {
  text-decoration: none; color: var(--text-2); padding: 8px 10px; border-radius: 8px; display: flex; gap: 10px;
}
#nav a:hover { background: var(--surface-2); color: var(--text); }
#nav a.active { background: var(--surface-2); color: var(--text); font-weight: 600; }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 6px; align-items: stretch; }
.user-chip { text-decoration: none; color: var(--text); padding: 8px 10px; border-radius: 8px; background: var(--surface-2); font-size: 13px; }
.user-chip small { display: block; color: var(--muted); }
.main { padding: 22px 26px 40px; min-width: 0; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- фільтри ---------- */
.filters {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; margin-bottom: 16px;
}
.filters input[type=date] { width: 150px; }
.filters input[type=search] { width: 220px; }
.filters select { width: auto; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { font: inherit; font-size: 13px; border: 0; background: var(--surface); color: var(--text-2); padding: 6px 10px; cursor: pointer; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.on { background: var(--accent); color: var(--accent-ink); }
.ms { position: relative; }
.ms > .btn { display: flex; gap: 6px; align-items: center; }
.ms .badge { background: var(--accent); color: var(--accent-ink); border-radius: 10px; padding: 0 7px; font-size: 12px; }
.ms-pop {
  position: absolute; z-index: 30; top: calc(100% + 4px); left: 0; min-width: 240px; max-width: 340px; max-height: 340px;
  overflow: auto; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.15); padding: 8px;
}
.ms-pop input[type=search] { margin-bottom: 6px; }
.ms-pop label { flex-direction: row; align-items: center; gap: 8px; padding: 4px 6px; border-radius: 6px; color: var(--text); cursor: pointer; }
.ms-pop label:hover { background: var(--surface-2); }
.ms-pop label input { width: auto; }
.ms-pop .ms-actions { display: flex; justify-content: space-between; border-top: 1px solid var(--border); margin-top: 6px; padding-top: 6px; }

/* ---------- картки й дашборд ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); min-width: 0; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin-bottom: 10px; }
.card-head h2 { margin: 0; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi .label { color: var(--text-2); font-size: 13px; }
.kpi .value { font-size: 28px; font-weight: 650; font-variant-numeric: tabular-nums; margin-top: 4px; }
.kpi .sub { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.delta.up { color: var(--danger); }
.delta.down { color: var(--ok); }
.grid { display: grid; gap: 16px; grid-template-columns: repeat(12, 1fr); }
.span-12 { grid-column: span 12; } .span-8 { grid-column: span 8; } .span-7 { grid-column: span 7; }
.span-6 { grid-column: span 6; } .span-5 { grid-column: span 5; } .span-4 { grid-column: span 4; }
.chart-box { position: relative; height: 280px; }
.chart-box.tall { height: 380px; }
.legend { display: flex; flex-wrap: wrap; gap: 12px; font-size: 12.5px; color: var(--text-2); margin-bottom: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
.chart-tip {
  position: fixed; z-index: 100; pointer-events: none; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: 12.5px; box-shadow: 0 6px 18px rgba(0,0,0,.18); max-width: 320px;
}
.view-toggle { font-size: 12px; }

/* ---------- таблиці ---------- */
.table-wrap { overflow: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
th { background: var(--surface-2); color: var(--text-2); font-weight: 600; position: sticky; top: 0; z-index: 1; white-space: nowrap; }
th.sortable { cursor: pointer; }
th.sortable:hover { color: var(--text); }
tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: var(--surface-2); }
tr.clickable { cursor: pointer; }
td.summary { min-width: 280px; max-width: 520px; }
td.nowrap { white-space: nowrap; }
.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; border: 1px solid var(--border); white-space: nowrap; }
.pill .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; }
.state-new { color: var(--accent); } .state-modified { color: var(--warn); } .state-conflict { color: var(--danger); font-weight: 600; }
.pager { display: flex; align-items: center; gap: 8px; justify-content: flex-end; margin-top: 10px; flex-wrap: wrap; }
.heat td.cell { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- форми ---------- */
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.form-grid .full { grid-column: 1 / -1; }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; flex-wrap: wrap; }
.form-actions .left { margin-right: auto; }

/* ---------- модальне вікно / тост ---------- */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal-card { background: var(--surface); border-radius: 14px; width: 100%; max-width: 860px; max-height: calc(100vh - 32px); display: flex; flex-direction: column; border: 1px solid var(--border); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.modal-body { padding: 18px; overflow: auto; }
.toast { position: fixed; bottom: 18px; right: 18px; background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 200; max-width: 420px; }
.toast.err { background: var(--danger); color: #fff; }

/* ---------- звіти ---------- */
.report-layout { display: grid; grid-template-columns: 320px 1fr; gap: 16px; align-items: start; }
.report-cfg { display: flex; flex-direction: column; gap: 12px; position: sticky; top: 12px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--border); border-radius: 999px; padding: 3px 10px; font-size: 12.5px; background: var(--surface); cursor: pointer; color: var(--text-2); }
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.summary-text p { margin: 0 0 8px; }
.saved-list { display: flex; flex-direction: column; gap: 6px; }
.saved-item { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; }
.saved-item .name { font-weight: 500; cursor: pointer; }
.kv { display: grid; grid-template-columns: 220px 1fr; gap: 6px 14px; }
.kv dt { color: var(--text-2); }
.kv dd { margin: 0; word-break: break-word; }
.notice { padding: 10px 12px; border-radius: 8px; background: var(--surface-2); border: 1px solid var(--border); }
.notice.warn { border-color: var(--warn); }
.notice.err { border-color: var(--danger); color: var(--danger); }
code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: 12.5px; }
.print-only { display: none; }


/* ---------- дашборд з віджетами ---------- */
.edit-bar { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 10px 12px; margin-bottom: 16px; border: 1px dashed var(--accent); border-radius: var(--radius); background: var(--surface); }
.edit-bar .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.dash-grid { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); grid-auto-rows: 24px; gap: 16px; }
.dash-grid > .muted, .empty-dash { grid-column: 1 / -1; }
.widget { position: relative; display: flex; flex-direction: column; min-width: 0; min-height: 0; overflow: hidden;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px 14px; }
.widget-head { display: flex; align-items: center; gap: 6px; margin-bottom: 8px; min-height: 26px; }
.widget-head h2 { margin: 0; font-size: 14px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.widget-actions { display: flex; gap: 2px; opacity: .55; transition: opacity .15s; }
.widget:hover .widget-actions, .editing .widget-actions { opacity: 1; }
.widget-actions .btn { padding: 2px 6px; }
.widget-body { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.chart-fill { position: relative; flex: 1; min-height: 60px; }
.table-wrap.fill { flex: 1; min-height: 0; }
.pad { padding: 12px; }
.kpi-value { font-size: 30px; font-weight: 650; font-variant-numeric: tabular-nums; line-height: 1.1; }
.kpi-sub { color: var(--muted); font-size: 12.5px; margin-top: 4px; }
.w-kpi .widget-head h2 { font-weight: 500; color: var(--text-2); }
.text-widget { overflow: auto; white-space: normal; }
.editing .widget { outline: 1px dashed var(--border); }
.editing .widget-head { flex-wrap: wrap; }
.editing .widget-head h2 { order: 3; flex-basis: 100%; }
.editing .widget-actions { margin-left: auto; }
.widget.narrow [data-act=left], .widget.narrow [data-act=right] { display: none; }
.drag { cursor: grab; color: var(--muted); padding: 0 2px; user-select: none; font-size: 16px; }
.widget.dragging { opacity: .4; }
.widget.drop-before { box-shadow: -4px 0 0 0 var(--accent); }
.widget.drop-after { box-shadow: 4px 0 0 0 var(--accent); }
.resize { position: absolute; right: 2px; bottom: 0; width: 18px; height: 18px; cursor: nwse-resize; color: var(--muted);
  font-size: 13px; line-height: 18px; text-align: center; user-select: none; touch-action: none; }
.widget.resizing { outline: 2px solid var(--accent); }
.modal-card.wide { max-width: 980px; }
.small-label { font-size: 12.5px; margin-bottom: 6px; }
.cond { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; }
.cond select { width: auto; }
.chip input { display: none; }
.ms-pop .dot, .pill .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; flex: none; }
.filters.compact { margin: 0; padding: 8px; }
tr.total td { background: var(--surface-2); }

/* ---------- структура бази ---------- */
.tabs { display: flex; gap: 4px; margin-bottom: 16px; border-bottom: 1px solid var(--border); }
.tab { font: inherit; background: none; border: 0; border-bottom: 2px solid transparent; padding: 8px 14px; cursor: pointer; color: var(--text-2); }
.tab.on { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.dict-layout { display: grid; grid-template-columns: 260px 1fr; gap: 16px; align-items: start; }
.dict-fields { display: flex; flex-direction: column; gap: 2px; }
.dict-field { font: inherit; text-align: left; background: none; border: 0; border-radius: 8px; padding: 8px 10px; cursor: pointer; color: var(--text-2);
  display: flex; justify-content: space-between; }
.dict-field:hover { background: var(--surface-2); }
.dict-field.on { background: var(--surface-2); color: var(--text); font-weight: 600; }
.inline-form { display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap; }
.inline-form h2 { width: 100%; }
.inline-form label { flex: 1; min-width: 180px; }
.inline-form input[name=value] { flex: 1; min-width: 200px; width: auto; }
input[type=color] { width: 40px; height: 34px; padding: 2px; cursor: pointer; }
.schema-table td { vertical-align: middle; }
.schema-table input[type=checkbox] { width: auto; }
.muted-row td { color: var(--muted); }
.group-row td { background: var(--surface-2); font-weight: 600; }

/* ---------- попередній перегляд посилань ---------- */
.link-pop { position: fixed; z-index: 120; width: 360px; max-width: calc(100vw - 16px); background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,.22); padding: 12px; font-size: 13px; }
.lp-host { color: var(--muted); font-size: 12px; margin-bottom: 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lp-img { display: block; width: 100%; max-height: 180px; object-fit: cover; border-radius: 8px; margin-bottom: 8px; background: var(--surface-2); }
.lp-title { font-weight: 600; margin-bottom: 4px; }
.lp-desc { color: var(--text-2); display: -webkit-box; -webkit-line-clamp: 7; -webkit-box-orient: vertical; overflow: hidden; white-space: pre-line; }
.lp-actions { display: flex; gap: 6px; justify-content: flex-end; margin-top: 10px; }
.lp-actions a.btn { text-decoration: none; }
.lp-embed { width: 100%; height: 420px; border: 0; border-radius: 8px; background: #fff; }
a.ext { text-decoration-style: dotted; }

@media (max-width: 1100px) {
  .span-8, .span-7, .span-6, .span-5, .span-4 { grid-column: span 12; }
  .report-layout { grid-template-columns: 1fr; }
  .report-cfg { position: static; }
  .dict-layout { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; padding: 10px 16px; gap: 8px; }
  #nav { flex-direction: row; flex-wrap: wrap; }
  #nav a span { display: none; }
  .sidebar-foot { flex-direction: row; margin-top: 0; margin-left: auto; }
  .main { padding: 16px; }
  .form-grid { grid-template-columns: 1fr; }
  .filters input[type=search], .filters input[type=date] { width: 100%; }
  .kv { grid-template-columns: 1fr; }
  .dash-grid { grid-template-columns: 1fr; }
  .widget { grid-column: 1 / -1 !important; }
  .resize { display: none; }
}
@media print {
  .sidebar, .filters, .report-cfg, .page-head .actions, .no-print, .pager, .edit-bar, .resize, .link-pop { display: none !important; }
  .widget { break-inside: avoid; box-shadow: none; }
  .layout, .report-layout { display: block; }
  .main { padding: 0; }
  .card { box-shadow: none; break-inside: avoid; border-color: #ccc; }
  .print-only { display: block; }
  body { background: #fff; }
}
