/* Bit — the pixel guardian pet (vitals-pet.js).
   Deliberately pixel-styled in EVERY theme (Press Start 2P / VT323 are already
   loaded for the retro skin): Bit is an 8-bit creature, so his bubble, menu and
   GAME OVER card stay chunky and crisp even on the liquid-glass dashboard.
   Layering: pet 50 / glitch 51 / game-over 52 — all below the layout FAB (55)
   and every full-screen panel; anything panel-related also hides via
   body[data-panel] so Bit never sits on top of Settings or the app switcher. */

.vpet {
  position: fixed;
  left: 14px;
  bottom: 14px;
  z-index: 50;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  --vpet-rgb: 255, 215, 94;
  --vpet-body: rgb(var(--vpet-rgb));
  --vpet-surface: var(--surface);
  --vpet-control: var(--control-bg);
  --vpet-text: var(--text);
  --vpet-muted: var(--muted-text);
  --vpet-line: var(--line);
  --vpet-scrim: color-mix(in srgb, var(--bg) 80%, transparent);
  --vpet-shadow: color-mix(in srgb, var(--bg) 58%, transparent);
}
.vpet-menu-anchored,
.vpet-gameover,
.vpet-chip-bubble {
  --vpet-surface: var(--surface);
  --vpet-control: var(--control-bg);
  --vpet-text: var(--text);
  --vpet-muted: var(--muted-text);
  --vpet-line: var(--line);
  --vpet-scrim: color-mix(in srgb, var(--bg) 80%, transparent);
  --vpet-shadow: color-mix(in srgb, var(--bg) 58%, transparent);
}
body[data-panel] .vpet,
body.layout-editing .vpet { display: none; }
/* The GAME OVER card and the chip bubble are body children OUTSIDE .vpet — an
   already-open one must hide too while Settings / the app switcher is up. */
body[data-panel] .vpet-gameover,
body.layout-editing .vpet-gameover,
body[data-panel] .vpet-chip-bubble,
body.layout-editing .vpet-chip-bubble,
body[data-panel] .vpet-menu-anchored,
body.layout-editing .vpet-menu-anchored { display: none; }
/* The news/stocks ticker bar (34px, z60) docks to the same bottom edge — sit above it. */
body.xe-has-ticker.xe-ticker-bottom .vpet { bottom: 52px; }

.vpet-sprite {
  width: 52px;
  height: 52px;
  padding: 4px;
  border: 0;
  background: transparent;
  cursor: pointer;
  filter: drop-shadow(0 3px 8px rgba(0, 0, 0, 0.45)) drop-shadow(0 0 10px rgba(var(--vpet-rgb), 0.35));
  animation: vpet-bob 2.6s ease-in-out infinite;
  will-change: transform;
  transition: filter 0.4s ease;
}
.vpet-sprite svg { width: 100%; height: 100%; display: block; }
@keyframes vpet-bob {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-3px); }
}
.vpet[data-mood="angry"] .vpet-sprite { animation: vpet-shake 0.55s steps(2) infinite; }
@keyframes vpet-shake {
  0%, 100% { transform: translateX(0); }
  25%      { transform: translateX(-2px) rotate(-2deg); }
  75%      { transform: translateX(2px) rotate(2deg); }
}
.vpet[data-mood="ghost"] .vpet-sprite { animation: vpet-float 3.4s ease-in-out infinite; opacity: 0.92; }
@keyframes vpet-float {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-7px); }
}
/* Asleep (away / night quiet): slow, deep breathing bob + a floating "zzz".
   The zzz rides ::after so the sprite itself stays the 1:1 brand shape. */
.vpet[data-mood="sleep"] .vpet-sprite { animation: vpet-sleep 4.6s ease-in-out infinite; }
@keyframes vpet-sleep {
  0%, 100% { transform: translateY(0) scale(1); }
  50%      { transform: translateY(-1px) scale(1.03); }
}
.vpet[data-mood="sleep"]::after,
.clock-bit[data-mood="sleep"]::after {
  content: 'zzz';
  position: absolute;
  font-family: 'VT323', 'Courier New', monospace;
  color: rgba(var(--vpet-rgb), 0.9);
  pointer-events: none;
  animation: vpet-zzz 2.8s ease-in-out infinite;
  will-change: transform, opacity;
}
.vpet[data-mood="sleep"]::after { left: 46px; bottom: 40px; font-size: 16px; }
.clock-bit[data-mood="sleep"]::after { right: -4px; top: -6px; font-size: 11px; }
@keyframes vpet-zzz {
  0%   { opacity: 0; transform: translateY(3px); }
  30%  { opacity: 1; }
  100% { opacity: 0; transform: translateY(-6px); }
}

/* Speech bubble — pixel frame, VT323 body. Anchored above the sprite. */
.vpet-bubble {
  max-width: min(300px, 58vw);
  padding: 8px 10px;
  background: var(--vpet-surface);
  color: var(--vpet-text);
  border: 2px solid rgb(var(--vpet-rgb));
  box-shadow: 4px 4px 0 var(--vpet-shadow);
  font-family: 'VT323', 'Courier New', monospace;
  font-size: 17px;
  line-height: 1.15;
  letter-spacing: 0.2px;
  image-rendering: pixelated;
}
.vpet-bubble::after {
  content: '';
  display: block;
  position: absolute;
  left: 22px;
  bottom: -8px;
  width: 8px;
  height: 8px;
  background: var(--vpet-surface);
  border-right: 2px solid rgb(var(--vpet-rgb));
  border-bottom: 2px solid rgb(var(--vpet-rgb));
  transform: rotate(45deg);
}
.vpet-bubble { position: relative; }

/* Quick menu — worst meters + truce buttons. */
.vpet-menu {
  position: absolute;
  left: 0;
  bottom: calc(100% + 8px);
  min-width: 190px;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 5px;
  background: var(--vpet-surface);
  border: 2px solid rgb(var(--vpet-rgb));
  box-shadow: 4px 4px 0 var(--vpet-shadow);
  font-family: 'VT323', 'Courier New', monospace;
}
.vpet-menu-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  color: var(--vpet-text);
  font-family: 'Press Start 2P', 'VT323', monospace;
  font-size: 10px;
  padding-bottom: 4px;
  border-bottom: 1px dashed var(--vpet-line);
}
.vpet-menu-lv { color: var(--vpet-text); font-family: 'VT323', monospace; font-size: 15px; }
.vpet-menu-streak { color: var(--color-warn); font-family: 'VT323', monospace; font-size: 15px; }

/* Menu opened from the topbar chip: no .vpet host to nest in, so the engine
   appends it to <body>, positions it under the chip (fixed, viewport-clamped)
   and restates the mood palette on the menu itself. */
.vpet-menu-anchored {
  position: fixed;
  left: auto;
  bottom: auto;
  z-index: 300; /* above the topbar (island) but below modals/toasts */
  --vpet-rgb: 255, 215, 94;
}
.vpet-menu-row,
.vpet-menu-act {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 5px 7px;
  border: 1px solid var(--vpet-line);
  background: var(--vpet-control);
  color: var(--vpet-text);
  font-family: inherit;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  text-align: left;
}
.vpet-menu-row:hover, .vpet-menu-act:hover { background: color-mix(in srgb, rgb(var(--vpet-rgb)) 18%, var(--vpet-control)); }
.vpet-menu-row.is-dead { border-color: var(--color-danger); color: var(--color-danger); }
.vpet-menu-pct { font-weight: 700; }
.vpet-menu-act { justify-content: center; color: var(--vpet-text); }

/* Stage 2 — dashboard decay: the pager progressively drains of colour while a
   vital rots at zero. `--vpet-decay` (0..1, set by the engine each tick) ramps
   from a faint tint at onset to FULL black & white the longer it's ignored; a
   refill removes the class and the filter eases back to normal. The filter goes
   on #dashboard-pager (NEVER on body: a filtered ancestor becomes the containing
   block for position:fixed children and would tear the floating clock island /
   toasts out of place). */
.pager.vpet-decay {
  --vpet-decay: 0.15;
  filter:
    grayscale(var(--vpet-decay))
    saturate(calc(1 - 0.5 * var(--vpet-decay)))
    brightness(calc(1 - 0.12 * var(--vpet-decay)))
    contrast(calc(1 - 0.08 * var(--vpet-decay)));
  transition: filter 1.4s ease;
}

/* Glitch pulse — a fleeting full-screen scanline + slice flicker. Transform/
   opacity only, self-removed by the engine after ~900ms. */
.vpet-glitch {
  position: fixed;
  inset: 0;
  z-index: 51;
  pointer-events: none;
  background:
    repeating-linear-gradient(0deg, rgba(255, 255, 255, 0.035) 0 1px, transparent 1px 3px);
  mix-blend-mode: overlay;
  animation: vpet-glitch-flicker 0.9s steps(6) forwards;
  will-change: opacity, transform;
}
@keyframes vpet-glitch-flicker {
  0%   { opacity: 0;   transform: translateX(0); }
  15%  { opacity: 1;   transform: translateX(-6px) skewX(-2deg); }
  30%  { opacity: 0.4; transform: translateX(4px); }
  45%  { opacity: 1;   transform: translateX(-3px) skewX(1.5deg); }
  60%  { opacity: 0.3; transform: translateX(6px); }
  80%  { opacity: 0.8; transform: translateX(-2px); }
  100% { opacity: 0;   transform: translateX(0); }
}

/* Stage 2b — GAME OVER: full-dashboard CRT card. */
.vpet-gameover {
  /* The sprite in the card lives outside .vpet — restate its palette (angry red)
     or the body rects would fall back to black. */
  --vpet-rgb: 255, 90, 95;
  --vpet-body: rgb(var(--vpet-rgb));
  position: fixed;
  inset: 0;
  z-index: 52;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--vpet-scrim);
  backdrop-filter: blur(3px);
  animation: vpet-go-in 0.25s steps(4);
}
@keyframes vpet-go-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}
.vpet-go-card {
  position: relative;
  max-width: min(430px, 86vw);
  padding: 26px 28px 20px;
  text-align: center;
  background: var(--vpet-surface);
  border: 3px solid var(--color-danger);
  box-shadow: 6px 6px 0 var(--vpet-shadow),
    0 0 34px color-mix(in srgb, var(--color-danger) 35%, transparent);
  overflow: hidden;
}
/* CRT scanlines across the card. */
.vpet-go-card::after {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: repeating-linear-gradient(0deg, rgba(255, 255, 255, 0.045) 0 1px, transparent 1px 3px);
}
.vpet-go-sprite { width: 56px; height: 56px; margin: 0 auto 10px; }
.vpet-go-sprite svg { width: 100%; height: 100%; }
.vpet-go-title {
  font-family: 'Press Start 2P', 'VT323', monospace;
  font-size: 26px;
  color: var(--color-danger);
  text-shadow: 3px 3px 0 var(--vpet-shadow);
  margin-bottom: 12px;
  animation: vpet-go-blink 1.2s steps(2) infinite;
}
@keyframes vpet-go-blink { 50% { opacity: 0.55; } }
.vpet-go-sub {
  font-family: 'VT323', 'Courier New', monospace;
  font-size: 19px;
  line-height: 1.2;
  color: var(--vpet-text);
  margin-bottom: 18px;
}
.vpet-go-actions { display: flex; gap: 10px; justify-content: center; margin-bottom: 14px; }
.vpet-go-btn {
  font-family: 'Press Start 2P', 'VT323', monospace;
  font-size: 10px;
  padding: 10px 14px;
  background: transparent;
  color: var(--vpet-text);
  border: 2px solid var(--vpet-line);
  cursor: pointer;
}
.vpet-go-btn.is-fix { color: var(--color-success); border-color: var(--color-success); box-shadow: 3px 3px 0 var(--vpet-shadow); }
.vpet-go-btn.is-fix:hover { background: color-mix(in srgb, var(--color-success) 14%, var(--vpet-control)); }
.vpet-go-btn:hover { background: var(--hover-bg); }
.vpet-go-coin {
  font-family: 'VT323', 'Courier New', monospace;
  font-size: 15px;
  color: var(--vpet-muted);
  letter-spacing: 1px;
  animation: vpet-go-blink 1.6s steps(2) infinite;
}

/* ── Topbar mini-Bit (#clock-bit, pet.position topbar/both) ──────────────────
   First child of #clock-vitals so it rides the minimal-island reparenting with
   the clock. Same 12×12 sprite at chip scale, mood glow via --vpet-rgb; the
   touch target stays comfortable (≥32px) even though the sprite is tiny. */
.clock-bit {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
  --vpet-rgb: 255, 215, 94;
  --vpet-body: rgb(var(--vpet-rgb));
}
.clock-bit .cb-sprite {
  width: 20px;
  height: 20px;
  display: block;
  filter: drop-shadow(0 0 6px rgba(var(--vpet-rgb), 0.45));
  animation: vpet-bob 2.6s ease-in-out infinite;
  will-change: transform;
}
.clock-bit .cb-sprite svg { width: 100%; height: 100%; display: block; }
.clock-bit[data-mood="angry"] .cb-sprite { animation: vpet-shake 0.55s steps(2) infinite; }
.clock-bit[data-mood="ghost"] .cb-sprite { animation: vpet-float 3.4s ease-in-out infinite; opacity: 0.92; }
.clock-bit[data-mood="sleep"] .cb-sprite { animation: vpet-sleep 4.6s ease-in-out infinite; }
/* Frosted overlay open: pause the pet's INFINITE loops only — one-shot moves
   (glitch, go-in, chip pops) must finish or they'd freeze mid-frame. The 700ms
   JS sprite flipper is gated on the same class in vitals-pet.js.
   (See the overlay-frozen block in styles/backgroundfx.css.) */
body.overlay-frozen .vpet-sprite,
body.overlay-frozen .vpet[data-mood="sleep"]::after,
body.overlay-frozen .clock-bit[data-mood="sleep"]::after,
body.overlay-frozen .vpet-go-title,
body.overlay-frozen .vpet-go-coin,
body.overlay-frozen .cb-sprite { animation-play-state: paused; }

/* Compact bubble under the chip — used when the floating pet is off. */
.vpet-chip-bubble {
  position: fixed;
  z-index: 300;
  padding: 7px 9px;
  background: var(--vpet-surface);
  color: var(--vpet-text);
  border: 2px solid rgb(var(--vpet-rgb, 255, 215, 94));
  box-shadow: 4px 4px 0 var(--vpet-shadow);
  font-family: 'VT323', 'Courier New', monospace;
  font-size: 16px;
  line-height: 1.15;
  letter-spacing: 0.2px;
}

/* Settings: small visual break before the Bit sub-section inside the Vitals card. */
.vpet-settings-head { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }

@media (prefers-reduced-motion: reduce) {
  .vpet-sprite, .vpet[data-mood] .vpet-sprite, .vpet-go-title, .vpet-go-coin,
  .clock-bit .cb-sprite, .clock-bit[data-mood] .cb-sprite,
  .vpet[data-mood="sleep"]::after, .clock-bit[data-mood="sleep"]::after { animation: none; }
  .vpet-glitch { display: none; }
}
