:root {
  --bg: #140c26;
  --bg2: #22163d;
  --panel: #2a1d4a;
  --panel2: #362659;
  --line: #4b3a75;
  --text: #f4ecff;
  --muted: #b5a6d6;
  --gold: #ffcf4a;
  --red: #ff5a6e;
  --blue: #5fb4ff;
  --violet: #b98cff;
  --green: #52e08a;
  --radius: 14px;
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}
* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  background: radial-gradient(ellipse at 50% -10%, #3b2466 0%, var(--bg) 60%) fixed, var(--bg);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 13px; }
.center { text-align: center; }
.err { color: var(--red); min-height: 1.2em; margin: 6px 0 0; font-size: 14px; }

.screen { flex: 1; display: flex; align-items: center; justify-content: center; padding: 16px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; width: 100%; max-width: 420px; box-shadow: 0 12px 40px #0008; }

.btn {
  font: inherit; font-weight: 700; color: var(--text); background: var(--panel2); border: 1px solid var(--line);
  border-radius: 12px; padding: 12px 16px; cursor: pointer; min-height: 48px; touch-action: manipulation;
}
.btn:hover:not(:disabled) { background: #43306c; }
.btn:disabled { opacity: .45; cursor: default; }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: 14px; }
.btn.big { width: 100%; font-size: 20px; min-height: 58px; margin-top: 10px; }
.btn.primary { background: linear-gradient(180deg, #ff6f80, #d93a55); border-color: #ff8d9b; box-shadow: 0 4px 0 #8f1f35; }
.btn.primary:hover:not(:disabled) { background: linear-gradient(180deg, #ff8090, #e44862); }
.btn.primary:active:not(:disabled) { transform: translateY(2px); box-shadow: 0 2px 0 #8f1f35; }

/* landing */
.landing-card { text-align: center; }
#hero { width: 100%; max-width: 320px; height: auto; display: block; margin: 0 auto -6px; }
.title { font-size: 52px; margin: 0; letter-spacing: -1px; color: var(--gold); text-shadow: 0 4px 0 #8a4b12, 0 0 30px #ffb44a55; }
.tagline { color: var(--muted); margin: 6px 0 16px; line-height: 1.4; }
.field { display: block; text-align: left; font-size: 14px; color: var(--muted); margin: 12px 0; }
.field input, #code {
  display: block; width: 100%; margin-top: 6px; font: inherit; font-size: 18px; color: var(--text); background: var(--bg2);
  border: 1px solid var(--line); border-radius: 10px; padding: 12px;
}
.row { display: flex; gap: 8px; margin-top: 10px; }
.row .btn { flex: 1; }
#code { width: 90px; flex: 0 0 90px; margin: 0; text-align: center; text-transform: uppercase; letter-spacing: 3px; }
.swatches { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.swatch { width: 40px; height: 40px; border-radius: 50%; border: 3px solid transparent; cursor: pointer; padding: 0; }
.swatch.on { border-color: #fff; box-shadow: 0 0 0 3px #0006; }
.swatch.taken { opacity: .25; cursor: not-allowed; }
.swatches.small .swatch { width: 32px; height: 32px; }
.invite { background: var(--bg2); border-radius: 10px; padding: 10px; margin: 10px 0; color: var(--gold); }

/* lobby */
.lobby-head { display: flex; justify-content: space-between; align-items: center; }
.code-big { font-size: 34px; font-weight: 800; letter-spacing: 6px; color: var(--gold); }
.lobby-players { list-style: none; padding: 0; margin: 14px 0; }
.lobby-players li { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 10px; background: var(--bg2); margin-bottom: 6px; }
.lobby-players .dot { width: 22px; height: 22px; border-radius: 50%; flex: none; }
.lobby-players .tag { margin-left: auto; font-size: 12px; color: var(--muted); }
.lobby-players .rm { margin-left: 6px; min-height: 30px; padding: 2px 10px; }
.settings { display: flex; gap: 16px; margin: 12px 0 4px; flex-wrap: wrap; }
.settings label { color: var(--muted); font-size: 14px; display: flex; align-items: center; gap: 8px; }
.settings select { font: inherit; color: var(--text); background: var(--bg2); border: 1px solid var(--line); border-radius: 8px; padding: 8px; }
#addbot { width: 100%; margin-top: 10px; }
.howto { margin-top: 14px; color: var(--muted); font-size: 14px; }
.howto summary { cursor: pointer; color: var(--text); }
.howto li { margin: 6px 0; }

/* match */
#game { flex-direction: column; align-items: stretch; justify-content: flex-start; padding: 8px 12px 0; gap: 8px; }
.topbar { display: flex; align-items: center; gap: 12px; min-height: 36px; }
.round { font-weight: 800; color: var(--gold); white-space: nowrap; }
.turn-info { flex: 1; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.timer { font-variant-numeric: tabular-nums; font-weight: 800; min-width: 44px; text-align: right; }
.timer.low { color: var(--red); animation: pulse .5s infinite alternate; }
.scores { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.scores::-webkit-scrollbar { display: none; }
.chip {
  flex: 1 0 auto; min-width: 104px; display: flex; flex-direction: column; gap: 2px; padding: 6px 10px; border-radius: 12px;
  background: var(--panel); border: 2px solid transparent; position: relative; font-size: 13px;
}
.chip .nm { font-weight: 800; display: flex; align-items: center; gap: 6px; white-space: nowrap; max-width: 150px; overflow: hidden; text-overflow: ellipsis; }
.chip .dot { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.chip .st { color: var(--muted); white-space: nowrap; }
.chip .st b { color: var(--text); }
.chip.now { border-color: var(--gold); background: #3b2a5e; }
.chip.gone { opacity: .45; }
.chip .badge { position: absolute; top: -7px; right: 6px; font-size: 10px; font-weight: 800; padding: 1px 6px; border-radius: 8px; }
.chip .badge.lead { background: var(--red); color: #fff; }
.chip .badge.last { background: var(--blue); color: #08203a; }
.chip.hit { animation: hit .5s; }

.main { display: grid; grid-template-columns: minmax(0, 1fr) 280px; gap: 12px; flex: 1; min-height: 0; }
.stage { display: flex; flex-direction: column; gap: 8px; min-width: 0; position: relative; }
.board-wrap { position: relative; width: 100%; aspect-ratio: 1000 / 620; max-height: calc(100dvh - 250px); margin: 0 auto; }
#board { width: 100%; height: 100%; display: block; border-radius: var(--radius); background: #1b1233; touch-action: manipulation; }
.board-wrap.shake { animation: shake .45s; }
.callouts { position: absolute; inset: 0; pointer-events: none; display: flex; flex-direction: column; align-items: center; justify-content: flex-start; padding-top: 10%; gap: 6px; }
.callout {
  font-weight: 900; font-size: clamp(16px, 3.2vw, 30px); text-align: center; padding: 8px 16px; border-radius: 14px; max-width: 92%;
  background: #120a22e6; border: 2px solid var(--violet); color: #fff; animation: pop 2.6s forwards; text-shadow: 0 2px 0 #0008;
}
.callout.red { border-color: var(--red); color: #ffd5da; }
.callout.gold { border-color: var(--gold); color: #fff1c4; }
.callout.blue { border-color: var(--blue); color: #d8ecff; }
.callout.big { font-size: clamp(20px, 4.4vw, 40px); animation: popbig 2.8s forwards; }
.pick-banner { position: absolute; left: 50%; bottom: 10px; transform: translateX(-50%); display: flex; gap: 10px; align-items: center; background: #120a22ee; padding: 6px 8px 6px 14px; border-radius: 12px; border: 2px solid var(--violet); font-weight: 700; white-space: nowrap; }

.actions { display: flex; gap: 8px; align-items: stretch; }
.hand { display: flex; gap: 8px; flex: 1; min-width: 0; }
.cardbtn {
  flex: 1 1 0; min-width: 0; min-height: 76px; border-radius: 12px; border: 2px solid var(--line); background: linear-gradient(180deg, #43306c, #2e2050);
  color: var(--text); font: inherit; cursor: pointer; padding: 6px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  touch-action: manipulation;
}
.cardbtn .ic { font-size: 24px; line-height: 1; }
.cardbtn .cn { font-weight: 800; font-size: 14px; }
.cardbtn .cd { font-size: 11px; color: var(--muted); line-height: 1.15; }
.cardbtn .key { font-size: 10px; color: var(--muted); opacity: .7; }
.cardbtn:hover:not(:disabled) { border-color: var(--violet); }
.cardbtn:disabled { opacity: .5; cursor: default; }
.cardbtn.empty { border-style: dashed; background: transparent; opacity: .35; }
.roll { min-width: 110px; font-size: 22px; background: linear-gradient(180deg, #ffd65c, #e9a520); color: #3a2300; border-color: #ffe08a; box-shadow: 0 4px 0 #8a5a00; }
.roll:hover:not(:disabled) { background: linear-gradient(180deg, #ffe07a, #f0b030); }
.roll.go { animation: pulse .6s infinite alternate; }

.mgpad {
  min-height: 92px; border-radius: 16px; display: flex; align-items: center; justify-content: center; font-size: 30px; font-weight: 900;
  background: linear-gradient(180deg, #ff6f80, #c92f4a); border: 3px solid #ff9aa7; user-select: none; -webkit-user-select: none;
  touch-action: none; cursor: pointer; box-shadow: 0 5px 0 #7a1a2c;
}
.mgpad.down { transform: translateY(3px); box-shadow: 0 2px 0 #7a1a2c; filter: brightness(1.15); }
.mgpad.guard { background: linear-gradient(180deg, #67b7ff, #2d74c9); border-color: #a8d6ff; box-shadow: 0 5px 0 #173c6b; }

.log-wrap { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); display: flex; flex-direction: column; min-height: 0; max-height: calc(100dvh - 110px); }
.log-title { padding: 10px 12px; font-weight: 800; border-bottom: 1px solid var(--line); }
.log { list-style: none; margin: 0; padding: 6px 8px; overflow-y: auto; flex: 1; font-size: 13px; }
.log li { padding: 5px 6px; border-radius: 8px; line-height: 1.3; color: var(--muted); }
.log li.stab { color: #ffd5da; background: #ff5a6e1c; }
.log li.stab::before { content: "🗡 "; }
.log li.fresh { animation: flash 1s; }
.log .r { font-size: 10px; opacity: .6; margin-right: 4px; }

/* results */
.results-card h2 { margin: 0 0 10px; color: var(--gold); font-size: 30px; text-align: center; }
.rank { list-style: none; padding: 0; margin: 0; }
.rank li { display: flex; align-items: center; gap: 10px; padding: 10px; border-radius: 12px; background: var(--bg2); margin-bottom: 6px; }
.rank li.first { background: linear-gradient(90deg, #5a4210, var(--bg2)); border: 1px solid var(--gold); }
.rank .pl { font-weight: 900; font-size: 20px; width: 26px; text-align: center; }
.rank .dot { width: 18px; height: 18px; border-radius: 50%; }
.rank .nm { font-weight: 800; flex: 1; }
.rank .sc { color: var(--muted); font-size: 14px; text-align: right; }
.awards { margin: 14px 0 4px; display: grid; gap: 8px; }
.award { background: #ff5a6e22; border: 1px solid #ff5a6e66; border-radius: 12px; padding: 10px 12px; }
.award b { color: var(--red); display: block; }

/* modal */
.modal { position: fixed; inset: 0; background: #0009; display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 20; }
.modal h3 { margin: 0 0 12px; }
.picker-list { display: grid; gap: 8px; margin-bottom: 10px; }
.picker-list .btn { display: flex; align-items: center; gap: 10px; text-align: left; min-height: 56px; }
.picker-list .dot { width: 18px; height: 18px; border-radius: 50%; flex: none; }
.picker-list .k { margin-left: auto; font-size: 12px; color: var(--muted); }
.modal .card > .btn { width: 100%; }

.toast { position: fixed; left: 50%; bottom: 40px; transform: translateX(-50%); background: #120a22f0; border: 1px solid var(--violet); padding: 10px 16px; border-radius: 12px; z-index: 30; font-weight: 700; max-width: 90vw; text-align: center; }
.announce {
  position: fixed; top: 10px; left: 50%; transform: translateX(-50%); z-index: 45; width: max-content; max-width: calc(100vw - 32px);
  background: #ffd23f; color: #2a1700; font-weight: 800; padding: 10px 18px; border-radius: 12px; box-shadow: 0 6px 24px #0008;
  text-align: center; animation: pop 10s forwards;
}
.conn { position: fixed; top: 10px; left: 50%; transform: translateX(-50%); background: var(--red); padding: 6px 14px; border-radius: 10px; font-weight: 800; z-index: 40; }
.footer { text-align: center; font-size: 12px; color: var(--muted); opacity: .7; padding: 10px; }

@keyframes pop { 0% { transform: scale(.4); opacity: 0; } 10% { transform: scale(1.08); opacity: 1; } 16% { transform: scale(1); } 85% { opacity: 1; } 100% { opacity: 0; transform: translateY(-14px); } }
@keyframes popbig { 0% { transform: scale(.3) rotate(-6deg); opacity: 0; } 8% { transform: scale(1.2) rotate(3deg); opacity: 1; } 14% { transform: scale(1) rotate(-1deg); } 18% { transform: rotate(1deg); } 22% { transform: rotate(0); } 85% { opacity: 1; } 100% { opacity: 0; transform: translateY(-18px); } }
@keyframes shake { 0%, 100% { transform: translate(0, 0); } 20% { transform: translate(-8px, 3px); } 40% { transform: translate(7px, -4px); } 60% { transform: translate(-5px, 2px); } 80% { transform: translate(4px, -1px); } }
@keyframes pulse { from { filter: brightness(1); } to { filter: brightness(1.25); } }
@keyframes flash { from { background: #ffcf4a44; } }
@keyframes hit { 0%, 100% { transform: none; } 25% { transform: translateX(-5px) rotate(-2deg); } 75% { transform: translateX(5px) rotate(2deg); } }

@media (max-width: 820px) {
  #game { padding: 6px 8px 0; gap: 6px; }
  .main { grid-template-columns: 1fr; }
  .board-wrap { max-height: none; }
  .log-wrap { max-height: 170px; }
  .chip { min-width: 92px; padding: 5px 8px; font-size: 12px; }
  .cardbtn { min-height: 72px; }
  .cardbtn .cd { display: none; }
  .roll { min-width: 84px; }
  .title { font-size: 44px; }
}
@media (prefers-reduced-motion: reduce) {
  .board-wrap.shake, .chip.hit { animation: none; }
}
