:root {
  --bg: #f5f7f6;
  --panel: #ffffff;
  --panel-border: #d8dfdc;
  --text: #1a2b27;
  --text-dim: #5b6b66;
  --accent: #0b7a5b;
  --accent-dim: #e6f3ef;
  --ok: #0b7a5b;
  --warn: #b8860b;
  --danger: #b3261e;
  --danger-bg: #fbe9e7;
  --input-bg: #ffffff;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.4;
}

.scope-banner {
  background: var(--accent);
  color: #fff;
  padding: 0.6rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  font-size: 0.85rem;
  text-align: center;
}
.scope-banner strong { font-size: 0.9rem; }
.scope-banner .scope-exclusions { font-size: 0.75rem; opacity: 0.85; }

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 1rem;
  width: 100%;
  box-sizing: border-box;
}

h1 { font-size: 1.4rem; margin: 0.5rem 0 0.25rem; }
.intro { color: var(--text-dim); margin-top: 0; font-size: 0.95rem; }

/* #calc-form is just a simple vertical stack: .setup-wizard (first-time
   setup) or .workspace (sidebar + results, once setup is complete) is shown
   depending on state, plus .main-results itself when in wizard mode (JS
   relocates it into .workspace once complete — see wireWizard() in app.js).
   The interesting layout — result cards pairing up, wide cards spanning
   both columns — happens inside .main-results, not here. */
#calc-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.result-mount { display: contents; }

/* Result cards + the rate slider bar are an ordered sequence of "boxes"
   inside here: a single column on mobile, a 2-column grid on larger screens
   (see the min-width media query below). Each .result-mount is
   display:contents so its single result-card child becomes the effective
   flex/grid item, keeping box order and pairing driven entirely by DOM
   order. */
.main-results {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* First-time setup wizard: one .setup-section visible at a time (others get
   `hidden` toggled by JS), with Back/Next/Finish navigation. */
.setup-wizard {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 1.25rem;
}
.wizard-progress { font-weight: 600; color: var(--accent); margin-bottom: 0.9rem; font-size: 0.95rem; }
.wizard-nav { display: flex; justify-content: space-between; gap: 0.75rem; margin-top: 1.1rem; }

.btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 0.6rem 1.2rem;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
}
.btn-secondary {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--panel-border);
}
.btn[hidden] { visibility: hidden; } /* keep Next's position stable even when Back is absent on step 1 */

/* Once setup is complete: a persistent sidebar of the same three sections
   (for quick editing) alongside the results. Single column on mobile —
   .setup-section's native <details> behaviour is what makes each one
   collapsible there; see .setup-section below. */
.workspace {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.setup-sidebar {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.setup-section {
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  background: var(--panel);
  padding: 0.9rem 1rem 1.1rem;
}
.setup-section > summary {
  font-weight: 600;
  color: var(--accent);
  cursor: pointer;
  list-style: none;
}
.setup-section > summary::-webkit-details-marker { display: none; }
.setup-section > summary::before {
  content: '▸';
  display: inline-block;
  margin-right: 0.4rem;
  transition: transform 0.15s ease;
}
.setup-section[open] > summary::before { transform: rotate(90deg); }
.setup-section > summary + * { margin-top: 0.7rem; }

/* While a step is showing inside the wizard, it isn't a collapsible
   accordion — lock it open and hide the disclosure affordance so it reads
   as a fixed wizard step, not something you could accidentally close.
   Collapsing only becomes available once sections move into the sidebar. */
.setup-wizard .setup-section > summary {
  pointer-events: none;
  cursor: default;
}
.setup-wizard .setup-section > summary::before { visibility: hidden; }

fieldset {
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  background: var(--panel);
  padding: 0.9rem 1rem 1.1rem;
  margin: 0;
}
legend {
  font-weight: 600;
  padding: 0 0.4rem;
  color: var(--accent);
}

.field {
  display: block;
  margin: 0.7rem 0;
  font-size: 0.9rem;
  font-weight: 500;
}
.field-label { display: inline-flex; align-items: center; gap: 0.3rem; }

.field input[type="number"],
.field select {
  display: block;
  width: 100%;
  margin-top: 0.3rem;
  padding: 0.55rem 0.6rem;
  font-size: 1rem;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  background: var(--input-bg);
  color: var(--text);
}

.checkbox-field {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 500;
}
.checkbox-field input[type="checkbox"] { width: 1.15rem; height: 1.15rem; flex-shrink: 0; }

.field-row {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
  font-size: 0.9rem;
  font-weight: 500;
  margin: 0.5rem 0;
}
.field-row label { display: flex; align-items: center; gap: 0.35rem; font-weight: 400; }

.field-note {
  margin: 0.1rem 0 0.6rem;
  font-size: 0.8rem;
  color: var(--text-dim);
}

.session-length .inline-inputs {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  margin-top: 0.3rem;
}
.session-length .inline-inputs input { width: 4.5rem; }

.info-btn {
  border: 1px solid var(--panel-border);
  background: var(--accent-dim);
  color: var(--accent);
  border-radius: 50%;
  width: 1.2rem;
  height: 1.2rem;
  font-size: 0.7rem;
  line-height: 1;
  cursor: pointer;
  padding: 0;
}

.itemised-expander {
  margin: 0.6rem 0;
  border: 1px dashed var(--panel-border);
  border-radius: 8px;
  padding: 0.4rem 0.7rem;
}
.itemised-expander summary {
  cursor: pointer;
  font-weight: 500;
  padding: 0.3rem 0;
}
.itemised-expander[open] summary { margin-bottom: 0.2rem; }

[data-show-when].js-hidden,
[hidden] { display: none !important; }

.result-card {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 1rem;
  margin: 0;
}
.result-card h2 { margin: 0 0 0.6rem; font-size: 1.05rem; }
.result-card h3 { margin: 0 0 0.5rem; font-size: 0.95rem; }

/* Spans both columns of the #calc-form grid (see the min-width media query)
   instead of pairing with a neighbouring box. Inert under the mobile
   single-column flex layout. */
.result-card.wide { grid-column: 1 / -1; }

/* Standalone (not nested in .main-results or .workspace) and sticky, so
   it's the first thing on the page and stays pinned to the top of the
   viewport as you scroll, regardless of wizard/sidebar state. */
.rate-slider-bar {
  position: sticky;
  top: 0;
  z-index: 15;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
}
.rate-slider-row { display: flex; align-items: center; gap: 0.9rem; flex-wrap: wrap; }
.rate-slider-label { font-weight: 600; color: var(--accent); flex-shrink: 0; min-width: 6rem; }
.rate-slider {
  flex: 1;
  min-width: 10rem;
  accent-color: var(--accent);
  height: 1.6rem;
  cursor: pointer;
}
.rate-slider-value-wrap {
  display: flex;
  align-items: center;
  gap: 0.15rem;
  font-weight: 700;
  font-size: 1.15rem;
  color: var(--accent);
  flex-shrink: 0;
}
.rate-slider-number {
  width: 5.5rem;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  background: var(--input-bg);
  color: var(--text);
  font-size: 1.15rem;
  font-weight: 700;
  text-align: right;
}

.headline {
  text-align: center;
  padding: 1.2rem 1rem;
}
.headline .big {
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--accent);
  margin: 0.2rem 0;
}
.headline .sub { color: var(--text-dim); font-size: 0.9rem; }
.headline .set-aside {
  margin: 0.9rem 0 0;
  padding-top: 0.8rem;
  border-top: 1px solid var(--panel-border);
  font-size: 0.9rem;
  text-align: left;
}

.detail-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 0.9rem;
  text-align: left;
  font-size: 0.88rem;
}
.detail-table th {
  font-weight: 600;
  color: var(--text-dim);
  padding: 0.3rem 0.4rem;
  border-bottom: 1px solid var(--panel-border);
}
.detail-table th:not(:first-child),
.detail-table td:not(:first-child) { text-align: right; font-variant-numeric: tabular-nums; }
.detail-table td {
  padding: 0.4rem;
  border-bottom: 1px solid var(--panel-border);
}
.detail-table tr:last-child td { border-bottom: none; font-weight: 700; }

/* The reverse-mode answer is rendered inside the "Reverse mode" fieldset
   itself (combined input+answer box) rather than as its own result card. */
.reverse-mode-answer:not(:empty) {
  margin-top: 0.9rem;
  padding-top: 0.8rem;
  border-top: 1px solid var(--panel-border);
  font-size: 0.9rem;
}

.waterfall { display: flex; flex-direction: column; gap: 0.4rem; }
.waterfall-row { display: flex; align-items: center; gap: 0.6rem; font-size: 0.85rem; }
.waterfall-label { width: 8.5rem; flex-shrink: 0; color: var(--text-dim); }

/* Diverging bars: track splits into two halves either side of a centre
   line. Negative bars sit in the left half and grow rightward-to-leftward
   (justify-content: flex-end pins them against the centre, flex-basis
   controls how far they reach left); positive bars sit in the right half
   and grow the opposite way. */
.waterfall-bar-track { flex: 1; min-width: 0; display: flex; background: #eef1f0; border-radius: 6px; height: 1.4rem; overflow: hidden; }
.waterfall-track-half { flex: 1 1 50%; min-width: 0; height: 100%; display: flex; }
.waterfall-track-left { justify-content: flex-end; }
.waterfall-track-right { justify-content: flex-start; }
.waterfall-track-mid { flex: 0 0 2px; height: 100%; background: var(--panel-border); }
.waterfall-bar { flex-shrink: 0; height: 100%; }
.waterfall-bar.positive { background: var(--ok); }
.waterfall-bar.negative { background: var(--danger); opacity: 0.75; }
.waterfall-value { width: 8rem; text-align: right; font-variant-numeric: tabular-nums; flex-shrink: 0; }
.waterfall-pct { color: var(--text-dim); font-size: 0.78rem; }

.compare-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.75rem;
}
.compare-card {
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 0.75rem;
}
.compare-card .figure { font-size: 1.15rem; font-weight: 700; }
.compare-card .muted { color: var(--text-dim); font-size: 0.85rem; }

.callout {
  border-radius: 8px;
  padding: 0.6rem 0.75rem;
  font-size: 0.85rem;
  margin-top: 0.6rem;
}
.callout.warn { background: #fbf3dc; color: #6b5300; border: 1px solid #eddca0; }
.callout.info { background: var(--accent-dim); color: #0a5a42; border: 1px solid #bfe3d6; }

.kv-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.kv-table td { padding: 0.3rem 0.2rem; border-bottom: 1px solid var(--panel-border); }
.kv-table td:last-child { text-align: right; font-variant-numeric: tabular-nums; }

.share-box textarea {
  width: 100%;
  min-height: 8rem;
  font-family: inherit;
  font-size: 0.85rem;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 0.6rem;
  resize: vertical;
}
.share-box button, .email-stub button {
  margin-top: 0.5rem;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 0.6rem 1rem;
  font-size: 0.9rem;
  cursor: pointer;
}
.share-box .copied-msg { color: var(--ok); font-size: 0.85rem; margin-left: 0.5rem; }

.email-stub { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; }
.email-stub input[type="email"] {
  flex: 1;
  min-width: 12rem;
  padding: 0.55rem 0.6rem;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  font-size: 0.95rem;
}
.email-stub .thanks-msg { color: var(--ok); font-size: 0.85rem; }

.disclaimer {
  max-width: 640px;
  margin: 0 auto;
  padding: 1rem;
  font-size: 0.78rem;
  color: var(--text-dim);
  border-top: 1px solid var(--panel-border);
}
.disclaimer p { margin: 0 0 0.6rem; }
.disclaimer p:last-child { margin-bottom: 0; }

.tooltip-popover {
  position: absolute;
  max-width: 16rem;
  background: #22322d;
  color: #fff;
  font-size: 0.78rem;
  padding: 0.5rem 0.65rem;
  border-radius: 6px;
  z-index: 20;
  box-shadow: 0 4px 14px rgba(0,0,0,0.2);
}

@media (min-width: 720px) {
  main {
    /* Fill most of the viewport rather than capping tightly — 94vw leaves a
       proportional gap on the edges instead of the box shrinking down to a
       narrow fixed column with large unused margins either side. */
    max-width: min(1320px, 94vw);
    padding: 1.75rem clamp(1rem, 3vw, 2.5rem);
  }
  .main-results {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
    align-items: start;
  }
  /* Sidebar sits alongside the results once setup is complete, instead of
     stacking above them. */
  .workspace {
    display: grid;
    grid-template-columns: 300px 1fr;
    gap: 1.5rem;
    align-items: start;
  }
  fieldset, .setup-section { padding: 1.25rem 1.5rem 1.5rem; }
  .result-card { padding: 1.5rem; }
  .compare-grid { grid-template-columns: 1fr 1fr; }
  .field-row { gap: 1.5rem; }
  .disclaimer { max-width: min(1320px, 94vw); padding: 1.25rem clamp(1rem, 3vw, 2.5rem); }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #10201b;
    --panel: #172721;
    --panel-border: #2b3d37;
    --text: #e8f1ee;
    --text-dim: #a6b8b2;
    --accent: #35c896;
    --accent-dim: #16332a;
    --input-bg: #10201b;
    --danger-bg: #3a1f1c;
  }
  .waterfall-bar-track { background: #223129; }
  .callout.warn { background: #3a3216; color: #f0d98c; border-color: #5a4c1e; }
}
