:root {
  --bg-canvas: #0b0e13;
  --bg-surface: #121720;
  --bg-surface-elev: #171e2a;
  --bg-row-hover: #1b2331;
  --bg-input: #0e131b;
  --border-hairline: #232c3b;
  --border-strong: #323e52;

  --text-primary: #e6ebf2;
  --text-secondary: #9aa6b8;
  --text-muted: #6c788b;

  --accent: #4c8dff;
  --accent-tint: rgba(76, 141, 255, 0.12);
  --accent-border: rgba(76, 141, 255, 0.35);

  --pos: #34c77b;
  --pos-bg: rgba(52, 199, 123, 0.13);
  --pos-border: rgba(52, 199, 123, 0.34);

  --neg: #ef5b5b;
  --neg-bg: rgba(239, 91, 91, 0.13);
  --neg-border: rgba(239, 91, 91, 0.34);

  --warn: #e5b546;
  --warn-bg: rgba(229, 181, 70, 0.13);
  --warn-border: rgba(229, 181, 70, 0.36);

  --info: #58a6ff;
  --info-bg: rgba(88, 166, 255, 0.13);
  --info-border: rgba(88, 166, 255, 0.32);

  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, "SFMono-Regular", Menlo, Monaco, Consolas, "Liberation Mono", monospace;

  --radius-sm: 6px;
  --radius-md: 8px;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-6: 24px;
}

@supports (color: oklch(60% 0.12 150)) {
  :root {
    --bg-canvas: oklch(15% 0.012 250);
    --bg-surface: oklch(19% 0.014 250);
    --bg-surface-elev: oklch(22% 0.016 250);
    --bg-row-hover: oklch(24% 0.016 250);
    --bg-input: oklch(16% 0.012 250);
    --border-hairline: oklch(28% 0.018 250);
    --border-strong: oklch(35% 0.02 250);

    --text-primary: oklch(93% 0.008 250);
    --text-secondary: oklch(72% 0.015 250);
    --text-muted: oklch(56% 0.015 250);

    --accent: oklch(66% 0.14 255);
    --pos: oklch(70% 0.14 152);
    --neg: oklch(65% 0.16 25);
    --warn: oklch(78% 0.13 82);
    --info: oklch(72% 0.12 242);
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  color-scheme: dark;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  background-color: var(--bg-canvas);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.45;
  overflow-x: clip;
  padding-bottom: env(safe-area-inset-bottom);
}

h1, h2, h3, p, dl, dd, dt, ul {
  margin: 0;
}

button,
input {
  font: inherit;
  color: inherit;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Views & auth screens */
.view-center {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
}

.boot-card,
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-surface);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-md);
  padding: var(--space-6);
}

.boot-card {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
}

.boot-brand,
.login-brand {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
}

.boot-title,
.login-brand h1 {
  font-size: 20px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.boot-status {
  color: var(--text-secondary);
  font-size: 13px;
}

.brand-icon {
  color: var(--accent);
  flex-shrink: 0;
}

.login-header {
  margin-bottom: var(--space-4);
}

.login-subtitle {
  margin-top: var(--space-1);
  color: var(--text-secondary);
  font-size: 13px;
}

.form-stack {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.field {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  min-width: 0;
}

.field label {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
}

.field input {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  background: var(--bg-input);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 16px;
  transition: border-color 80ms ease;
}

.field input:hover {
  border-color: var(--border-strong);
}

.field input:focus {
  border-color: var(--accent);
  outline: none;
}

.notice {
  margin-bottom: var(--space-3);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  border: 1px solid var(--info-border);
  background: var(--info-bg);
  color: var(--text-primary);
  font-size: 13px;
}

.notice.is-pos {
  border-color: var(--pos-border);
  background: var(--pos-bg);
  color: var(--pos);
}

.form-error {
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  border: 1px solid var(--neg-border);
  background: var(--neg-bg);
  color: var(--neg);
  font-size: 13px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 40px;
  padding: 0 var(--space-3);
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 80ms ease, border-color 80ms ease, color 80ms ease;
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-primary {
  min-height: 44px;
  background: var(--accent);
  color: #ffffff;
}

.btn-primary:hover:not(:disabled) {
  filter: brightness(1.08);
}

.btn-block {
  width: 100%;
}

.btn-ghost {
  background: var(--bg-surface-elev);
  border-color: var(--border-hairline);
  color: var(--text-primary);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--bg-row-hover);
  border-color: var(--border-strong);
}

.btn-danger-ghost:hover:not(:disabled) {
  border-color: var(--neg-border);
  color: var(--neg);
}

.btn-link {
  background: none;
  border: none;
  padding: var(--space-2) 0;
  min-height: 40px;
  color: var(--accent);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}

.btn-link:hover {
  text-decoration: underline;
}

.btn-icon {
  flex-shrink: 0;
}

.btn.is-loading .icon-refresh {
  animation: spin 750ms linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* Dashboard Shell */
.dashboard-shell {
  width: 100%;
  max-width: 1160px;
  margin: 0 auto;
  padding: var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

/* Topbar */
.topbar {
  background: var(--bg-surface);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-md);
  padding: var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.topbar-main {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.topbar-identity {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-2);
  min-width: 0;
}

.topbar-identity h1 {
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.01em;
  margin-right: var(--space-1);
}

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--border-hairline);
  background: var(--bg-canvas);
  font-size: 12px;
  font-weight: 500;
}

.status-badge.is-active {
  border-color: var(--pos-border);
  background: var(--pos-bg);
  color: var(--pos);
}

.status-badge.is-stopped {
  border-color: var(--neg-border);
  background: var(--neg-bg);
  color: var(--neg);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  flex-shrink: 0;
}

.status-badge.is-active .status-dot {
  background: var(--pos);
}

.status-badge.is-stopped .status-dot {
  background: var(--neg);
}

.service-name {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-secondary);
}

.topbar-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
}

.topbar-meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-2);
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  color: var(--text-secondary);
}

.meta-sep {
  color: var(--text-muted);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.conn-banner {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  border: 1px solid var(--warn-border);
  background: var(--warn-bg);
  color: var(--warn);
  font-size: 13px;
  font-weight: 500;
}

.conn-icon {
  flex-shrink: 0;
}

/* Main content */
.dashboard-main {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

/* 2. Summary Grid */
.summary-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-2);
}

.kpi-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-md);
  padding: var(--space-3);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: var(--space-2);
  min-width: 0;
}

.kpi-card-hero {
  background: var(--bg-surface-elev);
  border-color: var(--border-strong);
}

.kpi-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-1);
  min-width: 0;
}

.kpi-label {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
}

.kpi-meta {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 11px;
  color: var(--text-muted);
}

.kpi-value {
  font-size: 20px;
  font-weight: 700;
  line-height: 1.15;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}

.kpi-value-hero {
  font-size: 22px;
}

.kpi-bar {
  height: 4px;
  width: 100%;
  border-radius: 999px;
  background: var(--bg-canvas);
  overflow: hidden;
}

.kpi-bar-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 150ms ease;
}

.kpi-sub {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1) var(--space-3);
  font-size: 12px;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}

/* Semantic tags and colors */
.tag {
  display: inline-flex;
  align-items: center;
  padding: 2px 6px;
  border-radius: 4px;
  border: 1px solid var(--border-hairline);
  background: var(--bg-canvas);
  font-size: 11px;
  font-weight: 600;
  line-height: 1.2;
  white-space: nowrap;
}

.tag-pos {
  border-color: var(--pos-border);
  background: var(--pos-bg);
  color: var(--pos);
}

.tag-neg {
  border-color: var(--neg-border);
  background: var(--neg-bg);
  color: var(--neg);
}

.tag-warn {
  border-color: var(--warn-border);
  background: var(--warn-bg);
  color: var(--warn);
}

.tag-info {
  border-color: var(--info-border);
  background: var(--info-bg);
  color: var(--info);
}

.tag-neutral {
  border-color: var(--border-hairline);
  background: var(--bg-surface-elev);
  color: var(--text-secondary);
}

.is-pos {
  color: var(--pos);
}

.is-neg {
  color: var(--neg);
}

.is-warn {
  color: var(--warn);
}

.is-muted {
  color: var(--text-secondary);
}

/* Panels */
.panel {
  background: var(--bg-surface);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-md);
  padding: var(--space-3);
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
}

.panel-head h2 {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.panel-badge {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  color: var(--text-secondary);
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--border-hairline);
  background: var(--bg-canvas);
}

.empty-state {
  color: var(--text-muted);
  font-size: 13px;
  padding: var(--space-2) 0;
}

/* Tables */
.table-wrap {
  width: 100%;
  overflow-x: auto;
  scrollbar-gutter: stable;
}

.scroll-panel {
  max-height: 360px;
  overflow-y: auto;
  scrollbar-gutter: stable;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.data-table th {
  text-align: left;
  padding: 8px 8px;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border-hairline);
  white-space: nowrap;
}

.data-table td {
  padding: 8px 8px;
  border-bottom: 1px solid var(--border-hairline);
  vertical-align: middle;
  white-space: nowrap;
}

.data-table tbody tr:last-child td {
  border-bottom: none;
}

.data-table tbody tr:hover {
  background: var(--bg-row-hover);
}

.data-table .num {
  text-align: right;
  font-family: var(--font-mono);
}

.mono {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

/* Positions (Mobile cards vs Desktop table) */
.positions-cards {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

#positions-table-wrap {
  display: none;
}

.pos-card {
  background: var(--bg-canvas);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-sm);
  padding: var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.pos-card-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-2);
}

.pos-card-title {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.pos-symbol {
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: 14px;
}

.pos-upnl {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 14px;
  white-space: nowrap;
}

.pos-metrics {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-2);
}

.pos-metric {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.pos-metric-label {
  font-size: 11px;
  color: var(--text-muted);
}

.pos-metric-val {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  color: var(--text-primary);
}

/* Equity curve */
.curve-readout {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  color: var(--text-secondary);
}

.curve-wrap {
  position: relative;
  width: 100%;
  min-height: 196px;
}

#equity-canvas {
  display: block;
  width: 100%;
  height: 200px;
  touch-action: pan-y;
}

/* Split grid for Trades + Events */
.split-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--space-3);
}

/* Events feed */
.events-feed {
  list-style: none;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.event-item {
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-hairline);
  background: var(--bg-canvas);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.event-meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}

.event-time {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 11px;
  color: var(--text-muted);
}

.event-symbol {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
  color: var(--text-primary);
}

.event-text {
  font-size: 12px;
  color: var(--text-secondary);
  overflow-wrap: anywhere;
  line-height: 1.4;
}

/* Config section */
.config-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--space-4);
}

.config-block {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.config-subtitle {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
}

.limits-list {
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-sm);
  background: var(--bg-canvas);
}

.limit-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--border-hairline);
}

.limit-row:last-child {
  border-bottom: none;
}

.limit-row dt {
  font-size: 13px;
  color: var(--text-secondary);
}

.limit-row dd {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 13px;
  font-weight: 600;
}

/* Footer & Change Password */
.footer {
  background: var(--bg-surface);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-md);
  padding: var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.footer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--space-2);
}

.footer-user {
  font-size: 12px;
  color: var(--text-secondary);
}

.password-panel {
  border-top: 1px solid var(--border-hairline);
  padding-top: var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.password-form {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.password-fields {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--space-3);
}

.password-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
}

.pw-feedback {
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  font-size: 13px;
  border: 1px solid var(--border-hairline);
}

.pw-feedback.is-pos {
  border-color: var(--pos-border);
  background: var(--pos-bg);
  color: var(--pos);
}

.pw-feedback.is-neg {
  border-color: var(--neg-border);
  background: var(--neg-bg);
  color: var(--neg);
}

/* Responsive Breakpoints */
@media (min-width: 768px) {
  .dashboard-shell {
    padding: var(--space-4);
    gap: var(--space-4);
  }

  .topbar {
    padding: var(--space-3) var(--space-4);
  }

  .topbar-main {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }

  .panel,
  .footer {
    padding: var(--space-4);
  }

  .kpi-card {
    padding: var(--space-4);
  }

  .kpi-value {
    font-size: 24px;
  }

  .kpi-value-hero {
    font-size: 28px;
  }

  #positions-cards {
    display: none;
  }

  #positions-table-wrap {
    display: block;
  }

  #equity-canvas {
    height: 230px;
  }

  .config-grid {
    grid-template-columns: 280px minmax(0, 1fr);
  }

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

@media (min-width: 960px) {
  .summary-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: var(--space-3);
  }

  .split-grid {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1.2fr);
    gap: var(--space-4);
  }
}
