/* Shared components. Only tokens from tokens.css are used for colour,
   radius, shadow, font and motion. */

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  background: var(--page-texture), var(--bg);
  background-attachment: fixed;
  color: var(--text);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { font-family: var(--font-head); font-weight: var(--head-weight); line-height: 1.2; margin: 0; letter-spacing: -.01em; }
h1 { font-size: clamp(1.5rem, 4vw, 2rem); }
h2 { font-size: 1.05rem; }
p { margin: 0; }
a { color: var(--accent-text); }
button, input, select, textarea { font: inherit; color: inherit; }
.num, .money, td.amount { font-family: var(--font-num); font-variant-numeric: tabular-nums; font-feature-settings: "tnum"; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

:focus { outline: none; }
:focus-visible { outline: none; box-shadow: var(--focus); border-radius: var(--radius-btn); }

.skip-link { position: absolute; left: 12px; top: -48px; z-index: 100; background: var(--surface); color: var(--text); padding: 10px 14px; border-radius: var(--radius-btn); }
.skip-link:focus { top: 12px; }

/* ---- top bar + nav ---------------------------------------------------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px;
  padding: 10px max(16px, calc((100vw - 1200px) / 2));
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.header-line { height: 2px; background: var(--header-line); opacity: .9; }
.brand { display: flex; align-items: center; gap: 8px; min-height: var(--tap); font-family: var(--font-head); font-weight: var(--head-weight); white-space: nowrap; text-decoration: none; color: var(--text); }
.brand-mark { width: 22px; height: 22px; border-radius: 30%; background: conic-gradient(from 200deg, var(--person-perry), var(--person-joint), var(--person-errin), var(--person-perry)); }
.topbar-actions { display: flex; align-items: center; gap: 8px; margin-left: auto; }
.topbar-actions form { margin: 0; }

.nav ul { list-style: none; margin: 0; padding: 0; display: flex; gap: 4px; }
.nav-link {
  position: relative; display: flex; align-items: center; gap: 6px;
  min-height: var(--tap); padding: 0 14px;
  color: var(--muted); text-decoration: none; font-weight: 600; font-size: .95rem;
  border-radius: var(--radius-btn);
  transition: color var(--dur) var(--ease), background var(--dur) var(--ease);
}
.nav-link:hover { color: var(--text); background: var(--surface-2); }
.nav-link.is-current { color: var(--text); background: var(--surface-2); box-shadow: inset 0 -2px 0 var(--accent); }
.nav-icon { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.mine-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); box-shadow: var(--glow); }

@media (min-width: 761px) {
  .nav .nav-icon { display: none; }
}
@media (max-width: 760px) {
  body { padding-bottom: calc(68px + env(safe-area-inset-bottom)); }
  /* backdrop-filter would make the top bar the containing block for the fixed nav */
  .topbar { gap: 8px; padding: 8px 16px; backdrop-filter: none; -webkit-backdrop-filter: none; background: var(--bg); }
  .nav {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    background: var(--surface); border-top: var(--border-w) solid var(--border);
    padding: 4px 4px calc(4px + env(safe-area-inset-bottom));
    box-shadow: 0 -6px 20px rgba(0, 0, 0, .08);
  }
  .nav ul { justify-content: space-around; }
  .nav li { flex: 1; }
  .nav-link { flex-direction: column; justify-content: center; gap: 2px; min-height: 56px; padding: 4px; font-size: .75rem; }
  .nav-link.is-current { box-shadow: none; color: var(--accent-text); }
  .mine-dot { position: absolute; top: 8px; right: calc(50% - 18px); }
}

/* ---- buttons ---------------------------------------------------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 0 18px;
  border: var(--border-w) solid var(--border); border-radius: var(--radius-btn);
  background: var(--surface); color: var(--text);
  font-weight: 700; font-size: .95rem; cursor: pointer; text-decoration: none; white-space: nowrap;
  transition: transform var(--dur) var(--ease), box-shadow var(--dur) var(--ease), background var(--dur) var(--ease), border-color var(--dur) var(--ease);
}
.btn:hover { border-color: var(--accent); }
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--accent); border-color: transparent; color: var(--on-accent); box-shadow: var(--glow); }
.btn-primary:hover { border-color: transparent; filter: brightness(1.06); }
[data-theme="errin"] .btn-primary { background: linear-gradient(135deg, #F9A8D4, #F472B6 60%, #E879C7); box-shadow: 0 6px 16px rgba(244, 114, 182, .35); }
[data-theme="shared"] .btn-primary { background: linear-gradient(90deg, color-mix(in srgb, var(--person-perry) 55%, var(--person-joint)), var(--person-joint), color-mix(in srgb, var(--person-errin) 55%, var(--person-joint))); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-ghost:hover { color: var(--text); background: var(--surface-2); border-color: transparent; }
.btn-danger { color: var(--danger); }
.btn-danger.is-confirming { background: var(--danger); color: var(--surface); border-color: transparent; }
.btn-sm { min-height: 36px; padding: 0 12px; font-size: .85rem; }
/* Fingers need bigger targets than mouse pointers. */
@media (pointer: coarse) {
  .btn-sm { min-height: var(--tap); padding: 0 14px; }
  .btn:active, .linklike:active { filter: brightness(.94); }
}
.icon-btn { width: var(--tap); padding: 0; }
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.fab {
  position: fixed; right: 16px; bottom: calc(80px + env(safe-area-inset-bottom)); z-index: 25;
  width: 60px; height: 60px; border-radius: var(--radius-btn); min-height: 0;
  font-size: 2rem; line-height: 1; padding: 0;
  box-shadow: var(--shadow-lift);
}
[data-theme="perry"] .fab { border-radius: 14px; }
[data-theme="errin"] .fab { border-radius: 50%; }
[data-theme="shared"] .fab { border-radius: 18px; }
@media (min-width: 761px) { .fab { display: none; } }
@media (max-width: 760px) { .add-btn { display: none; } }

/* ---- cards ------------------------------------------------------------ */
.card {
  background: var(--surface);
  border: var(--border-w) solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 20px;
  min-width: 0;
}
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.card-head h2 { padding-top: 6px; }
.card-sub { color: var(--muted); font-size: .85rem; margin-top: 2px; }
[data-theme="perry"] .card-head h2 { text-transform: none; letter-spacing: .01em; font-size: .95rem; }
[data-theme="perry"] .card { position: relative; }
[data-theme="perry"] .card::before { content: ""; position: absolute; inset: 0 auto auto 0; width: 24px; height: 1px; background: var(--accent); box-shadow: var(--glow); border-radius: var(--radius-lg) 0 0 0; }

/* ---- chips + badges --------------------------------------------------- */
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px; border-radius: var(--chip-radius);
  font-size: .8rem; font-weight: 700; line-height: 1.4; white-space: nowrap;
  background: var(--surface-2); color: var(--text); border: var(--border-w) solid var(--border);
}
.chip-ok { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, transparent); background: color-mix(in srgb, var(--ok) 12%, transparent); }
.chip-warn { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 45%, transparent); background: color-mix(in srgb, var(--warn) 12%, transparent); }
.chip-danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, transparent); background: color-mix(in srgb, var(--danger) 12%, transparent); }
[data-theme="perry"] .chip { font-family: var(--font-num); font-size: .72rem; letter-spacing: .02em; font-weight: 600; }
[data-theme="perry"] .chip::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; box-shadow: 0 0 6px currentColor; }

/* category chip: Errin gets sticker-style pastels */
.cat-chip { font-weight: 600; }
[data-theme="errin"] .cat-chip {
  background: var(--sticker, #FFE4EF); border: 2px solid var(--surface); color: #4A3040;
  box-shadow: 0 2px 6px rgba(244, 114, 182, .18); transform: rotate(var(--tilt, 0deg));
}
.cat-icon { width: 18px; height: 18px; flex: none; display: none; }
[data-theme="errin"] .cat-icon { display: inline-block; }

.person-dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: var(--person-joint); flex: none; }
.person-dot[data-person="Perry"] { background: var(--person-perry); }
.person-dot[data-person="Errin"] { background: var(--person-errin); }

/* ---- form controls ---------------------------------------------------- */
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > label, .field > .label { font-size: .85rem; font-weight: 700; color: var(--muted); }
.input, select.input, textarea.input {
  width: 100%; min-height: var(--tap); padding: 10px 12px;
  background: var(--surface); color: var(--text);
  border: var(--border-w) solid var(--border); border-radius: calc(var(--radius) * .6);
  font-size: 1rem;
  transition: border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}
[data-theme="errin"] .input { border-radius: 14px; }
.input:focus-visible { border-color: var(--accent); box-shadow: var(--focus); border-radius: calc(var(--radius) * .6); }
[data-theme="perry"] .input:focus-visible { box-shadow: inset 0 0 0 1px var(--accent), inset 0 0 12px rgba(34, 211, 238, .18); }
textarea.input { min-height: 80px; resize: vertical; }
.input-money { position: relative; }
.input-money::before { content: "£"; position: absolute; left: 14px; top: 50%; transform: translateY(-50%); color: var(--muted); font-weight: 700; font-size: 1.4rem; font-family: var(--font-num); }
.input-money .input { padding-left: 34px; font-size: 1.6rem; font-family: var(--font-num); font-weight: var(--num-weight); min-height: 60px; }
.field-error { color: var(--danger); font-size: .85rem; font-weight: 600; }
.field-hint { color: var(--muted); font-size: .8rem; }

/* Category picker: one-tap chips instead of a dropdown. */
.cat-picker { border: 0; padding: 0; margin: 0; min-width: 0; }
.cat-picker legend { font-size: .85rem; font-weight: 700; color: var(--muted); margin-bottom: 8px; padding: 0; }
.cat-chips { display: flex; flex-wrap: wrap; gap: 8px; }
.cat-chips label { position: relative; }
.cat-chips input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; }
.cat-chips span {
  display: inline-flex; align-items: center; gap: 6px; min-height: 40px; padding: 0 14px;
  border-radius: var(--chip-radius); border: var(--border-w) solid var(--border); background: var(--surface);
  font-weight: 700; font-size: .9rem; white-space: nowrap;
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease);
}
.cat-chips input:checked + span { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 18%, var(--surface)); box-shadow: inset 0 0 0 1px var(--accent); }
.cat-chips input:focus-visible + span { box-shadow: var(--focus); }
[data-theme="errin"] .cat-chips span { border-radius: 999px; }

.more-details { border: var(--border-w) solid var(--border); border-radius: calc(var(--radius) * .6); background: var(--surface-2); }
[data-theme="errin"] .more-details { border-radius: 16px; }
.more-details summary { list-style: none; display: flex; align-items: center; justify-content: space-between; min-height: var(--tap); padding: 0 14px; cursor: pointer; font-weight: 700; color: var(--muted); font-size: .9rem; }
.more-details summary::-webkit-details-marker { display: none; }
.more-details summary::after { content: "+"; font-size: 1.2rem; }
.more-details[open] summary::after { content: "−"; }
.more-details-body { display: grid; gap: 14px; padding: 4px 14px 14px; }

.segmented { display: flex; gap: 6px; flex-wrap: wrap; border: 0; padding: 0; margin: 0; }
.segmented legend { font-size: .85rem; font-weight: 700; color: var(--muted); margin-bottom: 6px; padding: 0; }
.segmented label { flex: 1; position: relative; }
.segmented input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.segmented span {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 0 12px; border-radius: var(--radius-btn);
  border: var(--border-w) solid var(--border); background: var(--surface); font-weight: 700; font-size: .95rem;
}
.segmented input:checked + span { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 16%, var(--surface)); }
.segmented input:focus-visible + span { box-shadow: var(--focus); }

/* ---- segmented budget bar + legend (dashboard and hub) ---- */
.segbar { display: flex; height: 22px; border-radius: 11px; overflow: hidden; background: var(--progress-track); }
.segbar > span { height: 100%; width: 0; transition: width var(--dur-slow) var(--ease); }
.seg-paid { background: var(--accent); }
.seg-committed {
  background: repeating-linear-gradient(-45deg, color-mix(in srgb, var(--accent) 40%, transparent) 0 6px, transparent 6px 10px);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--accent) 50%, transparent);
}
.seg-over { background: repeating-linear-gradient(-45deg, var(--danger) 0 6px, color-mix(in srgb, var(--danger) 50%, transparent) 6px 10px); }
.legend { display: flex; gap: 8px 18px; flex-wrap: wrap; font-size: .85rem; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.swatch { width: 14px; height: 14px; border-radius: 4px; display: inline-block; flex: none; }
.swatch.paid { background: var(--accent); }
.swatch.committed { background: repeating-linear-gradient(-45deg, color-mix(in srgb, var(--accent) 45%, transparent) 0 3px, transparent 3px 5px); box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--accent) 60%, transparent); }
.swatch.remaining { background: var(--progress-track); box-shadow: inset 0 0 0 1px var(--border); }

/* ---- modal: bottom sheet on mobile, centred on desktop ---------------- */
dialog.sheet {
  border: 0; padding: 0; color: var(--text); background: var(--surface);
  width: min(560px, 100vw - 32px); max-height: calc(100vh - 48px);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-lift);
}
dialog.sheet::backdrop { background: rgba(10, 8, 20, .5); backdrop-filter: blur(2px); }
.sheet-inner { display: flex; flex-direction: column; max-height: inherit; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 18px 20px 8px; }
.sheet-body { padding: 8px 20px 12px; overflow-y: auto; display: grid; gap: 14px; }
.sheet-foot { display: flex; gap: 10px; padding: 12px 20px 20px; border-top: var(--divider); }
.sheet-foot .spacer { flex: 1; }
.sheet-grab { display: none; }
.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 760px) {
  /* --kb is the on-screen keyboard height (set from visualViewport), so the
     sheet and its Save button sit above the keyboard. */
  dialog.sheet {
    width: 100vw; max-width: 100vw; margin: auto 0 var(--kb, 0px); max-height: calc(94dvh - var(--kb, 0px));
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    padding-bottom: env(safe-area-inset-bottom);
    touch-action: pan-y;
  }
  dialog.sheet[open] { animation: sheet-up var(--dur) var(--ease); }
  dialog.sheet.is-dragging { transition: none; }
  dialog.sheet.is-settling { transition: transform var(--dur) var(--ease); }
  .sheet-grab { display: block; width: 44px; height: 5px; border-radius: 3px; background: var(--border); margin: 10px auto 0; }
  .sheet-head { cursor: grab; }
  .sheet-foot .btn { flex: 1; }
  .sheet-foot .spacer { display: none; }
}
@media (min-width: 761px) { dialog.sheet[open] { animation: pop-in var(--dur) var(--ease); } }
@media (max-width: 400px) { .row-2 { grid-template-columns: 1fr; } }
@keyframes sheet-up { from { transform: translateY(40%); opacity: .4; } }
@keyframes pop-in { from { transform: translateY(8px) scale(.98); opacity: 0; } }

/* ---- toast ------------------------------------------------------------ */
.toast {
  position: fixed; left: 50%; bottom: 24px; z-index: 60; transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 12px 18px; border-radius: var(--radius-btn);
  font-weight: 700; box-shadow: var(--shadow-lift); opacity: 0; pointer-events: none;
  transition: opacity var(--dur) var(--ease), transform var(--dur) var(--ease);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
@media (max-width: 760px) { .toast { bottom: calc(90px + env(safe-area-inset-bottom)); } }

/* ---- entrance motion (once, on page load) ----------------------------- */
.enter { animation: enter 600ms var(--ease) both; }
.enter-2 { animation-delay: 80ms; }
.enter-3 { animation-delay: 160ms; }
@keyframes enter { from { opacity: 0; transform: translateY(10px); } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; animation-iteration-count: 1 !important; transition-duration: 1ms !important; scroll-behavior: auto !important; }
}

/* ---- login ------------------------------------------------------------ */
.login-page { display: grid; place-items: center; padding: 24px 16px; background: linear-gradient(120deg, #0B1018 0%, #3B3550 50%, #FFD6E6 100%); }
.login-card { width: min(400px, 100%); display: grid; gap: 16px; padding: 28px; }
.login-card h1 { font-size: 1.6rem; }
.login-card .lede { color: var(--muted); }
.login-error { color: var(--danger); font-weight: 700; background: color-mix(in srgb, var(--danger) 10%, transparent); padding: 10px 12px; border-radius: 10px; }
