:root {
  --night: #070a0e;
  --deep: #111923;
  --deep-soft: #1b2733;
  --walnut: #5b3727;
  --honey: #c58a4a;
  --chalk: #edf0f2;
  --chalk-dim: #b9c1c8;
  --glass: #b8d9f0;
  --ember: #e86d4b;
  --line: rgba(238, 230, 209, 0.2);
  --display: "Iowan Old Style", "Palatino Linotype", "Noto Serif TC", "Songti TC", serif;
  --body: Inter, "Segoe UI", "PingFang TC", "Noto Sans TC", sans-serif;
  --utility: "IBM Plex Mono", "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  min-height: 100%;
  margin: 0;
  background: var(--night);
}

body {
  min-height: 100dvh;
  overflow: hidden;
  color: var(--chalk);
  font-family: var(--body);
  -webkit-font-smoothing: antialiased;
}

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

button,
select {
  color: inherit;
  font: inherit;
}

button:focus-visible,
a:focus-visible,
select:focus-visible,
canvas:focus-visible {
  outline: 2px solid var(--glass);
  outline-offset: 4px;
}

[hidden] {
  display: none !important;
}

.skip-link {
  position: fixed;
  z-index: 100;
  top: 12px;
  left: 12px;
  padding: 10px 14px;
  color: var(--night);
  background: var(--chalk);
  transform: translateY(-160%);
  transition: transform 160ms ease;
}

.skip-link:focus {
  transform: translateY(0);
}

.app-shell {
  display: grid;
  grid-template-columns: clamp(260px, 25vw, 350px) minmax(0, 1fr);
  width: 100%;
  height: 100dvh;
  min-height: 560px;
  background:
    radial-gradient(circle at 82% 12%, rgba(184, 217, 240, 0.07), transparent 28%),
    linear-gradient(145deg, #0c1117, #05070a 72%);
}

.identity-panel {
  position: relative;
  z-index: 2;
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: max(24px, env(safe-area-inset-top)) clamp(22px, 3vw, 44px) max(22px, env(safe-area-inset-bottom));
  overflow: hidden;
  border-right: 1px solid var(--line);
  background: linear-gradient(160deg, rgba(16, 23, 31, 0.96), rgba(5, 8, 12, 0.94));
}

.home-link {
  position: relative;
  z-index: 2;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  width: max-content;
  color: var(--chalk-dim);
  font-family: var(--utility);
  font-size: 0.66rem;
  letter-spacing: 0.12em;
  text-decoration: none;
  transition: color 160ms ease;
}

.home-link:hover {
  color: var(--glass);
}

.home-link > span:first-child {
  color: var(--ember);
  font-size: 1rem;
}

.title-block {
  position: relative;
  z-index: 2;
  margin-top: clamp(54px, 10vh, 110px);
}

.eyebrow,
.loading-kicker,
.error-state > p,
.complete-panel > p {
  margin: 0;
  color: var(--glass);
  font-family: var(--utility);
  font-size: 0.66rem;
  letter-spacing: 0.16em;
}

.title-block h1 {
  display: flex;
  flex-direction: column;
  margin: 14px 0 16px;
  font-family: var(--display);
  font-weight: 500;
  line-height: 0.9;
  letter-spacing: -0.035em;
}

.title-block h1 span {
  color: var(--chalk);
  font-size: clamp(2.5rem, 4.4vw, 4.7rem);
  white-space: nowrap;
}

.title-block h1 strong {
  margin-top: 9px;
  color: var(--honey);
  font-size: clamp(4.2rem, 7vw, 5.8rem);
  font-style: italic;
  font-weight: 600;
}

.lead {
  max-width: 25ch;
  margin: 0;
  color: var(--chalk-dim);
  font-family: var(--display);
  font-size: 0.93rem;
  line-height: 1.8;
}

.score-ledger {
  position: relative;
  z-index: 2;
  margin-top: auto;
  padding-top: 22px;
  border-top: 1px solid var(--line);
}

.score-ledger > p {
  margin: 0 0 12px;
  color: var(--chalk-dim);
  font-family: var(--utility);
  font-size: 0.65rem;
  letter-spacing: 0.14em;
}

.score-ledger dl {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin: 0;
}

.score-ledger dl > div {
  min-width: 0;
}

.score-ledger dt {
  color: var(--chalk-dim);
  font-size: 0.7rem;
}

.score-ledger dd {
  display: flex;
  align-items: baseline;
  gap: 4px;
  margin: 4px 0 0;
  font-family: var(--display);
}

.score-ledger dd span {
  font-size: clamp(1.9rem, 3.4vw, 3rem);
  font-variant-numeric: tabular-nums;
}

.score-ledger dd small {
  color: var(--honey);
  font-size: 0.67rem;
}

.capture-track {
  --target-count: 1;
  display: grid;
  grid-template-columns: repeat(var(--target-count), minmax(0, 1fr));
  gap: 4px;
  margin-top: 16px;
}

.capture-track i {
  display: block;
  height: 3px;
  background: rgba(238, 230, 209, 0.15);
  transition: background 220ms ease, transform 220ms ease;
}

.capture-track i.is-captured {
  background: var(--ember);
  transform: translateY(-2px);
}

.desk-note {
  position: relative;
  z-index: 2;
  margin: 22px 0 0;
  color: var(--chalk-dim);
  font-size: 0.72rem;
  line-height: 1.7;
}

.model-note {
  position: relative;
  z-index: 2;
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin: 18px 0 0;
  color: rgba(238, 230, 209, 0.4);
  font-family: var(--utility);
  font-size: 0.55rem;
  letter-spacing: 0.08em;
}

.identity-mark {
  position: absolute;
  z-index: 0;
  top: 29%;
  right: -0.2em;
  color: rgba(197, 138, 74, 0.055);
  font-family: var(--display);
  font-size: min(34vw, 390px);
  font-style: italic;
  line-height: 1;
  pointer-events: none;
  transform: rotate(-7deg);
  user-select: none;
}

.game-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 12px;
  min-width: 0;
  min-height: 0;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) 16px;
}

.game-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 42px;
  padding: 0 4px 0 8px;
}

.stage-id {
  display: flex;
  align-items: center;
  gap: 10px;
}

.stage-id p {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin: 0;
}

.stage-id b {
  font-family: var(--display);
  font-size: 0.93rem;
  font-weight: 500;
}

.stage-id small {
  color: var(--chalk-dim);
  font-family: var(--utility);
  font-size: 0.54rem;
  letter-spacing: 0.1em;
}

.live-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--ember);
  box-shadow: 0 0 0 5px rgba(232, 109, 75, 0.11);
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 5px;
  min-width: 0;
}

.target-count-control {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: 44px;
  padding-left: 9px;
  border-bottom: 1px solid rgba(238, 230, 209, 0.16);
  color: var(--chalk-dim);
  background: transparent;
}

.target-count-control > span {
  font-size: 0.7rem;
  white-space: nowrap;
}

.target-count-control select {
  min-width: 66px;
  min-height: 44px;
  padding: 0 5px;
  border: 0;
  color: var(--chalk);
  background: transparent;
  color-scheme: dark;
  cursor: pointer;
}

.target-count-control:focus-within {
  border-color: var(--glass);
}

.target-count-control select:disabled {
  color: var(--chalk-dim);
  cursor: not-allowed;
  opacity: 0.62;
}

.target-count-control option {
  color: var(--chalk);
  background: var(--deep);
}

.text-button {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 38px;
  padding: 0 11px;
  border: 0;
  border-bottom: 1px solid transparent;
  color: var(--chalk-dim);
  background: transparent;
  cursor: pointer;
  transition: color 160ms ease, border-color 160ms ease;
}

.text-button:hover {
  border-color: var(--glass);
  color: var(--chalk);
}

.text-button span {
  color: var(--honey);
  font-size: 1rem;
}

.text-button b {
  font-size: 0.7rem;
  font-weight: 500;
}

.scene-frame {
  position: relative;
  min-height: 0;
  overflow: hidden;
  border: 1px solid rgba(238, 230, 209, 0.16);
  border-radius: 4px 14px 4px 4px;
  background: #080b10;
  box-shadow: inset 0 0 8px rgba(0, 0, 0, 0.28);
  isolation: isolate;
}

.scene-frame::before,
.scene-frame::after {
  position: absolute;
  z-index: 1;
  width: 46px;
  height: 46px;
  border-color: rgba(238, 230, 209, 0.22);
  content: "";
  pointer-events: none;
}

.scene-frame::before {
  top: 14px;
  left: 14px;
  border-top: 1px solid;
  border-left: 1px solid;
}

.scene-frame::after {
  right: 14px;
  bottom: 14px;
  border-right: 1px solid;
  border-bottom: 1px solid;
}

#game-canvas {
  position: absolute;
  z-index: 0;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
  touch-action: none;
}

#game-canvas.is-aiming {
  cursor: grabbing;
}

.loading-state,
.error-state {
  position: absolute;
  z-index: 12;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: 30px;
  text-align: center;
  background: rgba(7, 10, 14, 0.96);
  backdrop-filter: blur(8px);
  transition: opacity 400ms ease, visibility 400ms ease;
}

.loading-state.is-complete {
  visibility: hidden;
  opacity: 0;
  pointer-events: none;
}

.loading-orbit {
  position: relative;
  width: 86px;
  height: 38px;
  margin-bottom: 24px;
}

.loading-orbit::after {
  position: absolute;
  right: 0;
  bottom: 2px;
  left: 0;
  height: 1px;
  background: rgba(238, 230, 209, 0.24);
  content: "";
}

.loading-orbit i {
  position: absolute;
  bottom: 4px;
  width: 22px;
  height: 22px;
  border: 1px solid rgba(255, 255, 255, 0.55);
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #dffdf9, #5fa9a8 24%, #284b55 68%);
  box-shadow: inset -4px -4px 7px rgba(0, 0, 0, 0.35), 0 5px 8px rgba(0, 0, 0, 0.4);
  animation: loading-roll 1.5s cubic-bezier(0.45, 0, 0.55, 1) infinite alternate;
}

.loading-orbit i:nth-child(1) {
  left: 5px;
}

.loading-orbit i:nth-child(2) {
  left: 32px;
  animation-delay: -0.32s;
  filter: hue-rotate(70deg);
}

.loading-orbit i:nth-child(3) {
  left: 59px;
  animation-delay: -0.64s;
  filter: hue-rotate(150deg);
}

.loading-state h2,
.error-state h2 {
  margin: 12px 0 7px;
  font-family: var(--display);
  font-size: clamp(1.65rem, 3vw, 2.5rem);
  font-weight: 500;
}

.loading-state > p:not(.loading-kicker),
.error-state > span {
  color: var(--chalk-dim);
  font-size: 0.74rem;
}

.loading-track {
  width: min(280px, 72vw);
  height: 2px;
  margin-top: 22px;
  overflow: hidden;
  background: rgba(238, 230, 209, 0.14);
}

.loading-track span {
  display: block;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, var(--honey), var(--glass));
  transform: scaleX(0);
  transform-origin: left;
  transition: transform 160ms ease;
}

.error-state > p {
  color: var(--ember);
}

.error-state button,
.complete-panel button {
  min-width: 130px;
  min-height: 44px;
  margin-top: 22px;
  padding: 0 20px;
  border: 1px solid var(--honey);
  color: var(--chalk);
  background: transparent;
  cursor: pointer;
}

.status-pill {
  position: absolute;
  z-index: 4;
  top: 22px;
  left: 50%;
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 34px;
  padding: 0 12px;
  border: 1px solid rgba(238, 230, 209, 0.16);
  color: var(--chalk-dim);
  background: rgba(7, 17, 21, 0.66);
  backdrop-filter: blur(10px);
  transform: translateX(-50%);
  pointer-events: none;
}

.status-pill span {
  color: var(--glass);
  font-family: var(--utility);
  font-size: 0.55rem;
  letter-spacing: 0.1em;
}

.status-pill b {
  font-size: 0.67rem;
  font-weight: 500;
  white-space: nowrap;
}

.mobile-ledger {
  display: none;
}

.power-gauge {
  position: absolute;
  z-index: 4;
  right: 24px;
  bottom: 26px;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  pointer-events: none;
}

.power-gauge p {
  margin: 0 0 -3px;
  color: var(--chalk-dim);
  font-size: 0.62rem;
}

.power-gauge p span {
  display: block;
  margin-bottom: 1px;
}

.power-gauge p b {
  color: var(--chalk);
  font-family: var(--display);
  font-size: 1.6rem;
  font-variant-numeric: tabular-nums;
  font-weight: 500;
}

.power-gauge p small {
  color: var(--honey);
}

.power-gauge > div {
  position: relative;
  width: 4px;
  height: 104px;
  overflow: hidden;
  background: rgba(238, 230, 209, 0.14);
}

.power-gauge i {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  height: 100%;
  background: linear-gradient(to top, var(--glass), var(--honey) 62%, var(--ember));
  transform: scaleY(0.55);
  transform-origin: bottom;
  transition: transform 100ms linear;
}

.shot-toast {
  position: absolute;
  z-index: 8;
  top: 50%;
  left: 50%;
  color: var(--chalk);
  font-family: var(--display);
  font-size: clamp(1.25rem, 3vw, 2.2rem);
  text-shadow: 0 3px 14px rgba(0, 0, 0, 0.65);
  opacity: 0;
  transform: translate(-50%, -20%);
  transition: opacity 180ms ease, transform 320ms ease;
  pointer-events: none;
}

.shot-toast.is-visible {
  opacity: 1;
  transform: translate(-50%, -50%);
}

.complete-panel {
  position: absolute;
  z-index: 15;
  top: 50%;
  left: 50%;
  width: min(390px, calc(100% - 42px));
  padding: clamp(26px, 5vw, 46px);
  border: 1px solid rgba(238, 230, 209, 0.32);
  color: var(--chalk);
  text-align: center;
  background:
    linear-gradient(rgba(91, 55, 39, 0.92), rgba(46, 31, 26, 0.96)),
    var(--walnut);
  box-shadow: 0 25px 90px rgba(0, 0, 0, 0.65);
  transform: translate(-50%, -50%);
}

.complete-panel h2 {
  margin: 10px 0 12px;
  color: var(--honey);
  font-family: var(--display);
  font-size: clamp(3.7rem, 8vw, 6.5rem);
  font-style: italic;
  font-weight: 600;
  line-height: 0.9;
}

.complete-panel > span {
  color: var(--chalk-dim);
  font-size: 0.8rem;
  line-height: 1.7;
}

.complete-panel > span b {
  color: var(--chalk);
  font-family: var(--display);
  font-size: 1.2em;
}

.corner-stamp {
  position: absolute;
  z-index: 2;
  bottom: 22px;
  left: 24px;
  margin: 0;
  color: rgba(238, 230, 209, 0.38);
  font-family: var(--utility);
  font-size: 0.53rem;
  letter-spacing: 0.12em;
  line-height: 1.5;
  pointer-events: none;
}

.control-strip {
  display: grid;
  grid-template-columns: auto auto 1fr;
  gap: clamp(18px, 4vw, 54px);
  min-height: 44px;
  padding: 2px 8px 0;
}

.control-strip p {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin: 0;
  white-space: nowrap;
}

.control-strip b {
  color: var(--honey);
  font-family: var(--display);
  font-size: 0.82rem;
  font-weight: 500;
}

.control-strip span {
  color: var(--chalk-dim);
  font-size: 0.64rem;
}

.keyboard-help {
  justify-self: end;
}

.visually-hidden {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

@keyframes loading-roll {
  from { transform: translateY(0) rotate(0deg); }
  to { transform: translateY(-8px) rotate(180deg); }
}

@media (max-width: 1080px) {
  .app-shell {
    grid-template-columns: 250px minmax(0, 1fr);
  }

  .identity-panel {
    padding-right: 24px;
    padding-left: 24px;
  }

  .keyboard-help {
    display: none !important;
  }

  .control-strip {
    grid-template-columns: auto auto;
  }
}

@media (max-width: 780px) {
  .app-shell {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr);
    min-height: 480px;
  }

  .identity-panel {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    align-items: center;
    gap: 8px 18px;
    padding: max(11px, env(safe-area-inset-top)) max(15px, env(safe-area-inset-right)) 10px max(15px, env(safe-area-inset-left));
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .home-link {
    grid-row: 1;
    grid-column: 1;
  }

  .home-link span:last-child {
    display: none;
  }

  .title-block {
    grid-row: 1;
    grid-column: 2;
    display: flex;
    align-items: baseline;
    gap: 9px;
    margin: 0;
  }

  .eyebrow,
  .lead,
  .score-ledger,
  .desk-note,
  .model-note,
  .identity-mark {
    display: none;
  }

  .title-block h1 {
    display: flex;
    flex-direction: row;
    align-items: baseline;
    gap: 8px;
    margin: 0;
    line-height: 1;
  }

  .title-block h1 span {
    font-size: clamp(1.25rem, 5vw, 1.75rem);
  }

  .title-block h1 strong {
    margin: 0;
    font-size: clamp(1.55rem, 6vw, 2.1rem);
  }

  .game-panel {
    gap: 7px;
    padding: 8px max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  }

  .game-topbar {
    min-height: 36px;
  }

  .stage-id small {
    display: none;
  }

  .text-button {
    min-width: 44px;
    min-height: 44px;
    justify-content: center;
    padding: 0 9px;
  }

  .scene-frame {
    border-radius: 3px 12px 3px 3px;
  }

  .mobile-ledger {
    position: absolute;
    z-index: 4;
    top: 12px;
    left: 14px;
    display: flex;
    gap: 13px;
    color: var(--chalk-dim);
    font-size: 0.6rem;
    pointer-events: none;
  }

  .mobile-ledger b {
    margin-left: 3px;
    color: var(--chalk);
    font-family: var(--display);
    font-size: 1.15em;
    font-variant-numeric: tabular-nums;
  }

  .status-pill {
    top: auto;
    bottom: 18px;
    max-width: calc(100% - 120px);
  }

  .status-pill span {
    display: none;
  }

  .status-pill b {
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .power-gauge {
    right: 16px;
    bottom: 18px;
  }

  .power-gauge > div {
    height: 64px;
  }

  .power-gauge p b {
    font-size: 1.25rem;
  }

  .corner-stamp {
    display: none;
  }

  .control-strip {
    display: none;
  }
}

@media (max-width: 560px) {
  .top-actions .text-button b {
    display: none;
  }

  .top-actions .text-button span {
    font-size: 1.1rem;
  }
}

@media (max-width: 430px) {
  .stage-id p b {
    font-size: 0.8rem;
  }

  .text-button b {
    display: none;
  }

  .text-button span {
    font-size: 1.1rem;
  }

  .top-actions {
    gap: 2px;
  }

  .target-count-control {
    gap: 0;
    padding-left: 0;
  }

  .target-count-control > span {
    display: none;
  }

  .target-count-control select {
    min-width: 62px;
    padding-right: 2px;
    padding-left: 2px;
  }

  .mobile-ledger {
    gap: 9px;
  }
}

@media (max-height: 640px) and (min-width: 781px) {
  .app-shell {
    min-height: 0;
  }

  .identity-panel {
    padding: 14px 20px;
  }

  .title-block {
    margin-top: 20px;
  }

  .title-block h1 span {
    font-size: 2.35rem;
  }

  .title-block h1 strong {
    font-size: 3.9rem;
  }

  .lead,
  .desk-note {
    display: none;
  }

  .score-ledger {
    margin-top: auto;
    padding-top: 12px;
  }

  .game-panel {
    gap: 5px;
    padding-top: 6px;
    padding-bottom: 6px;
  }

  .game-topbar {
    min-height: 32px;
  }

  .control-strip {
    display: none;
  }
}

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