:root {
  --bg: #F3F4EF; --surface: #FFFFFF; --surface-2: #EBEDE6; --text: #1B2320; --muted: #62716B;
  --border: #DCDFD6; --accent: #1F6F63; --accent-text: #FFFFFF; --accent-soft: #E3EEEB;
  --gold: #9A6316; --gold-soft: #F6ECDA; --danger: #B5482E; --danger-soft: #F5E3DE;
  --radius: 12px; --shadow: 0 1px 2px rgba(27,35,32,.06), 0 4px 14px rgba(27,35,32,.05);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #131C19; --surface: #1B2622; --surface-2: #22302B; --text: #ECEFEA; --muted: #9AABA3;
    --border: #2C3B36; --accent: #4FA593; --accent-text: #0A211B; --accent-soft: #223832;
    --gold: #D9A448; --gold-soft: #3A2F1C; --danger: #E28468; --danger-soft: #3A241E;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 6px 18px rgba(0,0,0,.28);
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-font-smoothing: antialiased; }
body { padding: 16px 16px 72px; }
.wrap { max-width: 960px; margin: 0 auto; }
.loading { color: var(--muted); padding: 40px 0; text-align: center; }
h1 { font-size: 22px; font-weight: 800; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 16px; font-weight: 700; margin: 0 0 12px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; }

.top { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.top .who { font-size: 13px; color: var(--muted); }
.top .actions { display: flex; gap: 6px; flex-wrap: wrap; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 14px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.card-head h2 { margin: 0; }

label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 5px; }
input, select, textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 9px; background: var(--surface);
  color: var(--text); font: inherit; font-size: 16px; font-variant-numeric: tabular-nums;
}
textarea { resize: vertical; min-height: 60px; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.field { margin-bottom: 12px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; }
.row > * { flex: 1; min-width: 140px; }

button {
  font: inherit; font-size: 15px; font-weight: 600; border-radius: 9px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); padding: 10px 16px; cursor: pointer; white-space: nowrap;
}
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.danger { background: transparent; color: var(--danger); border-color: var(--danger-soft); }
button.ghost { background: transparent; }
button.sm { padding: 6px 11px; font-size: 13px; }
a.btn { display: inline-block; font-weight: 600; font-size: 15px; border-radius: 9px; border: 1px solid var(--border); padding: 10px 16px; color: var(--text); text-decoration: none; }
button.link { background: none; border: none; padding: 0; color: var(--accent); text-decoration: underline; text-underline-offset: 2px; font-weight: 600; }
.block { width: 100%; }

.login { max-width: 360px; margin: 10vh auto 0; }
.login h1 { margin-bottom: 4px; }
.err { color: var(--danger); font-size: 14px; min-height: 20px; margin: 4px 0 8px; }

.period { display: flex; align-items: center; gap: 8px; }
.period .lbl { font-weight: 700; font-size: 15px; text-align: center; flex: 1; }
.period button { padding: 8px 14px; font-size: 18px; line-height: 1; }
.badge { display: inline-block; font-size: 12px; font-weight: 600; padding: 3px 8px; border-radius: 99px; background: var(--surface-2); color: var(--muted); }
.badge.lock { background: var(--gold-soft); color: var(--gold); }

.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; }
.stat { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; }
.stat .k { font-size: 12px; color: var(--muted); }
.stat .v { font-size: 18px; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; }
.stat.main { background: var(--accent-soft); }
.stat.main .v { color: var(--accent); font-size: 22px; }
.debt-note { margin-top: 10px; font-size: 13px; color: var(--muted); }

.qty-list { display: flex; flex-direction: column; gap: 8px; }
.qty-row { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 10px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface-2); }
.qty-row .nm { font-weight: 600; font-size: 15px; }
.qty-row .pr { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.stepper { display: flex; align-items: center; gap: 6px; }
.stepper button { width: 40px; height: 40px; padding: 0; font-size: 20px; }
.stepper input { width: 64px; text-align: center; padding: 8px 4px; }
.total-line { display: flex; justify-content: space-between; align-items: baseline; margin: 14px 0 4px; padding-top: 12px; border-top: 1px dashed var(--border); }
.total-line .v { font-size: 26px; font-weight: 800; color: var(--accent); font-variant-numeric: tabular-nums; }

.list { display: flex; flex-direction: column; }
.item { display: flex; justify-content: space-between; gap: 10px; padding: 11px 0; border-bottom: 1px solid var(--border); }
.item:last-child { border-bottom: none; }
.item .main { flex: 1; min-width: 0; }
.item .t { font-weight: 600; }
.item .d { font-size: 13px; color: var(--muted); margin-top: 2px; overflow-wrap: anywhere; }
.item .sum { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; text-align: right; }
.item .sum.minus { color: var(--danger); }
.item .sum.plus { color: var(--accent); }
.muted.small + .list, .card > .muted.small { margin-top: 8px; }
.item .acts { display: flex; gap: 6px; margin-top: 6px; justify-content: flex-end; }
.empty { color: var(--muted); font-size: 14px; padding: 6px 0; }

.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 11px; margin-bottom: 14px; overflow-x: auto; }
.tabs button { flex: 1; border: none; background: transparent; padding: 9px 12px; font-size: 14px; }
.tabs button.on { background: var(--surface); box-shadow: var(--shadow); }

.table-scroll { overflow-x: auto; margin: 0 -4px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 9px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; font-variant-numeric: tabular-nums; }
th { color: var(--muted); font-weight: 600; font-size: 12px; }
td.r, th.r { text-align: right; }
tbody tr.click { cursor: pointer; }
tbody tr.click:hover { background: var(--surface-2); }
tfoot td { font-weight: 700; border-bottom: none; }
td .neg { color: var(--danger); }
td .pos { color: var(--accent); font-weight: 700; }

.rate-row { display: grid; grid-template-columns: 1fr 110px auto; gap: 8px; align-items: center; margin-bottom: 8px; }
.rate-row.off { opacity: .5; }

.toolbar { display: flex; gap: 8px; flex-wrap: wrap; }
.back { margin-bottom: 10px; }

.toast { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%) translateY(20px); opacity: 0; background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px; transition: .2s; pointer-events: none; max-width: calc(100% - 32px); z-index: 50; }
.toast.on { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.bad { background: var(--danger); color: #fff; }

dialog { border: 1px solid var(--border); border-radius: 14px; padding: 0; width: min(440px, calc(100% - 24px)); background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(10,20,16,.45); }
#dlgBody { padding: 18px; }
.dlg-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; flex-wrap: wrap; }
.secret { font-family: ui-monospace, Menlo, monospace; font-size: 22px; font-weight: 700; letter-spacing: .04em; background: var(--surface-2); padding: 12px; border-radius: 10px; text-align: center; margin: 10px 0; user-select: all; }

@media (max-width: 560px) {
  body { padding: 12px 12px 72px; }
  .card { padding: 14px; }
  .hide-sm { display: none; }
}
.mb { margin-bottom: 14px; }
.card > .toolbar, .card .rate-row + .toolbar { margin-top: 8px; }
.dlg-actions + * { margin-top: 0; }
form .muted.small { margin-bottom: 10px; }
.period { margin-bottom: 14px; }

.grp-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 13px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 14px 0 6px; }
.grp-head:first-child { margin-top: 0; }
.qty-list .grp-head { margin: 8px 0 0; }
.qty-search { margin-bottom: 10px; }
.dirty { color: var(--gold); font-size: 14px; font-weight: 600; margin: 6px 0 10px; }
[hidden] { display: none !important; }
