:root {
  color-scheme: dark;
  --bg: #07111f;
  --panel: rgba(15, 23, 42, .88);
  --line: rgba(148, 163, 184, .22);
  --text: #f8fafc;
  --muted: #94a3b8;
  --blue: #38bdf8;
  --green: #22c55e;
  --red: #ef4444;
  --amber: #f59e0b;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at 10% -10%, rgba(56, 189, 248, .22), transparent 34%),
    radial-gradient(circle at 90% 0%, rgba(34, 197, 94, .14), transparent 30%),
    linear-gradient(180deg, #0f172a 0%, var(--bg) 100%);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, input, select {
  font: inherit;
}

button {
  min-height: 46px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255, 255, 255, .08);
  color: var(--text);
  font-weight: 800;
  cursor: pointer;
}

button:active {
  transform: translateY(1px);
}

.app {
  width: min(1280px, 100%);
  margin: 0 auto;
  padding: 18px;
}

.hidden {
  display: none !important;
}

.login-card, .panel, .metric {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 18px;
  box-shadow: 0 18px 44px rgba(0, 0, 0, .24);
}

.login-card {
  max-width: 420px;
  margin: 12vh auto 0;
  padding: 22px;
}

.login-card h1, .topbar h1, .panel h2 {
  margin: 0;
}

.login-card p {
  color: var(--muted);
  line-height: 1.45;
}

.login-card input {
  width: 100%;
  min-height: 46px;
  margin: 10px 0;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(2, 8, 23, .66);
  color: var(--text);
  padding: 0 12px;
}

.login-card button, .primary {
  background: linear-gradient(180deg, #38bdf8, #0284c7);
  color: #03111f;
  border: 0;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  margin-bottom: 14px;
}

.topbar-badges {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--blue);
  font-size: .82rem;
  text-transform: uppercase;
  letter-spacing: .12em;
  font-weight: 900;
}

.topbar h1 {
  font-size: clamp(2rem, 5vw, 3.4rem);
  letter-spacing: -.06em;
  line-height: .92;
}

.badge, .soft-label {
  display: inline-flex;
  align-items: center;
  min-height: 34px;
  border-radius: 999px;
  padding: 0 12px;
  border: 1px solid var(--line);
  color: var(--muted);
  background: rgba(255, 255, 255, .06);
  font-weight: 900;
  white-space: nowrap;
}

.badge.running {
  color: #052e16;
  background: var(--green);
  border-color: transparent;
}

.badge.stopped {
  color: var(--muted);
  background: rgba(255, 255, 255, .06);
  border-color: var(--line);
}

.badge.guardian-on {
  color: #082f49;
  background: var(--blue);
  border-color: transparent;
}

.badge.guardian-off {
  color: #fbbf24;
  background: rgba(245, 158, 11, .12);
  border-color: rgba(245, 158, 11, .28);
}

.status-grid {
  display: grid;
  grid-template-columns: minmax(190px, 1.2fr) minmax(430px, 2.55fr) minmax(150px, .9fr) minmax(180px, 1.05fr);
  gap: 12px;
}

.metric {
  padding: 16px;
  overflow: hidden;
}

.metric span, .ema-line span {
  display: block;
  color: var(--muted);
  font-size: .88rem;
  margin-bottom: 6px;
}

.metric strong {
  display: block;
  font-size: clamp(1.25rem, 3vw, 2rem);
  letter-spacing: -.04em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.metric small {
  display: block;
  margin-top: 6px;
  color: var(--muted);
  font-size: .74rem;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.feed-card {
  border-color: rgba(148, 163, 184, .28);
}

.feed-card strong {
  font-size: clamp(1rem, 2.2vw, 1.45rem);
}

.price-card strong {
  font-size: clamp(1.9rem, 4vw, 2.75rem);
  letter-spacing: 0;
}

.bot-state-card {
  display: grid;
  gap: 10px;
  align-content: start;
  padding: 14px;
}

.bot-state-item {
  min-width: 0;
}

.bot-state-item span {
  display: block;
  color: var(--muted);
  font-size: .78rem;
  margin-bottom: 3px;
}

.bot-state-item strong {
  display: block;
  font-size: clamp(.95rem, 1.5vw, 1.15rem);
  letter-spacing: -.03em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.mobile-pnl-card {
  display: none;
}

.mobile-pnl-item {
  min-width: 0;
}

.mobile-pnl-item strong {
  font-size: clamp(1rem, 1.6vw, 1.3rem);
}

.mobile-pnl-item .compact-currency {
  font-size: .68em;
  font-weight: 700;
}

.mobile-pnl-item strong.positive {
  color: var(--green);
}

.mobile-pnl-item strong.negative {
  color: #fca5a5;
}

.market-chart-card {
  min-height: 150px;
  padding: 12px;
}

.chart-card-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: center;
  margin-bottom: 6px;
}

.chart-card-head span {
  margin: 0;
}

.chart-card-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.chart-zoom {
  display: inline-flex;
  gap: 4px;
}

.chart-zoom button {
  width: 28px;
  min-height: 28px;
  padding: 0;
  border-radius: 8px;
  font-size: .78rem;
}

.chart-card-actions select {
  width: auto;
  min-width: 112px;
  min-height: 28px;
  margin-top: 0;
  border-radius: 8px;
  padding: 0 8px;
  font-size: .72rem;
  font-weight: 800;
}

.chart-card-head strong {
  color: var(--blue);
  font-size: .88rem;
  letter-spacing: 0;
  text-align: right;
}

.market-chart-card canvas {
  display: block;
  width: 100%;
  height: 106px;
  border-radius: 10px;
  background: rgba(2, 8, 23, .48);
}

.market-chart-card small {
  margin-top: 5px;
  font-size: .72rem;
}

.feed-dydx {
  border-color: rgba(34, 197, 94, .58);
  box-shadow: 0 0 0 1px rgba(34, 197, 94, .14), 0 18px 44px rgba(0, 0, 0, .24);
}

.feed-dydx strong {
  color: var(--green);
}

.feed-fallback {
  border-color: rgba(245, 158, 11, .62);
  box-shadow: 0 0 0 1px rgba(245, 158, 11, .16), 0 18px 44px rgba(0, 0, 0, .24);
}

.feed-fallback strong {
  color: #fbbf24;
}

.feed-binance strong, .feed-active strong {
  color: var(--blue);
}

.panel {
  margin-top: 12px;
  padding: 16px;
}

.panel-head {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
  margin-bottom: 12px;
}

.ema-line {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.ema-line div {
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(255, 255, 255, .05);
}

.ema-line strong {
  font-size: 1.3rem;
}

.chart {
  display: grid;
  gap: 8px;
  margin-top: 14px;
}

.bar {
  height: 12px;
  width: 8%;
  border-radius: 999px;
  transition: width .25s ease;
}

.bar.fast {
  background: var(--blue);
}

.bar.slow {
  background: var(--green);
}

.muted {
  color: var(--muted);
}

.controls {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  padding: 10px;
}

.controls button {
  min-width: 0;
  padding: 0 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.danger {
  background: rgba(239, 68, 68, .18);
  color: #fecaca;
  border-color: rgba(239, 68, 68, .34);
}

.danger-soft {
  color: #fecaca;
  background: rgba(239, 68, 68, .12);
  border-color: rgba(239, 68, 68, .28);
}

.small {
  min-height: 36px;
  padding: 0 12px;
}

.form-grid, .account-grid, .sim-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.sim-grid {
  grid-template-columns: repeat(7, minmax(0, 1fr));
}

.sim-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 12px;
}

.sim-save-btn {
  grid-column: 1 / -1;
  justify-self: start;
}

.sim-trades {
  display: grid;
  gap: 7px;
  margin-top: 12px;
}

.trade-row {
  display: grid;
  grid-template-columns: 76px 72px 72px minmax(0, 1fr);
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid rgba(148, 163, 184, .14);
  border-radius: 10px;
  background: rgba(2, 8, 23, .38);
  color: #cbd5e1;
  font-size: .88rem;
}

.trade-row strong {
  color: var(--blue);
}

.daily-history {
  display: grid;
  gap: 7px;
}

.pnl-summary {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.pnl-card {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  background: rgba(255, 255, 255, .05);
}

.pnl-card span, .pnl-card small {
  display: block;
  color: var(--muted);
}

.pnl-card strong {
  display: block;
  margin: 5px 0;
  font-size: 1.35rem;
}

.pnl-card .positive {
  color: var(--green);
}

.pnl-card .negative {
  color: #fca5a5;
}

.optimizer-summary {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.optimizer-card, .recommendation {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  background: rgba(255, 255, 255, .05);
}

.optimizer-card span, .recommendation span, .recommendation em {
  display: block;
  color: var(--muted);
}

.optimizer-card strong {
  display: block;
  margin-top: 5px;
  font-size: 1.15rem;
}

.optimizer-recommendations {
  display: grid;
  gap: 8px;
  margin-top: 10px;
}

.recommendation strong {
  display: block;
  margin-bottom: 4px;
}

.recommendation em {
  margin-top: 6px;
  color: #fde68a;
  font-style: normal;
  font-weight: 800;
}

.trade-history-head {
  align-items: flex-start;
}

.trade-history-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.trade-history-actions select {
  width: 170px;
  margin-top: 0;
  min-height: 36px;
}

.trade-history {
  display: grid;
  gap: 7px;
  max-height: 460px;
  overflow: auto;
}

.history-row {
  display: grid;
  grid-template-columns: 80px 70px 80px minmax(130px, 1fr) 80px 80px;
  gap: 8px;
  align-items: center;
  padding: 9px 10px;
  border: 1px solid rgba(148, 163, 184, .14);
  border-radius: 10px;
  background: rgba(2, 8, 23, .42);
  color: #cbd5e1;
  font-size: .9rem;
}

.history-row span:first-child {
  color: var(--text);
  font-weight: 800;
}

.history-row strong:nth-child(2)::before {
  content: "Entrées ";
  color: var(--muted);
  font-weight: 700;
}

.history-row strong:nth-child(3)::before {
  content: "Clôt. ";
  color: var(--muted);
  font-weight: 700;
}

.history-row span:nth-child(5)::before {
  content: "Gain ";
  color: var(--muted);
}

.history-row span:nth-child(6)::before {
  content: "Liq. ";
  color: var(--muted);
}

.history-head {
  background: transparent;
  border-color: transparent;
  color: var(--muted);
  font-size: .78rem;
  font-weight: 800;
  padding-top: 0;
  padding-bottom: 0;
}

.history-head span:first-child {
  color: var(--muted);
}

.history-head span::before {
  content: none !important;
}

.trade-detail-row {
  display: grid;
  grid-template-columns: 76px 72px 72px minmax(0, 1fr) 120px;
  gap: 8px;
  align-items: center;
  padding: 9px 10px;
  border: 1px solid rgba(148, 163, 184, .14);
  border-radius: 10px;
  background: rgba(2, 8, 23, .42);
  color: #cbd5e1;
  font-size: .9rem;
}

.trade-detail-row strong:nth-child(2) {
  color: var(--blue);
}

.trade-detail-row .positive {
  color: var(--green);
}

.trade-detail-row .negative {
  color: #fca5a5;
}

.trade-detail-head {
  background: transparent;
  border-color: transparent;
  color: var(--muted);
  font-size: .78rem;
  font-weight: 800;
  padding-top: 0;
  padding-bottom: 0;
}

label {
  color: var(--muted);
  font-size: .9rem;
}

input, select {
  width: 100%;
  min-height: 44px;
  margin-top: 6px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(2, 8, 23, .62);
  color: var(--text);
  padding: 0 10px;
}

.warning {
  margin: 12px 0 0;
  color: #fde68a;
  line-height: 1.4;
}

.account-cell, .sim-cell {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px;
  background: rgba(255, 255, 255, .05);
}

.account-cell span, .sim-cell span {
  color: var(--muted);
  font-size: .78rem;
}

.account-cell strong, .sim-cell strong {
  display: block;
  margin-top: 4px;
}

.sim-cell strong.positive {
  color: var(--green);
}

.sim-cell strong.negative {
  color: #fca5a5;
}

.history-row .positive {
  color: var(--green);
}

.history-row .negative {
  color: #fca5a5;
}

.logs {
  display: grid;
  gap: 7px;
  max-height: 340px;
  overflow: auto;
}

.log {
  display: grid;
  grid-template-columns: 76px 76px minmax(0, 1fr);
  gap: 8px;
  padding: 9px 10px;
  border: 1px solid rgba(148, 163, 184, .14);
  border-radius: 10px;
  background: rgba(2, 8, 23, .42);
  color: #cbd5e1;
  font-size: .9rem;
}

.log strong {
  color: var(--blue);
}

@media (max-width: 760px) {
  .app {
    padding: 12px;
    padding-top: max(56px, calc(12px + env(safe-area-inset-top)));
  }

  .status-grid, .form-grid, .account-grid, .sim-grid, .ema-line {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .market-chart-card {
    grid-column: auto;
    min-height: 150px;
  }

  .market-chart-card canvas {
    height: 106px;
  }

  .price-card {
    order: 1;
  }

  .feed-card {
    order: 2;
  }

  .market-chart-card {
    order: 3;
    grid-column: 1 / -1;
  }

  .bot-state-card {
    order: 4;
    grid-column: 1 / -1;
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .mobile-pnl-card {
    order: 5;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 10px;
    grid-column: 1 / -1;
  }

  .mobile-pnl-item:nth-child(2) {
    text-align: center;
  }

  .mobile-pnl-item:last-child {
    text-align: right;
  }

  .price-card strong {
    font-size: clamp(1.45rem, 7vw, 2.2rem);
  }

  .feed-card {
    padding: 14px;
  }

  .feed-card strong {
    font-size: clamp(.95rem, 4vw, 1.2rem);
  }

  .feed-card small {
    white-space: normal;
    overflow: visible;
    text-overflow: clip;
  }

  .strategy-head {
    align-items: flex-start;
    flex-direction: column;
  }

  .strategy-head .soft-label {
    max-width: 100%;
    white-space: normal;
    line-height: 1.2;
  }

  .sim-actions {
    grid-template-columns: 1fr;
  }

  .pnl-summary {
    grid-template-columns: 1fr;
  }

  .optimizer-summary {
    grid-template-columns: 1fr;
  }

  .controls {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px;
    padding: 8px;
  }

  .controls button {
    min-height: 38px;
    border-radius: 10px;
    padding: 0 6px;
    font-size: .78rem;
  }

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

  .trade-row {
    grid-template-columns: 64px 64px minmax(0, 1fr);
  }

  .history-row {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .trade-history-head, .trade-history-actions {
    flex-direction: column;
    align-items: stretch;
  }

  .trade-history-actions select {
    width: 100%;
  }

  .trade-detail-row {
    grid-template-columns: 64px 64px minmax(0, 1fr);
  }

  .trade-detail-head {
    display: none;
  }

  .log span:first-child {
    grid-row: span 2;
  }
}

@media (max-width: 430px) {
  .form-grid, .ema-line, .account-grid, .sim-grid {
    grid-template-columns: 1fr;
  }

  .market-chart-card {
    grid-column: 1 / -1;
  }

  .chart-card-head {
    align-items: flex-start;
    flex-direction: column;
  }

  .chart-card-actions {
    width: 100%;
    justify-content: space-between;
  }

  .bot-state-card {
    grid-column: 1 / -1;
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .bot-state-item strong {
    font-size: .92rem;
  }

  .controls button {
    font-size: .68rem;
  }

  .topbar {
    align-items: center;
    flex-direction: row;
  }

  .topbar h1 {
    font-size: clamp(2rem, 12vw, 3rem);
  }

  .badge {
    min-height: 30px;
    padding: 0 10px;
    font-size: .82rem;
  }
}
