/* ────────────────────────────────────────────────────────────
 * css/memory.css — Memory "Mirror" cosmic redesign (DOM rebuild).
 *
 * Mounted as `.app-window.app-memory > .mem-app-frame`. 3D-flip cards
 * use transform-style: preserve-3d. All colors resolve to tokens.
 * ─────────────────────────────────────────────────────────── */

.app-window.app-memory {
  width: 100% !important;
  height: 100% !important;
  min-height: 0 !important;
  padding: 0 !important;
  overflow: hidden;
  outline: none;
}

/* Open Memory at 90%×88% so the 6-col grid + 260px side rail fit
   comfortably on a 900px viewport. WindowChrome edge-handle resize
   still wins via inline styles. */
.window-chrome:has(.app-memory) {
  left: 5%;
  top: 6%;
  width: 90%;
  height: 88%;
}

/* ── Outer frame ─────────────────────────────────────────── */

.mem-app-frame {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  border-radius: 22px;
  background:
    linear-gradient(180deg, rgba(255,255,255,0.10) 0%, rgba(255,255,255,0.02) 22%, rgba(8, 18, 32, 0.6) 60%, rgba(2, 8, 16, 0.78) 100%),
    linear-gradient(160deg, rgba(10, 28, 52, 0.7), rgba(4, 12, 24, 0.9));
  border: 1px solid rgba(255, 255, 255, 0.10);
  box-shadow:
    inset 0 1px 0 rgba(255,255,255,0.18),
    inset 0 -1px 0 rgba(0,0,0,0.5),
    0 18px 60px -10px rgba(155, 123, 255, 0.20),
    0 30px 80px rgba(0, 0, 0, 0.55);
  overflow: hidden;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  color: var(--text-bright);
}
body.theme-light .mem-app-frame {
  background:
    linear-gradient(180deg, rgba(0, 0, 0, 0.04) 0%, rgba(255,255,255,0.02) 22%, rgba(245, 247, 250, 0.92) 60%, rgba(238, 240, 244, 0.95) 100%),
    linear-gradient(160deg, rgba(220, 230, 240, 0.7), rgba(240, 244, 250, 0.9));
}

/* ── Titlebar ────────────────────────────────────────────── */

.mem-titlebar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 18px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.10);
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.04), transparent);
  flex-shrink: 0;
}
.mem-name {
  font-family: 'Playfair Display', 'Amiri', Georgia, serif;
  font-style: italic;
  font-weight: 400;
  font-size: 22px;
  letter-spacing: 0.04em;
  color: var(--text-bright);
  text-shadow: 0 0 16px rgba(155, 123, 255, 0.22);
}

/* ── Stage ───────────────────────────────────────────────── */

.mem-stage {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: 1fr 260px;
  min-height: 0;
}

/* ── Board area (left) ───────────────────────────────────── */

/* CSS grid (not flex) for the board area: HUD = auto, grid-wrap = 1fr,
   bottom-bar = auto. This forces the grid-wrap to claim all remaining
   vertical space deterministically — flex-grow + aspect-ratio interact
   poorly when the content self-collapses to 0. height: 100% required
   for the 1fr row to have a definite size to bind against. */
.mem-board-area {
  position: relative;
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 12px;
  padding: 18px 24px 22px;
  min-height: 0;
  min-width: 0;
  height: 100%;
  background:
    radial-gradient(ellipse 60% 50% at 50% 50%, rgba(155, 123, 255, 0.06), transparent 70%);
}

/* ── HUD bar above the grid (theme + stats) ───────────────── */

.mem-theme-bar {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  align-items: center;
  flex: 0 0 auto;
}
.mem-theme-lp {
  padding: 5px 10px;
  border-radius: 7px;
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-size: 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text);
  border: 1px solid rgba(255, 255, 255, 0.10);
  background: rgba(0, 0, 0, 0.20);
  cursor: pointer;
  transition: color 120ms ease, background 120ms ease, border-color 120ms ease;
  user-select: none;
}
.mem-theme-lp:hover { color: var(--text-bright); }
.mem-theme-lp.is-active {
  color: var(--violet, #9b7bff);
  border-color: rgba(155, 123, 255, 0.40);
  background: rgba(155, 123, 255, 0.08);
}
.mem-stat {
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-size: 11px;
  color: var(--text);
  letter-spacing: 0.06em;
  margin-left: 12px;
}
.mem-stat b { color: var(--accent); margin-left: 2px; }

/* ── Grid + cards ────────────────────────────────────────── */

/* The grid lives inside a flex column (.mem-board-area). To size the
   grid by both width and height — fitting cards' 3:4 aspect-ratio
   while never overflowing — we wrap it in a centering container with
   flex: 1 1 0 + min-height: 0 so it claims remaining space, and
   aspect-ratio (set inline as cols×3 / rows×4) lets the browser pick
   whichever dimension is the binding constraint. */
.mem-grid-wrap {
  /* Lives in board-area's grid-row 2 (1fr). min-height: 0 lets the
     row shrink under content height. */
  min-height: 0;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px 0;
  overflow: hidden;
}
.mem-grid {
  display: grid;
  gap: 14px;
  /* aspect-ratio set inline as (cols×3) / (rows×4). Filling 100% of
     the wrap's height + max-width 100% means: grid claims height,
     derives width from aspect; if the derived width would exceed
     parent width, browser clamps and shrinks height to maintain
     ratio. Either way, no overflow. */
  height: 100%;
  width: auto;
  max-width: 100%;
}

/* The card itself is a 3D-flippable container. Two faces (back +
   face) absolutely positioned with backface-visibility: hidden so
   only one shows at a time. */
.mem-card {
  position: relative;
  width: 100%;
  height: 100%;
  /* aspect-ratio enforced by parent grid cell sizing — see .mem-grid */
  cursor: pointer;
  transform-style: preserve-3d;
  transition: transform 380ms cubic-bezier(0.16, 0.84, 0.32, 1);
  user-select: none;
  border-radius: 12px;
}
.mem-card.is-flipped { transform: rotateY(180deg); }
.mem-card.is-matched { animation: memMatchPulse 380ms cubic-bezier(0.16, 0.84, 0.32, 1); }
@keyframes memMatchPulse {
  50% { transform: rotateY(180deg) scale(1.05); }
}
.mem-card:focus-visible {
  outline: 2px solid var(--violet, #9b7bff);
  outline-offset: 4px;
}

.mem-card-back,
.mem-card-face {
  position: absolute;
  inset: 0;
  border-radius: 12px;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  display: grid;
  place-items: center;
  box-shadow:
    0 6px 20px rgba(0, 0, 0, 0.55),
    inset 0 1px 0 rgba(255, 255, 255, 0.20);
  overflow: hidden;
}

/* Back side — violet glow with a faint diagonal hatch pattern */
.mem-card-back {
  background:
    radial-gradient(ellipse at 50% 30%, rgba(155, 123, 255, 0.50), rgba(40, 20, 90, 0.90) 70%),
    #0e0a24;
  border: 1px solid rgba(155, 123, 255, 0.25);
}
.mem-card-back::after {
  content: '';
  position: absolute;
  inset: 8px;
  border-radius: 9px;
  border: 1px solid rgba(255, 255, 255, 0.06);
  background: repeating-linear-gradient(
    45deg,
    rgba(155, 123, 255, 0.08),
    rgba(155, 123, 255, 0.08) 6px,
    transparent 6px,
    transparent 12px);
  pointer-events: none;
}
.mem-card-glyph {
  position: relative;
  font-size: 22px;
  color: var(--violet, #9b7bff);
  opacity: 0.8;
  font-family: 'Georgia', serif;
}
.mem-card:hover .mem-card-back {
  box-shadow:
    0 6px 20px rgba(0, 0, 0, 0.55),
    inset 0 1px 0 rgba(255, 255, 255, 0.20),
    0 0 0 2px rgba(155, 123, 255, 0.30),
    0 0 18px rgba(155, 123, 255, 0.30);
}

/* Face side — dark glass with the orb centered. Pre-rotated 180° so
   when the parent flips, this face becomes visible. */
.mem-card-face {
  background: linear-gradient(180deg, #1a2436 0%, #0a131f 100%);
  border: 1px solid rgba(var(--accent-rgb), 0.20);
  transform: rotateY(180deg);
}
.mem-card.is-matched .mem-card-face {
  border-color: var(--accent);
  box-shadow:
    0 0 0 2px var(--accent),
    0 0 24px rgba(var(--accent-rgb), 0.50),
    inset 0 1px 0 rgba(255, 255, 255, 0.20);
}

/* ── Orbs (face front + side rail mini-versions) ─────────── */

.mem-orb {
  width: 60%;
  aspect-ratio: 1 / 1;
  max-width: 88px;
  border-radius: 50%;
  box-shadow:
    inset -6px -8px 16px rgba(0, 0, 0, 0.40),
    0 0 24px currentColor;
}
.mem-orb-amber  { background: radial-gradient(circle at 35% 30%, #ffd485, #ff8a3d 60%, #c25a1c); color: rgba(255, 140,  60, 0.50); }
.mem-orb-cyan   { background: radial-gradient(circle at 35% 30%, #b3fff0, #00e5c1 60%, #007a66); color: rgba(  0, 229, 193, 0.50); }
.mem-orb-rose   { background: radial-gradient(circle at 35% 30%, #ffd1dc, #ff6f8b 60%, #c0334d); color: rgba(255, 111, 139, 0.50); }
.mem-orb-violet { background: radial-gradient(circle at 35% 30%, #d4c5ff, #9b7bff 60%, #553a99); color: rgba(155, 123, 255, 0.50); }
.mem-orb-green  { background: radial-gradient(circle at 35% 30%, #b8f5cd, #2dcf6a 60%, #1c7a3e); color: rgba( 45, 207, 106, 0.50); }
.mem-orb-blue   { background: radial-gradient(circle at 35% 30%, #b8dcff, #5aa8ff 60%, #265a99); color: rgba( 90, 168, 255, 0.50); }

/* ── Bottom bar (pulse track + pair counter) ─────────────── */

.mem-bottom-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 16px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.34);
  border: 1px solid rgba(255, 255, 255, 0.08);
}
.mem-bottom-label {
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-size: 10px;
  color: var(--text-dim);
  letter-spacing: 0.10em;
  white-space: nowrap;
}
.mem-bottom-label b { color: var(--accent); margin-right: 2px; }
.mem-pulse-track {
  flex: 1;
  height: 3px;
  background: rgba(255, 255, 255, 0.06);
  border-radius: 999px;
  position: relative;
  overflow: hidden;
}
.mem-pulse-track i {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  background: linear-gradient(90deg, transparent, var(--violet, #9b7bff) 40%, var(--accent));
  border-radius: inherit;
  transition: width 380ms cubic-bezier(0.16, 0.84, 0.32, 1);
}

/* ── Side rail (right col, 260px) ────────────────────────── */

.mem-side {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 18px 16px;
  border-left: 1px solid rgba(255, 255, 255, 0.08);
  overflow-y: auto;
  min-width: 0;
}
body.theme-light .mem-side { border-left-color: rgba(0, 0, 0, 0.08); }

.mem-side-block { display: flex; flex-direction: column; gap: 4px; }
.mem-side-h {
  margin: 0 0 6px;
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-size: 9px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--text-dim);
}
.mem-side-empty {
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-size: 10px;
  color: var(--text-dim);
  letter-spacing: 0.06em;
  padding: 4px 0;
}

/* Streak card (gradient violet) */
.mem-streak-card {
  padding: 14px;
  border-radius: 12px;
  background:
    linear-gradient(160deg, rgba(155, 123, 255, 0.18), rgba(155, 123, 255, 0.04) 50%, rgba(8, 18, 32, 0.6));
  border: 1px solid rgba(155, 123, 255, 0.25);
}
.mem-streak-h {
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-size: 10px;
  color: var(--violet, #9b7bff);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  margin-bottom: 4px;
}
.mem-streak-v {
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-size: 32px;
  font-weight: 300;
  color: var(--text-bright);
  margin-bottom: 4px;
  line-height: 1;
}
.mem-streak-meta {
  font-size: 11px;
  color: var(--text);
}

/* Found-pairs legend */
.mem-legend-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
  border-bottom: 1px dashed rgba(255, 255, 255, 0.04);
}
.mem-legend-row:last-child { border-bottom: 0; }
.mem-legend-orb {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  flex-shrink: 0;
  /* Inherit gradient from .mem-orb-{name} via class composition */
}
.mem-legend-name {
  font-size: 12px;
  color: var(--text-bright);
}
.mem-legend-time {
  margin-left: auto;
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-size: 11px;
  color: var(--text-dim);
}
.mem-legend-row.is-matched .mem-legend-time { color: var(--accent); }

/* Difficulty picker (3 tiles) */
.mem-diff-pick {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 6px;
}
.mem-diff-tile {
  padding: 8px 4px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.10);
  background: rgba(0, 0, 0, 0.20);
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-size: 9px;
  color: var(--text);
  text-align: center;
  cursor: pointer;
  letter-spacing: 0.10em;
  transition: color 120ms ease, background 120ms ease, border-color 120ms ease;
  user-select: none;
}
.mem-diff-tile b {
  display: block;
  font-size: 11px;
  color: var(--text-bright);
  margin-bottom: 2px;
}
.mem-diff-tile.is-active {
  color: var(--violet, #9b7bff);
  border-color: rgba(155, 123, 255, 0.40);
  background: rgba(155, 123, 255, 0.08);
}
.mem-diff-tile.is-active b { color: var(--violet, #9b7bff); }

.mem-mode-note {
  margin-top: auto;
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-size: 10px;
  color: var(--text-dim);
  line-height: 1.6;
}
.mem-mode-note b { color: var(--violet, #9b7bff); }

body.theme-light .mem-diff-tile,
body.theme-light .mem-theme-lp,
body.theme-light .mem-bottom-bar {
  background: rgba(255, 255, 255, 0.55);
  border-color: rgba(0, 0, 0, 0.08);
}

/* ── Win overlay ─────────────────────────────────────────── */

.mem-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 24px;
  background: rgba(2, 8, 16, 0.82);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  z-index: 200;
  text-align: center;
}
.mem-overlay-title {
  font-family: 'Playfair Display', Georgia, serif;
  font-style: italic;
  font-size: 38px;
  color: var(--accent);
  text-shadow: 0 0 24px rgba(var(--accent-rgb), 0.55);
}
.mem-overlay-sub {
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-size: 13px;
  color: var(--text);
  line-height: 1.7;
}
.mem-overlay-best {
  margin-top: 8px;
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-size: 12px;
  letter-spacing: 0.12em;
  color: var(--warm, #ffb84a);
  text-shadow: 0 0 12px rgba(255, 184, 74, 0.45);
}
.mem-overlay-btn {
  padding: 10px 22px;
  border-radius: 10px;
  background: rgba(var(--accent-rgb), 0.15);
  border: 1px solid rgba(var(--accent-rgb), 0.40);
  color: var(--accent);
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-size: 11px;
  letter-spacing: 0.10em;
  text-transform: uppercase;
  cursor: pointer;
  transition: background 120ms ease;
}
.mem-overlay-btn:hover { background: rgba(var(--accent-rgb), 0.25); }

/* ── Mobile / narrow (≤ 720px) ───────────────────────────── */

@media (max-width: 720px) {
  .mem-stage {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr auto;
  }
  .mem-side {
    border-left: 0;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 10px 12px;
    gap: 12px;
    max-height: 160px;
  }
  .mem-side-block, .mem-streak-card { flex: 0 0 auto; min-width: 180px; }
  .mem-mode-note { display: none; }
  .mem-name { font-size: 18px; }
}

/* ── Reduced motion ──────────────────────────────────────── */

@media (prefers-reduced-motion: reduce) {
  .mem-card { transition: none; }
  .mem-card.is-matched { animation: none; }
  .mem-pulse-track i { transition: none; }
}
