/* ============================================================
   App shell: HUD / sub-HUD / stage / controls
   ============================================================ */
#app-shell {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  grid-template-areas: "hud" "sub" "stage" "controls";
  padding: max(6px, var(--safe-top)) max(8px, var(--safe-right)) max(6px, var(--safe-bottom)) max(8px, var(--safe-left));
  gap: 6px;
  overflow: hidden;
}

#scene-bg {
  position: absolute;
  inset: 0;
  z-index: 0;
  transition: filter .6s ease;
}
#scene-bg::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, rgba(30, 27, 75, .55), rgba(49, 46, 129, .35));
  opacity: 0;
  transition: opacity 1.2s ease;
  pointer-events: none;
}
.is-night #scene-bg::after { opacity: .55; }
.is-sleeping #scene-bg::after { opacity: .8; }

.hud, .subhud, .stage, .controls { position: relative; z-index: 1; }

/* ---------------- HUD row 1 ---------------- */
.hud {
  grid-area: hud;
  display: flex;
  align-items: center;
  gap: 6px;
  height: 46px;
  z-index: 5;
}

.hud-cat {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 46px;
  padding: 3px 10px 3px 3px;
  border-radius: var(--r-pill);
  background: rgba(255, 255, 255, .92);
  box-shadow: var(--sh-1);
  flex-shrink: 0;
  max-width: 34vw;
}
.xp-ring {
  --p: 0;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  background: conic-gradient(var(--amber) calc(var(--p) * 1%), var(--amber-soft) 0);
}
.xp-ring-inner {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #fff;
  display: grid;
  place-items: center;
  font-size: 18px;
}
.hud-cat-text { display: flex; flex-direction: column; line-height: 1.1; min-width: 0; text-align: left; }
.hud-cat-name { font-size: var(--fs-sm); font-weight: 800; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hud-level { font-size: var(--fs-xs); font-weight: 800; color: var(--amber); }

.hud-meters {
  flex: 1;
  min-width: 0;
  height: 46px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  align-content: center;
  gap: 3px 10px;
  padding: 3px 10px;
  border-radius: var(--r-lg);
  background: rgba(255, 255, 255, .92);
  box-shadow: var(--sh-1);
}
.meter { display: flex; align-items: center; gap: 4px; height: 11px; min-width: 0; }
.meter-icon { font-size: 10px; width: 14px; text-align: center; }
.meter-track { flex: 1; height: 6px; border-radius: 4px; background: #f3ebe4; overflow: hidden; }
.meter-fill { display: block; height: 100%; border-radius: 4px; transition: width .5s var(--ease-out); }
.meter-num { font-size: 10px; font-weight: 800; color: var(--ink-2); width: 18px; text-align: right; font-variant-numeric: tabular-nums; }
.meter[data-kind="happy"] .meter-fill { background: var(--rose-2); }
.meter[data-kind="full"] .meter-fill { background: var(--amber); }
.meter[data-kind="clean"] .meter-fill { background: var(--sky); }
.meter[data-kind="energy"] .meter-fill { background: #a78bfa; }
.meter[data-kind="health"] .meter-fill { background: #34d399; }
.meter-wide { grid-column: 1 / -1; }
.meter-trend { font-size: 10px; font-weight: 900; width: 10px; text-align: center; }
.meter-trend[data-dir="up"] { color: #10b981; }
.meter-trend[data-dir="down"] { color: var(--rose); animation: meterBlink 1.2s ease-in-out infinite; }
/* zones come from R.NEED_ZONES: yellow = 该照顾了, red = 正在伤害健康 */
.meter[data-zone="warn"] .meter-num { color: #d97706; }
.meter[data-zone="red"] .meter-fill { animation: meterBlink 1s ease-in-out infinite; }
.meter[data-zone="red"] .meter-num { color: var(--rose); }
.meter[data-kind="health"][data-zone="warn"] .meter-fill { background: #fbbf24; }
.meter[data-kind="health"][data-zone="red"] .meter-fill { background: var(--rose); }
/* at 0 drop the fill outright, so a blinking (composited) bar can't be left painted behind */
.meter.is-empty .meter-fill, .profile-meter.is-empty .meter-fill { display: none; }
@keyframes meterBlink { 50% { opacity: .35; } }

.hud-right { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.hud-partner, .hud-menu {
  position: relative;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  box-shadow: var(--sh-1);
}
.hud-partner {
  background: linear-gradient(135deg, #fbcfe8, #fecdd3);
  font-weight: 900;
  color: #9f1239;
  font-size: var(--fs-md);
}
.presence-dot {
  position: absolute;
  right: 1px;
  bottom: 1px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #cbd5e1;
  border: 2px solid #fff;
}
.partner-wx {
  position: absolute;
  left: -4px;
  top: -4px;
  font-size: 15px;
  line-height: 1;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, .25));
  pointer-events: none;
}
.hud-partner.is-online .presence-dot { background: #22c55e; box-shadow: 0 0 0 0 rgba(34, 197, 94, .6); animation: onlinePulse 2s infinite; }
@keyframes onlinePulse { 70% { box-shadow: 0 0 0 7px rgba(34, 197, 94, 0); } 100% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0); } }

.hud-menu { background: var(--primary-grad); font-size: 20px; color: #fff; }
.hud-badge {
  position: absolute;
  top: -3px;
  right: -3px;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  border-radius: 9px;
  background: #fde047;
  color: #713f12;
  font-size: 11px;
  font-weight: 900;
  display: grid;
  place-items: center;
  border: 2px solid #fff;
}
.sync-dot {
  position: absolute;
  left: 1px;
  bottom: 1px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 2px solid #fff;
  background: #94a3b8;
}
.sync-dot[data-state="online"] { background: #22c55e; }
.sync-dot[data-state="offline"] { background: #f97316; animation: meterBlink 1s infinite; }
.sync-dot[data-state="local"] { background: #eab308; }

/* ---------------- HUD row 2 ---------------- */
.subhud {
  grid-area: sub;
  display: flex;
  align-items: center;
  gap: 6px;
  height: 32px;
  z-index: 4;
}
.room-pill {
  display: flex;
  align-items: center;
  gap: 5px;
  height: 32px;
  padding: 0 10px;
  border-radius: var(--r-pill);
  background: rgba(255, 255, 255, .85);
  box-shadow: var(--sh-1);
  font-size: var(--fs-sm);
  font-weight: 800;
  flex-shrink: 0;
  white-space: nowrap;
}
.room-pill-name { color: var(--accent); }
.room-pill-sep { width: 1px; height: 14px; background: var(--line-2); }
.room-pill-stat { color: #b45309; font-variant-numeric: tabular-nums; }
.room-pill-stat.is-love { color: var(--rose); }
.room-pill-days { font-size: var(--fs-xs); background: var(--rose-soft); color: var(--rose); padding: 1px 6px; border-radius: 6px; }

.ticker {
  flex: 1;
  min-width: 0;
  height: 32px;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 4px 0 10px;
  border-radius: var(--r-pill);
  background: linear-gradient(90deg, rgba(254, 243, 199, .92), rgba(255, 228, 230, .92));
  box-shadow: var(--sh-1);
  font-size: var(--fs-sm);
  font-weight: 700;
  color: #9f1239;
}
.ticker-icon { flex-shrink: 0; }
.ticker-track { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; text-align: left; }
.ticker-badge {
  flex-shrink: 0;
  height: 24px;
  padding: 0 8px;
  border-radius: var(--r-pill);
  background: var(--rose);
  color: #fff;
  font-size: var(--fs-xs);
  font-weight: 800;
  display: grid;
  place-items: center;
}
.ticker.has-unread .ticker-badge { background: #f97316; animation: badgeWiggle 1.6s ease-in-out infinite; }
@keyframes badgeWiggle { 0%, 80%, 100% { transform: rotate(0); } 85% { transform: rotate(-8deg); } 90% { transform: rotate(8deg); } 95% { transform: rotate(-4deg); } }

/* ---------------- stage ---------------- */
.stage {
  grid-area: stage;
  position: relative;
  min-height: 0;
  touch-action: none;
  container-type: size;
}
.scene-layer {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  /* the cat's size in the room, and its usual spot: its feet on the floor at depth 0.7 (room/stage.js HOME_Y = 83.9%
     of the stage; the wrapper's box reaches 10.9% of its width below the feet) */
  /* the room is drawn in a frame of one shape on every screen: as wide as the stage on a phone, and on a
     tablet or a desktop window as wide as the stage's height allows, in the middle (the wall and floor run on) */
  --room-w: min(100cqw, 66cqh);
  --cat-w: calc(var(--room-w) * .54);
  padding-bottom: calc(16.1cqh - var(--cat-w) * .109);
  will-change: transform;
  /* the camera (room/camera.js) scales this layer and the backdrop from their top-left corners */
  transform-origin: 0 0;
}
#scene-bg { transform-origin: 0 0; }
/* the scene may spill over the top of the stage when the camera moves in: the bar above stays on top */
.subhud { z-index: 4; }

/* ---------------- controls ---------------- */
.controls {
  grid-area: controls;
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
  z-index: 4;
}

.toolbar-wrap {
  position: relative;
  width: 100%;
  max-width: 600px;
  display: flex;
  align-items: center;
  transition: margin .25s var(--ease-out);
}
/* grabber straddling the toolbar's top edge: tap or drag down to tuck the toolbar away */
.toolbar-handle {
  position: absolute;
  left: 50%;
  top: -12px;
  z-index: 2;
  width: 72px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  transform: translateX(-50%);
  touch-action: none;
  transition: top .25s var(--ease-out);
}
.toolbar-handle::before {
  content: "";
  width: 34px;
  height: 5px;
  border-radius: 3px;
  background: rgba(20, 20, 25, .22);
  box-shadow: 0 0 0 2px rgba(255, 255, 255, .7);
  transition: width .2s, background .2s;
}
.toolbar-handle.is-pressed::before { width: 44px; background: rgba(20, 20, 25, .4); }
.toolbar-wrap.is-collapsed .toolbar-handle {
  top: -22px;
  border-radius: 11px 11px 0 0;
  background: rgba(255, 255, 255, .92);
  box-shadow: 0 -2px 6px rgba(0, 0, 0, .08);
}
.toolbar-scroll-wrap {
  position: relative;
  flex: 1;
  min-width: 0;
  height: 48px;
  border-radius: var(--r-md);
  background: rgba(255, 255, 255, .92);
  box-shadow: var(--sh-1);
  overflow: hidden;
  transition: height .25s var(--ease-out), opacity .2s;
}
.toolbar-wrap.is-collapsed .toolbar-scroll-wrap { height: 0; opacity: 0; }
.toolbar-wrap.is-collapsed { margin-bottom: -6px; }
.toolbar-scroll-wrap::before, .toolbar-scroll-wrap::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  width: 22px;
  pointer-events: none;
  opacity: 0;
  transition: opacity .2s;
  z-index: 1;
}
.toolbar-scroll-wrap::before { left: 0; background: linear-gradient(90deg, #fff, rgba(255, 255, 255, 0)); }
.toolbar-scroll-wrap::after { right: 0; background: linear-gradient(-90deg, #fff 40%, rgba(255, 255, 255, 0)); }
/* more tools to the right: a fade and a small arrow (the classes sit on the scroll wrap itself) */
.toolbar-scroll-wrap::after {
  content: "›"; width: 34px; display: flex; align-items: center; justify-content: flex-end; padding-right: 7px;
  font-size: 24px; font-weight: 900; line-height: 1; color: var(--accent, #e11d48);
}
.toolbar-scroll-wrap.fade-left::before,
.toolbar-scroll-wrap.fade-right::after { opacity: 1; }
.toolbar-scroll-wrap.fade-right::after { animation: moreTools 1.6s ease-in-out 2; }
@keyframes moreTools { 50% { transform: translateX(-3px); } }

.toolbar {
  height: 100%;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 6px;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x proximity;
  scrollbar-width: none;
  touch-action: pan-x;
  overscroll-behavior-x: contain;
}
.toolbar::-webkit-scrollbar { display: none; }
/* opened up: every pill wrapped onto up to three rows; the stage gives up the space, nothing covers the cat */
.toolbar-wrap.is-open .toolbar-scroll-wrap { height: auto; max-height: 142px; }
.toolbar-wrap.is-open .toolbar {
  height: auto;
  max-height: 142px;
  flex-wrap: wrap;
  align-content: flex-start;
  padding: 6px;
  overflow-x: hidden;
  overflow-y: auto;
  scroll-snap-type: none;
  touch-action: pan-y;
  overscroll-behavior-y: contain;
}
.toolbar-wrap.is-open .toolbar-handle::before { width: 22px; }

.dock {
  position: relative;
  width: 100%;
  max-width: 600px;
  display: grid;
  grid-template-columns: repeat(var(--rooms, 8), 1fr);
  padding: 4px;
  border-radius: var(--r-lg);
  background: rgba(255, 255, 255, .95);
  box-shadow: var(--sh-2);
}
.dock-indicator {
  position: absolute;
  top: 4px;
  bottom: 4px;
  left: 4px;
  width: calc((100% - 8px) / var(--rooms, 8));
  border-radius: var(--r-md);
  background: color-mix(in srgb, var(--accent) 14%, #fff);
  border: 2px solid color-mix(in srgb, var(--accent) 45%, transparent);
  transition: transform .35s var(--ease-spring), background .3s, border-color .3s;
}
.dock-tab {
  position: relative;
  min-width: 0;
  height: 46px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  border-radius: var(--r-md);
  transition: transform .18s var(--ease-spring);
}
.dock-icon { font-size: 22px; line-height: 1; white-space: nowrap; transition: transform .25s var(--ease-spring); }
/* only the room you're in is named; the rest are icons */
.dock-label {
  max-height: 0;
  overflow: hidden;
  opacity: 0;
  font-size: var(--fs-xs);
  font-weight: 800;
  line-height: 1.2;
  white-space: nowrap;
  color: var(--accent);
  transition: max-height .25s var(--ease-out), opacity .2s;
}
.dock-tab.is-active .dock-icon { transform: scale(1.05); }
.dock-tab.is-active .dock-label { max-height: 14px; opacity: 1; }
.dock-tab.is-pair .dock-icon { font-size: 15px; letter-spacing: -2px; }
.dock-tab.is-open { background: rgba(0, 0, 0, .05); }
.dock-picker {
  position: absolute;
  z-index: 3;
  display: flex;
  gap: 4px;
  padding: 4px;
  border-radius: var(--r-md);
  background: #fff;
  box-shadow: var(--sh-2);
  animation: dockPickerIn .2s var(--ease-spring);
}
@keyframes dockPickerIn { from { opacity: 0; transform: translateY(6px) scale(.94); } }
.dock-picker-item {
  position: relative;
  min-width: 64px;
  height: 52px;
  padding: 0 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border-radius: var(--r-md);
  font-size: var(--fs-xs);
  font-weight: 800;
  color: var(--ink-2);
}
.dock-picker-item.is-active { background: color-mix(in srgb, var(--accent) 14%, #fff); color: var(--accent); }
.dock-picker-item.is-pressed { transform: scale(.94); }
.dock-tab.is-pressed { transform: scale(.9); }
.dock-dot {
  position: absolute;
  top: 5px;
  right: 18%;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #ef4444;
  border: 2px solid #fff;
  animation: meterBlink 1.2s infinite;
}
.dock-partner { position: absolute; top: 2px; left: 12%; font-size: 11px; animation: heartBeat 1.4s ease-in-out infinite; }
@keyframes heartBeat { 50% { transform: scale(1.25); } }
.dock-tab.is-nudge { animation: dockNudge .9s var(--ease-out) 2; }
@keyframes dockNudge {
  0%, 100% { transform: translateY(0); }
  30% { transform: translateY(-8px); }
  60% { transform: translateY(0); }
  75% { transform: translateY(-3px); }
}

/* ---------------- small phones ---------------- */
@media (max-width: 360px) {
  .hud-cat { max-width: 30vw; padding-right: 6px; }
  .room-pill-days { display: none; }
  .dock-label { font-size: 10px; }
}
@media (max-height: 640px) {
  .hud { height: 42px; }
  .hud-cat, .hud-meters { height: 42px; }
  .hud-partner, .hud-menu { width: 38px; height: 38px; }
  .xp-ring { width: 36px; height: 36px; }
  .xp-ring-inner { width: 28px; height: 28px; font-size: 16px; }
  .dock-tab { height: 42px; }
  .toolbar-scroll-wrap { height: 44px; }
}

/* ---------------- landscape phones: stage left, controls right ---------------- */
@media (orientation: landscape) and (max-height: 540px) {
  #app-shell {
    grid-template-columns: minmax(0, 1fr) 270px;
    grid-template-rows: auto auto minmax(0, 1fr);
    grid-template-areas: "hud hud" "stage sub" "stage controls";
  }
  .subhud { flex-direction: column; align-items: stretch; height: auto; }
  .room-pill { justify-content: center; }
  .controls { justify-content: flex-end; min-height: 0; }
  .toolbar-wrap { flex-direction: column; align-items: stretch; flex: 1; min-height: 0; }
  .toolbar-handle { display: none; }
  .toolbar-wrap.is-open .toolbar-scroll-wrap, .toolbar-wrap.is-open .toolbar { max-height: none; }
  .toolbar-scroll-wrap { height: auto; flex: 1; min-height: 0; }
  .toolbar { flex-wrap: wrap; align-content: flex-start; overflow-x: hidden; overflow-y: auto; padding: 6px; touch-action: pan-y; }
  .dock { grid-template-columns: repeat(3, 1fr); }
  .dock-indicator { display: none; }
  .dock-tab.is-active { background: color-mix(in srgb, var(--accent) 14%, #fff); }
  .dock-tab { height: 44px; }
  .dock-label { max-height: 14px; opacity: 1; color: var(--ink-2); }
  .dock-tab.is-active .dock-label { color: var(--accent); }
}

/* no room yet (the welcome sheet is up): the HUD would only show a stand-in's numbers */
.no-room .hud, .no-room .subhud { visibility: hidden; }

/* a room's "how to play here" hint: above the toolbar, never over the cat */
.room-hint {
  position: absolute; left: 50%; bottom: calc(100% + 6px); z-index: 6; max-width: min(92vw, 520px); transform: translate(-50%, 6px);
  padding: 5px 12px; border-radius: 14px; background: rgba(255, 255, 255, .94); box-shadow: var(--sh-1); text-align: center; width: max-content;
  font-size: 12px; font-weight: 700; line-height: 1.4; color: var(--ink-2);
  opacity: 0; pointer-events: none; transition: opacity .25s, transform .3s var(--ease-spring);
}
.room-hint.is-in { opacity: 1; transform: translate(-50%, 0); }
