:root {
  --bg: #f4f5fb;
  --surface: #ffffff;
  --surface-2: #f8f9fc;
  --border: #e4e7ef;
  --text: #1a1d29;
  --muted: #6b7280;
  --primary: #4f46e5;
  --primary-soft: #eef0fe;
  --personal: #4f46e5;
  --personal-soft: #eef0fe;
  --business: #0d9488;
  --business-soft: #e2f6f3;
  --danger: #dc2626;
  --danger-soft: #fdecec;
  --success: #16a34a;
  --warning: #d97706;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --shadow-lg: 0 10px 30px rgba(16, 24, 40, .10);
  --radius: 14px;
  --radius-sm: 9px;
  --sidebar-w: 232px;
  font-synthesis: none;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1017;
    --surface: #171a23;
    --surface-2: #1e222d;
    --border: #2a2f3c;
    --text: #e8eaf0;
    --muted: #9aa1b0;
    --primary: #7c78ff;
    --primary-soft: #23223a;
    --personal: #8b87ff;
    --personal-soft: #22223c;
    --business: #2dd4bf;
    --business-soft: #14312e;
    --danger: #f87171;
    --danger-soft: #341c1c;
    --success: #4ade80;
    --shadow: 0 1px 2px rgba(0,0,0,.3);
    --shadow-lg: 0 12px 34px rgba(0,0,0,.45);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--primary); text-decoration: none; }
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
button { font-family: inherit; cursor: pointer; }
input, select, button { font-size: 14px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 7px; justify-content: center;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 8px 14px; border-radius: var(--radius-sm); font-weight: 550;
  transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { filter: brightness(1.07); background: var(--primary); }
.btn-danger { background: var(--danger-soft); border-color: transparent; color: var(--danger); }
.btn-danger:hover { filter: brightness(.98); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-sm { padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ---------- Login / setup ---------- */
.auth-wrap {
  min-height: 100vh; display: grid; place-items: center; padding: 24px;
  background: radial-gradient(1200px 600px at 20% -10%, var(--primary-soft), transparent),
              radial-gradient(900px 500px at 110% 10%, var(--business-soft), transparent), var(--bg);
}
.auth-card {
  width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-lg); padding: 30px;
}
.auth-logo {
  width: 52px; height: 52px; border-radius: 14px; background: var(--primary); color: #fff;
  display: grid; place-items: center; font-weight: 800; font-size: 19px; margin-bottom: 18px;
}
.auth-card h1 { font-size: 21px; margin-bottom: 4px; }
.auth-card p.sub { color: var(--muted); margin: 0 0 22px; }
.field { margin-bottom: 14px; }
.field label { display: block; font-weight: 550; margin-bottom: 6px; font-size: 13px; }
.field input, .field select {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface-2); color: var(--text); outline: none;
}
.field input:focus, .field select:focus { border-color: var(--primary); background: var(--surface); }
.auth-card .btn-primary { width: 100%; padding: 11px; margin-top: 6px; }
.form-error { color: var(--danger); font-size: 13px; margin: 8px 0 0; min-height: 18px; }

/* ---------- App shell ---------- */
.shell { display: grid; grid-template-columns: var(--sidebar-w) 1fr; min-height: 100vh; }
.sidebar {
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 18px 14px; position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 20px; }
.brand .mark { width: 34px; height: 34px; border-radius: 10px; background: var(--primary); color: #fff; display: grid; place-items: center; font-weight: 800; font-size: 13px; }
.brand .name { font-weight: 700; font-size: 16px; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a {
  display: flex; align-items: center; gap: 11px; padding: 9px 11px; border-radius: 9px;
  color: var(--muted); font-weight: 550;
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--primary-soft); color: var(--primary); }
.nav a svg { width: 18px; height: 18px; flex: none; }
.sidebar .spacer { flex: 1; }

/* --- Icon sizing: inline SVGs are authored with a viewBox but no width/height,
   so without an explicit size they render at 0x0 (invisible). Size them here. --- */
.ic { display: inline-flex; align-items: center; justify-content: center; line-height: 0; flex: none; }
.ic svg { width: 1.15em; height: 1.15em; display: block; }
.month-nav button svg { width: 18px; height: 18px; display: block; }
.row-del svg { width: 16px; height: 16px; display: block; }
.account-box { border-top: 1px solid var(--border); padding-top: 12px; }
.account-box .who { display: flex; align-items: center; gap: 10px; padding: 6px 8px; }
.avatar { width: 32px; height: 32px; border-radius: 50%; background: var(--business-soft); color: var(--business); display: grid; place-items: center; font-weight: 700; }
.account-box .who .u { font-weight: 600; font-size: 13px; line-height: 1.2; }
.account-box .who .r { font-size: 11px; color: var(--muted); text-transform: capitalize; }

.main { padding: 26px 30px 60px; max-width: 1180px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 22px; flex-wrap: wrap; }
.page-head h1 { font-size: 23px; }
.page-head .sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* ---------- Cards / KPI ---------- */
.kpi-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 20px; }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; box-shadow: var(--shadow); position: relative; overflow: hidden;
}
.kpi .label { color: var(--muted); font-size: 12.5px; font-weight: 550; display: flex; align-items: center; gap: 7px; }
.kpi .value { font-size: 25px; font-weight: 720; margin-top: 8px; letter-spacing: -.02em; }
.kpi .foot { font-size: 12px; color: var(--muted); margin-top: 4px; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; vertical-align: middle; margin-right: 2px; }
.dot-personal { background: var(--personal); }
.dot-business { background: var(--business); }
.dot-total { background: var(--warning); }
.dot-out { background: var(--danger); }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px 20px; margin-bottom: 18px;
}
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; gap: 12px; flex-wrap: wrap; }
.card-head h2 { font-size: 15.5px; }
.card-head .legend { display: flex; gap: 14px; font-size: 12.5px; color: var(--muted); }
.card-head .legend span { display: inline-flex; align-items: center; gap: 6px; }

/* ---------- Chart ---------- */
.chart-wrap { width: 100%; overflow-x: auto; }
svg.chart { display: block; width: 100%; height: auto; }
.chart .grid-line { stroke: var(--border); stroke-width: 1; }
.chart .axis-label { fill: var(--muted); font-size: 11px; }
.chart .bar-personal { fill: var(--personal); }
.chart .bar-business { fill: var(--business); }
.chart .bar:hover { opacity: .82; }

/* ---------- Tables ---------- */
.table-scroll { overflow-x: auto; }
table.grid { width: 100%; border-collapse: collapse; }
table.grid th {
  text-align: left; font-size: 11.5px; text-transform: uppercase; letter-spacing: .03em;
  color: var(--muted); font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap;
}
table.grid td { padding: 4px 6px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.grid tr:last-child td { border-bottom: none; }
table.grid tfoot td { font-weight: 700; border-top: 2px solid var(--border); border-bottom: none; padding-top: 10px; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.cell-input {
  width: 100%; border: 1px solid transparent; background: transparent; color: var(--text);
  padding: 7px 8px; border-radius: 7px; outline: none;
}
.cell-input:hover { background: var(--surface-2); }
.cell-input:focus { border-color: var(--primary); background: var(--surface); }
.cell-input.num { text-align: right; }
.cell-input.amount { font-weight: 600; }
input[type="date"].cell-input { min-width: 130px; }
.pay-toggle { display: inline-flex; }
.chip {
  border: none; padding: 4px 11px; border-radius: 20px; font-size: 12px; font-weight: 600; cursor: pointer;
}
.chip.paid { background: var(--business-soft); color: var(--business); }
.chip.unpaid { background: var(--danger-soft); color: var(--danger); }
.row-del { color: var(--muted); border: none; background: transparent; padding: 6px; border-radius: 7px; line-height: 0; }
.row-del:hover { background: var(--danger-soft); color: var(--danger); }

/* multi-select */
.sel-cell { width: 34px; text-align: center; padding: 0 6px !important; }
.row-select, .select-all { width: 16px; height: 16px; cursor: pointer; accent-color: var(--primary); margin: 0; vertical-align: middle; }
tbody tr:has(.row-select:checked) { background: var(--primary-soft); }
.panel-actions { display: inline-flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

.panels { display: grid; grid-template-columns: 1fr; gap: 18px; align-items: start; }
.name-input { min-width: 290px; font-weight: 550; }
table.grid td:nth-child(2), table.grid th:nth-child(2) { width: 38%; }
.panel-title { display: flex; align-items: center; gap: 9px; }
.panel-title .tag { width: 10px; height: 10px; border-radius: 3px; }
.tag-personal { background: var(--personal); }
.tag-business { background: var(--business); }
.panel-total { font-weight: 700; font-size: 15px; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }

.month-nav { display: inline-flex; align-items: center; gap: 4px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 3px; }
.month-nav button { border: none; background: transparent; color: var(--muted); padding: 6px 9px; border-radius: 7px; line-height: 0; }
.month-nav button:hover { background: var(--surface-2); color: var(--text); }
.month-nav select { border: none; background: transparent; color: var(--text); font-weight: 650; padding: 5px; outline: none; cursor: pointer; }

.empty-state { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty-state .big { font-size: 34px; margin-bottom: 8px; }

.badge-out { display: inline-block; background: var(--danger-soft); color: var(--danger); padding: 2px 8px; border-radius: 20px; font-size: 12px; font-weight: 600; }
.badge-ok { display: inline-block; background: var(--business-soft); color: var(--business); padding: 2px 8px; border-radius: 20px; font-size: 12px; font-weight: 600; }

/* ---------- Modal ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(10, 12, 20, .5); display: grid; place-items: center; z-index: 50; padding: 20px; }
.modal { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-lg); width: 100%; max-width: 440px; padding: 22px; max-height: 90vh; overflow-y: auto; }
.modal h3 { font-size: 17px; margin-bottom: 6px; }
.modal .sub { color: var(--muted); margin: 0 0 16px; font-size: 13px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 20px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 10px;
  box-shadow: var(--shadow-lg); z-index: 100; font-weight: 550; font-size: 13.5px;
  animation: toastin .2s ease;
}
.toast.err { background: var(--danger); color: #fff; }
@keyframes toastin { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* ---------- Users ---------- */
.role-pill { padding: 2px 9px; border-radius: 20px; font-size: 12px; font-weight: 600; }
.role-admin { background: var(--primary-soft); color: var(--primary); }
.role-user { background: var(--surface-2); color: var(--muted); }

/* ---------- Responsive ---------- */
.mobile-topbar { display: none; }
@media (max-width: 920px) {
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .panels { grid-template-columns: 1fr; }
}
@media (max-width: 720px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; z-index: 40; left: 0; top: 0; bottom: 0; width: 250px; height: 100vh;
    transform: translateX(-105%); transition: transform .22s ease; box-shadow: var(--shadow-lg);
  }
  .sidebar.open { transform: translateX(0); }
  .mobile-topbar {
    display: flex; align-items: center; gap: 12px; padding: 12px 16px; background: var(--surface);
    border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 30;
  }
  .mobile-topbar .brand { padding: 0; }
  .mobile-topbar > button { width: 44px; height: 40px; padding: 0; flex: none; }
  .mobile-topbar > button svg { width: 24px; height: 24px; display: block; }
  .scrim { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 39; }
  .main { padding: 18px 16px 60px; }
  .page-head h1 { font-size: 20px; }
}
