:root {
  --bg: #f4f6f9; --card: #fff; --text: #0f172a; --muted: #64748b; --line: #e2e8f0; --line2: #f1f5f9;
  --side: #0f172a; --side-text: #cbd5e1; --side-muted: #64748b; --accent: #16a34a; --accent-d: #15803d;
  --blue: #2563eb; --green: #16a34a; --red: #dc2626; --amber: #d97706; --violet: #7c3aed; --teal: #0d9488; --grey: #64748b;
  --radius: 10px; --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 1px 3px rgba(15, 23, 42, .04);
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; color: var(--text); background: var(--bg); }
a { color: var(--blue); text-decoration: none; }
h1, h2, h3, h4 { margin: 0; line-height: 1.25; }
h3 { font-size: 15px; margin-bottom: 12px; }
h4 { font-size: 13px; margin: 18px 0 8px; color: #334155; text-transform: uppercase; letter-spacing: .03em; }
code { background: var(--line2); padding: 1px 5px; border-radius: 4px; }
.muted { color: var(--muted); } .small { font-size: 12px; } .neg { color: var(--red); } .pos { color: var(--green); }
.loading { padding: 40px; text-align: center; color: var(--muted); }
.loading.full { min-height: 100vh; display: grid; place-items: center; }

/* ---------- Layout ---------- */
.app { display: flex; min-height: 100vh; }
.sidebar { width: 240px; flex: none; background: var(--side); color: var(--side-text); display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.brand { display: flex; align-items: center; gap: 10px; padding: 18px 16px; border-bottom: 1px solid #1e293b; }
.brand b { display: block; color: #fff; font-size: 15px; } .brand small { color: var(--side-muted); font-size: 12px; }
.logo { width: 36px; height: 36px; border-radius: 9px; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 800; flex: none; }
nav { padding: 8px 8px 16px; flex: 1; }
.nav-group > span { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--side-muted); padding: 14px 10px 6px; }
nav a { display: flex; align-items: center; gap: 10px; color: var(--side-text); padding: 8px 10px; border-radius: 7px; font-size: 13.5px; }
nav a svg { width: 18px; height: 18px; flex: none; opacity: .8; }
nav a:hover { background: #1e293b; color: #fff; }
nav a.on { background: var(--accent); color: #fff; } nav a.on svg { opacity: 1; }
.side-foot { font-size: 11px; color: var(--side-muted); padding: 12px 16px 16px; border-top: 1px solid #1e293b; line-height: 1.6; }
.main-col { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar { position: sticky; top: 0; z-index: 20; background: rgba(255, 255, 255, .92); backdrop-filter: blur(6px); border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 12px; padding: 10px 24px; }
.crumb { font-weight: 600; flex: 1; }
.user-menu { display: flex; align-items: center; gap: 8px; }
.role-pill { font-size: 12px; background: #dcfce7; color: #166534; border-radius: 99px; padding: 3px 10px; font-weight: 600; }
.menu { display: none; font-size: 20px; }
main { padding: 22px 24px 60px; max-width: 1500px; width: 100%; }
.scrim { display: none; }

/* ---------- Page ---------- */
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head h1 { font-size: 22px; } .page-head p { margin: 4px 0 0; color: var(--muted); }
.head-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 16px; min-width: 0; }
.error-card { border-color: #fecaca; background: #fef2f2; }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.grid2 > .card { margin-bottom: 0; } .grid2 { margin-bottom: 16px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.chips > span { background: var(--card); border: 1px solid var(--line); border-radius: 99px; padding: 5px 12px; font-size: 13px; color: #334155; display: inline-flex; gap: 6px; align-items: center; }
.chips > span.red b { color: var(--red); } .chips > span.amber b { color: var(--amber); } .chips > span.grey b { color: var(--grey); }
.note { background: #fffbeb; border: 1px solid #fde68a; padding: 8px 12px; border-radius: 8px; margin: 10px 0; white-space: pre-wrap; }
.notice { background: #eff6ff; border: 1px solid #bfdbfe; padding: 8px 12px; border-radius: 8px; }

/* ---------- Workflow strip & KPIs ---------- */
.flow { display: flex; align-items: stretch; gap: 4px; overflow-x: auto; margin-bottom: 16px; padding-bottom: 4px; }
.stage { flex: 1 0 104px; background: var(--card); border: 1px solid var(--line); border-radius: 9px; padding: 9px 10px; color: var(--text); display: flex; flex-direction: column; box-shadow: var(--shadow); }
.stage span { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--accent-d); font-weight: 700; }
.stage b { font-size: 16px; margin: 2px 0; white-space: nowrap; } .stage small { color: var(--muted); font-size: 11px; }
.stage:hover { border-color: var(--accent); } .stage.dim { opacity: .55; }
.flow .arrow { align-self: center; color: #94a3b8; font-style: normal; font-size: 18px; }
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--card); border: 1px solid var(--line); border-left: 4px solid var(--c, var(--blue)); border-radius: var(--radius); padding: 12px 14px; color: var(--text); display: flex; flex-direction: column; gap: 2px; box-shadow: var(--shadow); }
.kpi span { color: var(--muted); font-size: 12.5px; font-weight: 600; } .kpi b { font-size: 22px; letter-spacing: -.01em; } .kpi small { color: var(--muted); font-size: 12px; }
a.kpi:hover { box-shadow: 0 4px 12px rgba(15, 23, 42, .08); }
.kpis.small b { font-size: 18px; }
.kpi.blue { --c: var(--blue); } .kpi.green { --c: var(--green); } .kpi.red { --c: var(--red); } .kpi.amber { --c: var(--amber); }
.kpi.violet { --c: var(--violet); } .kpi.teal { --c: var(--teal); } .kpi.grey { --c: var(--grey); }
.todo { list-style: none; margin: 0; padding: 0; }
.todo a { display: flex; align-items: center; gap: 10px; padding: 8px 6px; border-bottom: 1px solid var(--line2); color: var(--text); }
.todo a:hover { background: var(--line2); }
.todo b { min-width: 30px; text-align: center; background: #fef3c7; color: #92400e; border-radius: 6px; padding: 2px 6px; }
.todo span { margin-left: auto; color: var(--muted); }

/* ---------- Chart ---------- */
.chart .bars { display: flex; align-items: flex-end; gap: 4px; height: var(--h); border-bottom: 1px solid var(--line); padding-top: 8px; }
.bar-group { flex: 1; display: flex; align-items: flex-end; justify-content: center; gap: 2px; height: 100%; position: relative; padding-bottom: 18px; min-width: 0; }
.bar-group span { position: absolute; bottom: 0; font-size: 10px; color: var(--muted); }
.bar { width: 42%; max-width: 16px; border-radius: 3px 3px 0 0; min-height: 1px; }
.c1 { background: #22c55e; } .c2 { background: #f97316; }
.legend { display: flex; gap: 16px; font-size: 12px; color: var(--muted); margin-top: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; }

/* ---------- Buttons & badges ---------- */
.btn { font: inherit; font-size: 13.5px; border: 1px solid #cbd5e1; background: #fff; color: var(--text); border-radius: 8px; padding: 7px 14px; cursor: pointer; white-space: nowrap; display: inline-flex; align-items: center; gap: 6px; }
.btn:hover { background: #f8fafc; border-color: #94a3b8; }
.btn:disabled { opacity: .55; cursor: wait; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; } .btn.primary:hover { background: var(--accent-d); }
.btn.danger { background: #fff; border-color: #fca5a5; color: var(--red); } .btn.danger:hover { background: #fef2f2; }
.btn.ghost { border-color: transparent; }
.btn.sm { padding: 4px 10px; font-size: 12.5px; border-radius: 6px; }
.btn.ok { border-color: #86efac; color: #166534; background: #f0fdf4; }
.btn.danger-text { color: var(--red); border-color: transparent; background: transparent; }
.btn.block { width: 100%; justify-content: center; padding: 10px; }
.icon-btn { border: 0; background: transparent; cursor: pointer; font-size: 15px; color: var(--muted); padding: 4px 8px; border-radius: 6px; }
.icon-btn:hover { background: var(--line2); color: var(--text); }
.badge { display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: 11.5px; font-weight: 600; white-space: nowrap; }
.badge.grey { background: #f1f5f9; color: #475569; } .badge.amber { background: #fef3c7; color: #92400e; } .badge.blue { background: #dbeafe; color: #1e40af; }
.badge.violet { background: #ede9fe; color: #5b21b6; } .badge.teal { background: #ccfbf1; color: #115e59; } .badge.green { background: #dcfce7; color: #166534; } .badge.red { background: #fee2e2; color: #991b1b; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--line2); text-align: left; vertical-align: middle; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); background: #f8fafc; font-weight: 600; white-space: nowrap; border-bottom: 1px solid var(--line); }
.dt th { cursor: pointer; user-select: none; } .dt th.sorted { color: var(--text); }
td.r, th.r { text-align: right; } td.r { font-variant-numeric: tabular-nums; }
tbody tr:hover { background: #fafbfc; } tr.clickable { cursor: pointer; }
tfoot td { font-weight: 700; background: #f8fafc; border-top: 1px solid var(--line); }
td.empty { text-align: center; color: var(--muted); padding: 28px; }
td.actions { white-space: nowrap; } td.actions .btn { margin-left: 4px; }
table.mini td, table.mini th { padding: 6px 8px; font-size: 13px; }
.dt-tools { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 10px; }
.dt-search { flex: 1 1 220px; max-width: 320px; }
.dt-count { color: var(--muted); font-size: 12.5px; margin-left: auto; }
.dt-more { text-align: center; padding-top: 10px; }
input, select, textarea { font: inherit; font-size: 13.5px; border: 1px solid #cbd5e1; border-radius: 7px; padding: 7px 9px; background: #fff; color: var(--text); width: 100%; }
.dt-tools select, .dt-tools input { width: auto; }
input:focus, select:focus, textarea:focus { outline: 2px solid #86efac; border-color: var(--accent); }
input[readonly] { background: var(--line2); }

/* ---------- Tabs & range ---------- */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; overflow-x: auto; }
.tab { font: inherit; border: 0; background: none; padding: 9px 14px; cursor: pointer; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tab.on { color: var(--accent-d); border-color: var(--accent); font-weight: 600; }
.range { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.range label { display: flex; gap: 6px; align-items: center; font-size: 13px; color: var(--muted); }
.range input, .range select { width: auto; }
.card > .range { margin-bottom: 0; } .card .range + div { margin-top: 12px; }

/* ---------- Forms & modal ---------- */
.grid-form { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px 14px; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field label { font-size: 12.5px; font-weight: 600; color: #334155; } .field label b { color: var(--red); }
.field small { color: var(--muted); font-size: 11.5px; }
.span2 { grid-column: span 2; } .span3 { grid-column: 1 / -1; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13.5px; align-self: end; padding-bottom: 8px; }
.check input { width: auto; }
.modal-back { position: fixed; inset: 0; background: rgba(15, 23, 42, .45); z-index: 100; display: flex; align-items: flex-start; justify-content: center; padding: 5vh 16px; overflow-y: auto; }
.modal { background: #fff; border-radius: 12px; width: 100%; max-width: 600px; box-shadow: 0 20px 50px rgba(0, 0, 0, .25); display: flex; flex-direction: column; }
.modal.wide { max-width: 1060px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 16px 18px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--line); background: #f8fafc; border-radius: 0 0 12px 12px; flex-wrap: wrap; }
.kv { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; background: #f8fafc; border: 1px solid var(--line); border-radius: 9px; padding: 12px; margin-bottom: 12px; }
.kv span { display: block; font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; } .kv b { font-weight: 600; font-size: 13.5px; }
.lines table td { padding: 5px 6px; } .lines input, .lines select { padding: 6px 7px; } .lines .add-line { margin-top: 8px; }
.lines td.r input { text-align: right; }
.calc { font-variant-numeric: tabular-nums; white-space: nowrap; }
.totals { text-align: right; margin-top: 12px; color: #334155; } .totals b.big { font-size: 17px; }
.tot-box { margin: 12px 0 0 auto; width: min(320px, 100%); } .tot-box > div { display: flex; justify-content: space-between; padding: 3px 0; }
.tot-box .g { border-top: 1px solid var(--text); margin-top: 4px; padding-top: 6px; font-size: 16px; }
.attach { margin-top: 14px; border-top: 1px dashed var(--line); padding-top: 4px; }
.attach ul { list-style: none; padding: 0; margin: 0 0 8px; } .attach li { padding: 4px 0; display: flex; gap: 10px; align-items: center; } .attach small { color: var(--muted); }
.upload { cursor: pointer; }
.demo-log { max-height: 220px; overflow: auto; font-size: 12.5px; margin: 10px 0; color: #166534; }

/* ---------- Reports ---------- */
.reports { display: grid; grid-template-columns: 260px minmax(0, 1fr); gap: 16px; align-items: start; }
.report-list { padding: 6px; }
.report-list a { display: block; padding: 9px 10px; border-radius: 7px; color: var(--text); }
.report-list a small { display: block; color: var(--muted); font-size: 12px; }
.report-list a:hover { background: var(--line2); } .report-list a.on { background: #dcfce7; } .report-list a.on b { color: #166534; }
.report-head { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; justify-content: space-between; margin-bottom: 10px; }
.report-head h3 { margin: 0; }

/* ---------- Toasts & login ---------- */
#toasts { position: fixed; right: 16px; bottom: 16px; z-index: 200; display: flex; flex-direction: column; gap: 8px; }
.toast { background: #0f172a; color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: 0 6px 20px rgba(0, 0, 0, .2); max-width: 420px; transition: opacity .3s; font-size: 13.5px; }
.toast.err { background: #b91c1c; } .toast.out { opacity: 0; }
.login-wrap { min-height: 100vh; display: flex; padding: 20px; background: linear-gradient(135deg, #0f172a, #14532d); }
.login { width: 100%; max-width: 380px; margin: auto; padding: 28px; display: flex; flex-direction: column; gap: 14px; }
.brand.big { border: 0; padding: 0 0 6px; } .brand.big b { color: var(--text); font-size: 18px; }
.login h2 { font-size: 18px; } .login p { margin: 0; } .err { color: var(--red); margin: 0; }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) { .grid2 { grid-template-columns: 1fr; } .reports { grid-template-columns: 1fr; } .report-list { display: flex; overflow-x: auto; } .report-list a { flex: none; } .report-list a small { display: none; } }
@media (max-width: 860px) {
  .sidebar { position: fixed; left: 0; top: 0; bottom: 0; z-index: 60; transform: translateX(-100%); transition: transform .2s; }
  body.nav-open .sidebar { transform: none; }
  body.nav-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 50; }
  .menu { display: inline-block; }
  main { padding: 16px 12px 60px; }
  .topbar { padding: 8px 12px; } .user-menu [data-pw] { display: none; }
  .grid-form { grid-template-columns: 1fr 1fr; } .span3 { grid-column: 1 / -1; }
  .kpi b { font-size: 19px; }
}
@media (max-width: 560px) {
  .grid-form { grid-template-columns: 1fr; } .span2 { grid-column: auto; }
  .kpis { grid-template-columns: 1fr 1fr; }
  .modal-back { padding: 0; } .modal { border-radius: 0; min-height: 100vh; }
  .dt-search { max-width: none; }
  /* Operational tables become stacked cards on phones */
  .dt table thead { display: none; }
  .dt table, .dt tbody, .dt tr, .dt td { display: block; width: 100%; }
  .dt tbody tr { border: 1px solid var(--line); border-radius: 8px; margin-bottom: 8px; padding: 4px 0; }
  .dt td { display: flex; justify-content: space-between; gap: 12px; border: 0; padding: 4px 10px; text-align: right; }
  .dt td::before { content: attr(data-label); color: var(--muted); font-size: 12px; text-align: left; }
  .dt td.actions { justify-content: flex-end; } .dt td.actions::before { content: none; }
  .dt tfoot { display: none; }
}
@media print { .sidebar, .topbar, .head-actions, .dt-tools { display: none !important; } main { padding: 0; } .card { box-shadow: none; } }
