:root {
  color-scheme: light;
  --bg: #f3f5f1;
  --surface: #ffffff;
  --surface-muted: #f8faf7;
  --surface-tint: #eef6f2;
  --text: #1b2420;
  --muted: #66736d;
  --line: #dce4df;
  --line-strong: #c4d1cb;
  --accent: #176b52;
  --accent-strong: #0f4f3d;
  --accent-soft: #e3f2eb;
  --blue: #315f9f;
  --orange: #c76a22;
  --amber-soft: #fff2dd;
  --danger: #b3261e;
  --danger-soft: #fff1ef;
  --shadow: 0 18px 48px rgba(24, 41, 34, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  line-height: 1.5;
}

button,
input {
  font: inherit;
}

button {
  min-height: 42px;
  border: 0;
  border-radius: 7px;
  cursor: pointer;
  font-weight: 720;
}

button span {
  display: inline-block;
  margin-right: 6px;
  font-weight: 800;
}

.app-shell {
  width: min(1440px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 28px 0;
}

.app-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 780;
  text-transform: uppercase;
}

.app-header h1 {
  margin: 0;
  font-size: clamp(30px, 5vw, 48px);
  line-height: 1.05;
  font-weight: 800;
}

.app-header p:not(.eyebrow) {
  margin: 8px 0 0;
  color: var(--muted);
}

.rate-note {
  flex: 0 0 auto;
  border: 1px solid #bfd7cc;
  background: var(--accent-soft);
  color: var(--accent-strong);
  border-radius: 999px;
  padding: 8px 12px;
  font-size: 13px;
  font-weight: 680;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(320px, 410px) minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}

.controls,
.summary-card,
.chart-panel,
.table-panel,
.insight-panel,
.compare-panel {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.controls {
  position: sticky;
  top: 18px;
  display: grid;
  gap: 16px;
  padding: 18px;
}

.control-group {
  display: grid;
  gap: 8px;
}

.control-group.tight {
  gap: 6px;
}

.group-label,
.loan-panel legend {
  color: #38443f;
  font-size: 13px;
  font-weight: 740;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
  padding: 4px;
  border: 1px solid var(--line);
  background: var(--surface-muted);
  border-radius: 8px;
}

.segmented.two {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.segmented.three {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.segmented.range {
  grid-template-columns: repeat(3, minmax(78px, 1fr));
  width: min(100%, 330px);
}

.segmented label {
  min-width: 0;
}

.segmented input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.segmented span {
  display: flex;
  min-height: 36px;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  color: #4b5853;
  font-size: 14px;
  font-weight: 660;
  text-align: center;
  white-space: nowrap;
  cursor: pointer;
}

.segmented input:checked + span {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 5px 14px rgba(23, 107, 82, 0.22);
}

.segmented input:focus-visible + span,
button:focus-visible,
input:focus-visible {
  outline: 3px solid rgba(49, 95, 159, 0.25);
  outline-offset: 2px;
}

.loan-panel {
  min-width: 0;
  margin: 0;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-muted);
}

.loan-panel legend {
  padding: 0 4px;
}

.loan-panel.hidden {
  display: none;
}

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

.input-grid.compact {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.input-grid label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 660;
}

.input-with-unit {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  min-height: 42px;
  overflow: hidden;
  border: 1px solid #cad6d0;
  background: #fff;
  border-radius: 7px;
}

.input-with-unit input {
  width: 100%;
  min-width: 0;
  border: 0;
  background: transparent;
  color: var(--text);
  padding: 10px 11px;
  font-size: 16px;
  outline: 0;
}

.input-with-unit span {
  align-self: stretch;
  display: flex;
  align-items: center;
  padding: 0 10px;
  border-left: 1px solid var(--line);
  background: #f2f5f3;
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 36px;
  color: #34413b;
  font-size: 14px;
  font-weight: 720;
}

.check-row input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
}

.prepay-panel {
  display: grid;
  gap: 12px;
}

.prepay-options {
  display: grid;
  gap: 12px;
}

.prepay-options.is-disabled {
  display: none;
}

.error-message {
  min-height: 22px;
  margin: -4px 0 0;
  color: var(--danger);
  font-size: 13px;
  font-weight: 680;
}

.actions {
  display: grid;
  grid-template-columns: 1fr 108px;
  gap: 10px;
}

.primary-action {
  background: var(--accent);
  color: #fff;
}

.primary-action:hover {
  background: var(--accent-strong);
}

.secondary-action,
.export-action {
  border: 1px solid #c9d4cf;
  background: #fff;
  color: #394640;
}

.secondary-action:hover,
.export-action:hover {
  background: #eef3f0;
}

.results {
  display: grid;
  min-width: 0;
  gap: 18px;
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(124px, 1fr));
  gap: 10px;
}

.summary-card {
  min-width: 0;
  padding: 14px;
  box-shadow: none;
}

.summary-card.featured {
  border-color: #b7d7ca;
  background: linear-gradient(180deg, #ffffff, var(--accent-soft));
}

.summary-card.warning {
  border-color: #efd3a8;
  background: linear-gradient(180deg, #ffffff, var(--amber-soft));
}

.summary-card span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 740;
}

.summary-card strong {
  display: block;
  margin-top: 5px;
  color: var(--text);
  font-size: 19px;
  line-height: 1.2;
  word-break: break-word;
}

.summary-card small {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.chart-panel,
.table-panel,
.insight-panel,
.compare-panel {
  min-width: 0;
  padding: 16px;
}

.panel-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.panel-title h2 {
  margin: 0;
  font-size: 17px;
  line-height: 1.2;
}

.panel-title span,
.legend {
  color: var(--muted);
  font-size: 13px;
}

.insight-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.insight-item {
  min-width: 0;
  border: 1px solid var(--line);
  background: var(--surface-muted);
  border-radius: 8px;
  padding: 12px;
}

.insight-item b {
  display: block;
  color: #26322d;
  font-size: 13px;
}

.insight-item strong {
  display: block;
  margin-top: 4px;
  font-size: 18px;
  line-height: 1.2;
}

.insight-item small {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.comparison-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.comparison-card {
  border: 1px solid var(--line);
  background: var(--surface-muted);
  border-radius: 8px;
  padding: 14px;
}

.comparison-card.is-active {
  border-color: #a9cfc0;
  background: var(--surface-tint);
}

.comparison-card h3 {
  margin: 0 0 10px;
  font-size: 15px;
}

.comparison-list {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.comparison-list div {
  min-width: 0;
  border-top: 1px solid var(--line);
  padding-top: 8px;
}

.comparison-list span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.comparison-list strong {
  display: block;
  margin-top: 2px;
  font-size: 14px;
  word-break: break-word;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
}

.swatch {
  width: 10px;
  height: 10px;
  border-radius: 2px;
}

.swatch.payment {
  background: var(--accent);
}

.swatch.principal {
  background: var(--blue);
}

.swatch.interest {
  background: var(--orange);
}

.swatch.prepay {
  background: #7a4fb3;
}

canvas {
  display: block;
  width: 100%;
  aspect-ratio: 11 / 4;
  min-height: 220px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: linear-gradient(180deg, #fff, #f9fbfa);
}

.table-title {
  align-items: flex-start;
}

.table-tools {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
}

.export-action {
  min-width: 86px;
}

.table-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  max-height: 540px;
  background: #fff;
}

table {
  width: 100%;
  min-width: 820px;
  border-collapse: collapse;
}

th,
td {
  padding: 10px 12px;
  border-bottom: 1px solid #edf1ef;
  text-align: right;
  white-space: nowrap;
}

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

th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #f5f8f6;
  color: #4a5752;
  font-size: 12px;
}

td {
  color: #27312d;
  font-size: 13px;
}

td.is-prepay {
  color: #6c42a2;
  font-weight: 740;
}

tbody tr:nth-child(even) {
  background: #fbfcfb;
}

tbody tr.has-prepay {
  background: #f8f3ff;
}

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

  .controls {
    position: static;
  }

  .input-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .input-grid.compact {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .summary-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (max-width: 860px) {
  .insight-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .comparison-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 720px) {
  .app-shell {
    width: min(100% - 20px, 680px);
    padding: 18px 0;
  }

  .app-header {
    align-items: flex-start;
    flex-direction: column;
  }

  .rate-note {
    border-radius: 7px;
  }

  .controls,
  .chart-panel,
  .table-panel,
  .insight-panel,
  .compare-panel {
    padding: 13px;
  }

  .input-grid,
  .input-grid.compact,
  .summary-grid,
  .insight-grid {
    grid-template-columns: 1fr;
  }

  .actions {
    grid-template-columns: 1fr;
  }

  .panel-title,
  .table-title {
    align-items: flex-start;
    flex-direction: column;
  }

  .table-tools,
  .legend {
    justify-content: flex-start;
    width: 100%;
  }

  .segmented.range {
    width: 100%;
  }

  canvas {
    min-height: 250px;
    aspect-ratio: 4 / 3;
  }
}

@media (max-width: 460px) {
  .segmented,
  .segmented.two,
  .segmented.three,
  .segmented.range {
    grid-template-columns: 1fr;
  }

  .segmented span {
    min-height: 34px;
  }
}
