:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #f0efeb;
  --line: #e2e0da;
  --line-strong: #cfccc4;
  --text: #1d1c1a;
  --text-2: #5f5c55;
  --text-3: #8d8a82;
  --accent: #1f4e79;
  --accent-soft: #e6eef6;
  --ok: #1d6b3f;
  --ok-bg: #e3f1e8;
  --warn: #8a5a00;
  --warn-bg: #fbefd6;
  --bad: #a3272b;
  --bad-bg: #f8e2e1;
  --muted: #6b6860;
  --muted-bg: #ecebe7;
  --shadow: 0 10px 30px rgba(20, 18, 14, 0.12);
  --radius: 8px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151514;
    --surface: #1d1d1b;
    --surface-2: #252523;
    --line: #333330;
    --line-strong: #45443f;
    --text: #ecebe6;
    --text-2: #b3b0a8;
    --text-3: #85827a;
    --accent: #8db8e3;
    --accent-soft: #22303d;
    --ok: #7fcf9d;
    --ok-bg: #1f3327;
    --warn: #e8bf6a;
    --warn-bg: #3a2f17;
    --bad: #f09a96;
    --bad-bg: #3d2020;
    --muted: #a19e96;
    --muted-bg: #2b2b28;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
.hidden { display: none !important; }

/* ---- шапка ---- */
.top {
  position: sticky; top: 0; z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.top-row {
  display: flex; align-items: center; gap: 16px;
  padding: 10px 20px;
  max-width: 1500px; margin: 0 auto;
}
.brand { font-weight: 650; letter-spacing: 0.02em; white-space: nowrap; }
.brand span { color: var(--text-3); font-weight: 400; margin-left: 6px; }
.tabs {
  display: flex; gap: 2px; overflow-x: auto; flex: 1;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  border: 0; background: none; cursor: pointer;
  padding: 7px 12px; border-radius: 6px;
  color: var(--text-2); white-space: nowrap;
}
.tab:hover { background: var(--surface-2); color: var(--text); }
.tab[aria-selected="true"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.tab .n { color: var(--text-3); font-weight: 400; margin-left: 4px; font-variant-numeric: tabular-nums; }
.user { position: relative; }
.user-btn {
  border: 1px solid var(--line); background: var(--surface); border-radius: 6px;
  padding: 6px 10px; cursor: pointer; color: var(--text-2); max-width: 220px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.menu {
  position: absolute; right: 0; top: calc(100% + 6px);
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); min-width: 190px; padding: 4px; z-index: 30;
}
.menu button {
  display: block; width: 100%; text-align: left; border: 0; background: none;
  padding: 8px 10px; border-radius: 5px; cursor: pointer;
}
.menu button:hover { background: var(--surface-2); }

/* ---- панель инструментов ---- */
main { max-width: 1500px; margin: 0 auto; padding: 16px 20px 60px; }
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 12px; }
.search {
  flex: 1 1 260px; min-width: 200px;
  border: 1px solid var(--line-strong); background: var(--surface);
  border-radius: 6px; padding: 8px 12px;
}
.select {
  border: 1px solid var(--line-strong); background: var(--surface);
  border-radius: 6px; padding: 8px 10px; max-width: 260px;
}
.btn {
  border: 1px solid var(--line-strong); background: var(--surface);
  border-radius: 6px; padding: 8px 14px; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
@media (prefers-color-scheme: dark) { .btn.primary { color: #10202f; } }
.btn.danger { color: var(--bad); }
.btn:disabled { opacity: 0.5; cursor: default; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.chip {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 4px 11px; cursor: pointer; color: var(--text-2); font-size: 13px;
  font-variant-numeric: tabular-nums;
}
.chip[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.chip .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; vertical-align: 0; }

.note {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  margin-bottom: 14px;
}
.note summary { cursor: pointer; padding: 10px 14px; color: var(--text-2); }
.note-body { padding: 0 14px 12px; white-space: pre-wrap; color: var(--text-2); font-size: 13px; }
.note-body textarea { width: 100%; min-height: 220px; }

/* ---- таблица ---- */
.table-wrap {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
}
thead th:first-child { border-top-left-radius: var(--radius); }
thead th:last-child { border-top-right-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; }
thead th {
  text-align: left; font-weight: 600; font-size: 12px; color: var(--text-3);
  text-transform: uppercase; letter-spacing: 0.04em;
  padding: 10px 12px; border-bottom: 1px solid var(--line); background: var(--surface);
  position: sticky; top: var(--top-h, 57px); z-index: 5; cursor: pointer; user-select: none; white-space: nowrap;
}
thead th[data-sort]::after { content: " ↕"; color: var(--line-strong); }
thead th.asc::after { content: " ↑"; color: var(--text-2); }
thead th.desc::after { content: " ↓"; color: var(--text-2); }
tbody td { padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
tbody tr { cursor: pointer; }
tbody tr:hover td { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
.c-grp { color: var(--text-2); width: 16%; }
.c-name { font-weight: 550; width: 20%; }
.c-status { width: 17%; }
.c-docs { white-space: nowrap; color: var(--text-2); font-variant-numeric: tabular-nums; width: 90px; }
.c-gaps { color: var(--text-2); }
.c-date { white-space: nowrap; color: var(--text-3); font-size: 12px; width: 90px; }
.clamp { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.empty { padding: 40px; text-align: center; color: var(--text-3); }

.pill {
  display: inline-block; max-width: 100%; padding: 2px 8px; border-radius: 6px; font-size: 12.5px;
  line-height: 1.45; background: var(--muted-bg); color: var(--muted);
}
.pill.ok { background: var(--ok-bg); color: var(--ok); }
.pill.warn { background: var(--warn-bg); color: var(--warn); }
.pill.bad { background: var(--bad-bg); color: var(--bad); }
.pill.muted { background: var(--muted-bg); color: var(--muted); }
.dot.ok { background: var(--ok); } .dot.warn { background: var(--warn); }
.dot.bad { background: var(--bad); } .dot.muted, .dot.neutral { background: var(--text-3); }

/* ---- карточка записи ---- */
.scrim { position: fixed; inset: 0; background: rgba(15, 14, 12, 0.35); z-index: 40; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(720px, 100vw);
  background: var(--surface); z-index: 50; box-shadow: var(--shadow);
  display: flex; flex-direction: column;
}
.drawer-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 20px; border-bottom: 1px solid var(--line);
}
.drawer-head h2 { margin: 0; font-size: 16px; font-weight: 600; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.x { border: 0; background: none; font-size: 22px; line-height: 1; cursor: pointer; color: var(--text-3); padding: 4px 8px; }
.drawer-body { overflow-y: auto; padding: 16px 20px 20px; flex: 1; }
.drawer-foot {
  display: flex; gap: 8px; align-items: center; padding: 12px 20px;
  border-top: 1px solid var(--line); background: var(--surface);
}
.drawer-foot .spacer { flex: 1; }
.msg { color: var(--bad); font-size: 13px; }
.msg.ok { color: var(--ok); }

.form { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 14px; }
.form .full { grid-column: 1 / -1; }
.field label { display: block; font-size: 12px; color: var(--text-3); margin-bottom: 4px; }
.field input, .field textarea, .field select {
  width: 100%; border: 1px solid var(--line-strong); background: var(--surface);
  border-radius: 6px; padding: 7px 10px;
}
.field textarea { min-height: 110px; resize: vertical; }
input:focus, textarea:focus, select:focus, button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}

h3.sub { font-size: 13px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-3); margin: 22px 0 8px; font-weight: 600; }
.items { display: flex; flex-direction: column; gap: 8px; }
.item {
  display: grid; grid-template-columns: 1fr 150px 30px; gap: 6px;
  border: 1px solid var(--line); border-radius: 6px; padding: 8px;
}
.item input { border: 1px solid var(--line); background: var(--surface); border-radius: 5px; padding: 5px 8px; width: 100%; }
.item .path { grid-column: 1 / 3; font-size: 12px; color: var(--text-2); }
.item .del { border: 0; background: none; cursor: pointer; color: var(--text-3); font-size: 18px; }
.hist { font-size: 13px; color: var(--text-2); }
.hist li { margin-bottom: 6px; }
.hist .when { color: var(--text-3); margin-right: 6px; font-variant-numeric: tabular-nums; }
.meta { font-size: 12px; color: var(--text-3); margin-top: 16px; }

dialog {
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface);
  color: var(--text); box-shadow: var(--shadow); padding: 20px; width: min(420px, calc(100vw - 32px));
}
dialog::backdrop { background: rgba(15, 14, 12, 0.35); }
dialog h2 { margin: 0 0 14px; font-size: 17px; }
dialog .field { margin-bottom: 12px; }
dialog .row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; }

/* ---- вход ---- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login {
  width: min(380px, 100%); background: var(--surface); border: 1px solid var(--line);
  border-radius: 10px; padding: 28px; box-shadow: var(--shadow);
}
.login h1 { margin: 0 0 4px; font-size: 20px; }
.login p { margin: 0 0 20px; color: var(--text-3); }
.login .field { margin-bottom: 14px; }
.login .btn { width: 100%; padding: 10px; }

/* ---- телефон ---- */
@media (max-width: 760px) {
  .top-row { flex-wrap: wrap; padding: 10px 16px; gap: 8px; }
  .tabs { order: 3; flex-basis: 100%; }
  .brand { flex: 1; }
  main { padding: 12px 16px 60px; }
  thead { display: none; }
  table, tbody, tr, td { display: block; width: 100% !important; }
  tbody tr { padding: 10px 12px; border-bottom: 1px solid var(--line); }
  tbody td { border: 0; padding: 2px 0; }
  .c-date, .c-docs, .c-extra { display: none; }
  .c-name { font-size: 15px; }
  .form { grid-template-columns: 1fr; }
  .item { grid-template-columns: 1fr 30px; }
  .item .state { grid-column: 1 / 2; }
  .item .path { grid-column: 1 / 2; }
}
.login .msg { margin: 12px 0 0; min-height: 1em; }
