:root {
  --bg: #f3f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #1e3a5f;
  --primary-2: #2563eb;
  --primary-soft: #e0ebff;
  --success: #16a34a;
  --success-soft: #dcfce7;
  --warning: #d97706;
  --warning-soft: #fef3c7;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --info: #0891b2;
  --info-soft: #cffafe;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 4px 12px rgba(15, 23, 42, .05);
  --sidebar-w: 248px;
  --topbar-h: 56px;
  --bottom-h: 62px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --surface: #111a2c;
    --surface-2: #0f1728;
    --border: #1f2b42;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --primary: #3b82f6;
    --primary-2: #60a5fa;
    --primary-soft: #1e2d4d;
    --success-soft: #0f2e1d;
    --warning-soft: #33250b;
    --danger-soft: #3a1414;
    --info-soft: #0b2c33;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--primary-2); text-decoration: none; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h2 { font-size: 1.1rem; }
h3 { font-size: 1rem; }
small, .muted { color: var(--muted); }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.hidden { display: none !important; }

/* ---------- Layout ---------- */
.app { min-height: 100vh; }
.sidebar {
  position: fixed; inset: 0 auto 0 0; width: var(--sidebar-w);
  background: var(--primary); color: #fff;
  display: flex; flex-direction: column;
  transform: translateX(-100%); transition: transform .22s ease;
  z-index: 60; overflow-y: auto;
}
@media (prefers-color-scheme: dark) { .sidebar { background: #0d1628; border-right: 1px solid var(--border); } }
.sidebar.open { transform: translateX(0); }
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 55; display: none; }
.backdrop.show { display: block; }
.brand { display: flex; gap: 10px; align-items: center; padding: 18px 16px; border-bottom: 1px solid rgba(255,255,255,.12); }
.brand-logo { font-size: 26px; }
.brand strong { display: block; font-size: 15px; }
.brand small { color: rgba(255,255,255,.65); font-size: 12px; }
.nav { padding: 8px; display: flex; flex-direction: column; gap: 2px; }
.nav a { color: rgba(255,255,255,.85); padding: 10px 12px; border-radius: 8px; display: flex; gap: 10px; align-items: center; }
.nav a span { width: 22px; text-align: center; }
.nav a:hover { background: rgba(255,255,255,.08); }
.nav a.active { background: rgba(255,255,255,.16); color: #fff; font-weight: 600; }
.nav-title { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: rgba(255,255,255,.5); padding: 14px 12px 4px; }

.main { min-height: 100vh; padding-bottom: calc(var(--bottom-h) + env(safe-area-inset-bottom)); }
.topbar {
  position: sticky; top: 0; z-index: 40; height: var(--topbar-h);
  display: flex; align-items: center; gap: 10px; padding: 0 14px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 1.1rem; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { background: none; border: 0; font-size: 22px; color: var(--text); cursor: pointer; padding: 6px; border-radius: 8px; line-height: 1; }
.icon-btn:hover { background: var(--surface-2); }
.view { padding: 14px; max-width: 1400px; margin: 0 auto; }

.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 45;
  height: calc(var(--bottom-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--surface); border-top: 1px solid var(--border);
}
.bottom-nav a { display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: 11px; color: var(--muted); gap: 2px; }
.bottom-nav a span { font-size: 19px; }
.bottom-nav a.active { color: var(--primary-2); font-weight: 600; }
.bottom-nav .bn-main span {
  background: var(--primary-2); color: #fff; width: 42px; height: 42px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center; font-size: 24px; margin-top: -18px;
  box-shadow: 0 4px 12px rgba(37,99,235,.4);
}

@media (min-width: 960px) {
  .sidebar { transform: none; }
  .backdrop, .menu-btn, .bottom-nav { display: none !important; }
  .main { margin-left: var(--sidebar-w); padding-bottom: 0; }
  .topbar { padding: 0 24px; }
  .view { padding: 22px 24px; }
}
@media (max-width: 959px) { .hide-mobile { display: none !important; } }

/* ---------- Components ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-body { padding: 16px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 14px 16px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.stack { display: flex; flex-direction: column; gap: 14px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.grid { display: grid; gap: 14px; }
.grid-2 { grid-template-columns: 1fr; }
.grid-kpi { grid-template-columns: repeat(2, 1fr); }
@media (min-width: 700px) { .grid-kpi { grid-template-columns: repeat(3, 1fr); } }
@media (min-width: 1100px) { .grid-2 { grid-template-columns: 1fr 1fr; } .grid-kpi { grid-template-columns: repeat(6, 1fr); } }

.kpi { padding: 14px; }
.kpi .kpi-label { font-size: 12px; color: var(--muted); }
.kpi .kpi-value { font-size: 1.25rem; font-weight: 700; margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi .kpi-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.kpi.danger .kpi-value { color: var(--danger); }
.kpi.success .kpi-value { color: var(--success); }
.kpi.warning .kpi-value { color: var(--warning); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 14px; border-radius: 9px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 500;
  cursor: pointer; white-space: nowrap; min-height: 40px;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--primary-2); border-color: var(--primary-2); color: #fff; }
.btn-primary:hover { background: #1d4ed8; }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { background: #b91c1c; }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-sm { padding: 6px 10px; min-height: 32px; font-size: 13px; }
.btn-icon { padding: 6px 9px; min-height: 32px; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); white-space: nowrap; }
.badge-success { background: var(--success-soft); color: var(--success); border-color: transparent; }
.badge-warning { background: var(--warning-soft); color: var(--warning); border-color: transparent; }
.badge-danger { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.badge-info { background: var(--info-soft); color: var(--info); border-color: transparent; }
.badge-primary { background: var(--primary-soft); color: var(--primary-2); border-color: transparent; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; vertical-align: middle; }

.text-success { color: var(--success); }
.text-danger { color: var(--danger); }
.text-warning { color: var(--warning); }
.fw { font-weight: 600; }

/* ---------- Toolbar & Forms ---------- */
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.toolbar .grow { flex: 1 1 200px; }
input, select, textarea {
  width: 100%; font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 9px;
  padding: 9px 11px; min-height: 40px;
}
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary-2); }
.toolbar input, .toolbar select { width: auto; }
.toolbar .grow input { width: 100%; }
.form-grid { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 640px) { .form-grid { grid-template-columns: 1fr 1fr; } .form-grid .full { grid-column: 1 / -1; } }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 4px; }
.field .req { color: var(--danger); }
.field .hint { font-size: 12px; color: var(--muted); margin-top: 3px; }
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 3px; gap: 3px; flex-wrap: wrap; }
.seg label { cursor: pointer; }
.seg input { display: none; }
.seg span { display: block; padding: 6px 12px; border-radius: 8px; font-size: 14px; }
.seg input:checked + span { background: var(--primary-2); color: #fff; font-weight: 600; }

/* ---------- Tables (cards on mobile) ---------- */
.table-wrap { overflow-x: auto; }
table.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 10px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
.table th.right, .table td.right { text-align: right; }
.table th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); background: var(--surface-2); font-weight: 600; white-space: nowrap; }
.table tbody tr:hover { background: var(--surface-2); }
.table tr.clickable { cursor: pointer; }
.table tr.row-cancel td { opacity: .5; text-decoration: line-through; }
.table tr.row-cancel td.actions, .table tr.row-cancel td .badge { text-decoration: none; }
.table td.actions { text-align: right; white-space: nowrap; }
.table tfoot td { font-weight: 700; background: var(--surface-2); }

@media (max-width: 700px) {
  .table.responsive thead { display: none; }
  .table.responsive, .table.responsive tbody, .table.responsive tr, .table.responsive td { display: block; width: 100%; }
  .table.responsive tr { border-bottom: 1px solid var(--border); padding: 8px 4px; }
  .table.responsive td { border: 0; padding: 4px 10px; text-align: right; overflow-wrap: anywhere; }
  .table.responsive td::after { content: ""; display: table; clear: both; }
  .table.responsive td::before { content: attr(data-label); float: left; margin-right: 12px; font-weight: 600; color: var(--muted); font-size: 13px; text-align: left; }
  .table.responsive td[data-label=""]::before, .table.responsive td:not([data-label])::before { content: none; }
  .table.responsive tfoot tr { display: block; }

  .toolbar { display: grid; grid-template-columns: 1fr 1fr; }
  .toolbar > * { width: 100% !important; min-width: 0; }
  .toolbar .grow, .toolbar .btn-primary { grid-column: 1 / -1; }
  .toolbar .grow:empty { display: none; }
}
.view, .main { min-width: 0; }
.modal-body, .card { min-width: 0; }
.empty { padding: 36px 16px; text-align: center; color: var(--muted); }
.empty .empty-icon { font-size: 34px; display: block; margin-bottom: 6px; }

/* ---------- Detail ---------- */
.detail-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px 18px; }
@media (min-width: 800px) { .detail-grid { grid-template-columns: repeat(4, 1fr); } }
.detail-grid .dl-label { font-size: 12px; color: var(--muted); }
.detail-grid .dl-value { font-weight: 600; word-break: break-word; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.page-head .back { font-size: 14px; }

/* ---------- Bars ---------- */
.bars { display: flex; flex-direction: column; gap: 10px; }
.bar-row .bar-top { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 3px; gap: 8px; }
.bar-track { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; border: 1px solid var(--border); }
.bar-fill { height: 100%; background: var(--primary-2); border-radius: 99px; }

/* ---------- Modal ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(15,23,42,.5); z-index: 100; display: flex; align-items: flex-end; justify-content: center; animation: fade .15s; }
.modal {
  background: var(--surface); width: 100%; max-height: 100dvh; height: 100dvh;
  display: flex; flex-direction: column; animation: slide .2s ease;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--border); gap: 10px; }
.modal-head h2 { font-size: 1.05rem; }
.modal-body { padding: 16px; overflow-y: auto; flex: 1; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--border); padding-bottom: calc(12px + env(safe-area-inset-bottom)); flex-wrap: wrap; }
@media (min-width: 700px) {
  .modal-backdrop { align-items: center; padding: 20px; }
  .modal { height: auto; max-height: 92vh; border-radius: 14px; max-width: 640px; box-shadow: 0 20px 50px rgba(0,0,0,.25); }
  .modal.modal-lg { max-width: 980px; }
  .modal.modal-sm { max-width: 420px; }
}
@keyframes fade { from { opacity: 0; } }
@keyframes slide { from { transform: translateY(30px); opacity: 0; } }

/* ---------- Line items ---------- */
.lines { display: flex; flex-direction: column; gap: 8px; }
.line { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; padding: 10px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface-2); }
.line .l-part { grid-column: 1 / -1; }
.line .l-total { display: flex; align-items: center; justify-content: space-between; grid-column: 1 / -1; font-weight: 600; }
.line label { font-size: 12px; color: var(--muted); display: block; margin-bottom: 2px; }
@media (min-width: 700px) {
  .line { grid-template-columns: 3fr 1fr 1.3fr 1.3fr auto; align-items: end; }
  .line .l-part { grid-column: auto; }
  .line .l-total { grid-column: auto; justify-content: flex-end; gap: 8px; min-height: 40px; }
}
.totals { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; margin-top: 12px; }
.totals .t-row { display: flex; gap: 20px; min-width: 240px; justify-content: space-between; }
.totals .t-grand { font-size: 1.2rem; font-weight: 700; }
.alert { padding: 10px 12px; border-radius: 9px; font-size: 14px; }
.alert-warning { background: var(--warning-soft); color: var(--warning); }
.alert-info { background: var(--info-soft); color: var(--info); }
.alert-danger { background: var(--danger-soft); color: var(--danger); }

/* ---------- Toast ---------- */
.toast-root { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--bottom-h) + 14px); z-index: 200; display: flex; flex-direction: column; gap: 8px; width: min(92vw, 380px); }
@media (min-width: 960px) { .toast-root { bottom: 20px; left: auto; right: 20px; transform: none; } }
.toast { background: #0f172a; color: #fff; padding: 11px 14px; border-radius: 10px; box-shadow: 0 8px 20px rgba(0,0,0,.25); font-size: 14px; animation: slide .2s; }
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

/* ---------- Quick actions ---------- */
.quick { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
@media (min-width: 700px) { .quick { grid-template-columns: repeat(4, 1fr); } }
.quick a { display: flex; align-items: center; gap: 10px; padding: 14px; color: var(--text); font-weight: 600; }
.quick a span { font-size: 22px; }
.quick a:hover { border-color: var(--primary-2); }

.list-simple { list-style: none; margin: 0; padding: 0; }
.list-simple li { display: flex; justify-content: space-between; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--border); align-items: center; }
.list-simple li:last-child { border-bottom: 0; }
.list-simple .ls-main { min-width: 0; }
.list-simple .ls-main div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

@media print {
  .sidebar, .topbar, .bottom-nav, .toolbar, .no-print, .btn { display: none !important; }
  .main { margin: 0 !important; }
  .card { box-shadow: none; break-inside: avoid; }
  body { background: #fff; color: #000; }
}
