/* ============================================================
   Design tokens
   ============================================================ */
:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-alt: #f9fafb;
  --border: #e2e6ec;
  --text: #1a2233;
  --text-muted: #5b6474;
  --text-faint: #8a93a3;

  --brand: #2f5fce;
  --brand-dark: #1f3f94;
  --brand-light: #e8effe;

  --green: #14804a;
  --green-bg: #e5f6ed;
  --green-border: #b9e3cb;
  --red: #b3261e;
  --red-bg: #fbe9e8;
  --red-border: #f2c2be;

  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(20, 30, 60, 0.04), 0 4px 16px rgba(20, 30, 60, 0.06);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151d;
    --surface: #191f2b;
    --surface-alt: #1f2635;
    --border: #2b3344;
    --text: #eaedf3;
    --text-muted: #a4acbd;
    --text-faint: #737d90;

    --brand: #6b8fe8;
    --brand-dark: #4d6fce;
    --brand-light: #202c48;

    --green: #4ad495;
    --green-bg: #123425;
    --green-border: #1f5a3d;
    --red: #ff8a80;
    --red-bg: #3a1e1c;
    --red-border: #6b332e;

    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 16px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
}

body {
  overflow-x: hidden;
}

/* ============================================================
   Header
   ============================================================ */
.app-header {
  background: linear-gradient(135deg, var(--brand-dark), var(--brand));
  color: #fff;
  padding: 2rem 1.25rem 2.5rem;
}

.header-inner {
  max-width: 1200px;
  margin: 0 auto;
}

.app-header h1 {
  margin: 0 0 0.35rem;
  font-size: clamp(1.4rem, 2.8vw, 2rem);
  font-weight: 700;
  letter-spacing: -0.01em;
}

.app-header .subtitle {
  margin: 0;
  font-size: 0.95rem;
  color: rgba(255, 255, 255, 0.85);
  max-width: 60ch;
}

/* ============================================================
   Layout
   ============================================================ */
.app-main {
  max-width: 1200px;
  margin: -1.5rem auto 0;
  padding: 0 1.25rem 3rem;
}

.layout {
  display: grid;
  grid-template-columns: minmax(280px, 380px) 1fr;
  gap: 1.25rem;
  align-items: start;
}

@media (max-width: 900px) {
  .layout {
    grid-template-columns: 1fr;
  }
}

/* ============================================================
   Scenario switcher
   ============================================================ */
.scenario-bar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 0.85rem 1.1rem;
  margin-bottom: 1.25rem;
  flex-wrap: wrap;
}

.scenario-label {
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.scenario-buttons {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.scenario-btn {
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  padding: 0.45rem 0.9rem;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.scenario-btn:hover {
  border-color: var(--brand);
}

.scenario-btn.active {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
}

/* ============================================================
   Saved scenarios (browser storage + share link)
   ============================================================ */
.saved-scenarios-bar {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 0.85rem 1.1rem;
  margin-bottom: 1.25rem;
}

.scenario-save-row {
  display: flex;
  align-items: flex-end;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.scenario-title-field {
  flex: 1 1 260px;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin: 0;
}

.scenario-title-field > span {
  font-size: 0.83rem;
  font-weight: 500;
  color: var(--text-muted);
}

.scenario-title-field input {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-alt);
  padding: 0.55rem 0.7rem;
  font-size: 0.95rem;
  color: var(--text);
  font-family: var(--font);
}

.scenario-title-field input:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px var(--brand-light);
}

.scenario-save-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.btn-secondary {
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  padding: 0.55rem 0.9rem;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  font-family: var(--font);
  transition: border-color 0.15s ease, background 0.15s ease;
  white-space: nowrap;
}

.btn-secondary:hover {
  border-color: var(--brand);
  color: var(--brand);
}

.scenario-status {
  min-height: 1.1em;
  margin: 0.6rem 0 0;
  font-size: 0.82rem;
  color: var(--green);
}

.scenario-status.error {
  color: var(--red);
}

.scenario-status:empty {
  margin: 0;
}

.share-output {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.6rem;
}

.share-output[hidden] {
  display: none;
}

.share-output input {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-alt);
  padding: 0.55rem 0.7rem;
  font-size: 0.85rem;
  color: var(--text-muted);
  font-family: var(--font);
}

.saved-scenarios-list {
  margin-top: 0.85rem;
}

.scenario-empty {
  font-size: 0.85rem;
  color: var(--text-faint);
  font-style: italic;
  margin: 0.5rem 0 0;
}

.scenario-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.65rem 0;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.scenario-row:last-child {
  border-bottom: none;
}

.scenario-row-info {
  min-width: 0;
}

.scenario-row-title {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text);
  overflow-wrap: anywhere;
}

.scenario-row-date {
  font-size: 0.78rem;
  color: var(--text-faint);
  margin-top: 0.1rem;
}

.scenario-row-actions {
  display: flex;
  gap: 0.4rem;
  flex-shrink: 0;
}

.btn-tiny {
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  padding: 0.35rem 0.7rem;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  font-family: var(--font);
  transition: border-color 0.15s ease, background 0.15s ease, color 0.15s ease;
}

.btn-tiny:hover {
  border-color: var(--brand);
  color: var(--brand);
}

.btn-tiny.btn-danger:hover {
  border-color: var(--red);
  color: var(--red);
}

.btn-tiny.confirming {
  background: var(--red);
  border-color: var(--red);
  color: #fff;
}

/* Toast confirmation (e.g. "Link copied!") */
.copy-toast {
  position: fixed;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%) translateY(12px);
  background: var(--text);
  color: var(--bg);
  padding: 0.65rem 1.15rem;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 600;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 1000;
  max-width: calc(100vw - 2rem);
  text-align: center;
}

.copy-toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ============================================================
   Dashboard cards
   ============================================================ */
.dashboard {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1rem;
  margin-bottom: 1.5rem;
}

@media (max-width: 900px) {
  .dashboard { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 520px) {
  .dashboard { grid-template-columns: 1fr; }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.1rem 1.2rem;
}

.card.wide {
  grid-column: 1 / -1;
}

.card-label {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
  margin-bottom: 0.4rem;
}

.card-value {
  font-size: 1.65rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  line-height: 1.2;
}

.card-sub {
  margin-top: 0.3rem;
  font-size: 0.8rem;
  color: var(--text-faint);
}

.card.positive .card-value,
.card-value.positive { color: var(--green); }

.card.negative .card-value,
.card-value.negative { color: var(--red); }

#card-difference.positive {
  background: var(--green-bg);
  border-color: var(--green-border);
}
#card-difference.positive .card-value { color: var(--green); }

#card-difference.negative {
  background: var(--red-bg);
  border-color: var(--red-border);
}
#card-difference.negative .card-value { color: var(--red); }

/* ============================================================
   Inputs panel
   ============================================================ */
.inputs-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.input-group {
  border: none;
  padding: 0;
  margin: 0;
}

.input-group legend {
  font-size: 0.95rem;
  font-weight: 700;
  padding: 0 0 0.6rem;
  color: var(--text);
  width: 100%;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.75rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-bottom: 0.85rem;
}

.field:last-of-type {
  margin-bottom: 0;
}

.field > span {
  font-size: 0.83rem;
  font-weight: 500;
  color: var(--text-muted);
}

.input-wrap {
  display: flex;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-alt);
  overflow: hidden;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.input-wrap:focus-within {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px var(--brand-light);
}

.input-wrap input {
  flex: 1;
  min-width: 0;
  border: none;
  background: transparent;
  padding: 0.55rem 0.7rem;
  font-size: 0.95rem;
  color: var(--text);
  font-family: var(--font);
}

.input-wrap input:focus {
  outline: none;
}

.input-wrap .affix {
  padding: 0 0.7rem;
  color: var(--text-faint);
  font-size: 0.85rem;
  font-weight: 600;
  user-select: none;
}

.input-wrap.prefix .affix { border-right: 1px solid var(--border); }
.input-wrap.suffix .affix { border-left: 1px solid var(--border); }

/* Annual / Monthly period toggle, and %/$ unit toggle, embedded at the end of an input row */
.input-wrap.has-period input,
.input-wrap.has-unit input {
  padding-right: 0.4rem;
}

.period-toggle,
.unit-toggle {
  display: flex;
  align-self: stretch;
  border-left: 1px solid var(--border);
  flex-shrink: 0;
}

.period-btn,
.unit-btn {
  border: none;
  background: transparent;
  color: var(--text-faint);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  padding: 0 0.55rem;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
  font-family: var(--font);
}

.period-btn:first-child,
.unit-btn:first-child {
  border-right: 1px solid var(--border);
}

.period-btn:hover,
.unit-btn:hover {
  color: var(--text);
}

.period-btn.active,
.unit-btn.active {
  background: var(--brand);
  color: #fff;
}

/* Remove number input spinners' excess width on some browsers */
input[type="number"] {
  -moz-appearance: textfield;
}

.note {
  font-size: 0.78rem;
  color: var(--text-faint);
  line-height: 1.4;
  margin: 0.5rem 0 0;
  font-style: italic;
}

/* ============================================================
   Results panel
   ============================================================ */
.results-panel {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.result-block {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
}

.result-block h2 {
  margin: 0 0 0.85rem;
  font-size: 1.05rem;
  font-weight: 700;
}

.kv-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.kv-table tr {
  border-bottom: 1px solid var(--border);
}

.kv-table tr:last-child {
  border-bottom: none;
}

.kv-table td {
  padding: 0.55rem 0;
  vertical-align: middle;
}

.kv-table td:first-child {
  color: var(--text-muted);
}

.kv-table td:last-child {
  text-align: right;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.highlight-block {
  background: var(--surface);
}

.callout {
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-alt);
  padding: 1rem 1.1rem;
  margin-bottom: 0.9rem;
}

.callout:last-child {
  margin-bottom: 0;
}

.callout-title {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
}

.callout-value {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--brand);
  font-variant-numeric: tabular-nums;
}

.callout-sub {
  font-size: 0.85rem;
  color: var(--text-faint);
  margin-top: 0.15rem;
}

/* ============================================================
   Sensitivity table
   ============================================================ */
.sensitivity-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
  margin-top: 1.25rem;
}

.sensitivity-panel h2 {
  margin: 0 0 0.35rem;
  font-size: 1.05rem;
  font-weight: 700;
}

.table-scroll {
  overflow-x: auto;
  margin-top: 0.75rem;
}

.sensitivity-table {
  width: 100%;
  min-width: 620px;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.sensitivity-table th,
.sensitivity-table td {
  padding: 0.65rem 0.75rem;
  text-align: right;
  border-bottom: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.sensitivity-table th:first-child,
.sensitivity-table td:first-child {
  text-align: left;
}

.sensitivity-table thead th {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
  font-weight: 700;
  border-bottom: 2px solid var(--border);
}

.sensitivity-table tbody tr.current-row {
  background: var(--brand-light);
}

.sensitivity-table td.positive { color: var(--green); font-weight: 600; }
.sensitivity-table td.negative { color: var(--red); font-weight: 600; }

/* ============================================================
   Disclaimer
   ============================================================ */
.disclaimer {
  margin-top: 1.5rem;
  padding: 1rem 1.1rem;
  background: var(--surface-alt);
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
  color: var(--text-faint);
  line-height: 1.5;
}
