:root {
  --bg: #0b0f1a;
  --surface: #111827;
  --surface-2: #1f2937;
  --border: #1f2937;
  --text: #e5e7eb;
  --muted: #94a3b8;
  --accent: #60a5fa;
  --accent-2: #818cf8;
  --good: #34d399;
  --bad: #f87171;
  --warn: #fbbf24;
  --code: #0f172a;
  --radius: 12px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f8fafc;
    --surface: #ffffff;
    --surface-2: #f1f5f9;
    --border: #e2e8f0;
    --text: #0f172a;
    --muted: #475569;
    --accent: #2563eb;
    --accent-2: #4f46e5;
    --good: #16a34a;
    --bad: #dc2626;
    --warn: #d97706;
    --code: #f1f5f9;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
        "Helvetica Neue", Arial, sans-serif;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code, pre {
  font: 13px/1.5 ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  background: var(--code);
  border-radius: 6px;
  padding: 1px 6px;
}

/* ---------------- top bar ---------------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 28px;
  background: linear-gradient(180deg, var(--surface) 0%, var(--bg) 100%);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 5;
  backdrop-filter: blur(6px);
}
.brand {
  display: flex;
  align-items: center;
  gap: 14px;
}
.brand-logo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: white;
  font-weight: 700;
  font-size: 22px;
  box-shadow: 0 6px 20px -4px rgba(96, 165, 250, 0.45);
}
.brand-title { font-weight: 600; letter-spacing: -0.01em; }
.brand-sub { color: var(--muted); font-size: 12.5px; margin-top: 2px; }

.topbar-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.api-pill {
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--muted);
}
.api-pill[data-state="ok"]   { color: var(--good); border-color: rgba(52, 211, 153, 0.4); }
.api-pill[data-state="bad"]  { color: var(--bad);  border-color: rgba(248, 113, 113, 0.4); }
.api-pill[data-state="warn"] { color: var(--warn); border-color: rgba(251, 191, 36, 0.4); }

.key-field {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 4px 4px 12px;
  margin: 0;
  transition: border-color 120ms ease, box-shadow 120ms ease;
}
.key-field:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(96, 165, 250, 0.18);
}
.key-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  white-space: nowrap;
}
#topbar-api-key {
  border: none;
  background: transparent;
  padding: 6px 0;
  min-width: 240px;
  font: 13px ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  color: var(--text);
}
#topbar-api-key:focus { outline: none; box-shadow: none; }

/* ---------------- buttons ---------------- */
button {
  font: inherit;
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 14px;
  background: var(--surface-2);
  color: var(--text);
  transition: transform 80ms ease, background 120ms ease, border-color 120ms ease;
}
button:hover { background: var(--surface); border-color: var(--border); }
button:active { transform: translateY(1px); }
button.primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: white;
  border: none;
  font-weight: 600;
  padding: 10px 18px;
  box-shadow: 0 4px 14px -4px rgba(96, 165, 250, 0.45);
}
button.primary:hover { filter: brightness(1.06); }
button.ghost { background: transparent; border-color: var(--border); color: var(--muted); }
button.ghost:hover { color: var(--text); }
button.small { padding: 4px 10px; font-size: 13px; }

/* ---------------- tabs ---------------- */
.tabs {
  display: flex;
  gap: 6px;
  padding: 14px 28px 0;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 78px;
  background: var(--bg);
  z-index: 4;
}
.tab {
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  padding: 10px 14px;
  border-radius: 0;
  font-weight: 500;
}
.tab:hover { color: var(--text); background: transparent; }
.tab.is-active {
  color: var(--text);
  border-bottom-color: var(--accent);
}

main { padding: 28px; max-width: 1200px; margin: 0 auto; }

/* ---------------- panels & cards ---------------- */
.panel { display: grid; grid-template-columns: 1fr; gap: 24px; }
@media (min-width: 1100px) {
  .panel { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
}
.panel[hidden] { display: none; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px 24px 22px;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.02), 0 12px 32px -16px rgba(0, 0, 0, 0.4);
}
.card h2 { margin: 0 0 4px; font-size: 18px; letter-spacing: -0.01em; }
.muted { color: var(--muted); font-size: 13px; margin: 0 0 16px; }
.muted.small { font-size: 11px; margin: 0; opacity: 0.7; }

/* "Loan amount (derived)" — read-only auto-computed field. */
label.derived input[readonly] {
  background: var(--surface);
  border-style: dashed;
  color: var(--muted);
  cursor: not-allowed;
  font-variant-numeric: tabular-nums;
}

/* "Advanced" disclosure inside forms (e.g. APR override of unit_period/odd_days). */
.advanced {
  margin: -4px 0 14px;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: 8px;
}
.advanced > summary {
  cursor: pointer;
  font-size: 13px;
  color: var(--muted);
  user-select: none;
}
.advanced[open] > summary { color: var(--text); margin-bottom: 8px; }
.advanced .grid { margin-bottom: 0; }

/* "Verify APR for this quote" button row on the loan result. */
.result-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin: 14px 0 6px;
}

/* ---------------- forms ---------------- */
.form .grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px 14px;
  margin-bottom: 18px;
}
label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 12.5px;
  color: var(--muted);
}
input, select {
  font: inherit;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  outline: none;
  transition: border-color 120ms ease, box-shadow 120ms ease;
}
input:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(96, 165, 250, 0.18);
}

fieldset.streams {
  border: 1px dashed var(--border);
  border-radius: 10px;
  padding: 14px;
  margin: 0 0 18px;
}
fieldset.streams legend { padding: 0 6px; color: var(--muted); font-size: 12px; }
.stream-row {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 10px;
  align-items: end;
  margin-bottom: 10px;
}
.stream-row button.remove {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 6px 10px;
  border-radius: 8px;
}
.stream-row button.remove:hover { color: var(--bad); border-color: rgba(248, 113, 113, 0.35); }

/* ---------------- result area ---------------- */
.result-area { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.result-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
}
.result-card.error  { border-color: rgba(248, 113, 113, 0.4); }
.result-card.loading { color: var(--muted); }

.summary {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}
.kpi {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
}
.kpi .label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.kpi .value { margin-top: 4px; font-size: 18px; font-weight: 600; letter-spacing: -0.01em; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: 0.02em;
  background: var(--surface-2);
  color: var(--muted);
  border: 1px solid var(--border);
}
.badge.good { color: var(--good); border-color: rgba(52, 211, 153, 0.4); }
.badge.warn { color: var(--warn); border-color: rgba(251, 191, 36, 0.4); }
.badge.bad  { color: var(--bad);  border-color: rgba(248, 113, 113, 0.4); }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td {
  text-align: right;
  padding: 7px 10px;
  border-bottom: 1px solid var(--border);
}
th:first-child, td:first-child { text-align: left; }
thead th { color: var(--muted); font-weight: 500; font-size: 12px; }
tbody tr:hover { background: var(--surface-2); }

details { margin-top: 14px; }
details summary { cursor: pointer; color: var(--muted); font-size: 13px; }
pre.json {
  background: var(--code);
  padding: 14px;
  border-radius: 10px;
  overflow: auto;
  max-height: 360px;
  font-size: 12.5px;
}
.scroll-table { overflow: auto; max-height: 360px; border-radius: 10px; border: 1px solid var(--border); }

/* ---------------- dialog ---------------- */
dialog {
  border: none;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 0;
  box-shadow: 0 24px 60px -10px rgba(0, 0, 0, 0.5);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.6); backdrop-filter: blur(2px); }
.settings-card { width: min(420px, 90vw); padding: 22px; }
.settings-card label { margin-bottom: 12px; }
.settings-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 10px; }

/* ---------------- footer ---------------- */
.footer {
  display: flex;
  justify-content: space-between;
  padding: 18px 28px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 12.5px;
  margin-top: 32px;
}

/* ---------------- gate (lock overlay) ---------------- */
body.is-locked > header,
body.is-locked > nav,
body.is-locked > main,
body.is-locked > footer { display: none !important; }
body.is-locked { overflow: hidden; }

.gate {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(1100px 600px at 30% 10%, rgba(96, 165, 250, 0.18), transparent 60%),
    radial-gradient(900px 700px at 80% 80%, rgba(129, 140, 248, 0.18), transparent 60%),
    var(--bg);
  z-index: 100;
}
.gate[hidden] { display: none; }

.gate-card {
  width: min(420px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px;
  box-shadow: 0 24px 60px -10px rgba(0, 0, 0, 0.55);
  text-align: center;
}
.gate-logo {
  width: 56px;
  height: 56px;
  font-size: 30px;
  border-radius: 14px;
  margin: 0 auto 14px;
}
.gate-card h1 {
  margin: 0 0 6px;
  font-size: 22px;
  letter-spacing: -0.01em;
}
.gate-card .muted { margin: 0 0 18px; }

.gate-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  text-align: left;
  margin-bottom: 14px;
}
.gate-field span {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.gate-field input {
  font: 14px ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  padding: 11px 12px;
}

.gate-error {
  background: rgba(248, 113, 113, 0.08);
  color: var(--bad);
  border: 1px solid rgba(248, 113, 113, 0.35);
  border-radius: 8px;
  padding: 8px 12px;
  margin-bottom: 12px;
  font-size: 13px;
  text-align: left;
}

.gate-submit { width: 100%; padding: 12px 18px; }
.gate-foot { font-size: 12px; margin: 14px 0 0 !important; }
