:root {
  --bg: #14161f;
  --panel: #1f2330;
  --ink: #eef1f7;
  --muted: #9aa3b5;
  --blue: #3d8bff;
  --red: #ff4d5e;
  --elixir: #c04dff;
  --gold: #ffcc33;
  --ok: #43d17a;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink);
  font: 15px/1.4 "Segoe UI", system-ui, sans-serif; user-select: none; }
#app { height: 100%; display: flex; justify-content: center; }
.screen { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.hidden { display: none !important; }
.panel { background: var(--panel); border-radius: 16px; padding: 28px 32px; width: min(460px, 92vw);
  box-shadow: 0 12px 40px #0008; display: flex; flex-direction: column; gap: 14px; }
h1 { margin: 0; font-size: 34px; letter-spacing: 1px; }
h1 span { color: var(--gold); }
.sub { margin: 0; color: var(--muted); }
label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; }
label.check { flex-direction: row; align-items: center; font-weight: 400; color: var(--muted); }
select, input[type=number] { background: #11141c; color: var(--ink); border: 1px solid #39405a;
  border-radius: 8px; padding: 9px 10px; font-size: 15px; }
button { background: #2c3347; color: var(--ink); border: 0; border-radius: 10px; padding: 10px 18px;
  font-size: 15px; font-weight: 700; cursor: pointer; }
button:hover { filter: brightness(1.2); }
button.big { background: linear-gradient(#ffd54a, #f0a91c); color: #2a1c00; font-size: 20px; padding: 14px; }
.help { font-size: 13px; color: var(--muted); }
kbd { background: #11141c; border: 1px solid #39405a; border-radius: 4px; padding: 0 5px; font-size: 12px; }
.error { color: var(--red); min-height: 1em; }
.row { display: flex; gap: 10px; }
.row button { flex: 1; }

/* game */
#game { justify-content: flex-start; padding: 6px 0; gap: 6px; }
#topbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; width: var(--arena-w, 400px); }
.crowns { font-weight: 800; font-size: 18px; padding: 4px 10px; border-radius: 8px; min-width: 96px; }
.crowns.red { background: #ff4d5e33; color: #ff98a2; }
.crowns.blue { background: #3d8bff33; color: #9cc4ff; text-align: right; }
.crowns i { font-style: normal; color: var(--gold); }
#timer-box { text-align: center; }
#timer { font-size: 26px; font-weight: 800; font-variant-numeric: tabular-nums; }
#timer.urgent { color: var(--red); }
#rate { font-size: 12px; color: var(--elixir); font-weight: 800; min-height: 1em; }
#bot-hand { display: flex; gap: 4px; font-size: 12px; color: var(--muted); }
#bot-hand .card { width: 54px; height: 40px; font-size: 11px; }
#stage { position: relative; }
#arena { display: block; border-radius: 10px; box-shadow: 0 6px 24px #0009; touch-action: none; }
#banner { position: absolute; left: 50%; top: 42%; transform: translate(-50%, -50%);
  font-size: 34px; font-weight: 900; color: var(--gold); text-shadow: 0 3px 0 #000, 0 0 20px #000;
  pointer-events: none; white-space: nowrap; animation: pop .35s ease-out; }
@keyframes pop { from { transform: translate(-50%, -50%) scale(.4); opacity: 0; } }
#toasts { position: absolute; left: 50%; bottom: 10px; transform: translateX(-50%);
  display: flex; flex-direction: column; align-items: center; gap: 4px; pointer-events: none; }
.toast { background: #000c; color: #fff; padding: 5px 12px; border-radius: 8px; font-size: 13px;
  animation: fade 2.2s forwards; }
.toast.bad { color: #ffb3ba; }
@keyframes fade { 0% { opacity: 0 } 10% { opacity: 1 } 75% { opacity: 1 } 100% { opacity: 0 } }

#hud { display: flex; align-items: flex-end; gap: 8px; width: var(--arena-w, 400px); }
#hand { display: flex; gap: 6px; flex: 1; }
.card { position: relative; flex: 1; height: 92px; border-radius: 10px; border: 2px solid #ffffff22;
  display: flex; flex-direction: column; align-items: center; justify-content: center; cursor: grab;
  font-weight: 800; text-align: center; transition: transform .08s, filter .15s; overflow: hidden; }
.card .icon { font-size: 30px; line-height: 1; }
.card .name { font-size: 11px; margin-top: 2px; text-shadow: 0 1px 2px #000; }
.card .cost { position: absolute; left: 4px; top: 4px; width: 22px; height: 22px; border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg); background: var(--elixir); display: flex; align-items: center; justify-content: center; }
.card .cost b { transform: rotate(45deg); font-size: 13px; }
.card .key { position: absolute; right: 5px; bottom: 3px; font-size: 10px; color: #fff8; }
.card.poor { filter: grayscale(.85) brightness(.55); }
.card.selected { transform: translateY(-10px); border-color: var(--gold); box-shadow: 0 0 14px var(--gold); }
.card.pending { animation: pulse .6s infinite alternate; }
@keyframes pulse { to { filter: brightness(1.6); } }
#next-box { width: 54px; text-align: center; color: var(--muted); font-size: 11px; }
#next .card { height: 60px; cursor: default; }
#next .card .icon { font-size: 20px; }
#next .card .name { font-size: 9px; }
#elixir-wrap { display: flex; align-items: center; gap: 8px; width: var(--arena-w, 400px); }
#elixir-drop { width: 34px; height: 34px; border-radius: 50% 50% 50% 0; transform: rotate(-45deg);
  background: var(--elixir); display: flex; align-items: center; justify-content: center; font-weight: 900; font-size: 16px; }
#elixir-drop b { transform: rotate(45deg); }
#elixir-bar { position: relative; flex: 1; height: 22px; background: #2a1438; border-radius: 11px; overflow: hidden; }
#elixir-fill { position: absolute; inset: 0 auto 0 0; width: 50%; background: linear-gradient(#e18bff, #9b2fd6); }
#elixir-ticks { position: absolute; inset: 0; background: repeating-linear-gradient(90deg, transparent 0 calc(10% - 2px), #0007 calc(10% - 2px) 10%); }

#end-title { font-size: 44px; text-align: center; }
#end-title.win { color: var(--gold); }
#end-title.lose { color: var(--red); }
#end-crowns { text-align: center; font-size: 26px; font-weight: 800; }
#end-detail { color: var(--muted); font-size: 14px; text-align: center; }

/* web build */
#loading { display: flex; flex-direction: column; gap: 6px; color: var(--muted); font-size: 13px; }
#load-bar { height: 8px; background: #11141c; border-radius: 4px; overflow: hidden; }
#load-fill { height: 100%; width: 0; background: var(--gold); transition: width .3s; }
#play:disabled { filter: grayscale(1) brightness(.6); cursor: wait; }
.footer { font-size: 11px; color: var(--muted); border-top: 1px solid #2c3347; padding-top: 10px; }
.footer a { color: #9cc4ff; }
#download-replay { font-size: 13px; }
#arena3d { display: block; border-radius: 10px; box-shadow: 0 6px 24px #0009; touch-action: none; }
.card .art { width: 100%; height: 70%; object-fit: contain; pointer-events: none; filter: drop-shadow(0 2px 2px #0008); }
#elixir-bar.full #elixir-fill { animation: glow 0.8s infinite alternate; }
@keyframes glow { to { filter: brightness(1.45) saturate(1.3); } }
.crown-row { display: flex; align-items: center; justify-content: center; gap: 10px; margin: 6px 0; }
.crown-row .who { width: 46px; font-size: 14px; color: var(--muted); text-align: right; }
.crown { font-size: 44px; color: #3a3f52; text-shadow: 0 2px 0 #0008; transform: scale(.9); }
.crown.on { animation: crownIn .45s cubic-bezier(.3,1.6,.6,1) forwards; }
.crown-row.blue .crown.on { color: var(--gold); }
.crown-row.red .crown.on { color: #ff9aa5; }
@keyframes crownIn { 0% { transform: scale(1); } 40% { transform: scale(0.2) rotate(-30deg); } 100% { transform: scale(1.1); } }

/* phones */
html, body { overscroll-behavior: none; touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
body { padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
#game { height: 100dvh; overflow: hidden; padding: 4px 0; gap: 4px; }
.card { touch-action: none; height: clamp(62px, 11.5vh, 92px); }
#menu-error pre { white-space: pre-wrap; font-size: 11px; max-height: 160px; overflow: auto; background: #11141c; padding: 6px; border-radius: 6px; }
@media (max-width: 480px) {
  .panel { padding: 18px 16px; gap: 10px; }
  h1 { font-size: 28px; }
  .crowns { font-size: 15px; min-width: 70px; padding: 3px 8px; }
  #timer { font-size: 22px; }
  .card .name { font-size: 9px; }
  .card .cost { width: 18px; height: 18px; }
  .card .cost b { font-size: 11px; }
  #next-box { width: 44px; }
  #next .card { height: 50px; }
  #elixir-drop { width: 28px; height: 28px; font-size: 14px; }
}
@media (max-width: 480px) { .toast { font-size: 11px; padding: 3px 8px; opacity: .85; } }

/* dragging a card */
.card.lifted { opacity: .25; transform: scale(.92); }
#drag-card { position: fixed; z-index: 50; pointer-events: none; margin: 0; flex: none;
  transform: translate(-50%, -70%) rotate(-7deg) scale(1.12); box-shadow: 0 14px 30px #000a;
  transition: transform .14s ease-out, opacity .14s ease-out, left .22s ease-out, top .22s ease-out; }
#drag-card:not(.back) { transition: transform .14s ease-out, opacity .14s ease-out; }
#drag-card.over { transform: translate(-50%, -50%) scale(.38); opacity: .35; }
#drag-card.over.bad { filter: grayscale(.6) brightness(.8) drop-shadow(0 0 6px #ff3344); }
#drag-card.drop { transform: translate(-50%, -50%) scale(.05); opacity: 0; transition: transform .2s ease-in, opacity .2s ease-in; }
#drag-card.back { transform: translate(-50%, -50%) rotate(0) scale(1); opacity: .9; }

/* deck builder */
#my-deck { background: #171a24; border-radius: 12px; padding: 10px; display: flex; flex-direction: column; gap: 8px; }
#my-deck .row-head { display: flex; justify-content: space-between; color: var(--muted); font-size: 13px; }
#my-deck .row-head b { color: var(--ink); font-size: 15px; }
#my-deck-cards, #deck-slots { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.mcard { position: relative; height: 74px; border-radius: 9px; border: 2px solid #ffffff22; display: flex; flex-direction: column;
  align-items: center; justify-content: flex-end; padding-bottom: 3px; overflow: hidden; cursor: pointer; user-select: none; }
.mcard .art { position: absolute; inset: 4px 4px 16px; width: calc(100% - 8px); height: calc(100% - 20px); object-fit: contain; }
.mcard .icon { position: absolute; top: 18%; font-size: 24px; }
.mcard .name { position: relative; font-size: 10px; font-weight: 800; text-shadow: 0 1px 2px #000; text-align: center; line-height: 1.1; }
.mcard .cost { position: absolute; left: 3px; top: 3px; width: 18px; height: 18px; border-radius: 50% 50% 50% 0; transform: rotate(-45deg);
  background: var(--elixir); display: flex; align-items: center; justify-content: center; z-index: 1; }
.mcard .cost b { transform: rotate(45deg); font-size: 11px; }
.mcard.picked { outline: 3px solid var(--gold); filter: brightness(.6); }
.mcard.empty { background: #11141c; border-style: dashed; color: #39405a; font-size: 26px; justify-content: center; cursor: default; }
.mcard:hover:not(.empty) { filter: brightness(1.15); }
#deck { justify-content: flex-start; overflow-y: auto; height: 100dvh; width: 100%; }
.deck-wrap { width: min(640px, 96vw); display: flex; flex-direction: column; gap: 10px; padding: 10px 0 30px; }
.deck-top { position: sticky; top: 0; z-index: 5; background: var(--panel); border-radius: 14px; padding: 12px; display: flex; flex-direction: column; gap: 8px; box-shadow: 0 8px 20px #0008; }
.deck-top h2 { margin: 0; font-size: 20px; display: flex; justify-content: space-between; align-items: baseline; }
.deck-top h2 span { font-size: 13px; color: var(--muted); font-weight: 600; }
.deck-top .big { padding: 10px; font-size: 17px; }
.deck-presets { color: var(--muted); font-size: 13px; }
#presets { display: flex; gap: 6px; overflow-x: auto; padding: 6px 0; }
.preset { flex: none; display: flex; flex-direction: column; align-items: flex-start; font-size: 13px; padding: 7px 10px; background: #232838; }
.preset small { font-weight: 400; color: var(--muted); font-size: 10px; max-width: 150px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.deck-filters { display: flex; gap: 6px; }
.deck-filters input { flex: 1; background: #11141c; color: var(--ink); border: 1px solid #39405a; border-radius: 8px; padding: 8px 10px; font-size: 14px; }
.deck-filters select { background: #11141c; color: var(--ink); border: 1px solid #39405a; border-radius: 8px; padding: 8px; }
#deck-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(78px, 1fr)); gap: 6px; }
@media (max-width: 480px) { .mcard { height: 66px; } #deck-grid { grid-template-columns: repeat(4, 1fr); } }
