:root {
  color-scheme: dark;
  --panel: rgba(10, 14, 18, 0.76);
  --panel-strong: rgba(12, 17, 24, 0.92);
  --line: rgba(255, 255, 255, 0.16);
  --text: #f4f7fb;
  --muted: #aab4c2;
  --accent: #f4c95d;
  --ok: #75e0ad;
  --danger: #ff7d7d;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: #030406;
  color: var(--text);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

button,
select,
input {
  font: inherit;
}

.game-shell {
  position: relative;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  min-height: 620px;
  overflow: hidden;
  background:
    radial-gradient(circle at 18% 22%, rgba(178, 88, 72, 0.12), transparent 30%),
    radial-gradient(circle at 80% 24%, rgba(76, 163, 128, 0.1), transparent 28%),
    #030406;
}

#space {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: none;
}

.hud {
  position: absolute;
  z-index: 2;
  border: 1px solid var(--line);
  background: var(--panel);
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.24);
  backdrop-filter: blur(16px);
}

.hud-top {
  top: 10px;
  left: 18px;
  right: 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  min-height: 66px;
  padding: 10px 14px;
  border-radius: 8px;
}

.label {
  margin: 0 0 4px;
  color: var(--accent);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0;
}

h1 {
  margin: 0;
  font-size: clamp(1.1rem, 2.2vw, 1.65rem);
  font-weight: 820;
  line-height: 1.05;
}

.status-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(88px, 1fr));
  gap: 8px;
  min-width: min(680px, 60vw);
}

.status-grid div {
  min-width: 0;
  padding: 6px 10px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.06);
}

.status-grid span,
.control-row label,
.readout-row span {
  display: block;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 700;
}

.status-grid strong,
.readout-row strong {
  display: block;
  min-height: 1.35em;
  margin-top: 2px;
  font-size: 0.95rem;
  white-space: nowrap;
}

.controls {
  left: 18px;
  right: 18px;
  bottom: 10px;
  display: grid;
  grid-template-columns: minmax(330px, 1.1fr) 72px 82px minmax(120px, 0.7fr) minmax(360px, 0.9fr);
  align-items: end;
  gap: 10px;
  min-height: 86px;
  padding: 10px 12px;
  border-radius: 8px;
}

.control-row {
  min-width: 0;
}

.level-row {
  display: grid;
  grid-template-columns: 52px minmax(110px, 0.8fr) 44px minmax(160px, 1.1fr);
  align-items: center;
  gap: 8px;
}

.internal-level-select {
  display: none;
}

select {
  width: 100%;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-strong);
  color: var(--text);
  padding: 0 10px;
}

input[type="range"] {
  width: 100%;
  margin: 8px 0 0;
  accent-color: var(--accent);
}

.button-row {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 4px;
}

.button-panel {
  min-width: 0;
}

button {
  display: flex;
  height: 42px;
  min-width: 0;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.08);
  box-shadow:
    0 8px 18px rgba(0, 0, 0, 0.22),
    inset 0 1px 0 rgba(255, 255, 255, 0.1);
  color: var(--text);
  font-size: 1rem;
  font-weight: 800;
  cursor: pointer;
  touch-action: manipulation;
  transition:
    background 120ms ease,
    border-color 120ms ease,
    box-shadow 120ms ease,
    transform 120ms ease;
}

.button-icon,
.button-label {
  display: block;
  line-height: 1;
}

.button-icon {
  font-size: 1.08rem;
}

.button-label {
  max-width: 100%;
  overflow: hidden;
  font-size: 0.62rem;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.key-help {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 700;
  line-height: 1.2;
}

button:hover {
  background: rgba(255, 255, 255, 0.14);
}

button:active,
button.is-pressed {
  border-color: rgba(244, 201, 93, 0.55);
  background: rgba(244, 201, 93, 0.18);
  box-shadow:
    0 3px 8px rgba(0, 0, 0, 0.26),
    inset 0 3px 10px rgba(0, 0, 0, 0.32),
    0 0 0 2px rgba(244, 201, 93, 0.12);
  transform: translateY(2px) scale(0.98);
}

button:focus-visible,
select:focus-visible,
input:focus-visible {
  outline: 2px solid rgba(244, 201, 93, 0.72);
  outline-offset: 2px;
}

.mission-brief {
  right: 18px;
  bottom: 104px;
  width: min(360px, calc(100vw - 36px));
  min-height: 50px;
  padding: 8px 12px;
  border-radius: 8px;
  pointer-events: none;
}

.mission-brief p {
  margin: 0 0 3px;
  overflow: hidden;
  color: var(--accent);
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mission-brief span {
  display: -webkit-box;
  overflow: hidden;
  color: var(--muted);
  font-size: 0.75rem;
  line-height: 1.25;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
}

@media (min-width: 1021px) and (max-width: 1500px) {
  .controls {
    right: 18px;
    grid-template-columns: minmax(300px, 1.1fr) 64px 74px minmax(108px, 0.6fr) minmax(320px, 0.9fr);
  }

  .mission-brief {
    right: 18px;
    bottom: 102px;
    left: auto;
    width: min(340px, calc(100vw - 36px));
    min-height: 50px;
  }
}

.tutorial-panel {
  top: 128px;
  left: 50%;
  width: min(420px, calc(100vw - 40px));
  padding: 16px;
  border-radius: 8px;
  transform: translateX(-50%);
}

.tutorial-panel.hidden {
  display: none;
}

.tutorial-panel h2 {
  margin: 0 0 8px;
  font-size: 1.05rem;
}

.tutorial-panel span {
  display: block;
  color: var(--text);
  font-size: 0.9rem;
  line-height: 1.45;
}

.tutorial-panel button {
  width: 100%;
  margin-top: 12px;
}

.editor-panel {
  top: 112px;
  right: 18px;
  bottom: 18px;
  width: min(520px, calc(100vw - 36px));
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 5;
  overflow: auto;
  padding: 14px;
  border-radius: 8px;
}

.editor-panel.dock-left {
  right: auto;
  left: 18px;
}

.editor-panel.hidden {
  display: none;
}

.editor-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.editor-head-actions {
  display: flex;
  gap: 8px;
}

.editor-head h2 {
  margin: 0;
  font-size: 1rem;
}

.editor-head button {
  width: 72px;
  height: 36px;
}

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

.editor-tools {
  display: grid;
  grid-template-columns: minmax(120px, 1.1fr) repeat(3, minmax(82px, 0.7fr));
  align-items: end;
  gap: 8px;
}

.editor-tools label {
  min-width: 0;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
}

.editor-tools select {
  margin-top: 4px;
}

.editor-tools button {
  height: 36px;
  font-size: 0.72rem;
}

.editor-selection {
  grid-column: 1 / -1;
  min-height: 1.3em;
  color: var(--accent);
  font-size: 0.76rem;
  font-weight: 800;
}

.editor-property-grid {
  padding-bottom: 8px;
  border-bottom: 1px solid var(--line);
}

.editor-object-lists {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.editor-object-lists section {
  min-width: 0;
}

.editor-object-lists h3 {
  margin: 0 0 5px;
  color: var(--muted);
  font-size: 0.72rem;
}

.editor-object-list {
  display: grid;
  gap: 5px;
}

.editor-object-button {
  height: 34px;
  align-items: center;
  flex-direction: row;
  justify-content: flex-start;
  gap: 8px;
  padding: 0 8px;
  font-size: 0.7rem;
  text-align: left;
}

.editor-object-button.is-selected {
  border-color: rgba(244, 201, 93, 0.75);
  background: rgba(244, 201, 93, 0.18);
}

.editor-color-dot {
  width: 12px;
  height: 12px;
  flex: 0 0 auto;
  border: 1px solid rgba(255, 255, 255, 0.55);
  border-radius: 999px;
}

.editor-json-store {
  display: none;
}

.editor-grid label,
.editor-wide {
  min-width: 0;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
}

.editor-grid input,
.editor-grid select,
.editor-wide textarea {
  width: 100%;
  margin-top: 4px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-strong);
  color: var(--text);
}

.editor-grid input[type="color"] {
  height: 34px;
  padding: 2px;
}

.editor-grid input,
.editor-grid select {
  height: 34px;
  padding: 0 8px;
}

.editor-wide textarea {
  min-height: 92px;
  max-height: 150px;
  padding: 8px;
  resize: vertical;
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  font-size: 0.72rem;
  line-height: 1.35;
}

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

.editor-actions button {
  height: 38px;
  font-size: 0.75rem;
}

.editor-status {
  min-height: 1.4em;
  margin: 0;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 800;
  line-height: 1.35;
}

.banner {
  position: absolute;
  z-index: 3;
  left: 50%;
  top: 50%;
  width: min(560px, calc(100vw - 40px));
  padding: 18px 20px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: rgba(8, 12, 16, 0.88);
  box-shadow: 0 20px 70px rgba(0, 0, 0, 0.45);
  color: var(--text);
  text-align: center;
  transform: translate(-50%, -50%) scale(0.96);
  opacity: 0;
  pointer-events: none;
  transition:
    opacity 160ms ease,
    transform 160ms ease;
}

.banner.show {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}

.banner.success {
  border-color: rgba(117, 224, 173, 0.55);
}

.banner.fail {
  border-color: rgba(255, 125, 125, 0.55);
}

@media (max-width: 1020px) {
  .game-shell {
    min-height: 760px;
  }

  .hud-top {
    align-items: stretch;
    flex-direction: column;
    gap: 10px;
  }

  .status-grid {
    min-width: 0;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .controls,
  .mission-brief {
    left: 12px;
    right: 12px;
    width: auto;
  }

  .mission-brief {
    bottom: calc(206px + env(safe-area-inset-bottom, 0px) + 72px);
    min-height: 96px;
  }

  .tutorial-panel {
    top: 50%;
    width: min(360px, calc(100vw - 28px));
    transform: translate(-50%, -50%);
  }

  .controls {
    bottom: calc(env(safe-area-inset-bottom, 0px) + 72px);
    grid-template-columns: repeat(2, minmax(0, 1fr));
    min-height: 126px;
  }

  .button-panel {
    grid-column: span 2;
  }

  button {
    height: 44px;
  }
}

@media (max-width: 1020px) and (orientation: landscape) {
  .game-shell {
    min-height: 100dvh;
  }

  .hud-top {
    top: 8px;
    right: 258px;
    left: 10px;
    min-height: 56px;
    flex-direction: row;
    align-items: center;
    gap: 10px;
    padding: 7px 9px;
  }

  .hud-top h1 {
    font-size: 1rem;
    line-height: 1;
  }

  .label {
    margin-bottom: 2px;
    font-size: 0.62rem;
  }

  .status-grid {
    min-width: 0;
    flex: 1;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
  }

  .status-grid div {
    padding: 5px 6px;
  }

  .status-grid span,
  .control-row label,
  .readout-row span {
    font-size: 0.6rem;
  }

  .status-grid strong,
  .readout-row strong {
    font-size: 0.74rem;
  }

  .mission-brief {
    top: 70px;
    right: 258px;
    bottom: auto;
    left: 10px;
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 40px;
    max-height: 42px;
    padding: 6px 9px;
  }

  .mission-brief p {
    flex: 0 0 auto;
    max-width: 34%;
    margin: 0;
    overflow: hidden;
    font-size: 0.78rem;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .mission-brief span {
    display: -webkit-box;
    overflow: hidden;
    font-size: 0.72rem;
    line-height: 1.25;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
  }

  .tutorial-panel {
    top: 124px;
    right: 258px;
    left: 10px;
    width: auto;
    max-height: 150px;
    padding: 9px;
    transform: none;
  }

  .tutorial-panel h2 {
    margin-bottom: 5px;
    font-size: 0.88rem;
  }

  .tutorial-panel span {
    font-size: 0.74rem;
    line-height: 1.25;
  }

  .tutorial-panel button {
    height: 34px;
    margin-top: 7px;
  }

  .controls {
    top: 8px;
    right: 8px;
    bottom: calc(env(safe-area-inset-bottom, 0px) + 8px);
    left: auto;
    width: 244px;
    min-height: 0;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-content: start;
    align-items: stretch;
    gap: 7px;
    padding: 9px;
  }

  .level-row {
    grid-column: 1 / -1;
    grid-template-columns: 44px 1fr;
    gap: 8px;
  }

  .controls .control-row:nth-of-type(4) {
    grid-column: 1 / -1;
  }

  select {
    height: 36px;
    padding: 0 8px;
    font-size: 0.76rem;
  }

  input[type="range"] {
    margin-top: 5px;
  }

  .button-panel {
    grid-column: 1 / -1;
  }

  .button-row {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 5px;
  }

  button {
    height: 52px;
  }

  .button-icon {
    font-size: 1.14rem;
  }

  .button-label {
    font-size: 0.55rem;
  }

  .key-help {
    margin-top: 5px;
    font-size: 0.6rem;
  }

  .editor-panel {
    top: 8px;
    right: 258px;
    bottom: 8px;
    left: 10px;
    width: auto;
    gap: 7px;
    padding: 9px;
  }

  .editor-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
  }

  .editor-tools {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 5px;
  }

  .editor-tools label {
    grid-column: 1 / -1;
  }

  .editor-tools button {
    height: 30px;
    font-size: 0.62rem;
  }

  .editor-grid input,
  .editor-grid select {
    height: 30px;
    font-size: 0.7rem;
  }

  .editor-wide textarea {
    min-height: 54px;
    max-height: 70px;
    font-size: 0.62rem;
  }

  .editor-actions {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 5px;
  }

  .editor-actions button,
  .editor-head button {
    height: 32px;
    font-size: 0.68rem;
  }
}
