@font-face {
  font-family: 'UI Pixel';
  src: url('../assets/fonts/ui_pixel.ttf') format('truetype');
  font-display: swap;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  min-height: 100%;
}

body {
  background: #111;
  min-height: 100vh;
  font-family: 'UI Pixel', Consolas, 'Lucida Console', 'Courier New', monospace;
  overflow: hidden;
  --mobile-shell-edge-gap: 12px;
  --mobile-shell-stage-gap: 8px;
  --mobile-control-tray-height: 196px;
  --mobile-control-tray-gap: 12px;
  --mobile-browser-bottom-gap: 64px;
  --mobile-tool-panel-height: 68px;
  --mobile-debug-panel-height: 48px;
}

#app-shell {
  position: relative;
  width: 100vw;
  min-height: 100vh;
  min-height: 100svh;
  display: flex;
  flex-direction: column;
}

#game-stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.ui-header {
  font-family: 'UI Pixel', Consolas, 'Lucida Console', 'Courier New', monospace;
}

.ui-text {
  font-family: 'UI Pixel', Consolas, 'Lucida Console', 'Courier New', monospace;
}

.ui-text-bold {
  font-family: 'UI Pixel', Consolas, 'Lucida Console', 'Courier New', monospace;
}

#game-container {
  position: relative;
  width: 800px;
  height: 600px;
  transform-origin: center center;
  will-change: transform;
}

#game-canvas {
  display: block;
  background: #000;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

#ui-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

#ui-overlay > * {
  pointer-events: auto;
}

#mobile-top-bar,
#mobile-controls,
#mobile-rotate-prompt,
#mobile-tap-start,
#update-ready-toast {
  display: none;
}

.mobile-shell-status,
.mobile-control-tray,
.mobile-control-button,
.mobile-overlay-card {
  border: 3px solid #4d648a;
  background: rgba(10, 14, 24, 0.92);
  color: #f4e4a0;
  text-shadow: 1px 1px 0 #000;
}

.mobile-shell-status {
  min-height: 36px;
  padding: 8px 14px;
  display: flex;
  align-items: center;
}

.mobile-shell-status {
  font-size: 14px;
  color: #cfd7ea;
  justify-content: flex-end;
  text-align: right;
}

.mobile-control-tray,
.mobile-overlay-card {
  padding: 14px 16px;
}

#update-ready-toast[hidden] {
  display: none !important;
}

#update-ready-toast:not([hidden]) {
  position: fixed;
  top: calc(env(safe-area-inset-top, 0px) + 14px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 2800;
  width: min(calc(100vw - 24px), 360px);
  display: flex;
  justify-content: center;
  pointer-events: none;
}

.update-ready-card {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border: 3px solid #4d648a;
  border-radius: 16px;
  background: rgba(10, 14, 24, 0.96);
  color: #cfd7ea;
  text-shadow: 1px 1px 0 #000;
  box-shadow: 0 8px 0 rgba(0, 0, 0, 0.24);
  pointer-events: auto;
}

.update-ready-copy {
  flex: 1 1 auto;
  font-size: 14px;
  color: #f0c07f;
}

.update-ready-button {
  flex: 0 0 auto;
  min-width: 88px;
  min-height: 40px;
  padding: 0 14px;
  border: 3px solid #4d648a;
  border-radius: 12px;
  background: rgba(28, 38, 68, 0.96);
  color: #f4e4a0;
  text-shadow: 1px 1px 0 #000;
  font-family: 'UI Pixel', Consolas, 'Lucida Console', 'Courier New', monospace;
  font-size: 16px;
  cursor: pointer;
}

.update-ready-button:hover,
.update-ready-button:focus-visible {
  border-color: #9ab1df;
  outline: none;
}

.update-ready-button:active {
  transform: translateY(1px);
}

.update-ready-button:disabled {
  cursor: default;
  opacity: 0.6;
}

.mobile-overlay-title {
  font-size: 16px;
  color: #ffe07f;
  margin-bottom: 6px;
}

.mobile-overlay-copy {
  font-size: 12px;
  line-height: 1.4;
  color: #cfd7ea;
}

#mobile-controls[hidden],
#mobile-rotate-prompt[hidden],
#mobile-tap-start[hidden] {
  display: none !important;
}

.mobile-control-tray {
  width: min(100%, 420px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  border-radius: 16px;
  pointer-events: auto;
}

.mobile-control-cluster {
  display: flex;
  align-items: center;
  gap: 10px;
}

.mobile-control-dpad {
  position: relative;
  width: 170px;
  min-width: 170px;
  height: 170px;
}

.mobile-control-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(52px, 74px));
  grid-template-areas:
    'b a'
    'menu menu';
  align-items: center;
  justify-content: end;
  min-width: 160px;
}

.mobile-tool-panel {
  width: min(100%, 420px);
  display: flex;
  flex-direction: column;
  padding: 8px 10px;
  border-radius: 16px;
  background: rgba(8, 12, 24, 0.96);
  border: 3px solid #2a3555;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.3);
  pointer-events: auto;
}

.mobile-debug-panel {
  pointer-events: none;
}

.mobile-debug-panel[hidden] {
  display: none !important;
}

.mobile-debug-panel:not([hidden]) {
  display: contents;
}

.mobile-tool-actions {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.mobile-debug-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  width: min(100%, 320px);
  pointer-events: auto;
}

.mobile-control-button {
  position: relative;
  font-family: 'UI Pixel', Consolas, 'Lucida Console', 'Courier New', monospace;
  font-size: 22px;
  line-height: 1;
  border-radius: 14px;
  cursor: pointer;
  pointer-events: auto;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
  transition: transform 0.06s ease, background 0.06s ease, border-color 0.06s ease;
}

.mobile-control-button::after {
  content: '';
  position: absolute;
  inset: 4px;
  border: 2px solid rgba(207, 215, 234, 0.3);
  border-radius: 10px;
  pointer-events: none;
}

.mobile-control-button:hover,
.mobile-control-button:focus-visible {
  border-color: #7f97c7;
  outline: none;
}

.mobile-control-button.is-pressed,
.mobile-control-button:active {
  background: rgba(28, 38, 68, 0.96);
  border-color: #9ab1df;
  transform: translateY(1px);
}

.mobile-control-direction {
  position: absolute;
  min-width: 52px;
  min-height: 52px;
  width: 52px;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: transparent;
  font-size: 0;
  text-shadow: none;
}

.mobile-control-direction::before {
  content: '';
  position: absolute;
  left: 50%;
  top: 50%;
  width: 20px;
  height: 20px;
  transform: translate(-50%, -50%);
  background-position: center;
  background-repeat: no-repeat;
  background-size: 20px 20px;
}

.mobile-control-up::before {
  left: calc(50% + 1px);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 20 20' shape-rendering='crispEdges'%3E%3Cpolygon fill='%23f4e4a0' points='10,2 17,10 13,10 13,17 7,17 7,10 3,10'/%3E%3C/svg%3E");
}

.mobile-control-left::before {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 20 20' shape-rendering='crispEdges'%3E%3Cpolygon fill='%23f4e4a0' points='2,10 10,3 10,7 17,7 17,13 10,13 10,17'/%3E%3C/svg%3E");
}

.mobile-control-right::before {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 20 20' shape-rendering='crispEdges'%3E%3Cpolygon fill='%23f4e4a0' points='18,10 10,3 10,7 3,7 3,13 10,13 10,17'/%3E%3C/svg%3E");
}

.mobile-control-down::before {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 20 20' shape-rendering='crispEdges'%3E%3Cpolygon fill='%23f4e4a0' points='7,3 13,3 13,10 17,10 10,18 3,10 7,10'/%3E%3C/svg%3E");
}

.mobile-control-up {
  top: 0;
  left: 59px;
}

.mobile-control-left {
  top: 59px;
  left: 0;
}

.mobile-control-right {
  top: 59px;
  right: 0;
}

.mobile-control-down {
  bottom: 0;
  left: 59px;
}

.mobile-control-action {
  min-width: 52px;
  min-height: 52px;
  width: 74px;
  height: 74px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.mobile-control-a {
  grid-area: a;
}

.mobile-control-b {
  grid-area: b;
}

.mobile-control-menu {
  grid-area: menu;
  min-width: 0;
  min-height: 52px;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: auto;
  justify-self: stretch;
  padding: 0 10px;
  font-size: 18px;
}

.mobile-tool-button {
  min-width: 0;
  min-height: 46px;
  height: 46px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
}

.mobile-debug-button {
  min-width: 0;
  min-height: 42px;
  height: 42px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
}

.mobile-tool-button.is-placeholder {
  opacity: 0.34;
}

.mobile-tool-button.is-placeholder::after {
  border-color: rgba(207, 215, 234, 0.14);
}

.mobile-control-button:disabled {
  cursor: default;
}

body[data-layout-mode="mobile-portrait"],
body[data-layout-mode="mobile-landscape"] {
  background: #060810;
  touch-action: none;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

body[data-layout-mode="mobile-portrait"] #app-shell,
body[data-layout-mode="mobile-landscape"] #app-shell {
  gap: 12px;
  padding-top: calc(env(safe-area-inset-top, 0px) + var(--mobile-shell-edge-gap));
  padding-right: calc(env(safe-area-inset-right, 0px) + var(--mobile-shell-edge-gap));
  padding-left: calc(env(safe-area-inset-left, 0px) + var(--mobile-shell-edge-gap));
}

body[data-layout-mode="mobile-portrait"] #game-stage,
body[data-layout-mode="mobile-landscape"] #game-stage {
  width: 100%;
}

body[data-layout-mode="mobile-portrait"] #game-container,
body[data-layout-mode="mobile-landscape"] #game-container {
  transform-origin: top center;
}

body[data-layout-mode="mobile-portrait"] #mobile-top-bar,
body[data-layout-mode="mobile-landscape"] #mobile-top-bar {
  display: none !important;
}

body[data-layout-mode="mobile-portrait"] {
  --mobile-browser-bottom-gap: 64px;
}

body[data-layout-mode="mobile-portrait"][data-mobile-display="standalone"] {
  --mobile-browser-bottom-gap: 20px;
}

body[data-layout-mode="mobile-portrait"] #app-shell {
  min-height: 100vh;
  min-height: 100svh;
  padding-bottom: calc(env(safe-area-inset-bottom, 0px) + var(--mobile-shell-edge-gap));
}

body[data-layout-mode="mobile-portrait"][data-mobile-tray="visible"] #app-shell {
  padding-bottom: calc(
    env(safe-area-inset-bottom, 0px) +
    var(--mobile-control-tray-height) +
    var(--mobile-control-tray-gap) +
    var(--mobile-browser-bottom-gap)
  );
}

body[data-layout-mode="mobile-portrait"][data-mobile-tray="visible"][data-mobile-tools="visible"] #app-shell {
  padding-bottom: calc(
    env(safe-area-inset-bottom, 0px) +
    var(--mobile-control-tray-height) +
    var(--mobile-control-tray-gap) +
    var(--mobile-tool-panel-height) +
    6px +
    var(--mobile-browser-bottom-gap)
  );
}

body[data-layout-mode="mobile-portrait"][data-mobile-tray="visible"][data-mobile-debug="visible"] #app-shell {
  padding-bottom: calc(
    env(safe-area-inset-bottom, 0px) +
    var(--mobile-control-tray-height) +
    var(--mobile-control-tray-gap) +
    var(--mobile-debug-panel-height) +
    4px +
    var(--mobile-browser-bottom-gap)
  );
}

body[data-layout-mode="mobile-portrait"][data-mobile-tray="visible"][data-mobile-tools="visible"][data-mobile-debug="visible"] #app-shell {
  padding-bottom: calc(
    env(safe-area-inset-bottom, 0px) +
    var(--mobile-control-tray-height) +
    var(--mobile-control-tray-gap) +
    var(--mobile-tool-panel-height) +
    6px +
    var(--mobile-debug-panel-height) +
    4px +
    var(--mobile-browser-bottom-gap)
  );
}

body[data-layout-mode="mobile-portrait"] #game-stage {
  align-items: flex-start;
  padding-top: var(--mobile-shell-stage-gap);
}

body[data-layout-mode="mobile-landscape"] #app-shell {
  min-height: 100vh;
  min-height: 100svh;
  padding-bottom: calc(env(safe-area-inset-bottom, 0px) + var(--mobile-shell-edge-gap));
}

body[data-layout-mode="mobile-portrait"] #mobile-controls {
  position: fixed;
  left: calc(env(safe-area-inset-left, 0px) + var(--mobile-shell-edge-gap));
  right: calc(env(safe-area-inset-right, 0px) + var(--mobile-shell-edge-gap));
  bottom: calc(env(safe-area-inset-bottom, 0px) + var(--mobile-browser-bottom-gap));
  z-index: 2500;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: var(--mobile-control-tray-height);
  pointer-events: none;
}

body[data-layout-mode="mobile-landscape"] #mobile-controls {
  width: 100%;
  pointer-events: none;
}

body[data-layout-mode="mobile-portrait"] #mobile-rotate-prompt[hidden],
body[data-layout-mode="mobile-landscape"] #mobile-rotate-prompt[hidden],
body[data-layout-mode="mobile-portrait"] #mobile-tap-start[hidden],
body[data-layout-mode="mobile-landscape"] #mobile-tap-start[hidden] {
  display: none;
}

body[data-layout-mode="mobile-portrait"] #mobile-rotate-prompt,
body[data-layout-mode="mobile-landscape"] #mobile-rotate-prompt,
body[data-layout-mode="mobile-portrait"] #mobile-tap-start,
body[data-layout-mode="mobile-landscape"] #mobile-tap-start {
  position: fixed;
  inset: 0;
  z-index: 3000;
  padding-top: calc(env(safe-area-inset-top, 0px) + 20px);
  padding-right: calc(env(safe-area-inset-right, 0px) + 20px);
  padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 20px);
  padding-left: calc(env(safe-area-inset-left, 0px) + 20px);
  background: rgba(4, 6, 12, 0.82);
  align-items: center;
  justify-content: center;
}

body[data-layout-mode="mobile-portrait"] #update-ready-toast,
body[data-layout-mode="mobile-landscape"] #update-ready-toast {
  top: calc(env(safe-area-inset-top, 0px) + 18px);
}

body[data-layout-mode="mobile-landscape"] #mobile-rotate-prompt:not([hidden]),
body[data-layout-mode="mobile-portrait"] #mobile-tap-start:not([hidden]),
body[data-layout-mode="mobile-landscape"] #mobile-tap-start:not([hidden]) {
  display: flex;
}

.mobile-overlay-card {
  width: min(320px, 100%);
  text-align: center;
}

@media (max-width: 480px) {
  .mobile-control-tray {
    gap: 10px;
    padding: 12px;
  }

  .mobile-tool-panel {
    padding: 8px;
  }

  .mobile-control-dpad {
    width: 160px;
    min-width: 160px;
    height: 160px;
  }

  .mobile-control-up,
  .mobile-control-down {
    left: 54px;
  }

  .mobile-control-left,
  .mobile-control-right {
    top: 54px;
  }

  .mobile-control-action {
    width: 68px;
    height: 68px;
  }

  .mobile-control-menu,
  .mobile-tool-button,
  .mobile-debug-button {
    font-size: 15px;
  }

  .mobile-debug-button {
    height: 40px;
    min-height: 40px;
  }
}

.dom-input {
  position: absolute;
  font-family: 'UI Pixel', Consolas, 'Lucida Console', 'Courier New', monospace;
  font-size: 18px;
  padding: 8px 12px;
  border: 3px solid #444;
  border-radius: 4px;
  background: #222;
  color: #fff;
  outline: none;
  text-align: center;
}

.dom-input:focus {
  border-color: #ffcc00;
}

.dom-input.dom-input-bridge {
  position: fixed;
  left: 0;
  top: 0;
  width: 1px;
  height: 1px;
  padding: 0;
  border: 0;
  margin: 0;
  background: transparent;
  color: transparent;
  caret-color: transparent;
  box-shadow: none;
  outline: none;
  opacity: 0;
  pointer-events: none;
  z-index: -1;
  appearance: none;
  -webkit-appearance: none;
  border-radius: 0;
}

.dom-btn {
  position: absolute;
  font-family: 'UI Pixel', Consolas, 'Lucida Console', 'Courier New', monospace;
  font-size: 16px;
  padding: 10px 20px;
  border: 3px solid #555;
  border-radius: 6px;
  background: #333;
  color: #fff;
  cursor: pointer;
  text-align: center;
  transition: background 0.1s;
}

.dom-btn:hover {
  background: #555;
  border-color: #ffcc00;
}

.dom-btn:active {
  background: #666;
}

.dom-btn.primary {
  background: #2a6e2a;
  border-color: #4a4;
}

.dom-btn.primary:hover {
  background: #3a8e3a;
}

.dom-btn.danger {
  background: #6e2a2a;
  border-color: #a44;
}

.dom-btn.danger:hover {
  background: #8e3a3a;
}

.pill {
  --pill-outline: #18243f;
  --pill-body: #4269b7;
  --pill-inner: #5d88d8;
  --pill-highlight: #c8dbff;
  --pill-shadow: #2a4d98;
  --pill-text: #ffd45b;
  --pill-text-stroke: #152448;
  position: absolute;
  font-family: 'UI Pixel', Consolas, 'Lucida Console', 'Courier New', monospace;
  font-size: 18px;
  line-height: 1;
  color: var(--pill-text);
  letter-spacing: 0.02em;
  border: 2px solid var(--pill-outline);
  border-radius: 999px;
  background: var(--pill-body);
  padding: 0 18px;
  cursor: pointer;
  text-align: center;
  text-shadow:
    -1px 0 var(--pill-text-stroke),
     1px 0 var(--pill-text-stroke),
     0 -1px var(--pill-text-stroke),
     0 1px var(--pill-text-stroke);
}

.pill::before {
  content: '';
  position: absolute;
  left: 2px;
  right: 2px;
  top: 2px;
  bottom: 2px;
  border-radius: inherit;
  border: 1px solid var(--pill-inner);
  background: var(--pill-body);
  box-shadow: inset 0 1px 0 var(--pill-highlight), inset 0 -1px 0 var(--pill-shadow);
  pointer-events: none;
}

.pill > span,
.pill-label {
  position: relative;
  z-index: 1;
}

.pill:hover:not(.is-disabled):not(:disabled) {
  --pill-body: #4b75c9;
  --pill-inner: #6996eb;
  --pill-highlight: #dde9ff;
  --pill-shadow: #3158aa;
  --pill-text: #ffe17f;
}

.pill.is-selected {
  box-shadow: inset 0 0 0 2px #ffcc5f;
}

.pill.is-disabled,
.pill:disabled {
  --pill-outline: #2d3140;
  --pill-body: #4c5568;
  --pill-inner: #606b82;
  --pill-highlight: #8d95a8;
  --pill-shadow: #3c4458;
  --pill-text: #c6c9d1;
  --pill-text-stroke: #2f3442;
  cursor: default;
}

.pill-primary {
  --pill-body: #436fc2;
  --pill-inner: #6594e7;
  --pill-highlight: #c8dbff;
  --pill-shadow: #2a53a5;
}

.pill-slot {
  --pill-body: #4269b7;
  --pill-inner: #5d88d8;
  --pill-highlight: #b8ceff;
  --pill-shadow: #2a4d98;
}

.pill-danger {
  --pill-outline: #331b1b;
  --pill-body: #8e2f36;
  --pill-inner: #b84851;
  --pill-highlight: #e48a90;
  --pill-shadow: #6e2027;
  --pill-text: #ffe08f;
  --pill-text-stroke: #2c1212;
}

.pill-disabled {
  --pill-outline: #2d3140;
  --pill-body: #4c5568;
  --pill-inner: #606b82;
  --pill-highlight: #8d95a8;
  --pill-shadow: #3c4458;
  --pill-text: #c6c9d1;
  --pill-text-stroke: #2f3442;
}

.read-aloud-btn {
  position: absolute;
  font-size: 20px;
  width: 36px;
  height: 36px;
  border: 2px solid #555;
  border-radius: 50%;
  background: #333;
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.read-aloud-btn:hover {
  background: #555;
}
