:root {
  --bg: #0f0b1e;
  --bg2: #171230;
  --card: #1d173a;
  --line: #34295f;
  --text: #ece7ff;
  --muted: #a99fd0;
  --gold: #e9b949;
  --gold2: #ffd978;
  --red: #e0525f;
  --green: #5bd08a;
  --blue: #6aa8ff;
  --radius: 12px;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: radial-gradient(1200px 600px at 70% -10%, #2a1d5a 0%, transparent 60%), var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  min-height: 100vh;
}
a { color: var(--gold2); }
[hidden] { display: none !important; }
.top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px; border-bottom: 1px solid var(--line); background: rgba(15, 11, 30, .85);
  position: sticky; top: 0; z-index: 5; backdrop-filter: blur(6px);
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--text); text-decoration: none; font-size: 18px; }
.brand b { color: var(--gold); }
.eye {
  width: 26px; height: 16px; border-radius: 50%; border: 2px solid var(--gold);
  background: radial-gradient(circle, var(--gold2) 0 3px, #2a1d5a 4px 100%);
}
.online { color: var(--muted); font-size: 13px; }
main { max-width: 1040px; margin: 0 auto; padding: 16px; }
.hero h1 { font-size: clamp(22px, 4vw, 34px); line-height: 1.2; margin: 18px 0 8px; }
.hero h1 em { color: var(--gold); font-style: normal; }
.hero p { color: var(--muted); max-width: 760px; margin: 0 0 16px; }
.card {
  background: linear-gradient(180deg, var(--card), var(--bg2)); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 18px 20px; margin: 14px 0;
}
.card h2 { margin: 0 0 14px; font-size: 19px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.card h3 { margin: 14px 0 8px; font-size: 15px; color: var(--muted); font-weight: 600; }
.step {
  display: inline-grid; place-items: center; width: 26px; height: 26px; border-radius: 50%;
  background: var(--gold); color: #1b1300; font-size: 14px; font-weight: 700;
}
.row { display: grid; grid-template-columns: 150px 1fr; gap: 10px 16px; align-items: start; padding: 9px 0; border-top: 1px solid rgba(255, 255, 255, .05); }
.row:first-of-type { border-top: 0; }
.row > label { padding-top: 8px; color: var(--muted); }
.row small { opacity: .8; }
.pick { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.state { color: var(--muted); font-size: 13.5px; flex: 1 1 260px; }
.state.ok { color: var(--green); }
.state.bad { color: var(--red); }
input, select {
  background: #120e26; color: var(--text); border: 1px solid var(--line); border-radius: 8px;
  padding: 9px 11px; font: inherit; min-width: 0;
}
input:focus, select:focus { outline: 2px solid var(--gold); outline-offset: 1px; }
.row input, .row select { width: min(100%, 320px); }
.btn {
  background: #2b2252; color: var(--text); border: 1px solid #4a3d85; border-radius: 8px;
  padding: 9px 14px; font: inherit; cursor: pointer; white-space: nowrap;
}
.btn:hover:not(:disabled) { border-color: var(--gold); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: linear-gradient(180deg, var(--gold2), var(--gold)); color: #1b1300; border-color: var(--gold); font-weight: 700; }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-top: 12px; }
.hint { color: var(--muted); font-size: 13.5px; }
.saves { grid-column: 2; display: grid; gap: 6px; }
.saves label { display: flex; gap: 10px; align-items: center; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; }
.saves label:has(input:checked) { border-color: var(--gold); background: rgba(233, 185, 73, .08); }
.lobby, .room { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.create, .join { display: flex; gap: 8px; margin-bottom: 10px; }
.join input { flex: 1; text-transform: uppercase; }
.rooms { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.rooms li { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: #150f2c; }
.rooms li .title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rooms li .who { color: var(--muted); font-size: 13px; }
.rooms li.empty { color: var(--muted); justify-content: center; border-style: dashed; }
.tag { font-size: 12px; padding: 2px 8px; border-radius: 99px; border: 1px solid var(--line); color: var(--muted); font-weight: 500; }
.code { font-family: ui-monospace, Consolas, monospace; color: var(--gold2); letter-spacing: 2px; }
.chat { display: flex; flex-direction: column; min-height: 260px; }
.lines { list-style: none; margin: 0; padding: 8px; flex: 1; overflow-y: auto; max-height: 300px; min-height: 180px; background: #120e26; border: 1px solid var(--line); border-radius: 10px; }
.lines li { padding: 2px 0; word-wrap: break-word; }
.lines b { color: var(--gold2); font-weight: 600; }
.lines .sys { color: var(--muted); font-style: italic; }
.say { display: flex; gap: 8px; margin-top: 8px; }
.say input { flex: 1; }
.seats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.seat { border: 1px solid var(--line); border-radius: 10px; padding: 12px; background: #150f2c; }
.seat .label { font-size: 12px; color: var(--muted); }
.seat .name { font-size: 17px; font-weight: 600; margin: 2px 0; }
.seat.p1 { border-left: 4px solid var(--red); }
.seat.p2 { border-left: 4px solid var(--blue); }
.seat .ready { color: var(--green); font-size: 13px; }
.seat .wait { color: var(--muted); font-size: 13px; }
.help ul { margin: 0; padding-left: 20px; color: var(--muted); }
.help li { margin: 4px 0; }
kbd { background: #2b2252; border: 1px solid #4a3d85; border-radius: 5px; padding: 0 5px; font-size: 12px; font-family: inherit; color: var(--text); }
.foot { color: var(--muted); font-size: 12.5px; text-align: center; padding: 18px 0 30px; }

/* the game */
.game { position: fixed; inset: 0; background: #000; display: flex; flex-direction: column; z-index: 10; }
.bar { display: flex; align-items: center; gap: 10px; padding: 6px 10px; background: #0d0a1a; border-bottom: 1px solid var(--line); font-size: 14px; }
.bar .who { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar .score { color: var(--gold2); font-weight: 700; }
.bar .net { color: var(--muted); font-size: 12px; }
.stage { position: relative; flex: 1; min-height: 0; display: grid; place-items: center; overflow: hidden; }
#game-canvas { width: 100%; height: 100%; object-fit: contain; display: block; outline: none; background: #000; }
.overlay { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(5, 3, 12, .55); }
.overlay .box { background: var(--card); border: 1px solid var(--gold); border-radius: var(--radius); padding: 18px 22px; max-width: 420px; text-align: center; }
.overlay p { margin: 0; font-size: 16px; }
.toast { position: absolute; top: 12px; left: 50%; transform: translateX(-50%); background: rgba(29, 23, 58, .92); border: 1px solid var(--line); border-radius: 99px; padding: 6px 16px; font-size: 14px; pointer-events: none; }
.toast.bad { border-color: var(--red); }
.drawer { position: absolute; right: 10px; bottom: 10px; width: min(340px, calc(100% - 20px)); background: rgba(19, 14, 38, .94); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; display: flex; flex-direction: column; }
.drawer .lines { max-height: 220px; min-height: 120px; }
.drawer.keys { top: 10px; bottom: auto; font-size: 14px; }
.drawer.keys p { margin: 4px 0; }

@media (max-width: 760px) {
  .row { grid-template-columns: 1fr; }
  .row > label { padding-top: 0; }
  .saves { grid-column: 1; }
  .lobby, .room, .seats { grid-template-columns: 1fr; }
  main { padding: 10px; }
  .card { padding: 14px; }
  .bar { font-size: 13px; gap: 6px; }
}

/* the other player's secret screen (duel.js conceal): opaque, over the picture only */
.cover { position: absolute; inset: 0; display: grid; place-items: center; background: #0d0a1a;
         background-image: radial-gradient(600px 300px at 50% 45%, #2a1d5a 0%, transparent 70%); }
.cover p { margin: 0; padding: 12px 20px; border: 1px solid var(--line); border-radius: 99px; color: var(--muted); font-size: 15px; }

/* the key layout (keys.js) */
.keys { margin-top: 10px; }
.keygrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 6px; }
.keycap { display: flex; justify-content: space-between; align-items: center; gap: 10px; text-align: left; }
.keycap kbd { min-width: 34px; text-align: center; }
.keycap.listening { border-color: var(--gold); background: rgba(233, 185, 73, .12); }
#keys-summary kbd { margin: 0 1px; }

/* watching a room: the board, the room's chat beside it (below on phones) */
.watch { display: grid; grid-template-columns: minmax(0, 1fr) 280px; gap: 16px; align-items: start; }
.watch .chat .lines { max-height: 420px; }
.rooms li .room-actions { display: flex; gap: 6px; flex: none; }
@media (max-width: 980px) { .watch { grid-template-columns: minmax(0, 1fr); } }

/* spectator board (js/view.js) */
.bd-p1 { --bd-pc: var(--red); --bd-pt: #ff8d97; }
.bd-p2 { --bd-pc: var(--blue); --bd-pt: #9cc5ff; }
.bd-pn { color: var(--bd-pt); font-weight: 700; }
.bd { display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; max-width: 920px; margin: 0 auto; min-width: 0; }
.bd-mat {
  min-width: 0; display: flex; flex-direction: column; gap: 6px; padding: 8px;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: radial-gradient(520px 280px at 50% 50%, #251b4c 0%, transparent 70%), linear-gradient(180deg, #161131, #120e26);
}

/* a player's bar: name and wins, LP on the right, deck and hand below */
.bd-info {
  display: grid; grid-template-columns: minmax(0, 1fr) auto; grid-template-areas: "who lp" "counts lp";
  align-items: center; column-gap: 10px; padding: 6px 10px; border-radius: 10px;
  border: 1px solid var(--line); border-left: 4px solid var(--bd-pc);
  background: #150f2c; background: linear-gradient(90deg, color-mix(in srgb, var(--bd-pc) 14%, #150f2c), #150f2c 65%);
}
.bd-info.bd-turn { border-color: var(--gold); border-left-color: var(--bd-pc); box-shadow: 0 0 0 1px rgba(233, 185, 73, .35), 0 0 18px rgba(233, 185, 73, .3); }
.bd-info.bd-win { border-color: var(--gold2); border-left-color: var(--bd-pc); box-shadow: 0 0 0 2px rgba(255, 217, 120, .5), 0 0 26px rgba(233, 185, 73, .45); }
.bd-who { grid-area: who; display: flex; align-items: baseline; gap: 8px; min-width: 0; }
.bd-info .bd-name { font-size: 16px; font-weight: 700; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bd-wins { flex: none; font-size: 11.5px; color: var(--muted); border: 1px solid var(--line); border-radius: 99px; padding: 0 7px; }
.bd-counts { grid-area: counts; display: flex; flex-wrap: wrap; align-items: center; gap: 2px 12px; font-size: 12.5px; color: var(--muted); }
.bd-hand { display: inline-flex; align-items: center; gap: 6px; }
.bd-backs { display: inline-flex; gap: 2px; }
.bd-mini { display: inline-block; width: 9px; height: 13px; border-radius: 2px; border: 1px solid #8a76d0;
           background: repeating-linear-gradient(45deg, #3a2a6c 0 2px, #281d4e 2px 4px); }
.bd-lp { grid-area: lp; text-align: right; line-height: 1; }
.bd-lp small { display: block; font-size: 10px; color: var(--muted); letter-spacing: 1px; margin-bottom: 2px; }
.bd-lp b { display: inline-block; font-size: clamp(22px, 6.5vw, 30px); font-weight: 800; color: var(--gold2); font-variant-numeric: tabular-nums; transform-origin: right center; }
.bd-zero b { color: var(--red); }
.bd-hit b { animation: bd-hit .9s ease-out; }
.bd-heal b { animation: bd-heal .9s ease-out; }
.bd-delta { margin-left: 6px; font-weight: 700; letter-spacing: 0; animation: bd-fade 2.6s ease-in forwards; }
.bd-hit .bd-delta { color: var(--red); }
.bd-heal .bd-delta { color: var(--green); }
@keyframes bd-hit { from { color: var(--red); transform: scale(1.15); } }
@keyframes bd-heal { from { color: var(--green); transform: scale(1.1); } }
@keyframes bd-fade { 0%, 70% { opacity: 1; } 100% { opacity: 0; } }

/* rows of five zones; each zone is a size container, so a tile's text follows its own width */
.bd-row { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: clamp(4px, 1.4vw, 10px); width: 100%; max-width: 540px; margin: 0 auto; }
.bd-zone {
  position: relative; aspect-ratio: 5 / 7; min-width: 0; container-type: inline-size; border-radius: 6px;
  border: 1px dashed rgba(169, 159, 208, .2); border-color: color-mix(in srgb, var(--bd-pc) 28%, transparent);
}
.bd-empty::after {
  content: "Quái thú"; position: absolute; inset: 0; display: grid; place-items: center; padding: 2px; text-align: center;
  font-size: 8px; font-size: clamp(7px, 10cqw, 10px); line-height: 1.2; color: rgba(169, 159, 208, .3);
}
.bd-mag .bd-empty::after { content: "Phép · Bẫy"; }
.bd-tile {
  position: absolute; inset: -1px; display: flex; flex-direction: column; gap: 1px; overflow: hidden;
  padding: 3px 4px; padding: 6cqw 7cqw; border-radius: 6px; line-height: 1.15;
  border: 1px solid var(--bd-kc, #6d5bb0); background: var(--bd-kbg, #2a1f50); color: var(--bd-kt, var(--text));
  font-size: 8px; font-size: clamp(8px, 12cqw, 12.5px); box-shadow: 0 2px 6px rgba(0, 0, 0, .35);
}
.bd-tile > * { flex: none; min-width: 0; }
/* defense: the card lies sideways in its zone, with THỦ above it */
.bd-tile.bd-def { inset: auto -1px; top: 50%; transform: translateY(-50%); aspect-ratio: 7 / 5; }
.bd-tag {
  position: absolute; top: 2px; left: 50%; transform: translateX(-50%); z-index: 1; padding: 0 5px; border-radius: 99px;
  font-size: 8px; font-size: clamp(7px, 10cqw, 11px); font-weight: 800; line-height: 1.45; letter-spacing: .5px;
  color: #0f0b1e; background: var(--blue);
}
.bd-tile .bd-name {
  font-weight: 600; overflow: hidden; overflow-wrap: anywhere;
  display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2;
}
.bd-meta { font-size: .78em; opacity: .85; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bd-lv { color: var(--gold2); opacity: 1; }
.bd-stars { display: none; font-size: .85em; letter-spacing: -1px; }
.bd-stats { margin-top: auto; white-space: nowrap; overflow: hidden; font-size: 1.3em; font-weight: 800; line-height: 1.05; font-variant-numeric: tabular-nums; }
.bd-stats b { color: var(--gold2); }
.bd-stats span { font-size: .72em; font-weight: 600; opacity: .7; }
.bd-def .bd-name { -webkit-line-clamp: 1; line-clamp: 1; }
@container (min-width: 90px) {
  .bd-stars { display: inline; }
  .bd-lvn { display: none; }
}
@container (max-width: 89px) {
  .bd-def .bd-meta { display: none; }
  .bd-def .bd-name { -webkit-line-clamp: 2; line-clamp: 2; }
}
.bd-spell { text-align: center; }
.bd-spell .bd-name { margin: auto 0; -webkit-line-clamp: 4; line-clamp: 4; }
.bd-kind { font-size: .78em; font-weight: 800; text-transform: uppercase; letter-spacing: .3px; color: var(--bd-kl); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
/* the card colors (monster tan, magic green, trap purple, as on the real cards) */
.bd-k-monster { --bd-kc: #b48a3c; --bd-kbg: linear-gradient(165deg, #4a3616, #261b0b); --bd-kt: #f6ead0; --bd-kl: #f0c873; }
.bd-k-magic { --bd-kc: #33b98d; --bd-kbg: linear-gradient(165deg, #14503d, #0a2a21); --bd-kt: #e0f7ee; --bd-kl: #7fe3bf; }
.bd-k-equip { --bd-kc: #86c04a; --bd-kbg: linear-gradient(165deg, #2f4e15, #17280b); --bd-kt: #eaf6dc; --bd-kl: #b8e47e; }
.bd-k-ritual { --bd-kc: #4fa0e0; --bd-kbg: linear-gradient(165deg, #173c5e, #0c2034); --bd-kt: #e2f0fc; --bd-kl: #8cc8ff; }
.bd-k-trap { --bd-kc: #c860b4; --bd-kbg: linear-gradient(165deg, #4d1a49, #290e29); --bd-kt: #fae3f5; --bd-kl: #f29ae0; }
.bd-back {
  border: 1px solid #7a68c0;
  background: radial-gradient(ellipse 30% 22% at 50% 50%, #1b1338 0 70%, var(--gold) 74% 84%, transparent 88%),
              repeating-linear-gradient(45deg, #3a2a6c 0 3px, #281d4e 3px 6px);
}
.bd-tile.bd-back { align-items: center; justify-content: center; }
.bd-up { font-size: .85em; font-weight: 700; color: var(--gold2); }

/* the middle strip: duel, turn, field; the winner's banner once it is over */
.bd-mid {
  display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 4px 10px; padding: 5px 8px; margin: 2px 0;
  text-align: center; font-size: 13.5px; color: var(--muted);
  border-top: 1px solid rgba(233, 185, 73, .25); border-bottom: 1px solid rgba(233, 185, 73, .25);
  background: linear-gradient(90deg, transparent, rgba(233, 185, 73, .08), transparent);
}
.bd-mid > * { min-width: 0; overflow-wrap: anywhere; }
.bd-field { font-size: 12px; color: var(--gold2); border: 1px solid rgba(233, 185, 73, .4); border-radius: 99px; padding: 0 8px; white-space: nowrap; }
.bd-mid.bd-won { padding: 8px; background: linear-gradient(90deg, transparent, rgba(233, 185, 73, .2), transparent); border-color: rgba(233, 185, 73, .6); }
.bd-banner { flex: 0 1 100%; min-width: 0; overflow-wrap: anywhere; font-size: clamp(19px, 5.5vw, 26px); font-weight: 800; line-height: 1.2; color: var(--gold2); text-shadow: 0 0 14px rgba(233, 185, 73, .5); }

/* the cards used: below the mat on phones (a column per player), beside it on wider screens */
.bd-used { min-width: 0; padding: 10px 12px; border: 1px solid var(--line); border-radius: var(--radius); background: linear-gradient(180deg, var(--card), var(--bg2)); }
.bd-used h3 { margin: 0 0 8px; font-size: 14px; color: var(--gold2); }
.bd-groups { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.bd-group { min-width: 0; }
.bd-group h4 { margin: 0 0 5px; padding-left: 6px; border-left: 3px solid var(--bd-pc); font-size: 12.5px; color: var(--bd-pt); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bd-group h4 span { color: var(--muted); font-weight: 500; }
.bd-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 3px; }
.bd-u { display: flex; align-items: baseline; gap: 6px; min-width: 0; padding: 2px 6px; font-size: 12.5px; line-height: 1.3;
        border-left: 3px solid var(--bd-kc); border-radius: 0 6px 6px 0; background: rgba(255, 255, 255, .035); }
.bd-un { flex: 1; min-width: 0; overflow: hidden; overflow-wrap: anywhere; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2; }
.bd-us { flex: none; color: var(--muted); font-size: 11.5px; font-variant-numeric: tabular-nums; }
.bd-none { margin: 0; color: var(--muted); font-size: 12.5px; font-style: italic; }

/* no duel running */
.bd-wait {
  display: grid; justify-items: center; gap: 10px; max-width: 920px; margin: 0 auto; padding: 40px 16px; text-align: center;
  color: var(--muted); border: 1px dashed var(--line); border-radius: var(--radius); background: rgba(21, 15, 44, .6);
}
.bd-wait p { margin: 0; }
.bd-wait-card { width: 56px; aspect-ratio: 5 / 7; border-radius: 6px; box-shadow: 0 0 18px rgba(233, 185, 73, .18); animation: bd-breathe 3.2s ease-in-out infinite; }
.bd-score { font-size: 13px; overflow-wrap: anywhere; }
@keyframes bd-breathe { 50% { opacity: .55; transform: translateY(-3px); } }

@media (min-width: 760px) {
  .bd { grid-template-columns: minmax(0, 1fr) 250px; align-items: start; }
  .bd-mat { gap: 8px; padding: 12px; }
  .bd-info { padding: 7px 12px; }
  .bd-info .bd-name { font-size: 17px; }
  .bd-groups { grid-template-columns: 1fr; }
  .bd-list { max-height: 300px; overflow-y: auto; }
}
@media (prefers-reduced-motion: reduce) {
  .bd-hit b, .bd-heal b, .bd-wait-card { animation: none; }
}
