/* Monthly Finance - styles. System fonts only, nothing loaded from outside. */

:root {
  --paper: #f2f5f4;
  --surface: #ffffff;
  --ink: #14232b;
  --ink-2: #52646b;
  --line: #dbe3e1;
  --teal: #0f5e63;
  --teal-ink: #ffffff;
  --teal-soft: #dcecea;
  --slate: #3c4f58;
  --amber: #a86a00;
  --amber-soft: #fbefd5;
  --red: #b3261e;
  --red-soft: #fbe4e1;
  --green: #1e7a4c;
  --track: #dfe6e4;
  --shadow: 0 10px 30px rgba(20, 35, 43, .18);
  --ui: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --fig: ui-serif, "New York", "Iowan Old Style", Georgia, "Times New Roman", serif;
  --tabs-h: 62px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #0e1719;
    --surface: #162326;
    --ink: #e6eeec;
    --ink-2: #9db0ad;
    --line: #26373a;
    --teal: #5cc2b8;
    --teal-ink: #062426;
    --teal-soft: #173638;
    --slate: #9fb3b8;
    --amber: #e9b451;
    --amber-soft: #3a2f14;
    --red: #ff8a80;
    --red-soft: #3d1c1a;
    --green: #6fd19c;
    --track: #24363a;
    --shadow: 0 10px 30px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 16px/1.45 var(--ui);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
body.modal-open { overflow: hidden; }
h1, h2, p, dl, dd, ul, fieldset { margin: 0; padding: 0; }
fieldset { border: 0; min-width: 0; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--teal); outline-offset: 2px; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- figures: fils set smaller than dinars ---------- */
.m { font-family: var(--fig); font-variant-numeric: tabular-nums lining-nums; white-space: nowrap; }
.m .fils { font-size: .72em; opacity: .72; }
.m.neg { color: var(--red); }
.pos .m { color: var(--green); }
.tx .m.neg { color: inherit; }   /* red is reserved for negative balances */

/* ---------- frame ---------- */
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px;
  padding: calc(var(--safe-t) + 10px) 16px 10px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}
.top h1 { flex: 1; font-size: 1.06rem; font-weight: 650; letter-spacing: -.01em; }
.back { display: inline-flex; align-items: center; gap: 2px; border: 0; background: none; padding: 6px 6px 6px 0; color: var(--teal); min-height: 36px; }
.back svg { width: 20px; height: 20px; }
.sync { border: 0; border-radius: 999px; padding: 6px 12px; font-size: .8rem; font-weight: 600; min-height: 32px; background: var(--teal-soft); color: var(--teal); }
.sync.warn { background: var(--amber-soft); color: var(--amber); }
.sync.bad { background: var(--red-soft); color: var(--red); }
.sync.busy::after { content: ""; display: inline-block; width: 6px; height: 6px; margin-left: 7px; border-radius: 50%; background: currentColor; animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .2; } }

main {
  max-width: 560px; margin: 0 auto;
  padding: 14px 16px calc(var(--tabs-h) + var(--safe-b) + 28px);
  outline: none;
}

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  display: grid; grid-template-columns: repeat(5, 1fr);
  height: calc(var(--tabs-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  background: var(--surface); border-top: 1px solid var(--line);
}
.tabs button { border: 0; background: none; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: .68rem; color: var(--ink-2); }
.tabs button.on { color: var(--teal); font-weight: 650; }
.tabs .tab-add svg { width: 44px; height: 44px; padding: 10px; border-radius: 50%; background: var(--teal); color: var(--teal-ink); stroke-width: 2.4; margin-top: -4px; }
.tabs .tab-add span { display: none; }
.tabs .tab-add.on svg { box-shadow: 0 0 0 3px var(--paper), 0 0 0 5px var(--teal); }

/* ---------- text ---------- */
.sec { font-size: .95rem; font-weight: 650; margin: 26px 0 8px; }
.lead { font-size: 1.02rem; font-weight: 600; margin-bottom: 4px; }
.muted { color: var(--ink-2); font-size: .88rem; }
.muted.pad { padding: 12px 14px; }
.loading { text-align: center; color: var(--ink-2); padding: 64px 0; }
.empty { text-align: center; padding: 40px 8px 24px; display: grid; gap: 14px; justify-items: center; }
.empty h2 { font-size: 1.3rem; }
.empty p { color: var(--ink-2); max-width: 34ch; }
.tag { font-style: normal; font-size: .72rem; font-weight: 600; padding: 2px 7px; border-radius: 999px; background: var(--teal-soft); color: var(--teal); white-space: nowrap; }
.link { border: 0; background: none; padding: 6px 0; color: var(--teal); font-weight: 600; text-align: left; min-height: 36px; }
.danger-text { color: var(--red); }
.list-meta { margin: 14px 0 8px; color: var(--ink-2); font-size: .86rem; }
.list-meta .link { margin-left: 8px; padding: 0; min-height: 0; }

/* ---------- buttons and inputs ---------- */
.btn {
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  border-radius: 12px; padding: 10px 14px; min-height: 44px; font-weight: 600; font-size: .92rem;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--teal); border-color: var(--teal); color: var(--teal-ink); }
.btn.danger { color: var(--red); border-color: var(--red); background: transparent; }
.btn.ghost { background: transparent; }
.btn.big { width: 100%; min-height: 54px; font-size: 1.02rem; border-radius: 14px; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0 4px; }
.actions .btn { flex: 1 1 auto; }
.row-btns { display: flex; gap: 8px; margin-top: 18px; flex-wrap: wrap; }
.row-btns .btn { flex: 1 1 0; white-space: nowrap; }
.icon-btn { border: 0; background: none; width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; color: var(--teal); }
.icon-btn:disabled { color: var(--line); cursor: default; }

.field { display: grid; gap: 5px; margin-top: 12px; min-width: 0; }
.field-label { font-size: .82rem; font-weight: 600; color: var(--ink-2); }
.hint { font-size: .8rem; color: var(--ink-2); }
.input {
  width: 100%; min-width: 0; min-height: 46px; padding: 9px 12px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--surface); color: var(--ink);
  font-size: 16px; -webkit-appearance: none; appearance: none;
}
select.input { padding-right: 30px; background-image: linear-gradient(45deg, transparent 50%, var(--ink-2) 50%), linear-gradient(135deg, var(--ink-2) 50%, transparent 50%); background-position: calc(100% - 17px) 20px, calc(100% - 12px) 20px; background-size: 5px 5px; background-repeat: no-repeat; }
input[type="date"].input { display: block; text-align: left; }
.input.num { font-family: var(--fig); font-variant-numeric: tabular-nums; text-align: right; font-size: 17px; }
.input:focus { outline: 2px solid var(--teal); outline-offset: 0; border-color: var(--teal); }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.check { display: flex; align-items: flex-start; gap: 10px; margin-top: 12px; min-height: 30px; }
.check input { width: 22px; height: 22px; margin: 1px 0 0; accent-color: var(--teal); flex: none; }
.check span { display: grid; font-weight: 600; }
.check small { font-weight: 400; color: var(--ink-2); font-size: .8rem; }
.check.small span { font-weight: 400; font-size: .86rem; color: var(--ink-2); }

.switch { display: flex; align-items: center; gap: 12px; margin-top: 16px; font-weight: 600; min-height: 44px; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch-track { width: 50px; height: 30px; border-radius: 999px; background: var(--track); position: relative; flex: none; transition: background .15s; }
.switch-track::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, .3); transition: transform .15s; }
.switch input:checked + .switch-track { background: var(--teal); }
.switch input:checked + .switch-track::after { transform: translateX(20px); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--teal); outline-offset: 2px; }

/* ---------- period selector ---------- */
.pnav { display: flex; align-items: center; gap: 4px; margin: 0 -8px 6px; }
.pnav-label { flex: 1; border: 0; background: none; text-align: center; padding: 4px; display: grid; gap: 1px; font-weight: 650; }
.pnav-label small { font-weight: 400; color: var(--ink-2); font-size: .76rem; }

/* ---------- dashboard hero ---------- */
.hero { padding: 10px 0 4px; }
.hero-label { color: var(--ink-2); font-size: .9rem; }
.hero-num { font-size: clamp(2.6rem, 13vw, 3.6rem); line-height: 1.05; letter-spacing: -.02em; margin: 2px 0 14px; }
.hero-num .unit { font: 600 .95rem var(--ui); color: var(--ink-2); margin-left: 8px; letter-spacing: 0; }
.hero.over .hero-label { color: var(--red); font-weight: 600; }
.split { display: flex; height: 12px; border-radius: 6px; overflow: hidden; background: var(--track); }
.split i { display: block; height: 100%; }
.s-pots { background: var(--teal); }
.s-spent { background: var(--slate); }
.hero.over .split { box-shadow: 0 0 0 2px var(--red); }
.figures { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 12px; }
.figures dt { font-size: .8rem; color: var(--ink-2); display: flex; align-items: center; gap: 6px; }
.figures dd { font-size: 1.12rem; }
.dot { width: 9px; height: 9px; border-radius: 2px; display: inline-block; }
.hero-note { margin-top: 8px; color: var(--ink-2); font-size: .86rem; }

/* ---------- lists ---------- */
.group { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; margin-bottom: 12px; }
.group.pad { padding: 12px 14px; }
.group.pad .actions { margin-top: 10px; }
.group-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 9px 14px 7px; font-size: .82rem; font-weight: 650; color: var(--ink-2); border-bottom: 1px solid var(--line); }
.group-head .link { padding: 0; min-height: 0; font-size: inherit; }
.group-total { font-weight: 400; }
.row { display: flex; align-items: center; gap: 12px; width: 100%; padding: 12px 14px; min-height: 54px; border: 0; border-bottom: 1px solid var(--line); background: none; text-align: left; }
.group > .row:last-child, .group > .al-row:last-child { border-bottom: 0; }
.row:active { background: var(--teal-soft); }
.row-main { flex: 1; min-width: 0; display: grid; gap: 3px; }
.row-title { font-weight: 600; overflow-wrap: anywhere; }
.row-sub { font-size: .82rem; color: var(--ink-2); overflow-wrap: anywhere; }
.row-amt { font-size: 1.08rem; text-align: right; }
.row.archived .row-title, .row.archived .row-amt { opacity: .55; }
.bar { display: block; height: 5px; border-radius: 3px; background: var(--track); overflow: hidden; margin: 3px 0 1px; }
.bar i { display: block; height: 100%; background: var(--teal); border-radius: 3px; }
.bar.full i { background: var(--green); }

.filters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.filters .input { min-height: 42px; font-size: 15px; }
.filters .input:first-child { grid-column: 1 / -1; }

/* ---------- add ---------- */
.amount-box { display: grid; gap: 4px; }
.amount-input {
  width: 100%; border: 0; border-bottom: 2px solid var(--line); border-radius: 0; background: none; color: var(--ink);
  font: 3rem/1.15 var(--fig); font-variant-numeric: tabular-nums; padding: 4px 0 8px; letter-spacing: -.02em;
}
.amount-input:focus { outline: none; border-bottom-color: var(--teal); }
.amount-input::placeholder { color: var(--line); }
.chips { margin-top: 16px; }
.chips legend { margin-bottom: 8px; }
.chip { display: inline-block; margin: 0 6px 8px 0; }
.chip input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.chip span { display: inline-block; padding: 10px 14px; min-height: 44px; border: 1px solid var(--line); border-radius: 999px; background: var(--surface); font-size: .92rem; font-weight: 600; }
.chip input:checked + span { background: var(--teal); border-color: var(--teal); color: var(--teal-ink); }
.chip input:focus-visible + span { outline: 2px solid var(--teal); outline-offset: 2px; }
.add .btn.big { margin-top: 20px; }

/* ---------- new period and allocate ---------- */
.np-item { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 4px 14px 12px; margin: 10px 0; }
.np-item.off > :not(.np-top) { display: none; }
.np-item.off .np-amt { visibility: hidden; }
.np-item.off .check span { color: var(--ink-2); text-decoration: line-through; }
.np-top { display: flex; gap: 10px; align-items: center; }
.np-top .check { flex: 1; min-width: 0; }
.np-amt { width: 118px; flex: none; }
.np-more { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 10px; }
.np-more select { grid-column: 1 / -1; }
.al-row { display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.al-row .input { width: 124px; flex: none; }
.sticky-foot {
  position: sticky; bottom: calc(var(--tabs-h) + var(--safe-b)); z-index: 3;
  margin: 18px -16px 0; padding: 10px 16px 12px;
  background: var(--paper); border-top: 1px solid var(--line);
}
.foot-sum { margin-bottom: 8px; font-size: .95rem; }

/* ---------- settings ---------- */
.checks { list-style: none; margin-top: 12px; display: grid; gap: 8px; }
.checks li { display: grid; gap: 1px; padding-left: 14px; border-left: 3px solid var(--line); font-size: .9rem; }
.checks li.ok { border-color: var(--green); }
.checks li.bad { border-color: var(--red); }
.checks li span { color: var(--ink-2); overflow-wrap: anywhere; }
.checks li.bad span { color: var(--red); }

/* ---------- sheets, confirm, toasts ---------- */
.scrim { position: fixed; inset: 0; z-index: 20; background: rgba(8, 16, 18, .5); }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21;
  max-width: 560px; margin: 0 auto; max-height: 92dvh; max-height: 92vh;
  display: flex; flex-direction: column;
  background: var(--paper); border-radius: 18px 18px 0 0; box-shadow: var(--shadow);
  animation: rise .18s ease-out;
}
@supports (max-height: 92dvh) { .sheet { max-height: 92dvh; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 6px; }
.sheet-head h2 { font-size: 1.08rem; }
.sheet-body { padding: 0 16px calc(var(--safe-b) + 20px); overflow-y: auto; -webkit-overflow-scrolling: touch; }
.sheet-body > .link { margin-top: 10px; }
@keyframes rise { from { transform: translateY(24px); opacity: 0; } }
#confirm-root .scrim { z-index: 30; }
.confirm { position: fixed; z-index: 31; left: 16px; right: 16px; top: 50%; transform: translateY(-50%); max-width: 420px; margin: 0 auto; background: var(--surface); border-radius: 16px; padding: 18px; box-shadow: var(--shadow); }
#toasts { position: fixed; z-index: 40; left: 12px; right: 12px; top: calc(var(--safe-t) + 54px); display: grid; gap: 8px; justify-items: center; pointer-events: none; }
.toast { pointer-events: auto; max-width: 520px; border: 0; border-radius: 12px; padding: 11px 14px; text-align: left; font-size: .92rem; font-weight: 550; background: var(--ink); color: var(--paper); box-shadow: var(--shadow); }
.toast.ok { background: var(--green); color: #fff; }
.toast.error { background: var(--red); color: #fff; }
@media (prefers-color-scheme: dark) { .toast.ok, .toast.error { color: #101a1c; } }

@media (min-width: 700px) {
  .sheet { bottom: auto; top: 8vh; border-radius: 18px; max-height: 84vh; }
  .tabs { justify-content: center; grid-template-columns: repeat(5, 110px); }
}
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
