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

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #060610;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  color: #fff;
}

/* Atmospheric ambient glow — evokes the original's chromatic haze */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  background: radial-gradient(
    ellipse 80% 65% at 50% 48%,
    rgba(62, 220, 155, 0.045) 0%,
    rgba(80, 200, 230, 0.03)  35%,
    transparent               70%
  );
  pointer-events: none;
  z-index: 0;
}

#container {
  position: fixed;
  inset: 0;
}

/* ── Bottom bar: legend left, controls right ── */
#bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 18px 28px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  z-index: 10;
  pointer-events: none;
}

#legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  pointer-events: auto;
}

.cat-box {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 7px 13px;
  border-radius: 4px;
  border: 1px solid;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  user-select: none;
}

.cat-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
}

#controls {
  display: flex;
  align-items: center;
  gap: 8px;
  pointer-events: auto;
}

.btn-sep {
  width: 1px;
  align-self: stretch;
  margin: 2px 4px;
  background: rgba(255, 255, 255, 0.13);
}

.btn {
  background: rgba(255, 255, 255, 0.04);
  color: rgba(255, 255, 255, 0.45);
  border: 1px solid rgba(255, 255, 255, 0.1);
  padding: 8px 20px;
  border-radius: 3px;
  font-size: 11px;
  font-family: inherit;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  cursor: pointer;
  transition: background 0.18s, border-color 0.18s, color 0.18s;
  outline: none;
  -webkit-tap-highlight-color: transparent;
}

.btn:hover {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.22);
  color: rgba(255, 255, 255, 0.8);
}

.btn.active {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.32);
  color: rgba(255, 255, 255, 0.9);
}

/* ── Card ── */
/* background-color is set inline by JS as an OPAQUE dark-tint of the category
   color (computed mix of dark page color + category color).  Opaque so cards
   that overlap in 3D space cleanly block each other.
   No `transition: transform` here — CSS3DRenderer overwrites the inline
   `transform: matrix3d(...)` every frame, and a CSS transition would start a
   new 150ms animation on each of those updates.  The result was a faint
   oscillation around the target near the end of the InOut ease — exactly the
   flip-flap the user saw when cards landed in the grid.
   Width/height transitions are fine — they don't conflict with matrix3d. */
.card {
  width: 315px;
  height: 210px;
  border: 1px solid;
  border-radius: 8px;
  overflow: hidden;
  display: flex;
  cursor: pointer;
  user-select: none;
  background: rgb(6, 6, 18);
  will-change: transform;
  position: relative; /* so .card-actions (absolute) sit at the card's bottom */
  transition: width 0.4s ease, height 0.4s ease;
}

/* ── Sci-fi HUD corner brackets ──
   Four small L-shapes in each card corner, drawn in the card's category color
   via the --accent CSS variable (set inline by JS in createCard).  Softly
   pulsing on a staggered cycle for an alive-but-not-frantic feel.  pointer-
   events:none so they never intercept clicks meant for the card or its
   buttons.  z-index keeps them above the card-stripe and content. */
.card-corner {
  position: absolute;
  width: 12px;
  height: 12px;
  border: 0 solid var(--accent, rgba(255, 255, 255, 0.6));
  filter: drop-shadow(0 0 4px var(--accent-glow, rgba(255, 255, 255, 0.3)));
  animation: hud-pulse 2.6s ease-in-out infinite;
  pointer-events: none;
  z-index: 2;
  /* Force a deterministic compositing layer.  Without this, iOS Safari (and
     sometimes desktop Safari) composes `.card-corner` on a layer that doesn't
     fully inherit the parent's `opacity:0` — which let the corner brackets
     render even when the card body was hidden mid-tween, producing the
     "corner-only ghost cards" the user reported after a Cistercian → Return
     → Cistercian sequence. */
  transform: translateZ(0);
}

.card-corner-tl {
  top: 7px;
  left: 7px;
  border-top-width: 1.5px;
  border-left-width: 1.5px;
}

.card-corner-tr {
  top: 7px;
  right: 7px;
  border-top-width: 1.5px;
  border-right-width: 1.5px;
  animation-delay: 0.4s;
}

.card-corner-bl {
  bottom: 7px;
  left: 7px;
  border-bottom-width: 1.5px;
  border-left-width: 1.5px;
  animation-delay: 1.3s;
}

.card-corner-br {
  bottom: 7px;
  right: 7px;
  border-bottom-width: 1.5px;
  border-right-width: 1.5px;
  animation-delay: 0.9s;
}

@keyframes hud-pulse {
  0%, 100% { opacity: 0.65; }
  50%      { opacity: 1.0; }
}

.card-inner {
  flex: 1;
  padding: 24px 22px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.card-title {
  font-size: 26px;
  font-weight: 600;
  color: #fff;
  line-height: 1.3;
  letter-spacing: 0.01em;
  transition: font-size 0.4s ease;
}

/* Hidden until the card enters detail mode.  The typewriter effect in cards.js
   appends characters one at a time to this element. */
.card-description {
  display: none;
  margin-top: 10px;
  font-size: 12px;
  line-height: 1.55;
  color: rgba(255, 255, 255, 0.78);
  font-weight: 400;
  letter-spacing: 0.01em;
  white-space: pre-wrap;
}

.card.expanded {
  width: 380px;
  height: 280px;
  cursor: default;
}

.card.expanded .card-inner {
  justify-content: flex-start;
  padding: 22px 24px 48px; /* reserve space at bottom for .card-actions */
}

.card.expanded .card-title {
  font-size: 22px;
}

.card.expanded .card-description {
  display: block;
}

/* ── In-card detail-mode action buttons ── */
.card-actions {
  position: absolute;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
  z-index: 5;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.3s ease 0.2s, visibility 0.3s ease 0.2s;
}

.card.expanded .card-actions {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.card-action-btn {
  background: rgba(255, 255, 255, 0.06);
  color: rgba(255, 255, 255, 0.85);
  border: 1px solid rgba(255, 255, 255, 0.22);
  padding: 5px 14px;
  border-radius: 3px;
  font-size: 9px;
  font-family: inherit;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  cursor: pointer;
  outline: none;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.18s, border-color 0.18s, color 0.18s;
}

.card-action-btn:hover {
  background: rgba(255, 255, 255, 0.16);
  border-color: rgba(255, 255, 255, 0.45);
  color: rgba(255, 255, 255, 1);
}

/* Hide the bottom view/control bar while a card is expanded — keeps the
   user's attention on the card and prevents the on-card action buttons from
   visually competing with the bottom controls. */
#bottom-bar {
  transition: opacity 0.3s ease, visibility 0.3s ease;
}

body.detail-mode #bottom-bar {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

/* ── Mobile / narrow viewport ──
   Stack legend on top of controls so neither row overflows.  Both rows wrap.
   Smaller padding/font.  Hide the vertical separator (gap alone reads as the
   group boundary at this scale).  Buttons sit at the bottom and intentionally
   overlay the cards — the bar lets pointer events fall through to the canvas
   except on the buttons themselves. */
@media (max-width: 720px) {
  #bottom-bar {
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
    padding: 10px 10px 14px;
  }

  #legend,
  #controls {
    width: 100%;
    justify-content: center;
    flex-wrap: wrap;
    gap: 6px;
  }

  .cat-box {
    padding: 5px 10px;
    font-size: 9px;
    letter-spacing: 0.08em;
  }

  .btn {
    padding: 6px 14px;
    font-size: 10px;
    letter-spacing: 0.1em;
  }

  .btn-sep {
    display: none;
  }

  /* Expanded card is sized in CSS pixels but CSS3D scales it up by
     (viewport_height / visible_world_height_at_camera_distance).  On mobile
     portrait that ratio is ~1.3, so the default 380×280 visibly overflows.
     A 260×220 CSS card renders at ~340×288 visible on a 375×667 viewport. */
  .card.expanded {
    width: 260px;
    height: 220px;
  }

  .card.expanded .card-inner {
    padding: 14px 16px 38px;
  }

  .card.expanded .card-title {
    font-size: 18px;
  }

  .card-description {
    margin-top: 6px;
    font-size: 11px;
    line-height: 1.5;
  }

  .card-actions {
    bottom: 10px;
    gap: 6px;
  }

  .card-action-btn {
    padding: 4px 10px;
    font-size: 8.5px;
    letter-spacing: 0.12em;
  }
}

/* Loading state */
#loading {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  letter-spacing: 0.3em;
  text-transform: uppercase;
  color: rgba(255, 255, 255, 0.25);
  z-index: 20;
  pointer-events: none;
  transition: opacity 0.6s;
}

#loading.hidden {
  opacity: 0;
}
