/* Site-wide layout primitives shared across every calculator page.
 * Loaded after tokens.css; Tailwind utilities still handle most spacing
 * but these classes guarantee identical chrome (page header row,
 * stat cards, export bar, saved scenarios) across pages.
 */

/* === Phase 1: mobile-safe globals ===
 * These rules apply at every viewport size but are tuned to prevent the
 * common mobile pitfalls (horizontal page scroll, iOS auto-zoom on inputs,
 * sub-44px tap targets, runaway media). Desktop behaviour is unaffected
 * because the constraints are upper bounds. */
*, *::before, *::after { box-sizing: border-box; }
html, body {
  max-width: 100vw;
  overflow-x: hidden;
}
img, video, canvas, svg {
  max-width: 100%;
  height: auto;
}
/* SVG icons inside buttons/links use width/height attrs for sizing — restore
   the height: auto override so they don't stretch. */
button svg, a svg, .stat-card__icon svg, .lock-pill svg, .tb-info svg {
  height: auto;
}

@media (max-width: 639px) {
  /* iOS Safari zooms into any input whose computed font-size < 16px. */
  input, select, textarea {
    font-size: 16px;
  }
  /* Apple HIG / Material guidance — minimum tappable target. Buttons that
     are visually small still expand their hit area via padding. */
  button, .btn, [role="button"], a.btn, input[type="button"], input[type="submit"] {
    min-height: 44px;
  }
  /* Card padding tightens on mobile via the --card-pad token clamp(). */
}

/* Hide native scrollbar on the horizontally scrolling top nav. */
.no-scrollbar { scrollbar-width: none; -ms-overflow-style: none; }
.no-scrollbar::-webkit-scrollbar { display: none; }

/* Inline info-icon button used in form labels (e.g. paycheck pre-tax tooltip). */
.tb-info {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1rem;
  height: 1rem;
  padding: 0;
  margin: 0;
  border: none;
  background: transparent;
  color: var(--color-ink-faint);
  cursor: help;
}
.tb-info:hover, .tb-info:focus { color: var(--color-primary); outline: none; }
.tb-info svg, .tb-info i { width: 0.875rem; height: 0.875rem; }

/* Page shell — keeps top spacing identical across pages. */
.page-shell {
  width: 100%;
  max-width: var(--page-max-width);
  margin: 0 auto;
  padding-left: var(--page-gutter);
  padding-right: var(--page-gutter);
  padding-top: var(--space-lg);
  padding-bottom: var(--space-lg);
}
@media (min-width: 768px) {
  .page-shell {
    padding-left: var(--page-gutter-md);
    padding-right: var(--page-gutter-md);
    padding-top: var(--space-xl);
    padding-bottom: var(--space-xl);
  }
}

/* Page title row: H1 stack on the left, "Saved to this device" chip on the right. */
.page-title-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-md);
  margin-bottom: var(--space-lg);
}
.page-title-row .page-title-stack {
  min-width: 0;
  flex: 1 1 auto;
}
.page-title-row h1 {
  /* 26px floor at ~375px viewport, 40px (var(--text-3xl)) ceiling on desktop */
  font-size: clamp(1.625rem, 5.5vw, var(--text-3xl));
  font-weight: 800;
  letter-spacing: -0.025em;
  color: var(--color-ink);
  line-height: 1.2;
  margin: 0 0 0.5rem;
}
.page-title-row .page-last-updated {
  color: var(--color-ink-soft);
  /* 13px floor on mobile, var(--text-sm) (14px) ceiling on desktop */
  font-size: clamp(0.8125rem, 2.8vw, var(--text-sm));
  margin: 0;
}
.page-title-row .page-subhead {
  color: var(--color-ink-muted);
  font-size: var(--text-base);
  line-height: 1.6;
  margin: var(--space-sm) 0 0;
  max-width: 60ch;
}

/* "Saved to this device" lock chip — injected by partials/page-header.html. */
.device-save-chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-sm);
  font-size: var(--text-xs);
  color: var(--color-ink-soft);
  white-space: nowrap;
  flex-shrink: 0;
}
.device-save-chip .lock-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  color: var(--color-ink-muted);
}
.device-save-chip svg {
  width: 0.875rem;
  height: 0.875rem;
  color: var(--color-ink-soft);
}
.device-save-chip .saved-flash {
  color: var(--color-success);
  font-weight: 600;
  opacity: 0;
  transition: opacity 200ms ease;
}
.device-save-chip .saved-flash.is-on {
  opacity: 1;
}

/* Stat cards row — three equal cards across, one per row on mobile. */
.stat-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--space-md);
  margin-bottom: var(--section-gap);
}
@media (min-width: 768px) {
  .stat-row {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}
.stat-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-card);
  padding: var(--space-md);
  display: flex;
  align-items: center;
  gap: var(--space-md);
  min-width: 0;
}
.stat-card__icon {
  width: 2.5rem;
  height: 2.5rem;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.stat-card__icon svg { width: 1.25rem; height: 1.25rem; }
.stat-card__icon.is-blue { background: var(--color-primary-soft); color: var(--color-primary); }
.stat-card__icon.is-green { background: var(--color-success-soft); color: var(--color-success); }
.stat-card__icon.is-red { background: var(--color-danger-soft); color: var(--color-danger); }
.stat-card__icon.is-amber { background: var(--color-warning-soft); color: var(--color-warning); }
.stat-card__icon.is-indigo { background: #eef2ff; color: #4f46e5; }
.stat-card__body { min-width: 0; }
.stat-card__label {
  font-size: 0.6875rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-ink-faint);
  margin: 0;
}
.stat-card__value {
  margin: 0.125rem 0 0;
  font-size: var(--text-lg);
  font-weight: 700;
  color: var(--color-ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.stat-card__value.is-positive { color: var(--color-success); }
.stat-card__value.is-negative { color: var(--color-danger); }

/* Mobile: shrink stat cards so 3 stacked cards stay visible above the fold. */
@media (max-width: 767px) {
  .stat-row {
    gap: var(--space-sm);
    margin-bottom: var(--space-md);
  }
  .stat-card {
    padding: var(--space-sm) var(--space-md);
    gap: var(--space-sm);
  }
  .stat-card__icon {
    width: 2rem;
    height: 2rem;
  }
  .stat-card__icon svg { width: 1rem; height: 1rem; }
  .stat-card__label { font-size: 0.625rem; }
  .stat-card__value { font-size: var(--text-base); }
}

/* Phase 4: tighter still on phones (<= iPhone widths). Goal is ~72px tall
   per card so 3 stacked cards fit in roughly 240px. Icon left, label-over-
   value column right. */
@media (max-width: 639px) {
  .stat-card {
    padding: 12px;
    gap: var(--space-sm);
    min-height: 0;
    max-height: 72px;
    align-items: center;
  }
  .stat-card__icon {
    width: 32px;
    height: 32px;
  }
  .stat-card__icon svg { width: 18px; height: 18px; }
  .stat-card__body {
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
  .stat-card__value {
    font-size: 22px;
    line-height: 1.1;
  }
}

/* Calculator main 60/40 grid: a wider results column on the left so the
   primary breakdown card has room to breathe, with a balanced sidebar. */
.calc-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--grid-gap);
}
@media (min-width: 1024px) {
  .calc-grid {
    grid-template-columns: minmax(0, 3fr) minmax(0, 2fr);
  }
}
.calc-grid > .calc-main { min-width: 0; display: flex; flex-direction: column; gap: var(--section-gap); }
.calc-grid > .calc-sidebar { min-width: 0; display: flex; flex-direction: column; gap: var(--section-gap); }

/* Utility row beneath .calc-grid: hosts secondary controls like
   Saved Scenarios + Export at full page width. 2/1 split on desktop,
   stacks on mobile. Top spacing matches the inter-section gap so the
   About card below sits flush against it. */
.calc-utility-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--grid-gap);
  margin-top: var(--section-gap);
  align-items: start;
}
@media (min-width: 1024px) {
  .calc-utility-row {
    grid-template-columns: minmax(0, 2fr) minmax(0, 1fr);
  }
}

/* Export bar — Share / CSV / PDF row beneath the main results area. */
.export-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-md);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-md) var(--space-lg);
  box-shadow: var(--shadow-card);
}
.export-bar__title {
  font-size: var(--text-xs);
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--color-ink-faint);
  margin: 0;
}
.export-bar__actions { display: flex; flex-wrap: wrap; gap: var(--space-sm); }
.export-bar__btn {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  font-size: var(--text-xs);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0.5rem 0.875rem;
  border-radius: var(--radius-sm);
  background: var(--color-primary-soft);
  color: var(--color-primary);
  border: 1px solid transparent;
  cursor: pointer;
  transition: background 150ms ease, color 150ms ease;
}
.export-bar__btn:hover { background: #dbeafe; }

/* Flush variant: lives inside another card (e.g. the loan-payoff schedule
   card) and reads as that card's footer instead of a standalone tile.
   Use the doubled `.export-bar.export-bar--flush` selector so we always win
   over the base `.export-bar` rule no matter the cascade order. */
.export-bar.export-bar--flush {
  background: #f8fafc;
  border: 0;
  border-top: 1px solid var(--color-border);
  border-top-left-radius: 0;
  border-top-right-radius: 0;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  box-shadow: none;
  padding: var(--space-md) var(--space-lg);
}
.export-bar__btn.is-csv { background: var(--color-success-soft); color: var(--color-success); }
.export-bar__btn.is-csv:hover { background: #d1fae5; }
.export-bar__btn.is-premium {
  background: #f1f5f9;
  color: var(--color-ink-soft);
  cursor: not-allowed;
  opacity: 0.85;
}
.export-bar__btn.is-premium:hover { background: #f1f5f9; }
.export-bar__premium-pill {
  font-size: 0.625rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  background: linear-gradient(90deg, #f59e0b, #d97706);
  color: #fff;
  padding: 0.125rem 0.4rem;
  border-radius: var(--radius-pill);
  margin-left: 0.375rem;
  text-transform: uppercase;
}

/* Saved scenarios card — pattern matches the existing loan-payoff component. */
.saved-scenarios-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--card-pad);
  box-shadow: var(--shadow-card);
}
.saved-scenarios-card h3 {
  font-size: var(--text-base);
  font-weight: 700;
  color: var(--color-ink);
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0 0 var(--space-md);
}
.saved-scenarios-card .input-row {
  display: flex;
  gap: var(--space-sm);
  margin-bottom: var(--space-md);
}
.saved-scenarios-card .input-row input {
  flex: 1;
  min-width: 0;
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
  background: #f8fafc;
  font-size: var(--text-sm);
}
.saved-scenarios-card .input-row input:focus {
  outline: none;
  box-shadow: 0 0 0 2px var(--color-primary);
  border-color: var(--color-primary);
}
.saved-scenarios-card .save-btn {
  background: #7c3aed;
  color: #fff;
  font-size: var(--text-xs);
  font-weight: 700;
  text-transform: uppercase;
  padding: 0.5rem 1rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  white-space: nowrap;
  border: none;
}
.saved-scenarios-card .save-btn:hover { background: #6d28d9; }
.saved-scenarios-card .scenarios-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-height: 15rem;
  overflow-y: auto;
}
.saved-scenarios-card .scenarios-empty {
  font-size: var(--text-xs);
  color: var(--color-ink-faint);
  text-align: center;
  padding: 1rem 0;
}
.saved-scenarios-card .scenario-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #f8fafc;
  border-radius: var(--radius-sm);
  padding: 0.625rem 0.75rem;
  transition: background 150ms ease;
}
.saved-scenarios-card .scenario-item:hover { background: #eff2f6; }
.saved-scenarios-card .scenario-info { cursor: pointer; flex: 1; min-width: 0; }
.saved-scenarios-card .scenario-name {
  font-weight: 600;
  font-size: var(--text-sm);
  color: var(--color-ink-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.saved-scenarios-card .scenario-meta {
  font-size: 0.625rem;
  color: var(--color-ink-faint);
}
.saved-scenarios-card .scenario-delete {
  margin-left: 0.5rem;
  color: var(--color-ink-faint);
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 0.25rem;
  border-radius: var(--radius-sm);
  transition: color 150ms ease, background 150ms ease;
}
.saved-scenarios-card .scenario-delete:hover {
  color: var(--color-danger);
  background: var(--color-danger-soft);
}
.saved-scenarios-card .scenario-delete svg { width: 1rem; height: 1rem; }

/* Toast for share-link copied confirmations. */
.tb-toast {
  position: fixed;
  left: 50%;
  bottom: 5rem;
  transform: translateX(-50%) translateY(0.5rem);
  background: var(--color-ink);
  color: #fff;
  padding: 0.625rem 1rem;
  border-radius: var(--radius-pill);
  font-size: var(--text-sm);
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease, transform 180ms ease;
  z-index: 9999;
}
.tb-toast.is-on {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Single-line subtle disclaimer style used under primary results cards. */
.calc-disclaimer {
  font-size: var(--text-xs);
  color: var(--color-ink-soft);
  margin: var(--space-sm) 0 0;
  line-height: 1.6;
}

/* === Mobile nav (hamburger + slide-in panel) ===
 * Active below 768px. The desktop horizontal pill nav (hidden md:flex) is
 * untouched at >= 768px; this block only styles the slide-in panel and
 * its backdrop. Logic lives in js/includes.js (mounted once partials are
 * injected). */
.tb-mobile-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms ease;
  z-index: 60;
}
.tb-mobile-backdrop.is-open {
  opacity: 1;
  pointer-events: auto;
}

.tb-mobile-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(80vw, 320px);
  background: #fff;
  box-shadow: -8px 0 24px rgba(15, 23, 42, 0.12);
  transform: translateX(100%);
  transition: transform 200ms ease;
  z-index: 70;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  visibility: hidden; /* off-DOM-flow when closed; toggled in is-open */
}
.tb-mobile-panel.is-open {
  transform: translateX(0);
  visibility: visible;
}

.tb-mobile-panel__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-md) var(--space-md);
  border-bottom: 1px solid var(--color-border);
}

.tb-mobile-panel__list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: var(--space-sm) var(--space-md) var(--space-lg);
}

.tb-mobile-link {
  display: block;
  width: 100%;
}

.tb-mobile-divider {
  display: block;
  height: 1px;
  background: var(--color-border);
  margin: var(--space-sm) 0;
}

body.tb-nav-open {
  overflow: hidden;
}

/* === Phase 3: page header tweaks on mobile ===
 * The page title row keeps its desktop layout above 640px. Below that the
 * "Saved to this device" chip shrinks to icon-only (text reappears at >= 480px
 * if there's room) and the row aligns center-vertically so the chip sits
 * inline with the H1 baseline. */
.page-h1-supertag {
  display: none;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: 0.6875rem;        /* 11px */
  font-weight: 700;
  color: var(--color-ink-muted);
  margin: 0 0 0.25rem;
}
.page-h1-short { display: none; }
.page-h1-full { display: inline; }

@media (max-width: 639px) {
  .page-title-row {
    align-items: center;
    flex-wrap: nowrap;
  }
  .page-title-row .page-title-stack {
    flex: 1 1 0;
  }
  .device-save-chip {
    font-size: 0.6875rem;
    flex-shrink: 0;
  }
  /* Icon-only on the smallest phones */
  .device-save-chip .lock-pill > span { display: none; }

  /* Paycheck H1 swap: hide the long full title, show the short label.
     The supertag (uppercase eyebrow) sits above. */
  .page-h1-full { display: none; }
  .page-h1-short { display: inline; }
  .page-h1-supertag { display: block; }
}

/* Bring back the chip text once there's enough horizontal room. */
@media (min-width: 480px) and (max-width: 639px) {
  .device-save-chip .lock-pill > span { display: inline; }
}

/* === Phase 5: defensive collapse for two-column rows on mobile ===
 * Tailwind's md:grid-cols-2 already collapses to 1 col below 768px because
 * the base `grid-cols-1` utility is paired in every page. This rule is a
 * safety net so any inline override or future Tailwind-CDN drift can't
 * accidentally split the row on a phone-sized viewport. */
@media (max-width: 767px) {
  .grid.md\:grid-cols-2,
  .grid.md\:grid-cols-3,
  .grid.md\:grid-cols-4 {
    grid-template-columns: 1fr !important;
  }
}

/* === Phase 7: inputs, buttons, forms on mobile === */
@media (max-width: 639px) {
  /* Save Scenario row stacks vertically with full-width fields. */
  .saved-scenarios-card .input-row {
    flex-direction: column;
    align-items: stretch;
  }
  .saved-scenarios-card .input-row input,
  .saved-scenarios-card .save-btn {
    width: 100%;
  }

  /* Export bar: title above, action buttons stretch full width. The
     three buttons share the row in equal thirds with wrap fallback. */
  .export-bar {
    flex-direction: column;
    align-items: stretch;
  }
  .export-bar__actions {
    width: 100%;
    justify-content: stretch;
  }
  .export-bar__btn {
    flex: 1 1 calc(33% - 8px);
    justify-content: center;
  }
}
