:root {
  color-scheme: dark;
  --bg: #0f1419;
  --panel: #171d25;
  --panel-border: #2a3441;
  --text: #e7edf4;
  --muted: #8b98a8;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --error: #f87171;
  --radius: 12px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: radial-gradient(circle at top, #172033 0%, var(--bg) 45%);
  color: var(--text);
  min-height: 100vh;
}

.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--panel-border);
  padding: 1rem 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.nav-list {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-bottom: 0.5rem;
}

.nav-link {
  display: block;
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--muted);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 600;
}

.nav-link:hover {
  color: var(--text);
  border-color: var(--accent);
}

.nav-link.active {
  color: #bfdbfe;
  border-color: var(--accent);
  background: #1e3a5f;
}

.layout {
  flex: 1;
  max-width: 920px;
  margin: 0 auto;
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  padding: 1rem;
  gap: 1rem;
  width: 100%;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  padding: 1rem 1.25rem;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
}

.header h1 {
  margin: 0 0 0.35rem;
  font-size: 1.35rem;
}

.subtitle {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.badge {
  white-space: nowrap;
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  background: #243041;
  color: var(--muted);
  font-size: 0.85rem;
}

.badge.ok {
  color: #86efac;
  background: #14532d;
}

.typing {
  color: var(--muted);
  font-style: italic;
}

.trades-shell .layout.trades-layout {
  position: relative;
  max-width: 1200px;
  grid-template-rows: auto auto auto 1fr;
}

.loading-overlay {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(15, 20, 25, 0.78);
  backdrop-filter: blur(2px);
}

.loading-overlay[hidden] {
  display: none !important;
}

.loading-overlay-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.85rem;
  padding: 1.25rem 1.5rem;
  border-radius: 12px;
  border: 1px solid var(--panel-border);
  background: rgba(23, 29, 37, 0.96);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.45);
}

.loading-overlay-text {
  margin: 0;
  color: var(--text);
  font-size: 0.95rem;
  font-weight: 600;
}

.loading-spinner {
  width: 2.25rem;
  height: 2.25rem;
  border: 3px solid rgba(147, 197, 253, 0.25);
  border-top-color: #93c5fd;
  border-radius: 50%;
  animation: loading-spin 0.8s linear infinite;
}

@keyframes loading-spin {
  to {
    transform: rotate(360deg);
  }
}

.analytics-section {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.analytics-heading {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.analytics-note {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.analytics-cards {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.summary-value.pnl-pos {
  color: #86efac;
}

.summary-value.pnl-neg {
  color: #fca5a5;
}

.summary-cards {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.75rem;
}

.summary-card {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 0.9rem 1rem;
}

.summary-label {
  color: var(--muted);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.35rem;
}

.summary-value {
  font-size: 1.35rem;
  font-weight: 650;
}

.trades-panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 0.75rem;
  overflow: hidden;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.trades-filters {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: 0.75rem;
}

.trades-filter {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  color: var(--muted);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.trades-filter select {
  min-width: 12rem;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 0.45rem 0.65rem;
  font: inherit;
  text-transform: none;
  letter-spacing: normal;
  font-size: 0.92rem;
}

.trades-filter select:focus {
  outline: 2px solid color-mix(in srgb, var(--accent, #60a5fa) 55%, transparent);
  outline-offset: 1px;
}

.trades-error {
  color: var(--error);
  background: #3f1d1d;
  border: 1px solid #7f1d1d;
  border-radius: 10px;
  padding: 0.75rem 1rem;
  font-size: 0.9rem;
}

.table-wrap {
  overflow: auto;
  min-height: 0;
}

.trades-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.trades-table th,
.trades-table td {
  padding: 0.65rem 0.75rem;
  border-bottom: 1px solid var(--panel-border);
  text-align: left;
  white-space: nowrap;
}

.trades-table th {
  color: var(--muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 600;
  position: sticky;
  top: 0;
  background: var(--panel);
  z-index: 1;
}

.trades-table tbody tr:hover td {
  background: rgba(59, 130, 246, 0.08);
}

.table-empty {
  color: var(--muted);
  text-align: center !important;
  padding: 2rem 1rem !important;
}

.pnl-pos {
  color: #86efac;
}

.pnl-neg {
  color: #fca5a5;
}

.status-open {
  color: #93c5fd;
}

.status-closed {
  color: var(--muted);
}

.log-actions {
  display: flex;
  gap: 0.35rem;
  flex-wrap: wrap;
}

.log-btn {
  border: 1px solid var(--panel-border);
  background: var(--bg);
  color: var(--text);
  border-radius: 8px;
  padding: 0.35rem 0.55rem;
  font: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
}

.log-btn:hover:not(:disabled) {
  border-color: var(--accent);
  color: #bfdbfe;
}

.log-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.chart-link {
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--panel-border);
  background: rgba(147, 197, 253, 0.08);
  color: #93c5fd;
  border-radius: 8px;
  padding: 0.35rem 0.55rem;
  font-size: 0.78rem;
  font-weight: 600;
  text-decoration: none;
}

.chart-link:hover {
  border-color: var(--accent);
  color: #bfdbfe;
}

.chart-link.is-disabled {
  opacity: 0.35;
  pointer-events: none;
  cursor: not-allowed;
}

.market-chart-link {
  align-self: end;
  margin-bottom: 0.1rem;
}

.log-dialog {
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
  padding: 0;
  max-width: min(720px, 92vw);
  width: 100%;
  box-shadow: 0 24px 48px rgba(0, 0, 0, 0.45);
}

.log-dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
}

.log-dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--panel-border);
}

.log-dialog-header h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.log-dialog-close {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  padding: 0.15rem 0.35rem;
  border-radius: 6px;
}

.log-dialog-close:hover {
  color: var(--text);
  background: var(--bg);
}

.log-dialog-body {
  margin: 0;
  padding: 1rem;
  max-height: min(70vh, 560px);
  overflow: auto;
  font-size: 0.82rem;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
  background: var(--json-bg);
}

.report-filters {
  align-items: flex-start;
}

.report-markets-filter {
  flex-direction: column;
  align-items: flex-start;
  min-width: 12rem;
}

.report-markets-filter select[multiple] {
  width: 100%;
  min-width: 10rem;
  padding: 0.35rem 0.5rem;
  border-radius: 8px;
  border: 1px solid var(--panel-border);
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

.filter-hint {
  color: var(--muted);
  font-size: 0.72rem;
  line-height: 1.3;
}

.report-section {
  margin-top: 1.25rem;
}

.report-section + .report-section {
  margin-top: 1.75rem;
}

.report-heatmap-wrap {
  overflow: auto;
  max-height: min(70vh, 640px);
}

.heatmap-table {
  width: max-content;
  min-width: 100%;
  border-collapse: collapse;
  font-size: 0.78rem;
}

.heatmap-table th,
.heatmap-table td {
  border: 1px solid var(--panel-border);
  padding: 0.35rem 0.45rem;
  text-align: center;
  min-width: 2.75rem;
}

.heatmap-table thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--panel);
  color: var(--muted);
  font-weight: 600;
}

.heatmap-table tbody th {
  position: sticky;
  left: 0;
  z-index: 1;
  background: var(--panel);
  color: var(--muted);
  font-weight: 600;
  text-align: right;
  padding-right: 0.6rem;
}

.heatmap-hour-col-header {
  min-width: 4.5rem;
  line-height: 1.25;
}

.heatmap-hour-col-sub {
  font-size: 0.68rem;
  font-weight: 500;
  color: var(--muted);
}

.heatmap-hour-col {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.2rem;
  min-width: 4.5rem;
}

.heatmap-hour-label {
  font-size: 0.78rem;
  color: var(--muted);
}

.heatmap-hour-cumul {
  display: block;
  padding: 0.15rem 0.35rem;
  border-radius: 6px;
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
  line-height: 1.2;
}

.heatmap-cell {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.heatmap-gain {
  background: rgba(34, 197, 94, 0.35);
  color: #bbf7d0;
}

.heatmap-loss {
  background: rgba(239, 68, 68, 0.35);
  color: #fecaca;
}

.heatmap-flat {
  background: rgba(148, 163, 184, 0.2);
  color: var(--muted);
}

.heatmap-empty {
  background: rgba(15, 20, 25, 0.35);
}

.report-filter-note {
  margin: 0 0 0.75rem;
}

.report-filters input[type="number"] {
  width: 5.5rem;
  padding: 0.35rem 0.5rem;
  border-radius: 8px;
  border: 1px solid var(--panel-border);
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

.report-filters input[type="number"]::placeholder {
  color: var(--muted);
}

.report-go-btn {
  align-self: end;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #eff6ff;
  border-radius: 8px;
  padding: 0.55rem 1.25rem;
  font: inherit;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
}

.report-go-btn:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.session-legend {
  list-style: none;
  margin: 0 0 0.75rem;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem 1.25rem;
  color: var(--muted);
  font-size: 0.78rem;
}

.session-legend li {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

.session-swatch {
  width: 1.25rem;
  height: 0.35rem;
  border-radius: 2px;
  flex-shrink: 0;
}

.session-swatch-au {
  background: #f59e0b;
}

.session-swatch-london {
  background: #818cf8;
}

.session-swatch-ny {
  background: #38bdf8;
}

.session-badges {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 0.2rem;
  margin-left: 0.35rem;
  vertical-align: middle;
}

.heatmap-table tbody th .session-badges {
  display: flex;
  margin-left: 0;
  margin-top: 0.2rem;
}

.session-badge {
  display: inline-block;
  padding: 0.08rem 0.3rem;
  border-radius: 4px;
  font-size: 0.62rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  line-height: 1.2;
}

.session-badge-au {
  background: rgba(245, 158, 11, 0.25);
  color: #fcd34d;
  border: 1px solid rgba(245, 158, 11, 0.55);
}

.session-badge-london {
  background: rgba(129, 140, 248, 0.22);
  color: #c7d2fe;
  border: 1px solid rgba(129, 140, 248, 0.55);
}

.session-badge-ny {
  background: rgba(56, 189, 248, 0.2);
  color: #bae6fd;
  border: 1px solid rgba(56, 189, 248, 0.55);
}

.heatmap-cell.has-session-au {
  box-shadow: inset 0 3px 0 #f59e0b;
}

.heatmap-cell.has-session-london {
  box-shadow: inset 0 3px 0 #818cf8;
}

.heatmap-cell.has-session-ny {
  box-shadow: inset 0 3px 0 #38bdf8;
}

.heatmap-cell.has-session-au.has-session-london {
  box-shadow: inset 0 3px 0 #f59e0b, inset 0 6px 0 #818cf8;
}

.heatmap-cell.has-session-au.has-session-ny {
  box-shadow: inset 0 3px 0 #f59e0b, inset 0 6px 0 #38bdf8;
}

.heatmap-cell.has-session-london.has-session-ny {
  box-shadow: inset 0 3px 0 #818cf8, inset 0 6px 0 #38bdf8;
}

.heatmap-cell.has-session-au.has-session-london.has-session-ny {
  box-shadow: inset 0 3px 0 #f59e0b, inset 0 6px 0 #818cf8, inset 0 9px 0 #38bdf8;
}

.heatmap-table tbody th.has-session-au,
.trades-table td.has-session-au {
  border-left: 3px solid #f59e0b;
}

.heatmap-table tbody th.has-session-london,
.trades-table td.has-session-london {
  border-left: 3px solid #818cf8;
}

.heatmap-table tbody th.has-session-ny,
.trades-table td.has-session-ny {
  border-left: 3px solid #38bdf8;
}

.trades-table tbody tr.row-pnl-pos td:first-child {
  border-left: 3px solid #22c55e;
}

.trades-table tbody tr.row-pnl-neg td:first-child {
  border-left: 3px solid #ef4444;
}

@media (max-width: 900px) {
  .summary-cards,
  .analytics-cards {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 640px) {
  .app-shell {
    flex-direction: column;
  }

  .sidebar {
    width: 100%;
    border-right: none;
    border-bottom: 1px solid var(--panel-border);
    max-height: 220px;
  }

  .header {
    flex-direction: column;
  }

  .summary-cards {
    grid-template-columns: 1fr 1fr;
  }
}
