:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-soft: #eff6ff;
  --ink: #0f172a;
  --text: #334155;
  --muted: #64748b;
  --line: #e2e8f0;
  --line-strong: #cbd5e1;
  --surface: #ffffff;
  --canvas: #f8fafc;
  --safe: #15803d;
  --safe-soft: #f0fdf4;
  --warn: #b45309;
  --warn-soft: #fffbeb;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --sidebar-width: 348px;
  --topbar-height: 56px;
  --font: "Malgun Gothic", "Apple SD Gothic Neo", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  min-width: 320px;
  color: var(--text);
  background: var(--canvas);
  font-family: var(--font);
  font-size: 14px;
  letter-spacing: 0;
}

button,
input,
select {
  font: inherit;
}

button,
select {
  cursor: pointer;
}

button:focus-visible,
input:focus-visible,
select:focus-visible,
a:focus-visible {
  outline: 3px solid rgba(37, 99, 235, 0.24);
  outline-offset: 2px;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  min-height: var(--topbar-height);
  padding: 0 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--primary);
  text-decoration: none;
  font-size: 15px;
  font-weight: 800;
  white-space: nowrap;
}

.brand img {
  display: block;
  object-fit: contain;
}

.top-tabs {
  display: flex;
  align-items: center;
  gap: 3px;
  margin-left: 24px;
}

.top-tab,
.icon-button,
.text-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  background: transparent;
  color: var(--muted);
}

.top-tab {
  gap: 6px;
  min-height: 34px;
  padding: 0 11px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 700;
}

.top-tab:hover {
  color: var(--ink);
  background: #f1f5f9;
}

.top-tab.is-active {
  color: var(--primary);
  background: var(--primary-soft);
}

.top-tab svg,
.text-button svg,
.inline-action svg {
  width: 15px;
  height: 15px;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-left: auto;
}

.icon-button {
  width: 34px;
  height: 34px;
  border-radius: 6px;
}

.icon-button:hover {
  color: var(--primary);
  background: var(--primary-soft);
}

.icon-button svg {
  width: 18px;
  height: 18px;
}

.text-button {
  gap: 5px;
  min-height: 34px;
  padding: 0 8px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 700;
}

.text-button:hover {
  color: var(--danger);
  background: var(--danger-soft);
}

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
  min-height: calc(100vh - var(--topbar-height));
}

.sidebar {
  position: sticky;
  top: var(--topbar-height);
  align-self: start;
  height: calc(100vh - var(--topbar-height));
  overflow-y: auto;
  background: var(--surface);
  border-right: 1px solid var(--line);
}

.sidebar-heading,
.result-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.sidebar-heading {
  position: sticky;
  top: 0;
  z-index: 5;
  min-height: 66px;
  padding: 10px 14px;
  background: rgba(255, 255, 255, 0.96);
  border-bottom: 1px solid var(--line);
}

h1,
h2,
h3,
p {
  margin: 0;
}

.sidebar-heading h1,
.result-header h2 {
  color: var(--ink);
  font-size: 17px;
  line-height: 1.35;
}

.eyebrow {
  margin-bottom: 2px;
  color: var(--primary);
  font-size: 10px;
  font-weight: 800;
}

.status-chip {
  padding: 4px 7px;
  color: var(--safe);
  background: var(--safe-soft);
  border: 1px solid #bbf7d0;
  border-radius: 6px;
  font-size: 10px;
  font-weight: 800;
}

.input-section {
  padding: 14px;
  border-bottom: 1px solid var(--line);
}

#calculator-form {
  display: flex;
  flex-direction: column;
}

.preset-section {
  order: 1;
}

.collateral-section {
  order: 2;
}

.income-section {
  order: 4;
}

.loan-section {
  order: 3;
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 13px;
}

.section-title h2 {
  color: var(--ink);
  font-size: 13px;
}

.section-title svg {
  width: 16px;
  height: 16px;
  color: #94a3b8;
}

.field + .field {
  margin-top: 15px;
}

.field label,
.label-row {
  color: var(--text);
  font-size: 12px;
  font-weight: 700;
}

.label-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.label-row output {
  color: var(--primary);
  font-size: 12px;
}

.number-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  min-height: 36px;
  margin-top: 6px;
  padding: 0 10px;
  background: var(--canvas);
  border: 1px solid var(--line);
  border-radius: 6px;
}

.number-row:focus-within {
  background: var(--surface);
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.number-row input {
  width: 100%;
  min-width: 0;
  padding: 7px 8px 7px 0;
  color: var(--ink);
  background: transparent;
  border: 0;
  outline: 0;
  font-weight: 800;
  text-align: right;
}

.number-row span {
  color: var(--muted);
  font-size: 11px;
  white-space: nowrap;
}

.number-row.compact {
  min-height: 34px;
}

.range {
  --range-progress: 0%;
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 28px;
  margin: 3px 0 0;
  padding: 0;
  background: transparent;
  cursor: ew-resize;
  touch-action: pan-y;
}

.range::-webkit-slider-runnable-track {
  width: 100%;
  height: 5px;
  background: linear-gradient(
    to right,
    var(--primary) 0,
    var(--primary) var(--range-progress),
    #cbd5e1 var(--range-progress),
    #cbd5e1 100%
  );
  border-radius: 3px;
}

.range::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  margin-top: -6.5px;
  background: var(--primary);
  border: 2px solid #ffffff;
  border-radius: 50%;
  box-shadow: 0 0 0 1px var(--primary), 0 2px 5px rgba(15, 23, 42, 0.22);
}

.range::-moz-range-track {
  width: 100%;
  height: 5px;
  background: #cbd5e1;
  border-radius: 3px;
}

.range::-moz-range-progress {
  height: 5px;
  background: var(--primary);
  border-radius: 3px;
}

.range::-moz-range-thumb {
  width: 16px;
  height: 16px;
  background: var(--primary);
  border: 2px solid #ffffff;
  border-radius: 50%;
  box-shadow: 0 0 0 1px var(--primary), 0 2px 5px rgba(15, 23, 42, 0.22);
}

.range:hover::-webkit-slider-thumb,
.range:focus-visible::-webkit-slider-thumb {
  background: var(--primary-dark);
  box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.16);
}

.range:hover::-moz-range-thumb,
.range:focus-visible::-moz-range-thumb {
  background: var(--primary-dark);
  box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.16);
}

.range-scale {
  display: flex;
  justify-content: space-between;
  margin-top: 2px;
  color: #94a3b8;
  font-size: 9px;
}

.two-column-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.two-column-fields .field + .field {
  margin-top: 0;
}

select {
  width: 100%;
  min-height: 36px;
  margin-top: 6px;
  padding: 0 10px;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
  font-weight: 700;
}

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 4px;
  margin-top: 6px;
}

.segment,
.quick-rates button,
.preset-grid button {
  min-height: 31px;
  padding: 0 7px;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 10px;
  font-weight: 800;
}

.segment:hover,
.quick-rates button:hover,
.preset-grid button:hover {
  color: var(--primary);
  border-color: #93c5fd;
}

.segment.is-active,
.quick-rates button.is-active {
  color: #fff;
  background: var(--primary);
  border-color: var(--primary);
}

.quick-rates {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  margin-top: 7px;
}

.inline-action {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-top: 7px;
  padding: 0;
  color: var(--primary);
  background: transparent;
  border: 0;
  font-size: 11px;
  font-weight: 700;
}

.switch {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
}

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

.switch-track {
  position: relative;
  width: 29px;
  height: 17px;
  background: var(--line-strong);
  border-radius: 10px;
  transition: background 0.15s;
}

.switch-track::after {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 13px;
  height: 13px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.2);
  content: "";
  transition: transform 0.15s;
}

.switch input:checked + .switch-track {
  background: var(--primary);
}

.switch input:checked + .switch-track::after {
  transform: translateX(12px);
}

.preset-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 5px;
}

.preset-grid button {
  min-height: 38px;
  padding: 4px;
}

.results {
  min-width: 0;
  padding: 24px clamp(18px, 3vw, 44px) 36px;
}

.result-header {
  max-width: 1180px;
  margin: 0 auto 14px;
}

.saved-scenarios {
  display: flex;
  gap: 5px;
}

.saved-scenarios button {
  min-height: 30px;
  padding: 0 8px;
  color: var(--primary);
  background: var(--primary-soft);
  border: 1px solid #bfdbfe;
  border-radius: 6px;
  font-size: 10px;
  font-weight: 700;
}

.result-band {
  max-width: 1180px;
  margin: 0 auto 12px;
  padding: 20px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.primary-result {
  display: grid;
  grid-template-columns: minmax(250px, 0.8fr) minmax(420px, 1.2fr);
  gap: 28px;
  border-top: 3px solid var(--primary);
}

.dsr-overview > div:first-child {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.result-label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

#dsr-value {
  color: var(--ink);
  font-size: clamp(30px, 4vw, 46px);
  line-height: 1;
}

.result-status {
  padding: 3px 6px;
  border-radius: 5px;
  font-size: 10px;
  font-weight: 800;
}

.result-status.is-safe {
  color: var(--safe);
  background: var(--safe-soft);
}

.result-status.is-warning {
  color: var(--warn);
  background: var(--warn-soft);
}

.result-status.is-danger {
  color: var(--danger);
  background: var(--danger-soft);
}

.dsr-gauge {
  position: relative;
  height: 9px;
  margin-top: 22px;
  overflow: hidden;
  background: #e2e8f0;
  border-radius: 5px;
}

#dsr-gauge-fill {
  width: 0;
  height: 100%;
  background: var(--primary);
  transition: width 0.25s, background 0.25s;
}

#dsr-limit-marker {
  position: absolute;
  top: -3px;
  bottom: -3px;
  width: 2px;
  background: var(--ink);
}

.gauge-scale {
  display: flex;
  justify-content: space-between;
  margin-top: 5px;
  color: #94a3b8;
  font-size: 9px;
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  margin: 0;
  border-top: 1px solid var(--line);
  border-left: 1px solid var(--line);
}

.summary-grid div {
  padding: 14px;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}

.summary-grid dt,
.limit-breakdown dt {
  color: var(--muted);
  font-size: 11px;
}

.summary-grid dd,
.limit-breakdown dd {
  margin: 5px 0 0;
  color: var(--ink);
  font-size: 17px;
  font-weight: 800;
}

.band-heading {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  margin-bottom: 18px;
}

.band-heading h3,
.policy-band h3 {
  color: var(--ink);
  font-size: 16px;
}

.subtle-note {
  color: var(--muted);
  font-size: 10px;
}

.limit-layout {
  display: grid;
  grid-template-columns: minmax(230px, 0.75fr) minmax(390px, 1.25fr);
  gap: 28px;
}

.final-limit {
  padding: 18px;
  background: var(--primary-soft);
  border-left: 4px solid var(--primary);
  border-radius: 6px;
}

.final-limit span {
  color: var(--primary-dark);
  font-size: 11px;
  font-weight: 800;
}

.final-limit strong {
  display: block;
  margin-top: 8px;
  color: var(--ink);
  font-size: clamp(26px, 3.5vw, 38px);
}

.final-limit p {
  margin-top: 8px;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.5;
}

.limit-breakdown {
  display: grid;
  gap: 12px;
  margin: 0;
}

.limit-breakdown div {
  display: grid;
  grid-template-columns: 100px 1fr;
  align-items: center;
  gap: 4px 14px;
}

.limit-breakdown dd {
  margin: 0;
  text-align: right;
}

.limit-breakdown span {
  grid-column: 1 / -1;
  height: 5px;
  overflow: hidden;
  background: #e2e8f0;
  border-radius: 3px;
}

.limit-breakdown i {
  display: block;
  width: 0;
  height: 100%;
  background: var(--primary);
  transition: width 0.25s;
}

#ltv-limit-bar i {
  background: #0891b2;
}

#requested-limit-bar i {
  background: #64748b;
}

.sensitivity-chart {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  min-height: 170px;
}

.sensitivity-item {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  min-width: 0;
}

.sensitivity-value {
  margin-bottom: 6px;
  color: var(--ink);
  font-size: 12px;
  font-weight: 800;
  text-align: center;
}

.sensitivity-bar-track {
  display: flex;
  align-items: flex-end;
  height: 112px;
  overflow: hidden;
  background: var(--canvas);
  border-bottom: 1px solid var(--line-strong);
}

.sensitivity-bar {
  width: 100%;
  min-height: 4px;
  background: var(--primary);
  transition: height 0.25s;
}

.sensitivity-item:nth-child(2) .sensitivity-bar {
  background: #0e7490;
}

.sensitivity-item:nth-child(3) .sensitivity-bar {
  background: #b45309;
}

.sensitivity-item:nth-child(4) .sensitivity-bar {
  background: #dc2626;
}

.sensitivity-label {
  margin-top: 7px;
  color: var(--muted);
  font-size: 10px;
  text-align: center;
}

.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  padding: 11px 12px;
  border-bottom: 1px solid var(--line);
  text-align: right;
  white-space: nowrap;
}

th {
  color: var(--muted);
  background: var(--canvas);
  font-size: 10px;
}

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

td {
  color: var(--text);
  font-size: 12px;
}

tr.is-selected td {
  color: var(--primary-dark);
  background: var(--primary-soft);
  font-weight: 800;
}

.policy-band {
  display: flex;
  gap: 13px;
  background: #f8fafc;
}

.policy-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 32px;
  width: 32px;
  height: 32px;
  color: var(--primary);
  background: var(--primary-soft);
  border-radius: 50%;
}

.policy-icon svg {
  width: 17px;
  height: 17px;
}

.policy-band p {
  max-width: 900px;
  margin-top: 5px;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.65;
}

.policy-links {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 8px;
}

.policy-links a {
  color: var(--primary);
  font-size: 10px;
  font-weight: 700;
}

footer {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  max-width: 1180px;
  margin: 18px auto 0;
  color: #94a3b8;
  font-size: 10px;
  line-height: 1.5;
}

dialog {
  width: min(430px, calc(100vw - 28px));
  padding: 0;
  border: 0;
  border-radius: 8px;
  box-shadow: 0 20px 60px rgba(15, 23, 42, 0.24);
}

dialog::backdrop {
  background: rgba(15, 23, 42, 0.45);
}

.dialog-header,
.dialog-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.dialog-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.dialog-header h2 {
  color: var(--ink);
  font-size: 16px;
}

.dialog-body {
  padding: 18px 16px;
}

.helper-result {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 18px;
  padding: 12px;
  background: var(--primary-soft);
  border-left: 3px solid var(--primary);
}

.helper-result span {
  color: var(--muted);
  font-size: 11px;
}

.helper-result strong {
  color: var(--ink);
}

.dialog-actions {
  justify-content: flex-end;
  gap: 6px;
  padding: 12px 16px;
  background: var(--canvas);
  border-top: 1px solid var(--line);
}

.primary-button,
.secondary-button {
  min-height: 34px;
  padding: 0 12px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 800;
}

.primary-button {
  color: #fff;
  background: var(--primary);
  border: 1px solid var(--primary);
}

.secondary-button {
  color: var(--text);
  background: #fff;
  border: 1px solid var(--line);
}

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 80;
  max-width: min(340px, calc(100vw - 36px));
  padding: 10px 13px;
  color: #fff;
  background: var(--ink);
  border-radius: 6px;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.22);
  font-size: 11px;
  font-weight: 700;
  opacity: 0;
  pointer-events: none;
  transform: translateY(10px);
  transition: opacity 0.18s, transform 0.18s;
}

.toast.is-visible {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 900px) {
  .top-tabs {
    margin-left: 12px;
  }

  .top-tab {
    width: 34px;
    padding: 0;
  }

  .top-tab span {
    display: none;
  }

  .app-shell {
    display: block;
  }

  .sidebar {
    position: static;
    width: 100%;
    height: auto;
    overflow: visible;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .sidebar-heading {
    position: static;
  }

  #calculator-form {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
  }

  .input-section {
    border-right: 1px solid var(--line);
  }

  .preset-section {
    grid-column: 1 / -1;
  }

  .results {
    padding-top: 18px;
  }
}

@media (max-width: 680px) {
  .topbar {
    padding: 0 8px;
  }

  .brand span {
    font-size: 13px;
  }

  .top-tabs {
    margin-left: auto;
  }

  .top-actions {
    margin-left: 4px;
  }

  .text-button span {
    display: none;
  }

  #calculator-form {
    display: flex;
    flex-direction: column;
  }

  .input-section {
    border-right: 0;
  }

  .results {
    padding: 16px 10px 28px;
  }

  .primary-result,
  .limit-layout {
    grid-template-columns: 1fr;
  }

  .primary-result {
    gap: 20px;
  }

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

  .result-band {
    padding: 16px 13px;
  }

  .band-heading {
    align-items: flex-start;
  }

  .sensitivity-chart {
    gap: 5px;
  }

  .sensitivity-value {
    font-size: 10px;
  }

  .limit-breakdown div {
    grid-template-columns: 82px 1fr;
  }

  footer {
    display: block;
  }

  footer span {
    display: block;
    margin-top: 5px;
  }
}

@media (max-width: 390px) {
  .brand span {
    max-width: 104px;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .top-tab {
    width: 31px;
  }

  .icon-button {
    width: 31px;
  }

  .two-column-fields {
    grid-template-columns: 1fr;
  }

  .two-column-fields .field + .field {
    margin-top: 15px;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    scroll-behavior: auto !important;
    transition: none !important;
  }
}
