:root {
  --bg: #121a4a;
  --panel: #1c2560;
  --line: #4352a6;
  --text: #f2f6ff;
  --dim: #b4bff0;
  --accent: #4de2ff;
  --warn: #ffd23c;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: linear-gradient(160deg, #1a2566 0%, #121a4a 50%, #231a5e 100%) fixed var(--bg);
  color: var(--text);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, "Roboto Mono", monospace;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

/* Three columns: stats on the left, the field centred, buttons on the right.
   The d-pad and the cut button sit in the bottom corners of the side columns. */
#app {
  --side: clamp(126px, 17vw, 176px);
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-columns: var(--side) minmax(0, 1fr) var(--side);
  grid-template-rows: minmax(0, 1fr);
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  touch-action: none;
}

#left, #right { min-width: 0; padding: 10px; display: flex; flex-direction: column; }
#left { padding-right: 2px; }
#right { padding-left: 2px; align-items: flex-end; }

#hud { display: flex; flex-direction: column; gap: 8px; }

.stat { display: flex; align-items: baseline; justify-content: space-between; gap: 6px; font-size: 16px; font-weight: 700; min-width: 0; }
.stat .label { font-size: 10px; letter-spacing: .12em; color: var(--dim); font-weight: 400; }
#hudLives { color: #ff5d7a; letter-spacing: .05em; }

.hud-buttons { display: flex; gap: 8px; }
.hud-buttons button {
  width: 40px; height: 36px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  font: inherit; font-size: 17px;
}
.hud-buttons button.off { color: var(--dim); text-decoration: line-through; }

#bar {
  position: relative;
  height: 18px;
  margin-top: 2px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--panel);
  overflow: hidden;
}
#barFill { position: absolute; inset: 0 auto 0 0; width: 0; background: linear-gradient(90deg, #1b7fa8, var(--accent)); box-shadow: 0 0 12px rgba(77, 226, 255, .6); transition: width .25s; }
#barGoal { position: absolute; top: 0; bottom: 0; left: 90%; width: 2px; background: var(--warn); }
#barText { position: absolute; inset: 0; display: grid; place-items: center; font-size: 10px; letter-spacing: .04em; text-shadow: 0 1px 2px #000; }

#stage { position: relative; min-width: 0; min-height: 0; touch-action: none; }
#game { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

#overlay {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  padding: 16px;
  background: rgba(18, 26, 74, .5);
  -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px);
}
#overlay.hidden { display: none; }

#panel {
  width: min(340px, 100%);
  padding: 22px 20px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: linear-gradient(180deg, #26317c, #1a2260);
  box-shadow: 0 0 0 1px rgba(77, 226, 255, .14), 0 18px 50px rgba(5, 8, 40, .45), 0 0 44px rgba(77, 226, 255, .2);
  text-align: center;
}
#panel h1 {
  margin: 0 0 10px; font-size: 30px; letter-spacing: .16em; color: var(--accent);
  background: linear-gradient(90deg, #4de2ff, #b28cff 55%, #ff5d9e);
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent;
}
#panel p { margin: 0 0 16px; font-size: 13px; line-height: 1.55; color: var(--text); white-space: pre-line; }
#panel #ovHint { margin: 14px 0 0; font-size: 11px; color: var(--dim); }
#panel #ovHint:empty { display: none; }
#ovBtn {
  width: 100%;
  padding: 14px;
  border: 0;
  border-radius: 12px;
  background: linear-gradient(180deg, #7deaff, #27bfe6);
  box-shadow: 0 6px 22px rgba(77, 226, 255, .35);
  color: #03202a;
  font: inherit; font-size: 16px; font-weight: 700; letter-spacing: .1em;
}
#ovBtn:active { transform: scale(.98); }

/* Analog-style stick, bottom-left corner: the knob follows the thumb, the ship moves in the nearest of four directions. */
#dpad {
  --size: min(150px, calc(var(--side) - 12px));
  position: fixed;
  left: calc(env(safe-area-inset-left) + 6px);
  bottom: calc(env(safe-area-inset-bottom) + 10px);
  z-index: 5;
  width: var(--size); height: var(--size);
  border: 2px solid var(--line);
  border-radius: 50%;
  background: radial-gradient(circle at 50% 42%, #33419f, #1b2366 72%);
  box-shadow: inset 0 5px 16px rgba(5, 8, 40, .6), 0 4px 14px rgba(5, 8, 40, .4);
  touch-action: none;
}
#dpad i { position: absolute; pointer-events: none; }
#dpad .up, #dpad .left, #dpad .right, #dpad .down {
  width: 10px; height: 10px;
  border-top: 3px solid rgba(227, 233, 255, .45); border-left: 3px solid rgba(227, 233, 255, .45);
}
#dpad .up { left: 50%; top: 8%; transform: translate(-50%, 0) rotate(45deg); }
#dpad .down { left: 50%; bottom: 8%; transform: translate(-50%, 0) rotate(225deg); }
#dpad .left { top: 50%; left: 8%; transform: translate(0, -50%) rotate(-45deg); }
#dpad .right { top: 50%; right: 8%; transform: translate(0, -50%) rotate(135deg); }
#dpad[data-dir="up"] .up, #dpad[data-dir="left"] .left, #dpad[data-dir="right"] .right, #dpad[data-dir="down"] .down {
  border-color: var(--accent);
  filter: drop-shadow(0 0 5px rgba(77, 226, 255, .9));
}
#dpad .knob {
  left: 50%; top: 50%;
  width: 44%; height: 44%;
  margin: -22% 0 0 -22%;
  border: 2px solid #c9d3ff;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #b4c1ff, #5567dc 58%, #2b3794);
  box-shadow: 0 6px 12px rgba(5, 8, 40, .6);
  transform: translate(var(--kx, 0px), var(--ky, 0px));
  transition: transform .12s ease-out;
}
#dpad.active .knob {
  transition: none;
  border-color: var(--accent);
  box-shadow: 0 6px 12px rgba(5, 8, 40, .6), 0 0 18px rgba(77, 226, 255, .65);
}

/* Cut button, bottom-right corner: the ship only leaves the border while this is held. */
#btnCut {
  --size: min(108px, calc(var(--side) - 26px));
  position: fixed;
  right: calc(env(safe-area-inset-right) + 13px);
  bottom: calc(env(safe-area-inset-bottom) + 26px);
  z-index: 5;
  width: var(--size); height: var(--size);
  display: grid; place-items: center;
  border: 2px solid #ffb35c;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ffb35c, #e2531f 62%, #8f2a0c);
  box-shadow: 0 5px 0 #5c1a06, 0 10px 22px rgba(0, 0, 0, .55);
  color: #fff; font-size: 17px; font-weight: 700; letter-spacing: .14em;
  text-shadow: 0 1px 2px rgba(0, 0, 0, .6);
  touch-action: none;
}
#btnCut.on {
  transform: translateY(4px);
  border-color: #fff3b0;
  background: radial-gradient(circle at 35% 30%, #ffe08a, #ff8a2c 62%, #b8400f);
  box-shadow: 0 1px 0 #5c1a06, 0 0 26px rgba(255, 170, 60, .75);
}

#panel { max-height: 100%; overflow: auto; }
[hidden] { display: none !important; }

#ovBtn2 {
  width: 100%; margin-top: 8px; padding: 10px;
  border: 1px solid var(--accent); border-radius: 12px;
  background: transparent; color: var(--accent);
  font: inherit; font-size: 13px; font-weight: 700; letter-spacing: .06em;
}
#menuRow { display: flex; gap: 8px; margin-top: 10px; }
#menuRow button, #playerBtn {
  flex: 1; padding: 9px 4px;
  border: 1px solid var(--line); border-radius: 10px;
  background: #2a3684; color: var(--text);
  font: inherit; font-size: 11px; font-weight: 700; letter-spacing: .06em;
}
#playerBtn { width: 100%; margin-top: 8px; color: var(--dim); }
#playerBtn.in { border-color: transparent; background: transparent; color: var(--accent); }
#panel #ovHint { min-height: 0; }

#store {
  position: absolute; inset: 0; z-index: 10;
  display: grid; place-items: center;
  padding: 8px;
  background: rgba(18, 26, 74, .7);
  -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px);
}
#store.hidden { display: none; }
#storePanel {
  width: min(520px, 100%); max-height: 100%;
  display: flex; flex-direction: column;
  border: 1px solid var(--line); border-radius: 16px;
  background: linear-gradient(180deg, #26317c, #1a2260);
  box-shadow: 0 18px 50px rgba(5, 8, 40, .45), 0 0 44px rgba(77, 226, 255, .2);
}
#storePanel header { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px 8px 18px; }
#storePanel h2 { margin: 0; font-size: 18px; letter-spacing: .14em; color: var(--accent); }
#storeClose { width: 36px; height: 34px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); color: var(--text); font: inherit; font-size: 15px; }
#storeList { flex: 1; min-height: 0; overflow-y: auto; padding: 0 12px; touch-action: pan-y; -webkit-overflow-scrolling: touch; }
#storeList h3 { margin: 10px 6px 4px; font-size: 10px; font-weight: 400; letter-spacing: .14em; color: var(--dim); }
#storeList .item { display: flex; align-items: center; gap: 10px; padding: 7px 8px; margin-bottom: 5px; border: 1px solid var(--line); border-radius: 12px; background: rgba(40, 52, 128, .7); }
#storeList .icon { flex: none; width: 34px; height: 34px; display: grid; place-items: center; border-radius: 9px; background: #34409a; color: var(--warn); font-style: normal; font-size: 18px; }
#storeList .icon.ship { clip-path: polygon(100% 50%, 8% 8%, 28% 50%, 8% 92%); border-radius: 0; }
#storeList .info { flex: 1; min-width: 0; display: flex; flex-direction: column; text-align: left; line-height: 1.3; }
#storeList .info b { font-size: 13px; }
#storeList .info span { font-size: 10px; color: var(--dim); }
#storeList .item button {
  flex: none; min-width: 84px; padding: 8px 10px;
  border: 0; border-radius: 10px;
  background: linear-gradient(180deg, #7deaff, #27bfe6); color: #03202a;
  font: inherit; font-size: 12px; font-weight: 700;
}
#storeList .item button:disabled { background: #34409a; color: var(--dim); }
#storeList .item button.step { min-width: 38px; padding: 8px 0; background: #34409a; color: var(--text); font-size: 16px; }
#storeList .item button.step:disabled { opacity: .4; }
#storeList .item button.cash { min-width: 70px; background: linear-gradient(180deg, #ffe08a, #ffb428); }
#storeList canvas.icon { background: #1b2366; }
#storePanel footer { display: flex; align-items: center; gap: 10px; padding: 8px 14px 10px; }
#storeRestore { flex: none; padding: 7px 10px; border: 1px solid var(--line); border-radius: 10px; background: transparent; color: var(--dim); font: inherit; font-size: 10px; }
#storeNote { flex: 1; font-size: 10px; color: var(--warn); text-align: right; }
#ovPrivacy { display: inline-block; margin-top: 10px; padding: 4px 8px; font-size: 11px; color: var(--dim); }
#ovPrivacy[hidden] { display: none; }

/* Short screens (phones held sideways): tighter panel. */
@media (max-height: 460px) {
  #panel { width: min(480px, 100%); padding: 14px 20px; }
  #panel h1 { margin-bottom: 6px; font-size: 24px; }
  #panel p { margin-bottom: 10px; font-size: 12px; line-height: 1.45; }
  #panel #ovHint { margin-top: 8px; }
  #ovBtn { padding: 11px; }
}

/* Portrait is only a fallback for browsers; the apps are locked to landscape. */
@media (orientation: portrait) {
  #app { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr); }
  #left { padding: 8px 110px 6px 12px; }
  #hud { flex-direction: row; flex-wrap: wrap; gap: 6px 16px; }
  .stat { flex-direction: column; align-items: flex-start; gap: 0; }
  #bar { flex-basis: 100%; }
  #right { position: fixed; top: calc(env(safe-area-inset-top) + 8px); right: calc(env(safe-area-inset-right) + 12px); padding: 0; }
}

/* Phones held upright in a browser: the game is landscape only, so ask for a turn. (The apps are locked to landscape.) */
#rotate { display: none; }
@media (orientation: portrait) and (pointer: coarse) and (max-width: 600px) {
  #rotate {
    position: fixed; inset: 0; z-index: 50;
    display: grid; place-items: center; padding: 32px;
    background: linear-gradient(160deg, #1a2566, #121a4a 50%, #231a5e);
    color: var(--text); font-size: 18px; font-weight: 700; text-align: center; line-height: 1.5;
  }
  #rotate span::before { content: "⟳"; display: block; margin-bottom: 14px; font-size: 64px; color: var(--accent); }
}
