:root {
  color-scheme: dark;
  --bg-deep: #02070a;
  --text-main: rgba(226, 255, 252, 0.9);
  --text-sub: rgba(137, 222, 216, 0.68);
  --text-dim: rgba(151, 213, 207, 0.52);
  --panel-bg: rgba(2, 13, 15, 0.62);
  --panel-line: rgba(115, 245, 225, 0.16);
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  min-width: 320px;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: radial-gradient(circle at 50% 44%, #07191b 0%, #030b0d 42%, var(--bg-deep) 100%);
  color: var(--text-main);
  font-family: "Inter", "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  letter-spacing: 0;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  user-select: none;
  touch-action: none;
}

body {
  position: fixed;
  inset: 0;
  overscroll-behavior: none;
}

.cosmic-stage,
.webgl-root,
.webgl-root canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
}

.cosmic-stage {
  min-height: 100vh;
  min-height: 100dvh;
  overflow: hidden;
  isolation: isolate;
  background:
    radial-gradient(circle at 50% 50%, rgba(44, 210, 188, 0.08), transparent 34%),
    linear-gradient(180deg, rgba(4, 18, 19, 0.22), rgba(0, 0, 0, 0.48));
}

.webgl-root {
  z-index: 0;
}

.webgl-root canvas {
  display: block;
  outline: none;
}

.loading-screen {
  position: fixed;
  z-index: 8;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 10px;
  background:
    radial-gradient(circle at 50% 48%, rgba(69, 240, 221, 0.1), transparent 34%),
    rgba(2, 7, 10, 0.96);
  color: rgba(234, 255, 251, 0.92);
  text-align: center;
  transition: opacity 520ms ease, visibility 520ms ease;
}

.loading-screen p,
.loading-screen span {
  margin: 0;
}

.loading-screen p {
  font-size: clamp(1.08rem, 2vw, 1.62rem);
  font-weight: 500;
  line-height: 1;
  text-shadow: 0 0 22px rgba(73, 242, 221, 0.28);
}

.loading-screen span {
  color: var(--text-sub);
  font-size: 0.66rem;
  line-height: 1.3;
}

.loading-screen.is-hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.stage-title {
  position: fixed;
  z-index: 2;
  top: max(24px, env(safe-area-inset-top));
  left: max(22px, env(safe-area-inset-left));
  display: grid;
  gap: 7px;
  pointer-events: none;
  text-shadow: 0 0 22px rgba(60, 237, 218, 0.25);
}

.stage-title p,
.stage-title span {
  margin: 0;
  white-space: nowrap;
}

.stage-title p {
  font-size: clamp(1.15rem, 1.7vw, 1.85rem);
  font-weight: 500;
  line-height: 1;
}

.stage-title span {
  color: var(--text-sub);
  font-size: clamp(0.62rem, 0.86vw, 0.84rem);
  font-weight: 400;
  line-height: 1.25;
}

.global-controls {
  position: fixed;
  z-index: 4;
  top: max(22px, env(safe-area-inset-top));
  right: max(22px, env(safe-area-inset-right));
  display: flex;
  gap: 8px;
  align-items: center;
}

.global-controls button,
.global-controls a,
.info-panel button,
.system-actions button,
.system-actions a {
  min-height: 34px;
  border: 1px solid rgba(116, 237, 220, 0.18);
  border-radius: 4px;
  background: rgba(7, 31, 33, 0.56);
  color: rgba(224, 255, 250, 0.8);
  font: inherit;
  font-size: 0.64rem;
  line-height: 1;
  text-decoration: none;
  cursor: pointer;
  backdrop-filter: blur(8px);
}

.global-controls button,
.global-controls a {
  display: inline-grid;
  min-width: 72px;
  padding: 0 11px;
  place-items: center;
}

.global-controls button:hover,
.global-controls a:hover,
.info-panel button:hover,
.system-actions button:hover,
.system-actions a:hover {
  background: rgba(24, 68, 66, 0.62);
}

.global-controls button:focus-visible,
.global-controls a:focus-visible,
.info-panel button:focus-visible,
.system-actions button:focus-visible,
.system-actions a:focus-visible {
  outline: 1px solid rgba(142, 255, 238, 0.78);
  outline-offset: 2px;
}

.archive-hint {
  position: fixed;
  z-index: 2;
  left: 50%;
  bottom: max(74px, calc(env(safe-area-inset-bottom) + 52px));
  margin: 0;
  color: rgba(202, 252, 244, 0.74);
  font-size: clamp(0.62rem, 0.88vw, 0.78rem);
  line-height: 1.2;
  text-align: center;
  text-shadow: 0 0 18px rgba(72, 242, 221, 0.24);
  transform: translateX(-50%);
  transition: opacity 420ms ease;
  pointer-events: none;
}

.archive-hint.is-hidden {
  opacity: 0;
}

.info-panel,
.system-message {
  position: fixed;
  z-index: 5;
  width: min(320px, calc(100vw - 44px));
  display: grid;
  gap: 14px;
  padding: 17px;
  border: 1px solid var(--panel-line);
  background: rgba(2, 13, 15, 0.76);
  box-shadow: 0 22px 80px rgba(0, 0, 0, 0.28);
  color: var(--text-main);
  backdrop-filter: blur(12px);
}

#system-message[hidden] {
  display: none !important;
}

.info-panel {
  top: max(74px, calc(env(safe-area-inset-top) + 64px));
  right: max(22px, env(safe-area-inset-right));
  opacity: 0;
  pointer-events: none;
  transform: translate3d(0, -10px, 0);
  transition: opacity 260ms ease, transform 260ms ease;
}

.info-panel.is-open {
  opacity: 1;
  pointer-events: auto;
  transform: translate3d(0, 0, 0);
}

.info-panel p,
.info-panel h2,
.system-message p,
.system-message h2 {
  margin: 0;
}

.info-kicker,
.system-kicker {
  color: var(--text-dim);
  font-size: 0.58rem;
  line-height: 1.2;
}

.info-panel h2,
.system-message h2 {
  margin-top: 6px;
  color: rgba(237, 255, 251, 0.9);
  font-size: 1rem;
  font-weight: 500;
  line-height: 1.08;
}

.info-panel div > p:last-child,
.system-message div > p:last-child {
  margin-top: 9px;
  color: rgba(194, 239, 231, 0.74);
  font-size: 0.74rem;
  line-height: 1.55;
}

.info-panel button,
.system-actions button,
.system-actions a {
  min-width: 96px;
  padding: 0 12px;
}

.webgl-fallback {
  position: fixed;
  z-index: 3;
  inset: auto 24px 32px;
  margin: 0;
  color: rgba(226, 255, 252, 0.82);
  font-size: 0.9rem;
  line-height: 1.7;
  text-align: center;
  pointer-events: none;
}

.archive-overlay {
  position: fixed;
  z-index: 2;
  right: max(24px, env(safe-area-inset-right));
  bottom: max(24px, env(safe-area-inset-bottom));
  width: min(320px, calc(100vw - 48px));
  display: grid;
  gap: 7px;
  pointer-events: none;
  text-align: right;
  text-shadow: 0 0 18px rgba(50, 220, 200, 0.2);
}

.archive-overlay p {
  margin: 0;
}

.archive-kicker {
  color: var(--text-dim);
  font-size: clamp(0.58rem, 0.72vw, 0.7rem);
  line-height: 1.2;
}

.archive-record {
  color: rgba(232, 255, 250, 0.82);
  font-size: clamp(0.78rem, 1vw, 0.98rem);
  line-height: 1.2;
}

.archive-layer {
  color: var(--text-sub);
  font-size: clamp(0.62rem, 0.78vw, 0.76rem);
  line-height: 1.25;
}

.node-label {
  position: fixed;
  z-index: 2;
  min-width: 112px;
  max-width: 180px;
  display: grid;
  gap: 3px;
  opacity: 0;
  transform: translate3d(-50%, -50%, 0);
  pointer-events: none;
  text-shadow: 0 0 16px rgba(54, 238, 216, 0.24);
  transition: opacity 520ms ease;
}

.node-label span,
.node-label small {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.node-label span {
  color: rgba(235, 255, 251, 0.82);
  font-size: 0.72rem;
  line-height: 1.1;
}

.node-label small {
  color: var(--text-dim);
  font-size: 0.55rem;
  line-height: 1.1;
}

.node-label.is-visible {
  opacity: 1;
}

.archive-panel {
  position: fixed;
  z-index: 3;
  right: max(24px, env(safe-area-inset-right));
  top: 50%;
  width: min(340px, calc(100vw - 48px));
  max-height: calc(100dvh - 92px);
  display: grid;
  gap: 16px;
  padding: 18px;
  border: 1px solid var(--panel-line);
  background: var(--panel-bg);
  color: var(--text-main);
  opacity: 0;
  pointer-events: none;
  transform: translate3d(18px, -50%, 0);
  transition: opacity 360ms ease, transform 360ms ease;
  backdrop-filter: blur(10px);
}

.archive-panel.is-open {
  opacity: 1;
  pointer-events: auto;
  transform: translate3d(0, -50%, 0);
}

.archive-panel p,
.archive-panel h2,
.archive-panel dl {
  margin: 0;
}

.archive-panel-copy {
  display: grid;
  gap: 9px;
}

.panel-kicker {
  color: var(--text-dim);
  font-size: 0.62rem;
  line-height: 1.25;
}

.archive-panel h2 {
  color: rgba(237, 255, 251, 0.9);
  font-size: clamp(1rem, 1.25vw, 1.28rem);
  font-weight: 500;
  line-height: 1.08;
}

.panel-text {
  color: rgba(194, 239, 231, 0.72);
  font-size: 0.78rem;
  line-height: 1.55;
}

.panel-meta {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  padding-top: 3px;
}

.panel-meta div {
  min-width: 0;
}

.panel-meta dt {
  color: var(--text-dim);
  font-size: 0.54rem;
  line-height: 1.2;
}

.panel-meta dd {
  margin: 3px 0 0;
  overflow: hidden;
  color: rgba(232, 255, 250, 0.82);
  font-size: 0.68rem;
  line-height: 1.2;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

.archive-actions button {
  min-height: 34px;
  border: 1px solid rgba(116, 237, 220, 0.18);
  border-radius: 4px;
  background: rgba(13, 42, 43, 0.42);
  color: rgba(224, 255, 250, 0.78);
  font: inherit;
  font-size: 0.66rem;
  line-height: 1;
  cursor: pointer;
}

.archive-actions button:focus-visible {
  outline: 1px solid rgba(142, 255, 238, 0.72);
  outline-offset: 2px;
}

.archive-actions button:hover {
  background: rgba(26, 68, 66, 0.58);
}

.system-message {
  top: 50%;
  left: 50%;
  text-align: center;
  transform: translate3d(-50%, -50%, 0);
}

.system-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
}

.system-actions a {
  display: inline-grid;
  place-items: center;
}

@media (max-width: 720px) {
  .stage-title {
    top: max(18px, env(safe-area-inset-top));
    left: max(16px, env(safe-area-inset-left));
    gap: 6px;
  }

  .stage-title p {
    font-size: clamp(1rem, 5.4vw, 1.35rem);
  }

  .stage-title span {
    max-width: calc(100vw - 32px);
    font-size: clamp(0.58rem, 2.8vw, 0.72rem);
    white-space: normal;
  }

  .global-controls {
    top: auto;
    right: max(12px, env(safe-area-inset-right));
    bottom: max(12px, env(safe-area-inset-bottom));
    left: max(12px, env(safe-area-inset-left));
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
  }

  .global-controls button,
  .global-controls a {
    min-width: 0;
    min-height: 40px;
    padding: 0 6px;
    font-size: 0.56rem;
  }

  .archive-hint {
    bottom: max(66px, calc(env(safe-area-inset-bottom) + 58px));
    width: calc(100vw - 40px);
    font-size: 0.62rem;
  }

  .archive-hint::before {
    content: "TOUCH A MEMORY NODE";
  }

  .archive-hint {
    font-size: 0;
  }

  .archive-hint::before {
    font-size: 0.62rem;
  }

  .info-panel {
    top: auto;
    right: max(12px, env(safe-area-inset-right));
    bottom: max(64px, calc(env(safe-area-inset-bottom) + 54px));
    left: max(12px, env(safe-area-inset-left));
    width: auto;
    padding: 14px;
  }

  .archive-overlay {
    right: max(16px, env(safe-area-inset-right));
    bottom: max(72px, calc(env(safe-area-inset-bottom) + 62px));
    width: min(230px, calc(100vw - 32px));
    gap: 5px;
  }

  .archive-kicker,
  .archive-layer {
    display: none;
  }

  .archive-record {
    font-size: clamp(0.64rem, 3vw, 0.78rem);
  }

  .node-label {
    display: none;
  }

  .archive-panel {
    top: auto;
    right: max(12px, env(safe-area-inset-right));
    bottom: max(62px, calc(env(safe-area-inset-bottom) + 52px));
    left: max(12px, env(safe-area-inset-left));
    width: auto;
    max-height: 38dvh;
    padding: 14px;
    gap: 12px;
    transform: translate3d(0, 18px, 0);
  }

  .archive-panel.is-open {
    transform: translate3d(0, 0, 0);
  }

  .archive-panel h2 {
    font-size: 0.92rem;
  }

  .panel-text {
    display: -webkit-box;
    overflow: hidden;
    font-size: 0.72rem;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
  }

  .archive-actions button {
    min-height: 44px;
    font-size: 0.62rem;
  }

  .system-message {
    width: calc(100vw - 32px);
    padding: 15px;
  }
}

@media (orientation: landscape) and (max-height: 520px) {
  .stage-title {
    top: max(12px, env(safe-area-inset-top));
    left: max(14px, env(safe-area-inset-left));
  }

  .stage-title p {
    font-size: 0.98rem;
  }

  .stage-title span {
    font-size: 0.56rem;
  }

  .global-controls {
    top: max(10px, env(safe-area-inset-top));
    right: max(10px, env(safe-area-inset-right));
    bottom: auto;
    left: auto;
    display: flex;
  }

  .global-controls button,
  .global-controls a {
    min-width: 62px;
    min-height: 32px;
    font-size: 0.52rem;
  }

  .info-panel {
    top: max(52px, calc(env(safe-area-inset-top) + 42px));
    right: max(10px, env(safe-area-inset-right));
    bottom: auto;
    left: auto;
    width: min(280px, 48vw);
  }

  .archive-overlay {
    right: max(14px, env(safe-area-inset-right));
    bottom: max(12px, env(safe-area-inset-bottom));
    width: 210px;
  }

  .archive-record {
    font-size: 0.62rem;
  }

  .archive-panel {
    top: max(12px, env(safe-area-inset-top));
    right: max(12px, env(safe-area-inset-right));
    bottom: max(12px, env(safe-area-inset-bottom));
    left: auto;
    width: min(260px, 42vw);
    max-height: calc(100dvh - 24px);
    padding: 12px;
    transform: translate3d(16px, 0, 0);
  }

  .archive-panel.is-open {
    transform: translate3d(0, 0, 0);
  }

  .panel-meta {
    grid-template-columns: 1fr;
  }
}

@media (prefers-reduced-motion: reduce) {
  .cosmic-stage {
    background:
      radial-gradient(circle at 50% 50%, rgba(44, 210, 188, 0.05), transparent 34%),
      linear-gradient(180deg, rgba(4, 18, 19, 0.18), rgba(0, 0, 0, 0.52));
  }

  .loading-screen,
  .archive-hint,
  .info-panel,
  .archive-panel,
  .node-label {
    transition-duration: 1ms;
  }
}
