:root {
  --red: #C8102E;
  --red-light: #FF4D63;
  --ink: #0E1525;
  --deep: #070B14;
  --steel: #1F4E79;
  --steel-light: #3F8FD2;
  --gold: #F2A900;
  --paper: #FAF7F2;
  --slate: #8A94A6;
  --card: rgba(14, 21, 37, 0.92);
  --line: rgba(242, 169, 0, 0.35);
  --display: Anton, Impact, "Arial Black", sans-serif;
  --body: Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--deep);
  color: var(--paper);
  font-family: var(--body);
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
  user-select: none;
}

canvas#stage {
  position: fixed;
  inset: 0;
  display: block;
  touch-action: none;
}

.hidden { display: none !important; }

button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }

/* ---------- HUD ---------- */
#hud {
  position: fixed;
  top: 0; left: 0; right: 0;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: start;
  gap: 8px;
  padding: max(12px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 8px max(16px, env(safe-area-inset-left));
  pointer-events: none;
}
.side { display: flex; flex-direction: column; gap: 6px; }
.side-o { align-items: flex-end; text-align: right; }
.who {
  font-family: var(--display);
  font-size: clamp(16px, 2.6vw, 24px);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.side-x .who { color: var(--red-light); }
.side-o .who { color: var(--steel-light); }
.side.active .who { text-shadow: 0 0 14px currentColor; }
.side.active .who::after { content: " ⚔"; font-size: 0.8em; }
.side-o.active .who::after { content: ""; }
.side-o.active .who::before { content: "⚔ "; font-size: 0.8em; }
.pips { display: flex; gap: 6px; }
.pip { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--slate); }
.side-x .pip.on { background: var(--red); border-color: var(--red-light); }
.side-o .pip.on { background: var(--steel-light); border-color: #9cc8ef; }
.mid { text-align: center; max-width: 52vw; }
#roundLabel {
  font-family: var(--display);
  letter-spacing: 0.12em;
  color: var(--gold);
  font-size: 14px;
  text-transform: uppercase;
}
#prompt { font-weight: 600; font-size: clamp(13px, 2vw, 17px); margin-top: 4px; min-height: 1.3em; }

#corner {
  position: fixed;
  right: max(12px, env(safe-area-inset-right));
  bottom: max(12px, env(safe-area-inset-bottom));
  display: flex;
  gap: 8px;
  z-index: 5;
}
.icon-btn {
  width: 40px; height: 40px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: rgba(14, 21, 37, 0.8);
  display: grid; place-items: center;
  font-weight: 800;
}
.icon-btn svg { display: block; }

/* ---------- Stance picker ---------- */
#controls {
  position: fixed;
  left: 50%;
  bottom: max(14px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: min(560px, calc(100vw - 32px));
  text-align: center;
  z-index: 4;
  animation: rise 0.25s ease-out;
}
@keyframes rise { from { transform: translate(-50%, 20px); opacity: 0; } }
#controlsTitle { font-family: var(--display); letter-spacing: 0.08em; text-transform: uppercase; margin-bottom: 8px; font-size: 18px; }
#timer { height: 6px; background: rgba(255,255,255,0.1); border-radius: 3px; overflow: hidden; margin-bottom: 10px; }
#timerFill { height: 100%; width: 100%; background: var(--gold); transform-origin: left; }
#timer.low #timerFill { background: var(--red-light); }
.stances { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stance {
  position: relative;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 12px 6px 10px;
  border-radius: 14px;
  border: 2px solid var(--line);
  background: linear-gradient(180deg, #1c2740, #0f1626);
  box-shadow: 0 6px 0 #05080f;
  transition: transform 0.08s, box-shadow 0.08s, border-color 0.15s;
}
.stance svg { width: 34px; height: 34px; fill: none; stroke: var(--gold); stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round; }
.stance b { font-family: var(--display); font-weight: 400; font-size: 22px; letter-spacing: 0.05em; text-transform: uppercase; }
.stance small { color: var(--slate); font-size: 12px; min-height: 1.2em; }
.stance .key { position: absolute; top: 6px; left: 8px; font-size: 11px; color: var(--slate); }
.stance:hover { border-color: var(--gold); transform: translateY(-2px); }
.stance:active { transform: translateY(4px); box-shadow: 0 2px 0 #05080f; }
#controls.x .stance:hover { border-color: var(--red-light); }
#controls.o .stance:hover { border-color: var(--steel-light); }

/* ---------- Overlays ---------- */
.overlay {
  position: fixed; inset: 0;
  display: grid; place-items: center;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  z-index: 10;
  background: rgba(7, 11, 20, 0.35);
  overflow-y: auto;
}
.card {
  width: min(460px, 100%);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 24px 22px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  backdrop-filter: blur(6px);
  animation: pop 0.28s cubic-bezier(.2,1.4,.4,1);
}
@keyframes pop { from { transform: scale(0.9); opacity: 0; } }
.card h2 { font-family: var(--display); font-weight: 400; font-size: 36px; letter-spacing: 0.03em; margin: 0 0 8px; text-transform: uppercase; }
.card p { line-height: 1.5; }

#menu { background: linear-gradient(180deg, rgba(7,11,20,0.15), rgba(7,11,20,0.75)); }
.menu-card { background: rgba(14, 21, 37, 0.82); }
.logo { display: flex; align-items: center; justify-content: center; gap: 14px; }
.logo-mark { width: 84px; height: 84px; flex: none; }
.logo h1 { margin: 0; font-family: var(--display); font-weight: 400; line-height: 0.95; text-align: left; text-transform: uppercase; }
.logo .l1 { display: block; font-size: 52px; letter-spacing: 0.02em; }
.logo .l2 { display: block; font-size: 24px; letter-spacing: 0.22em; color: var(--red-light); }
.tagline { color: #c9d0dc; margin: 14px 0 18px; }

.btn {
  display: block; width: 100%;
  margin: 10px 0 0;
  padding: 14px 18px;
  border-radius: 14px;
  border: 2px solid var(--line);
  background: #17213a;
  font-weight: 800;
  font-size: 17px;
  box-shadow: 0 5px 0 #05080f;
  transition: transform 0.08s, box-shadow 0.08s;
}
.btn:active { transform: translateY(3px); box-shadow: 0 2px 0 #05080f; }
.btn.primary { background: linear-gradient(180deg, #e01a3a, var(--red)); border-color: #ff6b7e; }
.btn.ghost { background: transparent; box-shadow: none; }
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 6px; margin-top: 6px; }
.seg button { padding: 8px 4px; border-radius: 10px; border: 1px solid var(--line); background: transparent; font-weight: 600; color: #c9d0dc; display: flex; flex-direction: column; align-items: center; line-height: 1.2; }
.seg button small { font-weight: 400; font-size: 11px; opacity: 0.8; }
.seg button:disabled { opacity: 0.35; cursor: not-allowed; }
.seg button[aria-checked="true"] { background: var(--gold); color: var(--ink); border-color: var(--gold); }
.options { margin-top: 18px; display: grid; gap: 8px; text-align: left; font-size: 15px; }
.options label { display: flex; align-items: center; gap: 10px; }
.options small { color: var(--slate); }
.options input { width: 18px; height: 18px; accent-color: var(--gold); }
.fine { color: var(--slate); font-size: 12px; margin: 14px 0 0; }

.rules { text-align: left; padding-left: 20px; line-height: 1.5; }
.rules li { margin-bottom: 6px; }
.triangle { display: grid; gap: 6px; margin: 12px 0; }
.triangle div { padding: 8px 10px; border-radius: 10px; background: rgba(255,255,255,0.05); }
.tip { color: #c9d0dc; font-size: 14px; }

.score { font-family: var(--display); font-size: 48px; margin: 6px 0 4px; letter-spacing: 0.06em; }
.score .x { color: var(--red-light); }
.score .o { color: var(--steel-light); }
.stats { width: 100%; border-collapse: collapse; margin: 10px 0; font-size: 14px; }
.stats th, .stats td { padding: 6px 4px; border-bottom: 1px solid rgba(255,255,255,0.08); }
.stats th { text-align: left; color: var(--slate); font-weight: 600; }
.stats td { text-align: center; }
.stats td.x { color: var(--red-light); font-weight: 800; }
.stats td.o { color: var(--steel-light); font-weight: 800; }

@media (max-width: 520px) {
  .logo-mark { width: 64px; height: 64px; }
  .logo .l1 { font-size: 40px; }
  .logo .l2 { font-size: 18px; }
  .stance { padding: 10px 4px 8px; }
  .stance b { font-size: 18px; }
  .stance small { font-size: 10.5px; }
  .card { padding: 20px 16px; }
  #corner { bottom: auto; top: calc(104px + env(safe-area-inset-top)); right: max(10px, env(safe-area-inset-right)); }
  .icon-btn { width: 34px; height: 34px; }
}
@media (prefers-reduced-motion: reduce) {
  .card, #controls { animation: none; }
}

/* ---------- v0.2: setup, store, online, social ---------- */
.card.wide { width: min(620px, 100%); }
.card h3 { font-family: var(--display); font-weight: 400; letter-spacing: 0.04em; text-transform: uppercase; margin: 16px 0 4px; color: var(--gold); }
.profile-strip { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin: 12px 0 4px; }
.name-btn { background: transparent; border: 1px dashed var(--line); border-radius: 10px; padding: 6px 10px; font-weight: 600; }
.coins { font-weight: 800; color: var(--gold); white-space: nowrap; }
.btn.online { background: linear-gradient(180deg, #2a5f96, var(--steel)); border-color: var(--steel-light); }
.btn.small { padding: 8px 10px; font-size: 14px; margin: 0; width: auto; box-shadow: 0 3px 0 #05080f; }
.grid4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 10px; }
.grid4 .btn.small { width: 100%; }
.field { text-align: left; margin-top: 12px; }
.field .label { font-size: 13px; color: var(--slate); font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; }
.field .hint { display: block; color: #c9d0dc; margin-top: 6px; font-size: 13px; }
#segStake { grid-auto-flow: row; grid-template-columns: repeat(6, 1fr); }
.fighters { display: flex; gap: 8px; overflow-x: auto; padding: 6px 2px; }
.fighter { flex: none; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 4px 6px; border-radius: 12px; border: 2px solid transparent; background: rgba(255,255,255,0.04); font-size: 12px; }
.fighter.on { border-color: var(--gold); background: rgba(242,169,0,0.12); }
#onlineBox { margin-top: 14px; }
.join-row { display: flex; gap: 8px; margin-top: 10px; }
.join-row input, details.server input {
  flex: 1; min-width: 0; padding: 12px; border-radius: 12px; border: 2px solid var(--line); background: #0b1220; color: var(--paper);
  font: 600 16px var(--body); letter-spacing: 0.08em;
}
.join-row .btn { width: auto; margin: 0; }
details.server { margin-top: 10px; text-align: left; color: var(--slate); font-size: 13px; }
details.server input { width: 100%; margin-top: 6px; letter-spacing: 0; font-size: 14px; }
.status { color: #c9d0dc; font-size: 14px; min-height: 1.2em; }
.code { font-family: var(--display); font-size: 56px; letter-spacing: 0.25em; color: var(--gold); margin: 6px 0; user-select: all; }
.spinner { width: 34px; height: 34px; margin: 10px auto; border-radius: 50%; border: 4px solid rgba(255,255,255,0.15); border-top-color: var(--gold); animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.earned { color: var(--gold); font-weight: 700; min-height: 1.2em; }

/* Store */
.store-head { display: flex; justify-content: space-between; align-items: center; }
.tabs { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin: 10px 0; }
.tabs button { padding: 8px; border-radius: 10px; border: 1px solid var(--line); background: transparent; font-weight: 700; }
.tabs button[aria-selected="true"] { background: var(--gold); color: var(--ink); }
.items { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; max-height: min(56vh, 520px); overflow-y: auto; padding: 2px; }
.item { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 10px 8px; border-radius: 14px; border: 2px solid var(--rarity); background: rgba(255,255,255,0.04); }
.item canvas { border-radius: 10px; background: radial-gradient(circle at 50% 40%, #1d2a45, #0b1220); }
.item small { color: #c9d0dc; font-size: 11px; line-height: 1.3; text-align: center; }
.item .rarity { font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--rarity); font-weight: 800; }
.item .btn.small { width: 100%; margin-top: auto; }

/* Friends and leaderboard */
.friend-list { list-style: none; padding: 0; margin: 12px 0; text-align: left; }
.friend-list li { display: flex; align-items: center; gap: 8px; padding: 8px 4px; border-bottom: 1px solid rgba(255,255,255,0.08); }
.friend-list li b { flex: 1; }
.friend-list li small { color: var(--slate); }
.friend-list li.empty { color: #c9d0dc; font-size: 14px; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: #555; flex: none; }
.dot.idle { background: #3DBA6F; }
.dot.playing { background: var(--gold); }
.stats tr.me td, .stats tr.me th { color: var(--gold); }

/* Power-ups, toast, chat */
#powers {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: max(14px, env(safe-area-inset-bottom));
  display: flex; gap: 8px; z-index: 6;
}
body:has(#controls:not(.hidden)) #powers { bottom: calc(max(14px, env(safe-area-inset-bottom)) + 206px); }
.power { display: flex; flex-direction: column; align-items: center; padding: 6px 10px; border-radius: 12px; border: 2px solid #7FD3FF66; background: rgba(14,21,37,0.9); min-width: 92px; }
.power .pi { font-size: 20px; }
.power b { font-size: 13px; }
.power small { font-size: 11px; color: var(--slate); }
.power.on { border-color: #7FD3FF; box-shadow: 0 0 16px #7FD3FF88; }
.power:disabled { opacity: 0.35; }
#toast { position: fixed; left: 50%; transform: translateX(-50%); top: calc(18% + env(safe-area-inset-top)); background: rgba(14,21,37,0.95); border: 1px solid var(--gold); border-radius: 14px; padding: 10px 16px; z-index: 20; font-weight: 600; max-width: calc(100vw - 32px); text-align: center; }
#chatPanel { position: fixed; right: max(12px, env(safe-area-inset-right)); bottom: calc(64px + env(safe-area-inset-bottom)); z-index: 12; width: min(320px, calc(100vw - 24px)); background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 10px; }
#chatPanel .emotes { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; margin-bottom: 8px; }
#chatPanel .emotes button { font-size: 24px; background: transparent; border: none; }
#chatPanel .phrases { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
#chatPanel .phrases button { padding: 8px; border-radius: 10px; border: 1px solid var(--line); background: #17213a; font-size: 13px; font-weight: 600; }
.bubble { display: inline-block; margin-top: 4px; padding: 6px 10px; border-radius: 12px; background: var(--paper); color: var(--ink); font-weight: 700; font-size: 13px; max-width: 40vw; animation: pop 0.2s ease-out; }
.bubble.big { font-size: 26px; background: transparent; padding: 0; }
@media (max-width: 520px) {
  .grid4 { grid-template-columns: repeat(2, 1fr); }
  #segStake { grid-template-columns: repeat(3, 1fr); }
  .power { min-width: 76px; padding: 5px 6px; }
  #chatPanel { bottom: auto; top: calc(150px + env(safe-area-inset-top)); }
}

/* Real-money wallet (test mode) */
.balances { display: flex; align-items: center; gap: 8px; }
.wallet-chip { font: inherit; font-weight: 800; color: #7FD6A4; background: transparent; border: 1px solid rgba(127, 214, 164, 0.45); border-radius: 999px; padding: 3px 10px; white-space: nowrap; cursor: pointer; }
.wallet-chip:hover { background: rgba(127, 214, 164, 0.12); }
.test-banner { background: rgba(242, 169, 0, 0.12); color: var(--gold); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; font-weight: 700; font-size: 13px; margin: 8px 0 0; }
.wallet-sum { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin-top: 12px; }
.wallet-sum div { border: 1px solid var(--line); border-radius: 12px; padding: 10px 6px; text-align: center; }
.wallet-sum .label { display: block; font-size: 11px; color: var(--slate); font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; }
.wallet-sum b { font-size: clamp(18px, 5vw, 24px); color: var(--paper); }
#wallet .hint { color: #c9d0dc; font-size: 13px; }
.adult { display: flex; align-items: center; gap: 8px; margin-top: 10px; font-size: 14px; color: #c9d0dc; }
#btnSetupWallet { width: auto; margin-top: 8px; }
