:root {
  color-scheme: dark;
  --paper: #f0dec1;
  --paper-muted: #baaa91;
  --ink: #16120d;
  --panel: rgba(29, 23, 16, 0.94);
  --panel-soft: rgba(53, 42, 29, 0.72);
  --line: rgba(223, 183, 106, 0.3);
  --gold: #dfb76a;
  --gold-bright: #f4cf83;
  --red: #a53a2b;
}

* {
  box-sizing: border-box;
}

html {
  min-width: 320px;
  background: #0e0b08;
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--paper);
  font-family: "Noto Serif TC", "Microsoft JhengHei", system-ui, sans-serif;
  background:
    radial-gradient(circle at 20% 0%, rgba(157, 58, 42, 0.2), transparent 35rem),
    linear-gradient(rgba(13, 10, 7, 0.91), rgba(13, 10, 7, 0.97)),
    repeating-linear-gradient(90deg, #17110b 0 2px, #110d09 2px 6px);
}

button,
a {
  -webkit-tap-highlight-color: transparent;
}

button {
  min-height: 42px;
  padding: 0.65rem 1rem;
  border: 1px solid var(--line);
  border-radius: 0.45rem;
  color: var(--paper);
  font: inherit;
  font-weight: 700;
  background: linear-gradient(#443421, #2a2117);
  cursor: pointer;
}

button:hover:not(:disabled),
button:focus-visible {
  border-color: var(--gold-bright);
  outline: none;
  transform: translateY(-1px);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.52;
}

button.primary {
  border-color: #d07c53;
  background: linear-gradient(#ba4936, #7d241c);
  box-shadow: 0 0.5rem 1.6rem rgba(92, 26, 18, 0.3);
}

.app-shell {
  width: min(100% - 2rem, 1080px);
  margin: 0 auto;
  padding: 2.2rem 0 3rem;
}

.hero,
.panel-heading {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 1.5rem;
}

.hero {
  padding: 0 0.15rem 1.35rem;
}

.eyebrow {
  margin: 0 0 0.35rem;
  color: var(--gold);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.22em;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0.35rem;
  font-size: clamp(2rem, 6vw, 4.2rem);
  font-weight: 900;
  letter-spacing: 0.08em;
  line-height: 1;
  text-shadow: 0 0.12em 0 #25170f, 0 0 1.4rem rgba(223, 183, 106, 0.18);
}

h2 {
  margin-bottom: 0.55rem;
  color: var(--gold-bright);
  font-size: 1.05rem;
}

.subtitle,
#status,
footer {
  color: var(--paper-muted);
}

.subtitle,
#status {
  margin-bottom: 0;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 0.55rem;
}

.game-panel,
.help-grid article {
  border: 1px solid var(--line);
  border-radius: 0.75rem;
  background: var(--panel);
  box-shadow: 0 1rem 4rem rgba(0, 0, 0, 0.35);
}

.game-panel {
  overflow: hidden;
}

.panel-heading {
  align-items: center;
  padding: 1rem 1.15rem;
  border-bottom: 1px solid var(--line);
}

.panel-heading h2 {
  margin-bottom: 0.2rem;
}

.save-note {
  flex: 0 0 auto;
  color: #b7c990;
  font-size: 0.78rem;
}

.screen-shell {
  position: relative;
  display: grid;
  min-height: 320px;
  place-items: center;
  overflow: hidden;
  background: #000;
}

#canvas {
  display: block;
  width: min(100%, 800px) !important;
  height: auto !important;
  max-height: 100vh;
  aspect-ratio: 800 / 570;
  image-rendering: pixelated;
  outline: none;
}

.start-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 0.85rem;
  padding: 1.5rem;
  text-align: center;
  background:
    radial-gradient(circle, rgba(97, 45, 27, 0.48), transparent 60%),
    linear-gradient(rgba(0, 0, 0, 0.45), rgba(0, 0, 0, 0.86));
  transition: opacity 180ms ease;
}

.start-overlay p {
  margin-bottom: 0;
  color: var(--paper-muted);
}

.start-overlay[hidden] {
  display: none;
}

.screen-shell:fullscreen {
  width: 100vw;
  height: 100vh;
  background: #000;
}

.screen-shell:fullscreen #canvas {
  width: auto !important;
  height: 100vh !important;
  max-width: 100vw;
  object-fit: contain;
}

.touch-hud {
  position: fixed;
  inset: 0;
  z-index: 50;
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
}

.touch-hud[hidden] {
  display: none;
}

.rotate-hint {
  position: absolute;
  top: max(0.75rem, env(safe-area-inset-top));
  left: 50%;
  margin: 0;
  padding: 0.4rem 0.7rem;
  border: 1px solid rgba(223, 183, 106, 0.35);
  border-radius: 999px;
  color: rgba(255, 236, 201, 0.82);
  font-size: 0.72rem;
  background: rgba(15, 11, 8, 0.72);
  backdrop-filter: blur(8px);
  transform: translateX(-50%);
  pointer-events: none;
}

.hud-tools {
  position: absolute;
  top: max(0.65rem, env(safe-area-inset-top));
  right: max(0.65rem, env(safe-area-inset-right));
  display: flex;
  gap: 0.45rem;
  pointer-events: auto;
}

.hud-tool {
  display: grid;
  width: 42px;
  min-height: 42px;
  padding: 0;
  place-items: center;
  border-color: rgba(244, 207, 131, 0.42);
  border-radius: 50%;
  color: #f7dfb1;
  font-size: 1.15rem;
  line-height: 1;
  background: rgba(24, 18, 12, 0.76);
  backdrop-filter: blur(8px);
  touch-action: manipulation;
}

.virtual-stick {
  position: absolute;
  left: max(1rem, env(safe-area-inset-left));
  bottom: max(1rem, env(safe-area-inset-bottom));
  width: clamp(128px, 19vw, 176px);
  aspect-ratio: 1;
  border: 2px solid rgba(244, 207, 131, 0.42);
  border-radius: 50%;
  background:
    radial-gradient(circle, rgba(255, 229, 174, 0.12) 0 22%, transparent 23%),
    radial-gradient(circle, rgba(24, 18, 12, 0.76), rgba(38, 29, 20, 0.45) 67%, rgba(223, 183, 106, 0.2) 68% 70%, transparent 71%);
  box-shadow: inset 0 0 1.4rem rgba(0, 0, 0, 0.58), 0 0.6rem 1.8rem rgba(0, 0, 0, 0.28);
  pointer-events: auto;
  touch-action: none;
}

.virtual-stick::before,
.virtual-stick::after {
  position: absolute;
  inset: 14% 49%;
  content: "";
  border-radius: 999px;
  background: rgba(223, 183, 106, 0.13);
}

.virtual-stick::after {
  inset: 49% 14%;
}

.stick-knob {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 46%;
  aspect-ratio: 1;
  border: 1px solid rgba(255, 227, 168, 0.6);
  border-radius: 50%;
  background:
    radial-gradient(circle at 35% 28%, rgba(255, 235, 190, 0.38), transparent 34%),
    linear-gradient(145deg, rgba(128, 95, 54, 0.95), rgba(45, 33, 22, 0.96));
  box-shadow: inset -0.35rem -0.45rem 0.9rem rgba(0, 0, 0, 0.38), 0 0.5rem 1rem rgba(0, 0, 0, 0.45);
  transform: translate(-50%, -50%);
  transition: transform 60ms linear;
  pointer-events: none;
}

.stick-mark {
  position: absolute;
  z-index: 1;
  color: rgba(255, 234, 194, 0.46);
  font-size: 0.62rem;
  pointer-events: none;
}

.stick-up { top: 8%; left: 50%; transform: translateX(-50%); }
.stick-right { top: 50%; right: 8%; transform: translateY(-50%); }
.stick-down { bottom: 8%; left: 50%; transform: translateX(-50%); }
.stick-left { top: 50%; left: 8%; transform: translateY(-50%); }

.action-cluster {
  position: absolute;
  right: max(1rem, env(safe-area-inset-right));
  bottom: max(1rem, env(safe-area-inset-bottom));
  width: clamp(170px, 25vw, 230px);
  height: clamp(140px, 20vw, 188px);
  pointer-events: none;
}

.action-button {
  position: absolute;
  display: grid;
  min-height: 0;
  place-items: center;
  border: 2px solid rgba(255, 220, 151, 0.65);
  border-radius: 50%;
  color: #fff1d3;
  text-shadow: 0 1px 3px #000;
  background:
    radial-gradient(circle at 36% 28%, rgba(255, 226, 169, 0.38), transparent 30%),
    linear-gradient(145deg, rgba(154, 65, 43, 0.94), rgba(79, 24, 17, 0.96));
  box-shadow: inset -0.35rem -0.45rem 0.9rem rgba(0, 0, 0, 0.34), 0 0.5rem 1rem rgba(0, 0, 0, 0.36);
  pointer-events: auto;
  touch-action: none;
}

.action-button.is-pressed {
  filter: brightness(1.3);
  transform: scale(0.92);
}

.action-confirm {
  right: 0;
  bottom: 0;
  width: clamp(78px, 11vw, 108px);
  aspect-ratio: 1;
  font-size: 1.05rem;
}

.action-cancel {
  right: 48%;
  bottom: 5%;
  width: clamp(58px, 8vw, 78px);
  aspect-ratio: 1;
  font-size: 0.8rem;
  background:
    radial-gradient(circle at 36% 28%, rgba(255, 236, 197, 0.3), transparent 30%),
    linear-gradient(145deg, rgba(97, 82, 60, 0.94), rgba(38, 31, 23, 0.96));
}

.action-menu {
  right: 30%;
  top: 0;
  width: clamp(52px, 7vw, 68px);
  aspect-ratio: 1;
  font-size: 0.72rem;
  background:
    radial-gradient(circle at 36% 28%, rgba(226, 235, 255, 0.28), transparent 30%),
    linear-gradient(145deg, rgba(55, 83, 105, 0.94), rgba(24, 35, 45, 0.96));
}

.help-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1rem;
  margin-top: 1rem;
}

.help-grid article {
  padding: 1.1rem 1.25rem;
  background: var(--panel-soft);
}

.help-grid ol,
.help-grid ul {
  margin: 0;
  padding-left: 1.3rem;
  color: var(--paper-muted);
  line-height: 1.8;
}

kbd {
  padding: 0.08rem 0.35rem;
  border: 1px solid #6c5a43;
  border-bottom-width: 2px;
  border-radius: 0.22rem;
  color: var(--paper);
  font: 0.8em ui-monospace, monospace;
  background: #211a12;
}

footer {
  padding: 1.4rem 0.25rem 0;
  font-size: 0.8rem;
  line-height: 1.7;
}

footer p {
  margin-bottom: 0;
}

a {
  color: var(--gold-bright);
}

@media (max-width: 780px) {
  .hero,
  .panel-heading {
    align-items: stretch;
    flex-direction: column;
  }

  .toolbar {
    justify-content: flex-start;
  }

  .screen-shell {
    min-height: 220px;
  }

  .app-shell {
    width: min(100% - 1rem, 1080px);
    padding-top: 1rem;
  }

  .game-panel {
    border-radius: 0.45rem;
  }

  .help-grid {
    grid-template-columns: 1fr;
  }
}

@media (pointer: coarse) {
  body.hud-active {
    overflow: hidden;
    background: #000;
  }

  body.hud-active .app-shell {
    width: 100%;
    height: 100dvh;
    margin: 0;
    padding: 0;
  }

  body.hud-active .hero,
  body.hud-active .panel-heading,
  body.hud-active .help-grid,
  body.hud-active footer {
    display: none;
  }

  body.hud-active .game-panel,
  body.hud-active .screen-shell {
    width: 100vw;
    height: 100dvh;
    min-height: 0;
    border: 0;
    border-radius: 0;
  }

  body.hud-active #canvas {
    width: min(100vw, 800px) !important;
    height: auto !important;
    max-height: 100dvh;
    object-fit: contain;
  }
}

@media (orientation: landscape) and (max-height: 620px) {
  body.hud-active {
    overflow: hidden;
    background: #000;
  }

  body.hud-active .app-shell {
    width: 100%;
    padding: 0;
  }

  body.hud-active .hero,
  body.hud-active .panel-heading,
  body.hud-active .help-grid,
  body.hud-active footer {
    display: none;
  }

  body.hud-active .game-panel,
  body.hud-active .screen-shell {
    width: 100vw;
    height: 100vh;
    min-height: 0;
    border: 0;
    border-radius: 0;
  }

  body.hud-active #canvas {
    width: auto !important;
    height: 100vh !important;
    max-width: 100vw;
    object-fit: contain;
  }

  .rotate-hint {
    display: none;
  }
}

@media (orientation: portrait) and (pointer: coarse) {
  .rotate-hint {
    display: block;
  }
}

@media (orientation: landscape) {
  .rotate-hint {
    display: none;
  }
}
