:root {
  color-scheme: dark;
  --black: #050000;
  --ink: #0d0505;
  --red: #e30000;
  --red-dark: #720000;
  --blue: #5555ff;
  --blue-dark: #171764;
  --white: #f4f1e8;
  --gray: #aaa7a0;
  --yellow: #ffff55;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--black);
  touch-action: manipulation;
}

body {
  min-width: 300px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  overflow-x: hidden;
  background:
    radial-gradient(circle at 50% -20%, rgba(227, 0, 0, 0.2), transparent 38rem),
    repeating-linear-gradient(90deg, rgba(85, 85, 255, 0.035) 0 1px, transparent 1px 6px),
    #050000;
  color: var(--white);
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

button,
input {
  font: inherit;
}

button {
  color: var(--white);
}

.app-shell {
  width: min(100%, 1040px);
  margin: 0 auto;
  padding: 18px clamp(10px, 2.8vw, 28px) max(16px, env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 10px;
}

.brand span {
  display: block;
  color: var(--red);
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(30px, 5vw, 52px);
  font-style: italic;
  font-weight: 900;
  letter-spacing: -0.08em;
  line-height: 0.9;
  text-shadow: 2px 2px 0 #3b0000;
}

.brand small {
  display: block;
  margin-top: 6px;
  color: var(--gray);
  font-size: clamp(7px, 1.2vw, 10px);
  letter-spacing: 0.16em;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 7px;
}

.toolbar button,
.dialog-close,
#password-start {
  border: 1px solid var(--red);
  border-radius: 3px;
  background: linear-gradient(#1d1010, #090303);
  box-shadow: inset 0 0 0 1px rgba(85, 85, 255, 0.28), 0 2px 0 #320000;
  cursor: pointer;
}

.toolbar button {
  min-height: 34px;
  padding: 7px 11px;
  font-size: 12px;
}

.toolbar button:hover,
.toolbar button:focus-visible,
#password-start:hover,
#password-start:focus-visible {
  border-color: var(--yellow);
  outline: none;
  color: var(--yellow);
}

.cabinet {
  position: relative;
  padding: clamp(10px, 2vw, 17px);
  overflow: hidden;
  border: 2px solid var(--red-dark);
  border-radius: 6px;
  background: linear-gradient(145deg, #180606, #080202 55%, #0c071c);
  box-shadow:
    inset 0 0 0 2px #280808,
    inset 0 0 28px #000,
    0 16px 48px rgba(0, 0, 0, 0.66);
}

.cabinet-stripe {
  position: absolute;
  inset: 5px 8% auto;
  height: 3px;
  background: linear-gradient(90deg, transparent, var(--red), var(--blue), var(--red), transparent);
  opacity: 0.72;
}

.screen-bezel {
  padding: clamp(7px, 1.4vw, 12px);
  border: 1px solid #5e1818;
  border-radius: 4px;
  background: #020000;
  box-shadow: inset 0 0 18px #000, 0 0 0 1px #170044;
}

.screen-wrap {
  position: relative;
  width: 100%;
  overflow: hidden;
  border: 1px solid var(--blue-dark);
  background: #000;
  aspect-ratio: 16 / 10;
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  outline: none;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  touch-action: none;
}

.glass {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    linear-gradient(rgba(255,255,255,0.025), transparent 16%, transparent 84%, rgba(227,0,0,0.035)),
    repeating-linear-gradient(0deg, rgba(255,255,255,0.012) 0 1px, transparent 1px 3px);
  mix-blend-mode: screen;
}

.cabinet-footer {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  padding: 9px 3px 0;
  color: #b88787;
  font-size: clamp(8px, 1.4vw, 11px);
}

#status-text {
  color: var(--gray);
  text-align: right;
}

.mobile-controls {
  display: none;
  align-items: center;
  justify-content: center;
  gap: clamp(22px, 10vw, 72px);
  padding-top: 13px;
  touch-action: none;
}

.dpad {
  display: grid;
  grid-template: repeat(3, 46px) / repeat(3, 46px);
}

.dpad button,
.action-button {
  border: 1px solid #752020;
  background: linear-gradient(#261010, #0b0303);
  box-shadow: inset 0 0 0 1px #17174d, 0 3px 0 #340000;
  color: var(--white);
  touch-action: none;
}

.dpad button:active,
.action-button:active {
  transform: translateY(2px);
  box-shadow: inset 0 0 0 1px #17174d, 0 1px 0 #340000;
  color: var(--yellow);
}

.dpad .up { grid-area: 1 / 2; }
.dpad .left { grid-area: 2 / 1; }
.dpad .down { grid-area: 2 / 2; }
.dpad .right { grid-area: 2 / 3; }

.action-button {
  width: 74px;
  height: 74px;
  border-color: var(--red);
  border-radius: 50%;
  color: var(--red);
}

.action-button span {
  display: block;
  font-size: 21px;
  line-height: 1;
}

.action-button small {
  display: block;
  margin-top: 4px;
  color: var(--white);
  font-size: 9px;
}

dialog {
  width: min(92vw, 470px);
  border: 2px solid var(--red);
  border-radius: 4px;
  background: #090202;
  color: var(--white);
  box-shadow: 0 22px 80px #000, inset 0 0 0 2px #17174d;
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.8);
  backdrop-filter: blur(2px);
}

dialog form {
  position: relative;
  padding: 12px 16px 16px;
}

dialog h2 {
  margin: 2px 34px 15px 0;
  color: var(--red);
  font-family: Georgia, "Times New Roman", serif;
  font-style: italic;
}

dialog p {
  color: #d6ceca;
  font-size: 13px;
  line-height: 1.55;
}

.dialog-close {
  position: absolute;
  top: 0;
  right: 0;
  width: 31px;
  height: 31px;
  font-size: 20px;
}

#password-label {
  display: block;
  margin: 16px 0 6px;
  color: var(--gray);
  font-size: 12px;
}

#password-input {
  width: 100%;
  border: 1px solid var(--blue);
  border-radius: 2px;
  padding: 10px 12px;
  background: #000;
  color: var(--yellow);
  font-size: 20px;
}

#password-start {
  width: 100%;
  margin-top: 12px;
  padding: 10px;
}

.help-grid {
  display: grid;
  grid-template-columns: minmax(82px, auto) 1fr;
  gap: 9px 13px;
  align-items: center;
  font-size: 12px;
}

kbd {
  justify-self: start;
  min-width: 52px;
  border: 1px solid #703030;
  border-bottom-width: 3px;
  border-radius: 3px;
  padding: 4px 7px;
  background: #170707;
  color: var(--yellow);
  text-align: center;
}

.help-note {
  margin-bottom: 0;
  color: #a6a2d9;
}

.editor-dialog {
  width: min(96vw, 1120px);
  max-width: none;
  max-height: 94vh;
  max-height: 94dvh;
  padding: 0;
  overflow: auto;
}

.editor-panel {
  position: relative;
  padding: clamp(14px, 2.2vw, 24px);
}

.editor-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 24px;
  padding-right: 32px;
}

.editor-header h2 {
  margin-bottom: 5px;
}

.editor-header p {
  max-width: 660px;
  margin: 0 0 14px;
}

.editor-validation {
  flex: 0 1 300px;
  border: 1px solid #313131;
  border-radius: 3px;
  padding: 8px 10px;
  background: #030303;
  color: #9bdd9b;
  font-size: 11px;
  line-height: 1.4;
}

.editor-validation.warning {
  border-color: #715318;
  color: var(--yellow);
}

.editor-level-row,
.editor-tools,
.editor-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 7px;
}

.editor-level-row {
  margin-bottom: 11px;
}

.editor-level-row label {
  color: var(--gray);
  font-size: 12px;
}

#editor-level {
  min-width: 112px;
  border: 1px solid var(--blue);
  border-radius: 2px;
  padding: 7px 28px 7px 9px;
  background: #020202;
  color: var(--yellow);
}

.editor-selection {
  min-width: 120px;
  margin-left: auto;
  color: #b9b9b9;
  font-size: 11px;
  text-align: right;
}

.editor-workspace {
  display: grid;
  grid-template-columns: minmax(0, 3fr) minmax(250px, 1.15fr);
  gap: clamp(12px, 2vw, 22px);
  align-items: start;
}

.editor-board-frame {
  overflow: hidden;
  border: 2px solid var(--red-dark);
  border-radius: 3px;
  background: #000;
  box-shadow: 0 0 0 1px var(--blue-dark), inset 0 0 18px #000;
}

#editor-board {
  width: 100%;
  height: auto;
  aspect-ratio: 18 / 10;
  cursor: crosshair;
  image-rendering: pixelated;
  touch-action: none;
}

.editor-hint {
  min-height: 18px;
  margin: 7px 0;
  color: #a8a4cb;
  font-size: 10px;
}

.editor-palette-column h3 {
  margin: 0 0 8px;
  color: var(--red);
  font-size: 13px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.editor-palette {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(48px, 1fr));
  gap: 5px;
  max-height: min(46vh, 430px);
  padding: 6px;
  overflow: auto;
  border: 1px solid #331313;
  border-radius: 3px;
  background: #020202;
}

.editor-tile,
.editor-control,
.editor-primary {
  border: 1px solid #672020;
  border-radius: 3px;
  background: linear-gradient(#1b1010, #080303);
  box-shadow: inset 0 0 0 1px rgba(85, 85, 255, 0.2);
  color: var(--white);
  cursor: pointer;
}

.editor-control,
.editor-primary {
  min-height: 34px;
  padding: 7px 10px;
  font-size: 11px;
}

.editor-primary {
  border-color: var(--red);
  color: var(--yellow);
}

.editor-tile {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 44px;
  min-height: 48px;
  padding: 3px 3px 13px;
}

.editor-tile canvas {
  width: 28px;
  height: 28px;
  image-rendering: pixelated;
  pointer-events: none;
}

.editor-tile small {
  position: absolute;
  right: 3px;
  bottom: 1px;
  color: #9a9696;
  font-size: 8px;
  pointer-events: none;
}

.editor-tile:hover,
.editor-tile:focus-visible,
.editor-control:hover,
.editor-control:focus-visible,
.editor-primary:hover,
.editor-primary:focus-visible,
.editor-tile.selected,
.editor-control.selected {
  border-color: var(--yellow);
  outline: none;
  color: var(--yellow);
  box-shadow: inset 0 0 0 1px var(--blue), 0 0 8px rgba(255, 255, 85, 0.16);
}

.editor-control:disabled,
.editor-primary:disabled,
.editor-tile:disabled,
#editor-button:disabled {
  cursor: not-allowed;
  opacity: 0.38;
}

.editor-actions {
  margin-top: 13px;
  padding-top: 12px;
  border-top: 1px solid #351313;
}

@media (max-width: 820px) {
  .editor-dialog {
    width: 98vw;
    max-height: 96dvh;
  }

  .editor-header {
    display: block;
  }

  .editor-validation {
    display: block;
    margin-bottom: 10px;
  }

  .editor-workspace {
    grid-template-columns: 1fr;
  }

  .editor-palette {
    max-height: 230px;
  }

  .editor-selection {
    width: 100%;
    margin-left: 0;
    text-align: left;
  }
}

@media (hover: none), (pointer: coarse), (max-width: 720px) {
  .app-shell {
    padding: 10px 7px max(10px, env(safe-area-inset-bottom));
  }

  .topbar {
    align-items: flex-start;
    margin-bottom: 7px;
  }

  .brand small {
    display: none;
  }

  .toolbar {
    gap: 5px;
  }

  .toolbar button {
    min-height: 31px;
    padding: 5px 7px;
    font-size: 10px;
  }

  .cabinet {
    padding: 8px;
  }

  .screen-bezel {
    padding: 5px;
  }

  .mobile-controls {
    display: flex;
  }

  .cabinet-footer {
    padding-top: 7px;
  }

  #level-status {
    display: none;
  }
}

@media (max-height: 620px) and (orientation: landscape) {
  .app-shell {
    width: min(100%, 860px);
    padding-top: 4px;
  }

  .topbar {
    margin-bottom: 3px;
  }

  .brand span {
    font-size: 26px;
  }

  .mobile-controls {
    position: absolute;
    inset: auto 13px 6px auto;
    gap: 10px;
    padding: 0;
    opacity: 0.82;
  }

  .dpad {
    grid-template: repeat(2, 34px) / repeat(3, 34px);
  }

  .action-button {
    width: 54px;
    height: 54px;
  }

  .cabinet-footer {
    display: none;
  }
}

body.game-fullscreen {
  overflow: hidden;
  overscroll-behavior: none;
  background: #000;
}

body.game-fullscreen .app-shell {
  position: fixed;
  inset: 0;
  z-index: 2147483000;
  display: grid;
  width: 100vw;
  max-width: none;
  height: 100vh;
  height: 100dvh;
  margin: 0;
  padding: 0;
  place-items: center;
  background: #000;
}

body.game-fullscreen .topbar {
  position: absolute;
  top: max(7px, env(safe-area-inset-top));
  right: max(7px, env(safe-area-inset-right));
  z-index: 10;
  margin: 0;
  pointer-events: none;
}

body.game-fullscreen .brand,
body.game-fullscreen .toolbar button:not(#fullscreen-button) {
  display: none;
}

body.game-fullscreen .toolbar,
body.game-fullscreen #fullscreen-button {
  pointer-events: auto;
}

body.game-fullscreen #fullscreen-button {
  border-color: rgba(227, 0, 0, 0.62);
  background: rgba(5, 0, 0, 0.72);
  opacity: 0.72;
}

body.game-fullscreen #fullscreen-button:hover,
body.game-fullscreen #fullscreen-button:focus-visible {
  opacity: 1;
}

body.game-fullscreen .cabinet {
  width: min(100vw, calc(100vh * 1.6));
  width: min(100vw, calc(100dvh * 1.6));
  margin: 0;
  padding: 0;
  overflow: visible;
  border: 0;
  border-radius: 0;
  background: #000;
  box-shadow: none;
}

body.game-fullscreen .cabinet-stripe,
body.game-fullscreen .cabinet-footer {
  display: none;
}

body.game-fullscreen .screen-bezel {
  padding: 0;
  border: 0;
  border-radius: 0;
  background: #000;
  box-shadow: none;
}

body.game-fullscreen .screen-wrap {
  border: 0;
}

body.game-fullscreen .mobile-controls {
  position: absolute;
  inset: auto max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  z-index: 8;
  justify-content: space-between;
  padding: 0;
  opacity: 0.7;
  pointer-events: none;
}

body.game-fullscreen .mobile-controls button {
  pointer-events: auto;
}

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