:root {
  --primary: #1F3A5F;
  --primary-600: #2A4D7A;
  --primary-50: #EEF3F9;
  --accent: #F59E0B;
  --accent-50: #FEF6E7;
  --bg: #F5F7FA;
  --surface: #FFFFFF;
  --surface-2: #F9FAFB;
  --border: #E5E8EE;
  --text: #17202C;
  --muted: #6B7686;
  --success: #12805C;
  --success-50: #E7F6EF;
  --warn: #B45309;
  --warn-50: #FEF3E2;
  --danger: #C2362F;
  --danger-50: #FDECEB;
  --info: #2563A8;
  --info-50: #EAF2FB;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 1px 3px rgba(16, 24, 40, .06);
  --shadow-lg: 0 12px 32px rgba(16, 24, 40, .18);
  --sidebar-w: 236px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
svg { display: block; }
button, input, select, textarea { font: inherit; color: inherit; }

.app { display: flex; min-height: 100vh; }

/* Sidebar */
.sidebar {
  width: var(--sidebar-w);
  background: var(--primary);
  color: #DCE5F0;
  display: flex;
  flex-direction: column;
  position: fixed;
  inset: 0 auto 0 0;
  z-index: 30;
  transition: transform .2s ease;
}
.brand { display: flex; align-items: center; gap: 12px; padding: 20px 18px 18px; }
.brand-mark {
  width: 36px; height: 36px; border-radius: 9px;
  background: var(--accent); color: var(--primary);
  display: grid; place-items: center; font-weight: 700; font-size: 14px;
}
.brand-name { color: #fff; font-weight: 600; font-size: 15px; line-height: 1.2; }
.brand-sub { font-size: 11px; letter-spacing: .08em; text-transform: uppercase; opacity: .65; }
.nav { padding: 8px 10px; display: flex; flex-direction: column; gap: 2px; flex: 1; overflow-y: auto; }
.nav-label { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; opacity: .5; padding: 14px 10px 6px; }
.nav a {
  display: flex; align-items: center; gap: 11px;
  padding: 9px 10px; border-radius: 8px;
  color: inherit; text-decoration: none; font-weight: 500;
}
.nav a svg { width: 18px; height: 18px; opacity: .8; }
.nav a:hover { background: rgba(255,255,255,.07); color: #fff; }
.nav a.active { background: rgba(255,255,255,.12); color: #fff; box-shadow: inset 3px 0 0 var(--accent); }
.nav a.active svg { opacity: 1; color: var(--accent); }
.sidebar-foot { padding: 14px 16px 18px; border-top: 1px solid rgba(255,255,255,.08); }
.user-email { font-size: 12.5px; opacity: .75; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.signout { margin-top: 6px; padding: 0; border: 0; background: none; color: var(--accent); font-size: 12.5px; font-weight: 500; cursor: pointer; }
.signout:hover { text-decoration: underline; }

[hidden] { display: none !important; }
.loading { min-height: 100vh; display: grid; place-items: center; color: var(--muted); }
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: var(--bg); }
.login-card { width: 100%; max-width: 380px; padding: 32px 28px; display: flex; flex-direction: column; gap: 14px; }
.login-card .brand-mark { margin-bottom: 4px; }
.login-card h1 { margin: 0; font-size: 20px; }
.login-card p { margin: -8px 0 6px; }
.login-btn { width: 100%; justify-content: center; height: 40px; margin-top: 4px; }
.login-error { background: var(--danger-50); color: var(--danger); padding: 9px 12px; border-radius: 8px; font-size: 13px; }
.btn:disabled { opacity: .6; cursor: wait; }

.main { flex: 1; margin-left: var(--sidebar-w); min-width: 0; display: flex; flex-direction: column; }
.topbar {
  height: 60px; display: flex; align-items: center; gap: 12px;
  padding: 0 28px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.page-title { font-size: 17px; font-weight: 600; margin: 0; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 14px; }
.today { color: var(--muted); font-size: 13px; }
.avatar {
  width: 32px; height: 32px; border-radius: 50%;
  background: var(--primary-50); color: var(--primary);
  display: grid; place-items: center; font-weight: 600; font-size: 13px;
}
.topbar .menu-btn { display: none; }
.content { padding: 24px 28px 48px; max-width: 1320px; width: 100%; }

/* Generic */
.icon-btn {
  width: 34px; height: 34px; border-radius: 8px; border: 0; background: transparent;
  display: grid; place-items: center; cursor: pointer; color: var(--muted);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn svg { width: 18px; height: 18px; }

.btn {
  display: inline-flex; align-items: center; gap: 7px;
  height: 36px; padding: 0 14px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface);
  font-weight: 500; cursor: pointer; white-space: nowrap;
}
.btn svg { width: 16px; height: 16px; }
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-600); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-50); }
.btn-sm { height: 30px; padding: 0 10px; font-size: 13px; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 18px 6px; }
.card-head h3 { margin: 0; font-size: 14.5px; font-weight: 600; }
.card-head a { color: var(--primary-600); font-size: 13px; text-decoration: none; font-weight: 500; }
.card-body { padding: 8px 18px 18px; }

/* KPI */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 20px; }
.kpi { padding: 18px; }
.kpi-top { display: flex; align-items: center; justify-content: space-between; }
.kpi-label { color: var(--muted); font-size: 13px; font-weight: 500; }
.kpi-icon { width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center; }
.kpi-icon svg { width: 18px; height: 18px; }
.kpi-value { font-size: 24px; font-weight: 700; margin-top: 10px; letter-spacing: -.01em; font-variant-numeric: tabular-nums; }
.kpi-sub { color: var(--muted); font-size: 12.5px; margin-top: 4px; }

.grid-2 { display: grid; grid-template-columns: 1.6fr 1fr; gap: 16px; margin-bottom: 16px; }

/* Chart */
.chart { width: 100%; height: 240px; }
.chart text { fill: var(--muted); font-size: 11px; }
.chart .gridline { stroke: var(--border); }
.chart .bar { fill: var(--primary); }
.chart .bar.current { fill: var(--accent); }

/* Toolbar */
.toolbar { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; }
.search {
  display: flex; align-items: center; gap: 8px; height: 36px; padding: 0 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; min-width: 260px;
}
.search svg { width: 16px; height: 16px; color: var(--muted); flex: none; }
.search input { border: 0; outline: 0; background: transparent; width: 100%; }
.toolbar .spacer { flex: 1; }
.tabs { display: flex; gap: 4px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 3px; }
.tabs button { border: 0; background: transparent; padding: 5px 11px; border-radius: 6px; cursor: pointer; font-weight: 500; color: var(--muted); font-size: 13px; }
.tabs button.active { background: var(--primary-50); color: var(--primary); }

/* Table */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th {
  text-align: left; font-size: 12px; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: .04em;
  padding: 11px 16px; background: var(--surface-2); border-bottom: 1px solid var(--border); white-space: nowrap;
}
td { padding: 12px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover td { background: var(--surface-2); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.muted { color: var(--muted); }
.strong { font-weight: 600; }
.small { font-size: 12.5px; }
.mini-table td { padding: 10px 0; }
.mini-table td + td { padding-left: 12px; }
.empty { padding: 40px 16px; text-align: center; color: var(--muted); }

.badge {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 9px; border-radius: 999px; font-size: 12px; font-weight: 500; white-space: nowrap;
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.b-success { background: var(--success-50); color: var(--success); }
.b-warn { background: var(--warn-50); color: var(--warn); }
.b-danger { background: var(--danger-50); color: var(--danger); }
.b-info { background: var(--info-50); color: var(--info); }
.b-muted { background: #EEF0F3; color: var(--muted); }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15, 23, 36, .45);
  display: grid; place-items: center; z-index: 50; padding: 16px;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  background: var(--surface); border-radius: 12px; box-shadow: var(--shadow-lg);
  width: 100%; max-width: 560px; max-height: calc(100vh - 32px); display: flex; flex-direction: column;
}
.modal.wide { max-width: 860px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; font-size: 16px; font-weight: 600; }
.modal-body { padding: 20px; overflow-y: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); }
.modal-foot .left { margin-right: auto; }

/* Forms */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px 16px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field.full { grid-column: 1 / -1; }
.field label { font-size: 12.5px; font-weight: 500; color: var(--muted); }
.input, select.input, textarea.input {
  height: 38px; padding: 0 11px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); outline: none; width: 100%;
}
textarea.input { height: auto; padding: 9px 11px; resize: vertical; }
.input:focus { border-color: var(--primary-600); box-shadow: 0 0 0 3px var(--primary-50); }
.input.invalid { border-color: var(--danger); }

.lines th, .lines td { padding: 8px 6px; border-bottom: 1px solid var(--border); background: none; }
.lines th { text-transform: none; letter-spacing: 0; font-size: 12.5px; }
.lines .input { height: 34px; }
.lines td:last-child { width: 36px; }
.totals { margin-left: auto; width: 280px; margin-top: 14px; }
.totals div { display: flex; justify-content: space-between; padding: 5px 0; font-variant-numeric: tabular-nums; }
.totals .grand { border-top: 1px solid var(--border); margin-top: 6px; padding-top: 10px; font-weight: 700; font-size: 15px; }

/* Document view */
.doc-head { display: flex; justify-content: space-between; gap: 20px; margin-bottom: 18px; flex-wrap: wrap; }
.doc-head h3 { margin: 0 0 4px; font-size: 18px; }
.doc-meta { display: grid; grid-template-columns: auto auto; gap: 4px 16px; font-size: 13px; }
.doc-meta span:nth-child(odd) { color: var(--muted); }

.settings-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.section-note { color: var(--muted); font-size: 13px; margin: 0 0 14px; }

/* Toast */
.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: #fff; padding: 10px 16px; border-radius: 8px;
  box-shadow: var(--shadow-lg); z-index: 60; font-weight: 500; max-width: calc(100vw - 32px);
}
.toast.error { background: var(--danger); }

.scrim { display: none; }

@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .grid-2 { grid-template-columns: 1fr; }
}
@media (max-width: 820px) {
  .sidebar { transform: translateX(-100%); }
  .sidebar.open { transform: none; }
  .scrim.show { display: block; position: fixed; inset: 0; background: rgba(15,23,36,.4); z-index: 20; }
  .main { margin-left: 0; }
  .topbar .menu-btn { display: grid; }
  .topbar { padding: 0 16px; }
  .content { padding: 16px 16px 40px; }
  .today { display: none; }
  .search { min-width: 0; flex: 1 1 100%; }
  .form-grid, .settings-grid { grid-template-columns: 1fr; }
  .totals { width: 100%; }
  td { white-space: nowrap; }
}
@media (max-width: 520px) {
  .kpis { grid-template-columns: 1fr; }
}

@media print {
  .sidebar, .topbar, .modal-head, .modal-foot, .toast { display: none !important; }
  .main { margin: 0; }
  .modal-backdrop { position: static; background: none; padding: 0; }
  .modal { box-shadow: none; max-height: none; max-width: none; }
  body > .app { display: none; }
}
