:root {
  color-scheme: dark;
  --paper: #fffaf0;
  --amber: #f2b653;
  --focus: #ffe6a8;
  font-family: "Avenir Next", "Segoe UI", "Noto Sans SC", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: #17100b;
  color: var(--paper);
  -webkit-tap-highlight-color: transparent;
}

body { touch-action: none; }

#app {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

#scene {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
}

#scene:active { cursor: grabbing; }

.overlay { position: absolute; inset: 0; pointer-events: none; }
.hidden { display: none !important; }

button {
  font: inherit;
  color: inherit;
  border: 0;
  cursor: pointer;
}

button:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 3px;
}

#hud { z-index: 5; }

.hud-column {
  position: absolute;
  top: max(16px, env(safe-area-inset-top));
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: none;
}

.hud-left { left: max(16px, env(safe-area-inset-left)); width: min(360px, 42vw); }
.hud-right { right: max(16px, env(safe-area-inset-right)); align-items: flex-end; }

.hud-card {
  border-radius: 16px;
  background: linear-gradient(145deg, rgba(64,45,30,.82), rgba(31,22,15,.78));
  border: 1px solid rgba(255,226,164,.24);
  box-shadow: 0 14px 34px rgba(0,0,0,.28);
  padding: 14px 16px;
  backdrop-filter: blur(10px);
}

.hud-card.compact { padding: 10px 12px; }
.hud-kicker { font-size: 11px; letter-spacing: .18em; color: var(--amber); font-weight: 800; }
.hud-title { font-size: clamp(19px, 2.6vw, 28px); font-weight: 800; margin-top: 2px; }
.objective { font-size: clamp(12px, 1.55vw, 15px); line-height: 1.4; color: #f5e7d1; margin-top: 7px; max-width: 34ch; }

.progress-row { display: flex; align-items: center; gap: 10px; margin-top: 11px; }
.progress-track { flex: 1; height: 9px; border-radius: 99px; background: rgba(0,0,0,.36); overflow: hidden; }
.progress-fill { height: 100%; width: 0%; border-radius: 99px; background: linear-gradient(90deg, var(--amber), #ffe8a2); transition: width .18s ease; }
.progress-row strong { min-width: 42px; text-align: right; font-size: 14px; }
.next-row { margin-top: 7px; font-size: 12px; color: #ffd98d; }

.status-grid {
  display: grid;
  grid-template-columns: auto auto;
  gap: 3px 10px;
  font-size: 12px;
  color: #e6d4bb;
}
.status-grid strong { color: #fff6de; font-variant-numeric: tabular-nums; }

.hud-buttons { display: flex; gap: 8px; pointer-events: auto; }
.icon-button {
  width: 43px;
  height: 43px;
  border-radius: 14px;
  background: rgba(35,25,18,.80);
  border: 1px solid rgba(255,226,164,.26);
  color: #ffe6a8;
  font-size: 16px;
  font-weight: 800;
  display: grid;
  place-items: center;
}
.icon-button:active { transform: translateY(1px); background: rgba(62,43,28,.92); }

.toast {
  position: absolute;
  left: 50%;
  top: max(20px, env(safe-area-inset-top));
  transform: translateX(-50%);
  padding: 9px 16px;
  border-radius: 99px;
  background: rgba(27,19,13,.84);
  color: #ffe7a8;
  border: 1px solid rgba(255,226,164,.30);
  font-size: 14px;
  font-weight: 700;
}

.damage-flash {
  position: absolute;
  inset: 0;
  background: radial-gradient(circle, transparent 45%, rgba(214,72,49,.48));
  animation: flash .36s ease-out;
}

@keyframes flash {
  from { opacity: 1; } to { opacity: 0; }
}

.touch-controls {
  position: absolute;
  left: max(16px, env(safe-area-inset-left));
  right: max(16px, env(safe-area-inset-right));
  bottom: max(16px, env(safe-area-inset-bottom));
  display: none;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  pointer-events: none;
}

.dpad { display: grid; grid-template-columns: repeat(3, 58px); grid-template-rows: repeat(2, 58px); gap: 7px; pointer-events: auto; }
.dpad .up { grid-column: 2 / 3; grid-row: 1 / 2; }
.dpad .left { grid-column: 1 / 2; grid-row: 2 / 3; }
.dpad .right { grid-column: 3 / 4; grid-row: 2 / 3; }
.dpad .down { grid-column: 2 / 3; grid-row: 2 / 3; }
.dpad-middle { display: contents; }

.touch-button {
  width: 58px;
  height: 58px;
  border-radius: 20px;
  background: rgba(34,24,17,.75);
  border: 1px solid rgba(255,226,164,.31);
  color: #ffe6a8;
  font-size: 20px;
  font-weight: 800;
  display: grid;
  place-items: center;
  user-select: none;
  backdrop-filter: blur(8px);
}

.touch-button.action {
  width: 88px;
  height: 88px;
  border-radius: 50%;
  font-size: 13px;
  letter-spacing: .08em;
  pointer-events: auto;
}

.touch-button.active { background: rgba(239,170,68,.92); color: #341f10; transform: translateY(1px); }

@media (hover: none), (pointer: coarse), (max-width: 720px) {
  .touch-controls { display: flex; }
  .hud-left { width: min(310px, 72vw); }
  .hud-card { padding: 11px 12px; border-radius: 14px; }
  .objective { font-size: 12px; }
  .next-row { display: none; }
  .touch-button { width: min(64px, 16vw); height: min(64px, 16vw); border-radius: 22px; }
  .touch-button.action { width: min(88px, 22vw); height: min(88px, 22vw); }
}

.menu-screen {
  z-index: 10;
  pointer-events: auto;
  display: grid;
  place-items: center;
  padding: 22px;
  background:
    radial-gradient(circle at 15% 15%, rgba(255,206,116,.20), transparent 34%),
    linear-gradient(90deg, rgba(21,15,10,.84), rgba(21,15,10,.58) 48%, rgba(21,15,10,.24));
}

.menu-shell {
  width: min(1100px, 100%);
  max-height: calc(100dvh - 44px);
  overflow: auto;
  display: grid;
  gap: 20px;
}

.menu-header { display: flex; gap: 20px; align-items: center; }
.brand-mark {
  width: 64px; height: 64px; flex: 0 0 auto;
  border-radius: 20px;
  background: linear-gradient(135deg, #fff8e8 0 42%, #efdcb9 42% 52%, #ffe9bb 52% 100%);
  box-shadow: 0 14px 32px rgba(0,0,0,.38), inset 0 -7px 0 rgba(154,103,44,.28);
  position: relative;
  transform: rotate(8deg);
}
.brand-mark span {
  position:absolute; inset: 12px 10px;
  background:
    linear-gradient(115deg, transparent 0 45%, rgba(118,76,32,.34) 45% 52%, transparent 52%),
    linear-gradient(25deg, transparent 0 49%, rgba(118,76,32,.20) 49% 56%, transparent 56%);
  clip-path: polygon(0 100%, 48% 0, 100% 78%, 54% 100%);
}
.eyebrow { margin: 0; font-size: 11px; letter-spacing: .23em; color: #ffd68a; font-weight: 800; }
h1 { margin: 5px 0 6px; font-size: clamp(36px, 6vw, 62px); line-height: .98; letter-spacing: -.03em; }
.tagline { margin: 0; max-width: 62ch; color: #ecdbc3; font-size: clamp(14px, 1.8vw, 18px); }

.level-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 13px; }
.level-card {
  text-align: left;
  min-height: 170px;
  border-radius: 18px;
  padding: 16px;
  background: rgba(34,24,17,.68);
  border: 1px solid rgba(255,226,164,.20);
  box-shadow: 0 16px 34px rgba(0,0,0,.24);
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: transform .15s ease, border-color .15s ease, background .15s ease;
}
.level-card:not(:disabled):hover { transform: translateY(-2px); border-color: rgba(255,220,126,.55); }
.level-card.selected { border-color: #ffd57a; background: linear-gradient(145deg, rgba(94,60,30,.86), rgba(35,24,16,.80)); box-shadow: 0 0 0 2px rgba(255,207,104,.26), 0 16px 34px rgba(0,0,0,.26); }
.level-card:disabled { opacity: .48; cursor: not-allowed; }
.level-card .card-number { font-size: 11px; letter-spacing: .16em; color: #ffca70; font-weight: 800; }
.level-card h3 { margin: 0; font-size: 20px; }
.level-card p { margin: 0; color: #e9d7bd; font-size: 13px; line-height: 1.45; }
.card-goal { margin-top: auto; display: flex; justify-content: space-between; gap: 8px; color: #ffd98d; font-size: 12px; font-weight: 700; }
.card-goal .best { color: #d9e8c9; }

.menu-actions { display: flex; gap: 11px; flex-wrap: wrap; align-items: center; }
.primary-button, .secondary-button {
  min-height: 48px;
  border-radius: 16px;
  padding: 0 23px;
  font-size: 15px;
  font-weight: 800;
}
.primary-button { background: linear-gradient(145deg,#ffd36d,#e68d27); color: #34200f; box-shadow: 0 13px 27px rgba(172,100,25,.35); }
.secondary-button { background: rgba(255,238,204,.12); border: 1px solid rgba(255,226,164,.34); color: #ffe9bb; }
.primary-button:active, .secondary-button:active { transform: translateY(1px); }

.controls-help {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}
.controls-help div {
  border-radius: 14px;
  padding: 12px 14px;
  background: rgba(23,16,11,.62);
  border: 1px solid rgba(255,226,164,.14);
  display: grid;
  gap: 4px;
}
.controls-help strong { color: #ffd68a; font-size: 12px; letter-spacing: .10em; }
.controls-help span { color: #e6d4bb; font-size: 12.5px; line-height: 1.45; }

.countdown-screen, .modal-screen { z-index: 20; pointer-events: auto; display: grid; place-items: center; background: rgba(16,11,7,.52); }
.countdown-card, .modal-card {
  width: min(480px, calc(100vw - 36px));
  border-radius: 24px;
  padding: 28px;
  text-align: center;
  background: rgba(29,21,15,.90);
  border: 1px solid rgba(255,226,164,.24);
  box-shadow: 0 25px 70px rgba(0,0,0,.45);
}
.countdown-number { font-size: 82px; font-weight: 900; color: #ffd67b; line-height: 1; }
.countdown-card p { margin: 10px 0 0; color: #ecdbc3; }
.modal-card h2 { margin: 4px 0 10px; font-size: 34px; }
.modal-card p { margin: 0 0 18px; color: #e8d6bd; line-height: 1.5; }
.modal-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; }
.result-kicker { margin:0; font-size: 11px; letter-spacing: .2em; color: #ffd68a; font-weight: 800; }
.result-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 9px; margin: 4px 0 20px; }
.result-stats div { border-radius: 13px; background: rgba(255,238,204,.09); padding: 10px 8px; }
.result-stats span { display:block; font-size: 11px; color:#dcc8ac; }
.result-stats strong { font-size: 19px; }

@media (max-width: 880px) {
  .menu-header { align-items: flex-start; }
  .level-grid, .controls-help { grid-template-columns: 1fr; }
  .menu-screen { padding: 16px; overflow: auto; }
  .menu-shell { max-height: none; }
  .level-card { min-height: 0; }
}

@media (max-width: 520px) {
  .brand-mark { width: 52px; height: 52px; border-radius: 17px; }
  h1 { font-size: 38px; }
  .tagline { font-size: 13px; }
  .menu-shell { gap: 14px; }
  .level-card { padding: 13px; }
  .result-stats { grid-template-columns: 1fr; }
  .primary-button, .secondary-button { width: 100%; }
  .menu-actions { display: grid; }
  .level-card { padding: 11px; gap: 4px; }
  .level-card p { font-size: 12px; }
}
