:root {
  --bg: #FBF8F3;
  --paper: #FFFDF8;
  --lav: #D9CCF3;
  --lav-2: #EEE8FB;
  --lav-3: #F6F2FD;
  --lav-deep: #9D8BD8;
  --cream: #FBE9A8;
  --cream-2: #FDF3CF;
  --pink: #F7C6D4;
  --mint: #C9EBD5;
  --sky: #BFE0F2;
  --ink: #5B4B7A;
  --ink-2: #857AA0;
  --ink-3: #B3AAC6;
  --line: 2px;
  --r-lg: 26px;
  --r-md: 18px;
  --r-sm: 12px;
  --lift: 0 4px 0 var(--lav-2);
  --font: "Nunito", "PingFang TC", "Microsoft JhengHei", "Noto Sans TC", system-ui, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.55;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { margin: 0; font-weight: 800; letter-spacing: .02em; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---------- 漂浮裝飾 ---------- */
.floaters { position: fixed; inset: 0; pointer-events: none; z-index: 0; overflow: hidden; }
.floater { position: absolute; animation: float 14s ease-in-out infinite; }
@keyframes float { 0%, 100% { transform: translate(0, 0) rotate(0); } 50% { transform: translate(6px, -14px) rotate(8deg); } }
.logo-dot1 { animation: bob 4s ease-in-out infinite; transform-origin: center; }
.logo-dot2 { animation: bob 4s ease-in-out infinite -2s; }
@keyframes bob { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-5px); } }
@media (prefers-reduced-motion: reduce) { .floater, .logo-dot1, .logo-dot2, .card.pop { animation: none !important; } }

#app { position: relative; z-index: 1; min-height: 100%; }

/* ---------- 基本元件 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 0 22px;
  border: var(--line) solid var(--ink); border-radius: 999px;
  background: var(--paper); font-weight: 800;
  box-shadow: 0 3px 0 var(--ink);
  transition: transform .12s, box-shadow .12s, background .12s;
}
.btn:active { transform: translateY(3px); box-shadow: 0 0 0 var(--ink); }
.btn.primary { background: var(--lav); }
.btn.cream { background: var(--cream); }
.btn.ghost { box-shadow: none; border-color: var(--ink-3); background: transparent; }
.btn.small { min-height: 36px; padding: 0 14px; font-size: 14px; box-shadow: 0 2px 0 var(--ink); }
.btn.icon-only { width: 40px; min-height: 40px; padding: 0; }
.btn[disabled], .btn[aria-disabled="true"] { opacity: .45; cursor: default; box-shadow: none; transform: none; }
.btn:focus-visible, .card:focus-visible, .chip:focus-visible, input:focus-visible { outline: 3px solid var(--lav-deep); outline-offset: 2px; }

.panel {
  background: var(--paper); border: var(--line) solid var(--ink); border-radius: var(--r-lg);
  padding: 20px; box-shadow: var(--lift);
}
.soft { background: var(--lav-3); border-radius: var(--r-md); padding: 14px; }
.muted { color: var(--ink-2); }
.tiny { font-size: 12px; }
.row { display: flex; align-items: center; gap: 10px; }
.wrap { flex-wrap: wrap; }
.grow { flex: 1; }
.center { text-align: center; }
.stack > * + * { margin-top: 14px; }

.input {
  width: 100%; height: 48px; padding: 0 16px;
  border: var(--line) solid var(--ink); border-radius: var(--r-md); background: var(--paper);
  outline: none;
}
.input.code { text-transform: uppercase; letter-spacing: .3em; font-weight: 800; text-align: center; }

.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 12px; border-radius: 999px; border: var(--line) solid var(--ink);
  background: var(--paper); font-size: 13px; font-weight: 700; white-space: nowrap;
}
.chip.lav { background: var(--lav); }
.chip.cream { background: var(--cream); }
.chip.mint { background: var(--mint); }
.chip.pink { background: var(--pink); }
.chip.line { border-color: var(--ink-3); }
button.chip { cursor: pointer; }

.toggle { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; }
.switch { position: relative; width: 50px; height: 30px; flex: none; border: var(--line) solid var(--ink); border-radius: 999px; background: var(--lav-3); padding: 0; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: var(--paper); border: var(--line) solid var(--ink); transition: left .15s; }
.switch[aria-checked="true"] { background: var(--lav); }
.switch[aria-checked="true"]::after { left: 23px; }
.seg { display: inline-flex; border: var(--line) solid var(--ink); border-radius: 999px; overflow: hidden; }
.seg button { border: 0; background: var(--paper); padding: 6px 14px; font-weight: 700; font-size: 14px; }
.seg button + button { border-left: var(--line) solid var(--ink); }
.seg button[aria-pressed="true"] { background: var(--lav); }

/* ---------- 首頁 ---------- */
.home { max-width: 460px; margin: 0 auto; padding: 36px 16px 48px; }
.hero { text-align: center; margin-bottom: 22px; }
.hero .sub { font-size: 22px; font-weight: 800; margin-top: 4px; letter-spacing: .3em; }
.hero .tag { color: var(--ink-2); font-size: 13px; margin-top: 4px; }
.avatars { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.avatars button { border: var(--line) solid transparent; background: transparent; border-radius: var(--r-md); padding: 4px; display: grid; place-items: center; }
.avatars button[aria-pressed="true"] { border-color: var(--ink); background: var(--lav-2); }
.or { display: flex; align-items: center; gap: 12px; color: var(--ink-3); font-size: 13px; }
.or::before, .or::after { content: ""; flex: 1; height: var(--line); background: var(--lav-2); border-radius: 2px; }
.error { color: #B04A6B; font-size: 14px; min-height: 1.4em; }

/* ---------- 大廳 ---------- */
.lobby { max-width: 980px; margin: 0 auto; padding: 24px 16px 48px; display: grid; gap: 18px; grid-template-columns: 1fr; }
@media (min-width: 860px) { .lobby { grid-template-columns: 1.1fr 1fr; align-items: start; } }
.roomcode { font-size: 44px; font-weight: 900; letter-spacing: .25em; line-height: 1; }
.qr { width: 150px; height: 150px; border-radius: var(--r-md); border: var(--line) solid var(--ink); overflow: hidden; background: var(--paper); flex: none; }
.qr svg { width: 100%; height: 100%; display: block; }
.url { font-size: 13px; word-break: break-all; color: var(--ink-2); }
.plist { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.plist li { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-radius: var(--r-md); background: var(--lav-3); }
.plist .name { font-weight: 800; flex: 1; }
.plist li.off { opacity: .55; }
.settings .toggle + .toggle { border-top: var(--line) solid var(--lav-2); }
.settings h3 { font-size: 15px; margin-top: 6px; }

/* ---------- 遊戲 ---------- */
.game { display: grid; grid-template-rows: auto auto 1fr auto; height: 100dvh; max-width: 1180px; margin: 0 auto; }
.topbar { display: flex; align-items: center; gap: 8px; padding: 10px 12px 6px; }
.topbar .logo { flex: none; }
.topbar .scroll { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; flex: 1; }
.topbar .scroll::-webkit-scrollbar { display: none; }
.keycard { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px 3px 12px; border-radius: 999px; background: var(--paper); border: var(--line) solid var(--ink); font-weight: 800; font-size: 13px; white-space: nowrap; }
.keycard .safes { display: inline-flex; gap: 1px; }

.players { display: flex; gap: 8px; overflow-x: auto; padding: 4px 12px 8px; scrollbar-width: none; }
.players::-webkit-scrollbar { display: none; }
.pl { flex: none; display: flex; align-items: center; gap: 6px; padding: 4px 12px 4px 4px; border-radius: 999px; background: var(--paper); border: var(--line) solid var(--lav-2); position: relative; }
.pl.turn { border-color: var(--ink); background: var(--cream-2); box-shadow: 0 3px 0 var(--ink); }
.pl.me .pname::after { content: "（你）"; color: var(--ink-2); font-weight: 600; font-size: 11px; }
.pl .pname { font-weight: 800; font-size: 13px; max-width: 7.5em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pl .count { font-weight: 900; font-size: 13px; display: inline-flex; align-items: center; gap: 2px; }
.pl.off { opacity: .5; }
.pl .badges { display: inline-flex; gap: 2px; }
.dir { flex: none; align-self: center; color: var(--ink-2); }

.main { overflow-y: auto; padding: 4px 12px 12px; display: grid; gap: 12px; align-content: start; }
@media (min-width: 900px) {
  .main { grid-template-columns: 1fr 300px; }
  .main .side { grid-column: 2; grid-row: 1 / span 3; }
}
.board { padding: 16px; }
.board-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.board-head h2 { font-size: 17px; }
.beats-big { font-size: 26px; font-weight: 900; }
.measure {
  margin-top: 12px; display: grid; grid-template-columns: repeat(8, 1fr); gap: 0;
  height: 86px; border: var(--line) solid var(--ink); border-radius: var(--r-md); overflow: hidden; background: var(--lav-3);
  position: relative;
}
.measure .slot { border-right: 1px dashed transparent; }
.measure .beatline { position: absolute; top: 8px; bottom: 8px; width: var(--line); background: var(--lav-2); border-radius: 2px; }
.mcell {
  position: relative; z-index: 1; margin: 6px 3px; border-radius: var(--r-sm); border: var(--line) solid var(--ink);
  background: var(--paper); display: flex; align-items: center; justify-content: center; gap: 2px; overflow: hidden;
}
.mcell.rest { background: var(--lav-2); }
.mcell.outside { background: var(--cream-2); }
.mcell.new { animation: pop .45s cubic-bezier(.3, 1.6, .5, 1); }
@keyframes pop { 0% { transform: scale(.6); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }
.mcell .mark { position: absolute; top: 2px; right: 4px; font-size: 10px; font-weight: 900; }
.fx { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }

.track { padding: 14px 16px; }
.track h3 { font-size: 14px; margin-bottom: 8px; }
.trackrow { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 6px; }
.tmeasure { flex: none; display: grid; grid-template-columns: repeat(8, 12px); height: 34px; gap: 0; border: var(--line) solid var(--lav); border-radius: 10px; padding: 3px; background: var(--paper); }
.tmeasure.loop { border-color: var(--lav-deep); background: var(--lav-3); }
.tmeasure.playing { border-color: var(--ink); background: var(--cream-2); }
.tcell { border-radius: 6px; margin: 0 1px; display: grid; place-items: center; overflow: hidden; }
.tcell svg { width: 20px; height: 20px; }
.tcell.rest { background: var(--lav-2); }
.tcell.hit { animation: hit .35s ease-out; }
@keyframes hit { 50% { transform: translateY(-5px) scale(1.15); } }

.log { padding: 12px 16px; font-size: 13px; max-height: 260px; overflow-y: auto; }
.log h3 { font-size: 14px; margin-bottom: 6px; }
.log ol { list-style: none; padding: 0; margin: 0; display: grid; gap: 4px; }
.log li { color: var(--ink-2); }
.log li:last-child { color: var(--ink); font-weight: 700; }

.handarea { border-top: var(--line) solid var(--lav-2); background: var(--paper); padding: 10px 12px calc(12px + env(safe-area-inset-bottom)); border-radius: var(--r-lg) var(--r-lg) 0 0; }
.status { display: flex; align-items: center; gap: 8px; min-height: 46px; flex-wrap: wrap; }
.status .msg { flex: 1; font-weight: 800; font-size: 14px; min-width: 150px; }
.status .msg small { display: block; font-weight: 600; color: var(--ink-2); font-size: 12px; }
.hand { display: flex; gap: 8px; overflow-x: auto; padding: 14px 4px 12px; scroll-snap-type: x proximity; }
@media (min-width: 900px) { .hand { flex-wrap: wrap; overflow: visible; } }

/* ---------- 卡牌 ---------- */
.card {
  flex: none; position: relative; width: 80px; height: 114px; padding: 0;
  border: var(--line) solid var(--ink); border-radius: 16px; background: var(--paper);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  box-shadow: 0 3px 0 var(--lav); transition: transform .15s, box-shadow .15s;
  scroll-snap-align: start;
}
.card:hover { transform: translateY(-3px); }
.card.sel { transform: translateY(-10px); box-shadow: 0 6px 0 var(--ink); background: var(--cream-2); }
.card.dim { opacity: .38; filter: saturate(.6); }
.card.dim:hover { transform: none; }
.card.t-rest { background: var(--lav-3); }
.card.t-mod { background: var(--lav-2); }
.card.t-special { background: var(--cream-2); }
.card.t-rhythm { background: #EAF6EF; }
.card.t-magic { background: linear-gradient(var(--paper), var(--paper)) padding-box; border-color: var(--lav-deep); box-shadow: 0 3px 0 var(--lav-deep); }
.card.sel.t-magic { background: var(--cream-2); }
.card-reg { position: absolute; top: 5px; left: 7px; line-height: 0; }
.card-magic { position: absolute; top: 5px; right: 6px; line-height: 0; }
.card-art { display: grid; place-items: center; min-height: 54px; }
.card-name { font-size: 11px; font-weight: 800; line-height: 1.1; text-align: center; padding: 0 4px; margin-top: -4px; }
.card-beats { line-height: 0; min-height: 12px; }
.zero-beat { font-size: 10px; font-weight: 800; color: var(--ink-2); line-height: 12px; }
.card .tag { position: absolute; bottom: -10px; left: 50%; transform: translateX(-50%); font-size: 10px; font-weight: 900; padding: 1px 8px; border-radius: 999px; border: var(--line) solid var(--ink); white-space: nowrap; }
.tag.safe { background: var(--lav); }
.tag.outside { background: var(--cream); }
.tag.none { background: var(--paper); color: var(--ink-2); }
.mod-key { font-size: 28px; font-weight: 900; line-height: 1; }
.mod-sub { font-size: 11px; font-weight: 800; color: var(--ink-2); }
.trip { display: flex; gap: 0; }
.dyad { display: grid; }
.dyad svg { grid-area: 1 / 1; }
.dyad svg:first-child { transform: translate(8px, -8px); }
.dyad svg:last-child { transform: translate(-8px, 8px); }
.dotted { display: flex; align-items: center; }
.dotted .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ink); margin-left: 2px; }
.card.small { width: 62px; height: 88px; border-radius: 13px; gap: 3px; }
.card.small .card-art svg { transform: scale(.8); }
.card.pop { animation: pop .4s cubic-bezier(.3, 1.6, .5, 1); }

/* ---------- 對話框 ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(91, 75, 122, .28); z-index: 50; display: grid; place-items: end center; padding: 0; }
@media (min-width: 640px) { .modal-bg { place-items: center; padding: 16px; } }
.modal { width: 100%; max-width: 520px; max-height: 88dvh; overflow-y: auto; background: var(--paper); border: var(--line) solid var(--ink); border-radius: var(--r-lg) var(--r-lg) 0 0; padding: 22px 18px calc(18px + env(safe-area-inset-bottom)); }
@media (min-width: 640px) { .modal { border-radius: var(--r-lg); box-shadow: 0 6px 0 var(--ink); } }
.modal h2 { font-size: 18px; margin-bottom: 4px; }
.choices { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.choice { border: var(--line) solid var(--ink); background: var(--paper); border-radius: var(--r-md); padding: 8px 12px; font-weight: 800; display: inline-flex; align-items: center; gap: 6px; min-height: 44px; }
.choice[aria-pressed="true"] { background: var(--lav); box-shadow: 0 3px 0 var(--ink); }
.choice.key { flex-direction: column; min-width: 64px; gap: 2px; }
.choice .safes { display: flex; gap: 0; }
.modal .actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 18px; flex-wrap: wrap; }
.pgrid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; margin-top: 10px; }
.pgrid button { border: var(--line) solid var(--lav-2); background: var(--paper); border-radius: var(--r-sm); padding: 4px 0; display: grid; place-items: center; font-size: 11px; font-weight: 800; }
.pgrid button[aria-pressed="true"] { border-color: var(--ink); background: var(--cream-2); }
.pgrid button.safe { background: var(--lav-3); }

.toast { position: fixed; left: 50%; top: 14px; transform: translateX(-50%); z-index: 60; background: var(--paper); border: var(--line) solid var(--ink); border-radius: 999px; padding: 8px 18px; font-weight: 800; box-shadow: 0 3px 0 var(--ink); max-width: calc(100% - 32px); }

/* ---------- 終局 ---------- */
.end { max-width: 1000px; margin: 0 auto; padding: 24px 16px 60px; }
.winner { text-align: center; position: relative; }
.winner .avatar { width: 96px; height: 96px; }
.winner .title-in { font-size: 22px; font-weight: 900; text-align: center; border: 0; border-bottom: var(--line) dashed var(--lav); background: transparent; width: min(100%, 420px); padding: 4px; }
.winner .title-show { font-size: 24px; font-weight: 900; }
.sparkles { position: absolute; inset: -10px 0 auto; height: 140px; pointer-events: none; }
.sparkles svg { position: absolute; animation: float 6s ease-in-out infinite; }
.bigplay { width: 96px; height: 96px; border-radius: 50%; border: var(--line) solid var(--ink); background: var(--cream); box-shadow: 0 5px 0 var(--ink); display: grid; place-items: center; margin: 18px auto 6px; }
.bigplay:active { transform: translateY(4px); box-shadow: 0 1px 0 var(--ink); }
.song { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; margin-top: 18px; }
.song .tmeasure { grid-template-columns: repeat(8, 16px); height: 44px; }
.song .tcell svg { width: 26px; height: 26px; }
.song .tmeasure .mnum { display: none; }
.ranks { display: grid; gap: 8px; }

/* ---------- 桌面觀戰 ---------- */
.table-view .handarea { display: none; }
.table-view .game { grid-template-rows: auto auto 1fr; }
@media (min-width: 900px) { .table-view .measure { height: 130px; } .table-view .main { grid-template-columns: 1fr 340px; } }
a.btn, a.chip { text-decoration: none; color: inherit; }
.seg button { white-space: nowrap; }
.toggle > div:first-child { min-width: 0; }
.game { grid-template-columns: minmax(0, 1fr); }
.main { grid-template-columns: minmax(0, 1fr); }
.main > *, .handarea, .topbar, .players { min-width: 0; }
@media (min-width: 900px) { .main { grid-template-columns: minmax(0, 1fr) 300px; } }
.tcell svg { max-width: 100%; height: auto; }
.hand { padding-bottom: 20px; }

/* 五線譜＋迷你鍵盤卡面 */
.card { height: 122px; }
.card.t-note .card-art, .card.t-weird .card-art { display: flex; flex-direction: column; align-items: center; gap: 2px; margin-top: 8px; }
.card-shape { position: absolute; top: 4px; right: 5px; display: flex; line-height: 0; }
.card .staff, .card .keys { display: block; }
.card.small { height: 96px; }
.card.small .card-art svg.staff { transform: none; width: 58px; height: 39px; }
.card.small .card-art svg.keys { transform: none; width: 42px; height: 13px; }
.card.t-note .card-beats, .card.t-weird .card-beats { margin-top: -2px; }

/* 手機：手牌換行排列，一眼看到全部 */
@media (max-width: 640px) {
  .hand { flex-wrap: wrap; justify-content: center; overflow-x: visible; overflow-y: auto; max-height: 42dvh; gap: 12px 6px; padding: 12px 0 14px; scroll-snap-type: none; }
  .hand .card { width: 64px; height: 100px; border-radius: 13px; gap: 3px; }
  .hand .card .card-art svg.staff { width: 58px; height: 39px; }
  .hand .card .card-art svg.keys { width: 42px; height: 13px; }
  .hand .card .card-art svg.icon { width: 38px; height: 38px; }
  .hand .card .card-art { min-height: 40px; }
  .hand .card.t-note .card-art, .hand .card.t-weird .card-art { margin-top: 6px; }
  .hand .card .card-name { font-size: 10px; }
  .hand .card .mod-key { font-size: 22px; }
  .hand .card.sel { transform: translateY(-4px); }
  .hand .card .tag { bottom: -9px; font-size: 9px; padding: 0 6px; }
  .status .btn { min-height: 40px; padding: 0 16px; }
  .handarea { padding: 8px 10px calc(10px + env(safe-area-inset-bottom)); }
  .board { padding: 14px; }
  .measure { height: 70px; }
}
@media (max-width: 640px) {
  .hand .card { height: 104px; }
  .hand .card.k-dotted .card-name, .hand .card.k-longdotted .card-name, .hand .card.k-triplet .card-name, .hand .card.k-dyad .card-name { display: none; }
}

/* 1/16 拍精度：一個小節 64 格 */
.measure { grid-template-columns: repeat(64, 1fr); }
.mcell { margin: 6px 1px; min-width: 0; }
.tmeasure { grid-template-columns: repeat(64, 1.5px); }
.song .tmeasure { grid-template-columns: repeat(64, 2px); }
.tcell { margin: 0; min-width: 0; }
.frac-beat { font-size: 11px; font-weight: 900; line-height: 12px; background: var(--cream-2); border-radius: 999px; padding: 0 6px; white-space: nowrap; }
@media (max-width: 640px) {
  .hand .card.t-weird:not(.k-wild):not(.k-drum) .card-name { display: none; }
  .hand .frac-beat { font-size: 10px; padding: 0 4px; }
}
