/* ===========================================================================
   Smart Pantry — single mobile-first responsive stylesheet.

   Sections:
     1. Theme tokens (CSS variables; light + dark)
     2. Base / resets / typography
     3. App shell: header, sidebar (>=768px), bottom tab bar (<768px)
     4. Views (generic helpers used by feature views)
     5. UI kit (.ui-*): card, button, field, modal/sheet, spinner, badge, pill, toast
     6. Settings view specifics
     7. Safe-area insets + responsive breakpoints
   The breakpoint between "phone" (bottom tabs) and "desktop website" (sidebar) is
   768px. Everything is mobile-first; the wide layout is layered on at the media query.
   =========================================================================== */

/* ---------------------------------------------------------------------------
   1. Theme tokens
   ------------------------------------------------------------------------- */
/* HEARTHEN LIGHT — warm cream/hearthstone, ember accents. Derived from the shipped
   logo palette (#F7B450 / #D85B27 / #FFFDF7 / #F2923A / #FFF1D8). The INTERACTIVE
   brand is a deepened ember #B3471B (raw #D85B27 is only 3.79:1 on cream — fails AA
   for normal text); the logo embers serve as non-text accents. All ratios verified:
   text 15.65:1, muted 6.63:1, brand links/buttons 5.38:1, hover 7.69:1 (WCAG AA). */
:root,
:root[data-theme="light"] {
  --brand: #b3471b;            /* deep ember — 5.38:1 on cream */
  --brand-strong: #8f350f;     /* hover — 7.69:1 */
  --brand-tint: #fbe7d6;       /* brand text on it 4.57:1 */
  --brand-contrast: #fffdf7;   /* cream flame */
  --brand-soft: rgba(216, 91, 39, 0.1);
  --brand-weak: var(--brand-soft);
  --accent: #d85b27;           /* logo ember — large-text/graphics only */
  --accent-glow: #f2923a;      /* inner ember — decorative only, never text */

  --bg: #faf3e8;               /* warm parchment */
  --surface: #fffdf7;          /* cream flame */
  --surface-2: #f5ead8;        /* hearthstone wash */
  --surface-elev: #fffdf7;
  --border: #eaddc6;
  --border-strong: #d6c3a4;
  --border-weak: #f1e7d2;

  --text: #2b2014;             /* espresso — 15.65:1 */
  --text-muted: #6b5847;       /* 6.63:1 */
  --text-faint: #95805f;       /* 3.73:1 — placeholders/decorative only */

  --danger: #b3261e;
  --danger-bg: #fae5de;
  --danger-soft: rgba(179, 38, 30, 0.1);
  --warn: #935310;
  --warn-bg: #fbebd2;
  --warning: var(--warn);
  --caution: #7a6510;
  --caution-bg: #f8f0cf;
  --info: #1f6f77;             /* warm teal (replaces the cold blue) */
  --info-bg: #e0f0ee;
  --success: #477236;          /* warm moss */
  --success-bg: #e6f0dc;
  --water: #4ba3e3;            /* water is legitimately blue (garden meter) */
  --water-deep: #2f86c9;

  /* Inverted snackbar (e.g. inventory undo): espresso panel, ember-gold action. */
  --inverse-surface: #2b2014;
  --inverse-text: #fffdf7;
  --inverse-action: #f7b450;

  --shadow-1: 0 1px 2px rgba(63, 38, 16, 0.08), 0 1px 3px rgba(63, 38, 16, 0.06);
  --shadow-2: 0 4px 12px rgba(63, 38, 16, 0.14);

  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 18px;

  --tabbar-h: 60px;
  --sidebar-w: 232px;
  --header-h: 56px;
  --maxw-content: 920px;
  --tap-min: 44px;             /* minimum touch-target floor (Apple/Android guidance) */

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial,
    sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
}

/* HEARTHEN DARK — "hearth at night": warm brown-charcoal lit by embers, not pure
   black. Here the logo's inner ember #F2923A is luminous enough to BE the brand
   (links 7.27:1; dark espresso labels on ember buttons 7.41:1). Text 14.43:1. */
:root[data-theme="dark"] {
  --brand: #f2923a;            /* inner ember — 7.27:1 */
  --brand-strong: #f7a451;     /* 8.43:1 */
  --brand-tint: #3a2412;
  --brand-contrast: #2a1505;   /* 7.41:1 on ember */
  --brand-soft: rgba(242, 146, 58, 0.14);
  --brand-weak: var(--brand-soft);
  --accent: #f2923a;
  --accent-glow: #f7b450;

  --bg: #191210;
  --surface: #241a12;
  --surface-2: #2e2218;
  --surface-elev: #322519;
  --border: #41311f;
  --border-strong: #57422b;
  --border-weak: #38291c;

  --text: #f4ebdd;             /* 14.43:1 */
  --text-muted: #c5af97;       /* 8.08:1 */
  --text-faint: #8f7b64;       /* 4.21:1 */

  --danger: #f28b75;
  --danger-bg: #3a1b14;
  --danger-soft: rgba(242, 139, 117, 0.16);
  --warn: #e8a95c;
  --warn-bg: #392718;
  --warning: var(--warn);
  --caution: #d9c25e;
  --caution-bg: #33290f;
  --info: #7fbfc9;
  --info-bg: #14282e;
  --success: #9cc178;
  --success-bg: #22301a;
  --water: #5fa9d6;
  --water-deep: #3e80b8;

  --inverse-surface: #fff1d8;  /* hearthstone panel on the dark theme */
  --inverse-text: #2b2014;
  --inverse-action: #b3471b;

  --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.5);
  --shadow-2: 0 6px 18px rgba(0, 0, 0, 0.6);
}

/* ---------------------------------------------------------------------------
   2. Base / resets / typography
   ------------------------------------------------------------------------- */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

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

h1,
h2,
h3 {
  margin: 0 0 0.4em;
  line-height: 1.25;
}

p {
  margin: 0 0 0.75em;
}

a {
  color: var(--brand);
  text-decoration: none;
}

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.88em;
  background: var(--surface-2);
  border-radius: 4px;
  padding: 0.1em 0.35em;
  word-break: break-word;
}

/* Visible, consistent focus for keyboard users (a11y). */
:focus-visible {
  outline: 3px solid var(--brand);
  outline-offset: 2px;
  border-radius: 4px;
}

.noscript {
  max-width: 36rem;
  margin: 4rem auto;
  padding: 0 1.25rem;
  text-align: center;
}

/* ---------------------------------------------------------------------------
   3. App shell
   ------------------------------------------------------------------------- */
.app-shell {
  min-height: 100%;
  display: flex;
  flex-direction: column;
}

/* Header */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  /* Safe-area inset ADDS to the height (box-sizing: border-box would otherwise
     subtract the notch inset from the 56px and squash the installed-iOS header). */
  height: calc(var(--header-h) + env(safe-area-inset-top));
  padding: 0 1rem;
  padding-top: env(safe-area-inset-top);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.app-header-title {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}

.app-header-icon {
  border-radius: 6px;
  flex: 0 0 auto;
}

.app-title {
  font-size: 1.05rem;
  font-weight: 700;
  margin: 0;
  white-space: nowrap;
}

/* Connection indicator (in the header) */
.app-conn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-muted);
  border-radius: 999px;
  padding: 0.25rem 0.65rem;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  min-height: 2.5rem; /* tappable chip, not a 29px sliver */
}

.app-conn-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--text-faint);
  flex: 0 0 auto;
}

.app-conn-online .app-conn-dot {
  background: var(--success);
  box-shadow: 0 0 0 3px var(--success-bg);
}
.app-conn-offline .app-conn-dot {
  background: var(--danger);
  box-shadow: 0 0 0 3px var(--danger-bg);
}
.app-conn-warn .app-conn-dot {
  background: var(--warn);
  box-shadow: 0 0 0 3px var(--warn-bg);
}
.app-conn-unknown .app-conn-dot {
  background: var(--text-faint);
  animation: pulse 1.4s ease-in-out infinite;
}

/* Header actions cluster (notification bell + connection indicator). Phase 2. */
.app-header-actions {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  flex: 0 0 auto;
}

/* Header search trigger (UX arc: the navigation spine / command palette). On wide screens it
   reads as a real search field; on narrow screens it collapses to just the icon. */
.app-header-search {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  border: 1px solid var(--border);
  background: var(--surface-2, var(--surface));
  color: var(--text-muted, var(--text));
  border-radius: 999px;
  padding: 0.3rem 0.7rem;
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
}
.app-header-search:hover { border-color: var(--border-strong, var(--border)); color: var(--text); }
.app-header-search:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
.app-header-search-label { white-space: nowrap; }
@media (max-width: 560px) {
  .app-header-search { padding: 0.35rem; }
  .app-header-search-label { display: none; }
}

/* Member quick-jump (UX arc: the navigation spine). A native <details> pill in the header that
   drops a menu of members, each with one-tap links to their four health surfaces — collapses the
   Members → person → surface drill to a single step. Self-hides when the medical tier is off or
   the house is empty (rendered conditionally in app.js). */
.member-jump { position: relative; }
.member-jump-summary {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  border: 1px solid var(--border);
  background: var(--surface-2, var(--surface));
  color: var(--text-muted, var(--text));
  border-radius: 999px;
  padding: 0.3rem 0.7rem;
  font-size: 0.85rem;
  cursor: pointer;
  list-style: none;
  user-select: none;
}
.member-jump-summary::-webkit-details-marker { display: none; }
.member-jump-summary:hover { border-color: var(--border-strong, var(--border)); color: var(--text); }
.member-jump-summary:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
.member-jump[open] .member-jump-summary { border-color: var(--brand); color: var(--text); }
.member-jump-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 0.4rem);
  z-index: 40;
  min-width: 16rem;
  max-height: min(70vh, 28rem);
  overflow-y: auto;
  background: var(--surface, #fff);
  border: 1px solid var(--border);
  border-radius: 0.8rem;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.18);
  padding: 0.35rem;
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}
.member-jump-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.3rem 0.4rem;
  border-radius: 0.55rem;
}
.member-jump-row:hover { background: var(--surface-2, rgba(0, 0, 0, 0.04)); }
.member-jump-name {
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
  flex: 1 1 auto;
  text-align: left;
  background: transparent;
  border: 0;
  padding: 0.1rem 0;
  cursor: pointer;
  font-family: inherit;
}
.member-jump-name:hover { text-decoration: underline; color: var(--brand); }
.member-jump-name:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; border-radius: 4px; }
.member-jump-links { display: inline-flex; gap: 0.1rem; flex: 0 0 auto; }
.member-jump-links button {
  border: 0;
  background: transparent;
  cursor: pointer;
  font-size: 1.05rem;
  line-height: 1;
  padding: 0.3rem;
  border-radius: 0.45rem;
}
.member-jump-links button:hover { background: var(--brand-weak, rgba(0, 0, 0, 0.08)); }
.member-jump-links button:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; }
.member-jump-all {
  margin-top: 0.2rem;
  border: 0;
  border-top: 1px solid var(--border);
  background: transparent;
  color: var(--brand);
  font: inherit;
  font-size: 0.84rem;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
  padding: 0.45rem 0.4rem 0.3rem;
}
.member-jump-all:hover { text-decoration: underline; }
@media (max-width: 560px) {
  .member-jump-summary { padding: 0.35rem; }
  .member-jump-label { display: none; }
}

/* Notification bell (chrome, not a tab). An unread badge floats at the top-right. */
.app-bell {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.75rem;
  height: 2.75rem;
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 999px;
  cursor: pointer;
  line-height: 1;
}
.app-bell-icon {
  font-size: 1.05rem;
}
.app-bell-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  min-width: 1.05rem;
  height: 1.05rem;
  padding: 0 0.25rem;
  border-radius: 999px;
  background: var(--danger, #d92d20);
  color: #fff;
  font-size: 0.65rem;
  font-weight: 700;
  line-height: 1.05rem;
  text-align: center;
  box-shadow: 0 0 0 2px var(--surface);
}

@keyframes pulse {
  0%, 100% { opacity: 0.4; }
  50% { opacity: 1; }
}

/* Main column */
.app-main {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.app-content {
  flex: 1 1 auto;
  width: 100%;
  max-width: var(--maxw-content);
  margin: 0 auto;
  padding: 1rem;
  /* Leave room for the bottom tab bar + the iPhone home indicator. */
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 1rem);
}
.app-content:focus {
  outline: none;
}

/* Sidebar (hidden on phones; shown >=768px) */
.app-sidebar {
  display: none;
}

.app-brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 1rem 1rem 1.25rem;
  font-weight: 800;
  font-size: 1.05rem;
}
.app-brand-icon {
  border-radius: 7px;
}

/* Bottom tab bar (phones) */
.app-tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 30;
  display: flex;
  justify-content: space-around;
  align-items: stretch;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
}

/* Nav links: shared by tab bar and sidebar, variant-specific layout below. */
.app-nav-link {
  display: flex;
  align-items: center;
  color: var(--text-muted);
  text-decoration: none;
  font-weight: 600;
}

.app-nav-tab {
  flex: 1 1 0;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
  /* 0.78rem ≈ 12.5px — the primary navigation must stay readable for elder eyes
     (0.66rem ≈ 10.6px failed any reasonable minimum); 5 labels still fit at 320px. */
  font-size: 0.78rem;
  padding: 0.3rem 0.1rem;
  min-width: 0;
}
.app-nav-tab .app-nav-icon {
  font-size: 1.3rem;
  line-height: 1;
}
.app-nav-tab .app-nav-label {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}
.app-nav-tab.is-active {
  color: var(--brand);
}

.app-nav-rail {
  gap: 0.75rem;
  padding: 0.7rem 1rem;
  margin: 0.1rem 0.5rem;
  border-radius: var(--radius);
  font-size: 0.95rem;
}
.app-nav-rail .app-nav-icon {
  font-size: 1.2rem;
}
.app-nav-rail:hover {
  background: var(--surface-2);
  color: var(--text);
}
.app-nav-rail.is-active {
  background: var(--brand-tint);
  color: var(--brand);
}

.outlet-error {
  padding: 1rem;
  color: var(--danger);
}

/* ---------------------------------------------------------------------------
   4. Views — generic helpers feature views can use
   ------------------------------------------------------------------------- */
.view {
  display: block;
}
.view-title {
  font-size: 1.4rem;
  font-weight: 800;
  margin-bottom: 0.75rem;
}
.view-help {
  color: var(--text-muted);
  font-size: 0.92rem;
}
.view-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  margin-top: 0.5rem;
}
.view-steps,
.test-result-list {
  margin: 0.25rem 0 0.5rem;
  padding-left: 1.25rem;
}
.view-steps li,
.test-result-list li {
  margin: 0.25rem 0;
}
.test-result-list {
  list-style: none;
  padding-left: 0;
}
.test-result-list li {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

/* ---------------------------------------------------------------------------
   5. UI kit
   ------------------------------------------------------------------------- */

/* Card */
.ui-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1rem;
  margin: 0 0 1rem;
  box-shadow: var(--shadow-1);
}
.ui-card-title {
  font-size: 1rem;
  font-weight: 700;
  margin: 0 0 0.75rem;
}

/* Button */
.ui-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  font: inherit;
  font-weight: 650;
  line-height: 1;
  padding: 0.7rem 1.1rem;
  min-height: 44px; /* comfortable touch target */
  border-radius: var(--radius);
  border: 1px solid transparent;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease, opacity 0.12s ease;
  user-select: none;
}
.ui-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}
.ui-btn-primary {
  background: var(--brand);
  color: var(--brand-contrast);
}
.ui-btn-primary:hover:not(:disabled) {
  background: var(--brand-strong);
}
.ui-btn-secondary {
  background: var(--surface);
  color: var(--text);
  border-color: var(--border-strong);
}
.ui-btn-secondary:hover:not(:disabled) {
  background: var(--surface-2);
}
.ui-btn-ghost {
  background: transparent;
  color: var(--brand);
}
.ui-btn-ghost:hover:not(:disabled) {
  background: var(--brand-tint);
}
.ui-btn-danger {
  background: var(--danger);
  color: #fff;
}
.ui-btn-danger:hover:not(:disabled) {
  filter: brightness(0.95);
}

/* Icon button */
.ui-iconbtn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--tap-min);
  height: var(--tap-min);
  border-radius: var(--radius);
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  font-size: 1.05rem;
}
.ui-iconbtn:hover {
  background: var(--surface-2);
}

/* Form fields */
.ui-field {
  margin-bottom: 0.9rem;
}
.ui-label {
  display: block;
  font-size: 0.85rem;
  font-weight: 650;
  color: var(--text);
  margin-bottom: 0.3rem;
}
.ui-req {
  color: var(--danger);
}
.ui-input {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 0.65rem 0.75rem;
  min-height: 44px;
}
.ui-input::placeholder {
  color: var(--text-faint);
}
.ui-input:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px var(--brand-tint);
  outline: none;
}

/* Keyboard users get a REAL ring (the soft tint above is invisible in dark mode and
   this rule's specificity would otherwise beat the global :focus-visible). */
.ui-input:focus-visible {
  outline: 3px solid var(--brand);
  outline-offset: 1px;
  border-color: var(--brand);
  box-shadow: none;
}
.ui-select {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--text-muted) 50%),
    linear-gradient(135deg, var(--text-muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) center, calc(100% - 13px) center;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 2rem;
}
.ui-field.has-error .ui-input {
  border-color: var(--danger);
}
.ui-hint {
  margin: 0.3rem 0 0;
  font-size: 0.78rem;
  color: var(--text-muted);
  word-break: break-word;
}
.ui-error {
  margin: 0.3rem 0 0;
  font-size: 0.8rem;
  color: var(--danger);
}

/* Spinner */
.ui-spinner {
  display: inline-block;
  border-radius: 50%;
  border: 2px solid var(--border-strong);
  border-top-color: var(--brand);
  animation: spin 0.7s linear infinite;
  vertical-align: middle;
}
.ui-spinner-sm { width: 16px; height: 16px; }
.ui-spinner-md { width: 24px; height: 24px; }
.ui-spinner-lg { width: 38px; height: 38px; border-width: 3px; }
.ui-spinner.is-inline { margin-right: 0.25rem; }
/* currentColor tracks each variant's text (cream on primary, ember on ghost) —
   a hardcoded white spinner was invisible on secondary/ghost buttons. */
.ui-btn .ui-spinner {
  border-color: color-mix(in srgb, currentColor 35%, transparent);
  border-top-color: currentColor;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Empty state */
.ui-empty {
  text-align: center;
  padding: 2.5rem 1rem;
  color: var(--text-muted);
}
.ui-empty-icon {
  font-size: 2.6rem;
  margin-bottom: 0.5rem;
}
.ui-empty-title {
  font-weight: 700;
  color: var(--text);
  margin-bottom: 0.25rem;
}
.ui-empty-msg {
  max-width: 26rem;
  margin: 0 auto 0.75rem;
}
.ui-empty-action {
  margin-top: 0.5rem;
}

/* Badge */
.ui-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.74rem;
  font-weight: 700;
  border-radius: 999px;
  padding: 0.15rem 0.55rem;
  line-height: 1.4;
}
.ui-badge-neutral { background: var(--surface-2); color: var(--text-muted); }
.ui-badge-brand { background: var(--brand-tint); color: var(--brand); }
.ui-badge-info { background: var(--info-bg); color: var(--info); }
.ui-badge-warn { background: var(--warn-bg); color: var(--warn); }
.ui-badge-danger { background: var(--danger-bg); color: var(--danger); }
.ui-badge-success { background: var(--success-bg); color: var(--success); }

/* Expiry pill */
.ui-pill {
  display: inline-flex;
  align-items: center;
  font-size: 0.74rem;
  font-weight: 700;
  border-radius: 999px;
  padding: 0.12rem 0.5rem;
  white-space: nowrap;
}
.ui-pill-neutral { background: var(--surface-2); color: var(--text-muted); }
.ui-pill-caution { background: var(--caution-bg); color: var(--caution); }
.ui-pill-warn { background: var(--warn-bg); color: var(--warn); }
.ui-pill-danger { background: var(--danger-bg); color: var(--danger); }

/* Modal / sheet */
.ui-modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(28, 15, 6, 0.55); /* warm umber — firelight shadow, not green-black */
  display: flex;
  padding: 1rem;
}
.ui-modal-center {
  align-items: center;
  justify-content: center;
}
.ui-modal-bottom {
  align-items: flex-end;
  justify-content: center;
  padding: 0;
}
.ui-modal {
  background: var(--surface-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-2);
  width: 100%;
  max-width: 520px;
  max-height: calc(100dvh - 2rem);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.ui-modal-bottom .ui-modal {
  max-width: 640px;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  max-height: 90dvh;
  padding-bottom: env(safe-area-inset-bottom);
  animation: sheet-up 0.18s ease-out;
}
@keyframes sheet-up {
  from { transform: translateY(12%); opacity: 0.6; }
  to { transform: translateY(0); opacity: 1; }
}
.ui-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.9rem 1rem;
  border-bottom: 1px solid var(--border);
}
.ui-modal-title {
  font-size: 1.05rem;
  font-weight: 700;
  margin: 0;
}
.ui-modal-body {
  padding: 1rem;
  overflow-y: auto;
}
.ui-modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
  padding: 0.85rem 1rem;
  border-top: 1px solid var(--border);
}

/* Toast */
.ui-toast-host {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 0.5rem);
  z-index: 200;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
  padding: 0 1rem;
  pointer-events: none;
}
.ui-toast {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  max-width: min(520px, 100%);
  background: var(--surface-elev);
  color: var(--text);
  border: 1px solid var(--border);
  border-left: 4px solid var(--text-faint);
  border-radius: var(--radius);
  box-shadow: var(--shadow-2);
  padding: 0.6rem 0.5rem 0.6rem 0.85rem;
  font-size: 0.9rem;
  animation: toast-in 0.16s ease-out;
}
@keyframes toast-in {
  from { transform: translateY(8px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}
.ui-toast-msg { flex: 1 1 auto; }
.ui-toast-close { width: 40px; height: 40px; flex: 0 0 auto; }
.ui-toast-info { border-left-color: var(--info); }
.ui-toast-success { border-left-color: var(--success); }
.ui-toast-warn { border-left-color: var(--warn); }
.ui-toast-danger { border-left-color: var(--danger); }

/* ---------------------------------------------------------------------------
   6. Settings view specifics
   ------------------------------------------------------------------------- */
.test-result {
  margin-top: 0.75rem;
  padding: 0.85rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface-2);
}
.test-result.is-ok { border-color: var(--success); background: var(--success-bg); }
.test-result.is-err { border-color: var(--danger); background: var(--danger-bg); }
.test-result-head {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-weight: 700;
  margin: 0 0 0.4rem;
}
.test-result-msg {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.84rem;
  word-break: break-word;
}

/* ---------------------------------------------------------------------------
   7. Responsive: desktop website layout (>=768px)
   ------------------------------------------------------------------------- */
@media (min-width: 768px) {
  .app-shell {
    flex-direction: row;
  }

  /* Show the sidebar, hide the bottom tab bar. */
  .app-sidebar {
    display: flex;
    flex-direction: column;
    width: var(--sidebar-w);
    flex: 0 0 var(--sidebar-w);
    border-right: 1px solid var(--border);
    background: var(--surface);
    position: sticky;
    top: 0;
    height: 100vh;
    padding-top: env(safe-area-inset-top);
    overflow-y: auto;
  }
  .app-sidebar-links {
    display: flex;
    flex-direction: column;
    padding: 0.25rem 0;
  }
  .app-tabbar {
    display: none;
  }

  /* The header no longer needs to repeat the brand (sidebar has it), but keep it
     for the connection indicator + page context. */
  .app-header-icon {
    display: none;
  }

  .app-content {
    padding: 1.5rem;
    /* No bottom tab bar to clear on desktop. */
    padding-bottom: calc(env(safe-area-inset-bottom) + 1.5rem);
  }

  /* Toasts sit above the bottom edge (no tab bar to clear). */
  .ui-toast-host {
    bottom: calc(env(safe-area-inset-bottom) + 1rem);
    align-items: flex-end;
    right: 1rem;
    left: auto;
  }

  /* Modals can center comfortably; bottom sheets become centered dialogs on wide. */
  .ui-modal-bottom {
    align-items: center;
    padding: 1rem;
  }
  .ui-modal-bottom .ui-modal {
    border-radius: var(--radius-lg);
    max-width: 520px;
  }
}

/* Larger desktops: a touch more breathing room for the content column. */
@media (min-width: 1100px) {
  .app-content {
    padding: 2rem;
  }
}

/* ===========================================================================
 * Global in-flight progress bar (under the header). An indeterminate sliding bar
 * shown whenever any network request is in flight (driven by AppShell/useNetBusy).
 * ========================================================================= */
.app-progress {
  position: relative;
  height: 2px;
  overflow: hidden;
  background: transparent;
  opacity: 0;
  transition: opacity 0.2s ease;
}
.app-progress.is-busy {
  opacity: 1;
}
.app-progress-bar {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 40%;
  background: var(--brand);
  border-radius: 999px;
  animation: app-progress-slide 1.1s ease-in-out infinite;
}
.app-progress:not(.is-busy) .app-progress-bar {
  animation: none;
}
@keyframes app-progress-slide {
  0% {
    left: -40%;
    width: 40%;
  }
  50% {
    width: 55%;
  }
  100% {
    left: 100%;
    width: 40%;
  }
}

/* ===========================================================================
 * Skeleton / shimmer placeholders. Shown on FIRST load instead of a bare spinner
 * so the layout settles before data arrives. Reuses the theme surface tokens.
 * ========================================================================= */
.ui-skeleton {
  display: block;
  position: relative;
  overflow: hidden;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
}
.ui-skeleton::after {
  content: "";
  position: absolute;
  inset: 0;
  transform: translateX(-100%);
  background: linear-gradient(
    90deg,
    transparent 0%,
    var(--border) 50%,
    transparent 100%
  );
  animation: ui-shimmer 1.3s ease-in-out infinite;
}
.ui-skeleton-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.6rem;
}
.ui-skeleton-card {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-1);
  padding: 0.75rem 0.85rem;
}
.ui-skeleton-card .ui-skeleton-lines {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  min-width: 0;
}
@keyframes ui-shimmer {
  100% {
    transform: translateX(100%);
  }
}
@media (min-width: 768px) {
  .ui-skeleton-list {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* Respect reduced-motion preferences. */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}

/* ---------------------------------------------------------------------------
   8. Comfort profile (elder experience) — global, token-level.
   The shell resolves the signed-in person's effective experience profile once
   (store.resolveOwnMember -> <html data-profile="...">, mirroring data-theme).
   Because nearly every size in the app is rem-based, the root font-size bump
   carries most of the "larger type" promise; the rest enlarges the controls an
   elder actually touches. Plain CSS only — no per-view forks.
   ------------------------------------------------------------------------- */
:root[data-profile="comfort"] {
  --radius: 14px;
  font-size: 112.5%; /* 18px base — every rem size scales with it */
}
:root[data-profile="comfort"] .ui-btn {
  min-height: 52px;
  font-size: 1.05rem;
}
:root[data-profile="comfort"] .ui-iconbtn {
  width: 48px;
  height: 48px;
}
:root[data-profile="comfort"] .ui-input {
  min-height: 52px;
  font-size: 1.05rem;
}
:root[data-profile="comfort"] .ui-hint {
  font-size: 0.95rem;
}
:root[data-profile="comfort"] .app-nav-tab {
  font-size: 0.8rem;
}
:root[data-profile="comfort"] .ui-badge,
:root[data-profile="comfort"] .ui-pill {
  font-size: 0.9rem;
}

/* Print: the health summary, pocket card (and any view) print clean — hide app chrome and any
   element marked .no-print (action buttons, the bottom nav, the "generated at" footnote). Used by
   the clinician/caregiver health summary + the emergency pocket card's Print / Save-as-PDF button.
   NOTE: the real chrome classes are .app-sidebar / .app-header / .app-tabbar / .app-progress (the
   older .app-nav/.app-chrome/.bottom-nav selectors never matched the markup — kept harmless). */
@media print {
  .no-print,
  .app-sidebar,
  .app-header,
  .app-tabbar,
  .app-progress,
  .app-nav,
  .app-chrome,
  .bottom-nav {
    display: none !important;
  }
  .view {
    padding: 0 !important;
  }
}

/* Emergency pocket card (UX arc: trust + safety). A wallet-sized card — drug allergies (top, loud),
   conditions, current meds — that prints small to fold into a wallet / stick on the fridge. Reuses
   the health-summary read. The bordered box is sized so the printed page reads as a cut-out card. */
.pocket-card {
  max-width: 26rem;
  border: 2px solid var(--text, #222);
  border-radius: 0.7rem;
  padding: 0.85rem 1rem;
  background: var(--surface, #fff);
  color: var(--text, #222);
  font-size: 0.92rem;
  line-height: 1.45;
}
.pocket-card-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.6rem;
  flex-wrap: wrap;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.45rem;
  margin-bottom: 0.5rem;
}
.pocket-card-kicker { font-size: 0.74rem; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase; color: var(--text-muted); }
.pocket-card-name { font-size: 1.15rem; font-weight: 800; }
.pocket-card-section { margin: 0.4rem 0; }
.pocket-card-label { display: block; font-size: 0.72rem; font-weight: 700; letter-spacing: 0.03em; text-transform: uppercase; color: var(--text-muted); margin-bottom: 0.1rem; }
.pocket-card-value { display: block; }
.pocket-card-value-strong { font-weight: 800; }
.pocket-card-none { color: var(--text-muted); font-style: italic; }
.pocket-card-allergies {
  border: 1px solid color-mix(in srgb, var(--warn, #b00020) 45%, transparent);
  background: color-mix(in srgb, var(--warn, #b00020) 9%, transparent);
  border-radius: 0.5rem;
  padding: 0.4rem 0.55rem;
}
.pocket-card-allergies .pocket-card-label,
.pocket-card-allergies .pocket-card-value-strong { color: var(--warn, #b00020); }
.pocket-card-meds { list-style: none; margin: 0.1rem 0 0; padding: 0; display: flex; flex-direction: column; gap: 0.2rem; }
.pocket-card-meds li::before { content: "• "; color: var(--text-muted); }
.pocket-card-foot {
  margin-top: 0.6rem;
  padding-top: 0.45rem;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  font-size: 0.7rem;
  color: var(--text-muted);
}
@media print {
  .pocket-card {
    border-color: #000;
    color: #000;
    background: #fff;
    box-shadow: none;
  }
  .pocket-card-allergies { border-color: #000; background: transparent; }
  .pocket-card-allergies .pocket-card-label,
  .pocket-card-allergies .pocket-card-value-strong { color: #000; }
}

/* ===========================================================================
 * Accessibility preference classes (UX arc: multi-generational inclusivity).
 * Toggled on <html> by store.js applyAppearance() from per-device settings.
 * ========================================================================= */

/* High contrast: promote muted text to full strength + strengthen borders. */
html.a11y-contrast {
  --text-muted: var(--text);
  --text-faint: var(--text);
  --border: var(--border-strong, #555);
}
html.a11y-contrast .more-tile,
html.a11y-contrast .ui-card,
html.a11y-contrast .card,
html.a11y-contrast .ui-input,
html.a11y-contrast input,
html.a11y-contrast textarea,
html.a11y-contrast select {
  border-width: 2px;
}
html.a11y-contrast a { text-decoration: underline; }

/* Dyslexia-friendly: a more legible font + looser spacing (no bundled font needed). */
html.a11y-dyslexia body,
html.a11y-dyslexia .app-shell {
  font-family: "Atkinson Hyperlegible", Verdana, Tahoma, "Trebuchet MS", sans-serif;
  letter-spacing: 0.02em;
  line-height: 1.7;
  word-spacing: 0.05em;
}

/* Larger text: scale the root so the whole rem-based layout grows. */
html.a11y-large-text { font-size: 118%; }

/* Reduced motion: kill animations/transitions app-wide (respects the toggle even when the OS
   setting says otherwise). */
html.a11y-reduce-motion *,
html.a11y-reduce-motion *::before,
html.a11y-reduce-motion *::after {
  animation-duration: 0.001ms !important;
  animation-iteration-count: 1 !important;
  transition-duration: 0.001ms !important;
  scroll-behavior: auto !important;
}

/* Per-member context bar (UX arc: per-member spaces + navigation spine). A wayfinding strip atop
   member-scoped surfaces: who you're looking at (back to Members) + quick hops to that member's
   other surfaces, current one highlighted. Print-hidden via .no-print where used. */
.member-ctx {
  display: flex;
  align-items: center;
  gap: 0.5rem 0.8rem;
  flex-wrap: wrap;
  margin: -0.2rem 0 0.7rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--border);
}
.member-ctx-who {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  font-weight: 700;
  color: var(--text);
  text-decoration: none;
  flex: 0 0 auto;
}
.member-ctx-who:hover .member-ctx-name { text-decoration: underline; }
.member-ctx-links { display: inline-flex; flex-wrap: wrap; gap: 0.25rem; }
.member-ctx-link {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.82rem;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  text-decoration: none;
  color: var(--text-muted);
  border: 1px solid transparent;
}
.member-ctx-link:hover { background: var(--surface-2, rgba(0, 0, 0, 0.04)); color: var(--text); }
.member-ctx-link.is-active {
  background: var(--brand-weak, color-mix(in srgb, var(--brand) 12%, transparent));
  color: var(--brand);
  font-weight: 700;
}
.member-ctx-link:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; }

/* Hub-chat streaming caret (feature: hub-chat overhaul) — a soft blink while the answer streams. */
.hub-caret {
  display: inline-block;
  margin-left: 1px;
  color: var(--brand);
  animation: hub-caret-blink 1s steps(1) infinite;
}
@keyframes hub-caret-blink { 50% { opacity: 0; } }
html.a11y-reduce-motion .hub-caret { animation: none; }

/* "Why am I seeing this?" trust affordance (UX arc: trust + provenance). A quiet collapsible
   explainer for personalized/inferred surfaces. */
.why-seeing { margin: 0.2rem 0 0.55rem; }
.why-seeing-summary {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--brand);
  list-style: none;
  user-select: none;
}
.why-seeing-summary::-webkit-details-marker { display: none; }
.why-seeing-summary:hover { text-decoration: underline; }
.why-seeing-summary:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; border-radius: 4px; }
.why-seeing-body {
  margin-top: 0.4rem;
  padding: 0.5rem 0.65rem;
  font-size: 0.84rem;
  line-height: 1.5;
  color: var(--text-muted);
  background: var(--surface-2, rgba(0, 0, 0, 0.03));
  border-left: 3px solid var(--brand);
  border-radius: 0 0.4rem 0.4rem 0;
}

/* Privacy "stays on your hub" badge (UX arc: make the local-first moat visible). */
.ui-privacy-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--success, #477236);
  background: var(--success-bg, #e6f0dc);
  border: 1px solid color-mix(in srgb, var(--success, #477236) 30%, transparent);
  border-radius: 999px;
  padding: 0.12rem 0.5rem;
  white-space: nowrap;
}

/* Member space (UX arc: per-member spaces). One aggregated home per person/pet. */
.ms-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.6rem;
  flex-wrap: wrap;
  margin-bottom: 0.6rem;
}
.ms-head-id { display: flex; align-items: center; gap: 0.6rem; min-width: 0; }
.ms-avatar {
  font-size: 1.7rem;
  line-height: 1;
  width: 2.6rem;
  height: 2.6rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--surface-2, rgba(0, 0, 0, 0.05));
  border-radius: 50%;
  flex: 0 0 auto;
}
.ms-meta {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
  font-size: 0.86rem;
  color: var(--text-muted);
  margin-top: 0.15rem;
}
.ms-chips { margin: 0.35rem 0; }
.ms-chips .ui-label { display: block; margin-bottom: 0.2rem; }
.ms-chip-list { display: flex; flex-wrap: wrap; gap: 0.3rem; }
.ms-meds { list-style: none; margin: 0.1rem 0 0; padding: 0; display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.9rem; }
.ms-tiles {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(8.5rem, 1fr));
  gap: 0.6rem;
  margin: 0.7rem 0;
}
.ms-tile {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  text-align: left;
  padding: 0.7rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 0.7rem;
  background: var(--surface, #fff);
  cursor: pointer;
  font: inherit;
}
.ms-tile:hover { border-color: var(--brand); background: var(--surface-2, rgba(0, 0, 0, 0.03)); }
.ms-tile:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
.ms-tile-icon { font-size: 1.4rem; line-height: 1; }
.ms-tile-label { font-weight: 700; font-size: 0.92rem; color: var(--text); }
.ms-tile-desc { font-size: 0.76rem; color: var(--text-muted); }
