/* ---------- Themes: piece colours, board colours, page colours ---------- */
:root {
  --bg: #120c2b;
  --bg2: #1d1442;
  --panel: rgba(255, 255, 255, 0.06);
  --panel-line: rgba(255, 255, 255, 0.12);
  --text: #f6f2ff;
  --muted: #b8acd9;
  --accent: #ff5fa2;
  --good: #34d399;
  --bad: #f87171;

  --gem-k: #ffd60a;
  --gem-q: #ff3fa4;
  --gem-r: #2ec4ff;
  --gem-b: #2ee59d;
  --gem-n: #ff4d4d;
  --gem-p: #ffffff;

  --sq: min(10.5vw, 76px);
  font-family: 'Fredoka', system-ui, sans-serif;
}

body[data-theme='candy'] {
  --w-hi: #fff6c9; --w-lo: #ff9f1c; --w-line: #7a3b00;
  --b-hi: #c9a7ff; --b-lo: #5a189a; --b-line: #1b0636;
  --light: #ffe3f1; --dark: #ff8fc4;
  --board-edge: #b0306f;
}
body[data-theme='neon'] {
  --w-hi: #d9ffef; --w-lo: #00c896; --w-line: #004d3a;
  --b-hi: #ffb3c7; --b-lo: #e0115f; --b-line: #4a0320;
  --light: #2a2550; --dark: #17133a;
  --board-edge: #00e5ff;
  --gem-p: #00e5ff;
}
body[data-theme='ocean'] {
  --w-hi: #ffffff; --w-lo: #7dd3fc; --w-line: #075985;
  --b-hi: #fdba74; --b-lo: #c2410c; --b-line: #431407;
  --light: #e0f7ff; --dark: #3fa7d6;
  --board-edge: #0b6e99;
}
body[data-theme='battle'] {
  --w-hi: #6f93e8; --w-lo: #2347a0; --w-line: #0d1b3d;
  --b-hi: #d14a4a; --b-lo: #8a1616; --b-line: #2a0505;
  --light: #d9bb8e; --dark: #5e3c22;
  --board-edge: #2b1a0f;
  --accent: #d4a83a;
}
body[data-theme='royal'] {
  --w-hi: #fffdf4; --w-lo: #e2c275; --w-line: #5b4310;
  --b-hi: #6b7bff; --b-lo: #111a6b; --b-line: #05082b;
  --light: #f3e9d2; --dark: #7b5b3a;
  --board-edge: #3b2414;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  background: radial-gradient(1200px 700px at 15% -10%, #3b1d7a 0%, transparent 60%),
    radial-gradient(900px 600px at 110% 110%, #7a1d5c 0%, transparent 55%), var(--bg);
  color: var(--text);
  min-height: 100vh;
}
.hidden { display: none !important; }
button, input, select { font: inherit; color: inherit; }

/* ---------- Buttons & inputs ---------- */
.btn {
  border: 1px solid var(--panel-line);
  background: rgba(255, 255, 255, 0.08);
  padding: 9px 14px;
  border-radius: 12px;
  cursor: pointer;
  font-weight: 500;
  transition: transform 0.1s, background 0.2s;
}
.btn:hover { background: rgba(255, 255, 255, 0.16); }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn.primary {
  background: linear-gradient(135deg, #ff5fa2, #8b5cf6);
  border: none;
  font-weight: 600;
  box-shadow: 0 6px 20px rgba(255, 95, 162, 0.35);
}
.btn.danger { border-color: rgba(248, 113, 113, 0.5); }
.btn.ghost { background: transparent; }
.btn.small { padding: 6px 10px; font-size: 14px; }
.btn.icon { padding: 6px 10px; }

input, select {
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid var(--panel-line);
  border-radius: 10px;
  padding: 8px 10px;
  width: 100%;
}
select option { background: var(--bg2); }
label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: 8px; margin-top: 22px; }
label.check input { width: auto; }
input[type='range'] { padding: 0; accent-color: var(--accent); }

/* ---------- Lobby ---------- */
#lobby { max-width: 1100px; margin: 0 auto; padding: 32px 16px 48px; }
.brand { text-align: center; margin-bottom: 28px; }
.brand h1 {
  font-size: clamp(40px, 7vw, 68px);
  margin: 8px 0 4px;
  background: linear-gradient(90deg, #ffd60a, #ff5fa2, #8b5cf6, #2ec4ff);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.brand p { color: var(--muted); margin: 0; font-size: 18px; }
.brand-pieces { display: flex; justify-content: center; gap: 6px; }
.brand-pieces .piece { width: 56px; height: 56px; animation: bob 2.4s ease-in-out infinite; }
.brand-pieces .piece:nth-child(2n) { animation-delay: -1.2s; }
@keyframes bob { 50% { transform: translateY(-8px) rotate(-4deg); } }

.modes { display: grid; grid-template-columns: repeat(auto-fit, minmax(290px, 1fr)); gap: 18px; }
.mode-card {
  background: var(--panel);
  border: 1px solid var(--panel-line);
  border-radius: 20px;
  padding: 22px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  position: relative;
  overflow: hidden;
}
.mode-card::before {
  content: '';
  position: absolute;
  inset: 0 0 auto 0;
  height: 5px;
  background: var(--card-accent);
}
.mode-card[data-accent='pink'] { --card-accent: linear-gradient(90deg, #ff5fa2, #ffb86b); }
.mode-card[data-accent='violet'] { --card-accent: linear-gradient(90deg, #8b5cf6, #2ec4ff); }
.mode-card[data-accent='gold'] { --card-accent: linear-gradient(90deg, #ffd60a, #34d399); }
.mode-card h2 { margin: 0; font-size: 22px; }
.mode-card p { margin: 0; color: var(--muted); }
.mode-card .btn.primary { margin-top: auto; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.join-row { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
#joinCode { text-transform: uppercase; letter-spacing: 2px; }
.level-name { color: var(--text); font-weight: 600; }

/* ---------- Game layout ---------- */
#game { padding: 12px 16px 24px; max-width: 1320px; margin: 0 auto; }
.topbar { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 12px; margin-bottom: 12px; }
.topbar-right { display: flex; gap: 8px; }
.topbar-right select { width: auto; }
.status {
  text-align: center;
  font-size: 20px;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--panel-line);
  justify-self: center;
  transition: background 0.3s;
}
.status.check { background: rgba(248, 113, 113, 0.25); border-color: var(--bad); }
.status.over { background: linear-gradient(90deg, rgba(255, 95, 162, 0.3), rgba(139, 92, 246, 0.3)); }

.layout {
  display: grid;
  grid-template-columns: minmax(170px, 230px) auto minmax(260px, 340px);
  gap: 18px;
  align-items: start;
  justify-content: center;
}

/* ---------- Board ---------- */
.board-col { display: flex; flex-direction: column; gap: 8px; }
.board-wrap { position: relative; width: calc(var(--sq) * 8); height: calc(var(--sq) * 8); }
.board {
  display: grid;
  grid-template-columns: repeat(8, var(--sq));
  grid-template-rows: repeat(8, var(--sq));
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 0 0 6px var(--board-edge), 0 20px 60px rgba(0, 0, 0, 0.5);
  user-select: none;
  touch-action: none;
}
.sq { position: relative; display: flex; align-items: center; justify-content: center; }
.sq.light { background: var(--light); }
.sq.dark { background: var(--dark); }
.sq .coord { position: absolute; font-size: 11px; font-weight: 600; opacity: 0.7; pointer-events: none; }
.sq .coord.file { right: 4px; bottom: 2px; }
.sq .coord.rank { left: 4px; top: 2px; }
.sq.light .coord { color: var(--dark); }
.sq.dark .coord { color: var(--light); }
.sq.last::after { content: ''; position: absolute; inset: 0; background: rgba(255, 214, 10, 0.38); }
.sq.sel::after { content: ''; position: absolute; inset: 0; background: rgba(46, 196, 255, 0.45); }
.sq.check { background: radial-gradient(circle, #ff3b3b 0%, rgba(255, 59, 59, 0.6) 45%, transparent 75%), var(--dark); }
.sq.light.check { background: radial-gradient(circle, #ff3b3b 0%, rgba(255, 59, 59, 0.6) 45%, transparent 75%), var(--light); }
.sq.target::before {
  content: '';
  position: absolute;
  width: 30%;
  height: 30%;
  border-radius: 50%;
  background: rgba(20, 10, 40, 0.35);
  z-index: 2;
  pointer-events: none;
}
.sq.target.capture::before {
  width: 92%;
  height: 92%;
  background: transparent;
  border: 5px solid rgba(255, 59, 59, 0.7);
}
.sq.hover { box-shadow: inset 0 0 0 4px rgba(255, 255, 255, 0.8); }

.piece { width: 88%; height: 88%; position: relative; z-index: 3; filter: drop-shadow(0 4px 3px rgba(0, 0, 0, 0.35)); pointer-events: none; }
.piece svg { width: 100%; height: 100%; display: block; overflow: visible; }
.piece .body-w { fill: url(#body-w); stroke: var(--w-line); stroke-width: 3; stroke-linejoin: round; }
.piece .body-b { fill: url(#body-b); stroke: var(--b-line); stroke-width: 3; stroke-linejoin: round; }
.piece .gloss { fill: url(#gloss); stroke: none; }
.gem { stroke: rgba(0, 0, 0, 0.45); stroke-width: 1.5; }
.mane { fill: none; stroke: rgba(0, 0, 0, 0.35); stroke-width: 3; stroke-linecap: round; }
.nostril { fill: rgba(0, 0, 0, 0.5); }
.sq.mine { cursor: grab; }
.sq.mine:hover .piece { transform: translateY(-3px) scale(1.04); transition: transform 0.12s; }
.sq.dragging .piece { opacity: 0.25; }
.drag-ghost {
  position: fixed;
  width: calc(var(--sq) * 1.05);
  height: calc(var(--sq) * 1.05);
  pointer-events: none;
  z-index: 100;
  transform: translate(-50%, -55%) scale(1.12);
  filter: drop-shadow(0 12px 10px rgba(0, 0, 0, 0.45));
}
.piece.moving { transition: transform 0.22s cubic-bezier(0.2, 0.8, 0.3, 1.2); z-index: 10; }

/* Capture explosion shards */
.fx { position: absolute; inset: 0; pointer-events: none; z-index: 20; }
.shard {
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  animation: shard 0.75s cubic-bezier(0.1, 0.7, 0.4, 1) forwards;
}
@keyframes shard {
  0% { transform: translate(0, 0) rotate(0) scale(1); opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) rotate(var(--rot)) scale(0.3); opacity: 0; }
}
.ring {
  position: absolute;
  border-radius: 50%;
  border: 4px solid #fff;
  animation: ring 0.5s ease-out forwards;
}
@keyframes ring { from { transform: scale(0.2); opacity: 1; } to { transform: scale(1.6); opacity: 0; } }
.board-wrap.shake { animation: shake 0.3s; }
@keyframes shake { 25% { transform: translate(-3px, 2px); } 50% { transform: translate(3px, -2px); } 75% { transform: translate(-2px, -1px); } }

.overlay {
  position: absolute;
  inset: 0;
  z-index: 30;
  border-radius: 14px;
  background: rgba(10, 5, 30, 0.72);
  backdrop-filter: blur(3px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  text-align: center;
  padding: 24px;
}
.overlay h2 { margin: 0; font-size: 34px; }
.overlay p { margin: 0; color: var(--muted); max-width: 360px; }
.overlay .btns { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
.overlay .link-box { display: flex; gap: 8px; width: min(100%, 420px); }
.overlay .link-box input { font-size: 13px; }

/* ---------- Player strips & clocks ---------- */
.player-strip {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid var(--panel-line);
  transition: box-shadow 0.3s, border-color 0.3s;
}
.player-strip.turn { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(255, 95, 162, 0.35); }
.player-strip .dot { width: 16px; height: 16px; border-radius: 50%; border: 2px solid rgba(0, 0, 0, 0.4); }
.player-strip .dot.w { background: linear-gradient(135deg, var(--w-hi), var(--w-lo)); }
.player-strip .dot.b { background: linear-gradient(135deg, var(--b-hi), var(--b-lo)); }
.player-strip .pname { font-weight: 600; }
.player-strip .ptag { color: var(--muted); font-size: 13px; }
.player-strip .ptag.off { color: var(--bad); }
.player-strip .clock {
  margin-left: auto;
  font-variant-numeric: tabular-nums;
  font-size: 22px;
  font-weight: 600;
  padding: 2px 12px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.3);
  min-width: 86px;
  text-align: center;
}
.player-strip .clock:empty { display: none; }
.player-strip .clock.run { background: var(--text); color: var(--bg); }
.player-strip .clock.low { background: var(--bad); color: #fff; animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: 0.7; } }

/* ---------- Rubble ---------- */
.rubble-panel { display: flex; flex-direction: column; gap: 14px; height: calc(var(--sq) * 8 + 104px); justify-content: space-between; }
.rubble {
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.04), rgba(0, 0, 0, 0.25));
  border: 1px dashed var(--panel-line);
  border-radius: 16px;
  padding: 12px;
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
}
.rubble::after {
  /* a little heap of dust at the bottom */
  content: '';
  position: absolute;
  left: -10%;
  right: -10%;
  bottom: -28px;
  height: 60px;
  background: radial-gradient(ellipse at center, rgba(180, 160, 220, 0.25), transparent 70%);
  pointer-events: none;
}
.rubble-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 6px; }
.rubble-title { font-weight: 600; font-size: 14px; color: var(--muted); }
.adv { font-weight: 700; color: var(--bad); }
.rubble-pile { display: flex; flex-wrap: wrap-reverse; align-content: flex-start; gap: 0; flex: 1; align-items: flex-end; }
.rubble-pile .piece {
  width: 48px;
  height: 48px;
  margin: -4px -6px;
  filter: saturate(0.75) drop-shadow(0 3px 2px rgba(0, 0, 0, 0.5));
  transform: rotate(var(--tilt));
}
.rubble-pile .piece::after {
  /* crack across the fallen piece */
  content: '';
  position: absolute;
  inset: 20% 30% 25% 35%;
  background: linear-gradient(115deg, transparent 45%, rgba(0, 0, 0, 0.55) 47%, rgba(0, 0, 0, 0.55) 50%, transparent 52%),
    linear-gradient(60deg, transparent 55%, rgba(0, 0, 0, 0.4) 57%, transparent 59%);
}
.rubble-pile .piece.fresh { animation: tumble 0.6s cubic-bezier(0.3, 1.4, 0.5, 1); }
@keyframes tumble {
  from { transform: translateY(-80px) rotate(-120deg) scale(1.4); opacity: 0; }
  to { transform: rotate(var(--tilt)); opacity: 1; }
}
.rubble-empty { color: var(--muted); font-size: 13px; opacity: 0.6; }

/* ---------- Side panel: log, controls, chat ---------- */
.side-panel {
  background: var(--panel);
  border: 1px solid var(--panel-line);
  border-radius: 18px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  height: calc(var(--sq) * 8 + 104px);
}
.share { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 12px; background: rgba(255, 95, 162, 0.12); }
.share b { letter-spacing: 2px; }
.log-head { display: flex; justify-content: space-between; align-items: baseline; }
.log-head h3 { margin: 0; }
#moveCount { color: var(--muted); font-size: 13px; }
.log { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 6px; scroll-behavior: smooth; }
.log li {
  display: grid;
  grid-template-columns: 30px 34px 1fr;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.2);
  border-left: 4px solid transparent;
  font-size: 14px;
  animation: slidein 0.25s ease-out;
}
@keyframes slidein { from { transform: translateX(16px); opacity: 0; } }
.log li.w { border-left-color: var(--w-lo); }
.log li.b { border-left-color: var(--b-lo); }
.log li.cap { background: rgba(248, 113, 113, 0.14); }
.log .num { color: var(--muted); font-size: 12px; text-align: right; }
.log .piece { width: 32px; height: 32px; }
.log .who { font-weight: 600; }
.log .what { color: var(--muted); }
.log .what b { color: var(--text); font-weight: 600; }
.log .tag { display: inline-block; font-size: 11px; font-weight: 600; padding: 1px 6px; border-radius: 6px; margin-left: 4px; }
.log .tag.kill { background: var(--bad); color: #fff; }
.log .tag.chk { background: #ffd60a; color: #000; }
.log .tag.mate { background: #fff; color: #000; }
.log .tag.special { background: #8b5cf6; color: #fff; }
.log-empty { color: var(--muted); font-size: 14px; text-align: center; margin-top: 20px; }
.controls { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.controls .btn { padding: 8px 4px; font-size: 14px; }

.chat { display: flex; flex-direction: column; gap: 6px; height: 150px; border-top: 1px solid var(--panel-line); padding-top: 8px; }
.chat-msgs { flex: 1; overflow-y: auto; font-size: 14px; display: flex; flex-direction: column; gap: 3px; }
.chat-msgs .m b { margin-right: 4px; }
.chat-msgs .sys { color: var(--muted); font-style: italic; }
#chatForm { display: grid; grid-template-columns: 1fr auto; gap: 6px; }

/* ---------- Modal & toast ---------- */
.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.6); display: flex; align-items: center; justify-content: center; z-index: 200; }
.modal-card { background: var(--bg2); border: 1px solid var(--panel-line); border-radius: 20px; padding: 20px 24px; text-align: center; }
.modal-card h3 { margin: 0 0 12px; }
.promo-choices { display: flex; gap: 10px; }
.promo-choices button { width: 84px; height: 96px; border-radius: 14px; border: 1px solid var(--panel-line); background: var(--light); cursor: pointer; display: flex; flex-direction: column; align-items: center; justify-content: center; color: #222; font-size: 12px; font-weight: 600; }
.promo-choices button:hover { transform: translateY(-3px); }
.promo-choices .piece { width: 60px; height: 60px; }
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: var(--bg2);
  border: 1px solid var(--panel-line);
  padding: 10px 18px;
  border-radius: 12px;
  z-index: 300;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}

/* ---------- Smaller screens ---------- */
@media (max-width: 1100px) {
  .layout { grid-template-columns: auto minmax(260px, 340px); }
  .rubble-panel { grid-column: 1 / -1; order: 3; flex-direction: row; height: auto; }
  .rubble { min-height: 90px; }
}
@media (max-width: 760px) {
  :root { --sq: calc((100vw - 32px) / 8); }
  #game { padding: 8px 16px 24px; }
  .layout { grid-template-columns: 1fr; }
  .board-col { order: 1; }
  .rubble-panel { order: 2; }
  .side-panel { order: 3; height: 460px; }
  .topbar { grid-template-columns: auto 1fr; }
  .topbar-right { grid-column: 1 / -1; justify-content: center; }
  .status { font-size: 16px; }
  .row { grid-template-columns: 1fr; }
  label.check { margin-top: 0; }
  .rubble-pile .piece { width: 32px; height: 32px; }
}
