:root {
  --space: #050b21;
  --space-2: #0b1230;
  --ivory: #fff1d2;
  --gold: #d9a85b;
  --gold-soft: #f1c982;
  --plum: #6f315f;
  --cyan: #80d9ff;
  --panel: rgba(7, 15, 43, 0.86);
  --panel-soft: rgba(16, 20, 54, 0.72);
  --safe-t: max(12px, env(safe-area-inset-top));
  --safe-r: max(12px, env(safe-area-inset-right));
  --safe-b: max(12px, env(safe-area-inset-bottom));
  --safe-l: max(12px, env(safe-area-inset-left));
}

* { box-sizing: border-box; }

html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; background: #020617; }

body {
  color: var(--ivory);
  font-family: Inter, ui-rounded, "Avenir Next", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  touch-action: none;
}

button { font: inherit; }

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

#app {
  width: 100%;
  height: 100dvh;
  display: grid;
  place-items: center;
  background: radial-gradient(circle at 50% 45%, #121b47 0%, #050b21 56%, #020617 100%);
  padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l);
}

#game-shell {
  position: relative;
  width: min(calc(100vw - var(--safe-l) - var(--safe-r)), calc((100dvh - var(--safe-t) - var(--safe-b)) * 16 / 9));
  aspect-ratio: 16 / 9;
  overflow: hidden;
  border: 1px solid rgba(217, 168, 91, 0.65);
  border-radius: clamp(10px, 1.3vw, 22px);
  background: var(--space);
  box-shadow: 0 25px 80px rgba(0, 0, 0, 0.55), inset 0 0 40px rgba(95, 116, 214, 0.08);
  isolation: isolate;
}

#game-shell::after {
  content: "";
  position: absolute;
  inset: 7px;
  border: 1px solid rgba(217, 168, 91, 0.28);
  border-radius: inherit;
  pointer-events: none;
  z-index: 50;
}

#game-canvas { width: 100%; height: 100%; display: block; touch-action: none; }

[hidden] { display: none !important; }

.screen {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(18px, 3vw, 44px);
  text-align: center;
}

#loading-screen { flex-direction: column; background: var(--space); }
.loading-mark { color: var(--gold-soft); font-size: clamp(36px, 6vw, 72px); animation: breathe 1.8s ease-in-out infinite; }
.loading-title { margin: 16px 0 22px; font-weight: 700; letter-spacing: 0.18em; font-size: clamp(13px, 1.4vw, 20px); }
.loading-track { width: min(360px, 65%); height: 6px; border-radius: 999px; background: rgba(255,255,255,.12); overflow: hidden; }
.loading-track span { display: block; width: 0; height: 100%; background: linear-gradient(90deg, var(--gold), var(--cyan)); transition: width .25s ease; }
.microcopy { font-size: 12px; letter-spacing: .12em; opacity: .72; }

.title-screen { align-items: stretch; flex-direction: column; justify-content: space-between; padding: clamp(16px, 2.2vw, 34px) clamp(20px, 4.3vw, 68px) clamp(18px, 3vw, 46px); pointer-events: none; }
.title-screen > * { pointer-events: auto; }
.title-lockup { align-self: center; margin-top: clamp(2px, .8vw, 12px); text-shadow: 0 3px 24px rgba(0,0,0,.78); }
.title-lockup h1 { margin: 0; display: flex; flex-direction: column; line-height: .76; color: var(--ivory); font-family: Georgia, "Times New Roman", serif; font-size: clamp(34px, 6.3vw, 92px); font-weight: 500; letter-spacing: .03em; }
.title-lockup h1 small { font-family: inherit; font-size: .24em; line-height: 1.15; letter-spacing: .38em; color: var(--gold-soft); }
.title-lockup p { margin: clamp(9px, 1.2vw, 18px) 0 0; color: var(--gold-soft); font-size: clamp(11px, 1.45vw, 21px); font-weight: 700; letter-spacing: .36em; }
.title-actions, .complete-actions { display: flex; align-items: center; justify-content: center; gap: clamp(8px, 1.4vw, 22px); }
.title-actions { margin-top: auto; }

.game-button, .sound-button, .icon-button {
  color: var(--ivory);
  border: 2px solid rgba(231, 189, 116, .9);
  background: linear-gradient(180deg, rgba(25, 38, 82, .94), rgba(6, 15, 44, .96));
  box-shadow: inset 0 0 0 2px rgba(255,255,255,.04), 0 8px 26px rgba(0,0,0,.28);
  cursor: pointer;
  min-height: 44px;
  text-transform: uppercase;
  letter-spacing: .12em;
  font-weight: 800;
  transition: transform .16s ease, background .16s ease, box-shadow .16s ease;
}

.sound-button { display: inline-flex; align-items: center; justify-content: center; gap: 7px; }

.game-button { min-width: clamp(160px, 21vw, 315px); padding: clamp(10px, 1.2vw, 18px) clamp(14px, 2.3vw, 34px); border-radius: 18px; font-size: clamp(10px, 1.25vw, 18px); }
.game-button--primary { background: linear-gradient(180deg, rgba(111,49,95,.97), rgba(58,25,68,.98)); }
.game-button:hover, .sound-button:hover, .icon-button:hover { transform: translateY(-2px); box-shadow: 0 10px 30px rgba(68, 117, 210, .24); }
.game-button:active, .sound-button:active, .icon-button:active { transform: translateY(1px); }
.control-hint { align-self: center; margin: clamp(8px, 1vw, 14px) 0 0; font-size: clamp(8px, .9vw, 12px); letter-spacing: .15em; opacity: .72; }

.corner-control { position: absolute; top: clamp(14px, 2vw, 30px); right: clamp(14px, 2vw, 30px); z-index: 3; border-radius: 999px; padding: 10px 18px; font-size: clamp(9px, 1vw, 14px); }

#hud { position: absolute; inset: 0; z-index: 8; pointer-events: none; }
.hud-panel { position: absolute; top: clamp(12px, 1.6vw, 24px); color: var(--ivory); text-shadow: 0 2px 8px rgba(0,0,0,.75); }
.hearts { left: clamp(14px, 2vw, 30px); display: flex; gap: clamp(3px, .5vw, 8px); font-size: clamp(22px, 3.1vw, 44px); line-height: 1; }
.heart { color: var(--ivory); filter: drop-shadow(0 0 6px rgba(217,168,91,.5)); }
.heart--lost { opacity: .22; filter: none; }
.level-display { left: 50%; transform: translateX(-50%); min-width: 36%; text-align: center; display: flex; flex-direction: column; }
.level-display span { font-size: clamp(8px, .9vw, 13px); letter-spacing: .3em; color: var(--gold-soft); }
.level-display strong { font-family: Georgia, serif; font-size: clamp(13px, 1.55vw, 22px); letter-spacing: .08em; font-weight: 500; }
.score-display { right: clamp(62px, 6.4vw, 96px); min-width: clamp(130px, 17vw, 240px); display: grid; gap: 4px; padding: 8px 14px; border: 1px solid rgba(217,168,91,.62); background: var(--panel); border-radius: 16px; font-size: clamp(8px, .9vw, 13px); letter-spacing: .12em; text-align: right; }
.score-display span { display: flex; justify-content: space-between; gap: 14px; }
.score-display strong { color: var(--gold-soft); }
.icon-button { position: absolute; z-index: 9; width: clamp(44px, 4.3vw, 62px); height: clamp(44px, 4.3vw, 62px); border-radius: 16px; padding: 0; pointer-events: auto; font-size: clamp(16px, 2vw, 28px); }
.hud-sound { left: clamp(14px, 2vw, 30px); bottom: clamp(14px, 2vw, 30px); }
.hud-pause { right: clamp(14px, 2vw, 30px); bottom: clamp(14px, 2vw, 30px); }

#toast { position: absolute; left: 50%; bottom: clamp(74px, 10vw, 116px); z-index: 9; transform: translate(-50%, 10px); opacity: 0; pointer-events: none; padding: 10px 18px; border: 1px solid rgba(217,168,91,.62); border-radius: 999px; color: var(--ivory); background: var(--panel); font-weight: 800; letter-spacing: .1em; font-size: clamp(9px, 1vw, 14px); transition: opacity .2s ease, transform .2s ease; }
#toast.toast--show { opacity: 1; transform: translate(-50%, 0); }

.compact-screen { flex-direction: column; background: rgba(3, 8, 28, .72); backdrop-filter: blur(4px); }
.compact-screen p { margin: 0; color: var(--gold-soft); letter-spacing: .3em; font-size: clamp(10px, 1.2vw, 17px); }
.compact-screen h2 { margin: 8px 0; font-family: Georgia, serif; font-weight: 500; font-size: clamp(32px, 5vw, 72px); }
.compact-screen #transition-copy { color: var(--ivory); letter-spacing: .08em; opacity: .84; }
.countdown { margin-top: 18px; font-family: Georgia, serif; color: var(--gold-soft); font-size: clamp(48px, 8vw, 110px); }

.split-screen { justify-content: flex-end; text-align: left; padding-right: clamp(40px, 8vw, 120px); }
.split-copy { width: min(42%, 560px); }
.split-copy h2, .modal h2 { margin: 0 0 12px; font-family: Georgia, serif; color: var(--ivory); font-size: clamp(42px, 7vw, 98px); font-weight: 500; }
.split-copy > p { font-family: Georgia, serif; color: var(--gold-soft); font-size: clamp(14px, 2vw, 28px); line-height: 1.35; }
.result-list, .spec-list, .final-stats { margin: 18px 0; }
.result-list > div, .spec-list > div { display: flex; justify-content: space-between; gap: 24px; padding: 9px 0; border-bottom: 1px solid rgba(217,168,91,.22); }
dt { color: var(--gold-soft); font-size: clamp(9px, 1vw, 14px); letter-spacing: .16em; font-weight: 800; }
dd { margin: 0; color: var(--ivory); font-size: clamp(11px, 1.15vw, 16px); font-weight: 700; }
.stacked-actions { display: grid; gap: 10px; }
.stacked-actions .game-button { width: 100%; }

.complete-screen { display: block; padding: 0; }
.complete-title { position: absolute; z-index: 2; top: clamp(16px, 2vw, 30px); left: 0; right: 0; }
.complete-title h2 { margin: 0; display: flex; flex-direction: column; font-family: Georgia, serif; font-weight: 500; font-size: clamp(30px, 5.3vw, 76px); line-height: .72; }
.complete-title h2 small { display: block; color: var(--gold-soft); font-size: .24em; line-height: 1.15; letter-spacing: .28em; }
.complete-title p { margin: 8px 0 0; color: var(--gold-soft); letter-spacing: .3em; font-weight: 800; font-size: clamp(10px, 1.3vw, 18px); }
.final-stats { position: absolute; z-index: 3; left: clamp(24px, 4vw, 60px); right: clamp(24px, 4vw, 60px); bottom: clamp(82px, 10vw, 118px); display: grid; grid-template-columns: repeat(4, 1fr); gap: 1px; padding: 12px 20px; border: 1px solid rgba(217,168,91,.72); background: var(--panel); border-radius: 18px; }
.final-stats > div { padding: 4px 14px; border-right: 1px solid rgba(217,168,91,.24); }
.final-stats > div:last-child { border-right: 0; }
.final-stats dd { margin-top: 4px; color: var(--gold-soft); }
.complete-actions { position: absolute; z-index: 3; left: clamp(24px, 4vw, 60px); right: clamp(24px, 4vw, 60px); bottom: clamp(18px, 2vw, 28px); }

.modal-backdrop { position: absolute; inset: 0; z-index: 30; display: grid; place-items: center; padding: clamp(18px, 4vw, 60px); background: rgba(2, 6, 23, .78); backdrop-filter: blur(8px); }
.modal { position: relative; width: min(760px, 90%); max-height: 88%; overflow: auto; padding: clamp(24px, 4vw, 56px); border: 1px solid rgba(217,168,91,.78); border-radius: 24px; background: linear-gradient(145deg, rgba(17,26,68,.97), rgba(6,12,36,.98)); box-shadow: 0 28px 80px rgba(0,0,0,.48); text-align: center; }
.modal h2 { font-size: clamp(30px, 4vw, 58px); }
.modal-close { position: absolute; top: 10px; right: 14px; width: 44px; height: 44px; border: 0; color: var(--ivory); background: transparent; font-size: 32px; cursor: pointer; }
.how-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; margin: 22px 0; }
.how-grid div { display: grid; align-content: center; gap: 8px; min-height: 112px; padding: 12px; border: 1px solid rgba(217,168,91,.25); border-radius: 16px; background: rgba(255,255,255,.035); }
.how-grid span { color: var(--gold-soft); font-size: 28px; }
.how-grid strong { font-size: 9px; letter-spacing: .1em; }
.keyboard-map { font-size: 10px; letter-spacing: .13em; opacity: .68; }
.specs-modal { width: min(620px, 90%); }
.spec-list { text-align: left; }
.spec-list dd { text-align: right; }

#portrait-hint { display: none; position: absolute; inset: 0; z-index: 45; place-items: center; align-content: center; gap: 18px; padding: 28px; color: var(--ivory); background: rgba(4,9,30,.95); text-align: center; }
#portrait-hint span { color: var(--gold-soft); font-size: 52px; }
#portrait-hint strong { max-width: 300px; line-height: 1.5; }

@keyframes breathe { 50% { transform: scale(1.12) rotate(8deg); filter: drop-shadow(0 0 18px rgba(128,217,255,.65)); } }

@media (orientation: portrait) and (max-width: 600px) {
  #portrait-hint { display: grid; }
}

@media (max-height: 500px) {
  #game-shell { border-radius: 8px; }
  .title-lockup h1 { font-size: clamp(28px, 9.6vh, 54px); }
  .title-lockup p { margin-top: 6px; }
  .game-button { padding-block: 8px; border-radius: 12px; }
  .control-hint { display: none; }
  .how-grid { margin: 8px 0; }
  .how-grid div { min-height: 76px; }
  .modal { padding: 18px 34px; }
  .spec-list > div { padding: 5px 0; }
  .final-stats { bottom: 66px; padding-block: 7px; }
  .complete-actions { bottom: 10px; }
}

@media (max-width: 760px) {
  .title-actions, .complete-actions { gap: 6px; }
  .game-button { min-width: 0; flex: 1; letter-spacing: .08em; }
  .score-display { right: 66px; }
  .how-grid { grid-template-columns: repeat(3, 1fr); }
}

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