:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e3e8ef;
  --text: #1b2433;
  --muted: #667085;
  --primary: #1d4ed8;
  --primary-600: #1e40af;
  --primary-50: #eef3ff;
  --sidebar: #0f1b2d;
  --sidebar-text: #c6d0e0;
  --success: #15803d;
  --success-50: #ecfdf3;
  --danger: #b42318;
  --danger-50: #fef3f2;
  --warning: #b54708;
  --warning-50: #fffaeb;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: var(--font); font-size: 14px; color: var(--text); background: var(--bg); }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0; font-weight: 650; }
h2 { font-size: 16px; margin: 0 0 12px; font-weight: 600; }
h3 { font-size: 14px; margin: 0 0 8px; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 12.5px; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.hidden { display: none !important; }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: 240px 1fr; min-height: 100vh; }
.sidebar { background: var(--sidebar); color: var(--sidebar-text); display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; }
.brand { padding: 20px 20px 16px; display: flex; align-items: center; gap: 10px; color: #fff; font-weight: 700; font-size: 16px; letter-spacing: .2px; }
.brand .logo { width: 32px; height: 32px; border-radius: 8px; background: linear-gradient(135deg, #3b82f6, #14b8a6); display: grid; place-items: center; font-size: 15px; }
.brand small { display: block; font-weight: 500; font-size: 11px; color: #8fa0bb; }
.nav { flex: 1; overflow-y: auto; padding: 8px 10px; }
.nav-section { font-size: 11px; text-transform: uppercase; letter-spacing: .8px; color: #6f819e; margin: 16px 10px 6px; }
.nav a { display: flex; align-items: center; gap: 10px; color: var(--sidebar-text); padding: 9px 12px; border-radius: 8px; font-weight: 500; }
.nav a:hover { background: rgba(255, 255, 255, .06); text-decoration: none; color: #fff; }
.nav a.active { background: rgba(59, 130, 246, .18); color: #fff; }
.nav svg { width: 18px; height: 18px; flex: none; opacity: .9; }
.sidebar-footer { padding: 14px 18px; border-top: 1px solid rgba(255, 255, 255, .08); font-size: 12px; }
.sidebar-footer .user { color: #fff; font-weight: 600; }
.sidebar-footer a { color: #8fb3ff; cursor: pointer; }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 18px 28px; }
.content { padding: 0 28px 40px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.menu-toggle { display: none; }

/* ---------- cards ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.card + .card { margin-top: 16px; }
.card.flush { padding: 0; overflow: hidden; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-5 { grid-template-columns: repeat(5, minmax(0, 1fr)); }
.kpi .label { color: var(--muted); font-size: 12.5px; font-weight: 500; }
.kpi .value { font-size: 22px; font-weight: 700; margin-top: 6px; font-variant-numeric: tabular-nums; }
.kpi .hint { font-size: 12px; color: var(--muted); margin-top: 4px; }

/* ---------- alerts ---------- */
.alert { border-radius: 8px; padding: 11px 14px; margin-bottom: 14px; border: 1px solid; font-size: 13.5px; }
.alert-warning { background: var(--warning-50); border-color: #fedf89; color: #7a2e0e; }
.alert-danger { background: var(--danger-50); border-color: #fecdca; color: #7a271a; }
.alert-info { background: var(--primary-50); border-color: #c7d7fe; color: #1e3a8a; }
.alert-success { background: var(--success-50); border-color: #abefc6; color: #05603a; }

/* ---------- buttons ---------- */
.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--text); padding: 8px 14px; border-radius: 8px; font: inherit; font-weight: 550; cursor: pointer; white-space: nowrap; line-height: 1.2; }
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-600); }
.btn-danger { color: var(--danger); border-color: #fecdca; }
.btn-danger:hover { background: var(--danger-50); }
.btn-sm { padding: 5px 10px; font-size: 12.5px; }
.btn-icon { padding: 5px 8px; }
.btn-link { border: none; background: none; color: var(--primary); padding: 0; cursor: pointer; font: inherit; }
.dropdown { position: relative; display: inline-block; }
.dropdown-menu { position: absolute; right: 0; top: calc(100% + 4px); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 10px 30px rgba(16, 24, 40, .15); min-width: 220px; z-index: 30; padding: 6px; display: none; }
.dropdown.open .dropdown-menu { display: block; }
.dropdown-menu a { display: block; padding: 8px 10px; border-radius: 6px; color: var(--text); }
.dropdown-menu a:hover { background: var(--surface-2); text-decoration: none; }

/* ---------- forms ---------- */
.form-grid { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 14px; }
.field { display: flex; flex-direction: column; gap: 5px; grid-column: span 6; min-width: 0; }
.field.s2 { grid-column: span 2; } .field.s3 { grid-column: span 3; } .field.s4 { grid-column: span 4; }
.field.s8 { grid-column: span 8; } .field.s12 { grid-column: span 12; }
label { font-size: 12.5px; font-weight: 600; color: #344054; }
input, select, textarea { font: inherit; color: var(--text); background: #fff; border: 1px solid #d0d5dd; border-radius: 8px; padding: 8px 10px; width: 100%; min-width: 0; }
input:focus, select:focus, textarea:focus { outline: 2px solid #bfd3fe; border-color: var(--primary); }
input[readonly] { background: var(--surface-2); }
textarea { min-height: 70px; resize: vertical; }
.field .error { color: var(--danger); font-size: 12px; }
.field.has-error input, .field.has-error select { border-color: var(--danger); }
.checkbox { display: flex; align-items: center; gap: 8px; font-weight: 500; }
.checkbox input { width: auto; }
.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; margin-bottom: 14px; }
.filters .field { grid-column: auto; min-width: 140px; }
.filters .grow { flex: 1; min-width: 200px; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; }
table.table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.table th { text-align: left; font-size: 12px; font-weight: 600; color: var(--muted); background: var(--surface-2); padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table td { padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tr:last-child td { border-bottom: none; }
.table tbody tr.clickable { cursor: pointer; }
.table tbody tr.clickable:hover { background: #f9fbff; }
.table tfoot td { font-weight: 650; background: var(--surface-2); }
.table .num { text-align: right; white-space: nowrap; }
.lines-table td { padding: 6px; }
.lines-table input, .lines-table select { padding: 6px 8px; }
.empty { padding: 36px; text-align: center; color: var(--muted); }
.pagination { display: flex; justify-content: space-between; align-items: center; padding: 12px 14px; border-top: 1px solid var(--border); font-size: 13px; color: var(--muted); }

/* ---------- badges ---------- */
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11.5px; font-weight: 600; border: 1px solid transparent; white-space: nowrap; }
.badge-success { background: var(--success-50); color: var(--success); border-color: #abefc6; }
.badge-danger { background: var(--danger-50); color: var(--danger); border-color: #fecdca; }
.badge-warning { background: var(--warning-50); color: var(--warning); border-color: #fedf89; }
.badge-info { background: var(--primary-50); color: var(--primary); border-color: #c7d7fe; }
.badge-neutral { background: #f2f4f7; color: #475467; border-color: #e4e7ec; }
.doc-type { font-weight: 700; font-size: 11px; padding: 2px 6px; border-radius: 5px; background: #eef2f6; color: #364152; margin-right: 6px; }

/* ---------- document view ---------- */
.doc-header { display: flex; justify-content: space-between; gap: 20px; flex-wrap: wrap; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; font-size: 13.5px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-weight: 500; }
.totals { margin-left: auto; min-width: 280px; }
.totals .row { display: flex; justify-content: space-between; padding: 5px 0; font-variant-numeric: tabular-nums; }
.totals .row.grand { border-top: 2px solid var(--text); margin-top: 6px; padding-top: 9px; font-size: 17px; font-weight: 700; }
.qr-box { display: flex; gap: 14px; align-items: center; }
.qr-box svg { width: 110px; height: 110px; }
.hash-line { font-size: 12px; color: var(--muted); }

/* ---------- picker ---------- */
.picker { position: relative; }
.picker-list { position: absolute; z-index: 40; left: 0; right: 0; top: calc(100% + 2px); background: #fff; border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 10px 30px rgba(16, 24, 40, .15); max-height: 260px; overflow-y: auto; min-width: 280px; }
.picker-list div { padding: 8px 10px; cursor: pointer; border-bottom: 1px solid #f2f4f7; }
.picker-list div:hover, .picker-list div.active { background: var(--primary-50); }
.picker-list small { color: var(--muted); display: block; }

/* ---------- modal / toast ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(15, 23, 42, .45); display: grid; place-items: center; z-index: 100; padding: 16px; }
.modal { background: #fff; border-radius: 12px; width: min(720px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column; box-shadow: 0 25px 50px rgba(0, 0, 0, .25); }
.modal.sm { width: min(440px, 100%); }
.modal-head { padding: 16px 20px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
.modal-head h2 { margin: 0; }
.modal-body { padding: 20px; overflow-y: auto; }
.modal-foot { padding: 14px 20px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }
.close-x { border: none; background: none; font-size: 22px; cursor: pointer; color: var(--muted); line-height: 1; }
.toasts { position: fixed; right: 18px; bottom: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 200; }
.toast { background: #101828; color: #fff; padding: 11px 16px; border-radius: 8px; box-shadow: 0 10px 25px rgba(0, 0, 0, .2); max-width: 420px; font-size: 13.5px; animation: slide .2s ease-out; }
.toast.error { background: #b42318; }
.toast.success { background: #067647; }
@keyframes slide { from { transform: translateY(10px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; background: radial-gradient(circle at 20% 20%, #1e3a8a 0, #0f1b2d 55%); padding: 16px; }
.login-card { width: min(400px, 100%); background: #fff; border-radius: 14px; padding: 32px; box-shadow: 0 25px 50px rgba(0, 0, 0, .3); }
.login-card .brand { color: var(--text); padding: 0 0 20px; }
.login-card .brand small { color: var(--muted); }
.login-card form { display: grid; gap: 14px; }

/* ---------- chart ---------- */
.chart svg { width: 100%; height: 220px; display: block; }
.chart .bar { fill: #3b82f6; }
.chart .bar:hover { fill: #1d4ed8; }
.chart text { font-size: 11px; fill: var(--muted); }

.spinner { width: 18px; height: 18px; border: 2px solid #c7d7fe; border-top-color: var(--primary); border-radius: 50%; animation: spin .7s linear infinite; display: inline-block; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading { padding: 40px; text-align: center; color: var(--muted); }

@media (max-width: 1100px) {
  .grid-4, .grid-5 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-3 { grid-template-columns: 1fr; }
}
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; left: -260px; width: 240px; z-index: 50; transition: left .2s; }
  body.nav-open .sidebar { left: 0; }
  .menu-toggle { display: inline-flex; }
  .topbar, .content { padding-left: 16px; padding-right: 16px; }
  .grid-2 { grid-template-columns: 1fr; }
  .field, .field.s2, .field.s3, .field.s4, .field.s8 { grid-column: span 12; }
}
.btn-danger-solid { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger-solid:hover { background: #912018; }
