:root {
  --bg: #f6f3ee;
  --surface: #ffffff;
  --surface-2: #efebe4;
  --text: #1d1f1e;
  --muted: #6b6f6c;
  --border: #e2ddd4;
  --primary: #1f6f63;
  --primary-text: #ffffff;
  --ok: #2f8f5b;
  --warn: #d98a12;
  --over: #c8382f;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 2px 8px rgb(0 0 0 / 0.04);
  --tabs-h: calc(60px + env(safe-area-inset-bottom));
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141716;
    --surface: #1d2120;
    --surface-2: #262b29;
    --text: #ecebe8;
    --muted: #9ea4a0;
    --border: #2f3533;
    --primary: #3fa592;
    --primary-text: #08110f;
    --ok: #4cb37a;
    --warn: #e9a23b;
    --over: #ec6a5f;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body { min-height: 100dvh; }

main {
  max-width: 560px;
  margin: 0 auto;
  padding: calc(12px + env(safe-area-inset-top)) 16px calc(var(--tabs-h) + 88px);
}

h1, h2, h3 { margin: 0; font-weight: 650; }
h2 { font-size: 1rem; color: var(--muted); font-weight: 600; margin: 24px 4px 8px; text-transform: uppercase; letter-spacing: 0.04em; font-size: 0.78rem; }

a { color: inherit; text-decoration: none; }
button { font: inherit; color: inherit; cursor: pointer; }

.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.neg { color: var(--ok); }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 48px;
}
.topbar h1 { font-size: 1.15rem; text-align: center; flex: 1; }
.topbar h1::first-letter { text-transform: uppercase; }
.icon-btn {
  width: 44px; height: 44px;
  border-radius: 50%;
  border: none;
  background: transparent;
  font-size: 1.4rem;
  display: grid; place-items: center;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn[disabled] { opacity: 0.3; }

/* ---------- Month summary ---------- */

.hero { padding: 20px; text-align: center; margin-top: 8px; }
.hero .total { font-size: 2.3rem; font-weight: 700; letter-spacing: -0.02em; }
.hero .sub { color: var(--muted); font-size: 0.9rem; margin-top: 2px; }

.payers { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 16px; }
.payers div { background: var(--surface-2); border-radius: 10px; padding: 8px 4px; }
.payers .name { font-size: 0.78rem; color: var(--muted); }
.payers .amt { font-weight: 600; font-size: 0.95rem; }

.cats { padding: 4px 0; }
.cat {
  display: block;
  width: 100%;
  border: none;
  background: none;
  text-align: left;
  padding: 12px 16px;
}
.cat + .cat { border-top: 1px solid var(--border); }
.cat.active { background: var(--surface-2); }
.cat .row { display: flex; align-items: center; gap: 10px; }
.cat .label { flex: 1; font-weight: 550; }
.cat .amt { font-weight: 600; }
.cat .budget { font-size: 0.8rem; color: var(--muted); margin-top: 4px; display: flex; justify-content: space-between; }
.cat .budget.over { color: var(--over); font-weight: 600; }

.bar { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; margin-top: 8px; }
.bar > i { display: block; height: 100%; border-radius: 4px; background: var(--ok); }
.bar.warn > i { background: var(--warn); }
.bar.over > i { background: var(--over); }

.dot {
  width: 34px; height: 34px;
  border-radius: 10px;
  display: grid; place-items: center;
  font-size: 1.05rem;
  flex: none;
}

/* ---------- Expense list ---------- */

.day { font-size: 0.8rem; color: var(--muted); margin: 16px 4px 6px; text-transform: capitalize; }
.list { overflow: hidden; }
.item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
}
.item + .item { border-top: 1px solid var(--border); }
.item:active { background: var(--surface-2); }
.item .info { flex: 1; min-width: 0; }
.item .title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 520; }
.item .meta { font-size: 0.8rem; color: var(--muted); }
.item .amt { font-weight: 600; white-space: nowrap; }

.empty { text-align: center; color: var(--muted); padding: 32px 16px; }

.filter-chip {
  display: inline-flex; align-items: center; gap: 6px;
  margin: 16px 4px 0;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 0.85rem;
}

/* ---------- FAB ---------- */

.fab {
  position: fixed;
  right: max(16px, calc(50% - 264px));
  bottom: calc(var(--tabs-h) + 16px);
  width: 60px; height: 60px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  color: var(--primary-text);
  font-size: 2rem;
  line-height: 1;
  display: grid;
  place-items: center;
  padding: 0 0 3px;
  box-shadow: 0 4px 14px rgb(0 0 0 / 0.25);
}

/* ---------- Tabs ---------- */

#tabs {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: var(--tabs-h);
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: center;
}
#tabs[hidden] { display: none; }
#tabs a {
  flex: 1;
  max-width: 180px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: 0.72rem;
  color: var(--muted);
}
#tabs a span { font-size: 1.3rem; filter: grayscale(1); opacity: 0.7; }
#tabs a.active { color: var(--primary); font-weight: 600; }
#tabs a.active span { filter: none; opacity: 1; }

/* ---------- Forms ---------- */

form { display: flex; flex-direction: column; gap: 16px; margin-top: 8px; }
label.field, div.field { display: flex; flex-direction: column; gap: 6px; font-size: 0.85rem; color: var(--muted); }
input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  width: 100%;
  min-height: 48px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; }

.amount-input {
  font-size: 2.2rem;
  font-weight: 700;
  text-align: center;
  padding: 16px;
}

.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--surface-2); border-radius: 10px; padding: 3px; }
.seg button { color: var(--text); border: none; background: none; padding: 9px; border-radius: 8px; font-size: 0.9rem; }
.seg button.on { background: var(--surface); box-shadow: var(--shadow); font-weight: 600; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  color: var(--text);
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  padding: 9px 14px;
  font-size: 0.92rem;
  min-height: 42px;
}
.chip.on { background: var(--primary); border-color: var(--primary); color: var(--primary-text); font-weight: 600; }

.btn {
  border: none;
  border-radius: 12px;
  padding: 14px;
  font-weight: 600;
  background: var(--primary);
  color: var(--primary-text);
  min-height: 50px;
}
.btn.secondary { background: var(--surface-2); color: var(--text); }
.btn.danger { background: transparent; color: var(--over); }
.btn[disabled] { opacity: 0.6; }

.photos { display: flex; flex-wrap: wrap; gap: 8px; }
.photo {
  position: relative;
  width: 84px; height: 84px;
  border-radius: 10px;
  overflow: hidden;
  background: var(--surface-2);
  display: grid; place-items: center;
  font-size: 0.75rem;
  color: var(--muted);
}
.photo img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo .rm {
  position: absolute; top: 3px; right: 3px;
  width: 26px; height: 26px;
  border-radius: 50%;
  border: none;
  background: rgb(0 0 0 / 0.6);
  color: #fff;
  font-size: 0.9rem;
  line-height: 1;
}
.photo.add { border: 2px dashed var(--border); background: none; font-size: 1.6rem; cursor: pointer; }
.photo.add input { display: none; }

.form-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }

.error { color: var(--over); font-size: 0.9rem; }

/* ---------- Settings ---------- */

.setting-row { display: flex; align-items: center; gap: 12px; padding: 10px 14px; }
.setting-row + .setting-row { border-top: 1px solid var(--border); }
.setting-row .label { flex: 1; }
.setting-row input { width: 110px; text-align: right; min-height: 42px; padding: 8px 10px; }
.euro-suffix { color: var(--muted); }

.rec-paused { opacity: 0.5; }

/* ---------- History ---------- */

.year-head { display: flex; justify-content: space-between; align-items: baseline; margin: 24px 4px 8px; }
.year-head h3 { font-size: 1.2rem; }
.month-row { display: block; padding: 12px 14px; }
.month-row + .month-row { border-top: 1px solid var(--border); }
.month-row .row { display: flex; justify-content: space-between; }
.month-row .name { text-transform: capitalize; font-weight: 550; }
.stack { display: flex; height: 6px; border-radius: 3px; overflow: hidden; margin-top: 8px; background: var(--surface-2); }
.stack i { display: block; height: 100%; }

/* ---------- Login ---------- */

.login { min-height: 80dvh; display: flex; flex-direction: column; justify-content: center; }
.login .logo { width: 72px; height: 72px; border-radius: 18px; margin: 0 auto 12px; display: block; }
.login h1 { text-align: center; margin-bottom: 8px; }

/* ---------- Viewer & toast ---------- */

.viewer {
  position: fixed; inset: 0;
  background: rgb(0 0 0 / 0.92);
  display: grid; place-items: center;
  z-index: 20;
}
.viewer img { max-width: 100%; max-height: 100%; }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabs-h) + 90px);
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 0.9rem;
  z-index: 30;
  max-width: calc(100% - 32px);
}

body.no-tabs main { padding-bottom: calc(16px + env(safe-area-inset-bottom)); }

#toast { display: flex; align-items: center; gap: 14px; }
#toast[hidden] { display: none; }
#toast button {
  border: none;
  background: none;
  color: var(--primary);
  font-weight: 700;
  padding: 4px 2px;
  text-transform: uppercase;
  font-size: 0.85rem;
}
@media (prefers-color-scheme: light) { #toast button { color: #7fd9c6; } }

/* ---------- Add: category grid ---------- */

.add-head { text-align: center; padding: 8px 0 4px; font-size: 0.95rem; color: var(--muted); }
.add-head span { text-transform: capitalize; }
.add-head b { color: var(--text); }

.pending {
  margin: 8px 0 0;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--surface-2);
  font-size: 0.85rem;
  text-align: center;
}
.pending[hidden] { display: none; }

.tiles {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 12px;
}
.tile {
  position: relative;
  min-height: max(104px, calc((100dvh - var(--tabs-h) - 150px - env(safe-area-inset-top)) / 4));
  border-radius: 18px;
  background: var(--c);
  color: #fff;
  padding: 14px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  overflow: hidden;
  box-shadow: 0 2px 6px rgb(0 0 0 / 0.12);
  transition: transform 0.08s;
}
.tile:active { transform: scale(0.97); }
.tile:last-child:nth-child(odd) { grid-column: span 2; }
.tile-icon {
  position: absolute;
  top: 10px; right: 12px;
  font-size: 2rem;
  filter: drop-shadow(0 1px 2px rgb(0 0 0 / 0.25));
}
.tile-name { font-size: 1.15rem; font-weight: 700; }
.tile-info { font-size: 0.82rem; opacity: 0.92; min-height: 1.2em; }
.tile-bar { height: 5px; border-radius: 3px; background: rgb(255 255 255 / 0.3); margin-top: 6px; overflow: hidden; }
.tile-bar i { display: block; height: 100%; background: #fff; border-radius: 3px; }
.tile-bar.over i { background: #ffd2cc; }

/* ---------- Add: amount screen ---------- */

.amount-screen {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: calc(100dvh - 28px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
}
.amt-head { display: flex; align-items: center; justify-content: space-between; }
.amt-cat {
  background: var(--c);
  color: #fff;
  font-weight: 700;
  padding: 8px 16px;
  border-radius: 999px;
}
.amt-display { text-align: center; }
.amt-display .value { font-size: 3rem; font-weight: 750; letter-spacing: -0.02em; line-height: 1.1; }
.amt-display .kind { font-size: 0.85rem; color: var(--ok); font-weight: 600; min-height: 1.2em; }

.payer-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.payer {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  border: 2px solid transparent;
  background: var(--surface);
  border-radius: 14px;
  padding: 8px 4px;
}
.payer .avatar {
  width: 40px; height: 40px;
  border-radius: 50%;
  display: grid; place-items: center;
  background: var(--surface-2);
  font-weight: 700;
  font-size: 1.15rem;
}
.payer .pname { font-size: 0.82rem; color: var(--muted); }
.payer.on { border-color: var(--c); }
.payer.on .avatar { background: var(--c); color: #fff; }
.payer.on .pname { color: var(--text); font-weight: 600; }

.meta-row { display: flex; gap: 6px; flex-wrap: wrap; }
.meta-row .chip { padding: 7px 12px; min-height: 38px; font-size: 0.88rem; display: inline-flex; align-items: center; gap: 4px; }
.meta-row .chip.on { background: var(--c); border-color: var(--c); color: #fff; }
.photo-btn { margin-left: auto; cursor: pointer; }
.date-other { position: relative; cursor: pointer; }
.date-other input {
  position: absolute; inset: 0;
  opacity: 0;
  width: 100%; min-height: 0;
  padding: 0; border: 0;
  pointer-events: none;
}

.label-row { display: flex; flex-direction: column; gap: 8px; }
.label-row input { min-height: 44px; padding: 10px 12px; }
.label-chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; margin: 0 -16px; padding: 0 16px; }
.label-chips::-webkit-scrollbar { display: none; }
.label-chips .chip { white-space: nowrap; padding: 6px 12px; min-height: 34px; font-size: 0.85rem; }
.label-chips .chip.on { background: var(--c); border-color: var(--c); color: #fff; }

.keypad {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: minmax(52px, 1fr);
  gap: 8px;
  margin-top: auto;
}
.keypad button {
  border: none;
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--shadow);
  font-size: 1.6rem;
  font-weight: 550;
  font-variant-numeric: tabular-nums;
}
.keypad button:active { background: var(--surface-2); }
.keypad button.fn { background: var(--surface-2); font-size: 1.35rem; }
.amount-screen .submit { background: var(--c); color: #fff; font-size: 1.05rem; min-height: 56px; }
.amount-screen .submit[disabled] { opacity: 0.45; }

/* Short phones: keep the whole amount screen visible without scrolling. */
@media (max-height: 720px) {
  .amount-screen { gap: 8px; }
  .amt-head .icon-btn { height: 36px; }
  .amt-cat { padding: 6px 14px; }
  .amt-display .value { font-size: 2.4rem; }
  .payer { flex-direction: row; justify-content: center; gap: 6px; padding: 6px 4px; }
  .payer .avatar { width: 30px; height: 30px; font-size: 0.95rem; }
  .keypad { grid-auto-rows: minmax(46px, 1fr); gap: 6px; }
  .amount-screen .submit { min-height: 50px; }
}
