:root {
  --bg: #f7f4ef;
  --surface: #ffffff;
  --surface-2: #faf8f4;
  --border: #e7e1d7;
  --text: #2a2622;
  --muted: #7a7168;
  --primary: #b3261e;
  --primary-hover: #95201a;
  --primary-soft: #fbeceb;
  --green: #2f7d4f;
  --green-soft: #e6f3ea;
  --amber: #b26a00;
  --amber-soft: #fdf1dc;
  --blue: #2a5fa8;
  --blue-soft: #e7eefa;
  --shadow: 0 1px 2px rgba(42, 38, 34, .05), 0 4px 16px rgba(42, 38, 34, .06);
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171513;
    --surface: #211e1b;
    --surface-2: #1c1a17;
    --border: #35302b;
    --text: #f1ece6;
    --muted: #a39a90;
    --primary: #e5544b;
    --primary-hover: #f06a61;
    --primary-soft: #3a1f1d;
    --green: #6cc28f;
    --green-soft: #1d3326;
    --amber: #f0b45a;
    --amber-soft: #3a2c16;
    --blue: #7fa8e8;
    --blue-soft: #1c2840;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: Inter, system-ui, -apple-system, Segoe UI, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
h1, h2, h3 { font-family: Fraunces, Georgia, serif; font-weight: 700; margin: 0; letter-spacing: -.01em; }
a { color: var(--blue); }
.muted { color: var(--muted); }
.error { color: var(--primary); font-size: 14px; margin: 12px 0 0; }
.spacer { flex: 1; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* Controls */
input, select, textarea {
  font: inherit;
  color: inherit;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 12px;
  width: 100%;
  min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
textarea { resize: vertical; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 500; }

.btn {
  font: inherit;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 9px 16px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s, border-color .15s, opacity .15s;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .55; cursor: progress; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--surface-2); border-color: var(--border); }
.btn-danger-ghost { background: transparent; border-color: transparent; color: var(--primary); }
.btn-danger-ghost:hover { background: var(--primary-soft); }
.btn-small { padding: 5px 12px; font-size: 13px; }
.btn-icon { padding: 6px 9px; }
.btn-block { width: 100%; }

/* Loading + login */
.center-screen { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.spinner { width: 32px; height: 32px; border: 3px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 36px 28px;
  box-shadow: var(--shadow);
  text-align: center;
}
.login-icon { font-size: 44px; line-height: 1; margin-bottom: 12px; }
.login-card h1 { font-size: 28px; }
.login-card .muted { margin: 4px 0 24px; }
.divider { display: flex; align-items: center; gap: 12px; margin: 20px 0; color: var(--muted); font-size: 13px; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 24px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { font-family: Fraunces, Georgia, serif; font-weight: 700; font-size: 20px; white-space: nowrap; }
.topbar-actions { display: flex; align-items: center; gap: 8px; min-width: 0; }
.topbar-actions select { width: auto; max-width: 180px; padding: 7px 10px; }
.user { display: flex; align-items: center; gap: 4px; font-size: 13px; }

/* Stats */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; padding: 20px 24px 0; max-width: 1280px; margin: 0 auto; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.stat-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; font-weight: 600; }
.stat-value { font-family: Fraunces, Georgia, serif; font-size: 26px; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; }
.stat-sub { font-size: 12px; color: var(--muted); }

/* Layout */
.layout { display: grid; grid-template-columns: 290px 1fr; gap: 20px; padding: 20px 24px 48px; max-width: 1280px; margin: 0 auto; align-items: start; }
.sidebar { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; position: sticky; top: 76px; }
.sidebar-head { display: flex; justify-content: space-between; align-items: center; padding: 2px 4px 10px; }
.sidebar-head h2 { font-size: 18px; }
.people-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; max-height: calc(100vh - 170px); overflow-y: auto; }
.person-btn {
  width: 100%; text-align: left; font: inherit; color: inherit; cursor: pointer;
  background: transparent; border: 1px solid transparent; border-radius: 10px; padding: 10px 12px;
  display: flex; flex-direction: column; gap: 6px;
}
.person-btn:hover { background: var(--surface-2); }
.person-btn.active { background: var(--primary-soft); border-color: color-mix(in srgb, var(--primary) 30%, transparent); }
.person-top { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.person-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.person-spend { font-size: 13px; font-variant-numeric: tabular-nums; color: var(--muted); white-space: nowrap; }
.person-meta { font-size: 12px; color: var(--muted); }
.bar { height: 5px; background: var(--border); border-radius: 99px; overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--green); border-radius: 99px; }
.bar.over > span { background: var(--primary); }
.empty-side { color: var(--muted); font-size: 14px; padding: 12px 4px; }

/* Person view */
.person-view { min-width: 0; }
.person-header {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 20px 22px; display: flex; gap: 16px; justify-content: space-between; align-items: flex-start; flex-wrap: wrap;
}
.person-header h2 { font-size: 28px; }
.person-notes { margin: 6px 0 0; color: var(--muted); white-space: pre-wrap; max-width: 60ch; }
.person-budget { font-size: 14px; margin-top: 10px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.person-budget .bar { width: 160px; }
.header-actions { display: flex; gap: 8px; }

.section { margin-top: 22px; }
.section-head { display: flex; align-items: baseline; justify-content: space-between; margin: 0 4px 10px; }
.section-head h3 { font-size: 18px; }
.section-head .muted { font-size: 13px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 12px; }
.empty { background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius); padding: 22px; text-align: center; color: var(--muted); font-size: 14px; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; display: flex; flex-direction: column; gap: 10px; box-shadow: var(--shadow);
}
.card-top { display: flex; justify-content: space-between; gap: 10px; align-items: flex-start; }
.card-title { font-weight: 600; font-size: 16px; word-break: break-word; }
.card-sub { font-size: 13px; color: var(--muted); display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.card-sub a { text-decoration: none; }
.card-sub a:hover { text-decoration: underline; }
.card-notes { font-size: 14px; color: var(--muted); white-space: pre-wrap; margin: 0; }
.price { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; text-align: right; }
.price small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; }
.card-actions { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.status-select { width: auto; padding: 4px 8px; font-size: 13px; border-radius: 99px; }

.pill { display: inline-flex; align-items: center; gap: 4px; padding: 2px 9px; border-radius: 99px; font-size: 12px; font-weight: 600; }
.pill-idea { background: var(--amber-soft); color: var(--amber); }
.pill-bought { background: var(--blue-soft); color: var(--blue); }
.pill-arrived, .pill-wrapped, .pill-given { background: var(--green-soft); color: var(--green); }

/* Tracking */
.tracking { background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; font-size: 13px; }
.tracking-head { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.tracking-num { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; }
.tracking-status { font-weight: 600; white-space: nowrap; }
.tracking-status.ok { color: var(--green); }
.tracking-status.warn { color: var(--primary); }
.steps { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; margin: 10px 0 6px; }
.step { height: 6px; border-radius: 99px; background: var(--border); }
.step.done { background: var(--green); }
.step.current { background: var(--blue); animation: pulse 1.6s ease-in-out infinite; }
.steps.warn .step.current { background: var(--primary); animation: none; }
@keyframes pulse { 50% { opacity: .5; } }
.step-labels { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; font-size: 11px; color: var(--muted); }
.step-labels span:nth-child(2), .step-labels span:nth-child(3) { text-align: center; }
.step-labels span:last-child { text-align: right; }
.tracking-summary { margin: 8px 0 0; }
.tracking-foot { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 6px; color: var(--muted); font-size: 12px; }
.tracking details { margin-top: 8px; }
.tracking summary { cursor: pointer; color: var(--muted); font-size: 12px; }
.events { list-style: none; margin: 8px 0 0; padding: 0 0 0 12px; border-left: 2px solid var(--border); display: flex; flex-direction: column; gap: 8px; max-height: 240px; overflow-y: auto; }
.events time { display: block; font-size: 11px; color: var(--muted); }

.welcome { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 48px 24px; text-align: center; }
.welcome div { font-size: 44px; }
.welcome h2 { margin: 8px 0 6px; }
.welcome p { color: var(--muted); margin: 0 0 18px; }

/* Dialog */
dialog {
  border: 1px solid var(--border); border-radius: 18px; padding: 22px; width: min(520px, calc(100vw - 32px));
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
}
dialog::backdrop { background: rgba(20, 16, 12, .45); backdrop-filter: blur(2px); }
dialog h3 { font-size: 22px; margin-bottom: 4px; }
.dialog-actions { display: flex; gap: 8px; align-items: center; margin-top: 6px; }

/* Toast */
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 100;
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 10px; font-size: 14px;
  box-shadow: var(--shadow); max-width: calc(100vw - 32px);
}

/* Mobile */
@media (max-width: 860px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding: 16px 16px 40px; }
  .layout > * { min-width: 0; }
  .topbar-actions select { max-width: 140px; }
  .stats { grid-template-columns: repeat(2, 1fr); padding: 16px 16px 0; }
  .sidebar { position: static; }
  .people-list { flex-direction: row; overflow-x: auto; max-height: none; padding-bottom: 4px; }
  .people-list li { flex: 0 0 200px; }
  .topbar { padding: 10px 16px; }
  .hide-sm { display: none; }
  .cards { grid-template-columns: 1fr; }
}
@media (max-width: 480px) {
  .brand span { display: none; }
  .row { grid-template-columns: 1fr; }
  .stat-value { font-size: 22px; }
}

/* Card thumbnail */
.card-main { display: flex; gap: 12px; align-items: flex-start; min-width: 0; }
.thumb { width: 56px; height: 56px; border-radius: 8px; object-fit: cover; flex: 0 0 56px; background: var(--surface-2); border: 1px solid var(--border); }
.card-main > div { min-width: 0; }

/* Settings */
dialog.wide { width: min(620px, calc(100vw - 32px)); }
.dialog-title-row { display: flex; justify-content: space-between; align-items: center; }
.settings-section { border-top: 1px solid var(--border); padding-top: 14px; display: flex; flex-direction: column; gap: 10px; }
.settings-section h4 { margin: 0; font-size: 15px; }
.settings-section p { margin: 0; }
.small { font-size: 13px; }
.copy-row { display: flex; gap: 8px; }
.copy-row input { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.btn-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
a.btn { text-decoration: none; }
label.check { flex-direction: row; align-items: flex-start; gap: 8px; font-weight: 400; font-size: 14px; flex-wrap: wrap; }
label.check input { width: auto; margin-top: 3px; }
.token-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.token-list li { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 12px; border: 1px solid var(--border); border-radius: 10px; font-size: 13px; background: var(--surface-2); }
.token-list .muted { font-size: 12px; }
.highlight { animation: glow 1.2s ease-in-out 3; }
@keyframes glow { 50% { box-shadow: 0 0 0 4px color-mix(in srgb, var(--primary) 35%, transparent); } }

/* Total − postage breakdown in the present form */
.money-calc { display: grid; grid-template-columns: 1fr auto; gap: 2px 16px; font-size: 13px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; margin-top: -2px; font-variant-numeric: tabular-nums; }
.money-calc:empty { display: none; }
.money-calc .num { text-align: right; }
.money-calc .strong { font-weight: 600; border-top: 1px solid var(--border); padding-top: 4px; margin-top: 2px; }
.money-calc .error { grid-column: 1 / -1; margin: 0; }
.stat-split { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
