:root {
  --bg: #0f172a;
  --panel: #ffffff;
  --ink: #1e293b;
  --muted: #64748b;
  --line: #e2e8f0;
  --brand: #0e7490;
  --brand-dark: #155e75;
  --rec: #0d9488;
  --exp: #e11d48;
  --pos: #15803d;
  --neg: #b91c1c;
  --shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 1px 2px rgba(15, 23, 42, .06);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: #f1f5f9;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.num { text-align: right; font-variant-numeric: tabular-nums; }
body.role-expenses-only .hide-for-expenses-only { display: none !important; }
body.role-expenses-only #recent-grid { grid-template-columns: 1fr; }
.kpi-row.kpi-compact { grid-template-columns: repeat(2, 1fr); max-width: 560px; }

/* ---------- login ---------- */
.login-wrap {
  min-height: 100vh; display: grid; place-items: center;
  background: radial-gradient(1200px 600px at 30% -10%, #164e63, #0f172a);
  padding: 20px;
}
.login-card {
  background: var(--panel); border-radius: 14px; padding: 32px;
  width: min(340px, 100%); box-shadow: 0 20px 40px rgba(0, 0, 0, .3); display: grid; gap: 12px;
}
.login-card h1 { margin: 0; font-size: 20px; }
.login-card p { margin: 0 0 8px; }
.login-card label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.login-card input {
  padding: 10px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px;
}
.login-card button {
  margin-top: 6px; padding: 10px; border: 0; border-radius: 8px;
  background: var(--brand); color: #fff; font-weight: 600; cursor: pointer;
}
.error { color: var(--neg); font-size: 13px; min-height: 16px; }

/* ---------- topbar ---------- */
.topbar {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 8px 18px; padding-left: max(18px, env(safe-area-inset-left));
  padding-right: max(18px, env(safe-area-inset-right));
  background: var(--brand-dark); color: #fff; min-height: 54px; position: sticky; top: 0; z-index: 10;
}
.brand {
  font-weight: 700; letter-spacing: .3px; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.nav-toggle {
  display: none; align-items: center; justify-content: center;
  width: 40px; height: 40px; padding: 0; font-size: 20px; line-height: 1;
  background: transparent; border: 1px solid rgba(255, 255, 255, .5); color: #fff;
  border-radius: 8px; cursor: pointer;
}
#tabs { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
#tabs button {
  background: transparent; color: #cffafe; border: 0; padding: 8px 12px;
  border-radius: 8px; cursor: pointer; font-size: 14px;
}
#tabs button:hover { background: rgba(255, 255, 255, .1); }
#tabs button.active { background: #fff; color: var(--brand-dark); font-weight: 600; }
.user-box { display: flex; align-items: center; gap: 10px; font-size: 13px; margin-left: auto; }
#who { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
button.ghost {
  background: transparent; border: 1px solid rgba(255, 255, 255, .5); color: #fff;
  border-radius: 7px; padding: 5px 10px; cursor: pointer;
}
.card .filters button.ghost, .card button.ghost { border-color: var(--line); color: var(--brand-dark); }

main { max-width: 1180px; margin: 0 auto; padding: 20px 16px 60px; }

/* ---------- cards / kpi ---------- */
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: var(--shadow); margin-bottom: 18px; overflow: hidden;
}
.card-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 16px; border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
.card-head h2 { margin: 0; font-size: 15px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
@media (max-width: 860px) { .grid-2 { grid-template-columns: 1fr; } }

.kpi-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 18px; }
@media (max-width: 860px) { .kpi-row { grid-template-columns: 1fr 1fr; } }
.kpi {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 16px; box-shadow: var(--shadow);
}
.kpi .label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.kpi .value { font-size: 22px; font-weight: 700; margin-top: 6px; }
.kpi .sub { font-size: 12px; color: var(--muted); margin-top: 4px; }
.kpi.pos .value { color: var(--pos); }
.kpi.neg .value { color: var(--neg); }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; max-width: 100%; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 9px 14px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { background: #f8fafc; color: var(--muted); font-weight: 600; font-size: 12px; }
tbody tr:hover { background: #f8fafc; }
td.pos { color: var(--pos); font-weight: 600; }
td.neg { color: var(--neg); font-weight: 600; }
.row-link { cursor: pointer; }
button.del {
  background: transparent; border: 0; color: var(--neg); cursor: pointer; font-size: 13px;
}

/* ---------- forms ---------- */
.entry-form {
  display: flex; flex-wrap: wrap; gap: 10px; padding: 14px 16px;
}
.entry-form input, .entry-form select {
  padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; font-size: 13px; min-width: 130px;
}
.entry-form select[multiple] { padding: 4px; height: auto; }
.modal-body select[multiple] { padding: 4px; }
.entry-form button {
  padding: 8px 16px; border: 0; border-radius: 8px; background: var(--brand);
  color: #fff; font-weight: 600; cursor: pointer;
}
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.filters select, .filters input {
  padding: 6px 8px; border: 1px solid var(--line); border-radius: 7px; font-size: 13px;
}
.entry-form input[name="app_name"] { min-width: 280px; flex: 1; }
.settings-hint { padding: 0 16px 16px; margin: 0; }

/* ---------- sortable headers ---------- */
th[data-sort] { cursor: pointer; user-select: none; white-space: nowrap; }
th[data-sort]:hover { color: var(--brand-dark); }
th[data-sort]::after { content: "\2195"; opacity: .3; margin-left: 5px; font-size: 10px; }
th[data-sort].sorted[data-dir="asc"]::after { content: "\25B2"; opacity: .9; }
th[data-sort].sorted[data-dir="desc"]::after { content: "\25BC"; opacity: .9; }
th.col-act { width: 90px; }

/* ---------- pager ---------- */
.pager {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 12px 16px; border-top: 1px solid var(--line); font-size: 13px;
}
.pager button {
  background: transparent; border: 1px solid var(--line); color: var(--brand-dark);
  border-radius: 7px; padding: 5px 12px; cursor: pointer;
}
.pager button:disabled { opacity: .4; cursor: default; }
.pager .pager-page { font-weight: 600; }
.pager .pager-info { color: var(--muted); }
.pager select { margin-left: auto; padding: 5px 8px; border: 1px solid var(--line); border-radius: 7px; font-size: 13px; }

/* ---------- modal ---------- */
.modal-wrap {
  position: fixed; inset: 0; background: rgba(15, 23, 42, .45);
  display: grid; place-items: center; z-index: 60; padding: 20px;
}
.modal {
  background: var(--panel); border-radius: 12px; width: 460px; max-width: 100%;
  box-shadow: 0 24px 60px rgba(0, 0, 0, .3); overflow: hidden;
}
.modal-head, .modal-foot {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px; border-bottom: 1px solid var(--line);
}
.modal-head h3 { margin: 0; font-size: 15px; }
.modal-foot { border-bottom: 0; border-top: 1px solid var(--line); gap: 10px; }
.modal-body { padding: 16px 18px; display: grid; gap: 12px; }
.modal-body label { display: grid; gap: 4px; font-size: 12px; color: var(--muted); }
.modal-body input, .modal-body select {
  padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px;
}
.modal-foot button:not(.ghost) {
  background: var(--brand); color: #fff; border: 0; border-radius: 8px;
  padding: 8px 18px; font-weight: 600; cursor: pointer;
}
.modal-foot button.ghost { border-color: var(--line); color: var(--muted); }
button.act {
  background: transparent; border: 0; cursor: pointer; font-size: 13px;
  color: var(--brand-dark); padding: 2px 4px;
}
button.act.danger { color: var(--neg); }

/* ---------- charts ---------- */
.chart { padding: 14px 10px 4px; }
.chart svg { width: 100%; height: auto; display: block; }
.legend { display: flex; align-items: center; gap: 8px; padding: 0 16px 14px; font-size: 12px; color: var(--muted); }
.sw { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
.sw-rec { background: var(--rec); }
.sw-exp { background: var(--exp); }
.bars { padding: 14px 16px; display: grid; gap: 10px; }
.bar-row { display: grid; grid-template-columns: 130px 1fr 90px; gap: 10px; align-items: center; font-size: 12px; }
.bar-track { background: #f1f5f9; border-radius: 6px; overflow: hidden; height: 16px; }
.bar-fill { display: block; height: 100%; min-width: 2px; background: var(--brand); border-radius: 6px; }

.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 10px;
  font-size: 13px; box-shadow: 0 10px 30px rgba(0, 0, 0, .25); z-index: 50;
}
.toast.err { background: var(--neg); }

/* ---------- responsive ---------- */
@media (max-width: 960px) {
  .topbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto auto;
    align-items: center;
    gap: 8px 10px;
    padding: 8px 12px;
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
  }
  .nav-toggle { display: inline-flex; }
  .user-box { margin-left: 0; justify-self: end; }
  .user-box .ghost { min-height: 40px; padding: 8px 12px; }
  #who { max-width: 28vw; }
  #tabs {
    display: none;
    grid-column: 1 / -1;
    flex: none;
    flex-direction: column;
    gap: 2px;
  }
  .topbar.nav-open #tabs { display: flex; }
  #tabs button {
    width: 100%;
    text-align: left;
    padding: 12px 14px;
    min-height: 44px;
  }
}

@media (max-width: 860px) {
  main { padding: 14px 12px 48px; }
  .grid-2, body.role-expenses-only #recent-grid { grid-template-columns: 1fr; }
  .kpi-row { grid-template-columns: 1fr 1fr; }
  .kpi-row.kpi-compact { max-width: none; }
  .card-head { align-items: flex-start; }
  .card-head .filters, .card-head select { width: 100%; }
  .filters { width: 100%; }
  .filters input, .filters select, .filters button { min-width: 0; flex: 1 1 140px; }
  .filters input.search { flex: 1 1 100%; min-width: 0; }
  .entry-form input, .entry-form select, .entry-form button {
    flex: 1 1 160px; min-width: 0;
  }
  .entry-form input[name="app_name"] { min-width: 0; flex: 1 1 100%; }
  .pager select { margin-left: 0; }
  .modal-wrap { padding: 12px; align-items: end; }
  .modal { width: 100%; max-height: min(92vh, 720px); overflow: auto; border-radius: 12px 12px 0 0; }
  .bar-row { grid-template-columns: 1fr 80px; }
  .bar-row > span:first-child { grid-column: 1 / -1; }
  input, select, textarea, button { font-size: 16px; }
  .login-card { padding: 24px 18px; }
}

@media (max-width: 560px) {
  .kpi-row, .kpi-row.kpi-compact { grid-template-columns: 1fr; }
  .kpi .value { font-size: 20px; }
  .entry-form input, .entry-form select, .entry-form button {
    flex: 1 1 100%; width: 100%;
  }
  .pager { gap: 8px; }
  .pager .pager-info { width: 100%; }
  .toast {
    left: 12px; right: 12px; transform: none; width: auto; text-align: center;
  }
  #who { display: none; }
}
