/* Adelaide v2 Mini App — UX polish (session 18)
 *
 * Force dark theme regardless of Telegram light/dark mode.
 * WCAG-AAA contrast (>= 7:1) for all body text.
 * Hearts (Interest) + Stars (Trust) widget replaces progress bars.
 */

/* Hidden attribute always wins (v1 lesson) */
[hidden] { display: none !important; }

/* ─── Tokens ──────────────────────────────────────────────────────────── */

:root {
  /* Background palette — forced dark */
  --bg-primary: #0d0d1a;
  --bg-secondary: #1a1a2e;
  --bg-tertiary: #232241;
  --bg-message-user: #5b3bbb;
  --bg-message-user-grad-end: #7b5bdb;
  --bg-message-adelaide: #2a2a3e;
  --bg-input: #1f1f38;

  /* Text — all WCAG-AAA on bg-primary (#0d0d1a) */
  --text-primary: #ffffff;      /* contrast 19.0:1 — AAA */
  --text-secondary: #d0d0e0;    /* contrast 13.3:1 — AAA */
  --text-tertiary: #9090a8;     /* contrast 6.2:1 — AA   */
  --text-on-accent: #ffffff;

  /* Accent — purple/pink */
  --accent-purple: #5b3bbb;
  --accent-pink: #ff6b9d;
  --accent-grad: linear-gradient(135deg, #5b3bbb 0%, #ff6b9d 100%);

  /* State colors */
  --gold: #f0c067;
  --success: #4caf50;
  --danger: #ff5e5e;

  /* Episode dot states */
  --dot-bg: #2a2a3e;
  --dot-border: #444466;
  --dot-text: #888;
}

/* Hard override of Telegram theme variables so we don't end up with
   black text on dark backgrounds. */
body, .message-text, input, button, p, h1, h2, h3, span, div {
  color: var(--text-primary);
}

@media (prefers-color-scheme: light) {
  /* Keep our dark design even when Telegram is in light mode. */
  body { color: var(--text-primary); }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: linear-gradient(180deg, #0d0d1a 0%, #1a1a2e 100%) fixed;
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  height: 100%;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

#loader {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
}

#app {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100vh;
  height: 100dvh;
}

/* ─── HUD ─────────────────────────────────────────────────────────────── */

#hud {
  background: linear-gradient(180deg, var(--bg-tertiary) 0%, var(--bg-secondary) 100%);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  padding: 8px 12px 10px;
}

#hud-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 12px;
  margin-bottom: 6px;
}

#episode-counter {
  font-weight: 700;
  color: var(--gold);
  letter-spacing: 0.5px;
}

#episode-name {
  font-style: italic;
  color: var(--text-secondary);
  text-align: right;
  flex: 1;
  margin-left: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ─── Episode progress dots ───────────────────────────────────────────── */

.episode-progress {
  margin-bottom: 8px;
}

.episode-dots {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 2px;
}

.ep-dot {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--dot-bg);
  border: 2px solid var(--dot-border);
  color: var(--dot-text);
  font-size: 11px;
  font-weight: 600;
  transition: all 280ms ease;
}

.ep-dot.completed {
  background: var(--success);
  border-color: var(--success);
  color: white;
}

.ep-dot.active {
  background: var(--accent-grad);
  border-color: var(--accent-pink);
  color: white;
  box-shadow: 0 0 12px rgba(255, 107, 157, 0.55);
  transform: scale(1.15);
}

/* ─── Portrait area ───────────────────────────────────────────────────── */

#portrait-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 3;
  max-height: 36vh;
  border-radius: 14px;
  overflow: hidden;
  margin-bottom: 10px;
  background: #0a0a18;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

#background {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  filter: brightness(0.55) saturate(1.1);
}

#portrait {
  position: absolute;
  inset: 0;
  margin: auto;
  height: 100%;
  width: auto;
  display: block;
  filter: drop-shadow(0 0 14px rgba(192, 132, 252, 0.35));
  transition: filter 300ms ease;
}

#emotion-tag {
  position: absolute;
  bottom: 8px;
  right: 8px;
  background: rgba(0, 0, 0, 0.65);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: var(--text-primary);
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 11px;
  text-transform: lowercase;
  letter-spacing: 0.5px;
}

/* ─── Metrics (hearts + stars) ────────────────────────────────────────── */

.metrics {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.metric-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}

.metric-label {
  width: 64px;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0.2px;
}

.hearts, .stars {
  display: flex;
  gap: 2px;
  flex: 1;
}

.heart, .star {
  font-size: 18px;
  line-height: 1;
  display: inline-block;
  transform: scale(1);
  transition: transform 220ms cubic-bezier(0.34, 1.56, 0.64, 1);
  filter: grayscale(60%) opacity(0.5);
}

.heart.filled, .star.filled {
  filter: none;
}

.heart.filled {
  filter: drop-shadow(0 0 4px rgba(255, 100, 130, 0.55));
}

.star.filled {
  filter: drop-shadow(0 0 4px rgba(240, 192, 103, 0.6));
}

.metric-num {
  width: 28px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-size: 11px;
  color: var(--text-tertiary);
}

/* Animations for when a heart/star fills */
@keyframes pop {
  0%   { transform: scale(0.4); opacity: 0; }
  50%  { transform: scale(1.4); opacity: 1; }
  100% { transform: scale(1);   opacity: 1; }
}

.heart.pop-animation, .star.pop-animation {
  animation: pop 0.55s cubic-bezier(0.34, 1.56, 0.64, 1);
}

#personality-row {
  margin-top: 2px;
  text-align: center;
}

.personality-tag {
  font-size: 11px;
  color: var(--text-tertiary);
  font-style: italic;
}

/* ─── Chat ─────────────────────────────────────────────────────────────── */

#chat {
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  scroll-behavior: smooth;
}

.bubble {
  max-width: 80%;
  padding: 12px 16px;
  border-radius: 18px;
  word-wrap: break-word;
  white-space: pre-wrap;
  line-height: 1.5;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.18);
  animation: message-appear 0.4s ease-out;
}

@keyframes message-appear {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

.bubble.user {
  background: linear-gradient(
    135deg,
    var(--bg-message-user) 0%,
    var(--bg-message-user-grad-end) 100%
  );
  color: var(--text-on-accent) !important;
  align-self: flex-end;
  border-bottom-right-radius: 4px;
}

.bubble.npc {
  background: var(--bg-message-adelaide);
  color: var(--text-primary) !important;
  align-self: flex-start;
  border-bottom-left-radius: 4px;
}

/* Typing indicator — bouncing dots */
.bubble.typing {
  padding: 14px 18px;
  background: var(--bg-message-adelaide);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
  display: flex;
  gap: 5px;
  width: fit-content;
}

.typing-dot {
  width: 8px;
  height: 8px;
  background: rgba(255, 255, 255, 0.6);
  border-radius: 50%;
  animation: typing-bounce 1.4s infinite;
}
.typing-dot:nth-child(2) { animation-delay: 0.18s; }
.typing-dot:nth-child(3) { animation-delay: 0.36s; }

@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.55; }
  30%           { transform: translateY(-8px); opacity: 1; }
}

.metric-tick {
  font-size: 10px;
  margin-top: 1px;
  color: var(--text-tertiary);
  align-self: flex-start;
  letter-spacing: 0.5px;
}
.metric-tick.up { color: #7be08a; }
.metric-tick.down { color: var(--danger); }

.scene-divider {
  align-self: center;
  color: var(--gold);
  font-size: 12px;
  font-style: italic;
  padding: 4px 12px;
  border: 1px solid rgba(240, 192, 103, 0.4);
  border-radius: 12px;
  margin: 6px 0;
}

/* ─── Composer / choice buttons ───────────────────────────────────────── */

#composer-row {
  background: var(--bg-tertiary);
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  padding: 10px 12px;
}

#composer {
  display: flex;
  gap: 8px;
}

#composer-input {
  flex: 1;
  padding: 11px 14px;
  border: 1px solid transparent;
  border-radius: 20px;
  background: var(--bg-input);
  color: var(--text-primary) !important;
  font-size: 15px;
  outline: none;
}

#composer-input::placeholder {
  color: var(--text-tertiary);
}

#composer-input:focus {
  background: #252550;
  border-color: rgba(192, 132, 252, 0.5);
}

#send-btn {
  width: 42px;
  height: 42px;
  border: none;
  border-radius: 21px;
  background: var(--accent-grad);
  color: var(--text-on-accent);
  font-size: 20px;
  cursor: pointer;
  transition: transform 150ms;
}

#send-btn:hover, #send-btn:active { transform: scale(1.05); }
#send-btn:disabled { opacity: 0.45; cursor: not-allowed; transform: scale(1); }

#choice-area {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#choice-prompt {
  color: var(--gold);
  font-size: 13px;
  text-align: center;
  font-style: italic;
  line-height: 1.4;
}

#choice-buttons {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.choice-btn {
  padding: 12px 16px;
  background: rgba(192, 132, 252, 0.12);
  border: 1px solid rgba(192, 132, 252, 0.45);
  border-radius: 12px;
  color: var(--text-primary) !important;
  font-size: 14px;
  text-align: left;
  cursor: pointer;
  transition: all 180ms;
}
.choice-btn:hover, .choice-btn:active {
  background: rgba(192, 132, 252, 0.25);
  border-color: var(--accent-pink);
  transform: translateY(-1px);
}

/* ─── Overlays ────────────────────────────────────────────────────────── */

.screen-cover {
  position: fixed;
  inset: 0;
  background: var(--bg-primary);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(8, 8, 18, 0.94);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  animation: overlay-fade 0.45s ease;
}

@keyframes overlay-fade {
  from { opacity: 0; }
  to { opacity: 1; }
}

.tooltip-card, .transition-card, .ending-card {
  background: var(--bg-tertiary);
  border-radius: 16px;
  padding: 22px;
  max-width: 90%;
  width: 340px;
  border: 1px solid rgba(192, 132, 252, 0.4);
  text-align: center;
  animation: card-pop 320ms cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes card-pop {
  from { opacity: 0; transform: scale(0.85); }
  to   { opacity: 1; transform: scale(1); }
}

.tooltip-card p {
  margin: 0 0 14px;
  font-size: 14px;
  line-height: 1.5;
  color: var(--text-primary);
}

.tooltip-close,
.tooltip-card button,
.transition-card button,
.ending-card button {
  padding: 11px 22px;
  border-radius: 22px;
  border: none;
  background: var(--accent-grad);
  color: var(--text-on-accent);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  margin-top: 4px;
  transition: transform 150ms;
}

.tooltip-close:hover,
.tooltip-card button:hover,
.transition-card button:hover,
.ending-card button:hover {
  transform: scale(1.03);
}

/* Transition card — episode change */
.transition-card .sparkle {
  font-size: 48px;
  margin-bottom: 8px;
  animation: sparkle-rotate 2.4s ease-in-out infinite;
  display: inline-block;
}

@keyframes sparkle-rotate {
  0%, 100% { transform: rotate(-10deg); }
  50%      { transform: rotate(15deg); }
}

.transition-card h2 {
  color: var(--gold);
  font-size: 22px;
  margin: 0 0 8px;
}

.transition-card p {
  font-size: 15px;
  margin: 0 0 20px;
  color: var(--text-primary);
}

/* Ending card */
.ending-card { width: 360px; max-width: 95%; }

.ending-card h2 {
  font-size: 26px;
  margin: 0 0 12px;
  color: var(--gold);
}

.ending-headline {
  font-size: 14px;
  color: var(--text-secondary);
  font-style: italic;
  margin: 0 0 14px;
}

.ending-body {
  font-size: 14px;
  text-align: left;
  margin: 0 0 14px;
  line-height: 1.55;
  white-space: pre-line;
  color: var(--text-primary);
}

.ending-next {
  font-size: 13px;
  color: var(--text-tertiary);
  font-style: italic;
  margin: 0 0 16px;
}

.ending-stats {
  background: var(--bg-input);
  padding: 10px 12px;
  border-radius: 10px;
  margin-bottom: 16px;
  font-size: 12px;
  text-align: left;
  color: var(--text-secondary);
}
.ending-stats div { margin: 2px 0; }

/* ─── Metric change pop notification ──────────────────────────────────── */

.metric-change {
  position: fixed;
  top: 30%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0);
  background: var(--accent-grad);
  color: var(--text-on-accent) !important;
  padding: 14px 28px;
  border-radius: 50px;
  font-size: 22px;
  font-weight: 700;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.55);
  z-index: 999;
  opacity: 0;
  pointer-events: none;
  transition: all 0.42s cubic-bezier(0.34, 1.56, 0.64, 1);
  white-space: nowrap;
}

.metric-change.visible {
  transform: translate(-50%, -50%) scale(1);
  opacity: 1;
}

.metric-change.negative {
  background: linear-gradient(135deg, #5b3b3b 0%, #ff6b6b 100%);
}

.metric-change span { color: var(--text-on-accent); }

/* ─── Mobile width 390px sanity ───────────────────────────────────────── */

@media (max-width: 400px) {
  .ep-dot { width: 24px; height: 24px; font-size: 10px; }
  .metric-label { width: 56px; font-size: 11px; }
  .heart, .star { font-size: 16px; }
  .bubble { padding: 10px 13px; }
}

/* ─── Session 22 (Track 1): full-screen ending + collection ────────── */

.ending-screen {
  position: fixed;
  inset: 0;
  background: linear-gradient(135deg, #0d0d1a 0%, #2a1a3e 100%);
  overflow-y: auto;
  z-index: 1500;
  animation: overlay-fade 0.5s ease;
}

.ending-screen-inner {
  max-width: 500px;
  margin: 0 auto;
  padding: 32px 22px 56px;
  text-align: center;
  color: var(--text-primary);
}

.ending-trophy {
  font-size: 72px;
  margin: 4px 0 14px;
  animation: trophy-glow 2.4s ease-in-out infinite alternate;
  display: inline-block;
}

@keyframes trophy-glow {
  from { transform: scale(1);    filter: drop-shadow(0 0 8px rgba(255, 107, 157, 0.45)); }
  to   { transform: scale(1.05); filter: drop-shadow(0 0 24px rgba(255, 107, 157, 0.85)); }
}

.ending-screen-title {
  font-size: 30px;
  font-weight: 800;
  margin: 4px 0 4px;
  background: var(--accent-grad);
  -webkit-background-clip: text;
          background-clip: text;
  -webkit-text-fill-color: transparent;
  color: transparent;
}

.ending-screen-subtitle {
  font-size: 14px;
  color: var(--text-secondary);
  font-style: italic;
  margin: 0 0 20px;
}

.ending-screen-narrative {
  background: rgba(255, 255, 255, 0.04);
  border-left: 3px solid var(--accent-pink);
  padding: 14px 16px;
  margin: 18px 0 14px;
  text-align: left;
  line-height: 1.55;
  font-style: italic;
  white-space: pre-line;
  color: var(--text-primary);
  border-radius: 0 10px 10px 0;
}

.ending-screen-system {
  font-size: 13px;
  color: var(--text-secondary);
  text-align: left;
  white-space: pre-line;
  padding: 0 4px;
  margin-bottom: 22px;
}

.ending-screen-stats {
  background: var(--bg-input);
  border-radius: 12px;
  padding: 12px 14px;
  margin-bottom: 24px;
  font-size: 13px;
  text-align: left;
}

.ending-screen-stats .stat-row {
  display: flex;
  justify-content: space-between;
  padding: 4px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.ending-screen-stats .stat-row:last-child {
  border-bottom: none;
}

.endings-collection {
  margin-top: 6px;
}

.endings-collection h3 {
  font-size: 14px;
  color: var(--text-secondary);
  font-weight: 600;
  margin: 0 0 10px;
}

.endings-collection h3 span {
  color: var(--gold);
}

.endings-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 22px;
}

.ending-cell {
  aspect-ratio: 1;
  background: rgba(255, 255, 255, 0.04);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 30px;
  border: 2px solid transparent;
  transition: transform 200ms, border-color 200ms;
}

.ending-cell.unlocked {
  background: linear-gradient(
    135deg,
    rgba(91, 59, 187, 0.28) 0%,
    rgba(255, 107, 157, 0.28) 100%
  );
  border-color: var(--accent-pink);
}

.ending-cell.locked {
  color: rgba(255, 255, 255, 0.3);
  font-size: 24px;
}

.ending-cell.just-unlocked {
  animation: cell-pop 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
  transform: scale(1.08);
  box-shadow: 0 0 18px rgba(255, 107, 157, 0.6);
}

@keyframes cell-pop {
  0%   { transform: scale(0.5); opacity: 0; }
  60%  { transform: scale(1.2); opacity: 1; }
  100% { transform: scale(1.08); opacity: 1; }
}

.replay-hint {
  font-size: 13px;
  color: var(--text-secondary);
  font-style: italic;
  margin: 8px 0 14px;
  line-height: 1.5;
}

.replay-btn {
  background: var(--accent-grad);
  color: var(--text-on-accent);
  border: none;
  padding: 14px 28px;
  border-radius: 50px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  transition: transform 150ms;
}

.replay-btn:hover, .replay-btn:active {
  transform: scale(1.04);
}

@media (max-width: 400px) {
  .ending-trophy { font-size: 60px; }
  .ending-screen-title { font-size: 26px; }
  .ending-cell { font-size: 26px; }
}

/* ── Voice UI (Adelaide TTS + user voice STT) ─────────────────────────── */

.voice-btn {
  background: #2a2a3e;
  border: none;
  width: 44px;
  height: 44px;
  border-radius: 22px;
  font-size: 20px;
  cursor: pointer;
  color: white;
  margin-right: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

.voice-btn:active {
  transform: scale(0.96);
}

.voice-btn.recording {
  background: #ff4444;
  animation: voice-pulse 0.8s infinite;
}

@keyframes voice-pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50%      { opacity: 0.6; transform: scale(1.1); }
}

/* Adelaide voice message bubble inside the chat. */
.voice-message {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-radius: 16px;
  cursor: default;
}

.voice-play-btn {
  background: linear-gradient(135deg, #5b3bbb, #ff6b9d);
  border: none;
  width: 40px;
  height: 40px;
  border-radius: 20px;
  color: white;
  font-size: 16px;
  cursor: pointer;
  flex-shrink: 0;
}

.voice-play-btn:active { transform: scale(0.95); }

.voice-message-info {
  font-size: 13px;
  color: rgba(255, 255, 255, 0.65);
}
