/* ============ Mandala Lights — premium dark glass UI ============ */
:root {
  --bg0: #04050f;
  --bg1: #0a0d24;
  --gold: #e8c268;
  --violet: #8b7cf6;
  --cyan: #5eead4;
  --text: #eef0fa;
  --text-dim: #9aa0bf;
  --glass: rgba(255, 255, 255, 0.055);
  --glass-border: rgba(255, 255, 255, 0.12);
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.55);
  --radius: 22px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg0);
  color: var(--text);
  font-family: "PingFang SC", "Microsoft YaHei", "Noto Sans SC",
    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  user-select: none;
  -webkit-user-select: none;
}

button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }

.gone { display: none !important; }

/* ---------- ambient home background ---------- */
#home {
  position: fixed; inset: 0;
  display: flex; flex-direction: column;
  overflow-y: auto;
  padding: calc(18px + var(--safe-top)) 20px calc(18px + var(--safe-bottom));
  background:
    radial-gradient(60vmax 60vmax at 15% 10%, rgba(139, 124, 246, 0.16), transparent 60%),
    radial-gradient(55vmax 55vmax at 85% 85%, rgba(232, 194, 104, 0.10), transparent 60%),
    radial-gradient(45vmax 45vmax at 80% 15%, rgba(94, 234, 212, 0.08), transparent 60%),
    linear-gradient(160deg, var(--bg1), var(--bg0) 70%);
  animation: bgdrift 24s ease-in-out infinite alternate;
}
@keyframes bgdrift {
  from { background-position: 0 0, 0 0, 0 0, 0 0; }
  to   { background-position: 4vmax 2vmax, -3vmax -2vmax, 2vmax 3vmax, 0 0; }
}

/* ---------- header ---------- */
header {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 12px;
  margin-bottom: clamp(10px, 3vh, 30px);
}
.brand { display: flex; align-items: center; gap: 14px; min-width: 0; }
.brand img {
  width: clamp(44px, 8vmin, 62px); height: clamp(44px, 8vmin, 62px);
  border-radius: 16px;
  box-shadow: 0 4px 24px rgba(139, 124, 246, 0.45);
}
#app-title {
  font-size: clamp(1.35rem, 4.5vmin, 2.2rem);
  font-weight: 700;
  letter-spacing: 0.06em;
  background: linear-gradient(100deg, var(--gold), #fdf6e3 40%, var(--violet) 90%);
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent; color: transparent;
  white-space: nowrap;
}
#app-sub {
  font-size: clamp(0.7rem, 2.2vmin, 0.92rem);
  color: var(--text-dim);
  margin-top: 3px;
  letter-spacing: 0.04em;
}

/* language switch — top right */
.lang-switch {
  display: flex; flex-shrink: 0;
  background: var(--glass);
  border: 1px solid var(--glass-border);
  border-radius: 999px;
  padding: 3px;
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}
.lang-btn {
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 0.85rem;
  color: var(--text-dim);
  transition: all 0.25s;
}
.lang-btn.active {
  color: #14142a;
  background: linear-gradient(120deg, var(--gold), #f5e3ae);
  font-weight: 700;
  box-shadow: 0 2px 12px rgba(232, 194, 104, 0.4);
}

/* ---------- effect grid ---------- */
#choose-hint {
  text-align: center;
  color: var(--text-dim);
  font-size: clamp(0.8rem, 2.4vmin, 1rem);
  letter-spacing: 0.12em;
  margin-bottom: clamp(12px, 2.5vh, 24px);
}

/* ---------- fuzzy search ---------- */
#search-bar {
  display: flex; align-items: center; gap: 8px;
  width: 100%;
  max-width: 520px;
  margin: 0 auto clamp(12px, 2.5vh, 22px);
  padding: 4px 6px 4px 14px;
  border-radius: 999px;
  background: var(--glass);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  transition: border-color 0.25s, box-shadow 0.25s;
}
#search-bar:focus-within {
  border-color: rgba(232, 194, 104, 0.5);
  box-shadow: 0 6px 26px rgba(139, 124, 246, 0.22);
}
.search-icon { font-size: 0.9rem; opacity: 0.75; flex: 0 0 auto; }
#search-input {
  flex: 1; min-width: 0;
  padding: 9px 0;
  border: none; outline: none; background: none;
  color: var(--text);
  font-family: inherit;
  font-size: clamp(0.85rem, 2.4vmin, 0.98rem);
  letter-spacing: 0.04em;
  user-select: text; -webkit-user-select: text;
}
#search-input::placeholder { color: rgba(154, 160, 191, 0.75); }
#search-clear {
  flex: 0 0 auto;
  width: 30px; height: 30px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 999px;
  font-size: 0.8rem;
  color: var(--text-dim);
  background: rgba(255, 255, 255, 0.07);
  transition: background 0.2s, color 0.2s;
}
#search-clear:hover { background: rgba(255, 255, 255, 0.16); color: var(--text); }
#search-status {
  text-align: center;
  color: var(--text-dim);
  font-size: clamp(0.72rem, 2.1vmin, 0.85rem);
  letter-spacing: 0.06em;
  margin: -6px 0 clamp(10px, 2vh, 18px);
}
.card mark {
  background: rgba(232, 194, 104, 0.26);
  color: #ffe9b0;
  border-radius: 3px;
  padding: 0 1px;
}
.card-desc mark { color: #e7d3a2; }
/* Filtered-out cards leave the grid entirely so the remaining ones reflow. */
.card.filtered-out { display: none; }

#grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(150px, 42vw), 1fr));
  gap: clamp(10px, 2vmin, 18px);
  max-width: 1080px;
  width: 100%;
  margin: 0 auto;
}
/* The card is a fixed-aspect box, so its height is a known multiple of its own
   width. Sizing the contents in container units (cqw) instead of viewport units
   keeps icon + 2-line name + clamped description inside that box at every
   breakpoint. Browsers without container queries fall back to the vmin clamps
   declared alongside each cqw rule below. */
.card {
  position: relative;
  container-type: inline-size;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 6px;
  gap: clamp(3px, 3cqw, 7px);
  padding: clamp(5px, 1.6vmin, 12px) clamp(4px, 1.2vmin, 10px);
  aspect-ratio: 1 / 0.88;
  border-radius: var(--radius);
  background: var(--glass);
  border: 1px solid var(--glass-border);
  overflow: hidden;
  /* The grid holds 100+ cards but only ~15 are ever on screen. Let the browser
     skip style, layout and paint for the rest until they scroll close, and tell
     it how tall to reserve so the scrollbar never jumps. This is what keeps the
     first paint flat as the collection keeps growing. */
  content-visibility: auto;
  contain-intrinsic-size: auto 180px;
  transition: transform 0.3s cubic-bezier(0.22, 1, 0.36, 1), border-color 0.3s, box-shadow 0.3s;
  animation: cardIn 0.45s cubic-bezier(0.22, 1, 0.36, 1) both;
  /* Stagger only across the first screenful. A flat `--i * 60ms` ramp meant the
     last card of 117 waited ~7s to fade in; the cap keeps the same cascade feel
     up front while everything below the fold is already painted on arrival. */
  animation-delay: min(calc(var(--i) * 22ms), 330ms);
}
@keyframes cardIn {
  from { opacity: 0; transform: translateY(24px) scale(0.94); }
  to   { opacity: 1; transform: none; }
}
.card:hover, .card:focus-visible {
  transform: translateY(-5px) scale(1.02);
  border-color: rgba(232, 194, 104, 0.55);
  box-shadow: 0 14px 40px rgba(139, 124, 246, 0.25);
}
.card:active { transform: scale(0.97); }
.card-number {
  position: absolute;
  top: clamp(8px, 1.6vmin, 13px);
  left: clamp(8px, 1.6vmin, 13px);
  z-index: 2;
  min-width: 2.15em;
  padding: 0.22em 0.5em;
  border: 1px solid rgba(232, 194, 104, 0.34);
  border-radius: 999px;
  background: rgba(4, 5, 15, 0.52);
  color: rgba(255, 225, 156, 0.88);
  font: 700 clamp(0.61rem, 1.65vmin, 0.75rem)/1 ui-monospace, SFMono-Regular, Consolas, monospace;
  letter-spacing: 0.08em;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.24);
  pointer-events: none;
}
.card-icon {
  flex: 0 0 auto;
  line-height: 1;
  font-size: clamp(2rem, 7vmin, 3rem);
  font-size: min(3rem, 27cqw);
  filter: drop-shadow(0 4px 14px rgba(139, 124, 246, 0.55));
}
.card-name {
  flex: 0 0 auto;
  max-width: 100%;
  font-size: clamp(0.95rem, 2.8vmin, 1.15rem);
  font-size: clamp(0.76rem, 9cqw, 1.15rem);
  font-weight: 700;
  line-height: 1.25;
  letter-spacing: 0.05em;
  text-align: center;
  overflow-wrap: anywhere;
  /* two lines is enough for every name; never push the icon out of the card */
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  overflow: hidden;
}
/* On the narrowest phones the cards are barely wider than the longest names,
   and the tracking alone is enough to force a third line. Buy that back here
   rather than shrinking every name down a size. */
@container (max-width: 150px) {
  .card-name { letter-spacing: 0.02em; }
}
/* Descriptions vary wildly in length, so clamp them to a fixed number of lines.
   flex-shrink stays enabled purely as a safety valve: if anything ever does
   overflow, the description absorbs it rather than the icon or name. */
.card-desc {
  flex: 0 1 auto;
  min-height: 0;
  max-width: 100%;
  font-size: clamp(0.66rem, 2vmin, 0.8rem);
  font-size: clamp(0.58rem, 7cqw, 0.8rem);
  line-height: 1.3;
  color: var(--text-dim);
  padding: 0 6px;
  text-align: center;
  overflow-wrap: anywhere;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  overflow: hidden;
}
.card-glow {
  position: absolute; inset: -40%;
  background: radial-gradient(circle at 50% 120%, rgba(232, 194, 104, 0.16), transparent 55%);
  opacity: 0;
  transition: opacity 0.4s;
  pointer-events: none;
}
.card:hover .card-glow { opacity: 1; }

/* ---------- footer / exit ---------- */
footer {
  margin-top: clamp(16px, 4vh, 40px);
  display: flex; flex-direction: column; align-items: center; gap: 14px;
  padding-bottom: 6px;
}
#btn-exit {
  padding: 12px 46px;
  border-radius: 999px;
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  color: #ffd9d9;
  background: rgba(255, 90, 90, 0.12);
  border: 1px solid rgba(255, 110, 110, 0.35);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  transition: all 0.25s;
}
#btn-exit:hover { background: rgba(255, 90, 90, 0.24); box-shadow: 0 6px 24px rgba(255, 90, 90, 0.25); }
#install-hint {
  font-size: 0.72rem;
  color: rgba(154, 160, 191, 0.75);
  text-align: center;
  max-width: 420px;
}

/* ---------- stage (animation view) ---------- */
#stage { position: fixed; inset: 0; background: #02030b; }
#stage-canvas { position: absolute; inset: 0; display: block; }

#stage-bar {
  position: absolute;
  top: calc(10px + var(--safe-top));
  left: 12px; right: 12px;
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px;
  border-radius: 18px;
  background: rgba(8, 10, 26, 0.55);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  box-shadow: var(--shadow);
  transition: opacity 0.5s, transform 0.5s;
}
#stage-bar.hidden-bar { opacity: 0; transform: translateY(-12px); pointer-events: none; }
/* Keep essential Water Shader controls reachable on touch devices, even if an
   older controller or an already scheduled timer adds hidden-bar. */
#stage.water-shader-touch #stage-bar {
  z-index: 10;
  opacity: 1;
  transform: none;
  pointer-events: auto;
}


#btn-back {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 16px;
  border-radius: 12px;
  background: var(--glass);
  border: 1px solid var(--glass-border);
  font-size: 0.9rem;
  transition: background 0.2s;
}
#btn-back:hover { background: rgba(255, 255, 255, 0.12); }
/* The card name, with the scene showing on a smaller line beneath it for cards with named scenes
   (a tooltip alone never shows on a phone, which has no hover). */
#stage-heading {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
}
#stage-subtitle {
  max-width: 100%;
  margin-top: 1px;
  font-size: clamp(0.68rem, 2.2vmin, 0.8rem);
  font-weight: 500;
  letter-spacing: 0.06em;
  line-height: 1.25;
  color: rgba(255, 255, 255, 0.72);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
#stage-subtitle[hidden] { display: none; }
/* On a phone the English names need the room: the back button keeps only its arrow, and the
   scene name may take two lines. */
@media (max-width: 480px) {
  #stage.has-subtitle #btn-back .lbl { display: none; }
  #stage.has-subtitle #btn-back { padding: 8px 12px; }
  #stage.has-subtitle #stage-title { letter-spacing: 0.04em; }
  #stage-subtitle {
    white-space: normal; text-align: center;
    display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  }
}
/* Cards that add their own buttons to the bar (水風聚合's motion speed and panning) set compact-bar, so on a
   phone the title keeps room beside the extra buttons. */
@media (max-width: 480px) {
  #stage.compact-bar #stage-bar { gap: 5px; padding: 8px; }
  #stage.compact-bar .icon-btn { width: 34px; height: 38px; font-size: 1rem; }
}
#stage-title {
  max-width: 100%;
  line-height: 1.25;
  text-align: center;
  font-size: clamp(0.95rem, 3vmin, 1.15rem);
  font-weight: 700;
  letter-spacing: 0.08em;
  background: linear-gradient(100deg, var(--gold), #fff 55%, var(--cyan));
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent; color: transparent;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.icon-btn {
  width: 42px; height: 42px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 12px;
  font-size: 1.15rem;
  background: var(--glass);
  border: 1px solid var(--glass-border);
  transition: background 0.2s, transform 0.2s;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.12); }
.icon-btn:active { transform: scale(0.92); }
.icon-btn.muted { opacity: 0.55; }
.icon-btn.scene-night {
  color: #ffd477;
  border-color: rgba(255, 198, 92, 0.48);
  box-shadow: inset 0 0 16px rgba(255, 175, 55, 0.12), 0 0 14px rgba(255, 181, 68, 0.16);
}
.icon-btn.pulse { animation: pulse 0.6s ease; }
@keyframes pulse {
  0% { transform: scale(1); } 40% { transform: scale(1.25) rotate(20deg); } 100% { transform: scale(1); }
}

/* ---------- goodbye ---------- */
#goodbye {
  position: fixed; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 16px;
  text-align: center;
  padding: 24px;
  background: radial-gradient(70vmax 70vmax at 50% 30%, rgba(139, 124, 246, 0.12), transparent 60%), var(--bg0);
}
#goodbye-title {
  font-size: clamp(1.6rem, 6vmin, 2.4rem);
  background: linear-gradient(100deg, var(--gold), #fff 55%, var(--violet));
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent; color: transparent;
}
#goodbye-msg { color: var(--text-dim); font-size: 1rem; }
#btn-restart {
  margin-top: 12px;
  padding: 12px 42px;
  border-radius: 999px;
  font-size: 1rem; font-weight: 600;
  color: #14142a;
  background: linear-gradient(120deg, var(--gold), #f5e3ae);
  box-shadow: 0 6px 26px rgba(232, 194, 104, 0.4);
  transition: transform 0.2s;
}
#btn-restart:hover { transform: translateY(-2px); }

/* ---------- landscape phones: tighter header ---------- */
@media (max-height: 480px) and (orientation: landscape) {
  header { margin-bottom: 8px; }
  .brand img { width: 38px; height: 38px; }
  #choose-hint { margin-bottom: 8px; }
  #grid { grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); }
  .card { aspect-ratio: 1 / 0.7; }
  .card-desc { display: none; }
  footer { margin-top: 12px; }
}

/* ---------- large screens ---------- */
@media (min-width: 1100px) {
  #grid { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
  /* taller cards here, so one more line of description fits */
  .card-desc { -webkit-line-clamp: 3; line-clamp: 3; }
}

@media (prefers-reduced-motion: reduce) {
  .card { animation: none; }
  #home { animation: none; }
}

/* Smoke is a text-free visual; retain accessible, icon-only navigation. */
#stage.smoke-view #stage-title,
#stage.smoke-view #stage-subtitle,
#stage.smoke-view #btn-back .lbl { display: none; }
#stage.smoke-view #stage-bar { justify-content: space-between; }
