@font-face {
  font-family: "Figtree";
  src: url("fonts/figtree.woff2") format("woff2");
  font-weight: 300 900;
  font-display: swap;
}

:root {
  --bg: #F6F1E7;
  --surface: #FFFDF8;
  --surface-2: #EFE8DA;
  --ink: #2A2420;
  --muted: #6A5F52;
  --line: #DDD2BF;
  --accent: #2A2420;
  --accent-ink: #FFFDF8;
  --vakil: #1D5FB4;
  --vakil-soft: #DCE9F9;
  --mehbod: #1B7A3E;
  --mehbod-soft: #DCF1E2;
  --danger: #A32A1E;
  --warn-soft: #FBEFD0;
  --warn-ink: #6B4D00;
  --radius: 16px;
  --shadow: 0 1px 2px rgb(60 45 25 / .08);
  --nav-h: 68px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1D1A16;
    --surface: #282420;
    --surface-2: #35302A;
    --ink: #F4EEE3;
    --muted: #B6AB9B;
    --line: #443D35;
    --accent: #F4EEE3;
    --accent-ink: #1D1A16;
    --vakil: #7DB4F5;
    --vakil-soft: #1C3350;
    --mehbod: #6FD08F;
    --mehbod-soft: #1C3D28;
    --danger: #F08A7D;
    --warn-soft: #40341A;
    --warn-ink: #F1D48A;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 400 16px/1.45 "Figtree", system-ui, -apple-system, "Segoe UI", sans-serif;
  overflow-wrap: anywhere;
}
h1, h2, h3, p { margin: 0; }
button, input, select { font: inherit; color: inherit; }
button, label.btn, select { cursor: pointer; touch-action: manipulation; }
a { color: inherit; }
:focus-visible { outline: 3px solid var(--ink); outline-offset: 2px; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.grow { flex: 1; min-width: 0; }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap;
}

/* Spinner: always moves, per the house rule; reduced motion only slows it down. */
.spinner {
  display: inline-block; flex: none;
  width: 18px; height: 18px;
  border: 2.5px solid color-mix(in srgb, currentColor 25%, transparent);
  border-top-color: currentColor;
  border-radius: 50%;
  animation: spin .8s linear infinite;
  vertical-align: -3px;
}
.spinner-lg { width: 38px; height: 38px; border-width: 4px; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }
.boot { min-height: 100dvh; display: grid; place-items: center; color: var(--muted); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 10px 18px;
  border: 1.5px solid var(--line); border-radius: 12px;
  background: var(--surface); font-weight: 600; text-decoration: none; text-align: center;
  transition: background-color .15s, border-color .15s;
}
.btn:hover { border-color: var(--muted); }
.btn:disabled { opacity: .7; cursor: default; }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn-primary:hover { border-color: var(--accent); opacity: .92; }
.btn-danger { color: var(--danger); border-color: currentColor; }
.btn-block { width: 100%; }
.btn-small { min-height: 44px; padding: 8px 12px; font-size: 15px; }
.btn svg, .icon-btn svg { width: 20px; height: 20px; flex: none; }
.icon-btn {
  width: 44px; height: 44px; flex: none;
  display: inline-grid; place-items: center;
  background: none; border: 0; border-radius: 12px;
}
.icon-btn:hover { background: var(--surface-2); }

/* Language and login gate */
.gate { min-height: 100dvh; display: flex; flex-direction: column; justify-content: center; gap: 24px; padding: 32px 16px; max-width: 460px; margin: 0 auto; }
.gate h1 { font-size: 26px; font-weight: 800; letter-spacing: -.01em; text-align: center; }
.gate-mark { display: grid; place-items: center; color: var(--ink); }
.gate-mark svg { width: 56px; height: 56px; }
.lang-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.lang-btn {
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  padding: 18px 8px; min-height: 124px;
  background: var(--surface); border: 1.5px solid var(--line); border-radius: var(--radius);
  font-weight: 600; box-shadow: var(--shadow); transition: border-color .15s;
}
.lang-btn:hover, .lang-btn[aria-current="true"] { border-color: var(--ink); }
.flag { display: block; width: 84px; height: 56px; border-radius: 6px; box-shadow: 0 0 0 1px rgb(0 0 0 / .12); }
.flag-small { width: 30px; height: 20px; border-radius: 3px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span, .label { font-size: 14px; font-weight: 600; color: var(--muted); }
.input {
  width: 100%; min-height: 48px; padding: 10px 12px;
  background: var(--surface); border: 1.5px solid var(--line); border-radius: 12px;
  font-size: 16px; /* below 16px iOS zooms the page on focus */
}
.input:focus { border-color: var(--ink); outline: none; }
.form { display: flex; flex-direction: column; gap: 14px; }
.error-text { color: var(--danger); font-weight: 600; font-size: 15px; }

/* Shell */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 4px;
  padding: 6px 8px 6px 16px; padding-top: max(6px, env(safe-area-inset-top));
  background: var(--bg); border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 18px; font-weight: 800; letter-spacing: -.01em; flex: 1; min-width: 0; }
main { max-width: 680px; margin: 0 auto; padding: 16px 16px calc(var(--nav-h) + 96px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 12px; }
.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; align-items: stretch; justify-content: center;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--line);
}
.nav-btn {
  flex: 1; max-width: 200px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  background: none; border: 0; color: var(--muted); font-size: 13px; font-weight: 600;
}
.nav-btn svg { width: 24px; height: 24px; }
.nav-btn[aria-current="page"] { color: var(--ink); font-weight: 800; }
.fab {
  position: fixed; z-index: 11; right: max(16px, calc(50vw - 324px));
  bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom));
  display: inline-flex; align-items: center; gap: 8px;
  min-height: 56px; padding: 12px 20px;
  background: var(--accent); color: var(--accent-ink);
  border: 0; border-radius: 28px; font-weight: 700; font-size: 17px;
  box-shadow: 0 4px 14px rgb(40 30 15 / .28);
}
.fab svg { width: 22px; height: 22px; }

/* Settlement */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.settle { padding: 16px; display: flex; flex-direction: column; gap: 14px; }
.settle-final { font-size: 23px; line-height: 1.2; font-weight: 800; letter-spacing: -.01em; }
.settle-final .amount { display: block; font-size: 34px; margin-top: 2px; }
.settle-table { display: grid; grid-template-columns: 1fr auto auto; gap: 6px 16px; align-items: baseline; font-size: 15px; }
.settle-table .head { font-weight: 700; text-align: right; }
.settle-table .num { text-align: right; }
.v { color: var(--vakil); }
.m { color: var(--mehbod); }
.notice { padding: 10px 12px; background: var(--warn-soft); color: var(--warn-ink); border-radius: 10px; font-size: 14px; font-weight: 600; }

/* Item row */
.thumb { display: block; flex: none; object-fit: contain; background: #fff; border: 1px solid var(--line); border-radius: 10px; }
.thumb-item { width: 64px; height: 64px; }
.thumb-line { width: 72px; height: 72px; }
.thumb-logo { padding: 9px; }
.icon-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(48px, 1fr)); gap: 6px; }
.icon-choice { display: grid; place-items: center; height: 48px; background: var(--surface); border: 1.5px solid var(--line); border-radius: 10px; }
.icon-choice svg { width: 26px; height: 26px; }
.icon-choice[aria-pressed="true"], .icon-auto[aria-pressed="true"] { border-color: var(--ink); background: var(--surface-2); box-shadow: inset 0 0 0 1px var(--ink); }
.icon-tile { display: grid; place-items: center; background: var(--surface-2); color: var(--ink); }
.icon-tile svg { width: 36px; height: 36px; }
.thumb-link { flex: none; display: block; border-radius: 10px; }
.item-head {
  width: 100%; display: flex; align-items: flex-start; gap: 8px;
  padding: 14px 8px 8px 16px; background: none; border: 0; text-align: left;
  border-radius: var(--radius) var(--radius) 0 0;
}
.item-name { font-weight: 700; font-size: 17px; line-height: 1.25; hyphens: auto; overflow-wrap: break-word; }
.item-total { font-weight: 800; font-size: 17px; }
.chev { width: 44px; height: 24px; flex: none; transition: transform .2s; color: var(--muted); margin-left: -8px; }
[aria-expanded="true"] .chev { transform: rotate(180deg); }
.item-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; padding: 0 16px 10px; font-size: 14px; color: var(--muted); }
.receipt-link {
  display: inline-flex; align-items: center; gap: 5px; min-height: 44px; margin: -10px 0;
  font-weight: 600; color: var(--ink); text-underline-offset: 3px;
}
.receipt-link svg { width: 16px; height: 16px; flex: none; }
.split { padding: 0 16px 14px; display: flex; flex-direction: column; gap: 8px; }
.split-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.split-side { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.split-side > label { font-size: 14px; font-weight: 700; }
.pct-input { display: flex; align-items: center; gap: 4px; }
.pct-input input {
  width: 100%; min-width: 0; min-height: 48px; padding: 8px 10px; text-align: right;
  background: var(--surface); border: 1.5px solid var(--line); border-radius: 12px;
  font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums;
}
.pct-input input:focus { outline: none; border-color: currentColor; }
.pct-input input.invalid { border-color: var(--danger); color: var(--danger); }
.share { font-size: 15px; font-weight: 600; }
.bar { display: flex; height: 8px; border-radius: 4px; overflow: hidden; background: var(--surface-2); }
.bar > .bar-v { background: var(--vakil); transition: width .2s; }
.bar > .bar-m { background: var(--mehbod); flex: 1; }
.save-state { min-height: 20px; font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.save-state .spinner { width: 14px; height: 14px; border-width: 2px; }

.item-body { border-top: 1px solid var(--line); padding: 12px 16px 16px; display: flex; flex-direction: column; gap: 12px; }
.chips { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.line { padding: 12px 0; border-top: 1px solid var(--line); display: flex; flex-direction: column; gap: 8px; }
.line:first-child { border-top: 0; padding-top: 0; }
.line-top { display: flex; gap: 10px; align-items: flex-start; }
.line-desc { font-weight: 600; }
.line-sub { font-size: 14px; color: var(--muted); }
.line-actions { display: flex; gap: 8px; align-items: center; }
.line-actions select { flex: 1; min-width: 0; min-height: 44px; padding: 8px 10px; background: var(--surface); border: 1.5px solid var(--line); border-radius: 10px; font-size: 16px; }
.item-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.span-2 { grid-column: span 2; }
.empty { padding: 32px 20px; text-align: center; color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: 10px; }
.empty svg { width: 44px; height: 44px; }
.section-title { font-size: 15px; font-weight: 700; color: var(--muted); margin-top: 4px; }

/* Expense row */
.exp { padding: 14px 16px; display: flex; flex-direction: column; gap: 8px; }
.exp-top { display: flex; gap: 10px; align-items: flex-start; }
.exp-name { font-weight: 700; }
.exp-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.processing { display: flex; align-items: center; gap: 10px; font-weight: 600; color: var(--muted); }
.party-tag { font-weight: 700; }

/* Sheets */
dialog.sheet {
  width: 100%; max-width: 560px; max-height: 92dvh; margin: auto auto 0; padding: 0;
  border: 0; border-radius: 20px 20px 0 0;
  background: var(--surface); color: var(--ink);
  overflow-y: auto; overscroll-behavior: contain;
}
@media (min-width: 640px) { dialog.sheet { margin: auto; border-radius: 20px; } }
dialog.sheet::backdrop { background: rgb(25 18 8 / .5); }
.sheet-head { position: sticky; top: 0; z-index: 1; display: flex; align-items: center; gap: 8px; padding: 10px 8px 10px 18px; background: var(--surface); }
.sheet-head h2 { flex: 1; font-size: 19px; font-weight: 800; }
.sheet-body { padding: 4px 18px calc(18px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 14px; }
.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.seg-btn { min-height: 52px; border: 2px solid var(--line); border-radius: 12px; background: var(--surface); font-weight: 700; }
.seg-btn[aria-pressed="true"].seg-v { border-color: var(--vakil); background: var(--vakil-soft); color: var(--vakil); }
.seg-btn[aria-pressed="true"].seg-m { border-color: var(--mehbod); background: var(--mehbod-soft); color: var(--mehbod); }
.big-action { min-height: 64px; justify-content: flex-start; padding: 12px 16px; font-size: 17px; gap: 12px; }
.big-action svg { width: 26px; height: 26px; }
.big-action input, .file-pick input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.big-action:focus-within, .file-pick:focus-within { outline: 3px solid var(--ink); outline-offset: 2px; }
.lang-list { display: flex; flex-direction: column; gap: 8px; }
.lang-row { display: flex; align-items: center; gap: 12px; min-height: 56px; padding: 8px 14px; border: 1.5px solid var(--line); border-radius: 12px; background: var(--surface); font-weight: 600; text-align: left; }
.lang-row[aria-current="true"] { border-color: var(--ink); }

#toast {
  position: fixed; z-index: 100; left: 16px; right: 16px; top: max(12px, env(safe-area-inset-top));
  max-width: 560px; margin: 0 auto;
  padding: 12px 16px; border-radius: 12px;
  background: var(--danger); color: #fff; font-weight: 600;
}
@media (prefers-color-scheme: dark) { #toast { color: #1D1A16; } }
