:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --ink: #17202b;
  --ink-2: #4a5566;
  --muted: #7a8494;
  --line: #e1e5eb;
  --line-2: #eef0f4;
  --brand: #1d4e89;
  --brand-ink: #ffffff;
  --brand-soft: #e8eff8;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100; --s5: #e87ba4; --s6: #008300; --s7: #6250d6; --s8: #e34948;
  --ok: #157f3b; --ok-soft: #e6f4ea;
  --warn: #9a6200; --warn-soft: #fff4dc;
  --bad: #b42318; --bad-soft: #fdecea;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  color: var(--ink);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
.hidden { display: none !important; }
a { color: var(--brand); }
h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -.01em; }
h1 { font-size: 20px; }
h2 { font-size: 15px; }
h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-2); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.num { font-variant-numeric: tabular-nums; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: linear-gradient(160deg, #e8eff8, #f4f5f7 55%); }
.login-card { width: 100%; max-width: 360px; background: var(--surface); padding: 32px 28px; border-radius: 12px; box-shadow: 0 10px 30px rgba(16,24,40,.08); display: grid; gap: 14px; text-align: center; }
.login-card label { text-align: left; }
.login-card h1 { font-size: 22px; }
.login-card p { margin: -8px 0 6px; }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar { background: #13263f; color: #c9d4e3; display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; align-items: center; gap: 10px; padding: 18px 18px 16px; color: #fff; font-weight: 600; font-size: 16px; }
.brand-mark { width: 30px; height: 30px; border-radius: 7px; background: #2a5f9e; display: grid; place-items: center; font-size: 16px; color: #fff; }
.brand-mark.big { width: 48px; height: 48px; font-size: 26px; margin: 0 auto; background: var(--brand); border-radius: 11px; }
nav { display: flex; flex-direction: column; padding: 6px 10px; gap: 2px; flex: 1; }
nav a { color: #c9d4e3; text-decoration: none; padding: 9px 10px; border-radius: 6px; display: flex; gap: 10px; align-items: center; }
nav a .ico { width: 18px; text-align: center; opacity: .8; }
nav a:hover { background: rgba(255,255,255,.06); color: #fff; }
nav a.active { background: #2a5f9e; color: #fff; }
.sidebar-foot { padding: 14px 18px; border-top: 1px solid rgba(255,255,255,.08); font-size: 12px; }
.sidebar-foot #whoami { color: #fff; margin-bottom: 4px; }
.sidebar-foot .link { color: #9fb3cc; }
.main { padding: 22px 26px 40px; min-width: 0; }
.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head h1 { margin-right: auto; }

/* ---------- controls ---------- */
input, select, textarea { font: inherit; color: inherit; border: 1px solid var(--line); border-radius: 6px; padding: 7px 9px; background: #fff; min-width: 0; }
textarea { resize: vertical; min-height: 64px; }
input:focus, select:focus, textarea:focus { outline: 2px solid #b8d0ee; border-color: var(--brand); }
label { display: grid; gap: 4px; font-size: 12px; color: var(--ink-2); font-weight: 500; }
.btn { font: inherit; border: 1px solid var(--line); background: #fff; color: var(--ink); padding: 7px 13px; border-radius: 6px; cursor: pointer; white-space: nowrap; display: inline-flex; align-items: center; gap: 6px; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: #173f70; }
.btn.danger { color: var(--bad); border-color: #f1c4bf; }
.btn.sm { padding: 4px 9px; font-size: 12px; }
.btn.block { width: 100%; justify-content: center; padding: 10px; }
.btn:disabled { opacity: .5; cursor: default; }
.link { background: none; border: 0; padding: 0; color: var(--brand); cursor: pointer; font: inherit; }
.form-error { color: var(--bad); font-size: 13px; min-height: 1em; }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: end; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; margin-bottom: 14px; }
.toolbar label { font-size: 11px; }
.toolbar select, .toolbar input { padding: 5px 8px; font-size: 13px; }
.toolbar .grow { flex: 1; min-width: 180px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.chip { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 5px 12px; cursor: pointer; font-size: 13px; color: var(--ink-2); }
.chip.on { background: var(--brand); color: #fff; border-color: var(--brand); }
.chip .n { font-weight: 600; margin-left: 4px; }

/* ---------- cards / dashboard ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-head { display: flex; align-items: center; gap: 8px; padding: 12px 14px; border-bottom: 1px solid var(--line-2); }
.card-head h2 { margin-right: auto; }
.card-body { padding: 12px 14px; }
.grid { display: grid; gap: 14px; margin-bottom: 14px; }
.g-kpi { grid-template-columns: repeat(6, minmax(0, 1fr)); }
.g-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g-3 { grid-template-columns: 2fr 1fr; }
.kpi { padding: 14px 16px; cursor: pointer; }
.kpi:hover { border-color: #b8c6d8; }
.kpi .label { font-size: 12px; color: var(--ink-2); }
.kpi .value { font-size: 26px; font-weight: 600; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.kpi.alert { border-left: 3px solid var(--bad); }
.kpi.alert .value { color: var(--bad); }
.chart-box { position: relative; height: 260px; }
.chart-box.short { height: 200px; }
.tabs { display: flex; gap: 2px; background: var(--surface-2); border: 1px solid var(--line); border-radius: 6px; padding: 2px; }
.tabs button { border: 0; background: none; padding: 4px 10px; border-radius: 4px; cursor: pointer; font: inherit; font-size: 12px; color: var(--ink-2); }
.tabs button.on { background: #fff; color: var(--ink); box-shadow: var(--shadow); }

table.t { width: 100%; border-collapse: collapse; font-size: 13px; }
table.t th { text-align: left; font-weight: 600; color: var(--ink-2); font-size: 12px; padding: 7px 8px; border-bottom: 1px solid var(--line); background: var(--surface-2); position: sticky; top: 0; z-index: 1; }
table.t td { padding: 7px 8px; border-bottom: 1px solid var(--line-2); vertical-align: top; }
table.t tr:hover td { background: #fafbfc; }
table.t tr.click { cursor: pointer; }
table.t tfoot td { font-weight: 600; border-top: 1px solid var(--line); background: var(--surface-2); }
.scroll { max-height: 360px; overflow: auto; }
.empty { padding: 26px; text-align: center; color: var(--muted); }

.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--line-2); color: var(--ink-2); white-space: nowrap; }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.info { background: var(--brand-soft); color: var(--brand); }
.date-bad { color: var(--bad); font-weight: 600; }

/* ---------- grid (Tabulator) ---------- */
.grid-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.tabulator { font-size: 13px; border: 0; background: #fff; }
.tabulator .tabulator-header { background: var(--surface-2); border-bottom: 1px solid var(--line); }
.tabulator .tabulator-header .tabulator-col { background: var(--surface-2); }
.tabulator .tabulator-header .tabulator-col .tabulator-col-content { padding: 6px 8px; }
.tabulator .tabulator-header .tabulator-col .tabulator-col-title { font-weight: 600; color: var(--ink-2); font-size: 12px; }
.tabulator-row .tabulator-cell { padding: 6px 8px; border-right: 1px solid var(--line-2); }
.tabulator-row.tabulator-row-even { background: #fcfcfd; }
.tabulator-row.tabulator-selected { background: #dce9f8 !important; }
.tabulator-row .tabulator-cell.tabulator-editable { background-image: linear-gradient(transparent, transparent); }
.tabulator-row .tabulator-cell.tabulator-editing { border: 2px solid var(--brand); padding: 3px 6px; }
.tabulator .tabulator-footer { background: var(--surface-2); border-top: 1px solid var(--line); }
.grid-meta { display: flex; gap: 10px; align-items: center; margin-bottom: 8px; flex-wrap: wrap; }
.grid-meta .count { margin-right: auto; color: var(--ink-2); }

/* ---------- drawer & modal ---------- */
.overlay { position: fixed; inset: 0; background: rgba(15, 23, 35, .35); z-index: 50; }
.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(860px, 100vw); background: var(--bg); z-index: 51; display: flex; flex-direction: column; box-shadow: -10px 0 30px rgba(0,0,0,.12); }
.drawer-head { background: var(--surface); padding: 14px 18px; border-bottom: 1px solid var(--line); display: flex; gap: 12px; align-items: start; }
.drawer-head .title { flex: 1; min-width: 0; }
.drawer-head .title .sub { color: var(--muted); font-size: 12px; margin-top: 3px; }
.drawer-tabs { display: flex; gap: 0; background: var(--surface); border-bottom: 1px solid var(--line); padding: 0 12px; overflow-x: auto; }
.drawer-tabs button { border: 0; background: none; padding: 10px 12px; cursor: pointer; font: inherit; color: var(--ink-2); border-bottom: 2px solid transparent; white-space: nowrap; }
.drawer-tabs button.on { color: var(--brand); border-bottom-color: var(--brand); font-weight: 600; }
.drawer-body { flex: 1; overflow: auto; padding: 16px 18px 30px; }
.fields { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px 14px; }
.fields .wide { grid-column: span 3; }
.field-group { margin-bottom: 18px; }
.field-group > h3 { margin-bottom: 8px; }
.ro-value { padding: 7px 0; font-size: 13px; color: var(--ink); min-height: 32px; word-break: break-word; }

.timeline { display: grid; gap: 0; }
.tl-item { display: grid; grid-template-columns: 96px 1fr auto; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line-2); }
.tl-item .d { font-weight: 600; font-variant-numeric: tabular-nums; }
.tl-item .meta { color: var(--muted); font-size: 12px; margin-top: 2px; }

.modal { position: fixed; inset: 0; display: grid; place-items: center; z-index: 60; padding: 16px; }
.modal-card { background: var(--surface); border-radius: 10px; width: 100%; max-width: 520px; max-height: calc(100vh - 32px); display: flex; flex-direction: column; box-shadow: 0 20px 50px rgba(0,0,0,.2); }
.modal-card.wide { max-width: 860px; }
.modal-head { padding: 14px 18px; border-bottom: 1px solid var(--line-2); display: flex; align-items: center; }
.modal-head h2 { flex: 1; }
.modal-body { padding: 16px 18px; overflow: auto; display: grid; gap: 12px; }
.modal-foot { padding: 12px 18px; border-top: 1px solid var(--line-2); display: flex; justify-content: flex-end; gap: 8px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.row3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--ink); font-weight: 400; }
.check input { width: auto; }
.x { border: 0; background: none; font-size: 20px; cursor: pointer; color: var(--muted); line-height: 1; padding: 2px 6px; }

.toasts { position: fixed; bottom: 18px; right: 18px; display: grid; gap: 8px; z-index: 80; }
.toast { background: #17202b; color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); max-width: 420px; font-size: 13px; }
.toast.error { background: var(--bad); }
.toast.ok { background: #1f6f43; }

.stat-row { display: flex; gap: 22px; flex-wrap: wrap; }
.stat-row .s .l { font-size: 12px; color: var(--muted); }
.stat-row .s .v { font-size: 18px; font-weight: 600; font-variant-numeric: tabular-nums; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--ink-2); margin-top: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }

.report-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; }
.report-card { padding: 16px; display: grid; gap: 10px; align-content: start; }
.report-card p { margin: 0; color: var(--ink-2); font-size: 13px; }

.map-table select { width: 100%; padding: 4px 6px; font-size: 12px; }
.map-table td { vertical-align: middle; }
.steps { display: flex; gap: 8px; margin-bottom: 14px; }
.steps span { padding: 5px 12px; border-radius: 999px; background: #fff; border: 1px solid var(--line); font-size: 12px; color: var(--muted); }
.steps span.on { background: var(--brand); color: #fff; border-color: var(--brand); }

@media (max-width: 1100px) {
  .g-kpi { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .g-2, .g-3 { grid-template-columns: 1fr; }
  .fields { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .fields .wide { grid-column: span 2; }
}
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; }
  nav { flex-direction: row; flex-wrap: wrap; }
  .sidebar-foot { display: none; }
  .main { padding: 16px; }
  .g-kpi { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .fields, .row2, .row3 { grid-template-columns: 1fr; }
  .fields .wide { grid-column: span 1; }
}
@media print {
  .sidebar, .toolbar, .page-head .btn, .chips { display: none !important; }
  .app { display: block; }
  .main { padding: 0; }
  .card { box-shadow: none; }
  .scroll { max-height: none; overflow: visible; }
}
