* {
  box-sizing: border-box;
}

:root {
  --bg: #0a0e14;
  --bg-grad: #0d131b;
  --panel: #121a24;
  --panel-border: #22303f;
  --text: #e7edf3;
  --text-dim: #7e8fa0;
  --green: #22c58b;
  --green-dim: rgba(34, 197, 139, 0.14);
  --red: #ef4a5f;
  --red-dim: rgba(239, 74, 95, 0.14);
  --accent: #4da3ff;
  --accent-dim: rgba(77, 163, 255, 0.14);
}

html {
  background: var(--bg);
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: linear-gradient(180deg, var(--bg-grad), var(--bg));
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text);
  display: flex;
  justify-content: center;
  touch-action: manipulation;
}

.wrap {
  width: 100%;
  max-width: 480px;
  padding: 24px 16px 48px;
}

.hidden {
  display: none !important;
}

/* ---------- Header ---------- */

.stats-primary {
  display: flex;
  justify-content: center;
  gap: 28px;
  min-height: 2.6rem;
}

.stat-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-variant-numeric: tabular-nums;
}

.stat-value {
  font-size: 1.6rem;
  font-weight: 700;
  line-height: 1.15;
}

#timeLeft.urgent {
  color: var(--red);
}

.stat-unit {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-dim);
}

.stats-secondary {
  text-align: center;
  font-size: 0.85rem;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  min-height: 1.2em;
  margin-bottom: 20px;
}

.pl-positive { color: var(--green); }
.pl-negative { color: var(--red); }
.pl-neutral { color: var(--text-dim); }

.history-link {
  background: none;
  border: none;
  color: var(--accent);
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  text-decoration: underline;
  cursor: pointer;
  padding: 0;
}

.history-link:hover {
  opacity: 0.8;
}

.vs-chip {
  display: flex;
  align-items: baseline;
  gap: 6px;
  background: var(--panel);
  border: 2px solid var(--panel-border);
  border-radius: 999px;
  padding: 8px 16px;
}

.vs-chip-me,
.vs-chip-opp {
  font-size: 1.3rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.vs-chip-me { color: var(--accent); }
.vs-chip-opp { color: var(--text-dim); }

.vs-chip-sep {
  font-size: 0.65rem;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* ---------- Positions (basket overview + ticker picker) ---------- */

.positions-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 16px;
  animation: reveal 0.35s ease;
}

.position-row {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 10px 14px;
  cursor: pointer;
  text-align: left;
  transition: border-color 0.15s ease;
}

.position-row:hover {
  border-color: var(--text-dim);
}

.position-row.active {
  border-color: var(--accent);
}

.position-top,
.position-bottom {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

.position-top {
  font-weight: 700;
  margin-bottom: 4px;
}

.position-price {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

.position-change {
  font-size: 0.78rem;
  font-weight: 700;
}

.position-change.up { color: var(--green); }
.position-change.down { color: var(--red); }

.position-bottom {
  font-size: 0.8rem;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

.risk-tag {
  font-size: 0.62rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 2px 6px;
  border-radius: 999px;
  margin-left: 4px;
  vertical-align: middle;
}

.risk-aggressive { background: var(--red-dim); color: var(--red); }
.risk-balanced { background: var(--accent-dim); color: var(--accent); }
.risk-defensive { background: var(--green-dim); color: var(--green); }

/* ---------- Ticker / chart ---------- */

.ticker-panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 16px;
  margin-bottom: 16px;
  animation: reveal 0.35s ease;
}

@keyframes reveal {
  0% { opacity: 0; transform: translateY(-6px); }
  100% { opacity: 1; transform: translateY(0); }
}

.ticker-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 10px;
}

.ticker-name {
  font-weight: 800;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  font-size: 0.85rem;
}

.ticker-price-block {
  text-align: right;
}

.ticker-price {
  font-size: 1.8rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  display: block;
  line-height: 1.1;
}

.ticker-change {
  font-size: 0.85rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.ticker-change.up { color: var(--green); }
.ticker-change.down { color: var(--red); }

.price-chart {
  display: block;
  width: 100%;
  height: 220px;
  border-radius: 10px;
}

.chart-range {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  color: var(--text-dim);
  margin-top: 8px;
  font-variant-numeric: tabular-nums;
}

/* ---------- Trade panel ---------- */

.trade-panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 16px;
  animation: reveal 0.35s ease;
}

.trade-side {
  margin-bottom: 14px;
}

.trade-side-title {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin: 0 0 8px;
}

.buy-title { color: var(--green); }
.sell-title { color: var(--red); }

.quick-row {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}

/* Fills the amount field only — it never fires a trade on its own, Buy/Sell
   is always the explicit confirm step, same as typing a number by hand. */
.quick-btn {
  flex: 1 1 0;
  border: 1px solid var(--panel-border);
  background: transparent;
  color: var(--text);
  border-radius: 8px;
  padding: 8px 4px;
  font-weight: 700;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.12s ease, transform 0.06s ease;
}

.quick-btn.buy:hover { background: var(--green-dim); border-color: var(--green); }
.quick-btn.sell:hover { background: var(--red-dim); border-color: var(--red); }
.quick-btn:active { transform: scale(0.96); }
.quick-btn:disabled { opacity: 0.35; cursor: not-allowed; }
.quick-btn:disabled:hover { background: transparent; border-color: var(--panel-border); }

.amount-row {
  display: flex;
  align-items: center;
  gap: 0;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  overflow: hidden;
}

.amount-prefix {
  padding: 0 0 0 12px;
  color: var(--text-dim);
  font-weight: 700;
}

.amount-input {
  flex: 1 1 auto;
  min-width: 0;
  background: transparent;
  border: none;
  color: var(--text);
  font: inherit;
  font-variant-numeric: tabular-nums;
  padding: 10px 8px;
  outline: none;
}

.amount-input::-webkit-outer-spin-button,
.amount-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.trade-btn {
  border: none;
  font-weight: 800;
  padding: 10px 18px;
  cursor: pointer;
  font-size: 0.9rem;
}

.trade-btn.buy { background: var(--green); color: #06251a; }
.trade-btn.sell { background: var(--red); color: #2b0b10; }
.trade-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.fee-note {
  text-align: center;
  font-size: 0.72rem;
  color: var(--text-dim);
  margin-top: 4px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 12px);
  background: var(--panel);
  border: 1px solid var(--panel-border);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
  box-shadow: 0 8px 20px rgba(0,0,0,0.4);
  max-width: 90vw;
  text-align: center;
  z-index: 70;
}

.toast.visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ---------- End overlay ---------- */

.end-overlay {
  position: fixed;
  inset: 0;
  background: rgba(5, 8, 12, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 60;
}

.end-card {
  width: 100%;
  max-width: 360px;
  max-height: 90vh;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 24px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0,0,0,0.5);
}

.end-card h2 {
  margin: 0 0 8px;
}

.end-score {
  font-size: 2.2rem;
  font-weight: 800;
  color: var(--accent);
  margin-bottom: 4px;
  font-variant-numeric: tabular-nums;
}

.end-best {
  font-size: 0.9rem;
  color: var(--text-dim);
  margin-bottom: 18px;
}

.end-waiting {
  font-size: 0.95rem;
  color: var(--text-dim);
  margin-bottom: 18px;
}

.end-result-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 10px 4px;
  border-bottom: 1px solid var(--panel-border);
  font-weight: 700;
}

.end-result-name {
  font-size: 1rem;
}

.end-result-score {
  font-size: 1.3rem;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

#endMeRow .end-result-score {
  color: var(--accent);
}

.end-bar {
  display: flex;
  height: 14px;
  border-radius: 999px;
  overflow: hidden;
  background: var(--panel-border);
  margin: 14px 0 8px;
}

.end-bar-me {
  background: var(--accent);
}

.end-bar-opp {
  background: var(--text-dim);
  opacity: 0.6;
}

.end-delta {
  text-align: center;
  font-size: 0.85rem;
  color: var(--text-dim);
}

.end-verdict {
  margin: 14px 0 4px;
  font-weight: 700;
  font-size: 1.05rem;
}

.play-again {
  background: none;
  border: 2px solid var(--panel-border);
  color: var(--text);
  border-radius: 999px;
  padding: 8px 18px;
  font-weight: 700;
  cursor: pointer;
}

/* ---------- Trade history ---------- */

.history-card {
  max-width: 400px;
  text-align: left;
}

.history-card h2 {
  text-align: center;
  margin-bottom: 14px;
}

.history-empty {
  text-align: center;
  color: var(--text-dim);
  font-size: 0.9rem;
  padding: 16px 0;
}

.history-list {
  max-height: 50vh;
  overflow-y: auto;
  margin-bottom: 16px;
}

.history-row {
  display: grid;
  grid-template-columns: 3.2em 3em 1fr auto;
  align-items: baseline;
  gap: 8px;
  padding: 8px 4px;
  border-bottom: 1px solid var(--panel-border);
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
}

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

.history-time {
  color: var(--text-dim);
  font-size: 0.78rem;
}

.history-side {
  font-weight: 800;
  font-size: 0.75rem;
  letter-spacing: 0.03em;
}

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

.history-detail {
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-amount {
  font-weight: 700;
  text-align: right;
}

/* ---------- How to play ---------- */

.howto-card {
  max-width: 400px;
  text-align: left;
}

.howto-card h2 {
  text-align: center;
  margin-bottom: 14px;
}

.howto-list {
  margin: 0 0 18px;
  padding-left: 1.2em;
  display: flex;
  flex-direction: column;
  gap: 10px;
  font-size: 0.9rem;
  line-height: 1.4;
  color: var(--text-dim);
}

.howto-list strong {
  color: var(--text);
}

/* ---------- Debug ---------- */

.debug-toggle {
  position: fixed;
  right: 14px;
  bottom: 14px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--panel);
  color: var(--text);
  opacity: 0.35;
  cursor: pointer;
  font-size: 1rem;
  transition: opacity 0.15s ease;
  z-index: 50;
}

.debug-toggle:hover {
  opacity: 0.9;
}

.debug-panel {
  position: fixed;
  right: 14px;
  bottom: 58px;
  width: 240px;
  max-width: calc(100vw - 28px);
  background: #0d1319;
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 14px;
  box-shadow: 0 12px 30px rgba(0,0,0,0.5);
  z-index: 50;
  font-size: 0.8rem;
}

.debug-panel h3 {
  margin: 0 0 4px;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.debug-note {
  margin: 0 0 10px;
  opacity: 0.6;
  font-size: 0.72rem;
  line-height: 1.3;
}

.debug-group {
  margin-bottom: 10px;
}

.debug-label {
  display: block;
  opacity: 0.6;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 4px;
}

.debug-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.debug-row button {
  flex: 1 1 auto;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 0.72rem;
  cursor: pointer;
}

.debug-row button:hover {
  background: #1a2530;
}

.debug-close {
  width: 100%;
  margin-top: 4px;
  background: transparent;
  border: 1px solid var(--panel-border);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 0.75rem;
  cursor: pointer;
  opacity: 0.7;
}

.debug-close:hover {
  opacity: 1;
}

/* ---------- Menu screens ---------- */

.menu-screen {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 12px;
  max-width: 320px;
  margin: 15vh auto 0;
  text-align: center;
}

.menu-title {
  margin: 0;
  font-size: 1.8rem;
  font-weight: 700;
}

.menu-tagline {
  margin: 0 0 8px;
  color: var(--text-dim);
  font-size: 0.95rem;
}

.menu-btn {
  border: 2px solid var(--panel-border);
  background: none;
  color: var(--text);
  border-radius: 999px;
  padding: 12px 18px;
  font-weight: 700;
  font-size: 1rem;
  cursor: pointer;
}

.menu-btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #05131f;
}

.menu-btn.small {
  padding: 10px 14px;
  flex: 0 0 auto;
}

.menu-btn.link {
  border: none;
  opacity: 0.6;
  font-weight: 600;
}

.menu-btn.link:hover {
  opacity: 1;
}

.menu-input {
  border: 2px solid var(--panel-border);
  border-radius: 8px;
  padding: 12px 14px;
  font: inherit;
  background: var(--panel);
  color: var(--text);
  text-align: center;
}

.lobby-link {
  margin-bottom: 8px;
}

.lobby-link-row {
  display: flex;
  gap: 8px;
}

.lobby-link-row .menu-input {
  flex: 1 1 auto;
  min-width: 0;
}

.lobby-players {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 8px 0;
  text-align: left;
}

.lobby-player {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--panel);
  border: 2px solid var(--panel-border);
  border-radius: 10px;
  padding: 10px 14px;
  font-weight: 600;
}

.lobby-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--panel-border);
  flex: 0 0 auto;
}

.lobby-dot.ready {
  background: var(--green);
}
