:root {
    --bg: #25170e; --bg2: #140b06; --card: rgba(255,255,255,.06); --line: rgba(255,255,255,.17);
    --ink: #fdf6f0; --muted: #dfc6b3; --accent: #f3a96f; --accent-ink: #3a1d08; --field: rgba(255,255,255,.08);
    --soft: rgba(243,169,111,.14); --to-me: #4ade80; --i-owe: #fb8a8a; --warn-bg: rgba(251,138,138,.13); --bad: #fda4af;
    color-scheme: dark;
}
@media (prefers-color-scheme: light) {
    :root { --bg: #fdf7f2; --bg2: #f6e9de; --card: #fff; --line: #ecd8c8; --ink: #2a170b; --muted: #6b4a33;
            --accent: #9c4a17; --accent-ink: #fff; --field: #fffaf6; --soft: #fbeadc; --to-me: #15803d; --i-owe: #c0262d; --warn-bg: #fdeceb; --bad: #b91c1c; color-scheme: light; }
}
:root[data-theme="light"] { --bg: #fdf7f2; --bg2: #f6e9de; --card: #fff; --line: #ecd8c8; --ink: #2a170b; --muted: #6b4a33;
            --accent: #9c4a17; --accent-ink: #fff; --field: #fffaf6; --soft: #fbeadc; --to-me: #15803d; --i-owe: #c0262d; --warn-bg: #fdeceb; --bad: #b91c1c; color-scheme: light; }
:root[data-theme="dark"] {
    --bg: #25170e; --bg2: #140b06; --card: rgba(255,255,255,.06); --line: rgba(255,255,255,.17);
    --ink: #fdf6f0; --muted: #dfc6b3; --accent: #f3a96f; --accent-ink: #3a1d08; --field: rgba(255,255,255,.08);
    --soft: rgba(243,169,111,.14); --to-me: #4ade80; --i-owe: #fb8a8a; --warn-bg: rgba(251,138,138,.13); --bad: #fda4af;
    color-scheme: dark;
}
* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; padding: clamp(1rem,4vw,2.5rem) 1rem 4rem; display: flex; justify-content: center;
       font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--ink);
       background: var(--bg2) linear-gradient(180deg, var(--bg), var(--bg2) 900px) no-repeat; touch-action: manipulation; }
.app { width: min(560px, 100%); min-width: 0; display: flex; flex-direction: column; }
.col-main { display: contents; }
.app > .topbar, .app > .back { order: 0; }
.app > header { order: 1; }
.totals { order: 2; }
.add { order: 3; }
.list { order: 4; }
.tools { order: 5; }
.app > .faq { order: 6; }
.app > footer { order: 7; }
.report { display: none; }
.vh { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; margin: 0; }
header { text-align: center; margin-bottom: 1.1rem; }
h1 { margin: 0; font-size: 1.8rem; line-height: 1.2; letter-spacing: -.02em; }
.sub { margin: .4rem 0 0; color: var(--muted); font-size: 1rem; }
h2 { margin: 0 0 .7rem; font-size: 1.15rem; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 1.1rem; margin-bottom: .9rem; min-width: 0; }
label, legend { display: block; margin: .9rem 0 .35rem; font-size: .9rem; font-weight: 600; color: var(--muted); padding: 0; }
.add h2 + label { margin-top: 0; }
input[type=text], input[type=date], input[type=search], select, textarea { width: 100%; min-width: 0; min-height: 48px; padding: .6rem .8rem; font: inherit; font-size: 1.05rem; color: var(--ink);
       background: var(--field); border: 1px solid var(--line); border-radius: 12px; }
textarea { min-height: 7.5rem; resize: vertical; line-height: 1.45; font-size: 1rem; }
#amount { font-size: 1.5rem; font-weight: 700; font-variant-numeric: tabular-nums; }
input[type=date] { -webkit-appearance: none; appearance: none; display: block; }
input[type=date]::-webkit-date-and-time-value { text-align: left; }
input[aria-invalid="true"] { border-color: var(--bad); }
select option { color: #111; background: #fff; }
input:focus-visible, button:focus-visible, select:focus-visible, summary:focus-visible, a:focus-visible, textarea:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.kind input:focus-visible + span { outline: 3px solid var(--accent); outline-offset: 2px; }
::placeholder { color: var(--muted); opacity: .75; }
.hint { margin: .45rem 0 0; color: var(--muted); font-size: .88rem; overflow-wrap: anywhere; }
.hint:empty { margin: 0; }
.hint.new { color: var(--ink); }
[hidden] { display: none !important; }
.field { min-width: 0; }

/* what happened: four big choices */
.kinds { border: 0; margin: 0; padding: 0; min-width: 0; display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: .5rem; }
.kinds legend { grid-column: 1 / -1; float: left; width: 100%; }
.kind { margin: 0; display: block; position: relative; cursor: pointer; }
.kind input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.kind span { display: flex; align-items: center; justify-content: center; min-height: 52px; height: 100%; padding: .4rem .6rem; border: 2px solid var(--line); border-radius: 12px;
       background: var(--field); color: var(--ink); font-size: .95rem; font-weight: 700; text-align: center; line-height: 1.25; }
.kind input:checked + span { border-color: var(--accent); background: var(--soft); }
.kind input[value="borrowed"]:checked + span, .kind input[value="paid"]:checked + span { border-color: var(--i-owe); background: var(--warn-bg); }

.amount-row { display: flex; align-items: stretch; gap: .4rem; }
.cur-tag { display: flex; align-items: center; padding: 0 .7rem; border: 1px solid var(--line); border-radius: 12px; background: var(--field); font-weight: 700; color: var(--muted); flex: none; }
.preview { margin: .9rem 0 0; padding: .55rem .75rem; border-radius: 12px; background: var(--field); border: 1px dashed var(--line); font-weight: 600; overflow-wrap: anywhere; }
.preview:empty { display: none; }

/* buttons */
.primary, .secondary, .danger { display: flex; align-items: center; justify-content: center; width: 100%; min-height: 48px; padding: .5rem .8rem; border-radius: 12px;
       font: inherit; font-weight: 700; text-align: center; text-decoration: none; cursor: pointer; line-height: 1.2; overflow-wrap: anywhere; }
.primary { border: 0; background: var(--accent); color: var(--accent-ink); }
.add .primary { margin-top: 1rem; min-height: 54px; font-size: 1.1rem; }
.secondary { border: 1px solid var(--line); background: var(--field); color: var(--ink); font-weight: 600; }
.secondary.small, .primary.small, .danger.small { width: auto; min-height: 40px; padding: 0 .8rem; font-size: .9rem; margin: 0; }
.danger { border: 1px solid var(--bad); background: transparent; color: var(--bad); font-weight: 600; margin-top: .9rem; }
.link { display: block; min-height: 44px; margin-top: .3rem; padding: 0 .1rem; border: 0; background: none; color: var(--accent); font: inherit; font-size: .95rem; font-weight: 600; text-align: left; cursor: pointer; }
.link.inline { display: inline; min-height: 0; margin: 0; padding: .55rem .2rem; text-decoration: underline; }
button:disabled { opacity: .45; cursor: default; }
.when-line { margin: .5rem 0 0; color: var(--muted); }
.when-line b { color: var(--ink); }

.err { margin: .4rem 0 0; color: var(--bad); font-weight: 600; font-size: .92rem; overflow-wrap: anywhere; }
.err:empty { margin: 0; }
.msg { margin: 0; color: var(--muted); font-size: .95rem; overflow-wrap: anywhere; }
.msg.show { margin-top: .8rem; padding: .6rem .75rem; border-radius: 12px; background: var(--soft); color: var(--ink); }
.list .msg.show { margin: 0 0 .8rem; }
.msg .undo { margin-left: .2rem; font-weight: 700; }

/* totals */
.tot-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: .6rem; }
.tot { border: 2px solid var(--line); border-radius: 14px; padding: .6rem .75rem; background: var(--field); min-width: 0; display: flex; flex-direction: column; }
.tot.to-me { border-color: var(--to-me); }
.tot.i-owe { border-color: var(--i-owe); }
.t-label { font-size: .85rem; font-weight: 700; color: var(--muted); }
.t-val { font-size: clamp(1.05rem, 5vw, 2rem); font-weight: 800; line-height: 1.2; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.to-me .t-val { color: var(--to-me); } .i-owe .t-val { color: var(--i-owe); }
.t-sub { font-size: .85rem; color: var(--muted); overflow-wrap: anywhere; }
.late { margin: .7rem 0 0; padding: .5rem .7rem; border-radius: 12px; background: var(--warn-bg); font-weight: 700; border-left: 5px solid var(--i-owe); }

/* people list: each person in their own box */
.list-top { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .4rem .8rem; margin-bottom: .6rem; }
.list-top h2 { margin: 0; }
.sort-field { display: flex; align-items: center; gap: .5rem; min-width: 0; }
.sort-field label { margin: 0; }
.sort-field select { width: auto; min-height: 42px; padding: .3rem .6rem; font-size: .95rem; }
#find-wrap { margin-bottom: .7rem; }
.empty { margin: .2rem 0 0; color: var(--muted); text-align: center; }
.people { display: grid; gap: .75rem; }
.person { border: 1px solid var(--line); border-left: 6px solid var(--pc, var(--line)); border-radius: 14px; padding: .8rem .85rem .85rem; background: var(--field); min-width: 0; }
.person.to-me { --pc: var(--to-me); } .person.i-owe { --pc: var(--i-owe); }
.p-name { margin: 0; font-size: 1.05rem; font-weight: 800; overflow-wrap: anywhere; }
.p-bal { margin: .15rem 0 0; font-size: 1.35rem; font-weight: 800; line-height: 1.25; color: var(--pc, var(--muted)); overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }
.person.settled .p-bal { font-size: 1.05rem; }
.p-due { margin: .3rem 0 0; font-weight: 600; font-size: .92rem; overflow-wrap: anywhere; }
.p-due.late-d { color: var(--bad); }
.p-meta { margin: .2rem 0 0; font-size: .88rem; color: var(--muted); overflow-wrap: anywhere; }
.p-btns { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: .45rem; margin-top: .75rem; }
.p-btns button { min-height: 44px; font-size: .92rem; }
.panel { margin-top: .75rem; padding: .7rem .7rem .75rem; border: 1px solid var(--line); border-radius: 12px; background: var(--card); min-width: 0; }
.panel h3 { margin: 0 0 .4rem; font-size: .95rem; }
.panel label:first-of-type { margin-top: 0; }
.row-btns { display: flex; flex-wrap: wrap; gap: .45rem; margin-top: .6rem; }
.row-btns > button { flex: 1 1 6rem; }
.hist { list-style: none; margin: 0; padding: 0; display: grid; gap: .45rem; }
.h-row { border: 1px solid var(--line); border-radius: 10px; padding: .45rem .6rem; background: var(--field); min-width: 0;
         display: grid; grid-template-columns: minmax(0, 1fr) auto; column-gap: .6rem; align-items: center; }
.h-main { min-width: 0; }
.h-what { font-weight: 700; overflow-wrap: anywhere; }
.h-sub { font-size: .85rem; color: var(--muted); overflow-wrap: anywhere; }
.h-btns { display: flex; gap: .35rem; }
.h-edit { display: block; outline: 3px solid var(--accent); outline-offset: 1px; }
.h-edit .kinds { margin-top: .2rem; }
.h-edit .kind span { min-height: 44px; font-size: .88rem; }
.settled { margin-top: .9rem; }
.settled summary { cursor: pointer; min-height: 44px; display: flex; align-items: center; font-weight: 700; color: var(--muted); }
.settled .people { margin-top: .4rem; }

/* questions */
.faq { margin-top: 1.2rem; }
.faq details { border: 1px solid var(--line); border-radius: 12px; margin: 0 0 .5rem; background: var(--card); }
.faq summary { cursor: pointer; padding: .75rem .9rem; font-weight: 600; min-height: 44px; }
.faq details p { margin: 0; padding: 0 .9rem .8rem; color: var(--muted); }
footer { margin-top: 1.4rem; text-align: center; font-size: .9rem; }
footer a { color: var(--muted); }
.tool-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: .5rem; }

.back { display: inline-flex; align-items: center; gap: .4rem; min-height: 44px; margin: 0 0 .5rem; padding: 0 1rem 0 .8rem; border: 1px solid var(--line, rgba(127,127,127,.4)); border-radius: 999px; color: var(--muted, var(--mut, inherit)); font-size: .92rem; font-weight: 600; text-decoration: none; background: transparent; }
.back:hover { color: var(--ink, inherit); border-color: currentColor; }
.back:focus-visible { outline: 3px solid var(--accent, var(--acc, #fff)); outline-offset: 2px; }

/* narrow phones */
@media (max-width: 400px) {
    .tool-grid { grid-template-columns: minmax(0, 1fr); }
    .person { padding: .7rem .65rem .75rem; }
    .panel { padding: .6rem .55rem .65rem; }
}
@media (max-width: 359px) {
    .p-btns { grid-template-columns: minmax(0, 1fr); }
    .h-row { grid-template-columns: minmax(0, 1fr); }
    .h-btns { margin-top: .4rem; }
    .h-btns button { flex: 1; }
    .kind span { font-size: .88rem; }
}
@media (max-width: 360px) {
    .topbar .theme-toggle { padding: 0; width: 44px; justify-content: center; }
    .topbar .theme-toggle span { display: none; }
}

/* printing: only the report */
@media print {
    body { background: #fff; color: #000; padding: 0; display: block; font-size: 11pt; }
    .app > *:not(.report) { display: none !important; }
    .app { display: block; width: 100%; }
    .report { display: block !important; }
    .report h2 { font-size: 16pt; margin: 0 0 .3rem; }
    .report h3 { font-size: 12pt; margin: .9rem 0 .2rem; }
    .report p { margin: .1rem 0; }
    .report table { width: 100%; border-collapse: collapse; margin-top: .3rem; font-size: 9.5pt; }
    .report th, .report td { border: 1px solid #999; padding: 2px 5px; text-align: left; }
    .report thead { display: table-header-group; }
    .report tr { break-inside: avoid; }
}

/* laptops and desktops: totals across the top, adding and settings on the left, the people on the right */
@media (min-width: 900px) {
    .app { width: min(1140px, 100%); display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.25fr); column-gap: 1.5rem; align-items: start; }
    .app > * { grid-column: 1 / -1; }
    .col-main { display: block; min-width: 0; }
    .app > .topbar { grid-row: 1; }
    .app > header { grid-row: 2; }
    .app > .totals { grid-row: 3; }
    .app > .col-main { grid-column: 1; grid-row: 4; }
    .app > .list { grid-column: 2; grid-row: 4; }
    .app > .faq { grid-row: 5; width: min(760px, 100%); justify-self: center; }
    .app > footer { grid-row: 6; width: min(760px, 100%); justify-self: center; }
    .app > .report { grid-row: 7; }
    header h1 { font-size: 2.3rem; }
    .t-val { font-size: 2.2rem; }
    .tot { padding: .8rem 1rem; }
}
