:root {
    --bg: #f4f5f7;
    --card: #ffffff;
    --text: #1b1d21;
    --muted: #6b7280;
    --border: #e3e5e9;
    --input: #f8f9fb;
    --accent: #1f7a4d;
    --accent-text: #ffffff;
    --danger: #c2410c;
    --warn: #b45309;
    --shadow: 0 1px 2px rgb(0 0 0 / .05), 0 2px 8px rgb(0 0 0 / .04);
    --radius: 14px;
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #14161a;
        --card: #1e2127;
        --text: #eceef1;
        --muted: #9aa1ab;
        --border: #2e323a;
        --input: #171a1e;
        --accent: #34b37a;
        --accent-text: #0d1f16;
        --danger: #f97362;
        --warn: #f2b84b;
        --shadow: none;
        color-scheme: dark;
    }
}

:root[data-theme="dark"] {
    --bg: #14161a;
    --card: #1e2127;
    --text: #eceef1;
    --muted: #9aa1ab;
    --border: #2e323a;
    --input: #171a1e;
    --accent: #34b37a;
    --accent-text: #0d1f16;
    --danger: #f97362;
    --warn: #f2b84b;
    --shadow: none;
    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.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    -webkit-tap-highlight-color: transparent;
}

h1, h2 { margin: 0 0 12px; line-height: 1.2; }
h1 { font-size: 24px; }
h2 { font-size: 18px; }

.wrap {
    max-width: 640px;
    margin: 0 auto;
    padding: 12px 16px 120px;
}

.muted { color: var(--muted); }
.small { font-size: 13px; margin: 8px 0 0; }
.error { color: var(--danger); margin: 0; }

/* ---------- Шапка ---------- */
.topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    padding-top: max(8px, env(safe-area-inset-top));
    background: var(--bg);
    border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 18px; color: var(--text); text-decoration: none; }
.who { color: var(--muted); font-size: 14px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.actions { display: flex; gap: 4px; }
.actions form { margin: 0; }

/* ---------- Карточки ---------- */
.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 16px;
    margin-bottom: 12px;
}

/* ---------- Поля и кнопки ---------- */
input, textarea, select {
    width: 100%;
    min-width: 0;
    font: inherit;
    font-size: 17px; /* ≥16px — iOS не зумит */
    color: var(--text);
    background: var(--input);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px 12px;
    outline: none;
}
input:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent); }
input::placeholder, textarea::placeholder { color: var(--muted); opacity: .7; }
textarea { resize: vertical; }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-height: 44px;
    padding: 0 16px;
    font: inherit;
    font-weight: 600;
    color: var(--text);
    background: var(--input);
    border: 1px solid var(--border);
    border-radius: 10px;
    cursor: pointer;
    white-space: nowrap;
    text-decoration: none;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.danger { color: var(--danger); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.big { min-height: 52px; font-size: 17px; padding: 0 22px; }
.btn.grow { flex: 1 1 140px; }
.btn.link { background: none; border: 0; color: var(--muted); font-weight: 500; min-height: 36px; padding: 0; margin-top: 8px; }

.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    flex: none;
    font-size: 18px;
    color: var(--text);
    background: none;
    border: 0;
    border-radius: 10px;
    cursor: pointer;
    text-decoration: none;
}
.icon-btn:hover { background: var(--input); }
.icon-btn.danger { color: var(--danger); font-size: 16px; }

.row { display: flex; gap: 8px; align-items: center; margin-top: 10px; }
.row.end { justify-content: flex-end; }
.row.center { justify-content: center; }
.row.wrap-row { flex-wrap: wrap; }

.field { display: block; margin-bottom: 14px; }
.field span { display: block; font-size: 14px; color: var(--muted); margin-bottom: 6px; }

/* ---------- Вход ---------- */
.login { max-width: 360px; margin: 12vh auto 0; padding: 24px; }
.login h1 { text-align: center; margin-bottom: 20px; }
.login .btn { width: 100%; margin-top: 8px; }
.login .error { margin-bottom: 8px; }

/* ---------- Купюры ---------- */
.notes { display: grid; gap: 8px; }
.note-row {
    display: grid;
    grid-template-columns: 58px 44px 1fr 44px minmax(72px, auto);
    gap: 6px;
    align-items: center;
}
.nom {
    font-weight: 700;
    font-size: 17px;
    text-align: right;
    padding-right: 4px;
    font-variant-numeric: tabular-nums;
}
.note-row.coins { grid-template-columns: auto 1fr; margin-top: 4px; }
.note-row.coins .nom { font-size: 14px; font-weight: 600; }
.qty { text-align: center; font-weight: 600; font-variant-numeric: tabular-nums; }
.step {
    height: 44px;
    font-size: 22px;
    line-height: 1;
    color: var(--text);
    background: var(--input);
    border: 1px solid var(--border);
    border-radius: 10px;
    cursor: pointer;
    touch-action: manipulation;
}
.step:active { background: var(--border); }
.sub { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; font-size: 15px; }
.sub.has { color: var(--text); font-weight: 600; }

.total-line { margin-top: 12px; padding-top: 10px; border-top: 1px dashed var(--border); text-align: right; }
.total-line b { font-size: 18px; font-variant-numeric: tabular-nums; }

.money { font-variant-numeric: tabular-nums; }

/* ---------- Люди и расходы ---------- */
.list { display: grid; gap: 8px; }
.list:empty { display: none; }
.list:not(:empty) + .btn { margin-top: 8px; }
.person-row, .expense-row { display: grid; gap: 6px; align-items: center; }
.person-row { grid-template-columns: 1fr 110px 40px; }
.expense-row { grid-template-columns: 1fr 110px 40px; }
.person-info { min-width: 0; }
.add-person { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 10px; }
.add-person #new-person { grid-column: 1 / -1; }
.add-person .money { text-align: right; }
.person-name { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.person-last { display: block; font-size: 12px; }
.person-amount, .expense-amount { text-align: right; }

/* ---------- Фото ---------- */
.photo-preview { position: relative; margin-top: 12px; }
.photo-preview img { display: block; width: 100%; max-height: 360px; object-fit: contain; border-radius: 10px; background: var(--input); }
.photo-preview .icon-btn { position: absolute; top: 8px; right: 8px; background: var(--card); box-shadow: var(--shadow); }

/* ---------- Итог ---------- */
.summary div { display: flex; justify-content: space-between; padding: 4px 0; font-variant-numeric: tabular-nums; }
.summary span { color: var(--muted); }
.summary .grand { border-top: 1px solid var(--border); margin-top: 6px; padding-top: 10px; font-size: 20px; }
.summary .grand span { color: var(--text); font-weight: 600; }

.sendbar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    gap: 12px;
    max-width: 640px;
    margin: 0 auto;
    padding: 10px 16px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    background: color-mix(in srgb, var(--bg) 92%, transparent);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border-top: 1px solid var(--border);
}
.sendbar-total { flex: 1; line-height: 1.15; }
.sendbar-total small { display: block; color: var(--muted); font-size: 12px; }
.sendbar-total b { font-size: 22px; font-variant-numeric: tabular-nums; }

/* ---------- Тост ---------- */
.toast {
    position: fixed;
    left: 50%;
    bottom: 96px;
    z-index: 30;
    transform: translateX(-50%);
    max-width: calc(100% - 32px);
    padding: 12px 16px;
    border-radius: 12px;
    background: var(--text);
    color: var(--bg);
    box-shadow: 0 6px 24px rgb(0 0 0 / .2);
    text-align: center;
}
.toast.error { background: var(--danger); color: #fff; }

/* ---------- Диалоги ---------- */
dialog.sheet {
    width: min(440px, calc(100% - 32px));
    padding: 20px;
    color: var(--text);
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}
dialog::backdrop { background: rgb(0 0 0 / .45); }
.reminder-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.reminder-list li { display: flex; align-items: center; justify-content: space-between; padding: 6px 6px 6px 12px; background: var(--input); border-radius: 10px; font-size: 18px; font-variant-numeric: tabular-nums; }
.reminder-list:empty::after { content: "Пока нет напоминаний"; color: var(--muted); font-size: 14px; }
#reminder-time { width: auto; flex: 1; }

.alarm {
    position: fixed;
    inset: 0;
    z-index: 50;
    display: grid;
    place-items: center;
    padding: 16px;
    background: rgb(0 0 0 / .55);
}
.alarm[hidden] { display: none; }
.alarm-box { background: var(--card); border-radius: 20px; padding: 28px 20px; text-align: center; width: min(400px, 100%); }
.alarm-icon { font-size: 56px; animation: ring 1s ease-in-out infinite; }
.alarm .row { flex-wrap: wrap; }
@keyframes ring { 0%, 100% { transform: rotate(0); } 25% { transform: rotate(-15deg); } 75% { transform: rotate(15deg); } }

/* ---------- История отчётов ---------- */
.page-title { margin: 8px 0 16px; }
.report summary { list-style: none; display: flex; justify-content: space-between; align-items: center; gap: 12px; cursor: pointer; }
.report summary::-webkit-details-marker { display: none; }
.report-head { display: grid; gap: 2px; }
.report-head .muted { font-size: 14px; }
.report-sum { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.report-body { margin-top: 12px; border-top: 1px solid var(--border); padding-top: 8px; }
.report-body p { margin: 10px 0 4px; }
.report-body ul { margin: 0; padding-left: 20px; color: var(--muted); }
.report-photo { display: block; max-width: 100%; max-height: 320px; border-radius: 10px; margin: 10px 0; }
.badge { display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 50%; font-size: 13px; margin-left: 6px; }
.badge.ok { background: color-mix(in srgb, var(--accent) 20%, transparent); color: var(--accent); }
.badge.warn { background: color-mix(in srgb, var(--warn) 20%, transparent); color: var(--warn); }

@media (max-width: 380px) {
    .wrap { padding-left: 12px; padding-right: 12px; }
    .note-row { grid-template-columns: 48px 40px 1fr 40px minmax(64px, auto); gap: 4px; }
    .person-row, .expense-row { grid-template-columns: 1fr 96px 36px; }
    .btn.big { padding: 0 16px; }
}
