/* Copytrade operator panel — design system.
   Self-hosted (no CDN) to satisfy the app CSP: style-src 'self'.
   Light/dark theming via CSS custom properties; `.dark`/`.light` set on <html> by app.js. */

:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --text: #0f172a;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;

  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-fg: #ffffff;
  --primary-soft-bg: #eef2ff;
  --primary-soft-fg: #4338ca;

  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --danger-soft-bg: #fef2f2;
  --danger-soft-fg: #b91c1c;

  --ok: #16a34a;
  --ok-soft-bg: #f0fdf4;
  --ok-soft-fg: #15803d;

  --warn: #d97706;
  --warn-soft-bg: #fffbeb;
  --warn-soft-fg: #b45309;

  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08), 0 1px 2px rgba(15, 23, 42, 0.06);
  --radius: 12px;
  --radius-sm: 8px;
  --nav-h: 56px;
  --maxw: 1180px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial,
    "Apple Color Emoji", "Segoe UI Emoji", sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

/* Dark theme tokens — applied when <html> has `.dark`, or by system preference
   when no explicit choice (`:root:not(.light)`) has been made. */
.dark {
  --bg: #0b1220;
  --surface: #111827;
  --surface-2: #1f2937;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --border: #1f2937;
  --border-strong: #334155;

  --primary: #6366f1;
  --primary-hover: #818cf8;
  --primary-fg: #ffffff;
  --primary-soft-bg: rgba(99, 102, 241, 0.16);
  --primary-soft-fg: #a5b4fc;

  --danger: #ef4444;
  --danger-hover: #f87171;
  --danger-soft-bg: rgba(239, 68, 68, 0.14);
  --danger-soft-fg: #fca5a5;

  --ok: #22c55e;
  --ok-soft-bg: rgba(34, 197, 94, 0.14);
  --ok-soft-fg: #86efac;

  --warn: #f59e0b;
  --warn-soft-bg: rgba(245, 158, 11, 0.14);
  --warn-soft-fg: #fcd34d;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.5), 0 1px 2px rgba(0, 0, 0, 0.4);
}

@media (prefers-color-scheme: dark) {
  :root:not(.light):not(.dark) {
    --bg: #0b1220;
    --surface: #111827;
    --surface-2: #1f2937;
    --text: #f1f5f9;
    --text-muted: #94a3b8;
    --border: #1f2937;
    --border-strong: #334155;
    --primary: #6366f1;
    --primary-hover: #818cf8;
    --primary-soft-bg: rgba(99, 102, 241, 0.16);
    --primary-soft-fg: #a5b4fc;
    --danger: #ef4444;
    --danger-hover: #f87171;
    --danger-soft-bg: rgba(239, 68, 68, 0.14);
    --danger-soft-fg: #fca5a5;
    --ok: #22c55e;
    --ok-soft-bg: rgba(34, 197, 94, 0.14);
    --ok-soft-fg: #86efac;
    --warn: #f59e0b;
    --warn-soft-bg: rgba(245, 158, 11, 0.14);
    --warn-soft-fg: #fcd34d;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.5), 0 1px 2px rgba(0, 0, 0, 0.4);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

a {
  color: var(--primary);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}

/* ---------- Navbar ---------- */
.navbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

.nav-inner {
  max-width: var(--maxw);
  margin: 0 auto;
  height: var(--nav-h);
  padding: 0 20px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  font-weight: 700;
  font-size: 16px;
  color: var(--text);
  margin-right: 18px;
  white-space: nowrap;
}
.brand:hover {
  text-decoration: none;
}
.brand-dot {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), #8b5cf6);
  box-shadow: 0 0 0 4px var(--primary-soft-bg);
}

.nav-links {
  display: flex;
  align-items: center;
  gap: 2px;
}

.nav-link {
  padding: 7px 12px;
  border-radius: var(--radius-sm);
  font-weight: 500;
  font-size: 13.5px;
  color: var(--text-muted);
  transition: background 0.12s ease, color 0.12s ease;
  white-space: nowrap;
}
.nav-link:hover {
  background: var(--surface-2);
  color: var(--text);
  text-decoration: none;
}
.nav-link.active {
  background: var(--primary-soft-bg);
  color: var(--primary-soft-fg);
}

.nav-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
}
.logout-form {
  margin: 0;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
}
.icon-btn:hover {
  background: var(--surface-2);
  color: var(--text);
}
.icon {
  width: 19px;
  height: 19px;
}

/* theme icon visibility */
.icon-sun {
  display: none;
}
.icon-moon {
  display: block;
}
.dark .icon-sun {
  display: block;
}
.dark .icon-moon {
  display: none;
}

.nav-burger {
  display: none;
}

.mobile-menu {
  display: none;
  flex-direction: column;
  gap: 2px;
  padding: 8px 14px 14px;
  border-top: 1px solid var(--border);
}
.mobile-menu.open {
  display: flex;
}
.mobile-menu .nav-link {
  padding: 9px 12px;
}

@media (max-width: 760px) {
  .nav-links {
    display: none;
  }
  .nav-burger {
    display: inline-flex;
  }
}

/* ---------- Layout ---------- */
.container {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 26px 20px 64px;
}

.page-header {
  margin-bottom: 22px;
}
.page-title {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.page-subtitle {
  margin: 4px 0 0;
  color: var(--text-muted);
  font-size: 14px;
}

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  margin-bottom: 20px;
  overflow: hidden;
}
.card-header {
  padding: 15px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.trades-side-filter {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  flex-shrink: 0;
}
.trades-side-filter .field-label {
  margin: 0;
  white-space: nowrap;
}
.trades-side-select {
  width: auto;
  min-width: 7rem;
}
.card-title {
  margin: 0;
  font-size: 15px;
  font-weight: 650;
}
.card-subtitle {
  margin: 3px 0 0;
  font-size: 13px;
  color: var(--text-muted);
  font-weight: 400;
}
.card-body {
  padding: 18px;
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 16px;
  margin-bottom: 20px;
}
.card-grid .card {
  margin-bottom: 0;
}

/* status tiles */
.status-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}
.status-label {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  font-weight: 600;
  margin-bottom: 8px;
}

/* stat cards (paper trading metrics) */
.stat-value {
  font-size: 24px;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}
.stat-sub {
  margin-top: 5px;
  font-size: 12.5px;
  font-weight: 500;
}
.pos {
  color: var(--ok);
}
.neg {
  color: var(--danger);
}
.divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 18px 0;
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 15px;
  font-size: 13.5px;
  font-weight: 600;
  font-family: inherit;
  line-height: 1;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease, color 0.12s ease,
    box-shadow 0.12s ease, transform 0.04s ease;
}
.btn:hover {
  background: var(--surface-2);
  text-decoration: none;
}
.btn:active {
  transform: translateY(1px);
}
.btn:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}
.text-center,
.center {
  text-align: center;
}

.status-pill {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  background: var(--muted-bg, #e5e7eb);
  color: var(--muted, #6b7280);
}

.status-pill.pos {
  background: rgba(34, 197, 94, 0.15);
  color: #16a34a;
}

.status-pill.neg {
  background: rgba(239, 68, 68, 0.15);
  color: #dc2626;
}

.btn-sm {
  padding: 7px 12px;
  font-size: 13px;
}

.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--primary-fg);
}
.btn-primary:hover {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}

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

.btn-ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text-muted);
}
.btn-ghost:hover {
  background: var(--surface-2);
  color: var(--text);
}

/* ---------- Badges ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.02em;
  border: 1px solid transparent;
}
.badge::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}
.badge-ok {
  background: var(--ok-soft-bg);
  color: var(--ok-soft-fg);
}
.badge-danger {
  background: var(--danger-soft-bg);
  color: var(--danger-soft-fg);
}
.badge-warn {
  background: var(--warn-soft-bg);
  color: var(--warn-soft-fg);
}
.badge-muted {
  background: var(--surface-2);
  color: var(--text-muted);
}

/* ---------- Tables ---------- */
.table-wrap {
  overflow-x: auto;
}
.fills-scroll {
  --fills-visible-rows: 20;
  max-height: calc(var(--fills-visible-rows) * 2.45rem + 2.75rem);
  overflow-y: auto;
}
.fills-scroll .table thead th {
  position: sticky;
  top: 0;
  z-index: 1;
}
.load-sentinel-cell {
  text-align: center;
  font-size: 12px;
  padding: 8px 16px;
}
.load-sentinel.loading .load-sentinel-cell::after {
  content: " …";
}
.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13.5px;
}
.table thead th {
  text-align: left;
  padding: 11px 16px;
  font-size: 11.5px;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.table tbody td {
  padding: 11px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.table tbody tr:last-child td {
  border-bottom: none;
}
.table tbody tr:hover {
  background: var(--surface-2);
}
.table .num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.mono {
  font-family: var(--mono);
  font-size: 12.5px;
}
.muted {
  color: var(--text-muted);
}
.empty {
  text-align: center;
  color: var(--text-muted);
  padding: 26px 16px;
  font-style: italic;
}

/* side pills inside tables */
.side {
  font-weight: 700;
  font-size: 12px;
  letter-spacing: 0.03em;
}
.side-buy {
  color: var(--ok);
}
.side-sell {
  color: var(--danger);
}
.side-settlement {
  color: var(--text-muted);
}

/* ---------- Forms ---------- */
.form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px;
}
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.field > label,
.field-label {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
}
.field-help {
  margin: 2px 0 0;
  font-size: 12px;
  line-height: 1.45;
  color: var(--text-muted);
}
.input,
.select {
  width: 100%;
  padding: 9px 11px;
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}
.input:focus,
.select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft-bg);
}
.input::placeholder {
  color: var(--text-muted);
  opacity: 0.7;
}

.check {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
}
.check input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--primary);
  cursor: pointer;
}

.form-actions {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.hint {
  font-size: 12.5px;
  color: var(--text-muted);
}

/* ---------- Alerts ---------- */
.alert {
  padding: 11px 14px;
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  font-weight: 500;
  margin-bottom: 16px;
  border: 1px solid transparent;
}
.alert-success {
  background: var(--ok-soft-bg);
  color: var(--ok-soft-fg);
  border-color: var(--ok-soft-fg);
}
.alert-error {
  background: var(--danger-soft-bg);
  color: var(--danger-soft-fg);
  border-color: var(--danger-soft-fg);
}
.alert-info {
  background: var(--primary-soft-bg);
  color: var(--primary-soft-fg);
}

/* ---------- Login ---------- */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 30px 28px;
}
.login-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 19px;
  margin-bottom: 4px;
}
.login-sub {
  color: var(--text-muted);
  margin: 0 0 22px;
  font-size: 13.5px;
}
.login-card .btn-primary {
  width: 100%;
  margin-top: 4px;
  padding: 11px 15px;
}

.hidden {
  display: none !important;
}

.paper-open-value-chart {
  margin-top: 20px;
  min-height: 280px;
}

.chart-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.chart-title {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
}

.chart-subtitle {
  margin: 4px 0 0;
  font-size: 12px;
}

.chart-toolbar-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.chart-range-group {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.chart-range-btn {
  border: none;
  background: var(--surface);
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  padding: 6px 10px;
  cursor: pointer;
}

.chart-range-btn:hover {
  background: var(--surface-2);
  color: var(--text);
}

.chart-range-btn.active {
  background: var(--primary-soft-bg);
  color: var(--primary-soft-fg);
}

.paper-open-value-chart-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 280px;
}

.paper-open-value-chart-canvas-wrap {
  position: relative;
  height: 280px;
}

.paper-open-value-chart-canvas-wrap canvas {
  width: 100% !important;
  height: 100% !important;
}

.chart-spinner {
  width: 20px;
  height: 20px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: chart-spin 0.8s linear infinite;
}

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

.chart-empty {
  text-align: center;
  padding: 24px 0;
  font-size: 13px;
}

.pnl-period-group {
  margin-left: auto;
}

.pnl-stat-strip {
  display: flex;
  align-items: baseline;
  gap: 12px;
  margin-bottom: 16px;
}

.pnl-summary-row {
  cursor: pointer;
}

.pnl-summary-row:hover td {
  background: var(--surface-2);
}

.pnl-expand-cell {
  width: 2rem;
  text-align: center;
  color: var(--text-muted);
}

.pnl-chevron {
  display: inline-block;
  transition: transform 0.15s ease;
}

.pnl-chevron.expanded {
  transform: rotate(90deg);
}

.pnl-fill-detail td {
  background: var(--bg-subtle);
  padding: 0.5rem 0.75rem;
}

.pnl-fill-detail-table {
  margin: 0;
  font-size: 0.92em;
}

.pnl-chart-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 280px;
}

.pnl-chart-canvas-wrap {
  position: relative;
  height: 280px;
}

.pnl-chart-canvas-wrap canvas {
  width: 100% !important;
  height: 100% !important;
}

/* Progress meter (simulation and other long-running tasks) */
.progress-bar {
  height: 8px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  border: 1px solid var(--border);
  overflow: hidden;
  margin-bottom: 10px;
}

.progress-fill {
  height: 100%;
  background: var(--primary);
  transition: width 0.25s ease;
}

.progress-meta {
  margin: 0;
}

/* Period preset radios — chart-range look, form-compatible */
.field > .form-grid {
  margin-top: 14px;
}

.period-preset-group {
  flex-wrap: wrap;
  margin-top: 6px;
}

.period-preset-option {
  display: flex;
  align-items: stretch;
  margin: 0;
  cursor: pointer;
}

.period-preset-option input {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.period-preset-option span {
  display: block;
  border: none;
  background: var(--surface);
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  padding: 6px 10px;
  white-space: nowrap;
  transition: background 0.12s ease, color 0.12s ease;
}

.period-preset-option:hover span {
  background: var(--surface-2);
  color: var(--text);
}

.period-preset-option input:checked + span {
  background: var(--primary-soft-bg);
  color: var(--primary-soft-fg);
}

.period-preset-option input:focus-visible + span {
  outline: none;
  box-shadow: inset 0 0 0 2px var(--primary);
}

