:root {
  --brand: #0b3a75;
  --brand-2: #1456a8;
  --brand-ink: #ffffff;
  --accent: #f37021;
  --bg: #f3f6fb;
  --surface: #ffffff;
  --surface-2: #f8fafd;
  --ink: #16243a;
  --ink-2: #4a5a73;
  --ink-3: #7b8aa3;
  --line: #e2e8f2;
  --ok: #138a4b;
  --warn: #c76a00;
  --bad: #c62828;
  --info: #1456a8;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 36, 70, .06), 0 4px 16px rgba(16, 36, 70, .06);
  --font: "Inter", "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.45 var(--font); color: var(--ink); background: var(--bg); }
a { color: var(--brand-2); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, h4 { margin: 0; line-height: 1.25; }
h1 { font-size: 22px; font-weight: 650; }
h2 { font-size: 17px; font-weight: 650; }
h3 { font-size: 15px; font-weight: 650; }
.muted { color: var(--ink-3); }
.small { font-size: 12px; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.hidden { display: none !important; }

/* ---------- shell ---------- */
.topbar {
  height: 56px; background: linear-gradient(90deg, var(--brand), var(--brand-2)); color: var(--brand-ink);
  display: flex; align-items: center; padding: 0 16px; gap: 12px; position: sticky; top: 0; z-index: 20;
}
.logo { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; letter-spacing: .2px; }
.logo-mark {
  width: 30px; height: 30px; border-radius: 8px; background: var(--accent); display: grid; place-items: center;
  font-weight: 800; color: #fff; font-size: 15px;
}
.topbar .spacer { flex: 1; }
.topbar .who { font-size: 13px; opacity: .92; text-align: right; }
.topbar .who b { display: block; font-weight: 600; }
.topbar button.ghost { color: #fff; border-color: rgba(255,255,255,.35); }
.topbar .menu-btn { display: none; background: none; border: 0; color: #fff; font-size: 22px; cursor: pointer; }

.brand { display: flex; flex-direction: column; line-height: 1.15; }
.brand b { font-size: 16px; font-weight: 700; }
.brand small { font-size: 10.5px; font-weight: 500; opacity: .85; letter-spacing: .2px; }
.layout { display: grid; grid-template-columns: 232px 1fr; min-height: calc(100vh - 56px); align-items: start; }
/* The menu scrolls on its own and keeps its position while the page content changes. */
.sidebar { background: var(--surface); border-right: 1px solid var(--line); padding: 12px 8px 0;
  position: sticky; top: 56px; height: calc(100vh - 56px); overflow-y: auto; overscroll-behavior: contain;
  display: flex; flex-direction: column; scrollbar-width: thin; }
.sidebar::-webkit-scrollbar { width: 8px; }
.sidebar::-webkit-scrollbar-thumb { background: #c9d4e5; border-radius: 4px; }
.sidebar .ver { margin-top: auto; position: sticky; bottom: 0; background: var(--surface); border-top: 1px solid var(--line);
  padding: 10px 10px 12px; font-size: 11.5px; color: var(--ink-3); }
.login-ver { position: fixed; left: 14px; bottom: 10px; font-size: 12px; color: var(--ink-3); z-index: 2; }
.sidebar .group { font-size: 11px; text-transform: uppercase; letter-spacing: .6px; color: var(--ink-3); margin: 14px 10px 6px; }
.sidebar a {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; color: var(--ink-2); font-weight: 500;
}
.sidebar a:hover { background: var(--surface-2); text-decoration: none; color: var(--ink); }
.sidebar a.active { background: #e8f0fb; color: var(--brand); font-weight: 600; }
.sidebar a .ico { width: 18px; text-align: center; opacity: .85; }
.sidebar .badge { margin-left: auto; }
.main { padding: 20px 24px 40px; min-width: 0; }
.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head .spacer { flex: 1; }
.crumbs { font-size: 12px; color: var(--ink-3); margin-bottom: 4px; }

/* ---------- cards & grids ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card > .hd { padding: 14px 16px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.card > .hd .spacer { flex: 1; }
.card > .bd { padding: 16px; }
.card + .card, .card + .grid, .grid + .card, .grid + .grid { margin-top: 16px; }
.grid { display: grid; gap: 16px; }
.grid > .card { margin-top: 0; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.kpi { padding: 14px 16px; }
.kpi .lbl { color: var(--ink-3); font-size: 12px; font-weight: 500; text-transform: uppercase; letter-spacing: .4px; }
.kpi .val { font-size: 22px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: 12px; color: var(--ink-3); margin-top: 2px; }
.kpi.bad .val { color: var(--bad); }
.kpi.ok .val { color: var(--ok); }
.kpi.warn .val { color: var(--warn); }
.kpi { border-top: 3px solid var(--brand-2); }
.kpi.bad { border-top-color: var(--bad); } .kpi.ok { border-top-color: var(--ok); } .kpi.warn { border-top-color: var(--warn); }

.dl { display: grid; grid-template-columns: 160px 1fr; gap: 6px 12px; }
.dl dt { color: var(--ink-3); }
.dl dd { margin: 0; word-break: break-word; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; }
table.t { width: 100%; border-collapse: collapse; font-size: 13px; }
table.t th { text-align: left; font-weight: 600; color: var(--ink-2); background: var(--surface-2); padding: 9px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; position: sticky; top: 0; }
table.t td { padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.t tr:hover td { background: #fafcff; }
table.t tr.click { cursor: pointer; }
table.t th.num, table.t td.num { text-align: right; }
table.t tfoot td { font-weight: 650; background: var(--surface-2); }
.empty { padding: 32px; text-align: center; color: var(--ink-3); }

/* ---------- badges ---------- */
.badge { display: inline-flex; align-items: center; gap: 5px; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; background: #eef2f8; color: var(--ink-2); }
.badge .dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.badge.ok { background: #e6f5ec; color: var(--ok); }
.badge.bad { background: #fdecec; color: var(--bad); }
.badge.warn { background: #fff3e0; color: var(--warn); }
.badge.info { background: #e8f0fb; color: var(--info); }
.pill-count { background: var(--accent); color: #fff; border-radius: 999px; font-size: 11px; padding: 0 7px; font-weight: 700; }

/* ---------- forms ---------- */
label.f { display: block; font-size: 12px; font-weight: 600; color: var(--ink-2); }
label.f > input, label.f > select, label.f > textarea { display: block; margin-top: 5px; }
label.f .req { color: var(--bad); }
label.f .hint { font-weight: 400; color: var(--ink-3); }
input, select, textarea {
  font: inherit; color: var(--ink); background: #fff; border: 1px solid #cfd8e6; border-radius: 8px; padding: 8px 10px; width: 100%;
  min-height: 36px;
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(20, 86, 168, .25); border-color: var(--brand-2); }
input[readonly] { background: var(--surface-2); color: var(--ink-2); }
input[type=checkbox], input[type=radio] { width: auto; min-height: 0; }
.check { display: flex; align-items: center; gap: 8px; font-weight: 500; font-size: 13px; color: var(--ink); }
.form-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; }
.form-grid .span2 { grid-column: span 2; } .form-grid .span3 { grid-column: 1 / -1; }
.section-title { grid-column: 1 / -1; font-size: 13px; font-weight: 700; color: var(--brand); border-bottom: 1px solid var(--line); padding-bottom: 6px; margin-top: 6px; text-transform: uppercase; letter-spacing: .5px; }
.filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.filters > * { min-width: 140px; }
.filters .grow { flex: 1; min-width: 200px; }
.field-error { border-color: var(--bad) !important; }

button, .btn {
  font: inherit; font-weight: 600; border-radius: 8px; border: 1px solid transparent; padding: 8px 14px; cursor: pointer;
  background: var(--brand-2); color: #fff; display: inline-flex; align-items: center; gap: 6px; min-height: 36px; white-space: nowrap;
}
:is(button,.btn):hover { filter: brightness(1.06); }
button:disabled { opacity: .55; cursor: not-allowed; }
:is(button,.btn).secondary { background: #fff; color: var(--brand-2); border-color: #c7d5ea; }
:is(button,.btn).ghost { background: transparent; color: var(--ink-2); border-color: var(--line); }
:is(button,.btn).danger { background: var(--bad); }
:is(button,.btn).ok { background: var(--ok); }
:is(button,.btn).accent { background: var(--accent); }
:is(button,.btn).sm { padding: 4px 10px; min-height: 28px; font-size: 12px; }
button.link { background: none; border: 0; color: var(--brand-2); padding: 0; min-height: 0; font-weight: 600; }
.btn:hover { text-decoration: none; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* ---------- alerts, modal, toast ---------- */
.alert { padding: 10px 12px; border-radius: 8px; font-size: 13px; border: 1px solid; }
.alert.err { background: #fdecec; color: #8e1c1c; border-color: #f5c2c2; }
.alert.info { background: #e8f0fb; color: #0e3d78; border-color: #c4d7f2; }
.alert.warn { background: #fff6e8; color: #7a4300; border-color: #f6d9a8; }
.alert.ok { background: #e6f5ec; color: #0c5a31; border-color: #b9e2c9; }

.modal-bg { position: fixed; inset: 0; background: rgba(10, 25, 50, .45); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { background: #fff; border-radius: 12px; width: min(760px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.modal.wide { width: min(980px, 100%); }
.modal .hd { padding: 14px 18px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 10px; }
.modal .hd .x { margin-left: auto; background: none; border: 0; color: var(--ink-3); font-size: 22px; padding: 0 4px; }
.modal .bd { padding: 18px; overflow: auto; }
.modal .ft { padding: 12px 18px; border-top: 1px solid var(--line); display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
#toast { position: fixed; right: 16px; bottom: 16px; z-index: 60; display: flex; flex-direction: column; gap: 8px; }
#toast .t { background: var(--ink); color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); max-width: 380px; font-size: 13px; }
#toast .t.err { background: var(--bad); }
#toast .t.ok { background: var(--ok); }

.calc { background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; }
.calc .row { display: flex; justify-content: space-between; padding: 4px 0; font-variant-numeric: tabular-nums; }
.calc .row.total { border-top: 1px dashed #c7d5ea; margin-top: 4px; padding-top: 8px; font-weight: 700; }
.calc .row.neg span:last-child { color: var(--ok); }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; overflow-x: auto; }
.tabs button { background: none; color: var(--ink-2); border: 0; border-bottom: 2px solid transparent; border-radius: 0; padding: 10px 12px; }
.tabs button.active { color: var(--brand); border-bottom-color: var(--accent); }

.bar-chart { display: flex; align-items: flex-end; gap: 10px; height: 150px; padding-top: 8px; }
.bar-chart .b { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 6px; height: 100%; justify-content: flex-end; }
.bar-chart .b .fill { width: 100%; max-width: 46px; background: var(--brand-2); border-radius: 4px 4px 0 0; min-height: 2px; }
.bar-chart .b .v { font-size: 11px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.bar-chart .b .l { font-size: 11px; color: var(--ink-3); }
.stack { display: flex; height: 12px; border-radius: 6px; overflow: hidden; background: #edf1f7; }
.stack > div { height: 100%; }
.legend { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 10px; font-size: 12px; color: var(--ink-2); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }

.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { padding: 8px 0 8px 18px; border-left: 2px solid var(--line); position: relative; font-size: 13px; }
.timeline li::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--brand-2); position: absolute; left: -5px; top: 13px; }
.timeline li .when { font-size: 11px; color: var(--ink-3); }
pre.mail { white-space: pre-wrap; font: 13px/1.5 var(--font); background: var(--surface-2); padding: 12px; border-radius: 8px; border: 1px solid var(--line); margin: 0; }
code.kv { font-size: 11px; color: var(--ink-2); word-break: break-word; }

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: grid; grid-template-columns: 1.1fr 1fr; }
.login-hero { background: linear-gradient(140deg, var(--brand), var(--brand-2)); color: #fff; padding: 48px; display: flex; flex-direction: column; justify-content: space-between; }
.login-hero h1 { font-size: 30px; margin-top: 40px; }
.login-hero p { opacity: .85; max-width: 440px; font-size: 15px; }
.login-hero ul { padding-left: 18px; opacity: .9; line-height: 1.9; }
.login-form { display: grid; place-items: center; padding: 32px 16px; }
.login-form .card { width: min(380px, 100%); }

@media (max-width: 1100px) {
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: fixed; top: 56px; left: 0; bottom: 0; height: auto; width: 250px; z-index: 30; transform: translateX(-100%); transition: transform .2s; overflow-y: auto; }
  .topbar .brand small { display: none; }
  .login-ver { position: static; text-align: center; padding: 10px; }
  .sidebar.open { transform: none; box-shadow: var(--shadow); }
  .topbar .menu-btn { display: block; }
  .topbar .who, .topbar .portal-link { display: none; }
  .topbar .logo span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 170px; }
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpi .val { font-size: 18px; }
  .main { padding: 16px; }
  .g2, .g3, .g4, .form-grid { grid-template-columns: 1fr; }
  .form-grid .span2 { grid-column: auto; }
  .login-wrap { grid-template-columns: 1fr; }
  .login-hero { padding: 24px 16px; }
  .login-hero h1 { margin-top: 12px; font-size: 22px; }
  .dl { grid-template-columns: 1fr; }
  .dl dt { margin-top: 6px; }
}
@media print {
  .topbar, .sidebar, .no-print { display: none !important; }
  .layout { display: block; }
  body { background: #fff; }
}
