:root {
  --ink: #edf4ff;
  --muted: #91a2bd;
  --soft: #60738f;
  --night: #071226;
  --night-deep: #040b19;
  --panel: rgba(14, 29, 54, 0.82);
  --panel-strong: rgba(12, 24, 47, 0.96);
  --line: rgba(171, 204, 238, 0.14);
  --line-strong: rgba(171, 204, 238, 0.25);
  --gold: #ffd477;
  --gold-deep: #f2ad46;
  --cyan: #71e3f4;
  --coral: #ff7d78;
  --green: #7ce0ae;
  font-family: "Avenir Next", Avenir, "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  color: var(--ink);
  background: var(--night);
  font-synthesis: none;
}

* { box-sizing: border-box; }

html { min-width: 320px; background: var(--night); }

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  background:
    radial-gradient(circle at 9% 3%, rgba(56, 120, 197, 0.19), transparent 28rem),
    radial-gradient(circle at 90% 80%, rgba(199, 133, 43, 0.11), transparent 27rem),
    var(--night);
}

button { font: inherit; }

.dice-page {
  width: min(1280px, calc(100% - 42px));
  min-height: 100vh;
  margin: 0 auto;
  padding: clamp(24px, 4.5vw, 58px) 0 26px;
}

.back-link {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-decoration: none;
  transition: color 160ms ease, transform 160ms ease;
}

.back-link span { color: var(--gold); font-size: 1rem; }
.back-link:hover { color: var(--ink); transform: translateX(-3px); }

.game-header {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 24px;
  margin: clamp(32px, 6vw, 68px) 0 28px;
}

.eyebrow,
.section-kicker,
.page-footer,
.cover-kicker {
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.eyebrow {
  display: flex;
  align-items: center;
  gap: 9px;
  color: var(--gold);
  font-size: 0.66rem;
  font-weight: 850;
}

.eyebrow-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--gold);
  box-shadow: 0 0 0 5px rgba(255, 212, 119, 0.12), 0 0 16px rgba(255, 212, 119, 0.75);
}

h1, h2, h3, p { margin: 0; }

h1 {
  margin-top: 15px;
  color: var(--ink);
  font-size: clamp(3rem, 7vw, 6.3rem);
  font-weight: 850;
  letter-spacing: -0.1em;
  line-height: 0.86;
}

h1 span { color: var(--gold); }

.game-tagline {
  margin-top: 17px;
  color: var(--muted);
  font-size: clamp(1rem, 2vw, 1.2rem);
  font-weight: 650;
}

.header-actions { display: flex; gap: 10px; padding-bottom: 4px; }

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 42px;
  padding: 0 16px;
  border: 1px solid transparent;
  border-radius: 12px;
  cursor: pointer;
  color: var(--ink);
  font-size: 0.75rem;
  font-weight: 850;
  letter-spacing: 0.02em;
  transition: transform 160ms ease, background 160ms ease, border-color 160ms ease, box-shadow 160ms ease;
  -webkit-tap-highlight-color: transparent;
}

.button:hover { transform: translateY(-2px); }
.button:active { transform: translateY(0); }
.button:disabled { cursor: not-allowed; opacity: 0.42; transform: none; }
.button:focus-visible { outline: 3px solid rgba(113, 227, 244, 0.4); outline-offset: 3px; }

.button-primary {
  color: #101b2c;
  background: linear-gradient(135deg, var(--gold), #ffeab2);
  box-shadow: 0 11px 25px rgba(255, 197, 92, 0.17);
}

.button-primary:hover { background: linear-gradient(135deg, #ffe39a, #fff3ce); box-shadow: 0 14px 30px rgba(255, 197, 92, 0.25); }
.button-primary span { margin-left: 9px; font-size: 1rem; }

.button-quiet {
  border-color: var(--line);
  background: rgba(21, 40, 70, 0.68);
}

.button-quiet:hover { border-color: var(--line-strong); background: rgba(31, 56, 91, 0.82); }

.game-shell {
  display: grid;
  grid-template-columns: minmax(0, 1.6fr) minmax(300px, 0.78fr);
  gap: 18px;
  align-items: start;
}

.board-panel,
.control-block {
  border: 1px solid var(--line);
  background: var(--panel);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2), inset 0 1px 0 rgba(255, 255, 255, 0.035);
  backdrop-filter: blur(14px);
}

.board-panel { min-width: 0; padding: clamp(15px, 2.6vw, 24px); border-radius: 22px; }

.board-topline,
.block-heading,
.shop-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.board-topline { margin-bottom: 14px; }
.section-kicker { display: block; color: var(--soft); font-size: 0.55rem; font-weight: 850; }
.battlefield-stats { display: flex; align-items: center; gap: 17px; margin-top: 6px; }
.battlefield-stats > span { display: flex; align-items: baseline; gap: 6px; }
.battlefield-stats small { color: var(--soft); font-size: 0.5rem; font-weight: 850; letter-spacing: 0.12em; }
.battlefield-stats strong { color: var(--ink); font-size: 0.86rem; font-variant-numeric: tabular-nums; }
.battlefield-stats b { color: var(--muted); font-size: 0.55rem; font-weight: 750; }
.board-actions { display: flex; align-items: center; gap: 10px; }
.wave-countdown { color: var(--gold); font-size: 1.05rem; font-weight: 850; font-variant-numeric: tabular-nums; }
.board-start-button { min-height: 36px; padding: 0 12px; font-size: 0.68rem; }

.board-frame {
  position: relative;
  width: 100%;
  overflow: hidden;
  border: 1px solid rgba(151, 195, 228, 0.18);
  border-radius: 17px;
  background: #07162a;
  box-shadow: inset 0 0 0 6px rgba(5, 14, 28, 0.2), 0 18px 35px rgba(0, 0, 0, 0.23);
  isolation: isolate;
}

#game-canvas {
  display: block;
  width: 100%;
  aspect-ratio: 10 / 8;
  touch-action: none;
  cursor: pointer;
}

.board-toast {
  position: absolute;
  z-index: 4;
  top: 16px;
  left: 50%;
  min-height: 22px;
  color: var(--gold);
  font-size: 0.74rem;
  font-weight: 850;
  text-shadow: 0 0 18px rgba(255, 212, 119, 0.8);
  transform: translateX(-50%);
  pointer-events: none;
  white-space: nowrap;
}

.control-panel { display: grid; gap: 12px; }
.control-block { padding: 17px; border-radius: 18px; }
.block-heading strong { color: var(--gold); font-size: 0.82rem; font-weight: 850; }
.shop-balance { text-align: right; }
.shop-balance span { display: block; color: var(--soft); font-size: 0.53rem; font-weight: 800; }
.shop-balance strong { display: block; margin-top: 3px; font-variant-numeric: tabular-nums; }
.shop-balance b { font-size: 1rem; }

.shop-row { margin-top: 13px; }
.shop-row p { color: var(--ink); font-size: 0.76rem; font-weight: 800; }
.shop-row small { display: block; margin-top: 3px; color: var(--soft); font-size: 0.61rem; font-weight: 600; }
.shop-row .button { min-height: 35px; padding: 0 11px; font-size: 0.68rem; }

.shop-row .buy-dice-button {
  width: 100%;
  min-height: 48px;
  justify-content: space-between;
  padding: 0 14px;
  border-color: rgba(255, 224, 148, 0.9);
  color: #111d2f;
  background: linear-gradient(135deg, #ffc95f, #fff0b8);
  box-shadow: 0 10px 26px rgba(255, 193, 72, 0.25), inset 0 1px rgba(255, 255, 255, 0.72);
  font-size: 0.78rem;
  font-weight: 950;
}

.shop-row .buy-dice-button:hover {
  border-color: #fff4cf;
  background: linear-gradient(135deg, #ffda7c, #fff7d8);
  box-shadow: 0 14px 32px rgba(255, 193, 72, 0.34), inset 0 1px rgba(255, 255, 255, 0.82);
}

.buy-dice-button strong {
  padding: 5px 8px;
  border-radius: 8px;
  color: #fff4c8;
  background: #14233a;
  font-size: 0.7rem;
  letter-spacing: 0.06em;
}

.dice-tray {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  min-height: 66px;
  max-height: 190px;
  margin-top: 14px;
  padding: 9px;
  border: 1px dashed var(--line-strong);
  border-radius: 13px;
  background: rgba(5, 13, 27, 0.26);
  overflow-y: auto;
}

.tray-hint { margin-top: 9px; color: var(--soft); font-size: 0.64rem; line-height: 1.5; }

.die-button {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  min-width: 0;
  min-height: 0;
  aspect-ratio: 1 / 1;
  align-self: start;
  padding: 5px;
  overflow: hidden;
  border: 1px solid color-mix(in srgb, var(--die-color), white 18%);
  border-radius: 10px;
  color: #071226;
  background: var(--die-color);
  cursor: pointer;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.12), 0 5px 12px rgba(0, 0, 0, 0.18);
  transition: transform 150ms ease, box-shadow 150ms ease, filter 150ms ease;
  touch-action: none;
}

.die-button:hover { filter: brightness(1.1); transform: translateY(-2px); }
.die-button.is-selected { box-shadow: 0 0 0 3px var(--gold), 0 7px 16px rgba(255, 212, 119, 0.3); transform: translateY(-2px); }
.die-button.is-empty { min-height: 48px; aspect-ratio: auto; border-style: dashed; color: var(--soft); background: transparent; box-shadow: none; cursor: default; }

.die-face {
  position: absolute;
  inset: 4px;
  color: #071226;
  pointer-events: none;
}

.die-pip {
  position: absolute;
  left: 50%;
  top: 50%;
  font-size: 0.78rem;
  font-weight: 950;
  line-height: 1;
  transform: translate(-50%, -50%);
}

.die-face.is-tier-1 .die-pip { font-size: 1.45rem; }
.die-face.is-tier-2 .die-pip { font-size: 0.96rem; }
.die-face.is-tier-3 .die-pip { font-size: 0.78rem; }
.die-face.is-tier-4 .die-pip { font-size: 0.66rem; }
.die-face.is-tier-5 .die-pip { font-size: 0.6rem; }
.die-face.is-tier-6 .die-pip { font-size: 0.56rem; }
.die-face.is-tier-2 .die-pip:nth-child(1),
.die-face.is-tier-3 .die-pip:nth-child(1),
.die-face.is-tier-4 .die-pip:nth-child(1),
.die-face.is-tier-5 .die-pip:nth-child(1),
.die-face.is-tier-6 .die-pip:nth-child(1) { left: 26%; top: 26%; }
.die-face.is-tier-4 .die-pip:nth-child(2),
.die-face.is-tier-5 .die-pip:nth-child(2),
.die-face.is-tier-6 .die-pip:nth-child(2) { left: 74%; top: 26%; }
.die-face.is-tier-3 .die-pip:nth-child(2) { left: 50%; top: 50%; }
.die-face.is-tier-5 .die-pip:nth-child(3) { left: 50%; top: 50%; }
.die-face.is-tier-6 .die-pip:nth-child(3) { left: 26%; top: 50%; }
.die-face.is-tier-6 .die-pip:nth-child(4) { left: 74%; top: 50%; }
.die-face.is-tier-4 .die-pip:nth-child(3),
.die-face.is-tier-5 .die-pip:nth-child(4),
.die-face.is-tier-6 .die-pip:nth-child(5) { left: 26%; top: 74%; }
.die-face.is-tier-2 .die-pip:nth-child(2),
.die-face.is-tier-3 .die-pip:nth-child(3),
.die-face.is-tier-4 .die-pip:nth-child(4),
.die-face.is-tier-5 .die-pip:nth-child(5),
.die-face.is-tier-6 .die-pip:nth-child(6) { left: 74%; top: 74%; }

.inspector-block { min-height: 183px; }
.empty-inspector { display: grid; place-items: center; min-height: 146px; text-align: center; }
.inspector-icon { display: block; margin-bottom: 9px; color: var(--gold); font-size: 1.4rem; }
.empty-inspector strong { color: var(--ink); font-size: 0.82rem; }
.empty-inspector p { margin-top: 6px; color: var(--soft); font-size: 0.66rem; }

.tower-inspector { display: grid; gap: 12px; }
.tower-inspector-heading { display: flex; align-items: center; gap: 10px; }
.tower-symbol {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  color: #071226;
  background: var(--tower-color);
  font-size: 1.18rem;
  font-weight: 950;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.14);
}

.tower-inspector-heading strong { color: var(--ink); font-size: 0.88rem; }
.tower-inspector-heading small { display: block; margin-top: 4px; color: var(--muted); font-size: 0.62rem; }
.tower-stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.tower-stat { padding: 8px 7px; border-radius: 9px; background: rgba(5, 13, 27, 0.34); text-align: center; }
.tower-stat span { display: block; color: var(--soft); font-size: 0.52rem; }
.tower-stat strong { display: block; margin-top: 4px; color: var(--ink); font-size: 0.72rem; }
.crit-row { display: grid; grid-template-columns: auto auto 1fr; align-items: center; gap: 8px; color: var(--muted); font-size: 0.61rem; }
.crit-row strong { color: var(--gold); font-size: 0.78rem; }
.crit-row b { justify-self: end; color: var(--soft); font-size: 0.58rem; font-weight: 750; }
.crit-row b.is-ready { color: #fff2a8; text-shadow: 0 0 8px rgba(255, 212, 119, 0.72); }
.construction-row { display: grid; grid-template-columns: auto auto 1fr; align-items: center; gap: 8px; padding: 9px 10px; border: 1px solid rgba(113, 216, 244, 0.24); border-radius: 10px; background: rgba(113, 216, 244, 0.08); }
.construction-row span { color: #9cecff; font-size: 0.62rem; font-weight: 800; }
.construction-row strong { color: #d9f9ff; font-size: 0.78rem; }
.construction-row b { justify-self: end; color: var(--soft); font-size: 0.58rem; font-weight: 750; }
.inspector-description { color: var(--muted); font-size: 0.64rem; line-height: 1.55; }
.inspector-actions { display: grid; grid-template-columns: repeat(2, 1fr); gap: 7px; }
.inspector-actions.is-single { grid-template-columns: 1fr; }
.inspector-actions .button { min-height: 34px; padding: 0 8px; font-size: 0.63rem; }
.inspector-actions .button-danger { color: #ffb8b0; border-color: rgba(255, 125, 120, 0.22); background: rgba(132, 39, 52, 0.3); }
.inspector-mode-note { padding: 9px; border: 1px solid var(--line); border-radius: 9px; color: var(--muted); background: rgba(5, 13, 27, 0.3); font-size: 0.62rem; text-align: center; }
.drag-preview {
  position: fixed;
  z-index: 100;
  display: grid;
  place-items: center;
  width: 54px;
  height: 54px;
  border: 2px solid rgba(255, 255, 255, 0.78);
  border-radius: 13px;
  color: #071226;
  background: var(--drag-color, var(--gold));
  box-shadow: 0 14px 30px rgba(0, 0, 0, 0.34), 0 0 0 4px rgba(255, 212, 119, 0.18);
  transform: translate(-50%, -50%) rotate(-5deg);
  pointer-events: none;
  user-select: none;
}
.drag-preview .die-face { inset: 5px; }
.drag-preview .die-face.is-tier-1 .die-pip { font-size: 1.72rem; }
.drag-preview .die-face.is-tier-2 .die-pip { font-size: 1.08rem; }
.drag-preview .die-face.is-tier-3 .die-pip { font-size: 0.9rem; }
.drag-preview .die-face.is-tier-4 .die-pip { font-size: 0.76rem; }
.drag-preview .die-face.is-tier-5 .die-pip { font-size: 0.68rem; }
.drag-preview .die-face.is-tier-6 .die-pip { font-size: 0.62rem; }
body.is-dragging { cursor: grabbing; user-select: none; }

.pause-cover,
.startup-cover,
.tutorial-cover,
.result-cover {
  position: absolute;
  z-index: 10;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(3, 9, 20, 0.73);
  backdrop-filter: blur(7px);
}

[hidden] { display: none !important; }

.cover-card {
  width: min(100%, 330px);
  padding: 28px;
  border: 1px solid rgba(255, 212, 119, 0.23);
  border-radius: 18px;
  background: var(--panel-strong);
  box-shadow: 0 24px 55px rgba(0, 0, 0, 0.35);
  text-align: center;
}

.cover-kicker { color: var(--gold); font-size: 0.58rem; font-weight: 850; }
.cover-card h2 { margin-top: 11px; color: var(--ink); font-size: 1.55rem; letter-spacing: -0.04em; }
.cover-card p { margin-top: 10px; color: var(--muted); font-size: 0.74rem; line-height: 1.6; }
.cover-card .button { width: 100%; margin-top: 21px; }
.startup-card { width: min(100%, 390px); }
.tutorial-card { width: min(100%, 440px); }
.tutorial-cover { align-items: start; overflow-y: auto; }
.tutorial-cover .tutorial-card { margin-block: auto; }
.tutorial-merge { display: flex; align-items: center; justify-content: center; gap: 9px; margin-top: 18px; }
.tutorial-merge > b { color: var(--muted); font-size: 1rem; }
.tutorial-die { position: relative; display: grid; place-items: center; width: 48px; height: 48px; border-radius: 11px; color: #071226; background: #ff9b65; box-shadow: inset 0 -4px rgba(0, 0, 0, 0.13), 0 7px 16px rgba(0, 0, 0, 0.24); font-size: 1.05rem; font-weight: 950; }
.tutorial-die.is-upgraded { width: 54px; height: 54px; border: 2px solid #ffe49b; box-shadow: inset 0 -4px rgba(0, 0, 0, 0.13), 0 0 22px rgba(255, 212, 119, 0.38); }
.tutorial-die small { position: absolute; right: 5px; bottom: 3px; color: rgba(7, 18, 38, 0.72); font-size: 0.58rem; }
.tutorial-points { display: grid; gap: 7px; margin: 18px 0 0; padding: 0; list-style: none; color: var(--muted); font-size: 0.68rem; line-height: 1.45; text-align: left; }
.tutorial-points li { position: relative; padding-left: 17px; }
.tutorial-points li::before { position: absolute; left: 0; color: var(--gold); content: "◆"; font-size: 0.54rem; }
.startup-actions { display: grid; grid-template-columns: 1.25fr 0.9fr; gap: 9px; margin-top: 21px; }
.startup-actions .button { margin-top: 0; }
.result-card { position: relative; width: min(100%, 390px); }
.result-close { position: absolute; top: 10px; right: 10px; display: grid; place-items: center; width: 32px; height: 32px; padding: 0; border: 1px solid var(--line-strong); border-radius: 9px; color: var(--muted); background: rgba(5, 13, 27, 0.5); cursor: pointer; font-size: 1.2rem; line-height: 1; }
.result-close:hover { color: var(--ink); border-color: rgba(255, 212, 119, 0.45); background: rgba(30, 48, 76, 0.8); }
.result-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; margin-top: 20px; }
.result-grid div { padding: 9px 5px; border-radius: 9px; background: rgba(5, 13, 27, 0.4); }
.result-grid span { display: block; color: var(--soft); font-size: 0.52rem; }
.result-grid strong { display: block; margin-top: 4px; color: var(--gold); font-size: 0.85rem; }

@media (max-width: 430px) {
  .startup-actions { grid-template-columns: 1fr; }
}

.page-footer {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-top: 25px;
  color: var(--soft);
  font-size: 0.58rem;
  font-weight: 850;
}

.footer-rule { width: 35px; height: 1px; background: var(--line-strong); }

@media (max-width: 960px) {
  .game-shell { grid-template-columns: minmax(0, 1.3fr) minmax(280px, 0.8fr); }
}

@media (max-width: 760px) {
  .dice-page { width: 100%; padding-top: 24px; }
  .back-link,
  .game-header,
  .control-panel,
  .page-footer { margin-right: 12px; margin-left: 12px; }
  .game-header { align-items: start; flex-direction: column; margin-top: 42px; }
  .header-actions { width: 100%; padding-bottom: 0; }
  .header-actions .button { flex: 1; }
  .game-shell { width: 100%; grid-template-columns: 1fr; gap: 0; border-right: 0; border-left: 0; border-radius: 0; }
  .board-panel { padding: 0; border-right: 0; border-left: 0; border-radius: 0; }
  .board-topline { margin-bottom: 0; padding: 12px; }
  .board-frame { border: 0; border-radius: 0; }
  .control-panel { grid-template-columns: repeat(2, minmax(0, 1fr)); margin: 0; padding: 12px; }
  .shop-block, .inspector-block { grid-column: span 2; }
  .dice-tray { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
}

@media (max-width: 480px) {
  .game-header { margin-top: 34px; }
  h1 { font-size: clamp(3.15rem, 17vw, 4.6rem); }
  .game-tagline { font-size: 0.92rem; }
  .board-actions { gap: 7px; }
  .battlefield-stats { gap: 10px; }
  .battlefield-stats > span { gap: 4px; }
  .board-start-button { min-height: 34px; padding: 0 9px; font-size: 0.61rem; }
  .control-panel { grid-template-columns: 1fr; }
  .shop-block, .inspector-block { grid-column: auto; }
  .cover-card { padding: 24px 18px; }
  .page-footer { flex-wrap: wrap; line-height: 1.6; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; }
}
