/* Arrakis-style orbital probe display — vintage amber / sand CRT on warm dark */

:root {
  --void: #0a0806;
  --panel: #14110c;
  --bezel: #1a1610;
  --bezel-edge: #3a3228;
  /* Main display & UI text: desert CRT yellow */
  --phosphor: #e4c06a;
  --phosphor-dim: #8f7a4a;
  --phosphor-rgb: 228, 192, 106;
  --phosphor-dim-rgb: 143, 122, 74;
  /* Spice / title accent */
  --amber: #e8a928;
  --hazard: #c45c2a;
  --rivet: #4a4034;
  --font-ui: "IBM Plex Mono", "SF Mono", "Consolas", "Menlo", monospace;
  --font-grid: "JetBrains Mono", "SF Mono", "Consolas", monospace;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
  height: 100%;
  max-height: 100dvh;
  overflow: hidden;
  background: radial-gradient(ellipse 120% 80% at 50% 18%, #1a1510 0%, var(--void) 55%);
  color: var(--phosphor);
  font-family: var(--font-ui);
  font-size: 13px;
}

.deck {
  box-sizing: border-box;
  height: 100%;
  max-height: 100dvh;
  max-width: min(1480px, 100%);
  margin: 0 auto;
  padding: 0.5rem 0.65rem 0.45rem;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.deck__header {
  flex-shrink: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.35rem 0.75rem;
  margin-bottom: 0.45rem;
  padding-bottom: 0.35rem;
  border-bottom: 1px solid var(--bezel-edge);
}

.deck__brand {
  font-weight: 700;
  letter-spacing: 0.22em;
  font-size: 0.85rem;
  color: var(--amber);
  text-shadow: 0 0 14px rgba(232, 169, 40, 0.45);
}

.deck__subtitle {
  color: var(--phosphor-dim);
  font-size: 0.75rem;
  letter-spacing: 0.08em;
}

.deck__tick {
  margin-left: 0;
  font-variant-numeric: tabular-nums;
  color: var(--phosphor);
  opacity: 0.9;
}

.deck__lang-toggle {
  margin-left: auto;
}

.console {
  flex: 1 1 0;
  min-height: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem 0.75rem;
  align-items: stretch;
}

/* --- Bezel & square viewport (rounded CRT frame) --- */
.bezel {
  --bezel-radius: 16px;
  --screen-radius: 12px;
  position: relative;
  flex: 1 1 0;
  min-width: min(100%, 420px);
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 1.1rem 1rem 0.85rem;
  background: linear-gradient(165deg, var(--bezel) 0%, #14110c 40%, #0c0a08 100%);
  border: 3px solid var(--bezel-edge);
  border-radius: var(--bezel-radius);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.06),
    inset 0 -2px 8px rgba(0, 0, 0, 0.5),
    0 12px 40px rgba(0, 0, 0, 0.55),
    0 0 0 1px rgba(0, 0, 0, 0.4);
}

.bezel::before {
  content: "";
  position: absolute;
  inset: 10px;
  border: 1px dashed rgba(var(--phosphor-rgb), 0.14);
  border-radius: calc(var(--bezel-radius) - 6px);
  pointer-events: none;
}

.bezel__corner {
  position: absolute;
  width: 14px;
  height: 14px;
  border: 2px solid var(--rivet);
  border-radius: 5px;
  background: linear-gradient(135deg, #2c261c, #1a1610);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08);
  pointer-events: none;
}

.bezel__corner--tl {
  top: 8px;
  left: 8px;
}
.bezel__corner--tr {
  top: 8px;
  right: 8px;
}
.bezel__corner--bl {
  bottom: 8px;
  left: 8px;
}
.bezel__corner--br {
  bottom: 8px;
  right: 8px;
}

.bezel__label {
  position: absolute;
  font-size: 0.55rem;
  letter-spacing: 0.18em;
  color: var(--phosphor-dim);
  opacity: 0.75;
  pointer-events: none;
}

.bezel__label--tl {
  top: 10px;
  left: 28px;
}
.bezel__label--tr {
  top: 10px;
  right: 28px;
}
.bezel__label--bl {
  bottom: 10px;
  left: 28px;
}
.bezel__label--br {
  bottom: 10px;
  right: 28px;
}

/* Fills remaining bezel height; square viewport uses min(cqw,cqh) inside this box */
.bezel__viewport-host {
  flex: 1 1 0;
  min-height: 0;
  min-width: 0;
  margin-top: 0.65rem;
  container-type: size;
  container-name: bezel-screen;
  display: flex;
  align-items: center;
  justify-content: center;
}

.viewport {
  position: relative;
  aspect-ratio: 1 / 1;
  box-sizing: border-box;
  margin-top: 0;
  background: #080705;
  border: 2px solid #2a241c;
  border-radius: var(--screen-radius, 12px);
  overflow: hidden;
  cursor: grab;
  box-shadow:
    inset 0 0 80px rgba(0, 0, 0, 0.85),
    inset 0 0 22px rgba(var(--phosphor-rgb), 0.07);
  max-width: 100%;
  max-height: 100%;
  height: auto;
  /* Fallback when container query units are unavailable: cap by viewport height */
  width: min(100%, 90dvh);
}

@supports (width: 1cqw) {
  .viewport {
    width: min(100cqw, 100cqh);
  }
}

.viewport:active {
  cursor: grabbing;
}

.viewport__inner {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.pan-zoom-layer {
  transform-origin: center center;
  will-change: transform;
  width: max-content;
  height: max-content;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.viewport__grain {
  position: absolute;
  inset: 0;
  opacity: 0.07;
  pointer-events: none;
  z-index: 2;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  animation: grain 0.4s steps(2) infinite;
}

.viewport__scanlines {
  position: absolute;
  inset: 0;
  z-index: 3;
  pointer-events: none;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 2px,
    rgba(0, 0, 0, 0.13) 2px,
    rgba(0, 0, 0, 0.13) 3px
  );
  mix-blend-mode: multiply;
}

@keyframes grain {
  0% {
    transform: translate(0, 0);
  }
  50% {
    transform: translate(-2%, 1%);
  }
  100% {
    transform: translate(1%, -1%);
  }
}

/* Row sweep keyframes: injected by orbital-probe.js as glyph-sweep-beam */

/* Square matrix: N×N equal cells (monospace <pre> is taller-than-wide; grid fixes that)
   --matrix-zoom: layout zoom via width + font-size (sharp); pan-zoom-layer uses translate only */
.grid-matrix {
  --grid-dim: 10;
  --matrix-zoom: 1;
  display: grid;
  grid-template-columns: repeat(var(--grid-dim), minmax(0, 1fr));
  grid-template-rows: repeat(var(--grid-dim), minmax(0, 1fr));
  aspect-ratio: 1 / 1;
  /* Base square fits host; × zoom may exceed viewport and clip (sharp glyphs, no transform scale) */
  width: calc(min(100%, 90dvh) * var(--matrix-zoom));
  max-width: none;
  max-height: none;
  box-sizing: border-box;
  margin: 0;
  user-select: none;
  color: var(--phosphor);
  font-family: var(--font-grid);
  line-height: 1;
  font-size: clamp(
    4px,
    calc(72vmin * var(--matrix-zoom) / var(--grid-dim) * 0.88),
    calc(28px * var(--matrix-zoom))
  );
  text-rendering: geometricPrecision;
}

@supports (width: 1cqw) {
  .grid-matrix {
    width: calc(min(100cqw, 100cqh) * var(--matrix-zoom));
    font-size: clamp(
      4px,
      calc(min(100cqw, 100cqh) * var(--matrix-zoom) / var(--grid-dim) * 0.88),
      calc(28px * var(--matrix-zoom))
    );
  }
}

.grid-cell {
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  min-width: 0;
  min-height: 0;
  font-size: inherit;
  text-shadow: 0 0 4px rgba(var(--phosphor-rgb), 0.28);
}

/* After .grid-cell so overflow/animation win; full shorthand so duration is never default 0s */
.grid-cell--empty {
  opacity: 0.22;
  animation: none;
}

.grid-cell.grid-cell--row-pulse {
  position: relative;
  z-index: 1;
  overflow: visible;
  opacity: 0;
  will-change: opacity, transform, color, text-shadow;
}

/* --- Controls --- */
.controls {
  flex: 0 0 auto;
  width: min(200px, 100%);
  max-height: 100%;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  padding: 0.65rem 0.75rem;
  background: var(--panel);
  border: 1px solid var(--bezel-edge);
  border-radius: 4px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
}

.controls__caption {
  display: block;
  font-size: 0.6rem;
  letter-spacing: 0.2em;
  color: var(--amber);
  margin-bottom: 0.5rem;
}

.controls__row {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  align-items: center;
}

.btn {
  font-family: inherit;
  font-size: 0.8rem;
  cursor: pointer;
  border: 1px solid var(--bezel-edge);
  background: linear-gradient(180deg, #221c14 0%, #14110c 100%);
  color: var(--phosphor);
  border-radius: 3px;
  padding: 0.35rem 0.65rem;
  transition:
    background 0.12s,
    box-shadow 0.12s;
}

.btn:hover {
  background: linear-gradient(180deg, #2e2618 0%, #1c1810 100%);
  box-shadow: 0 0 10px rgba(var(--phosphor-rgb), 0.18);
}

.btn:active {
  transform: translateY(1px);
}

.btn--round {
  width: 2.25rem;
  height: 2.25rem;
  padding: 0;
  border-radius: 50%;
  font-size: 1.1rem;
  line-height: 1;
}

.btn--small {
  font-size: 0.65rem;
  letter-spacing: 0.08em;
}

.dpad {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: auto auto auto;
  gap: 4px;
  width: 132px;
  justify-items: center;
}

.dpad__btn {
  width: 40px;
  height: 36px;
  font-size: 0.75rem;
  cursor: pointer;
  border: 1px solid var(--bezel-edge);
  background: linear-gradient(180deg, #262018 0%, #16140e 100%);
  color: var(--phosphor-dim);
  border-radius: 4px;
}

.dpad__btn:hover {
  color: var(--phosphor);
  border-color: var(--phosphor-dim);
  box-shadow: 0 0 8px rgba(var(--phosphor-rgb), 0.16);
}

.dpad__btn--n {
  grid-column: 2;
  grid-row: 1;
}
.dpad__btn--w {
  grid-column: 1;
  grid-row: 2;
}
.dpad__btn--e {
  grid-column: 3;
  grid-row: 2;
}
.dpad__btn--s {
  grid-column: 2;
  grid-row: 3;
}

.dpad__hub {
  grid-column: 2;
  grid-row: 2;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #302820, #12100c);
  border: 2px solid var(--bezel-edge);
  box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.6);
  pointer-events: none;
  z-index: 0;
}

.dpad__btn--n,
.dpad__btn--s,
.dpad__btn--w,
.dpad__btn--e {
  z-index: 1;
}

.controls__block--hint .legend {
  margin: 0;
  max-width: 100%;
  font-size: 0.58rem;
  line-height: 1.4;
  color: var(--phosphor-dim);
  overflow-wrap: anywhere;
  word-break: break-word;
}

.legend__row {
  display: grid;
  grid-template-columns: minmax(5.6em, max-content) minmax(0, 1fr);
  gap: 0.2rem 0.45rem;
  align-items: start;
  margin-bottom: 0.4rem;
}

.legend__row:last-child {
  margin-bottom: 0;
}

.legend__row--stack {
  grid-template-columns: 1fr;
}

.legend__row--stack dt {
  margin-bottom: 0.15rem;
}

.legend__row dt {
  margin: 0;
  font-size: 0.56rem;
  letter-spacing: 0.14em;
  color: var(--amber);
  font-weight: 600;
}

.legend__row dd {
  margin: 0;
}

.legend-glyph {
  display: inline-block;
  min-width: 1em;
  font-family: var(--font-grid);
  font-weight: 600;
  color: var(--phosphor);
}

.legend-sep {
  margin: 0 0.2em;
  opacity: 0.45;
}

.legend-terrain {
  margin: 0.35rem 0 0;
  padding-left: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.22rem;
}

.legend-terrain li {
  display: flex;
  flex-wrap: nowrap;
  align-items: baseline;
  gap: 0.35rem;
}

.legend-terrain__name {
  flex: 1;
  min-width: 0;
}

.legend-terrain code {
  flex-shrink: 0;
  font-family: var(--font-grid);
  font-size: 0.85em;
  color: var(--phosphor-dim);
  opacity: 0.85;
}

.deck__footer {
  flex-shrink: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 0.75rem;
  margin-top: 0.4rem;
  padding-top: 0.35rem;
  border-top: 1px solid var(--bezel-edge);
  font-size: 0.65rem;
  color: var(--phosphor-dim);
}

/* Phone / narrow: screen on top, controls below (DOM order: bezel → controls) */
@media (max-width: 719px) {
  .deck {
    padding-left: max(0.45rem, env(safe-area-inset-left, 0px));
    padding-right: max(0.45rem, env(safe-area-inset-right, 0px));
    padding-bottom: max(0.4rem, env(safe-area-inset-bottom, 0px));
  }

  .deck__header {
    gap: 0.25rem 0.5rem;
  }

  .deck__subtitle {
    flex-basis: 100%;
    font-size: 0.68rem;
  }

  .deck__tick {
    margin-left: auto;
    font-size: 0.72rem;
  }

  .console {
    flex-direction: column;
    flex-wrap: nowrap;
    align-items: stretch;
    gap: 0.55rem;
    min-height: 0;
  }

  .bezel {
    flex: 1 1 0;
    min-width: 0;
    width: 100%;
    min-height: 0;
    padding: 0.75rem 0.65rem 0.65rem;
  }

  .bezel__viewport-host {
    flex: 1 1 0;
    min-height: min(52vw, 58dvh);
  }

  .controls {
    flex: 0 1 auto;
    width: 100%;
    max-width: none;
    max-height: min(38dvh, 360px);
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    align-self: stretch;
    overscroll-behavior: contain;
  }

  .controls__row {
    justify-content: flex-start;
  }

  .dpad {
    margin: 0 auto;
  }
}

/* Side-by-side: controls column matches console row height */
@media (min-width: 720px) {
  .console {
    flex-wrap: nowrap;
  }

  .controls {
    width: 188px;
    align-self: stretch;
  }
}

/* Very short viewports: trim chrome */
@media (max-height: 520px) {
  .deck {
    padding-top: 0.35rem;
    padding-bottom: 0.25rem;
  }

  .controls__block--hint {
    display: none;
  }

  .bezel {
    padding: 0.65rem 0.75rem 0.55rem;
  }
}

#statusLine.error {
  color: var(--hazard);
}
