/* Основа панели: токены, каркас, кнопки, поля, таблицы, диалоги.
   Цвета данных (--s1…--s5) и хром графиков — из проверенной палитры
   (см. dashboard.css): тёмная тема — свои ступени, а не инверсия. */

:root {
  color-scheme: light;
  --page: #f4f4f1;
  --surface: #fcfcfb;
  --surface-2: #f1f1ed;
  --surface-3: #e8e7e1;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --ink-3: #6f6d68;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --border-strong: rgba(11, 11, 11, 0.18);
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.04), 0 6px 20px rgba(11, 11, 11, 0.05);
  --shadow-lg: 0 24px 64px rgba(11, 11, 11, 0.2);
  --primary: #0b0b0b;
  --on-primary: #ffffff;
  --focus: #2a78d6;

  --s1: #2a78d6;
  --s2: #eb6834;
  --s3: #1baf7a;
  --s4: #eda100;
  --s5: #e87ba4;

  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --good-ink: #006300;
  --warning-ink: #7a4a00;
  --critical-ink: #b42318;
  --good-wash: rgba(12, 163, 12, 0.1);
  --warning-wash: rgba(250, 178, 25, 0.16);
  --serious-wash: rgba(236, 131, 90, 0.14);
  --critical-wash: rgba(208, 59, 59, 0.1);

  --radius: 16px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Consolas, monospace;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232321;
  --surface-3: #2e2e2b;
  --ink: #ffffff;
  --ink-2: #c3c2b7;
  --ink-3: #a3a198;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.1);
  --border-strong: rgba(255, 255, 255, 0.18);
  --shadow: none;
  --shadow-lg: 0 24px 64px rgba(0, 0, 0, 0.6);
  --primary: #ffffff;
  --on-primary: #0b0b0b;
  --focus: #3987e5;

  --s1: #3987e5;
  --s2: #d95926;
  --s3: #199e70;
  --s4: #c98500;
  --s5: #d55181;

  --good-ink: #3ccf3c;
  --warning-ink: #fab219;
  --critical-ink: #f27a7a;
  --good-wash: rgba(12, 163, 12, 0.16);
  --warning-wash: rgba(250, 178, 25, 0.13);
  --serious-wash: rgba(236, 131, 90, 0.15);
  --critical-wash: rgba(208, 59, 59, 0.2);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font: 14px/1.45 var(--font);
  color: var(--ink);
  background: var(--page);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3, p { margin: 0; }
a { color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.icon {
  width: 18px; height: 18px; flex: none;
  fill: none; stroke: currentColor; stroke-width: 1.8;
  stroke-linecap: round; stroke-linejoin: round;
}
.muted { color: var(--ink-3); }
.mono { font-family: var(--mono); font-size: 13px; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.nowrap { white-space: nowrap; }

/* ── Каркас ─────────────────────────────────────────────────────────────── */

.shell { display: grid; grid-template-columns: 244px minmax(0, 1fr); min-height: 100vh; }

.sidebar {
  position: sticky; top: 0; height: 100vh;
  display: flex; flex-direction: column; gap: 28px;
  padding: 20px 14px;
  background: var(--surface);
  border-right: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 11px; padding: 2px 8px; text-decoration: none; }
.brand-mark {
  width: 36px; height: 36px; border-radius: 11px;
  display: grid; place-items: center;
  background: var(--primary); color: var(--on-primary);
}
.brand-mark .icon { width: 20px; height: 20px; }
.brand-name { font-weight: 650; font-size: 15px; letter-spacing: -0.01em; line-height: 1.2; }
.brand-sub { font-size: 12px; color: var(--ink-3); }

.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-link {
  display: flex; align-items: center; gap: 11px;
  height: 38px; padding: 0 10px; border-radius: 10px;
  color: var(--ink-2); text-decoration: none; font-weight: 520;
  border: 0; background: none; cursor: pointer; width: 100%; text-align: left;
}
.nav-link:hover { background: var(--surface-2); color: var(--ink); }
.nav-link[aria-current="page"] { background: var(--surface-3); color: var(--ink); font-weight: 620; }
.nav-count { margin-left: auto; font-size: 12px; color: var(--ink-3); font-variant-numeric: tabular-nums; }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 2px; }

.main { min-width: 0; }
.topbar { display: none; }
.tabbar { display: none; }
.content { max-width: 1320px; margin: 0 auto; padding: 28px 32px 56px; }

.page-head {
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: 12px 16px; flex-wrap: wrap; margin-bottom: 20px;
}
.page-title { font-size: 26px; font-weight: 680; letter-spacing: -0.025em; line-height: 1.15; }
.page-sub { margin-top: 5px; color: var(--ink-3); }
.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* ── Кнопки и поля ──────────────────────────────────────────────────────── */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 36px; padding: 0 14px; border-radius: 10px;
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--ink);
  font-weight: 560; white-space: nowrap; cursor: pointer; text-decoration: none;
  transition: background-color 0.15s, opacity 0.15s, border-color 0.15s;
}
.btn:hover { background: var(--surface-2); }
.btn .icon { width: 16px; height: 16px; }
.btn-primary { background: var(--primary); color: var(--on-primary); border-color: transparent; }
.btn-primary:hover { background: var(--primary); opacity: 0.86; }
.btn-danger { background: var(--critical); color: #fff; border-color: transparent; }
.btn-danger:hover { background: var(--critical); opacity: 0.9; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--ink-2); }
.btn-ghost:hover { background: var(--surface-2); color: var(--ink); }
.btn-icon { width: 36px; padding: 0; }
.btn-sm { height: 30px; padding: 0 10px; border-radius: 8px; font-size: 13px; }
.btn-sm.btn-icon { width: 30px; padding: 0; }
.btn-block { width: 100%; }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn.is-busy { cursor: progress; }
.btn-danger-ghost { color: var(--critical-ink); }
.btn-danger-ghost:hover { background: var(--critical-wash); color: var(--critical-ink); }

.input, .select, .textarea {
  height: 36px; padding: 0 12px; min-width: 0;
  border-radius: 10px; border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--ink);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.input::placeholder, .textarea::placeholder { color: var(--muted); }
.input:focus, .select:focus, .textarea:focus {
  outline: none; border-color: var(--focus);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 22%, transparent);
}
.select {
  appearance: none; padding-right: 32px; cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23898781' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 11px center;
}
.textarea { height: auto; min-height: 84px; padding: 9px 12px; resize: vertical; line-height: 1.45; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field-label { font-size: 13px; font-weight: 580; color: var(--ink-2); }
.field-hint { font-size: 12px; color: var(--ink-3); }
.field-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.form-error { min-height: 18px; font-size: 13px; color: var(--critical-ink); }

.search { position: relative; display: flex; align-items: center; }
.search .icon { position: absolute; left: 11px; width: 16px; height: 16px; color: var(--muted); pointer-events: none; }
.search .input { padding-left: 34px; width: 240px; }

.segmented {
  display: inline-flex; gap: 2px; padding: 3px;
  border-radius: 11px; background: var(--surface-3);
}
.segmented button {
  height: 30px; padding: 0 12px; border: 0; border-radius: 8px;
  background: transparent; color: var(--ink-2); font-weight: 560; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px;
}
.segmented button:hover { color: var(--ink); }
.segmented button[aria-checked="true"],
.segmented button[aria-pressed="true"] {
  background: var(--surface); color: var(--ink);
  box-shadow: 0 1px 2px rgba(11, 11, 11, 0.08), 0 0 0 1px var(--border);
}
.segmented.sm button { height: 26px; padding: 0 8px; }
.segmented .icon { width: 15px; height: 15px; }

/* ── Карточки, бейджи, плашки ───────────────────────────────────────────── */

.card {
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding: 18px 20px 0; }
.card-title { font-size: 15px; font-weight: 620; letter-spacing: -0.01em; }
.card-sub { margin-top: 3px; font-size: 13px; color: var(--ink-3); }
.card-body { padding: 16px 20px 20px; }
.card-foot {
  display: flex; flex-wrap: wrap; gap: 6px 18px;
  padding: 12px 20px; border-top: 1px solid var(--border);
  font-size: 13px; color: var(--ink-2);
}

.badge {
  display: inline-flex; align-items: center; gap: 6px;
  height: 22px; padding: 0 8px; border-radius: 999px;
  font-size: 12px; font-weight: 560; white-space: nowrap;
  color: var(--ink-2); background: var(--surface-2); border: 1px solid var(--border);
}
.badge .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--c, var(--muted)); flex: none; }
.badge .icon { width: 13px; height: 13px; stroke-width: 2.2; }
.badge-good { color: var(--good-ink); background: var(--good-wash); border-color: transparent; }
.badge-warning { color: var(--warning-ink); background: var(--warning-wash); border-color: transparent; }
.badge-critical { color: var(--critical-ink); background: var(--critical-wash); border-color: transparent; }
.badges { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }

.callout {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 12px 14px; border-radius: 12px; border: 1px solid transparent;
}
.callout > .icon { width: 18px; height: 18px; stroke-width: 2; }
.callout-body { flex: 1 1 260px; min-width: 0; }
.callout-title { font-weight: 620; }
.callout-text { font-size: 13px; color: var(--ink-2); margin-top: 1px; }
.callout-serious { background: var(--serious-wash); }
.callout-serious > .icon { color: var(--serious); }
.callout-warning { background: var(--warning-wash); }
.callout-warning > .icon { color: var(--warning-ink); }
.callout-critical { background: var(--critical-wash); }
.callout-critical > .icon { color: var(--critical-ink); }
.callout-info { background: var(--surface-2); border-color: var(--border); }
.callout-info > .icon { color: var(--ink-3); }

/* ── Таблицы ────────────────────────────────────────────────────────────── */

.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th {
  padding: 12px; text-align: left; white-space: nowrap;
  font-size: 12px; font-weight: 560; color: var(--ink-3);
  border-bottom: 1px solid var(--border);
}
.table td { padding: 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
.table th:first-child, .table td:first-child { padding-left: 20px; }
.table th:last-child, .table td:last-child { padding-right: 20px; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr { transition: background-color 0.12s; }
.table tbody tr:hover { background: var(--surface-2); }
.table tr.is-dim > td:not(.actions) { opacity: 0.6; }
.table .actions { width: 1%; text-align: right; white-space: nowrap; }
.cell-main { font-weight: 580; color: var(--ink); }
.cell-sub { margin-top: 2px; font-size: 12px; color: var(--ink-3); }
.cell-clip { max-width: 360px; overflow-wrap: anywhere; }

.empty { padding: 44px 20px; text-align: center; color: var(--ink-3); }
.empty .icon { width: 34px; height: 34px; stroke-width: 1.5; color: var(--muted); margin-bottom: 8px; }
.empty-title { color: var(--ink-2); font-weight: 620; }

/* ── Диалоги, всплывашки ────────────────────────────────────────────────── */

.modal {
  width: min(500px, calc(100vw - 32px)); max-height: calc(100vh - 32px);
  padding: 0; border: 1px solid var(--border); border-radius: 18px;
  background: var(--surface); color: var(--ink); box-shadow: var(--shadow-lg);
}
.modal::backdrop { background: rgba(11, 11, 11, 0.45); backdrop-filter: blur(3px); }
.modal[open] { animation: pop 0.18s ease-out; }
.modal-sm { width: min(420px, calc(100vw - 32px)); }
.modal-head { padding: 22px 22px 0; }
.modal-title { font-size: 18px; font-weight: 660; letter-spacing: -0.015em; }
.modal-sub { margin-top: 6px; font-size: 14px; color: var(--ink-2); white-space: pre-line; }
.modal-body { display: flex; flex-direction: column; gap: 14px; padding: 18px 22px 4px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 18px 22px 22px; }

.choice { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.choice label {
  display: flex; flex-direction: column; gap: 2px; padding: 10px 12px;
  border: 1px solid var(--border-strong); border-radius: 12px; cursor: pointer;
}
.choice input { position: absolute; opacity: 0; pointer-events: none; }
.choice label:has(input:checked) { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); }
.choice label:has(input:focus-visible) { outline: 2px solid var(--focus); outline-offset: 2px; }
.choice-title { font-weight: 600; }
.choice-text { font-size: 12px; color: var(--ink-3); }

.toasts {
  position: fixed; right: 20px; bottom: 20px; z-index: 80;
  display: flex; flex-direction: column; align-items: flex-end; gap: 8px;
}
.toast {
  display: flex; align-items: center; gap: 10px;
  max-width: min(380px, calc(100vw - 32px)); padding: 11px 14px; border-radius: 12px;
  background: var(--primary); color: var(--on-primary); box-shadow: var(--shadow-lg);
  font-weight: 540; animation: pop 0.2s ease-out;
}
.toast .icon { width: 16px; height: 16px; stroke-width: 2.2; }
.toast-error { background: var(--critical); color: #fff; }

@keyframes pop { from { opacity: 0; transform: translateY(6px) scale(0.985); } }

/* ── Вход ───────────────────────────────────────────────────────────────── */

.login {
  min-height: 100vh; display: grid; place-items: center; padding: 24px 16px;
  background:
    radial-gradient(900px 480px at 50% -8%, var(--surface-3), transparent 70%),
    var(--page);
}
.login-card { width: min(380px, 100%); padding: 30px 28px 28px; display: flex; flex-direction: column; gap: 16px; }
.login-card .brand-mark { width: 46px; height: 46px; border-radius: 14px; }
.login-card .brand-mark .icon { width: 24px; height: 24px; }
.login-title { font-size: 22px; font-weight: 680; letter-spacing: -0.02em; }
.login-sub { color: var(--ink-3); margin-top: 2px; }

/* ── Телефон и планшет ──────────────────────────────────────────────────── */

@media (max-width: 899px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar { display: none; }
  .topbar {
    position: sticky; top: 0; z-index: 30;
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    padding: 10px 16px;
    background: var(--surface);
    background: color-mix(in srgb, var(--surface) 86%, transparent);
    backdrop-filter: blur(14px);
    border-bottom: 1px solid var(--border);
  }
  .topbar .brand-mark { width: 32px; height: 32px; border-radius: 10px; }
  .topbar-actions { display: flex; gap: 4px; }
  .tabbar {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    display: grid; grid-template-columns: repeat(4, minmax(0, 1fr));
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    background: var(--surface);
    background: color-mix(in srgb, var(--surface) 90%, transparent);
    backdrop-filter: blur(14px);
    border-top: 1px solid var(--border);
  }
  .tabbar a {
    display: flex; flex-direction: column; align-items: center; gap: 3px;
    padding: 6px 2px; border-radius: 10px;
    color: var(--ink-3); text-decoration: none; font-size: 11px; font-weight: 560;
  }
  .tabbar a[aria-current="page"] { color: var(--ink); }
  .tabbar .icon { width: 22px; height: 22px; }
  .content { padding: 20px 16px 104px; }
  .page-title { font-size: 23px; }
  .toasts { left: 16px; right: 16px; bottom: calc(84px + env(safe-area-inset-bottom)); align-items: stretch; }
}

@media (max-width: 720px) {
  .table.stack thead { display: none; }
  .table.stack, .table.stack tbody, .table.stack tr, .table.stack td { display: block; width: auto; }
  .table.stack tr { padding: 12px 16px; border-bottom: 1px solid var(--border); }
  .table.stack tbody tr:last-child { border-bottom: 0; }
  .table.stack td, .table.stack td:first-child, .table.stack td:last-child { padding: 3px 0; border: 0; }
  .table.stack td[data-label]::before {
    content: attr(data-label); display: block; margin-bottom: 1px;
    font-size: 11px; font-weight: 560; color: var(--ink-3);
  }
  .table.stack td.actions { width: auto; text-align: left; padding-top: 8px; }
  /* История на телефоне — плотная карточка вместо столбика подписей:
     тип и время, детали, локация и отправитель, действия справа. */
  .table.stack.history-table tr {
    display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 4px 12px;
    grid-template-areas: "type when" "details details" "loc actions" "who actions";
  }
  .table.stack.history-table td::before { display: none !important; }
  .table.stack.history-table td:nth-child(1) { grid-area: when; text-align: right; font-size: 12px; color: var(--ink-3); }
  .table.stack.history-table td:nth-child(2) { grid-area: type; }
  .table.stack.history-table td:nth-child(3) { grid-area: loc; font-size: 13px; color: var(--ink-2); }
  .table.stack.history-table td:nth-child(4) { grid-area: details; padding: 4px 0; }
  .table.stack.history-table td:nth-child(5) { grid-area: who; font-size: 12px; color: var(--ink-3); }
  .table.stack.history-table td.actions { grid-area: actions; align-self: end; padding-top: 0; }
  #historyFilters .select { flex: 1 1 100%; }
  #historyFilters .input { flex: 1 1 calc(50% - 4px); }
  .search, .search .input { width: 100%; }
  .toolbar { width: 100%; }
  .toolbar > .search { flex: 1 1 100%; }
  .field-row { grid-template-columns: minmax(0, 1fr); }
  .cell-clip { max-width: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
