:root {
  --bg: #f5f7fb;
  --bg-elev: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-soft: #eef2ff;
  --accent: #10b981;
  --accent-soft: #ecfdf5;
  --danger: #ef4444;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 8px 24px rgba(15, 23, 42, 0.06);
  --radius: 14px;
  --radius-sm: 8px;
}

[data-theme="dark"] {
  --bg: #0b1020;
  --bg-elev: #121934;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --border: #1e2748;
  --primary: #818cf8;
  --primary-hover: #a5b4fc;
  --primary-soft: #1e1b4b;
  --accent: #34d399;
  --accent-soft: #064e3b;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.2), 0 12px 40px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: 'Inter', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.app {
  max-width: 1320px;
  margin: 0 auto;
  padding: 24px;
}

/* Hero */
.hero {
  background: linear-gradient(135deg, #1e293b 0%, #334155 55%, #475569 100%);
  border-radius: var(--radius);
  padding: 26px 30px;
  color: white;
  margin-bottom: 24px;
  box-shadow: var(--shadow);
  position: relative;
  overflow: hidden;
}
.hero::after {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at 85% 20%, rgba(99, 102, 241, 0.25), transparent 50%);
  pointer-events: none;
}
[data-theme="dark"] .hero {
  background: linear-gradient(135deg, #0f172a 0%, #1e293b 60%, #1e1b4b 100%);
}

.hero-inner {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
}

.brand { display: flex; align-items: center; gap: 16px; }

.logo {
  width: 52px; height: 52px;
  background: rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(8px);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 24px;
  border: 1px solid rgba(255,255,255,0.3);
}

.hero h1 {
  margin: 0;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.tagline {
  margin: 4px 0 0;
  opacity: 0.9;
  font-size: 14px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  position: relative;
  z-index: 1;
}
.header-link {
  color: rgba(255,255,255,0.9);
  text-decoration: none;
  font-size: 13px;
  font-weight: 500;
  padding: 9px 14px;
  border-radius: 999px;
  background: rgba(255,255,255,0.1);
  border: 1px solid rgba(255,255,255,0.18);
  transition: background 0.15s;
}
.header-link:hover { background: rgba(255,255,255,0.2); }
.brand, .hero h1, .tagline, .logo { position: relative; z-index: 1; }

.theme-toggle {
  width: 42px; height: 42px;
  background: rgba(255,255,255,0.15);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.2s;
  font-size: 18px;
  border: 1px solid rgba(255,255,255,0.25);
}
.theme-toggle:hover { background: rgba(255,255,255,0.25); }

/* Layout */
.grid {
  display: grid;
  grid-template-columns: 380px 1fr;
  gap: 24px;
  align-items: start;
}

.inputs {
  position: sticky;
  top: 24px;
  max-height: calc(100vh - 48px);
  overflow-y: auto;
}

@media (max-width: 960px) {
  .grid { grid-template-columns: 1fr; }
  .inputs { position: static; max-height: none; overflow: visible; }
}

.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--shadow);
}

h2 {
  font-size: 15px;
  font-weight: 600;
  margin: 0 0 14px;
  letter-spacing: -0.01em;
  color: var(--text);
  text-transform: uppercase;
  font-size: 12px;
  letter-spacing: 0.06em;
  color: var(--muted);
}

hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 20px 0;
}

/* Forms */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }

.row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

label {
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
}

input, select {
  width: 100%;
  padding: 10px 12px;
  font-size: 14px;
  font-family: inherit;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  transition: border-color 0.15s, box-shadow 0.15s;
}

input:focus, select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

.input-wrap {
  position: relative;
  display: flex;
  align-items: center;
}
.input-wrap .prefix {
  position: absolute;
  left: 12px;
  color: var(--muted);
  font-weight: 500;
  font-size: 14px;
  pointer-events: none;
}
.input-wrap input { padding-left: 28px; }

/* Buttons */
.btn {
  padding: 11px 18px;
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  border-radius: var(--radius-sm);
  border: none;
  cursor: pointer;
  transition: all 0.15s;
  width: 100%;
}
.btn.primary {
  background: var(--primary);
  color: white;
  box-shadow: 0 4px 12px rgba(79,70,229,0.3);
}
.btn.primary:hover { background: var(--primary-hover); transform: translateY(-1px); }
.btn.secondary {
  background: var(--primary-soft);
  color: var(--primary);
}
.btn.secondary:hover { background: var(--border); }
.btn.small { width: auto; padding: 7px 14px; font-size: 13px; }

/* One-off list */
.one-off-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 12px;
}
.one-off-row {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 8px;
  align-items: end;
}
.one-off-row .remove {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  cursor: pointer;
  font-size: 14px;
}
.one-off-row .remove:hover { background: var(--bg); }

.one-off-empty {
  color: var(--muted);
  font-size: 13px;
  font-style: italic;
  padding: 8px 0;
}

/* Summary stats */
.results {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}
@media (max-width: 700px) {
  .summary-grid { grid-template-columns: repeat(2, 1fr); }
}

.stat {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 18px 20px;
}
.stat .label {
  font-size: 12px;
  color: var(--muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.stat .value {
  font-size: 24px;
  font-weight: 700;
  letter-spacing: -0.02em;
}
.stat.highlight {
  background: linear-gradient(135deg, var(--accent-soft) 0%, var(--primary-soft) 100%);
  border-color: transparent;
}
.stat.highlight .value { color: var(--accent); }
[data-theme="dark"] .stat.highlight .value { color: #6ee7b7; }

/* Chart */
.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 16px;
}
.chart-header h2 { margin: 0; }
.chart-toggle { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 500;
  border-radius: 999px;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.15s;
}
.chip:hover { color: var(--text); }
.chip.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}
.chart-wrap {
  position: relative;
  height: 340px;
}

/* Totals block */
.totals-block {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}
.totals-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 10px;
  gap: 12px;
  flex-wrap: wrap;
}
.totals-title {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.totals-grand {
  font-size: 22px;
  font-weight: 800;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  background: linear-gradient(135deg, #4f46e5, #10b981);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.stack-bar {
  display: flex;
  height: 14px;
  border-radius: 999px;
  overflow: hidden;
  background: var(--bg);
  border: 1px solid var(--border);
  margin-bottom: 14px;
}
.stack-seg {
  height: 100%;
  transition: width 0.4s cubic-bezier(.4,0,.2,1);
  min-width: 0;
}
.stack-seg[data-kind="regular"]   { background: linear-gradient(180deg, #6366f1, #4f46e5); }
.stack-seg[data-kind="recurring"] { background: linear-gradient(180deg, #34d399, #10b981); }
.stack-seg[data-kind="oneoff"]    { background: linear-gradient(180deg, #fbbf24, #f59e0b); }

.totals-legend {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}
@media (max-width: 700px) {
  .totals-legend { grid-template-columns: 1fr; }
}
.legend-item {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.legend-item .swatch {
  width: 10px;
  flex: 0 0 10px;
  align-self: stretch;
  border-radius: 4px;
}
.legend-item[data-kind="regular"] .swatch   { background: #4f46e5; }
.legend-item[data-kind="recurring"] .swatch { background: #10b981; }
.legend-item[data-kind="oneoff"] .swatch    { background: #f59e0b; }
.total-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  font-weight: 600;
  margin-bottom: 2px;
}
.total-value {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.01em;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* Schedule */
.schedule-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 16px;
}
.schedule-header h2 { margin: 0; }
.schedule-controls { display: flex; align-items: center; gap: 16px; }
.checkbox {
  display: flex; align-items: center; gap: 6px;
  font-size: 13px; color: var(--muted);
  cursor: pointer;
}
.checkbox input { width: auto; }

.table-wrap {
  overflow-x: auto;
  max-height: 500px;
  overflow-y: auto;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
thead {
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 1;
}
th, td {
  padding: 10px 14px;
  text-align: right;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
th:first-child, td:first-child,
th:nth-child(2), td:nth-child(2) { text-align: left; }
th {
  font-weight: 600;
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
tbody tr:hover { background: var(--bg); }
tbody tr.overpay-row td { font-weight: 600; color: var(--accent); }
[data-theme="dark"] tbody tr.overpay-row td { color: #6ee7b7; }
tbody tr.year-row { background: var(--primary-soft); font-weight: 600; }

/* Advanced */
.advanced { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 0; background: var(--bg); }
.advanced summary {
  list-style: none;
  cursor: pointer;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 600;
  font-size: 13px;
  color: var(--text);
  user-select: none;
}
.advanced summary::-webkit-details-marker { display: none; }
.advanced-title { text-transform: uppercase; letter-spacing: 0.06em; font-size: 12px; color: var(--muted); }
.advanced-chevron { transition: transform 0.2s; color: var(--muted); }
.advanced[open] .advanced-chevron { transform: rotate(180deg); }
.advanced-body { padding: 4px 14px 14px; }
.advanced-body h3 {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin: 6px 0 4px;
}
.advanced-body .hint { font-size: 12px; color: var(--muted); margin: 0 0 10px; }

.remortgage-row {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 8px;
  align-items: end;
}
.remortgage-row .full { grid-column: 1 / -1; }

/* Glossary */
.glossary {
  display: grid;
  grid-template-columns: 240px 1fr;
  gap: 24px;
  align-items: start;
}
@media (max-width: 800px) {
  .glossary { grid-template-columns: 1fr; }
  .glossary-toc { position: static !important; }
}
.glossary-toc {
  position: sticky;
  top: 24px;
  padding: 18px 20px;
  grid-column: 1;
  grid-row: 1;
  z-index: 2;
}
.glossary-section { grid-column: 2; }
@media (max-width: 800px) {
  .glossary-toc, .glossary-section { grid-column: 1; }
  .glossary-toc { grid-row: auto; }
}
.glossary-toc h2 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin: 0 0 10px;
}
.glossary-toc ul {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.glossary-toc a {
  display: block;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  color: var(--text);
  text-decoration: none;
  font-size: 14px;
  font-weight: 500;
  transition: background 0.15s;
}
.glossary-toc a:hover { background: var(--bg); color: var(--primary); }

.glossary-section {
  margin-bottom: 24px;
  padding: 28px 32px;
  scroll-margin-top: 24px;
}
.glossary-section > h2 {
  font-size: 22px;
  font-weight: 700;
  text-transform: none;
  letter-spacing: -0.02em;
  color: var(--text);
  margin: 0 0 8px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--border);
}
.glossary-section article { padding: 18px 0; border-bottom: 1px solid var(--border); }
.glossary-section article:last-child { border-bottom: none; padding-bottom: 0; }
.glossary-section h3 {
  font-size: 16px;
  font-weight: 600;
  margin: 0 0 6px;
  color: var(--text);
}
.glossary-section p {
  margin: 6px 0;
  font-size: 14px;
  line-height: 1.65;
  color: var(--text);
  opacity: 0.85;
}
.glossary-section a { color: var(--primary); text-decoration: none; font-weight: 500; }
.glossary-section a:hover { text-decoration: underline; }
.glossary-section pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  overflow-x: auto;
}

@media (max-width: 640px) {
  .glossary-section { padding: 18px; }
  .glossary-section > h2 { font-size: 18px; }
}

footer {
  margin-top: 32px;
  text-align: center;
  color: var(--muted);
  font-size: 12px;
}

/* ===== Mobile overrides (kept at end so they win the cascade) ===== */
@media (max-width: 640px) {
  .app { padding: 12px; }
  .hero { padding: 18px 16px; border-radius: 12px; margin-bottom: 16px; }
  .hero-inner { gap: 10px; }
  .hero h1 { font-size: 18px; }
  .tagline { font-size: 12px; margin-top: 2px; }
  .brand { gap: 12px; }
  .logo { width: 40px; height: 40px; font-size: 18px; border-radius: 10px; }
  .header-link { padding: 6px 10px; font-size: 12px; }
  .theme-toggle { width: 36px; height: 36px; font-size: 15px; }

  .grid { gap: 14px; }
  .results { gap: 14px; }
  .card { padding: 14px; border-radius: 12px; }

  h2 { font-size: 11px; margin-bottom: 10px; }
  hr { margin: 14px 0; }
  .field { margin-bottom: 10px; }
  .row { grid-template-columns: 1fr; gap: 10px; }
  input, select { padding: 9px 11px; font-size: 14px; }

  .one-off-row, .remortgage-row {
    grid-template-columns: 1fr 1fr auto;
    gap: 6px;
  }
  .one-off-row .remove, .remortgage-row .remove { padding: 8px 10px; }

  .summary-grid { grid-template-columns: 1fr 1fr; gap: 10px; }
  .stat { padding: 12px 14px; gap: 4px; }
  .stat .label { font-size: 10px; }
  .stat .value { font-size: 17px; }

  .chart-header, .schedule-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }
  .chart-toggle {
    width: 100%;
    overflow-x: auto;
    flex-wrap: nowrap;
    padding-bottom: 4px;
    -webkit-overflow-scrolling: touch;
  }
  .chart-toggle::-webkit-scrollbar { display: none; }
  .chip { flex-shrink: 0; }
  .chart-wrap { height: 240px; }

  .totals-head { margin-bottom: 8px; }
  .totals-title { font-size: 10px; }
  .totals-grand { font-size: 18px; }
  .totals-legend { grid-template-columns: 1fr; gap: 8px; }
  .legend-item { padding: 8px 10px; }
  .total-value { font-size: 14px; }

  .table-wrap { max-height: 360px; }
  th, td { padding: 7px 9px; font-size: 12px; }

  footer { margin-top: 20px; }
}

@media (max-width: 400px) {
  .summary-grid { grid-template-columns: 1fr; }
  .hero-inner { flex-wrap: wrap; }
  .header-actions { margin-left: auto; }
}
