:root {
  color-scheme: dark;
  --bg: #0f1419;
  --panel: #171d25;
  --panel-border: #2a3441;
  --text: #e7edf4;
  --muted: #8b98a8;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --user-bg: #1e3a5f;
  --assistant-bg: #1a222d;
  --error: #f87171;
  --json-bg: #111827;
  --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;
}

.sidebar-header h2 {
  margin: 0 0 0.25rem;
  font-size: 1rem;
  font-weight: 600;
}

.sidebar-subtitle {
  margin: 0;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.35;
}

.ticker-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  overflow-y: auto;
}

.ticker-btn {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.15rem;
  padding: 0.65rem 0.75rem;
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.ticker-btn:hover:not(:disabled) {
  border-color: var(--accent);
}

.ticker-btn.active {
  border-color: var(--accent);
  background: #1e3a5f;
}

.ticker-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.ticker-symbol {
  font-weight: 600;
  font-size: 0.95rem;
}

.ticker-meta {
  font-size: 0.78rem;
  color: var(--muted);
}

.ticker-btn.active .ticker-meta {
  color: #bfdbfe;
}

.ticker-empty {
  padding: 0.75rem;
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.4;
}

.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;
}

.messages {
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 0.25rem;
}

.message {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-width: 100%;
}

.message-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.bubble {
  padding: 0.95rem 1rem;
  border-radius: var(--radius);
  border: 1px solid var(--panel-border);
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}

.message.user .bubble {
  background: var(--user-bg);
}

.message.assistant .bubble {
  background: var(--assistant-bg);
}

.message.error .bubble {
  border-color: #7f1d1d;
  color: var(--error);
}

.json-block {
  margin: 0;
  padding: 0.85rem 1rem;
  background: var(--json-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  overflow-x: auto;
  font-size: 0.85rem;
  line-height: 1.45;
}

.composer {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 1rem;
}

#prompt {
  width: 100%;
  resize: vertical;
  min-height: 84px;
  padding: 0.85rem 1rem;
  border-radius: var(--radius);
  border: 1px solid var(--panel-border);
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

#prompt:focus {
  outline: 2px solid rgba(59, 130, 246, 0.45);
  border-color: var(--accent);
}

.composer-actions {
  margin-top: 0.75rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.hints {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.hint {
  border: 1px solid var(--panel-border);
  background: var(--bg);
  color: var(--muted);
  border-radius: 999px;
  padding: 0.35rem 0.75rem;
  font-size: 0.82rem;
  cursor: pointer;
}

.hint:hover {
  color: var(--text);
  border-color: var(--accent);
}

#send-btn {
  border: none;
  background: var(--accent);
  color: white;
  border-radius: 999px;
  padding: 0.65rem 1.25rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

#send-btn:hover:not(:disabled) {
  background: var(--accent-hover);
}

#send-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.typing {
  color: var(--muted);
  font-style: italic;
}

.trades-shell .layout.trades-layout {
  max-width: 1200px;
  grid-template-rows: auto auto auto 1fr;
}

.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);
}

@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;
  }
}
