:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #f0efe9;
  --border: #e2e0d8;
  --text: #1d1f1e;
  --muted: #6b6f6c;
  --accent: #2f6f5e;
  --accent-soft: #e3efe9;
  --income: #2f7d4f;
  --expense: #b4493a;
  --warn: #a8690b;
  --warn-soft: #fbf0dc;
  --danger-soft: #f8e4e0;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.04);
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121413; --surface: #1a1d1c; --surface-2: #222624; --border: #2e3330;
    --text: #e8eae8; --muted: #9aa19d; --accent: #5fb39a; --accent-soft: #1f3530;
    --income: #5fbf85; --expense: #e37b6a; --warn: #e0a54a; --warn-soft: #3a2f1c; --danger-soft: #3d2420;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121413; --surface: #1a1d1c; --surface-2: #222624; --border: #2e3330;
  --text: #e8eae8; --muted: #9aa19d; --accent: #5fb39a; --accent-soft: #1f3530;
  --income: #5fbf85; --expense: #e37b6a; --warn: #e0a54a; --warn-soft: #3a2f1c; --danger-soft: #3d2420;
  --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 15px; margin: 0 0 12px; }
.num { font-variant-numeric: tabular-nums; }

.shell { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--surface); border-right: 1px solid var(--border);
  padding: 20px 14px; display: flex; flex-direction: column; gap: 18px;
  position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; padding: 0 6px; }
.brand small { color: var(--muted); font-weight: 500; }
.logo {
  width: 28px; height: 28px; border-radius: 8px; background: var(--accent); color: #fff;
  display: grid; place-items: center; font-size: 14px;
}
.logo-img { width: 30px; height: 30px; object-fit: contain; }
.logo-dark-only { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .logo-light { display: none; }
  :root:not([data-theme="light"]) .logo-dark-only { display: inline-block; }
}
:root[data-theme="dark"] .logo-light { display: none; }
:root[data-theme="dark"] .logo-dark-only { display: inline-block; }
.login-brand { display: grid; justify-items: center; gap: 4px; margin-bottom: 18px; }
.login-brand img { width: min(240px, 80%); height: auto; }
.login-brand small { color: var(--muted); font-weight: 500; letter-spacing: .14em; text-transform: uppercase; font-size: 11px; }
nav { display: flex; flex-direction: column; gap: 2px; }
nav a { color: var(--muted); padding: 8px 10px; border-radius: 8px; font-weight: 500; }
nav a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
nav a.active { background: var(--accent-soft); color: var(--accent); }
.theme-toggle { margin-top: auto; }

.view { padding: 28px 32px 60px; max-width: 1200px; width: 100%; min-width: 0; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head .sub { color: var(--muted); margin-top: 2px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

button, .btn {
  font: inherit; font-weight: 500; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 7px 14px; border-radius: 8px; white-space: nowrap;
}
button:hover { background: var(--surface-2); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--expense); }
button.small { padding: 3px 10px; font-size: 13px; }
button.link { border: none; background: none; color: var(--accent); padding: 0; }

.grid { display: grid; gap: 16px; }
.kpis { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); margin-bottom: 16px; }
.two { grid-template-columns: 1fr 1fr; }
.two.wide-left { grid-template-columns: 2fr 1fr; align-items: start; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; box-shadow: var(--shadow); min-width: 0;
}
.kpi .label { color: var(--muted); font-size: 13px; }
.kpi .value { font-size: 24px; font-weight: 650; margin-top: 4px; letter-spacing: -.01em; }
.kpi .hint { color: var(--muted); font-size: 12px; margin-top: 2px; }
.pos { color: var(--income); }
.neg { color: var(--expense); }
.muted { color: var(--muted); }

.table-wrap { overflow-x: auto; margin: 0 -18px; padding: 0 18px; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { color: var(--muted); font-weight: 500; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; white-space: nowrap; }
td.r, th.r { text-align: right; }
tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-2); }
td .row-actions { display: flex; gap: 6px; justify-content: flex-end; }
.empty { color: var(--muted); padding: 22px 0; text-align: center; }

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 500;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.badge.ok { background: var(--accent-soft); color: var(--accent); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--danger-soft); color: var(--expense); }

.progress { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--accent); border-radius: 99px; }

.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; width: 100%;
}
.filters select, .filters input { width: auto; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
textarea { min-height: 70px; resize: vertical; }

/* Chart */
.chart svg { width: 100%; height: auto; display: block; }
.chart .legend { display: flex; gap: 16px; font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.chart .legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }

.list-item { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 9px 0; border-bottom: 1px solid var(--border); }
.list-item:last-child { border-bottom: none; }
.list-item .meta { color: var(--muted); font-size: 12px; }

.stat-row { display: flex; gap: 28px; flex-wrap: wrap; margin: 6px 0 14px; }
.stat-row div span { display: block; color: var(--muted); font-size: 12px; }
.stat-row div b { font-size: 18px; }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.4); display: grid; place-items: center;
  padding: 16px; z-index: 50;
}
.modal {
  background: var(--surface); border-radius: 14px; width: 100%; max-width: 520px;
  max-height: calc(100vh - 32px); overflow: auto; padding: 20px 22px; border: 1px solid var(--border);
}
.modal h2 { font-size: 17px; margin-bottom: 16px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 14px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field.full { grid-column: 1 / -1; }
.field label { font-size: 12px; color: var(--muted); font-weight: 500; }
.field.check { flex-direction: row; align-items: center; gap: 8px; }
.field.check input { width: auto; }
.field .help { font-size: 12px; color: var(--muted); }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 8px;
  opacity: 0; pointer-events: none; transition: .2s; z-index: 60;
}
.toast.show { opacity: 1; transform: translateX(-50%); }

button.danger-fill { background: var(--expense); border-color: var(--expense); color: #fff; }

.warn-box {
  background: var(--warn-soft); color: var(--warn); border-radius: 10px; padding: 10px 14px;
  margin-bottom: 14px; display: flex; gap: 12px; align-items: center; justify-content: space-between; flex-wrap: wrap;
}
.back { display: inline-block; margin-bottom: 10px; color: var(--muted); }
.title-cell b { display: block; font-weight: 600; }
.title-cell span { color: var(--muted); font-size: 12px; }

.bars .bar-row { display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; padding: 6px 0; }
.bars .bar-row .progress { grid-column: 1 / -1; height: 6px; }

.settings-section + .settings-section { margin-top: 16px; }
.plan-info { background: var(--accent-soft); color: var(--text); border-radius: 10px; padding: 10px 12px; font-size: 13px; line-height: 1.6; }

/* Safe areas: Capacitor's Android shell provides --safe-area-inset-*; browsers provide env(). */
:root {
  --sat: var(--safe-area-inset-top, env(safe-area-inset-top, 0px));
  --sab: var(--safe-area-inset-bottom, env(safe-area-inset-bottom, 0px));
}
.view { padding-top: calc(28px + var(--sat)); }
.auth { padding-top: calc(16px + var(--sat)); }
.settings-section p { color: var(--muted); margin: 0 0 12px; }

/* Login */
body.locked .shell { display: none; }
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth[hidden] { display: none; }
.auth .card { width: 100%; max-width: 360px; padding: 28px 24px; }
.auth .brand { justify-content: center; margin-bottom: 18px; font-size: 18px; padding: 0; }
.auth form { display: grid; gap: 12px; }
.auth .error { color: var(--expense); font-size: 13px; min-height: 18px; }
.auth button { width: 100%; padding: 10px; }

@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; bottom: 0; left: 0; right: 0; top: auto; height: auto; z-index: 20;
    flex-direction: row; align-items: stretch; gap: 0; padding: 0 0 var(--sab);
    border-right: none; border-top: 1px solid var(--border);
  }
  .sidebar .brand, .theme-toggle { display: none; }
  nav { flex-direction: row; overflow-x: auto; scrollbar-width: none; width: 100%; }
  nav::-webkit-scrollbar { display: none; }
  nav a { flex: 1 0 auto; text-align: center; padding: 13px 14px; border-radius: 0; font-size: 13px; white-space: nowrap; }
  nav a.active { background: none; box-shadow: inset 0 2px 0 var(--accent); }
  .view { padding: calc(18px + var(--sat)) 16px calc(80px + var(--sab)); }
  .two, .two.wide-left { grid-template-columns: 1fr; }
  .page-head .actions button.primary { padding: 9px 14px; }
}
@media (max-width: 640px) {
  input, select, textarea { font-size: 16px; } /* prevents iOS zoom on focus */
  .filters select, .filters input { flex: 1 1 130px; }
  h1 { font-size: 20px; }
  .kpis { grid-template-columns: 1fr 1fr; gap: 10px; }
  .kpi { padding: 12px 14px; }
  .kpi .value { font-size: 18px; }

  /* Tables become stacked cards */
  .table-wrap { margin: 0; padding: 0; overflow: visible; }
  table.rtable thead { display: none; }
  table.rtable, table.rtable tbody, table.rtable tr, table.rtable td { display: block; width: 100%; }
  table.rtable tr { border-bottom: 1px solid var(--border); padding: 10px 0; }
  table.rtable tr:last-child { border-bottom: none; }
  table.rtable tbody tr:hover { background: none; }
  table.rtable td { border: none; padding: 3px 0; display: flex; justify-content: flex-end; align-items: center; flex-wrap: wrap; gap: 4px 6px; text-align: right; }
  table.rtable td::before { content: attr(data-label); color: var(--muted); font-size: 12px; text-align: left; flex-shrink: 0; margin-right: auto; }
  table.rtable td.primary-cell { text-align: left; display: block; padding-bottom: 6px; }
  table.rtable td.primary-cell::before { display: none; }
  table.rtable td.actions-cell { padding-top: 8px; }
  table.rtable td.actions-cell::before { display: none; }
  table.rtable td.actions-cell .row-actions { justify-content: flex-start; flex-wrap: wrap; width: 100%; }
  table.rtable td:empty, table.rtable td.hide-mobile { display: none; }

  /* Modals become bottom sheets */
  .modal-backdrop { place-items: end center; padding: 0; }
  .modal { border-radius: 16px 16px 0 0; max-height: calc(92vh - var(--sat)); padding: 18px 16px calc(18px + var(--sab)); }
  .form-grid { grid-template-columns: 1fr; }
  .modal-foot button { flex: 1; padding: 11px; }
  .toast { bottom: calc(76px + var(--sab)); }
}
