/* Emily — design system. Owned by the lead.
 * Warm, calm, family-friendly. Mobile first. Light and dark.
 * View modules use these tokens and classes; they add no colours of their own. */

:root {
  color-scheme: light dark;

  --bg: #faf7f4;
  --surface: #ffffff;
  --surface-2: #f4efe9;
  --surface-3: #e9e1d8;

  --text: #2a2421;
  --text-dim: #6c625b;
  --text-faint: #6e635c;

  --border: #e8ded4;
  --border-strong: #d8cabc;

  --accent: #bb5c0e;
  --accent-hover: #a94f08;
  --accent-soft: #fdeee0;
  --accent-text: #8a3f05;
  --on-accent: #ffffff;

  --good: #1c7a48;
  --good-soft: #e4f5ea;
  /* The readable text on each SOLID status colour. A dark theme's status colours are
     bright, so white on them is unreadable: a toast needs its own text token. */
  --on-good: #ffffff;
  --on-warn: #ffffff;
  --on-danger: #ffffff;
  --warn: #9c6306;
  --warn-soft: #fdf2dd;
  --danger: #b3382c;
  --danger-soft: #fcebe8;
  --info-soft: #e8f1fb;

  --radius-sm: 10px;
  --radius: 16px;
  --radius-lg: 24px;
  --radius-pill: 999px;

  --shadow-sm: 0 1px 2px rgba(52, 38, 26, 0.06);
  --shadow: 0 2px 4px rgba(52, 38, 26, 0.05), 0 8px 24px rgba(52, 38, 26, 0.07);
  --shadow-lg: 0 8px 16px rgba(52, 38, 26, 0.08), 0 24px 48px rgba(52, 38, 26, 0.12);

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --space-7: 48px;

  --font: ui-rounded, "SF Pro Rounded", "Segoe UI Variable", "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;

  --app-bar-h: 56px;
  --tabbar-h: 62px;
  --content-max: 720px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17130f;
    --surface: #211c17;
    --surface-2: #2a231d;
    --surface-3: #352c24;

    --text: #f6f0ea;
    --text-dim: #b6a99d;
    /* 4.78:1 on cards, so secondary text still meets WCAG AA in dark mode. */
    --text-faint: #93867b;

    --border: #3a3028;
    --border-strong: #4c4036;

    --accent: #f2913f;
    --accent-hover: #ffa658;
    --accent-soft: #382414;
    --accent-text: #ffcfa3;
    --on-accent: #24160a;

    --good: #6ede9c;
    --good-soft: #16301f;
    --on-good: #0b2413;
    --on-warn: #241806;
    --on-danger: #330d09;
    --warn: #f0c268;
    --warn-soft: #33280f;
    --danger: #ff8f83;
    --danger-soft: #3a1c18;
    --info-soft: #16273a;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow: 0 2px 4px rgba(0, 0, 0, 0.35), 0 8px 24px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 8px 16px rgba(0, 0, 0, 0.4), 0 24px 48px rgba(0, 0, 0, 0.5);
  }
}

/* ------------------------------------------------------------------ base */

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  max-width: 100%;
  overflow-x: hidden;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3, p, figure { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent-text); text-decoration: none; }
a:hover { text-decoration: underline; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

.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;
}

.h1 { font-size: 1.5rem; font-weight: 700; letter-spacing: -0.01em; }
.h2 { font-size: 1.15rem; font-weight: 650; letter-spacing: -0.005em; }
.h3 { font-size: 1rem; font-weight: 650; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--text-dim); }
.faint { color: var(--text-faint); font-size: 0.85rem; }
.strong { font-weight: 650; }
.divider { height: 1px; background: var(--border); border: 0; margin: var(--space-2) 0; }

/* ---------------------------------------------------------------- shell */

#root { min-height: 100%; }

.shell {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 100%;
}

.app-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  height: var(--app-bar-h);
  padding: 0 var(--space-4);
  padding-top: env(safe-area-inset-top);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}

.app-bar-left, .app-bar-right {
  display: flex; align-items: center; gap: var(--space-2); min-width: 0;
}
.app-bar-right { flex: 0 0 auto; }

.brand { display: flex; align-items: center; gap: var(--space-2); color: var(--text); min-width: 0; min-height: 44px; }
.brand:hover { text-decoration: none; }
.brand-mark { font-size: 1.15rem; line-height: 1; }
.brand-name {
  font-weight: 700; letter-spacing: -0.01em;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.outlet {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
}

.outlet > * {
  width: 100%;
  max-width: var(--content-max);
  margin: 0 auto;
  padding: var(--space-4);
}

.tabbar {
  flex: 0 0 auto;
  /* Flex rather than a fixed column count: every tab takes an equal share and the row
     never wraps, so adding a tab cannot push one onto a second row. */
  display: flex;
  gap: var(--space-1);
  padding: var(--space-1) var(--space-2);
  padding-bottom: calc(var(--space-1) + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.tab {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: var(--space-2) var(--space-1);
  border-radius: var(--radius-sm);
  color: var(--text-dim);
  font-size: 0.72rem; font-weight: 600;
  min-height: 48px;
  /* An equal share of the row, and a label that ellipsises rather than wrapping. */
  flex: 1 1 0;
  min-width: 0;
  overflow: hidden;
  justify-content: center;
}
/* At 350px and below five tabs leave too little room for a whole label, so the label
   is a little smaller and the bar a little tighter: measured to keep every one of them
   unclipped at 320px with headroom, and to leave wider phones exactly as they were. */
@media (max-width: 350px) {
  .tabbar { padding-inline: 4px; gap: 2px; }
  .tab { font-size: 0.68rem; padding-inline: 1px; }
}

/* Five tabs at 320px leave ~64px each: the label shortens rather than wrapping. */
.tab > span:last-child {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tab:hover { text-decoration: none; background: var(--surface-2); color: var(--text); }
.tab-active { color: var(--accent-text); background: var(--accent-soft); }
.tab-emoji { font-size: 1.15rem; line-height: 1; }

.tabs { display: flex; gap: var(--space-1); overflow-x: auto; padding-bottom: 2px; }
.tabs::-webkit-scrollbar { display: none; }

.boot, .view-loading {
  display: flex; align-items: center; justify-content: center;
  min-height: 40vh;
}

/* ----------------------------------------------------------- primitives */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  max-width: 100%;
}
.card-pad { padding: var(--space-4); }

.stack { display: flex; flex-direction: column; gap: var(--space-3); }
.stack-tight { gap: var(--space-1); }
.row { display: flex; align-items: center; gap: var(--space-2); flex-wrap: wrap; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); flex-wrap: wrap; }
.grid-2 { display: grid; grid-template-columns: 1fr; gap: var(--space-3); }
.grid-3 { display: grid; grid-template-columns: 1fr; gap: var(--space-3); }

@media (min-width: 560px) {
  .grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

.list { display: flex; flex-direction: column; }
.list-row {
  display: flex; align-items: center; gap: var(--space-3);
  padding: var(--space-3) var(--space-1);
  border-bottom: 1px solid var(--border);
  min-height: 52px;
  text-align: left;
  background: none; border-left: 0; border-right: 0; border-top: 0;
  width: 100%; cursor: pointer; color: inherit;
}
.list-row:last-child { border-bottom: 0; }
.list-row:hover { background: var(--surface-2); }

.pill {
  display: inline-flex; align-items: center; gap: var(--space-1);
  padding: 3px 10px;
  border-radius: var(--radius-pill);
  background: var(--surface-2);
  font-size: 0.78rem; font-weight: 600; color: var(--text-dim);
  white-space: nowrap;
}

.badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 10px; border-radius: var(--radius-pill);
  background: var(--surface-2); color: var(--text-dim);
  font-size: 0.75rem; font-weight: 650; white-space: nowrap;
}
.badge-good { background: var(--good-soft); color: var(--good); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }

.kpi { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.kpi-label { font-size: 0.78rem; color: var(--text-faint); font-weight: 600; }
.kpi-value { font-size: 1.35rem; font-weight: 700; letter-spacing: -0.01em; font-variant-numeric: tabular-nums; }

/* -------------------------------------------------------------- buttons */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  padding: 11px 16px;
  min-height: 44px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: var(--surface-2);
  color: var(--text);
  font-weight: 650; font-size: 0.92rem;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.06s ease, opacity 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}
.btn:hover { background: var(--surface-3); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; transform: none; }

.btn-primary { background: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost { background: transparent; border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-quiet { background: transparent; color: var(--text-dim); }
.btn-quiet:hover { background: var(--surface-2); color: var(--text); }
.btn-danger { background: var(--danger-soft); color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); filter: brightness(0.96); }

.btn-sm { padding: 6px 12px; min-height: 44px; font-size: 0.85rem; }
.btn-icon { padding: 0; width: 44px; height: 44px; min-height: 44px; border-radius: var(--radius-pill); font-size: 1.05rem; }
.btn-block { width: 100%; }

/* --------------------------------------------------------------- inputs */

.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.label { font-size: 0.82rem; font-weight: 650; color: var(--text-dim); }

.input, .select, .textarea {
  width: 100%;
  padding: 11px 13px;
  min-height: 46px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  color: var(--text);
  -webkit-appearance: none;
  appearance: none;
}
.input::placeholder { color: var(--text-faint); }
.input:focus, .select:focus, .textarea:focus {
  border-color: var(--accent);
  outline: 2px solid var(--accent-soft);
  outline-offset: 0;
}
.textarea { min-height: 88px; resize: vertical; line-height: 1.45; }
.select {
  background-image: linear-gradient(45deg, transparent 50%, var(--text-dim) 50%),
                    linear-gradient(135deg, var(--text-dim) 50%, transparent 50%);
  background-position: calc(100% - 18px) 20px, calc(100% - 13px) 20px;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 34px;
}
.select-compact { min-height: 38px; padding: 7px 30px 7px 10px; font-size: 0.85rem; max-width: 150px; }

.money-field { display: flex; gap: var(--space-2); align-items: stretch; }
.money-field .input { flex: 1 1 auto; min-width: 0; }
.money-field .select { flex: 0 0 auto; width: auto; min-width: 104px; }

.segmented {
  /* Wraps rather than clipping: two long labels side by side do not fit a
     320px phone, and a clipped label is an unreadable option. */
  display: flex; flex-wrap: wrap; gap: 2px; padding: 3px;
  background: var(--surface-2); border-radius: var(--radius-sm);
  max-width: 100%;
}
.segmented-item {
  padding: 8px 14px; min-height: 44px;
  border: 0; background: transparent; color: var(--text-dim);
  border-radius: 7px; font-weight: 650; font-size: 0.86rem;
  cursor: pointer; white-space: nowrap;
}
.segmented-item:hover { color: var(--text); }
.segmented-item.segmented-active { background: var(--surface); color: var(--text); box-shadow: var(--shadow-sm); }

/* ---------------------------------------------------------------- chips */

.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 11px; min-height: 36px; border-radius: var(--radius-pill);
  background: var(--surface-2); border: 1px solid transparent;
  color: var(--text); font-size: 0.82rem; font-weight: 600;
  cursor: pointer; white-space: nowrap;
}
.chip-static { cursor: default; }
.chip:hover { background: var(--surface-3); }
.chip-emoji { font-size: 0.95rem; line-height: 1; }
.chip-count { color: var(--text-faint); font-variant-numeric: tabular-nums; }
.chip-active {
  background: var(--accent-soft); border-color: var(--accent);
  color: var(--accent-text);
}

/* -------------------------------------------------------------- avatars */

.avatar {
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: var(--radius-pill);
  font-weight: 700; flex: 0 0 auto;
  line-height: 1; overflow: hidden; user-select: none;
}
.avatar-sm { width: 30px; height: 30px; font-size: 0.9rem; }
.avatar-md { width: 38px; height: 38px; font-size: 1.05rem; }
.avatar-lg { width: 52px; height: 52px; font-size: 1.4rem; }

/* ---------------------------------------------------------------- empty */

.empty {
  display: flex; flex-direction: column; align-items: center;
  gap: var(--space-2); text-align: center;
  padding: var(--space-6) var(--space-4);
}
.empty-emoji { font-size: 2.2rem; line-height: 1; }
.empty-title { font-weight: 700; font-size: 1.05rem; }
.empty-body { color: var(--text-dim); font-size: 0.92rem; max-width: 34ch; }

/* -------------------------------------------------------------- loading */

.spinner {
  display: inline-block;
  width: 20px; height: 20px;
  border: 2px solid var(--surface-3);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: emily-spin 0.7s linear infinite;
}
.spinner[style*="lg"], .spinner-lg { width: 30px; height: 30px; border-width: 3px; }
@keyframes emily-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 1.6s; }
  * { scroll-behavior: auto !important; }
}

.skeleton {
  height: 14px; border-radius: 7px;
  background: linear-gradient(90deg, var(--surface-2), var(--surface-3), var(--surface-2));
  background-size: 200% 100%;
  animation: emily-shimmer 1.4s ease-in-out infinite;
}
@keyframes emily-shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }
.skeleton-group { gap: var(--space-3); }

/* --------------------------------------------------------------- notice */

.notice {
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
}
.notice:empty { display: none; }
.notice-danger, .notice-warn, .notice-good, .notice-info {
  padding: var(--space-3);
  border: 1px solid var(--border);
}
.notice-danger { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.notice-warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.notice-good { background: var(--good-soft); color: var(--good); border-color: transparent; }
.notice-info { background: var(--info-soft); color: var(--text-dim); border-color: transparent; }

/* ------------------------------------------------------------- progress */

.progress {
  height: 8px; border-radius: var(--radius-pill);
  background: var(--surface-3); overflow: hidden; width: 100%;
}
.progress-bar {
  height: 100%; border-radius: var(--radius-pill);
  background: var(--accent); transition: width 0.3s ease;
}
.progress-bar.progress-good { background: var(--good); }
.progress-bar.progress-warn { background: var(--warn); }
.progress-bar.progress-danger { background: var(--danger); }

/* -------------------------------------------------------------- overlays */

.modal-backdrop, .sheet-backdrop {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(30, 22, 16, 0.44);
  display: flex; align-items: flex-end; justify-content: center;
  padding: 0;
  animation: emily-fade 0.15s ease;
}
.modal-backdrop { align-items: center; padding: var(--space-4); }
@keyframes emily-fade { from { opacity: 0; } to { opacity: 1; } }

.modal, .sheet {
  background: var(--surface);
  width: 100%;
  max-width: 520px;
  max-height: 88dvh;
  display: flex; flex-direction: column;
  box-shadow: var(--shadow-lg);
  animation: emily-rise 0.2s cubic-bezier(0.2, 0.8, 0.3, 1);
}
.modal { border-radius: var(--radius-lg); }
.sheet {
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  max-width: 620px;
  padding-bottom: env(safe-area-inset-bottom);
}
@keyframes emily-rise { from { transform: translateY(14px); opacity: 0.6; } to { transform: none; opacity: 1; } }

.modal-head, .sheet-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-4);
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.modal-body, .sheet-body {
  padding: var(--space-4);
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 0;
}
.modal-foot, .sheet-foot {
  display: flex; gap: var(--space-2); justify-content: flex-end;
  padding: var(--space-4);
  border-top: 1px solid var(--border);
  flex: 0 0 auto;
  flex-wrap: wrap;
}
.sheet-foot .btn { flex: 1 1 auto; }

.overlay-out { animation: emily-fade-out 0.15s ease forwards; }
@keyframes emily-fade-out { to { opacity: 0; } }

/* ---------------------------------------------------------------- toasts */

.toast-host {
  position: fixed;
  left: 50%; transform: translateX(-50%);
  /* At the top, so a toast never covers the composer or a pending draft card's
     Confirm/Edit/Discard buttons while a family is using them. */
  top: calc(var(--app-bar-h) + 12px + env(safe-area-inset-top));
  z-index: 80;
  display: flex; flex-direction: column; gap: var(--space-2);
  align-items: center;
  pointer-events: none;
  width: max-content; max-width: min(92vw, 460px);
}
.toast {
  padding: 10px 16px;
  border-radius: var(--radius-pill);
  background: var(--text);
  color: var(--bg);
  font-size: 0.88rem; font-weight: 600;
  box-shadow: var(--shadow);
  animation: emily-rise 0.2s ease;
  text-align: center;
}
.toast-good { background: var(--good); color: var(--on-good); }
.toast-warn { background: var(--warn); color: var(--on-warn); }
.toast-danger { background: var(--danger); color: var(--on-danger); }
.toast-out { opacity: 0; transition: opacity 0.35s ease; }

/* ------------------------------------------------------------------ auth */

.auth-screen {
  /* Percentage, not 100dvh: inside the shell this must fill the outlet, not
     overflow it by the height of the app bar and the tab bar. */
  min-height: 100%;
  display: flex; align-items: center; justify-content: center;
  padding: var(--space-4);
  background:
    radial-gradient(120% 80% at 50% -10%, var(--accent-soft) 0%, transparent 62%),
    var(--bg);
}
.auth-card { width: 100%; max-width: 420px; box-shadow: var(--shadow); }
.auth-brand { display: flex; flex-direction: column; gap: 6px; }
.auth-mark { font-size: 2.2rem; line-height: 1; }
.auth-foot { text-align: center; margin-top: var(--space-1); }

/* -------------------------------------------------------------- utility */

.scroll-y { overflow-y: auto; -webkit-overflow-scrolling: touch; }
.sticky-bottom { position: sticky; bottom: 0; }
.toolbar {
  display: flex; align-items: center; gap: var(--space-2);
  flex-wrap: wrap;
}
.toolbar .input, .toolbar .select { width: auto; flex: 1 1 140px; }
