/* 單車闖陣 — 月下石庭・墨玉棋局 theme tokens */
:root {
  --ink: #16262b; --ink2: #0c171a; --stone: #2f4a4d; --paper: #e9dcc0; --wood: #d8b98a;
  --cinnabar: #c8402f; --gold: #d9a441; --bamboo: #6fae7a; --mute: #b4c6bf;
  --r: 6px; --dur: .2s; --cell: 40px;
  --sat: env(safe-area-inset-top, 0px); --sar: env(safe-area-inset-right, 0px);
  --sab: env(safe-area-inset-bottom, 0px); --sal: env(safe-area-inset-left, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; width: 100%; height: 100%; overflow: hidden; background: var(--ink2); color: var(--paper); }
body { font-size: 15px; line-height: 1.45; -webkit-user-select: none; user-select: none; }
button { font: inherit; color: inherit; cursor: pointer; }
#app { position: fixed; inset: 0; overflow: hidden; }
.screen { position: absolute; inset: 0; }
.screen[hidden] { display: none !important; }

/* ---------- 共用組件：按鈕（九宮格外殼 + 置中文字） ---------- */
.btn {
  display: flex; align-items: center; justify-content: center; text-align: center;
  min-height: 52px; min-width: 0; padding: 0 4px; background: none;
  border-style: solid; border-color: transparent; border-width: 14px 24px;
  font-size: 18px; letter-spacing: .14em; color: #fff6e2; text-shadow: 0 1px 2px rgba(0, 0, 0, .55);
  transition: transform .12s, filter .12s; outline: none;
}
.btn span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; line-height: 1.2; }
.btn.primary { border-image: url(assets/ui_btn_primary.webp) 110 170 fill / 17px 26px stretch; }
.btn.secondary { border-image: url(assets/ui_btn_secondary.webp) 120 150 fill / 15px 19px stretch; color: #e6ecdf; }
.btn:hover { filter: brightness(1.08); }
.btn:active { transform: translateY(1px) scale(.98); filter: brightness(.94); }
.btn:focus-visible, .iconbtn:focus-visible { box-shadow: 0 0 0 2px var(--gold); border-radius: var(--r); }
.btn:disabled { filter: grayscale(.7) brightness(.7); cursor: default; }

.iconbtn {
  width: 46px; height: 46px; flex: none; padding: 0; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  border: 1.5px solid rgba(217, 164, 65, .8); color: #f1e3c2; font-size: 19px; font-weight: 700; line-height: 1;
  background: radial-gradient(circle at 35% 30%, #3d5c5f, #1c3034);
  box-shadow: 0 2px 6px rgba(0, 0, 0, .5), inset 0 1px 0 rgba(255, 255, 255, .12);
  transition: transform .12s;
}
.iconbtn:active { transform: scale(.94); }
.iconbtn.off { opacity: .55; text-decoration: line-through; }

/* ---------- 標題畫面 ---------- */
#screen-title {
  display: flex; flex-direction: column; justify-content: space-between; align-items: center;
  padding: calc(16px + var(--sat)) calc(16px + var(--sar)) calc(18px + var(--sab)) calc(16px + var(--sal));
}
.title-art {
  position: absolute; inset: 0; z-index: 0; pointer-events: none;
  background: linear-gradient(180deg, rgba(8, 16, 18, .55) 0%, rgba(8, 16, 18, .05) 34%, rgba(8, 16, 18, .25) 55%, rgba(8, 16, 18, .93) 82%),
    url(assets/title_keyart_v2.webp) center / cover no-repeat, var(--ink2);
}
.title-head, .title-body { position: relative; z-index: 1; }
.title-head { margin-top: 7vh; text-align: center; }
.title-head h1 {
  margin: 0; font-size: clamp(44px, 15vw, 84px); line-height: 1.1; letter-spacing: .12em; text-indent: .12em;
  color: var(--paper); text-shadow: 0 2px 0 #05090a, 0 0 26px rgba(200, 64, 47, .65);
}
.tagline { margin: 6px 0 0; color: var(--gold); letter-spacing: .4em; text-indent: .4em; font-size: 15px; text-shadow: 0 1px 3px #000; }
.title-body { width: min(440px, 100%); display: flex; flex-direction: column; gap: 10px; }
.best-line { text-align: center; color: var(--mute); font-size: 14px; }
.best-line b { color: var(--gold); font-size: 18px; font-variant-numeric: tabular-nums; }
.stage-list { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; }
.stage-btn { min-height: 62px; border-width: 10px 8px; border-image-width: 13px 16px; letter-spacing: 0; padding: 0; }
.stage-btn span { display: flex; flex-direction: column; align-items: center; gap: 2px; overflow: visible; }
.stage-btn b { font-size: 20px; line-height: 1; color: var(--paper); }
.stage-btn em { font-style: normal; font-size: clamp(9px, 2.8vw, 12px); line-height: 1.1; color: var(--mute); white-space: nowrap; }
.stage-btn.sel { filter: brightness(1.25); box-shadow: 0 0 0 2px var(--gold); border-radius: var(--r); }
.stage-btn.sel b { color: var(--gold); }
.stage-btn.locked { filter: grayscale(.8) brightness(.62); }
.stage-desc { margin: 0; min-height: 2.9em; text-align: center; font-size: 13.5px; color: #dfe6da; text-shadow: 0 1px 2px #000; }
.title-actions { display: flex; flex-direction: column; gap: 8px; }

/* ---------- 對局畫面 ---------- */
#screen-play {
  display: grid; grid-template-rows: auto minmax(0, 1fr) auto; gap: 8px;
  padding: calc(8px + var(--sat)) calc(8px + var(--sar)) calc(8px + var(--sab)) calc(8px + var(--sal));
  background: linear-gradient(rgba(8, 16, 18, .35), rgba(8, 16, 18, .35)), url(assets/bg_table.webp) center / cover no-repeat, var(--ink);
}
#hud { display: grid; grid-template-columns: 1fr 1.25fr 1.15fr; gap: 6px; }
.plaque {
  position: relative; min-width: 0; min-height: 66px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
  border-style: solid; border-color: transparent; border-width: 10px 14px;
  border-image: url(assets/ui_hud_plaque.webp) 110 130 fill / 12px 15px stretch;
}
.plaque small { font-size: 11px; line-height: 1.2; color: var(--mute); white-space: nowrap; }
.plaque > b { font-size: 23px; line-height: 1.1; color: var(--gold); font-variant-numeric: tabular-nums; font-weight: 400; }
.turnbar { width: 100%; height: 4px; margin-top: 3px; border-radius: 2px; overflow: hidden; background: rgba(255, 255, 255, .14); }
.turnbar i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--bamboo), var(--gold)); transition: width .3s; }
#tray { display: flex; gap: 4px; min-height: 28px; align-items: center; justify-content: center; }
.mini {
  width: 26px; height: 26px; flex: none; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center;
  font-size: 15px; line-height: 1; color: #1d2a2c;
  background: radial-gradient(circle at 35% 30%, #f3deb6, #dcbc8a 55%, #b88f5a);
  box-shadow: inset 0 0 0 1.5px rgba(110, 70, 35, .5), 0 1px 2px rgba(0, 0, 0, .5);
}
.mini.rook { color: #b8301f; }
.combo {
  position: absolute; top: -8px; right: -10px; z-index: 2; font-style: normal; font-size: 11px; line-height: 1.5; padding: 0 6px;
  background: var(--cinnabar); color: #fff; border-radius: 4px; white-space: nowrap;
}
.combo.pop { animation: pop .3s ease-out; }

#board-wrap { min-width: 0; min-height: 0; display: flex; align-items: center; justify-content: center; }
#board {
  position: relative; flex: none; box-sizing: content-box; touch-action: manipulation; cursor: pointer;
  width: calc(var(--cell) * 9); height: calc(var(--cell) * 10);
  border-style: solid; border-color: #3a2a1c; border-width: calc(var(--cell) * .45);
  border-image: url(assets/ui_board.webp) 80 fill / calc(var(--cell) * .45) stretch;
  filter: drop-shadow(0 8px 14px rgba(0, 0, 0, .55));
}
#board .lines { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
#board .lines line { stroke: #3c3a33; stroke-width: 1.3px; vector-effect: non-scaling-stroke; stroke-linecap: round; opacity: .85; }
#board .lines .outer { fill: none; stroke: #3c3a33; stroke-width: 2.2px; vector-effect: non-scaling-stroke; opacity: .8; }
#marks, #pieces, #fx { position: absolute; inset: 0; pointer-events: none; }
#fx { z-index: 3; }

.piece {
  position: absolute; left: 0; top: 0; width: calc(var(--cell) * .88); height: calc(var(--cell) * .88);
  margin: calc(var(--cell) * -.44) 0 0 calc(var(--cell) * -.44);
  transition: transform .19s cubic-bezier(.3, .8, .3, 1); will-change: transform;
}
.piece.moving { z-index: 2; }
.piece.rook { z-index: 1; }
.notrans .piece, .notrans .mark { transition: none !important; }
.disc {
  width: 100%; height: 100%; border-radius: 50%; display: flex; align-items: center; justify-content: center;
  background: radial-gradient(circle at 36% 30%, #f5e2bd 0%, #e2c592 42%, #c39a63 100%);
  box-shadow: inset 0 0 0 calc(var(--cell) * .06) #dcbc8a, inset 0 0 0 calc(var(--cell) * .085) rgba(104, 66, 32, .55),
    0 calc(var(--cell) * .06) 0 #8a6238, 0 calc(var(--cell) * .12) calc(var(--cell) * .12) rgba(0, 0, 0, .5);
}
.disc span { font-size: calc(var(--cell) * .5); line-height: 1; color: #1d2a2c; text-shadow: 0 1px 0 rgba(255, 255, 255, .35); }
.piece.rook .disc span { color: #b8301f; }
.piece.aim .disc { box-shadow: inset 0 0 0 calc(var(--cell) * .06) #dcbc8a, inset 0 0 0 calc(var(--cell) * .085) rgba(104, 66, 32, .55), 0 calc(var(--cell) * .06) 0 #8a6238, 0 0 0 2px var(--cinnabar), 0 0 calc(var(--cell) * .3) rgba(200, 64, 47, .9); }
.piece.threat .disc { animation: threat 1s ease-in-out infinite; }
.piece.drop .disc { animation: drop .32s cubic-bezier(.3, 1.3, .5, 1) both; }
.piece.gone .disc { animation: gone .26s ease-in forwards; }
.piece.dead .disc { animation: dead .45s ease-in forwards; }

.mark { position: absolute; left: 0; top: 0; width: var(--cell); height: var(--cell); margin: calc(var(--cell) * -.5) 0 0 calc(var(--cell) * -.5); }
.mark::after { content: ""; position: absolute; inset: 0; margin: auto; border-radius: 50%; }
.mark.go.safe::after { width: 28%; height: 28%; background: var(--bamboo); box-shadow: 0 0 0 calc(var(--cell) * .07) rgba(111, 174, 122, .3), 0 1px 2px rgba(0, 0, 0, .4); }
.mark.go.danger::after { width: 36%; height: 36%; border: 2px solid var(--cinnabar); background: rgba(200, 64, 47, .16); }
.mark.go.danger::before { content: "×"; position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: var(--cinnabar); font-size: calc(var(--cell) * .3); font-weight: 700; line-height: 1; }
.mark.cap::after { width: 100%; height: 100%; border: 3px solid var(--bamboo); animation: ring 1.1s ease-in-out infinite; }
.mark.cap.danger::after { border-color: var(--cinnabar); border-style: dashed; }
.mark.sel::after { width: 104%; height: 104%; border: 2px solid var(--gold); box-shadow: 0 0 calc(var(--cell) * .28) rgba(217, 164, 65, .8); animation: ring 1.4s ease-in-out infinite; }
.mark.cursor::after { width: 96%; height: 96%; border-radius: 4px; border: 2px dashed #fff3cf; box-shadow: 0 0 0 1px rgba(0, 0, 0, .5); }

.float {
  position: absolute; left: 0; top: 0; margin: calc(var(--cell) * -.4) 0 0 calc(var(--cell) * -1); width: calc(var(--cell) * 2); text-align: center;
  font-size: calc(var(--cell) * .46); line-height: 1; color: var(--gold); text-shadow: 0 1px 0 #000, 0 0 6px rgba(0, 0, 0, .8);
  animation: floatup .85s ease-out forwards;
}
.float.big { font-size: calc(var(--cell) * .6); color: #ffd98a; }
.spark { position: absolute; width: 6px; height: 6px; margin: -3px; border-radius: 50%; background: var(--gold); animation: spark .5s ease-out forwards; }
.spark.red { background: var(--cinnabar); }
.spark.ink { background: #26363a; }
#board.shake { animation: shake .36s; }
#board.nudge { animation: nudge .22s; }

#actions { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 48px; }
.stage-tag { min-width: 0; display: flex; flex-direction: column; text-shadow: 0 1px 2px #000; }
.stage-tag b { font-size: 17px; line-height: 1.25; color: var(--paper); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stage-tag span { font-size: 13px; color: var(--mute); white-space: nowrap; }
.stage-tag span.enemy { color: #f39a86; }
.icon-row { display: flex; gap: 10px; flex: none; }

/* 橫屏 / 桌面：棋盤在左，HUD 與操作移到右側欄 */
@media (min-aspect-ratio: 1/1) {
  #screen-play { grid-template-columns: minmax(0, 1fr) clamp(190px, 30vw, 300px); grid-template-rows: minmax(0, 1fr) auto; gap: 10px 14px; }
  #board-wrap { grid-column: 1; grid-row: 1 / 3; }
  #hud { grid-column: 2; grid-row: 1; grid-template-columns: 1fr; align-content: start; }
  #actions { grid-column: 2; grid-row: 2; flex-direction: column; align-items: stretch; }
  .icon-row { justify-content: space-between; }
  #screen-title { flex-direction: row; align-items: center; justify-content: space-evenly; gap: 24px; }
  .title-head { margin-top: 0; }
  .title-head h1 { font-size: clamp(40px, 9vh + 2vw, 84px); writing-mode: vertical-rl; letter-spacing: .1em; text-indent: 0; }
  .tagline { writing-mode: vertical-rl; display: inline-block; margin: 0 0 0 8px; vertical-align: top; text-indent: 0; }
  .title-head { display: flex; flex-direction: row-reverse; align-items: flex-start; }
  .title-art { background: linear-gradient(90deg, rgba(8, 16, 18, .2) 0%, rgba(8, 16, 18, .9) 62%), url(assets/title_keyart_v2.webp) left center / cover no-repeat, var(--ink2); }
}
@media (min-aspect-ratio: 1/1) and (max-height: 480px) {
  .plaque { min-height: 58px; }
  .title-body { gap: 6px; }
  .stage-btn { min-height: 54px; }
  .stage-desc { min-height: 0; font-size: 12.5px; }
  .title-actions { flex-direction: row; }
  .title-actions .btn { flex: 1 1 0; }
  .title-head h1 { font-size: clamp(34px, 15vh, 60px); }
}

/* ---------- 彈窗 ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 40; display: flex; align-items: center; justify-content: center; overflow: hidden;
  padding: calc(12px + var(--sat)) calc(12px + var(--sar)) calc(12px + var(--sab)) calc(12px + var(--sal));
  background: rgba(5, 11, 13, .74); animation: fade .18s ease-out;
}
.panel {
  position: relative; width: min(400px, 100%); max-height: 100%; display: flex; flex-direction: column; gap: 10px;
  border-style: solid; border-color: transparent; border-width: 26px;
  border-image: url(assets/ui_panel_v2.webp) 90 fill / 28px stretch;
  animation: panelin .24s cubic-bezier(.3, 1.2, .5, 1);
}
.panel h2 { margin: 0; padding: 0 40px; text-align: center; font-size: 26px; line-height: 1.3; letter-spacing: .12em; text-indent: .12em; color: var(--paper); }
.overlay.win .panel h2 { color: var(--gold); }
.overlay.lose .panel h2 { color: #f08a76; }
.panel .close { position: absolute; top: -10px; right: -10px; width: 44px; height: 44px; font-size: 22px; }
.panel-body { min-height: 0; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; padding: 0 2px; }
.panel-actions { display: flex; flex-direction: column; gap: 8px; flex: none; }
.lead { margin: 0 0 8px; font-size: 14px; color: #dfe6da; text-align: center; overflow-wrap: anywhere; }
.stat { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 5px 4px; border-bottom: 1px solid rgba(217, 164, 65, .22); font-size: 14px; color: var(--mute); }
.stat b { font-size: 18px; font-weight: 400; color: var(--gold); font-variant-numeric: tabular-nums; }
.toggles { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.toggle { font-size: 14px; letter-spacing: .04em; border-width: 12px 14px; min-height: 48px; }
.legend { list-style: none; margin: 0 0 8px; padding: 0; display: grid; gap: 6px; }
.legend li { display: grid; grid-template-columns: 26px 1fr; gap: 10px; align-items: center; font-size: 13.5px; color: #e4eadf; }
.hint { margin: 6px 0 0; font-size: 12.5px; color: var(--mute); }
.dot { display: inline-block; width: 11px; height: 11px; border-radius: 50%; vertical-align: -1px; margin-right: 4px; }
.dot.safe { background: var(--bamboo); }
.dot.danger { border: 2px solid var(--cinnabar); }
@media (min-aspect-ratio: 1/1) and (max-height: 520px) {
  .panel { width: min(640px, 100%); gap: 6px; border-width: 22px 26px; }
  .panel h2 { font-size: 22px; }
  .panel-actions { flex-direction: row; flex-wrap: wrap; }
  .panel-actions .btn { flex: 1 1 28%; font-size: 16px; min-height: 48px; }
  .panel-body { display: grid; grid-template-columns: 1fr 1fr; column-gap: 18px; align-content: start; }
  .panel-body .lead, .panel-body .legend, .panel-body .hint, .panel-body .toggles { grid-column: 1 / -1; }
  .legend { grid-template-columns: 1fr 1fr; column-gap: 14px; }
}
.petal { position: absolute; top: -12px; width: 8px; height: 12px; border-radius: 60% 0; background: var(--gold); opacity: .9; pointer-events: none; animation: petal 2.2s linear forwards; }
.petal:nth-child(3n) { background: var(--cinnabar); }
.petal:nth-child(3n+1) { background: var(--paper); }

#toast {
  position: fixed; left: 50%; bottom: calc(76px + var(--sab)); z-index: 60; transform: translate(-50%, 8px); max-width: calc(100% - 32px);
  padding: 8px 16px; border-radius: var(--r); border: 1px solid rgba(217, 164, 65, .7); background: rgba(12, 23, 26, .94);
  color: var(--paper); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  opacity: 0; pointer-events: none; transition: opacity .18s, transform .18s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@keyframes fade { from { opacity: 0; } }
@keyframes panelin { from { opacity: 0; transform: translateY(14px) scale(.96); } }
@keyframes pop { 0% { transform: scale(.6); } 60% { transform: scale(1.25); } 100% { transform: scale(1); } }
@keyframes ring { 0%, 100% { opacity: 1; transform: scale(1); } 50% { opacity: .55; transform: scale(.93); } }
@keyframes threat { 0%, 100% { filter: none; } 50% { filter: drop-shadow(0 0 6px #ff5a43) brightness(1.08); } }
@keyframes drop { from { opacity: 0; transform: translateY(calc(var(--cell) * -1.1)) scale(1.25); } }
@keyframes gone { to { opacity: 0; transform: scale(.25) rotate(40deg); } }
@keyframes dead { 30% { transform: scale(1.15); } 100% { opacity: 0; transform: scale(.2) rotate(-70deg); } }
@keyframes floatup { from { opacity: 0; top: 0; } 20% { opacity: 1; } to { opacity: 0; top: calc(var(--cell) * -1); } }
@keyframes spark { to { opacity: 0; transform: translate(var(--dx), var(--dy)) scale(.3); } }
@keyframes shake { 20% { transform: translate(-5px, 2px); } 40% { transform: translate(5px, -2px); } 60% { transform: translate(-3px, 1px); } 80% { transform: translate(2px, 0); } }
@keyframes nudge { 30% { transform: translate(-3px, 0); } 70% { transform: translate(3px, 0); } }
@keyframes petal { to { transform: translateY(110vh) rotate(540deg); opacity: .3; } }

/* 減少動態 */
body.reduce *, body.reduce *::before, body.reduce *::after { animation-duration: .01s !important; animation-iteration-count: 1 !important; transition-duration: .01s !important; }
@media (prefers-reduced-motion: reduce) {
  .mark::after, .piece.threat .disc { animation: none; }
}
