.space-layer {
  --space-rgb: 255, 178, 92;
  position: fixed;
  z-index: 180;
  inset: 0;
  overflow: hidden;
  color: #f4f7ff;
  background-color: #010208;
  background-image:
    radial-gradient(circle at 66% 48%, rgba(var(--space-rgb), 0.05), transparent 28%),
    linear-gradient(90deg, rgba(1, 3, 9, 0.88), rgba(1, 3, 9, 0.2) 52%, rgba(1, 3, 9, 0.08)),
    url("./assets/modes/mode-space-v2.webp");
  background-position: center, center, center;
  background-size: auto, auto, cover;
  background-repeat: no-repeat;
  isolation: isolate;
}

.space-layer[hidden] {
  display: none;
}

body.space-mode-open {
  overflow: hidden;
}

.space-canvas,
.space-field {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.space-canvas {
  z-index: -3;
  cursor: crosshair;
  touch-action: none;
}

.space-field {
  z-index: -2;
  overflow: hidden;
  background:
    linear-gradient(rgba(157, 195, 255, 0.028) 1px, transparent 1px),
    linear-gradient(90deg, rgba(157, 195, 255, 0.028) 1px, transparent 1px);
  background-size: 56px 56px;
  mask-image: linear-gradient(90deg, rgba(0, 0, 0, 0.62), transparent 26%, transparent 72%, rgba(0, 0, 0, 0.5));
  pointer-events: none;
}

.space-field::before,
.space-field::after {
  position: absolute;
  content: "";
  border: 1px solid rgba(var(--space-rgb), 0.11);
  border-radius: 50%;
}

.space-field::before {
  top: 50%;
  left: 61%;
  width: min(65vw, 900px);
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  box-shadow:
    0 0 0 90px rgba(var(--space-rgb), 0.018),
    0 0 0 180px rgba(var(--space-rgb), 0.012);
}

.space-field::after {
  top: 50%;
  left: 61%;
  width: min(38vw, 520px);
  aspect-ratio: 1;
  border-style: dashed;
  transform: translate(-50%, -50%) rotate(18deg);
  animation: space-orbit-spin 34s linear infinite;
}

.space-field i {
  position: absolute;
  top: 50%;
  left: 61%;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: rgb(var(--space-rgb));
  box-shadow: 0 0 26px 7px rgba(var(--space-rgb), 0.34);
  animation: space-beacon 5.4s ease-in-out infinite;
}

.space-field i:nth-child(2) {
  top: 24%;
  left: 78%;
  width: 2px;
  height: 2px;
  animation-delay: -1.9s;
}

.space-field i:nth-child(3) {
  top: 78%;
  left: 46%;
  width: 3px;
  height: 3px;
  animation-delay: -3.3s;
}

.space-header {
  position: absolute;
  top: max(32px, env(safe-area-inset-top));
  left: max(38px, env(safe-area-inset-left));
  width: min(430px, 34vw);
  pointer-events: none;
}

.space-header > p:first-child {
  display: flex;
  gap: 10px;
  align-items: center;
  margin: 0 0 18px;
  color: rgba(211, 224, 255, 0.58);
  font: 700 8px/1 Consolas, "Courier New", monospace;
  letter-spacing: 0.21em;
}

.space-header > p:first-child span {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: rgb(var(--space-rgb));
  box-shadow: 0 0 13px rgba(var(--space-rgb), 0.9);
}

.space-header h2 {
  margin: 0;
  font-family: "Arial Narrow", "Yu Gothic", sans-serif;
  font-size: clamp(34px, 4.25vw, 68px);
  font-weight: 300;
  letter-spacing: -0.065em;
  line-height: 0.96;
}

.space-header > p:last-child {
  max-width: 390px;
  margin: 19px 0 0;
  color: rgba(224, 232, 250, 0.56);
  font: 550 9px/1.9 "Yu Gothic", "Hiragino Kaku Gothic ProN", sans-serif;
  letter-spacing: 0.09em;
}

.space-close {
  position: absolute;
  top: max(34px, env(safe-area-inset-top));
  right: max(36px, env(safe-area-inset-right));
  display: flex;
  gap: 11px;
  align-items: center;
  min-height: 36px;
  padding: 0 15px;
  border: 1px solid rgba(217, 228, 255, 0.18);
  border-radius: 999px;
  color: rgba(235, 240, 255, 0.67);
  background: rgba(3, 6, 16, 0.52);
  backdrop-filter: blur(14px);
  font: 700 8px/1 "Yu Gothic", sans-serif;
  letter-spacing: 0.14em;
  cursor: pointer;
  transition: border-color 180ms ease, color 180ms ease, background 180ms ease;
}

.space-close:hover,
.space-close:focus-visible {
  border-color: rgba(var(--space-rgb), 0.68);
  color: #fff;
  background: rgba(var(--space-rgb), 0.09);
  outline: none;
}

.space-readout {
  position: absolute;
  bottom: max(112px, calc(env(safe-area-inset-bottom) + 92px));
  left: max(38px, env(safe-area-inset-left));
  width: min(390px, 32vw);
  padding-left: 16px;
  border-left: 1px solid rgba(var(--space-rgb), 0.5);
}

.space-readout-index {
  margin: 0 0 13px;
  color: rgba(215, 226, 250, 0.45);
  font: 700 8px/1 Consolas, monospace;
  letter-spacing: 0.17em;
}

.space-readout-index span {
  color: rgb(var(--space-rgb));
  font-size: 18px;
}

.space-readout-index em {
  margin-left: 12px;
  color: rgba(var(--space-rgb), 0.75);
  font-style: normal;
}

.space-readout h3 {
  margin: 0;
  font-family: "Yu Gothic", "Hiragino Kaku Gothic ProN", sans-serif;
  font-size: clamp(18px, 2vw, 29px);
  font-weight: 500;
  letter-spacing: 0.075em;
}

.space-mode-title-en {
  margin: 7px 0 0;
  color: rgba(220, 229, 249, 0.42);
  font: 700 8px/1 Consolas, monospace;
  letter-spacing: 0.18em;
  text-transform: uppercase;
}

.space-narrative {
  margin: 16px 0 0;
  color: rgba(231, 237, 251, 0.7);
  font: 520 10px/1.85 "Yu Gothic", "Hiragino Kaku Gothic ProN", sans-serif;
  letter-spacing: 0.075em;
}

.space-metric {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 16px;
  align-items: end;
  margin-top: 17px;
  padding-top: 13px;
  border-top: 1px solid rgba(var(--space-rgb), 0.15);
}

.space-metric > span {
  grid-row: 1;
  color: rgba(var(--space-rgb), 0.72);
  font: 700 7px/1 Consolas, monospace;
  letter-spacing: 0.15em;
}

.space-metric strong {
  grid-row: 1 / 3;
  grid-column: 2;
  justify-self: end;
  color: #fff;
  font: 300 clamp(18px, 2.4vw, 34px)/1 "Arial Narrow", sans-serif;
  letter-spacing: -0.025em;
}

.space-metric strong small {
  color: rgba(var(--space-rgb), 0.72);
  font: 700 8px/1 Consolas, monospace;
  letter-spacing: 0.08em;
}

.space-metric > p {
  grid-row: 2;
  margin: 0;
  color: rgba(218, 227, 247, 0.42);
  font: 650 7px/1.4 Consolas, monospace;
  letter-spacing: 0.08em;
}

.space-encoding {
  display: grid;
  gap: 5px;
  margin: 13px 0 0;
  color: rgba(227, 234, 250, 0.56);
  font: 560 8px/1.65 "Yu Gothic", sans-serif;
  letter-spacing: 0.06em;
}

.space-encoding span {
  color: rgba(var(--space-rgb), 0.62);
  font: 700 7px/1 Consolas, monospace;
  letter-spacing: 0.16em;
}

.space-encoding b {
  font-weight: 560;
}

.space-mode-bank {
  position: absolute;
  right: max(35px, env(safe-area-inset-right));
  bottom: max(112px, calc(env(safe-area-inset-bottom) + 92px));
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 5px;
  width: min(390px, 31vw);
}

.space-mode-option {
  position: relative;
  display: grid;
  grid-template-columns: 26px minmax(0, 1fr);
  gap: 3px 9px;
  align-content: center;
  min-width: 0;
  min-height: 45px;
  padding: 8px 10px;
  overflow: hidden;
  border: 1px solid rgba(215, 227, 255, 0.12);
  color: rgba(226, 234, 252, 0.56);
  text-align: left;
  background: rgba(3, 6, 15, 0.58);
  backdrop-filter: blur(12px);
  cursor: pointer;
  transition: border-color 180ms ease, color 180ms ease, transform 180ms ease, background 180ms ease;
}

.space-mode-option::after {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  height: 1px;
  content: "";
  background: linear-gradient(90deg, rgb(var(--space-rgb)), transparent);
  opacity: 0;
}

.space-mode-option:hover,
.space-mode-option:focus-visible,
.space-mode-option[aria-current="true"] {
  border-color: rgba(var(--space-rgb), 0.55);
  color: #fff;
  background: rgba(var(--space-rgb), 0.08);
  outline: none;
  transform: translateX(-3px);
}

.space-mode-option[aria-current="true"]::after {
  opacity: 0.9;
}

.space-mode-option span {
  grid-row: 1 / 3;
  align-self: center;
  color: rgb(var(--space-rgb));
  font: 700 9px/1 Consolas, monospace;
  letter-spacing: 0.1em;
}

.space-mode-option strong {
  overflow: hidden;
  font: 650 9px/1.1 "Yu Gothic", sans-serif;
  letter-spacing: 0.08em;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.space-mode-option small {
  overflow: hidden;
  color: rgba(212, 223, 248, 0.38);
  font: 700 6px/1 Consolas, monospace;
  letter-spacing: 0.11em;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.space-footer {
  position: absolute;
  right: max(35px, env(safe-area-inset-right));
  bottom: max(28px, env(safe-area-inset-bottom));
  left: max(38px, env(safe-area-inset-left));
  display: grid;
  grid-template-columns: auto minmax(240px, 360px) minmax(220px, 1fr);
  gap: 12px;
  align-items: center;
  padding-top: 12px;
  border-top: 1px solid rgba(212, 224, 252, 0.12);
}

.space-sequence-controls {
  display: flex;
  gap: 5px;
}

.space-sequence-controls button {
  width: 37px;
  height: 37px;
  border: 1px solid rgba(219, 230, 255, 0.15);
  border-radius: 50%;
  color: rgba(232, 239, 255, 0.65);
  background: rgba(4, 7, 16, 0.68);
  cursor: pointer;
}

.space-sequence-controls button:hover,
.space-sequence-controls button:focus-visible {
  border-color: rgba(var(--space-rgb), 0.65);
  color: #fff;
  outline: none;
}

.space-data-button {
  display: grid;
  gap: 4px;
  padding: 5px 12px;
  border: 0;
  border-left: 1px solid rgba(var(--space-rgb), 0.48);
  color: rgba(231, 238, 254, 0.74);
  text-align: left;
  background: transparent;
  cursor: pointer;
}

.space-data-button span {
  color: rgba(var(--space-rgb), 0.68);
  font: 700 7px/1 Consolas, monospace;
  letter-spacing: 0.16em;
}

.space-data-button strong {
  font: 600 9px/1.2 "Yu Gothic", sans-serif;
  letter-spacing: 0.08em;
}

.space-footer > p {
  justify-self: end;
  max-width: 520px;
  margin: 0;
  color: rgba(211, 222, 246, 0.38);
  font: 650 7px/1.6 "Yu Gothic", sans-serif;
  letter-spacing: 0.08em;
  text-align: right;
}

.space-data-panel {
  position: absolute;
  z-index: 5;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(640px, 92vw);
  border-left: 1px solid rgba(var(--space-rgb), 0.27);
  background: rgba(2, 5, 12, 0.95);
  box-shadow: -35px 0 100px rgba(0, 0, 0, 0.42);
  backdrop-filter: blur(24px);
  animation: space-panel-in 360ms cubic-bezier(0.2, 0.78, 0.24, 1) both;
}

.space-data-panel[hidden] {
  display: none;
}

.space-data-panel > header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 92px;
  padding: max(24px, env(safe-area-inset-top)) 28px 18px;
  border-bottom: 1px solid rgba(var(--space-rgb), 0.16);
}

.space-data-panel > header div {
  display: grid;
  gap: 7px;
}

.space-data-panel > header span {
  color: rgba(var(--space-rgb), 0.66);
  font: 700 7px/1 Consolas, monospace;
  letter-spacing: 0.18em;
}

.space-data-panel > header strong {
  font: 500 18px/1.2 "Yu Gothic", sans-serif;
  letter-spacing: 0.06em;
}

.space-data-panel > header button {
  width: 36px;
  height: 36px;
  border: 1px solid rgba(224, 234, 255, 0.18);
  border-radius: 50%;
  color: #eef3ff;
  background: transparent;
  cursor: pointer;
}

.space-data-scroll {
  height: calc(100% - 92px);
  padding: 22px 28px max(36px, env(safe-area-inset-bottom));
  overflow: auto;
}

.space-source-entry {
  padding: 19px 0 25px;
  border-bottom: 1px solid rgba(214, 226, 255, 0.11);
}

.space-source-entry:first-child {
  padding-top: 0;
}

.space-source-entry > p:first-child {
  margin: 0 0 8px;
  color: rgba(var(--space-rgb), 0.68);
  font: 700 7px/1 Consolas, monospace;
  letter-spacing: 0.15em;
}

.space-source-entry h4 {
  margin: 0;
  font: 550 16px/1.35 "Yu Gothic", sans-serif;
  letter-spacing: 0.045em;
}

.space-source-entry dl {
  display: grid;
  grid-template-columns: 94px minmax(0, 1fr);
  gap: 8px 13px;
  margin: 16px 0;
  color: rgba(226, 234, 251, 0.66);
  font: 520 9px/1.65 "Yu Gothic", sans-serif;
  letter-spacing: 0.045em;
}

.space-source-entry dt {
  color: rgba(202, 216, 246, 0.39);
  font: 700 7px/1.7 Consolas, monospace;
  letter-spacing: 0.12em;
}

.space-source-entry dd {
  margin: 0;
}

.space-source-entry a {
  color: rgb(var(--space-rgb));
  font: 700 8px/1 "Yu Gothic", sans-serif;
  letter-spacing: 0.08em;
  text-decoration: none;
}

.space-source-entry pre {
  max-height: 310px;
  margin: 16px 0 0;
  padding: 15px;
  overflow: auto;
  border: 1px solid rgba(var(--space-rgb), 0.12);
  color: rgba(214, 228, 255, 0.7);
  background: rgba(0, 0, 0, 0.24);
  font: 500 8px/1.65 Consolas, "Courier New", monospace;
  tab-size: 2;
}

.space-layer.is-entering .space-header,
.space-layer.is-entering .space-readout,
.space-layer.is-entering .space-mode-bank,
.space-layer.is-entering .space-footer {
  animation: space-interface-in 620ms cubic-bezier(0.2, 0.78, 0.24, 1) both;
}

.space-layer.is-entering .space-readout { animation-delay: 90ms; }
.space-layer.is-entering .space-mode-bank { animation-delay: 150ms; }
.space-layer.is-entering .space-footer { animation-delay: 210ms; }

@keyframes space-interface-in {
  from { opacity: 0; filter: blur(7px); transform: translateY(12px); }
  to { opacity: 1; filter: none; transform: none; }
}

@keyframes space-panel-in {
  from { opacity: 0; transform: translateX(34px); }
  to { opacity: 1; transform: none; }
}

@keyframes space-orbit-spin {
  to { transform: translate(-50%, -50%) rotate(378deg); }
}

@keyframes space-beacon {
  0%, 100% { opacity: 0.25; transform: scale(0.7); }
  50% { opacity: 1; transform: scale(1.25); }
}

@media (max-width: 900px) {
  .space-header {
    top: max(23px, env(safe-area-inset-top));
    left: max(20px, env(safe-area-inset-left));
    width: 58vw;
  }

  .space-header > p:last-child { display: none; }
  .space-close { top: max(22px, env(safe-area-inset-top)); right: max(18px, env(safe-area-inset-right)); }
  .space-close span:last-child { display: none; }

  .space-readout {
    bottom: max(257px, calc(env(safe-area-inset-bottom) + 237px));
    left: max(20px, env(safe-area-inset-left));
    width: min(410px, calc(100vw - 40px));
  }

  .space-narrative { max-width: 350px; }

  .space-mode-bank {
    right: max(20px, env(safe-area-inset-right));
    bottom: max(86px, calc(env(safe-area-inset-bottom) + 66px));
    left: max(20px, env(safe-area-inset-left));
    grid-template-columns: repeat(5, minmax(0, 1fr));
    width: auto;
  }

  .space-mode-option {
    grid-template-columns: 1fr;
    min-height: 55px;
    padding: 7px;
    text-align: center;
  }

  .space-mode-option span { grid-row: auto; font-size: 8px; }
  .space-mode-option strong { font-size: 7px; white-space: normal; }
  .space-mode-option small { display: none; }

  .space-footer {
    right: max(20px, env(safe-area-inset-right));
    bottom: max(18px, env(safe-area-inset-bottom));
    left: max(20px, env(safe-area-inset-left));
    grid-template-columns: auto 1fr;
  }

  .space-footer > p { display: none; }
}

@media (max-width: 600px) {
  .space-header h2 { font-size: 35px; }

  .space-readout {
    bottom: max(248px, calc(env(safe-area-inset-bottom) + 228px));
    padding-left: 11px;
  }

  .space-readout h3 { font-size: 18px; }
  .space-narrative { margin-top: 11px; font-size: 9px; line-height: 1.7; }
  .space-metric { margin-top: 11px; }
  .space-encoding { font-size: 7px; }
  .space-mode-bank { grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 3px; }
  .space-mode-option { min-height: 51px; padding: 6px 3px; }
  .space-mode-option strong { font-size: 6px; letter-spacing: 0.02em; }
  .space-data-button strong { font-size: 8px; }
  .space-data-scroll { padding-inline: 20px; }
  .space-source-entry dl { grid-template-columns: 76px minmax(0, 1fr); }
}

@media (max-height: 650px) and (orientation: landscape) {
  .space-header h2 { font-size: 34px; }
  .space-header > p:last-child { display: none; }
  .space-readout { bottom: 78px; width: 34vw; }
  .space-narrative { margin-top: 8px; font-size: 8px; line-height: 1.55; }
  .space-metric { margin-top: 8px; padding-top: 8px; }
  .space-encoding { margin-top: 7px; }
  .space-mode-bank { bottom: 76px; }
  .space-mode-option { min-height: 37px; }
  .space-footer { bottom: 14px; }
}

@media (prefers-reduced-motion: reduce) {
  .space-field::after,
  .space-field i,
  .space-layer.is-entering .space-header,
  .space-layer.is-entering .space-readout,
  .space-layer.is-entering .space-mode-bank,
  .space-layer.is-entering .space-footer {
    animation: none !important;
  }
}
