* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

:root {
  --bg: #0f0f1a;
  --surface: #1a1a2e;
  --surface2: #232340;
  --border: #2d2d50;
  --text: #e0e0e0;
  --text-dim: #888;
  --green: #00e676;
  --red: #ff5252;
  --blue: #448aff;
  --font-mono: "SF Mono", "Fira Code", "Cascadia Code", monospace;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
}

/* Login Screen */
.screen {
  min-height: 100vh;
}

#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 48px;
  text-align: center;
  min-width: 320px;
}

.login-card h1 {
  font-size: 28px;
  margin-bottom: 4px;
}

.subtitle {
  color: var(--text-dim);
  margin-bottom: 32px;
}

.error {
  color: var(--red);
  margin-top: 16px;
  font-size: 14px;
}

/* Passkey / WebAuthn UI */
#passkey-signin-panel,
#passkey-register-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.passkey-hint {
  color: var(--text-dim);
  font-size: 13px;
  margin: 0;
}

.form-input {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 15px;
  padding: 10px 14px;
  box-sizing: border-box;
  outline: none;
  transition: border-color 0.15s;
}

.form-input:focus {
  border-color: var(--blue);
}

.btn-link {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 13px;
  cursor: pointer;
  padding: 4px 0;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.btn-link:hover {
  color: var(--text);
}

/* Buttons */
.btn {
  border: none;
  border-radius: 8px;
  padding: 12px 24px;
  font-size: 16px;
  cursor: pointer;
  transition: opacity 0.15s;
}

.btn:hover {
  opacity: 0.85;
}

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

.btn-primary {
  background: var(--blue);
  color: #fff;
}

.btn-danger {
  background: var(--red);
  color: #fff;
}

.btn-small {
  background: var(--surface2);
  color: var(--text);
  padding: 8px 16px;
  font-size: 13px;
}

/* Header */
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 32px;
}

.header-left h1 {
  font-size: 20px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

/* Tabs */
nav {
  display: flex;
  gap: 4px;
}

.tab {
  background: none;
  border: none;
  color: var(--text-dim);
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
}

.tab.active {
  background: var(--surface2);
  color: var(--text);
}

/* Connection Status */
.status {
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.status::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}

.status.connected::before {
  background: var(--green);
}

.status.disconnected::before {
  background: var(--red);
}

/* Main Content */
.tab-content {
  padding: 24px;
  max-width: 900px;
  margin: 0 auto;
}

/* Wider modifier for data-dense tabs (Analytics, Positions). Caps at 1400px
 * but stays fluid below so it never overflows on tablets. */
.tab-content--wide {
  max-width: min(1400px, 100% - 32px);
}

/* Tracker Controls */
#tracker-controls {
  margin-bottom: 24px;
}

/* P&L Hero */
.pnl-hero {
  text-align: center;
  padding: 40px 0;
}

.pnl-label {
  display: block;
  font-size: 14px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 2px;
  margin-bottom: 8px;
}

.pnl-value {
  font-family: var(--font-mono);
  font-size: 56px;
  font-weight: 700;
  transition: color 0.2s;
}

.pnl-value.profit {
  color: var(--green);
}

.pnl-value.loss {
  color: var(--red);
}

/* Legs */
.legs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-top: 24px;
}

.leg-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px;
}

.leg-card h3 {
  font-size: 14px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 4px;
}

.leg-symbol {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: 16px;
}

.leg-row {
  display: flex;
  justify-content: space-between;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}

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

.leg-row span:first-child {
  color: var(--text-dim);
}

.leg-row span:last-child {
  font-family: var(--font-mono);
}

.leg-pnl.profit {
  color: var(--green);
}

.leg-pnl.loss {
  color: var(--red);
}

/* Signed-value coloring applied beyond P&L hero/leg cards:
 * stat cards (analytics summary, metrics panel), detail tables, greeks rows. */
.stat-value.profit,
.leg-row .profit,
td.profit,
td .profit {
  color: var(--green);
}

.stat-value.loss,
.leg-row .loss,
td.loss,
td .loss {
  color: var(--red);
}

/* Last Update */
.last-update {
  text-align: center;
  margin-top: 24px;
  font-size: 13px;
  color: var(--text-dim);
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.heartbeat-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 4px var(--green);
  transition: transform 120ms ease-out, background-color 200ms ease, box-shadow 200ms ease;
}

.heartbeat-dot.pulse {
  transform: scale(1.6);
}

.heartbeat-dot.flat {
  background: #ffb74d;
  box-shadow: 0 0 4px #ffb74d;
}

.heartbeat-dot.stale {
  background: var(--red);
  box-shadow: 0 0 4px var(--red);
}

#tracking-status-label.flat { color: #ffb74d; }
#tracking-status-label.stale { color: var(--red); }

.lots-summary {
  margin-left: 8px;
  opacity: 0.85;
}

/* Orders */
.orders-controls {
  display: flex;
  gap: 12px;
  margin-bottom: 16px;
}

select {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 14px;
}

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

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

th, td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

th {
  color: var(--text-dim);
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

td {
  font-family: var(--font-mono);
  font-size: 13px;
}

tr:hover td {
  background: var(--surface2);
}

.status-complete {
  color: var(--green);
}

.status-rejected {
  color: var(--red);
}

.side-buy {
  color: var(--green);
}

.side-sell {
  color: var(--red);
}

/* Auto-Close Panel */
.autoclose-panel {
  margin-top: 32px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px;
}

.autoclose-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.autoclose-header h3 {
  font-size: 16px;
}

.autoclose-toggle {
  display: flex;
  align-items: center;
  gap: 12px;
}

.btn-warn {
  background: #ff9800;
  color: #000;
  font-weight: 600;
}

.dry-run-label {
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 4px;
  color: var(--text-dim);
  cursor: pointer;
}

.dry-run-badge {
  font-family: var(--font-mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 1px;
  padding: 3px 8px;
  border-radius: 4px;
  background: rgba(255, 152, 0, 0.25);
  color: #ff9800;
  font-weight: 600;
}

.autoclose-state {
  font-family: var(--font-mono);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 1px;
  padding: 4px 8px;
  border-radius: 4px;
  background: var(--surface2);
}

.autoclose-state.armed {
  background: rgba(255, 152, 0, 0.2);
  color: #ff9800;
}

.autoclose-state.triggered,
.autoclose-state.closing {
  background: rgba(255, 82, 82, 0.2);
  color: var(--red);
  animation: pulse 1s infinite;
}

.autoclose-state.closed {
  background: rgba(0, 230, 118, 0.2);
  color: var(--green);
}

.autoclose-state.error {
  background: rgba(255, 82, 82, 0.2);
  color: var(--red);
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

@keyframes flash-profit {
  0% { box-shadow: 0 0 0 0 rgba(0, 230, 118, 0.6); }
  100% { box-shadow: 0 0 40px 10px rgba(0, 230, 118, 0); }
}

@keyframes flash-loss {
  0% { box-shadow: 0 0 0 0 rgba(255, 82, 82, 0.6); }
  100% { box-shadow: 0 0 40px 10px rgba(255, 82, 82, 0); }
}

.pnl-hero.flash-profit {
  animation: flash-profit 0.6s ease-out;
}

.pnl-hero.flash-loss {
  animation: flash-loss 0.6s ease-out;
}

.pnl-hero.armed {
  border: 1px solid rgba(255, 152, 0, 0.4);
  border-radius: 10px;
  padding: 39px 0;
  box-shadow: 0 0 20px rgba(255, 152, 0, 0.1);
}

.target-group {
  margin-bottom: 16px;
}

.target-group h4 {
  font-size: 12px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 8px;
}

.target-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 0;
}

.target-row label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  min-width: 160px;
}

.target-row input[type="number"] {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 6px 10px;
  font-family: var(--font-mono);
  font-size: 13px;
  width: 120px;
}

.target-row input[type="number"]:focus {
  outline: none;
  border-color: var(--blue);
}

/* Slippage buffer input — visually subordinate to the main target value */
.target-row input.buf-input {
  width: 170px;
  border-style: dashed;
  color: var(--text-muted, #888);
  font-size: 12px;
}

/* Effective-trigger hint shown next to the buffer field */
.effective-trigger {
  font-size: 11px;
  color: var(--yellow, #e5c07b);
  white-space: nowrap;
}

.autoclose-panel.armed .target-row input,
.autoclose-panel.armed .dry-run-label input {
  opacity: 0.5;
  pointer-events: none;
}

.autoclose-log {
  margin-top: 16px;
  border-top: 1px solid var(--border);
  padding-top: 12px;
}

.autoclose-log h4 {
  font-size: 12px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 8px;
}

.autoclose-log ul {
  list-style: none;
  max-height: 200px;
  overflow-y: auto;
}

.autoclose-log li {
  font-size: 12px;
  font-family: var(--font-mono);
  padding: 4px 0;
  color: var(--text-dim);
  border-bottom: 1px solid rgba(45, 45, 80, 0.5);
}

/* History Files */
.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.history-header h2 {
  font-size: 18px;
}

.history-files {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-file-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px 20px;
  transition: border-color 0.15s;
}

.history-file-card:hover {
  border-color: var(--blue);
}

.file-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.file-date {
  font-size: 16px;
  font-weight: 600;
  font-family: var(--font-mono);
}

.file-meta {
  font-size: 12px;
  color: var(--text-dim);
  display: flex;
  gap: 12px;
}

.file-meta span {
  display: flex;
  align-items: center;
  gap: 4px;
}

.btn-download {
  background: var(--blue);
  color: #fff;
  padding: 8px 16px;
  font-size: 13px;
  border-radius: 6px;
  border: none;
  cursor: pointer;
  transition: opacity 0.15s;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.btn-download:hover {
  opacity: 0.85;
}

.empty-state {
  text-align: center;
  color: var(--text-dim);
  padding: 40px 0;
  font-size: 14px;
}

/* Responsive */
@media (max-width: 600px) {
  .legs {
    grid-template-columns: 1fr;
  }

  .pnl-value {
    font-size: 40px;
  }

  header {
    flex-direction: column;
    gap: 12px;
  }

  .header-left {
    flex-direction: column;
    gap: 12px;
  }

  .history-file-card {
    flex-direction: column;
    gap: 12px;
    align-items: flex-start;
  }
}

/* --- Sparkline --- */
.sparkline-wrap {
  margin: 12px 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px;
}
#pnl-sparkline {
  width: 100%;
  height: 60px;
  display: block;
}

/* --- Analytics --- */
.analytics-summary {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 8px;
  margin: 12px 0;
}
.stat-box {
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 8px 12px;
}
.stat-label {
  color: var(--text-dim);
  font-size: 12px;
}
.stat-value {
  font-family: var(--font-mono);
  font-size: 16px;
  color: var(--text);
}
/* Row accent: a left-border tint that signals win/loss without overriding
 * per-cell sign coloring. Applied to the first cell so it reads as a
 * leading indicator. */
#analytics-table .row-win td:first-child {
  box-shadow: inset 3px 0 0 var(--green);
}
#analytics-table .row-loss td:first-child {
  box-shadow: inset 3px 0 0 var(--red);
}

/* --- Greeks --- */
.greeks-controls {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  margin: 12px 0;
}
.greeks-controls label {
  display: flex;
  flex-direction: column;
  font-size: 12px;
  color: var(--text-dim);
}
.greeks-controls input {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 6px 8px;
  border-radius: 4px;
  font-family: var(--font-mono);
}
.greeks-output {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 12px;
}
.greeks-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 12px;
}
.greeks-box.straddle {
  border-color: var(--blue);
}
.greeks-box h3 {
  font-size: 14px;
  margin-bottom: 8px;
  color: var(--text-dim);
}

/* --- Metrics --- */
.metrics-panel {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 8px;
  margin: 12px 0;
}

/* --- Auto-start indicator --- */
.autostart-indicator {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: #1a1f2b;
  border: 1px solid #2c3446;
  border-radius: 6px;
  margin-bottom: 12px;
  font-size: 14px;
}
.autostart-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #6b7280;
}
.autostart-indicator[data-mood="active"] .autostart-dot { background: #10b981; animation: pulse 1.4s ease-in-out infinite; }
.autostart-indicator[data-mood="polling"] .autostart-dot { background: #f59e0b; animation: pulse 1.4s ease-in-out infinite; }
.autostart-indicator[data-mood="warn"] .autostart-dot { background: #f43f5e; }
.autostart-indicator[data-mood="idle"] .autostart-dot { background: #6b7280; }
@keyframes pulse { 0%,100% { opacity: 1 } 50% { opacity: 0.4 } }
.link-btn {
  margin-left: auto;
  background: transparent;
  border: 1px solid #3b4358;
  color: #9ab;
}
.link-btn:hover { background: #2c3446; color: #fff; }

/* --- Analytics drill-down --- */
.disclosure-btn {
  padding: 2px 8px;
  background: transparent;
  border: 1px solid #3b4358;
  color: #9ab;
  cursor: pointer;
  font-size: 12px;
}
.disclosure-btn:hover { background: #2c3446; color: #fff; }
.analytics-detail-row td { background: #12151e; padding: 14px; }
.analytics-detail { display: grid; gap: 14px; grid-template-columns: 1fr; }
.analytics-detail-section h5 { margin: 0 0 6px; font-size: 13px; color: #9ab; text-transform: uppercase; letter-spacing: 0.5px; }
.analytics-detail-section table { width: 100%; border-collapse: collapse; font-size: 12px; }
.analytics-detail-section th,
.analytics-detail-section td { padding: 4px 8px; border-bottom: 1px solid #1f2635; text-align: left; }
.analytics-detail-section th { color: #6b7280; font-weight: 500; }

/* --- Header actions, mode toggle, columns popover --- */
.header-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.mode-toggle {
  display: inline-flex;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 2px;
}
.mode-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  padding: 6px 14px;
  font-size: 13px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.mode-btn.active {
  background: var(--surface2);
  color: var(--text);
}
.mode-btn:hover:not(.active) {
  color: var(--text);
}

.columns-btn {
  white-space: nowrap;
}

.columns-popover {
  position: fixed;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 4px;
  min-width: 180px;
  max-height: 70vh;
  overflow-y: auto;
  z-index: 1000;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}
.columns-popover-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
  padding: 4px 12px 8px;
}
.columns-popover-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
  border-radius: 4px;
}
.columns-popover-row:hover {
  background: var(--surface2);
}
.columns-popover-row input[type="checkbox"] {
  cursor: pointer;
}

/* Hide the disclosure column header (no label). */
th.th-system {
  width: 28px;
  padding-left: 8px;
  padding-right: 4px;
}

/* --- Positions table specific --- */
#positions-table tr.position-flat td {
  opacity: 0.55;
}
.warn-cell {
  color: #ff9800;
}

/* Sticky table headers — keeps column labels visible while scrolling
 * long analytics or positions tables. */
.table-wrap {
  max-height: calc(100vh - 200px);
  overflow: auto;
}
table thead th {
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 1;
}

/* --- Wide-desktop tweaks (1200px+) --- */
@media (min-width: 1200px) {
  .tab-content--wide th,
  .tab-content--wide td {
    padding: 8px 10px;
  }
  .tab-content--wide td {
    font-size: 13px;
  }
}

/* --- Phone breakpoint (≤480px) — collapse tables to card-per-row.
 * Only the columns marked mobileDefault remain visible (column-visibility
 * model handles which columns render at all on mobile). The table itself
 * reflows so each row becomes a small card with key:value pairs. */
@media (max-width: 480px) {
  .table-wrap {
    max-height: none;
    overflow: visible;
  }
  /* Disable sticky headers on tiny screens — they steal vertical space. */
  table thead th {
    position: static;
  }
  /* Compact padding so more data fits per row. */
  th, td {
    padding: 8px 6px;
    font-size: 12px;
  }
  .pnl-value {
    font-size: 32px;
  }
  .leg-card {
    padding: 14px;
  }
  .leg-row {
    padding: 4px 0;
    font-size: 13px;
  }
  /* Sparkline shorter on phones. */
  #pnl-sparkline {
    height: 40px;
  }
}

/* --- Mobile (≤600px): bottom tab bar, larger tap targets, sticky controls --- */
@media (max-width: 600px) {
  /* Push body content up so the fixed bottom nav doesn't overlap. */
  body {
    padding-bottom: 64px;
  }

  /* Header keeps the brand + connection status; nav moves out. */
  header {
    flex-direction: row;
    flex-wrap: wrap;
    padding: 12px 16px;
    gap: 8px;
  }
  .header-left {
    flex-direction: row;
    flex: 1 1 auto;
    gap: 12px;
  }
  .header-left h1 {
    font-size: 18px;
  }

  /* Bottom tab bar: pin nav to the bottom of the viewport. */
  header nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    justify-content: space-around;
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 4px 0;
    margin: 0;
    z-index: 100;
  }
  header nav .tab {
    flex: 1;
    text-align: center;
    padding: 10px 4px;
    border-radius: 0;
    font-size: 12px;
    min-height: 48px;
  }

  /* 44px-minimum tap targets across primary controls (Apple HIG). */
  .btn,
  .btn-primary,
  .btn-danger,
  .btn-small {
    min-height: 44px;
    padding-left: 16px;
    padding-right: 16px;
  }
  .target-row input[type="number"],
  .greeks-controls input,
  select {
    min-height: 44px;
    font-size: 14px;
  }

  /* Tab content padding: tighter side margins so tables get more room. */
  .tab-content {
    padding: 16px 12px;
  }

  /* Header actions wrap so the columns/refresh buttons don't overflow. */
  .history-header,
  .orders-controls {
    flex-wrap: wrap;
    gap: 8px;
  }

  /* Stick the auto-close action bar to the top of its panel so users can
   * Arm/Disarm without scrolling on small screens. */
  .autoclose-header {
    position: sticky;
    top: 0;
    background: var(--surface);
    z-index: 5;
    margin: -20px -20px 16px;
    padding: 14px 20px;
    border-bottom: 1px solid var(--border);
  }

  /* Mode toggle adapts to small width — buttons grow equally. */
  .mode-toggle {
    flex: 1 1 100%;
  }
  .mode-btn {
    flex: 1;
  }
}
