/* Colors come from palette tokens (src/render/palette.js), set as --c-* at boot. */

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

/* `hidden` wins over any display rule below. */
[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--c-space);
  color: var(--c-ink);
}

html {
  color-scheme: dark;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Noto Sans', 'Helvetica Neue', Arial, sans-serif;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* The canvas fills the whole screen, under the camera cutout too. */
#board {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

/* The DOM UI layer sits inside the safe area. It lets touches through to the
   canvas except on its own controls. */
#ui {
  position: fixed;
  top: env(safe-area-inset-top, 0px);
  right: env(safe-area-inset-right, 0px);
  bottom: env(safe-area-inset-bottom, 0px);
  left: env(safe-area-inset-left, 0px);
  pointer-events: none;
}

#screen {
  position: absolute;
  inset: 0;
}

#ui button,
#ui textarea,
#ui .dev-panel {
  pointer-events: auto;
}

button,
textarea {
  font: inherit;
  color: inherit;
  touch-action: manipulation;
}

textarea {
  user-select: text;
  -webkit-user-select: text;
}

/* Buttons: touch targets are at least 48 CSS px. */
.btn {
  min-width: 48px;
  min-height: 48px;
  padding: 0 16px;
  border: 1px solid var(--c-line);
  border-radius: 10px;
  background: var(--c-space-hi);
  font-weight: 600;
}

.btn[aria-pressed='true'] {
  border-color: var(--c-accent);
  color: var(--c-accent);
}

.btn:active {
  filter: brightness(1.3);
}

/* Title */
.title {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px;
  text-align: center;
}

.title h1 {
  margin: 0;
  font-size: clamp(32px, 10vh, 76px);
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  text-shadow: 0 2px 24px rgba(0, 0, 0, 0.7);
}

.title .tag {
  margin: 0.6em 0 0;
  color: var(--c-ink-dim);
  font-size: clamp(13px, 3.4vh, 18px);
}

/* Builder: the palette drawer on the left, tools on the right, and the
   board (drawn on the canvas underneath) takes the rest. */
.builder {
  --tile: 56px;
  --gap: 6px;
  position: absolute;
  inset: 0;
  display: flex;
}

/* The drawer: Parts and Ship tabs over the palette or the ship panel. Extra
   room on the left keeps a drag off Android's back-gesture edge. */
.bld-drawer {
  flex: none;
  display: flex;
  flex-direction: column;
  width: calc(3 * var(--tile) + 2 * var(--gap) + 24px);
  border-right: 1px solid var(--c-line);
  background: var(--c-panel);
  pointer-events: auto;
}

.bld-tabs {
  flex: none;
  display: flex;
  gap: var(--gap);
  padding: 8px 8px 0 16px;
}

#ui .bld-tab {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 0;
  padding: 0 6px;
  font-size: 13px;
}

#ui .bld-tab[aria-selected='true'] {
  border-color: var(--c-accent);
  color: var(--c-accent);
}

.bld-badge {
  min-width: 20px;
  padding: 1px 6px;
  border-radius: 10px;
  font-size: 11px;
  line-height: 16px;
  color: var(--c-space);
}

.bld-badge[data-tone='bad'] {
  background: var(--c-bad);
}

.bld-badge[data-tone='good'] {
  background: var(--c-good);
}

.bld-palette {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(3, var(--tile));
  grid-auto-rows: var(--tile);
  gap: var(--gap);
  align-content: end;
  padding: 8px 8px 8px 16px;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: none;
  touch-action: none;
}

/* The Ship tab: flight check, Launch and totals. It scrolls when long. */
.bld-ship {
  flex: 1;
  min-height: 0;
  padding: 4px 8px 12px 16px;
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
  font-size: 12.5px;
  line-height: 1.35;
}

.bld-ship h3 {
  margin: 8px 0 6px;
  color: var(--c-ink-dim);
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.bld-checks {
  margin: 0;
  padding: 0;
  list-style: none;
}

.bld-checks li {
  display: flex;
  gap: 6px;
  padding: 4px 0;
  border-bottom: 1px solid var(--c-line);
}

.bld-mark {
  flex: none;
  padding-top: 1px;
}

.bld-checks li[data-state='ok'] .bld-mark {
  color: var(--c-good);
}

.bld-checks li[data-state='fail'] .bld-mark {
  color: var(--c-bad);
}

.bld-checks li[data-state='wait'] .bld-mark,
.bld-checks li[data-state='wait'] .bld-check-text {
  color: var(--c-ink-dim);
}

.bld-check {
  min-width: 0;
  flex: 1;
}

.bld-check p {
  margin: 0;
}

.bld-check-fix {
  color: var(--c-accent);
}

#ui .bld-fix {
  width: 100%;
  margin: 4px 0 2px;
  padding: 4px 8px;
  text-align: left;
  font-size: 12px;
  font-weight: 600;
  color: var(--c-accent);
}

/* Launch stays in view at the bottom of the tab while a long checklist
   scrolls under it. */
#ui .bld-launch {
  position: sticky;
  bottom: 0;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 52px;
  margin-top: 10px;
  font-size: 15px;
  letter-spacing: 0.04em;
  box-shadow: 0 -10px 12px var(--c-panel);
}

#ui .bld-launch small {
  font-size: 11px;
  font-weight: 500;
}

/* Dim text on a solid face: the list scrolls under it. */
#ui .bld-launch[data-state='locked'] {
  color: var(--c-ink-dim);
}

#ui .bld-launch[data-state='ready'] {
  border-color: var(--c-good);
  color: var(--c-good);
}

#ui .bld-launch[data-state='armed'] {
  border-color: var(--c-accent);
  background: var(--c-accent);
  color: var(--c-space);
}

.bld-stats {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2px 8px;
  margin: 0;
}

.bld-stats div {
  display: contents;
}

.bld-stats dt {
  color: var(--c-ink-dim);
}

.bld-stats dd {
  margin: 0;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#ui .bld-part {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  min-width: 0;
  padding: 3px 1px;
  border: 1px solid var(--c-line);
  border-radius: 10px;
  background: var(--c-space-hi);
  /* Shrinks with the tile so "generator" fits a 48 px tile. */
  font-size: min(10.5px, calc(var(--tile) * 0.18));
  font-weight: 600;
  line-height: 1.1;
  text-align: center;
  overflow: hidden;
  overflow-wrap: anywhere;
  touch-action: none;
  -webkit-touch-callout: none;
}

/* Only a drawer taller than the screen scrolls; then a part has to be
   dragged out sideways. */
.bld-palette[data-scrolls],
#ui .bld-palette[data-scrolls] .bld-part {
  touch-action: pan-y;
}

.bld-part svg {
  flex: none;
  max-width: 100%;
  width: auto;
  height: auto;
  max-height: calc(var(--tile) * 0.36);
}

.bld-part[aria-pressed='true'] {
  border-color: var(--c-accent);
  box-shadow: inset 0 0 0 1px var(--c-accent);
  color: var(--c-accent);
}

.bld-board {
  position: relative;
  flex: 1;
  min-width: 0;
  pointer-events: auto;
  touch-action: none;
  -webkit-touch-callout: none;
}

.bld-tools {
  flex: none;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: var(--gap);
  padding: 8px;
}

#ui .bld-tool,
#ui .bld-fit {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  width: 56px;
  height: 54px;
  padding: 0;
  font-size: 10.5px;
}

#ui .bld-fit {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 48px;
  height: 48px;
  background: var(--c-panel);
}

.btn:disabled {
  opacity: 0.35;
  filter: none;
}

/* Short screens (the Fold's cover screen in a browser tab): tools shrink to
   the 48px minimum so all four fit under the dev button. */
@media (max-height: 320px) {
  .builder {
    --gap: 4px;
  }

  #ui .bld-tool {
    height: 48px;
  }
}

.bld-status,
.bld-layout {
  position: absolute;
  max-width: calc(100% - 16px);
  margin: 0;
  padding: 5px 10px;
  border-radius: 12px;
  background: rgba(7, 10, 20, 0.82);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
}

.bld-status {
  left: 50%;
  bottom: 4px;
  transform: translateX(-50%);
  font-size: 13px;
}

.bld-layout {
  top: 4px;
  left: 8px;
  max-width: calc(100% - 72px);
  font-size: 12px;
}

.bld-status[data-tone='bad'],
.bld-layout[data-tone='bad'] {
  color: var(--c-bad);
}

.bld-status[data-tone='good'],
.bld-layout[data-tone='good'] {
  color: var(--c-good);
}

.bld-card {
  position: absolute;
  top: 36px;
  left: 8px;
  max-width: min(340px, calc(100% - 16px));
  padding: 10px 12px;
  border: 1px solid var(--c-line);
  border-radius: 12px;
  background: var(--c-panel);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
  font-size: 13px;
  line-height: 1.4;
  pointer-events: auto;
}

/* Opened on the side away from the finger, below the Fit button. */
.bld-card[data-side='right'] {
  top: 64px;
  right: 8px;
  left: auto;
}

.bld-card h3 {
  margin: 0 0 4px;
  font-size: 15px;
}

.bld-card p {
  margin: 0;
  color: var(--c-ink-dim);
}

/* The part under the finger while it is dragged off the hull. */
.bld-drag {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 4;
  opacity: 0.9;
  pointer-events: none;
  filter: drop-shadow(0 4px 10px rgba(0, 0, 0, 0.6));
}

/* Update prompt */
.update-prompt {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  min-height: 48px;
  padding: 0 24px;
  border: 0;
  border-radius: 24px;
  background: var(--c-accent);
  color: var(--c-space);
  font-weight: 700;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
}

/* Rotate prompt: pure CSS, so it works before any script runs. */
#rotate {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 10;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 24px;
  background: var(--c-space);
  color: var(--c-ink);
  text-align: center;
}

#rotate svg {
  width: 96px;
  height: 96px;
  animation: rotate-hint 2.4s ease-in-out infinite;
}

#rotate p {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

#rotate small {
  color: var(--c-ink-dim);
}

@media (orientation: portrait) {
  #rotate {
    display: flex;
  }
}

@keyframes rotate-hint {
  0%,
  25% {
    transform: rotate(0deg);
  }
  55%,
  100% {
    transform: rotate(-90deg);
  }
}

@media (prefers-reduced-motion: reduce) {
  #rotate svg {
    animation: none;
    transform: rotate(-90deg);
  }
}

/* Dev overlay and menu (?dev=1) */
/* Top right, beside DEV: the left side holds the builder's drawer tabs. */
.fps {
  position: absolute;
  top: 4px;
  right: 72px;
  z-index: 2;
  padding: 4px 6px;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.6);
  font: 11px/1.35 ui-monospace, 'Roboto Mono', Menlo, Consolas, monospace;
  white-space: pre;
  pointer-events: none;
}

.dev-toggle {
  position: absolute;
  z-index: 2;
  top: 4px;
  right: 4px;
  font-size: 12px;
  letter-spacing: 0.08em;
  opacity: 0.85;
}

.dev-panel {
  position: absolute;
  z-index: 3;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(440px, 100%);
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 8px 12px 24px;
  border-left: 1px solid var(--c-line);
  background: var(--c-panel);
}

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

.dev-head h2 {
  margin: 0;
  font-size: 18px;
}

.dev-section h3 {
  margin: 16px 0 8px;
  color: var(--c-ink-dim);
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.dev-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 8px 0;
}

.dev-note {
  margin: 4px 0;
  color: var(--c-ink-dim);
  font-size: 13px;
}

.dev-note[data-tone='good'] {
  color: var(--c-good);
}

.dev-note[data-tone='bad'] {
  color: var(--c-bad);
}

.dev-code {
  width: 100%;
  padding: 8px;
  border: 1px solid var(--c-line);
  border-radius: 8px;
  background: var(--c-space);
  font: 12px/1.4 ui-monospace, 'Roboto Mono', Menlo, Consolas, monospace;
  word-break: break-all;
  resize: vertical;
}

.dev-status {
  margin: 0;
  font: 12px/1.5 ui-monospace, 'Roboto Mono', Menlo, Consolas, monospace;
  white-space: pre-wrap;
  word-break: break-word;
  user-select: text;
  -webkit-user-select: text;
}
