:root {
  --bg: #0b1020;
  --panel: rgba(18, 24, 40, 0.76);
  --panel-strong: rgba(20, 26, 44, 0.94);
  --line: rgba(255, 255, 255, 0.14);
  --text: #f6f7fb;
  --muted: #aeb7cb;
  --accent: #ffb44a;
  --accent-2: #ff6e55;
  --good: #63d7a2;
  --danger: #ff657c;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

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

html, body {
  width: 100%;
  height: 100%;
  min-height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  touch-action: none;
  overscroll-behavior: none;
}

button, input { font: inherit; }

button {
  color: inherit;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.08);
  border-radius: 14px;
  min-height: 44px;
  padding: 0.7rem 1rem;
  cursor: pointer;
  transition: transform 120ms ease, background 120ms ease, border-color 120ms ease;
}

button:hover { background: rgba(255, 255, 255, 0.14); }
button:active { transform: scale(0.97); }
button:focus-visible, input:focus-visible { outline: 3px solid rgba(255, 180, 74, 0.55); outline-offset: 2px; }

.app-shell, #game-canvas { position: fixed; inset: 0; width: 100%; height: 100%; height: 100dvh; }
#game-canvas { display: block; background: linear-gradient(#ea9e72, #6f8198); }
.is-hidden { display: none !important; }

.glass-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: 0 18px 54px rgba(0, 0, 0, 0.28);
  backdrop-filter: blur(14px) saturate(120%);
}

.screen {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: calc(24px + var(--safe-top)) calc(20px + var(--safe-right)) calc(24px + var(--safe-bottom)) calc(20px + var(--safe-left));
  background: radial-gradient(circle at 50% 35%, rgba(255, 177, 81, 0.22), rgba(7, 10, 19, 0.88) 58%);
}

.screen--boot { text-align: center; }
.screen--boot h1 { margin: 1rem 0 0.4rem; letter-spacing: 0.08em; }
.screen--boot p { color: var(--muted); }

.brand-mark {
  width: 82px;
  height: 82px;
  display: grid;
  place-items: center;
  border-radius: 26px;
  margin: auto;
  background: linear-gradient(145deg, var(--accent), var(--accent-2));
  box-shadow: 0 20px 50px rgba(255, 110, 85, 0.3);
  font-size: 2.6rem;
  font-weight: 900;
}

.loading-track { width: min(320px, 76vw); height: 6px; margin-top: 1.4rem; border-radius: 99px; overflow: hidden; background: rgba(255,255,255,0.1); }
.loading-track span { display: block; width: 8%; height: 100%; border-radius: inherit; background: linear-gradient(90deg, var(--accent), var(--accent-2)); transition: width 240ms ease; }

.screen--menu { justify-items: start; background: linear-gradient(90deg, rgba(7,10,19,0.88), rgba(7,10,19,0.16)), radial-gradient(circle at 72% 35%, rgba(255,181,91,0.18), transparent 48%); }
.menu-card { width: min(430px, 100%); margin-left: clamp(0px, 5vw, 72px); padding: clamp(24px, 4vw, 42px); border-radius: 28px; }
.eyebrow { margin: 0 0 0.35rem; text-transform: uppercase; letter-spacing: 0.16em; color: var(--accent); font-size: 0.72rem; font-weight: 800; }
.menu-card h1 { margin: 0; font-size: clamp(2.3rem, 7vw, 4.5rem); line-height: 0.92; letter-spacing: -0.05em; }
.menu-tagline { color: var(--muted); line-height: 1.55; margin: 1rem 0 1.6rem; }
.field-label { display: block; margin-bottom: 0.45rem; font-size: 0.78rem; color: var(--muted); }
.menu-card input { width: 100%; min-height: 50px; padding: 0 14px; color: var(--text); background: rgba(0,0,0,0.22); border: 1px solid var(--line); border-radius: 14px; }
.menu-card input[type="range"] { min-height: 28px; padding: 0; accent-color: var(--accent); }
.creator-slider-label { margin-top: 0.9rem; }
.creator-row { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 18px; }
.choice-chip { flex: 1 1 90px; font-size: 0.8rem; min-height: 40px; padding: 0.5rem; }
.choice-chip.is-active { border-color: var(--accent); background: rgba(255,180,74,0.18); }
.primary-button { width: 100%; border: 0; color: #201306; font-weight: 900; background: linear-gradient(135deg, var(--accent), #ff8d56); }
.primary-button:hover { background: linear-gradient(135deg, #ffc167, #ff9e6d); }
.secondary-button { width: 100%; margin-top: 9px; }
.capability-note { min-height: 1.2em; margin-bottom: 0; color: var(--muted); font-size: 0.72rem; }

.hud { position: fixed; inset: 0; z-index: 10; pointer-events: none; padding: calc(12px + var(--safe-top)) calc(12px + var(--safe-right)) calc(12px + var(--safe-bottom)) calc(12px + var(--safe-left)); }
.hud button, .mobile-controls, .action-dock, .context-card, .minimap { pointer-events: auto; }
.hud-top { display: flex; gap: 9px; align-items: flex-start; }
.identity-card, .world-card, .money-card { border-radius: 17px; padding: 9px 12px; min-height: 54px; }
.identity-card { display: flex; align-items: center; gap: 9px; min-width: 180px; }
.identity-card div:last-child, .world-card { display: grid; gap: 2px; }
.identity-card span, .world-card span, .money-card span { color: var(--muted); font-size: 0.7rem; }
.avatar-badge { width: 35px; height: 35px; display: grid; place-items: center; border-radius: 12px; background: linear-gradient(145deg, var(--accent), var(--accent-2)); color: #27180b; font-weight: 900; }
.world-card { text-align: center; margin-inline: auto; min-width: 170px; }
.money-card { display: grid; text-align: right; min-width: 115px; }
.money-card strong { color: #ffe08c; }

.need-strip { position: fixed; left: calc(12px + var(--safe-left)); top: calc(81px + var(--safe-top)); width: min(280px, 48vw); border-radius: 16px; padding: 9px; display: grid; gap: 6px; }
.need-item { display: grid; grid-template-columns: 62px 1fr 28px; gap: 7px; align-items: center; font-size: 0.67rem; color: var(--muted); }
.need-bar { height: 5px; overflow: hidden; border-radius: 99px; background: rgba(255,255,255,0.09); }
.need-bar span { display: block; height: 100%; border-radius: inherit; background: var(--good); transition: width 300ms linear; }
.need-item.is-low .need-bar span { background: var(--danger); }

.minimap { position: fixed; right: calc(14px + var(--safe-right)); top: calc(82px + var(--safe-top)); width: 146px; height: 146px; border-radius: 50%; border: 2px solid rgba(255,255,255,0.62); box-shadow: 0 12px 30px rgba(0,0,0,0.35); background: #253042; }
.context-card { position: fixed; left: 50%; bottom: calc(78px + var(--safe-bottom)); transform: translateX(-50%); display: flex; align-items: center; gap: 12px; padding: 8px 9px 8px 14px; border-radius: 18px; }
.context-card span { font-size: 0.82rem; }
.context-button { background: var(--accent); color: #26180a; border: 0; font-weight: 800; }

.notification-stack { position: fixed; right: calc(15px + var(--safe-right)); top: calc(240px + var(--safe-top)); display: grid; gap: 8px; width: min(310px, 72vw); }
.notification { padding: 11px 13px; border-radius: 14px; background: rgba(16,22,36,0.9); border: 1px solid var(--line); box-shadow: 0 10px 24px rgba(0,0,0,0.24); font-size: 0.78rem; animation: notification-in 180ms ease-out; }
@keyframes notification-in { from { opacity: 0; transform: translateX(14px); } }

.desktop-help { position: fixed; left: calc(12px + var(--safe-left)); bottom: calc(14px + var(--safe-bottom)); display: flex; flex-wrap: wrap; gap: 6px 12px; max-width: min(720px, calc(100vw - 390px)); padding: 8px 12px; border-radius: 13px; color: var(--muted); font-size: 0.68rem; }
.action-dock { position: fixed; right: calc(12px + var(--safe-right)); bottom: calc(12px + var(--safe-bottom)); display: flex; gap: 6px; padding: 6px; border-radius: 16px; }
.action-dock button { min-height: 38px; padding: 0.45rem 0.72rem; font-size: 0.72rem; }
.mobile-controls { display: none; }

.side-panel { position: fixed; z-index: 16; right: calc(12px + var(--safe-right)); top: calc(12px + var(--safe-top)); bottom: calc(12px + var(--safe-bottom)); width: min(390px, calc(100vw - 24px)); border-radius: 24px; padding: 20px; overflow: auto; }
.side-panel--left { right: auto; left: calc(12px + var(--safe-left)); }
.panel-heading { display: flex; justify-content: space-between; align-items: flex-start; }
.panel-heading h2 { margin: 0; }
.icon-button { width: 42px; padding: 0; font-size: 1.5rem; }
.side-panel > p { color: var(--muted); line-height: 1.5; }
.build-catalog { display: grid; grid-template-columns: repeat(2, 1fr); gap: 9px; }
.build-item { display: grid; min-height: 92px; text-align: left; align-content: space-between; }
.build-item small { color: var(--muted); }
.build-item.is-selected { border-color: var(--accent); background: rgba(255,180,74,0.15); }
.build-actions { display: flex; gap: 8px; margin-top: 14px; }
.build-actions button { flex: 1; }
.life-grid { display: grid; gap: 10px; margin-top: 18px; }
.life-grid article { display: grid; gap: 5px; padding: 14px; border: 1px solid var(--line); border-radius: 16px; background: rgba(255,255,255,0.05); }
.life-grid span, .life-grid small { color: var(--muted); font-size: 0.72rem; }
.life-grid strong { font-size: 1.05rem; }
.pause-card { width: min(330px, 100%); padding: 28px; border-radius: 24px; text-align: center; }
.pause-card h2 { margin-top: 0; }

.debug-overlay { position: fixed; z-index: 30; left: 8px; top: 8px; margin: 0; padding: 8px 10px; max-width: 330px; color: #b8ffd7; background: rgba(0,0,0,0.72); border-radius: 8px; font: 11px/1.45 ui-monospace, monospace; pointer-events: none; }
.fatal-error { position: fixed; z-index: 99; inset: 20px; display: grid; place-items: center; padding: 24px; text-align: center; border-radius: 20px; color: white; background: rgba(57,10,20,0.96); }

@media (max-width: 760px), (pointer: coarse) {
  .screen--menu { justify-items: center; align-items: end; }
  .menu-card { margin: 0; width: 100%; max-height: 82vh; max-height: 82dvh; overflow: auto; border-radius: 28px 28px 18px 18px; }
  .hud-top { gap: 6px; }
  .identity-card { min-width: 0; padding: 7px; }
  .identity-card div:last-child span { display: none; }
  .world-card { min-width: 112px; padding: 8px; }
  .money-card { min-width: 84px; padding: 8px; }
  .need-strip { width: 150px; top: calc(75px + var(--safe-top)); }
  .need-item { grid-template-columns: 44px 1fr; }
  .need-item > span:last-child { display: none; }
  .minimap { width: 104px; height: 104px; top: calc(76px + var(--safe-top)); }
  .desktop-help { display: none; }
  .action-dock { right: 50%; transform: translateX(50%); bottom: calc(6px + var(--safe-bottom)); gap: 4px; padding: 5px; max-width: calc(100vw - 20px - var(--safe-left) - var(--safe-right)); }
  .action-dock button { width: clamp(46px, 13vw, 58px); min-height: 36px; overflow: hidden; white-space: nowrap; padding-inline: 0.2rem; font-size: clamp(0.52rem, 2.2vw, 0.62rem); }
  .mobile-controls { display: block; }
  .joystick-zone { position: fixed; left: calc(clamp(12px, 4vw, 22px) + var(--safe-left)); bottom: calc(64px + var(--safe-bottom)); bottom: calc(clamp(58px, 8.5svh, 72px) + var(--safe-bottom)); width: clamp(86px, 25vmin, 108px); height: clamp(86px, 25vmin, 108px); border: 1px solid rgba(255,255,255,0.16); border-radius: 50%; background: rgba(12,16,28,0.24); }
  .joystick-knob { position: absolute; left: 50%; top: 50%; width: clamp(36px, 10.5vmin, 44px); height: clamp(36px, 10.5vmin, 44px); margin: -21px 0 0 -21px; border-radius: 50%; background: rgba(255,255,255,0.32); border: 1px solid rgba(255,255,255,0.42); transform: translate(0,0); }
  .face-button-cluster { position: fixed; right: calc(clamp(10px, 3vw, 18px) + var(--safe-right)); bottom: calc(64px + var(--safe-bottom)); bottom: calc(clamp(58px, 8.5svh, 72px) + var(--safe-bottom)); width: clamp(128px, 36vmin, 154px); height: clamp(128px, 36vmin, 154px); display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr); align-items: center; justify-items: center; }
  .face-button { --face-color: #fff; --face-glow: rgba(255,255,255,0.62); position: relative; width: clamp(44px, 12vmin, 52px); height: clamp(44px, 12vmin, 52px); min-height: 44px; padding: 0; display: grid; place-items: center; border: 1.5px solid var(--face-color); border-radius: 50%; color: var(--face-color); background: rgba(8, 12, 20, 0.48); box-shadow: 0 0 10px var(--face-glow), inset 0 0 10px rgba(0,0,0,0.42); backdrop-filter: blur(4px); touch-action: none; -webkit-tap-highlight-color: transparent; }
  .face-button::before { content: ""; position: absolute; inset: 4px; border: 1px solid rgba(255,255,255,0.10); border-radius: inherit; }
  .face-button__symbol { position: relative; z-index: 1; font: 400 clamp(1.35rem, 5.5vmin, 1.7rem)/1 ui-sans-serif, system-ui, sans-serif; filter: drop-shadow(0 0 4px var(--face-color)); }
  .face-button:active { transform: scale(0.9); background: rgba(8, 12, 20, 0.72); box-shadow: 0 0 22px var(--face-color), inset 0 0 18px rgba(0,0,0,0.6); }
  .face-button.is-held { transform: scale(0.9); background: rgba(8, 12, 20, 0.72); box-shadow: 0 0 18px var(--face-color), inset 0 0 15px rgba(0,0,0,0.6); }
  .face-button--triangle { --face-color: #55e2c3; --face-glow: rgba(85,226,195,0.68); grid-column: 2; grid-row: 1; }
  .face-button--square { --face-color: #ee76d6; --face-glow: rgba(238,118,214,0.68); grid-column: 1; grid-row: 2; }
  .face-button--circle { --face-color: #ff7d79; --face-glow: rgba(255,125,121,0.68); grid-column: 3; grid-row: 2; }
  .face-button--cross { --face-color: #70a9ff; --face-glow: rgba(112,169,255,0.68); grid-column: 2; grid-row: 3; }
  .context-card { bottom: calc(210px + var(--safe-bottom)); bottom: calc(clamp(198px, 29svh, 230px) + var(--safe-bottom)); max-width: 78vw; }
  .notification-stack { top: calc(190px + var(--safe-top)); }
}

@media (pointer: coarse) and (orientation: landscape) and (max-height: 560px) {
  .face-button-cluster { right: calc(8px + var(--safe-right)); bottom: calc(8px + var(--safe-bottom)); width: clamp(116px, 29vmin, 136px); height: clamp(116px, 29vmin, 136px); }
  .face-button { width: clamp(42px, 10.5vmin, 48px); height: clamp(42px, 10.5vmin, 48px); min-height: 42px; }
  .joystick-zone { left: calc(10px + var(--safe-left)); bottom: calc(10px + var(--safe-bottom)); width: clamp(82px, 22vmin, 98px); height: clamp(82px, 22vmin, 98px); }
  .context-card { bottom: calc(58px + var(--safe-bottom)); }
  .need-strip { max-height: calc(100vh - 148px - var(--safe-top) - var(--safe-bottom)); max-height: calc(100dvh - 148px - var(--safe-top) - var(--safe-bottom)); overflow: hidden; }
}

@media (max-width: 420px) {
  .hud { padding-inline: calc(7px + var(--safe-left)) calc(7px + var(--safe-right)); }
  .hud-top { gap: 4px; }
  .identity-card, .world-card, .money-card { min-height: 48px; border-radius: 14px; }
  .identity-card { max-width: 31vw; }
  .identity-card strong { max-width: 19vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .avatar-badge { width: 31px; height: 31px; border-radius: 10px; }
  .world-card { min-width: 0; width: 34vw; }
  .money-card { min-width: 0; width: 26vw; }
  .need-strip { left: calc(7px + var(--safe-left)); width: clamp(126px, 40vw, 150px); padding: 7px; gap: 4px; }
  .need-item { grid-template-columns: 40px 1fr; gap: 5px; font-size: 0.61rem; }
  .minimap { right: calc(8px + var(--safe-right)); width: clamp(84px, 27vw, 102px); height: clamp(84px, 27vw, 102px); }
}

@media (min-width: 761px) and (pointer: coarse) {
  .face-button-cluster { width: clamp(142px, 20vmin, 166px); height: clamp(142px, 20vmin, 166px); }
  .face-button { width: clamp(48px, 6.8vmin, 56px); height: clamp(48px, 6.8vmin, 56px); }
  .joystick-zone { width: clamp(100px, 14vmin, 122px); height: clamp(100px, 14vmin, 122px); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

body.high-contrast { --panel: rgba(0,0,0,0.92); --line: rgba(255,255,255,0.5); --muted: #e8ebf2; }
