:root {
  --bg: #f5f3ee;
  --surface: #ffffff;
  --surface-2: #faf9f6;
  --text: #1d2327;
  --muted: #5d6870;
  --line: #e4e0d7;
  --primary: #0e5a5f;
  --primary-ink: #ffffff;
  --primary-soft: #e3f0ef;
  --red: #b42318;
  --red-soft: #fdecea;
  --amber: #a15c00;
  --amber-soft: #fff3dc;
  --blue: #1d5fa8;
  --blue-soft: #e7f0fb;
  --green: #1d7a46;
  --green-soft: #e5f5ec;
  --shadow: 0 1px 2px rgba(20, 30, 35, .06), 0 4px 16px rgba(20, 30, 35, .05);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12171a; --surface: #1b2226; --surface-2: #20282d; --text: #e8ecee; --muted: #9aa7ae; --line: #2e383e;
    --primary: #4fb3b0; --primary-ink: #08201f; --primary-soft: #19393a;
    --red: #ff8a7d; --red-soft: #3a1f1c; --amber: #f2b45a; --amber-soft: #3a2c14; --blue: #8ab8f0; --blue-soft: #1a2b40;
    --green: #6fd29b; --green-soft: #173323; --shadow: 0 1px 2px rgba(0,0,0,.3); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #12171a; --surface: #1b2226; --surface-2: #20282d; --text: #e8ecee; --muted: #9aa7ae; --line: #2e383e;
  --primary: #4fb3b0; --primary-ink: #08201f; --primary-soft: #19393a;
  --red: #ff8a7d; --red-soft: #3a1f1c; --amber: #f2b45a; --amber-soft: #3a2c14; --blue: #8ab8f0; --blue-soft: #1a2b40;
  --green: #6fd29b; --green-soft: #173323; --shadow: 0 1px 2px rgba(0,0,0,.3); color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.5 "Segoe UI", Roboto, -apple-system, BlinkMacSystemFont, "Noto Sans", Arial, sans-serif;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--primary); }
h1, h2, h3 { line-height: 1.2; margin: 0; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.hidden { display: none !important; }

/* ---------- boot / auth ---------- */
.boot { min-height: 100vh; display: grid; place-items: center; }
.spinner { width: 36px; height: 36px; border: 3px solid var(--line); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card { width: 100%; max-width: 400px; background: var(--surface); border-radius: 20px; padding: 32px 24px; box-shadow: var(--shadow); text-align: center; }
.auth-card .logo { width: 64px; height: 64px; margin: 0 auto 12px; }
.auth-card form { display: grid; gap: 12px; margin-top: 20px; text-align: left; }

/* ---------- layout ---------- */
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.side { background: var(--surface); border-right: 1px solid var(--line); padding: 20px 12px; position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 4px; }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 18px; font-weight: 700; font-size: 1.1rem; }
.brand img { width: 32px; height: 32px; }
.nav-btn { display: flex; align-items: center; gap: 12px; width: 100%; border: 0; background: none; padding: 11px 12px; border-radius: 10px; cursor: pointer; text-align: left; font-weight: 500; color: var(--muted); }
.nav-btn:hover { background: var(--surface-2); color: var(--text); }
.nav-btn[aria-current="page"] { background: var(--primary-soft); color: var(--primary); font-weight: 650; }
.nav-btn svg { width: 22px; height: 22px; flex: none; }
.nav-btn .count { margin-left: auto; background: var(--red); color: #fff; border-radius: 999px; font-size: .75rem; padding: 1px 8px; font-weight: 700; }
.main { padding: 28px 32px 48px; max-width: 1100px; width: 100%; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 20px; }
.tabbar { display: none; }

@media (max-width: 820px) {
  .shell { grid-template-columns: 1fr; }
  .side { display: none; }
  .main { padding: 18px 16px calc(96px + env(safe-area-inset-bottom)); }
  .tabbar { display: grid; grid-template-columns: repeat(4, 1fr); position: fixed; inset: auto 0 0 0; background: var(--surface); border-top: 1px solid var(--line); padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); z-index: 20; }
  .tabbar .nav-btn { flex-direction: column; gap: 2px; font-size: .72rem; padding: 6px 2px; text-align: center; justify-content: center; position: relative; }
  .tabbar .nav-btn .count { position: absolute; top: 2px; left: calc(50% + 6px); margin: 0; padding: 0 6px; }
  h1 { font-size: 1.35rem; }
}

/* ---------- components ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.stack { display: grid; gap: 16px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1 1 auto; min-width: 0; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; border: 1px solid var(--line); background: var(--surface); padding: 10px 16px; border-radius: 10px; cursor: pointer; font-weight: 600; min-height: 44px; white-space: nowrap; text-decoration: none; color: var(--text); }
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--red); }
.btn.ghost { border-color: transparent; background: none; }
.btn.sm { min-height: 36px; padding: 6px 12px; font-size: .9rem; }
.btn svg { width: 18px; height: 18px; }
.btn.block { width: 100%; }

label.field { display: grid; gap: 6px; font-weight: 600; font-size: .92rem; }
label.field .hint { font-weight: 400; color: var(--muted); font-size: .85rem; }
input[type=text], input[type=email], input[type=tel], input[type=password], input[type=number], input[type=date], input[type=search], select, textarea {
  width: 100%; padding: 11px 12px; border: 1px solid var(--line); background: var(--surface); border-radius: 10px; min-height: 44px; font-weight: 400;
}
textarea { min-height: 220px; resize: vertical; line-height: 1.55; }
input:focus, select:focus, textarea:focus, .btn:focus-visible, .nav-btn:focus-visible { outline: 3px solid color-mix(in srgb, var(--primary) 35%, transparent); outline-offset: 1px; border-color: var(--primary); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 600px) { .grid-2 { grid-template-columns: 1fr; } }

.switch { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 4px 0; cursor: pointer; }
.switch input { appearance: none; width: 50px; height: 30px; background: var(--line); border-radius: 999px; position: relative; flex: none; cursor: pointer; transition: background .2s; }
.switch input::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; transition: transform .2s; box-shadow: 0 1px 3px rgba(0,0,0,.25); }
.switch input:checked { background: var(--primary); }
.switch input:checked::after { transform: translateX(20px); }

.badge { display: inline-flex; align-items: center; gap: 4px; padding: 2px 10px; border-radius: 999px; font-size: .8rem; font-weight: 650; white-space: nowrap; }
.badge.overdue { background: var(--red-soft); color: var(--red); }
.badge.week { background: var(--amber-soft); color: var(--amber); }
.badge.month { background: var(--blue-soft); color: var(--blue); }
.badge.later { background: var(--surface-2); color: var(--muted); border: 1px solid var(--line); }
.badge.ok { background: var(--green-soft); color: var(--green); }
.badge.ai { background: var(--primary-soft); color: var(--primary); }

/* stats */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
@media (max-width: 820px) { .stats { grid-template-columns: repeat(2, 1fr); } }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; cursor: pointer; text-align: left; box-shadow: var(--shadow); }
.stat:hover { border-color: var(--primary); }
.stat .num { font-size: 1.9rem; font-weight: 750; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat .lbl { color: var(--muted); font-size: .88rem; font-weight: 550; }
.stat.overdue .num { color: var(--red); }
.stat.week .num { color: var(--amber); }
.stat.month .num { color: var(--blue); }

.callout { display: flex; gap: 14px; align-items: center; padding: 16px 18px; border-radius: var(--radius); background: var(--primary-soft); border: 1px solid color-mix(in srgb, var(--primary) 25%, transparent); flex-wrap: wrap; }
.callout.warn { background: var(--amber-soft); border-color: color-mix(in srgb, var(--amber) 30%, transparent); }
.callout .icon { font-size: 1.6rem; }
.callout .icon { flex: none; }
.callout .grow { flex: 1 1 0; min-width: 160px; }

/* client rows */
.list { display: grid; gap: 10px; }
.item { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; display: grid; grid-template-columns: 1fr auto; gap: 6px 16px; align-items: center; }
.item.overdue { border-left: 4px solid var(--red); }
.item.week { border-left: 4px solid var(--amber); }
.item.month { border-left: 4px solid var(--blue); }
.item.inactive { opacity: .6; }
.item .name { font-weight: 650; font-size: 1.05rem; cursor: pointer; }
.item .name:hover { color: var(--primary); }
.item .meta { color: var(--muted); font-size: .9rem; display: flex; gap: 6px 14px; flex-wrap: wrap; }
.item .side-info { text-align: right; display: grid; gap: 4px; justify-items: end; }
.item .amount { font-weight: 700; font-variant-numeric: tabular-nums; }
.item .actions { grid-column: 1 / -1; display: flex; gap: 8px; flex-wrap: wrap; margin-top: 4px; }
@media (max-width: 600px) { .item .actions .btn { flex: 1 1 auto; } }

.toolbar { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.toolbar input[type=search] { flex: 1 1 220px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 6px 14px; cursor: pointer; font-weight: 550; font-size: .9rem; min-height: 36px; }
.chip[aria-pressed="true"] { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }

/* mail */
.mail { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; display: grid; gap: 10px; box-shadow: var(--shadow); }
.mail .subject { font-weight: 650; }
.mail .body { white-space: pre-wrap; background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; font-size: .95rem; max-height: 260px; overflow: auto; }
.mail .err { color: var(--red); font-size: .9rem; }

.empty { text-align: center; padding: 40px 16px; color: var(--muted); }
.empty .big { font-size: 2.4rem; }

/* modal */
.modal-bg { position: fixed; inset: 0; background: rgba(10, 20, 25, .45); display: grid; place-items: center; padding: 16px; z-index: 50; animation: fade .15s ease; }
.modal { background: var(--surface); border-radius: 18px; width: 100%; max-width: 620px; max-height: calc(100vh - 32px); overflow: auto; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal.wide { max-width: 900px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 18px 20px 6px; gap: 12px; position: sticky; top: 0; background: var(--surface); z-index: 1; }
.modal-body { padding: 10px 20px 20px; display: grid; gap: 14px; }
.modal-foot { display: flex; gap: 10px; justify-content: flex-end; padding: 0 20px 20px; flex-wrap: wrap; }
@media (max-width: 600px) {
  .modal-bg { place-items: end stretch; padding: 0; }
  .modal { border-radius: 18px 18px 0 0; max-height: 92vh; }
  .modal-foot .btn { flex: 1 1 auto; }
}
@keyframes fade { from { opacity: 0; } }

table.preview { width: 100%; border-collapse: collapse; font-size: .88rem; }
table.preview th, table.preview td { border-bottom: 1px solid var(--line); padding: 6px 8px; text-align: left; white-space: nowrap; }
.table-wrap { overflow: auto; border: 1px solid var(--line); border-radius: 10px; }
.map-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }

#toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); display: grid; gap: 8px; z-index: 60; width: min(92vw, 420px); }
@media (max-width: 820px) { #toasts { bottom: calc(84px + env(safe-area-inset-bottom)); } }
.toast { background: var(--text); color: var(--bg); padding: 12px 16px; border-radius: 12px; box-shadow: var(--shadow); font-weight: 550; animation: fade .2s ease; }
.toast.error { background: var(--red); color: #fff; }

.section-title { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin: 26px 0 10px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; font-size: .95rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
details summary { cursor: pointer; font-weight: 600; color: var(--primary); }
.steps { margin: 0; padding-left: 20px; display: grid; gap: 6px; }
