:root {
  --bg: #0d1117;
  --bg2: #161b22;
  --bg3: #21262d;
  --border: #30363d;
  --text: #e6edf3;
  --muted: #8b949e;
  --green: #3fb950;
  --green-dim: #238636;
  --red: #f85149;
  --red-dim: #da3633;
  --accent: #58a6ff;
  --gold: #d29922;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

#app { min-height: 100dvh; display: flex; flex-direction: column; }
.view { flex: 1; display: flex; flex-direction: column; }
.hidden { display: none !important; }
.muted { color: var(--muted); }

/* ---------- home ---------- */
.home-wrap {
  margin: auto;
  width: min(420px, 92vw);
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 32px 0;
  text-align: center;
}
.logo { font-size: 2rem; }
.tagline { color: var(--muted); margin-bottom: 10px; }

input[type="text"], input[type="number"] {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font-size: 1.1rem;
  padding: 14px;
  width: 100%;
  text-align: center;
}
input:focus { outline: 2px solid var(--accent); border-color: transparent; }
#code-input { text-transform: uppercase; letter-spacing: 0.3em; font-weight: 700; }

.join-row { display: flex; gap: 10px; }
.join-row input { flex: 1; }
.join-row .btn { flex: 1; }

.btn {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  cursor: pointer;
  font-size: 1rem;
  font-weight: 600;
  padding: 12px 16px;
  touch-action: manipulation;
  transition: filter 0.1s, transform 0.05s;
}
.btn:active { transform: scale(0.97); filter: brightness(1.2); }
.btn.primary { background: var(--green-dim); border-color: transparent; }
.btn.danger { background: transparent; border-color: var(--red-dim); color: var(--red); }
.btn.big { padding: 16px; font-size: 1.15rem; }
.btn.small { padding: 8px 12px; font-size: 0.85rem; }
.btn:disabled { opacity: 0.45; pointer-events: none; }

.rules { text-align: left; margin-top: 16px; color: var(--muted); font-size: 0.9rem; }
.rules summary { cursor: pointer; color: var(--text); font-weight: 600; padding: 6px 0; }
.rules ul { padding: 10px 0 0 18px; display: grid; gap: 8px; }

/* ---------- lobby ---------- */
.lobby-wrap {
  margin: auto;
  width: min(420px, 92vw);
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 32px 0;
  text-align: center;
}
.lobby-label { color: var(--muted); font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.1em; }
.lobby-code {
  font-size: 3rem;
  font-weight: 800;
  letter-spacing: 0.35em;
  padding-left: 0.35em; /* visually recenter */
  color: var(--accent);
}
.lobby-players { list-style: none; display: flex; flex-direction: column; gap: 8px; }
.lobby-players li {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 10px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  font-weight: 600;
}
.lobby-players .who { flex: 1; text-align: left; }
.lobby-players .tag { color: var(--muted); font-size: 0.8rem; font-weight: 400; }
.kick { background: none; border: none; color: var(--red); font-size: 1.1rem; cursor: pointer; padding: 4px 8px; }
.bot-row { display: flex; gap: 8px; }
.bot-row .btn { flex: 1; }
#lobby-host-controls { display: flex; flex-direction: column; gap: 14px; }

/* ---------- game ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--bg2);
  position: sticky;
  top: 0;
  z-index: 5;
}
.chip {
  background: var(--bg3);
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 700;
  padding: 4px 12px;
}
.conn-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--green); margin-left: auto; }
.conn-dot.off { background: var(--red); }

.board {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 14px;
  width: min(560px, 100vw);
  margin: 0 auto;
}
.section-label {
  color: var(--muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  margin-bottom: 6px;
}

/* cards */
.cards { display: flex; gap: 8px; flex-wrap: wrap; }
.card {
  width: 58px;
  height: 82px;
  border-radius: 8px;
  background: #f5f0e8;
  color: #111;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 1.25rem;
  position: relative;
  box-shadow: 0 2px 6px rgba(0,0,0,0.5);
}
.card.red { color: #c22; }
.card .suit { font-size: 1.15rem; line-height: 1; }
.card .val {
  position: absolute;
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--text);
  font-size: 0.68rem;
  font-weight: 700;
  padding: 1px 7px;
  white-space: nowrap;
}
.card.pos .val { color: var(--green); }
.card.neg .val { color: var(--red); }
.card.back {
  background: repeating-linear-gradient(45deg, #1a3a5c, #1a3a5c 6px, #14304e 6px, #14304e 12px);
  border: 2px solid #2a5a8c;
}
.card.reveal { animation: flip 0.45s ease; }
@keyframes flip {
  from { transform: rotateY(90deg); }
  to { transform: rotateY(0); }
}
.hand-sum { color: var(--gold); margin-left: 6px; letter-spacing: normal; text-transform: none; }

/* player strip */
.player-strip {
  list-style: none;
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
  scrollbar-width: none;
}
.player-strip li {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 10px;
  flex: 0 0 auto;
  min-width: 86px;
  padding: 8px 10px;
  text-align: center;
  font-size: 0.8rem;
}
.player-strip li.maker { border-color: var(--gold); box-shadow: 0 0 0 1px var(--gold); }
.player-strip li.me { background: #10202e; }
.player-strip .pname { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 110px; }
.player-strip .pmeta { color: var(--muted); font-size: 0.72rem; margin-top: 2px; }
.player-strip .pos-long { color: var(--green); }
.player-strip .pos-short { color: var(--red); }
.player-strip li.disconnected { opacity: 0.5; }
.player-strip .passed { color: var(--muted); }

/* action panel */
.action-panel {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 130px;
  justify-content: center;
}
.panel-title { font-weight: 700; text-align: center; }
.panel-sub { color: var(--muted); text-align: center; font-size: 0.9rem; }

.quote-builder { display: flex; flex-direction: column; gap: 12px; }
.quote-display {
  display: flex;
  justify-content: center;
  align-items: baseline;
  gap: 12px;
  font-variant-numeric: tabular-nums;
}
.quote-display .bid { color: var(--green); font-size: 2.2rem; font-weight: 800; }
.quote-display .ask { color: var(--red); font-size: 2.2rem; font-weight: 800; }
.quote-display .at { color: var(--muted); font-size: 1.4rem; }
.stepper-row { display: flex; gap: 8px; justify-content: center; }
.stepper-row .btn { flex: 1; max-width: 80px; font-variant-numeric: tabular-nums; }

.order-buttons { display: flex; gap: 10px; }
.order-buttons .btn { flex: 1; padding: 18px 8px; font-size: 1.05rem; line-height: 1.3; }
.btn.sell { background: var(--red-dim); border-color: transparent; }
.btn.buy { background: var(--green-dim); border-color: transparent; }
.btn.pass-btn { flex: 0.6; }
.order-buttons small { display: block; font-weight: 400; opacity: 0.85; }

.live-quote {
  text-align: center;
  font-size: 1.6rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}
.live-quote .bid { color: var(--green); }
.live-quote .ask { color: var(--red); }

/* countdown */
.timebar { height: 5px; background: var(--bg3); border-radius: 999px; overflow: hidden; }
.timebar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 999px;
  transition: width 0.25s linear;
}
.timebar-fill.urgent { background: var(--red); }

/* results */
.results { display: flex; flex-direction: column; gap: 10px; }
.actual-value { text-align: center; font-size: 1.1rem; }
.actual-value b { color: var(--gold); font-size: 1.6rem; }
.pnl-table { width: 100%; border-collapse: collapse; font-size: 0.95rem; }
.pnl-table td { padding: 8px 6px; border-bottom: 1px solid var(--border); }
.pnl-table td:last-child { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }
.pnl-pos { color: var(--green); }
.pnl-neg { color: var(--red); }
.trophy { margin-right: 4px; }
.result-hand { display: flex; gap: 4px; align-items: center; flex-wrap: wrap; }
.mini-card {
  background: #f5f0e8;
  color: #111;
  border-radius: 4px;
  font-size: 0.72rem;
  font-weight: 800;
  padding: 2px 5px;
}
.mini-card.red { color: #c22; }

/* log */
.log {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.82rem;
  color: var(--muted);
  max-height: 150px;
  overflow-y: auto;
  display: flex;
  flex-direction: column-reverse;
  gap: 4px;
}
.log .latest { color: var(--text); }

/* toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--red-dim);
  color: #fff;
  border-radius: 10px;
  padding: 12px 18px;
  font-weight: 600;
  z-index: 50;
  max-width: 90vw;
  text-align: center;
  animation: toast-in 0.2s ease;
}
.toast.info { background: var(--bg3); border: 1px solid var(--border); }
@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, 10px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

@media (min-width: 600px) {
  .card { width: 66px; height: 94px; font-size: 1.4rem; }
}

/* ---------- helpers toggle ---------- */
.no-helpers .card .val { display: none; }
.no-helpers .hand-sum { display: none; }
.no-helpers .card { height: 74px; }

.home-links { display: flex; gap: 10px; justify-content: center; }

.topbar-btn {
  background: none;
  border: none;
  color: var(--text);
  font-size: 1.1rem;
  cursor: pointer;
  padding: 4px 8px;
}

/* ---------- settings modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 40;
}
.modal-card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 16px 16px 0 0;
  padding: 22px 18px calc(22px + env(safe-area-inset-bottom));
  width: min(480px, 100vw);
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.modal-card h2 { font-size: 1.15rem; }
.setting-row {
  display: flex;
  gap: 14px;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
}
.setting-row small { display: block; color: var(--muted); margin-top: 4px; line-height: 1.4; }
.setting-row input[type="checkbox"] {
  appearance: none;
  width: 52px;
  height: 30px;
  flex: 0 0 auto;
  border-radius: 999px;
  background: var(--bg3);
  border: 1px solid var(--border);
  position: relative;
  cursor: pointer;
  transition: background 0.15s;
}
.setting-row input[type="checkbox"]::after {
  content: "";
  position: absolute;
  top: 2px; left: 2px;
  width: 24px; height: 24px;
  border-radius: 50%;
  background: var(--muted);
  transition: transform 0.15s, background 0.15s;
}
.setting-row input[type="checkbox"]:checked { background: var(--green-dim); }
.setting-row input[type="checkbox"]:checked::after { transform: translateX(22px); background: #fff; }

/* ---------- quote builder input ---------- */
.bid-input-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}
.bid-input-row input {
  width: 130px;
  font-size: 1.8rem;
  font-weight: 800;
  color: var(--green);
  padding: 8px;
  font-variant-numeric: tabular-nums;
}
.bid-input-row .at { color: var(--muted); font-size: 1.4rem; }
.bid-input-row .ask-preview {
  color: var(--red);
  font-size: 1.8rem;
  font-weight: 800;
  min-width: 90px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}
input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; }

/* ---------- retro & stats ---------- */
.retro-head { text-align: center; padding: 8px 0; }
.retro-pnl { font-size: 2.4rem; font-weight: 800; font-variant-numeric: tabular-nums; }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(105px, 1fr));
  gap: 8px;
}
.stat-cell {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 8px;
  text-align: center;
}
.stat-value { font-size: 1.15rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.stat-label { color: var(--muted); font-size: 0.68rem; margin-top: 3px; line-height: 1.3; }

.retro-list { display: flex; flex-direction: column; gap: 8px; }
.retro-row {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
}
.retro-row.good { border-left-color: var(--green); }
.retro-row.bad { border-left-color: var(--red); }
.retro-row.warn { border-left-color: var(--gold); }
.retro-row-top {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.88rem;
  font-weight: 600;
}
.retro-round {
  background: var(--bg3);
  border-radius: 6px;
  padding: 2px 7px;
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--muted);
}
.retro-quote { flex: 1; }
.retro-tag { font-size: 0.75rem; font-weight: 700; white-space: nowrap; }
.retro-tag.good { color: var(--green); }
.retro-tag.bad { color: var(--red); }
.retro-tag.warn { color: var(--gold); }
.retro-tag.neutral { color: var(--muted); }
.retro-detail { color: var(--muted); font-size: 0.8rem; margin-top: 5px; line-height: 1.45; }
.game-link { cursor: pointer; }
.game-link:active { filter: brightness(1.3); }
