:root {
  --bg: #030733;
  --bg-2: #070d45;
  --card: #0b1352;
  --card-2: #101a63;
  --line: rgba(130, 150, 255, .16);
  --text: #eef1ff;
  --muted: #9ea7d8;
  --gold: #f6bd2f;
  --gold-2: #d8901a;
  --cyan: #22d3ee;
  --blue: #1e8bff;
  --green: #34d399;
  --red: #ff5d6c;
  --radius: 16px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: var(--font);
  color: var(--text);
  background:
    radial-gradient(900px 500px at 85% -10%, rgba(34, 211, 238, .13), transparent 60%),
    radial-gradient(700px 400px at -10% 10%, rgba(246, 189, 47, .08), transparent 60%),
    var(--bg);
  background-attachment: fixed;
  min-height: 100vh;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
body[data-role="viewer"] .admin-only { display: none !important; }
body[data-role="admin"] .viewer-only { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); font-weight: 400; }
h2 { font-size: 1.05rem; margin: 0 0 14px; letter-spacing: .01em; }

/* ---------- buttons & inputs */
.btn {
  appearance: none; border: 1px solid transparent; border-radius: 10px; cursor: pointer;
  font: 600 .95rem/1 var(--font); padding: 12px 18px; color: var(--text); background: var(--card-2);
  text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  transition: transform .06s ease, filter .15s ease, background .15s ease;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .6; cursor: progress; }
.btn-sm { padding: 8px 12px; font-size: .85rem; }
.btn-block { width: 100%; }
.btn-gold { background: linear-gradient(180deg, var(--gold), var(--gold-2)); color: #1a1300; }
.btn-gold:hover { filter: brightness(1.06); }
.btn-ghost { background: transparent; border-color: var(--line); color: var(--text); }
.btn-ghost:hover { background: rgba(255, 255, 255, .05); }
.btn-danger { background: var(--red); color: #fff; }
.btn-danger-ghost { background: transparent; color: var(--red); border-color: rgba(255, 93, 108, .4); }
.link-btn { background: none; border: 0; color: var(--gold); font: 600 .85rem var(--font); cursor: pointer; padding: 0; }
.link-btn:hover { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--cyan); outline-offset: 2px; }

input, select, textarea {
  width: 100%; font: 400 1rem var(--font); color: var(--text);
  background: var(--bg-2); border: 1px solid var(--line); border-radius: 10px; padding: 11px 12px;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--cyan); box-shadow: 0 0 0 3px rgba(34, 211, 238, .18); }
input[type="date"] { min-height: 44px; }
textarea { resize: vertical; }
label { display: block; font-size: .82rem; color: var(--muted); font-weight: 600; }
label input, label textarea, label select { margin-top: 6px; }
.form { display: grid; gap: 12px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-error { color: var(--red); font-size: .88rem; min-height: 1.2em; margin: 0; }

/* ---------- lock screen */
.lock { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.lock-card {
  width: 100%; max-width: 400px; text-align: center; padding: 28px 26px 22px;
  background: #010437; /* matches the logo background */
  border: 1px solid var(--line); border-radius: 22px; box-shadow: 0 30px 80px rgba(0, 0, 0, .45);
}
.lock-logo { width: 100%; max-width: 300px; height: auto; display: block; margin: 0 auto 6px; }
.lock-title { font-size: 1.35rem; margin: 6px 0 2px; }
.lock-sub { color: var(--muted); margin: 0 0 18px; font-size: .95rem; }
#pin { text-align: center; font-size: 1.6rem; letter-spacing: .4em; padding: 14px; margin-bottom: 12px; }
#pin::placeholder { letter-spacing: .3em; color: #4a548f; }
.lock .form-error { margin-top: 10px; }
body[data-role="admin"] .lock-card { border-color: rgba(246, 189, 47, .35); }

/* ---------- top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px max(16px, env(safe-area-inset-left));
  background: rgba(3, 7, 51, .82); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand img { border-radius: 10px; }
.brand-name { font-weight: 800; letter-spacing: .01em; }
.brand-sub { color: var(--gold); font-size: .78rem; font-weight: 700; letter-spacing: .14em; text-transform: uppercase; }
.topbar-right { display: flex; align-items: center; gap: 10px; }
.role-pill { font-size: .75rem; font-weight: 700; padding: 5px 10px; border-radius: 99px; background: rgba(34, 211, 238, .12); color: var(--cyan); border: 1px solid rgba(34, 211, 238, .3); }
body[data-role="admin"] .role-pill { background: rgba(246, 189, 47, .12); color: var(--gold); border-color: rgba(246, 189, 47, .35); }

/* ---------- layout */
.wrap { max-width: 1120px; margin: 0 auto; padding: 22px 16px 40px; display: grid; gap: 18px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; }
.grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 18px; align-items: start; }
body[data-role="viewer"] .grid { grid-template-columns: 1fr; }

/* ---------- summary */
.summary { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; }
.stat {
  position: relative; overflow: hidden; border-radius: var(--radius); padding: 18px 18px 16px;
  background: var(--card); border: 1px solid var(--line);
}
.stat::before { content: ""; position: absolute; inset: 0 0 auto 0; height: 3px; }
.stat-budget::before { background: linear-gradient(90deg, var(--gold), var(--gold-2)); }
.stat-spent::before { background: linear-gradient(90deg, var(--cyan), var(--blue)); }
.stat-remaining::before { background: var(--green); }
.stat-remaining.over::before { background: var(--red); }
.stat-head { display: flex; justify-content: space-between; align-items: center; }
.stat-label { color: var(--muted); font-size: .8rem; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; }
.stat-value { font-size: clamp(1.5rem, 3.2vw, 2.1rem); font-weight: 800; margin-top: 6px; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.stat-budget .stat-value { color: var(--gold); }
.stat-spent .stat-value { color: var(--cyan); }
.stat-remaining .stat-value { color: var(--green); }
.stat-remaining.over .stat-value { color: var(--red); }
.stat-remaining.low .stat-value { color: var(--gold); }
.stat-foot { color: var(--muted); font-size: .85rem; margin-top: 2px; }
.inline-form { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.money-input { display: flex; align-items: center; flex: 1 1 160px; background: var(--bg-2); border: 1px solid var(--line); border-radius: 10px; padding-left: 12px; }
.money-input span { color: var(--muted); font-weight: 700; }
.money-input input { border: 0; background: transparent; box-shadow: none !important; padding-left: 6px; }

.meter-wrap { margin-top: -4px; }
.meter { height: 12px; border-radius: 99px; background: var(--card); border: 1px solid var(--line); overflow: hidden; }
.meter-fill { height: 100%; width: 0; border-radius: 99px; background: linear-gradient(90deg, var(--cyan), var(--blue)); transition: width .5s ease; }
.meter-fill.low { background: linear-gradient(90deg, var(--gold), var(--gold-2)); }
.meter-fill.over { background: var(--red); }
.meter-label { display: flex; justify-content: space-between; color: var(--muted); font-size: .82rem; margin-top: 6px; gap: 10px; flex-wrap: wrap; }

.empty-budget { display: flex; gap: 8px; flex-wrap: wrap; border-color: rgba(246, 189, 47, .35); }

/* ---------- breakdown */
.breakdown { display: grid; gap: 12px; }
.bd-row { display: grid; gap: 5px; }
.bd-top { display: flex; justify-content: space-between; gap: 10px; font-size: .92rem; }
.bd-top .amt { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.bd-top .pct { color: var(--muted); font-weight: 500; margin-left: 6px; }
.bd-bar { height: 8px; border-radius: 99px; background: var(--bg-2); overflow: hidden; }
.bd-bar > div { height: 100%; border-radius: 99px; background: linear-gradient(90deg, var(--gold), var(--cyan)); }

/* ---------- list */
.list-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.list-head h2 { margin: 0; }
.list-tools { display: flex; gap: 8px; flex-wrap: wrap; }
.list-tools input { width: 200px; padding: 8px 12px; }
.list-tools select { width: auto; padding: 8px 12px; }
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: .93rem; }
.table th { text-align: left; color: var(--muted); font-size: .75rem; text-transform: uppercase; letter-spacing: .08em; font-weight: 700; padding: 10px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.table td { padding: 12px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table tbody tr:hover { background: rgba(255, 255, 255, .025); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; font-weight: 700; }
.table tfoot td { font-weight: 800; border-bottom: 0; padding-top: 14px; }
.table .date { white-space: nowrap; color: var(--muted); }
.table .title { font-weight: 600; }
.table .note { color: var(--muted); font-size: .84rem; margin-top: 2px; white-space: pre-wrap; word-break: break-word; }
.chip { display: inline-block; font-size: .78rem; font-weight: 600; padding: 3px 9px; border-radius: 99px; background: rgba(34, 211, 238, .1); color: #a5f0fb; border: 1px solid rgba(34, 211, 238, .22); white-space: nowrap; }
.actions-col { width: 1%; }
.icon-btn { background: none; border: 1px solid var(--line); color: var(--muted); border-radius: 8px; padding: 6px 10px; cursor: pointer; font: 600 .8rem var(--font); }
.icon-btn:hover { color: var(--text); border-color: var(--cyan); }
.empty { color: var(--muted); text-align: center; padding: 24px 0 8px; margin: 0; }

/* ---------- activity */
.activity { list-style: none; margin: 0; padding: 0; display: grid; gap: 0; max-height: 320px; overflow: auto; }
.activity li { display: flex; gap: 12px; padding: 9px 0; border-bottom: 1px solid var(--line); font-size: .9rem; }
.activity time { color: var(--muted); white-space: nowrap; font-size: .82rem; min-width: 128px; }

/* ---------- dialogs */
.dialog { border: 1px solid var(--line); border-radius: 18px; background: var(--card); color: var(--text); padding: 22px; width: min(560px, calc(100vw - 24px)); }
.dialog-sm { width: min(400px, calc(100vw - 24px)); }
.dialog::backdrop { background: rgba(1, 3, 25, .7); backdrop-filter: blur(3px); }
.dialog p { color: var(--muted); }
.dialog-actions { display: flex; gap: 10px; align-items: center; margin-top: 6px; }
.spacer { flex: 1; }

/* ---------- toast */
.toast {
  position: fixed; left: 50%; bottom: 22px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: #0f1b6b; border: 1px solid var(--line); color: var(--text); padding: 10px 16px; border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .4); transition: all .25s ease; font-size: .92rem; z-index: 50;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { border-color: rgba(255, 93, 108, .5); }

.foot { text-align: center; color: #5c6599; font-size: .8rem; padding: 0 0 28px; }

/* ---------- responsive */
@media (max-width: 860px) {
  .grid { grid-template-columns: 1fr; }
}
@media (max-width: 680px) {
  .summary { grid-template-columns: 1fr 1fr; }
  .stat-budget { grid-column: 1 / -1; }
  .stat { padding: 14px 14px 12px; }
  .stat-value { font-size: 1.3rem; overflow-wrap: anywhere; }
  .stat-budget .stat-value { font-size: 1.7rem; }
  .brand-name { font-size: .92rem; }
  .list-tools, .list-tools input, .list-tools select { width: 100%; }
  .list-tools .btn { width: 100%; }
  /* table → cards */
  .table thead { display: none; }
  .table, .table tbody, .table tr, .table td, .table tfoot { display: block; width: 100%; }
  .table tbody tr { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; padding: 12px 0; border-bottom: 1px solid var(--line); }
  .table td { border: 0; padding: 0; }
  .table td.c-title { grid-column: 1; grid-row: 1; }
  .table td.num { grid-column: 2; grid-row: 1; font-size: 1.02rem; }
  .table td.date { grid-column: 1; grid-row: 2; font-size: .82rem; }
  .table td.c-cat { grid-column: 2; grid-row: 2; text-align: right; }
  .table td.c-paid { grid-column: 1; grid-row: 3; font-size: .84rem; color: var(--muted); }
  .table td.c-paid:empty { display: none; }
  .table td.c-paid::before { content: "Paid by "; }
  .table td.c-act { grid-column: 2; grid-row: 3; text-align: right; }
  .table tfoot tr { display: flex; justify-content: space-between; padding: 14px 0 0; }
  .table tfoot td { width: auto; }
  .table tfoot td:last-child { display: none; }
  .activity li { flex-direction: column; gap: 2px; }
  .row2 { grid-template-columns: 1fr; }
}
@media (max-width: 380px) {
  .summary { grid-template-columns: 1fr; }
  .brand-sub { display: none; }
}

/* ================================================================
   Admin on phones: bottom action bar, bottom-sheet forms, tappable rows
   ================================================================ */
.tappable { cursor: pointer; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.sheet-head h2 { margin: 0; }
.sheet-close { background: none; border: 1px solid var(--line); color: var(--muted); width: 36px; height: 36px; border-radius: 50%; font-size: 1rem; cursor: pointer; flex: none; }
.sheet-close:hover { color: var(--text); }
.activity-card summary { display: flex; align-items: baseline; gap: 10px; cursor: pointer; list-style: none; }
.activity-card summary::-webkit-details-marker { display: none; }
.activity-card summary::after { content: "▾"; margin-left: auto; color: var(--muted); transition: transform .2s; }
.activity-card[open] summary::after { transform: rotate(180deg); }
.activity-card summary h2 { margin: 0; }
.activity-card[open] .activity { margin-top: 12px; }
.mobile-bar { display: none; }

@media (max-width: 680px) {
  /* compact header */
  .topbar { padding: 8px 12px; }
  .brand img { width: 34px; height: 34px; }
  .brand-name { font-size: .88rem; }
  .brand-sub { font-size: .66rem; }
  .role-pill { padding: 4px 8px; }
  .wrap { padding: 14px 12px 32px; gap: 14px; }
  .card { padding: 16px 14px; }

  /* budget editor: input full width, buttons share a row */
  .inline-form .money-input { flex: 1 1 100%; }
  .inline-form .btn { flex: 1; padding: 11px; }
  .money-input input { font-size: 1.1rem; }

  /* admin: replace the inline add form with a fixed bottom button */
  body[data-role="admin"] #add-card { display: none; }
  body[data-role="admin"] .mobile-bar {
    display: block; position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
    background: rgba(3, 7, 51, .9); backdrop-filter: blur(10px); border-top: 1px solid var(--line);
  }
  body[data-role="admin"] .mobile-bar .btn { padding: 15px; font-size: 1rem; border-radius: 12px; }
  body[data-role="admin"] .foot { padding-bottom: calc(96px + env(safe-area-inset-bottom)); }
  body[data-role="admin"] .toast { bottom: calc(86px + env(safe-area-inset-bottom)); }

  /* rows: bigger touch area, edit button stays visible */
  body[data-role="admin"] .table tbody tr { padding: 14px 4px; margin: 0 -4px; border-radius: 10px; }
  body[data-role="admin"] .table tbody tr:active { background: rgba(255, 255, 255, .05); }
  .icon-btn { padding: 7px 12px; }

  /* dialogs become bottom sheets */
  .dialog, .dialog-sm {
    width: 100%; max-width: 100%; margin: auto 0 0; border-radius: 20px 20px 0 0;
    border-bottom: 0; max-height: 92dvh; overflow-y: auto; overscroll-behavior: contain;
    padding: 10px 16px calc(14px + env(safe-area-inset-bottom));
    animation: sheet-up .22s ease-out;
  }
  .dialog::before { content: ""; display: block; width: 40px; height: 4px; border-radius: 4px; background: rgba(255, 255, 255, .2); margin: 0 auto 12px; }
  .dialog .row2 { grid-template-columns: 1fr 1fr; }
  .dialog .row-cat { grid-template-columns: 1.5fr 1fr; }
  .dialog .form-error:empty { display: none; }
  .dialog input, .dialog textarea { font-size: 16px; } /* stops iOS zooming into fields */
  .dialog-actions { position: sticky; bottom: calc(-14px - env(safe-area-inset-bottom)); background: var(--card); padding: 10px 0 4px; flex-wrap: wrap; }
  .dialog-actions .spacer { display: none; }
  .dialog-actions .btn { flex: 1 1 0; padding: 13px 10px; }
  #edit-save, #confirm-yes { order: -1; flex: 1 1 100%; padding: 15px; font-size: 1rem; }
}
@media (max-width: 360px) {
  .dialog .row2, .dialog .row-cat { grid-template-columns: 1fr; }
}
@keyframes sheet-up { from { transform: translateY(40px); opacity: .4; } to { transform: none; opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .dialog { animation: none; } }
