:root {
  color-scheme: dark;
  --bg: #071116;
  --surface: #0b171d;
  --surface-2: #101e25;
  --surface-3: #13252c;
  --line: #20343d;
  --line-strong: #31505b;
  --text: #e4eef0;
  --muted: #83979f;
  --teal: #20c7b5;
  --teal-soft: #123b3a;
  --cyan: #45bde0;
  --green: #38d39f;
  --red: #f06467;
  --amber: #e4b94f;
  --sidebar: 218px;
  --radius: 6px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  letter-spacing: 0;
}
* { box-sizing: border-box; }
html, body { min-height: 100%; margin: 0; background: var(--bg); color: var(--text); }
body { min-width: 320px; }
button, input, select { font: inherit; letter-spacing: 0; }
button, a { -webkit-tap-highlight-color: transparent; }
button { color: inherit; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--cyan); outline-offset: 2px; }

.app-shell { min-height: 100vh; }
.sidebar { position: fixed; inset: 0 auto 0 0; z-index: 20; width: var(--sidebar); display: flex; flex-direction: column; border-right: 1px solid var(--line); background: #09151a; }
.brand { min-height: 76px; padding: 18px; display: flex; align-items: center; gap: 11px; color: var(--text); text-decoration: none; border-bottom: 1px solid var(--line); }
.brand-mark { width: 34px; height: 34px; flex: 0 0 34px; display: grid; place-items: center; background: var(--teal); color: #03110f; border-radius: 5px; font-weight: 800; }
.brand strong, .brand small { display: block; }
.brand strong { font-size: 15px; }
.brand small { color: var(--muted); margin-top: 2px; font-size: 11px; }
.main-nav { padding: 14px 10px; display: grid; gap: 3px; }
.main-nav a { display: flex; align-items: center; min-height: 40px; gap: 12px; padding: 0 12px; color: #9eb0b6; text-decoration: none; border-radius: 5px; }
.main-nav a span { width: 18px; text-align: center; font-size: 17px; }
.main-nav a:hover { color: var(--text); background: var(--surface-2); }
.main-nav a.active { color: #dffaf5; background: var(--teal-soft); box-shadow: inset 2px 0 var(--teal); }
.sidebar-foot { margin-top: auto; padding: 15px 18px; border-top: 1px solid var(--line); }
.mode-block { display: flex; justify-content: space-between; color: var(--muted); font-size: 12px; }
.mode-block strong { color: var(--green); text-transform: uppercase; }
.text-button { margin-top: 14px; padding: 0; border: 0; background: none; color: var(--muted); cursor: pointer; }
.text-button:hover { color: var(--text); }

.workspace { min-width: 0; min-height: 100vh; margin-left: var(--sidebar); overflow-x: hidden; }
.topbar { height: 76px; padding: 0 26px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--line); background: rgba(7, 17, 22, .96); position: sticky; top: 0; z-index: 15; }
.topbar h1 { margin: 0; font-size: 19px; line-height: 1.3; }
.topbar p { margin: 3px 0 0; color: var(--muted); font-size: 12px; }
.top-status { display: flex; align-items: center; gap: 9px; }
.freshness { color: var(--muted); font-size: 12px; }
.status-chip { display: inline-flex; align-items: center; gap: 7px; min-height: 28px; padding: 0 9px; border: 1px solid var(--line); border-radius: 99px; color: var(--muted); font-size: 11px; white-space: nowrap; }
.status-chip i, .dot { width: 7px; height: 7px; display: inline-block; border-radius: 50%; background: var(--muted); }
.status-chip.good { color: var(--green); border-color: #1d5346; background: #0b2924; }
.status-chip.good i, .dot.good { background: var(--green); }
.status-chip.bad { color: var(--red); border-color: #593033; background: #281719; }
.status-chip.warn { color: var(--amber); border-color: #5a4b26; background: #282312; }
.icon-button { width: 32px; height: 32px; display: inline-grid; place-items: center; padding: 0; border: 1px solid var(--line); border-radius: 5px; background: var(--surface); cursor: pointer; font-size: 18px; }
.icon-button:hover { border-color: var(--line-strong); background: var(--surface-2); }
main { width: 100%; min-width: 0; padding: 24px 26px 42px; max-width: 1600px; margin: 0 auto; }
main:focus { outline: none; }
main section, .view-stack { min-width: 0; max-width: 100%; }

.section-head, .toolbar, .panel-head { display: flex; align-items: center; justify-content: space-between; gap: 14px; }
.section-head { margin-bottom: 16px; }
.section-head h2, .panel-head h2 { margin: 0; font-size: 15px; }
.section-head p, .panel-head p { margin: 3px 0 0; color: var(--muted); font-size: 12px; }
.view-stack { display: grid; gap: 24px; }
.metric-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; background: var(--surface); }
.metric { min-width: 0; padding: 18px; border-right: 1px solid var(--line); }
.metric:last-child { border-right: 0; }
.metric span { color: var(--muted); font-size: 12px; }
.metric strong { display: block; margin-top: 8px; font-family: "SFMono-Regular", Consolas, monospace; font-size: 22px; line-height: 1.2; overflow-wrap: anywhere; }
.metric small { display: block; margin-top: 6px; color: var(--muted); }
.positive { color: var(--green) !important; }
.negative { color: var(--red) !important; }
.warning { color: var(--amber) !important; }
.split-grid { display: grid; grid-template-columns: minmax(0, 1.45fr) minmax(280px, .55fr); gap: 24px; }
.panel { min-width: 0; }
.panel-head { min-height: 38px; margin-bottom: 9px; }
.table-wrap { width: 100%; max-width: 100%; overflow: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.funding-table { max-height: 390px; }
.funding-table thead { position: sticky; top: 0; z-index: 1; }
table { width: 100%; border-collapse: collapse; text-align: left; }
th { height: 38px; padding: 0 12px; background: var(--surface-2); color: var(--muted); font-size: 11px; font-weight: 600; white-space: nowrap; }
td { height: 50px; padding: 8px 12px; border-top: 1px solid var(--line); vertical-align: middle; }
tr.clickable { cursor: pointer; }
tbody tr:hover { background: #0f1d23; }
.mono { font-family: "SFMono-Regular", Consolas, monospace; font-variant-numeric: tabular-nums; }
.subtle { color: var(--muted); font-size: 12px; }
.stack-cell strong, .stack-cell span { display: block; }
.stack-cell span { color: var(--muted); font-size: 11px; margin-top: 3px; }
.badge { display: inline-flex; align-items: center; min-height: 23px; padding: 0 7px; border-radius: 4px; background: var(--surface-3); color: #b3c4c9; font-size: 11px; white-space: nowrap; }
.badge.long { background: #103544; color: var(--cyan); }
.badge.short { background: #3b2024; color: #ff8587; }
.badge.good { background: #10342d; color: var(--green); }
.badge.warn { background: #393017; color: var(--amber); }
.badge.bad { background: #3a2022; color: var(--red); }
.exchange-list { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.exchange-row { min-height: 58px; padding: 10px 13px; display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 10px; border-top: 1px solid var(--line); }
.exchange-row:first-child { border-top: 0; }
.exchange-name { display: flex; align-items: center; gap: 9px; font-weight: 600; text-transform: capitalize; }
.exchange-row small { display: block; margin-top: 4px; color: var(--muted); font-weight: 400; }
.toolbar { margin-bottom: 12px; flex-wrap: wrap; }
.toolbar-group { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
label { color: #b5c4c8; font-size: 12px; }
input, select { min-height: 34px; border: 1px solid var(--line-strong); border-radius: 5px; background: #09151a; color: var(--text); padding: 0 10px; }
input::placeholder { color: #60747b; }
input:disabled { color: var(--muted); opacity: .75; }
label > input, label > select { display: block; width: 100%; margin-top: 6px; }
.segmented { display: inline-flex; padding: 2px; border: 1px solid var(--line); border-radius: 5px; background: var(--surface); }
.segmented button { min-height: 28px; border: 0; border-radius: 3px; padding: 0 11px; background: transparent; color: var(--muted); cursor: pointer; }
.segmented button.active { background: var(--surface-3); color: var(--text); }
button.primary, button.secondary, button.danger { min-height: 34px; padding: 0 13px; border-radius: 5px; cursor: pointer; font-weight: 600; }
button.primary { border: 1px solid var(--teal); background: var(--teal); color: #041412; }
button.primary:hover { background: #31d5c2; }
button.secondary { border: 1px solid var(--line-strong); background: var(--surface-2); color: var(--text); }
button.danger { border: 1px solid #7a3539; background: #3a1d20; color: #ff9b9d; }
button:disabled { cursor: not-allowed; opacity: .5; }
.compact { min-height: 28px !important; padding: 0 9px !important; font-size: 11px; }
.wide { width: 100%; }
.empty-state, .loading-state, .error-state { min-height: 190px; display: grid; place-items: center; align-content: center; gap: 8px; padding: 30px; border: 1px solid var(--line); border-radius: var(--radius); text-align: center; color: var(--muted); }
.empty-state strong, .error-state strong { color: var(--text); }
.error-state { border-color: #543033; }
.spinner { width: 22px; height: 22px; border: 2px solid var(--line); border-top-color: var(--teal); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.callout { padding: 11px 13px; border: 1px solid #594c29; border-radius: 5px; color: #dcc477; background: #231f13; font-size: 12px; line-height: 1.5; }

.settings-grid { display: grid; grid-template-columns: minmax(320px, 1fr) minmax(420px, 1.35fr); gap: 24px; align-items: start; }
.settings-section { border-top: 1px solid var(--line); padding: 18px 0; }
.settings-section:first-child { border-top: 0; padding-top: 0; }
.settings-section h2 { margin: 0 0 4px; font-size: 15px; }
.settings-section > p { margin: 0 0 15px; color: var(--muted); font-size: 12px; }
.mode-controls { display: flex; gap: 8px; }
.switch-row { min-height: 52px; display: flex; justify-content: space-between; align-items: center; gap: 20px; }
.switch-row strong, .switch-row small { display: block; }
.switch-row small { color: var(--muted); margin-top: 3px; }
.switch { position: relative; width: 42px; height: 23px; flex: 0 0 auto; }
.switch input { position: absolute; opacity: 0; }
.switch span { position: absolute; inset: 0; border-radius: 99px; background: #31434a; cursor: pointer; }
.switch span::after { content: ""; position: absolute; width: 17px; height: 17px; top: 3px; left: 3px; border-radius: 50%; background: #cad6d9; transition: transform .18s; }
.switch input:checked + span { background: var(--teal); }
.switch input:checked + span::after { transform: translateX(19px); background: #05201c; }
.credential-grid { display: grid; gap: 12px; }
.credential-block { border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; background: var(--surface); }
.credential-title { display: flex; justify-content: space-between; align-items: center; margin-bottom: 11px; text-transform: capitalize; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.form-grid .full { grid-column: 1 / -1; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 13px; }
.risk-form { border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; background: var(--surface); }
.risk-form .form-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }

.chart-shell { height: 250px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); padding: 12px; }
canvas { width: 100%; height: 100%; display: block; }

dialog { width: min(520px, calc(100vw - 28px)); padding: 0; border: 1px solid var(--line-strong); border-radius: 7px; background: var(--surface); color: var(--text); box-shadow: 0 18px 70px #0009; }
dialog::backdrop { background: #02080bc7; }
.dialog-head { min-height: 64px; padding: 13px 16px; display: flex; align-items: center; justify-content: space-between; gap: 16px; border-bottom: 1px solid var(--line); }
.dialog-head h2 { margin: 0; font-size: 16px; }
.dialog-head p { margin: 3px 0 0; color: var(--muted); font-size: 11px; }
.dialog-body { padding: 16px; display: grid; gap: 13px; }
.dialog-actions { padding: 12px 16px; display: flex; justify-content: flex-end; gap: 8px; border-top: 1px solid var(--line); }
.form-error { margin: 0 16px 12px; padding: 9px; border: 1px solid #68363a; border-radius: 4px; background: #321c1f; color: #ff9b9d; font-size: 12px; }
.drawer { width: min(720px, 100vw); max-width: none; height: 100vh; max-height: none; margin: 0 0 0 auto; border-radius: 0; border-width: 0 0 0 1px; }
.drawer-shell { min-height: 100%; }
.drawer-body { padding: 18px; display: grid; gap: 22px; }
.eyebrow { color: var(--teal) !important; text-transform: uppercase; }
.detail-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); border: 1px solid var(--line); border-radius: var(--radius); }
.detail-grid > div { min-width: 0; padding: 12px; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.detail-grid > div:nth-child(3n) { border-right: 0; }
.detail-grid dt { color: var(--muted); font-size: 11px; }
.detail-grid dd { margin: 6px 0 0; overflow-wrap: anywhere; }
.leg-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.leg { border: 1px solid var(--line); border-radius: var(--radius); padding: 13px; background: #09151a; }
.leg header { display: flex; justify-content: space-between; align-items: center; }
.leg dl { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 15px 0 0; }
.leg dt { color: var(--muted); font-size: 11px; }
.leg dd { margin: 4px 0 0; }
.toast-region { position: fixed; right: 18px; bottom: 18px; z-index: 100; display: grid; gap: 8px; }
.toast { width: min(360px, calc(100vw - 36px)); padding: 11px 13px; border: 1px solid var(--line-strong); border-radius: 5px; background: #122228; box-shadow: 0 10px 35px #0008; }
.toast.error { border-color: #7b373b; }

.login-screen { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: #071116; }
.login-panel { width: min(380px, 100%); padding: 24px; border: 1px solid var(--line-strong); border-radius: 7px; background: var(--surface); box-shadow: 0 20px 70px #0008; }
.login-brand { display: flex; align-items: center; gap: 10px; font-weight: 700; }
.login-panel h1 { margin: 28px 0 6px; font-size: 20px; }
.login-panel > p { color: var(--muted); margin: 0 0 22px; }
.login-panel label { display: block; margin-top: 13px; }
.login-panel .primary { margin-top: 20px; }
.login-panel .form-error { margin: 12px 0 0; }

@media (max-width: 1050px) {
  .metric-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .metric:nth-child(2) { border-right: 0; }
  .metric:nth-child(-n+2) { border-bottom: 1px solid var(--line); }
  .split-grid, .settings-grid { grid-template-columns: 1fr; }
}
@media (max-width: 720px) {
  body { padding-bottom: 62px; }
  .sidebar { inset: auto 0 0; width: 100%; height: 62px; border: 0; border-top: 1px solid var(--line); }
  .brand, .sidebar-foot { display: none; }
  .main-nav { height: 100%; padding: 4px 6px; display: grid; grid-template-columns: repeat(6, 1fr); gap: 1px; }
  .main-nav a { min-width: 0; min-height: 52px; padding: 4px 1px; flex-direction: column; justify-content: center; gap: 2px; border-radius: 4px; font-size: 10px; }
  .main-nav a span { font-size: 16px; }
  .main-nav a.active { box-shadow: inset 0 -2px var(--teal); }
  .workspace { margin-left: 0; }
  .topbar { height: 66px; padding: 0 14px; }
  .topbar h1 { font-size: 17px; }
  .topbar p, .freshness { display: none; }
  main { padding: 16px 12px 28px; }
  .metric { padding: 14px; }
  .metric strong { font-size: 18px; }
  .section-head { align-items: flex-start; }
  .risk-form .form-grid, .form-grid { grid-template-columns: 1fr; }
  .form-grid .full { grid-column: auto; }
  .detail-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .detail-grid > div:nth-child(3n) { border-right: 1px solid var(--line); }
  .detail-grid > div:nth-child(2n) { border-right: 0; }
  .leg-grid { grid-template-columns: 1fr; }
  .drawer { width: 100vw; }
  th, td { padding-left: 10px; padding-right: 10px; }
}
@media (max-width: 420px) {
  .metric-grid { grid-template-columns: 1fr; }
  .metric { border-right: 0; border-bottom: 1px solid var(--line); }
  .metric:last-child { border-bottom: 0; }
  .top-status { gap: 6px; }
  .status-chip { padding: 0 7px; }
}
@media (prefers-reduced-motion: reduce) { * { scroll-behavior: auto !important; animation-duration: .01ms !important; } }
