:root {
  --bg-deep: #0b0614;
  --bg-panel: rgba(28, 18, 45, 0.72);
  --primary: #d4af37;
  --primary-light: #f3d77c;
  --accent-purple: #7b4be2;
  --accent-pink: #c77dff;
  --text-main: #f0e6ff;
  --text-muted: #b8a9cf;
  --card-back: linear-gradient(135deg, #1a0f2e 0%, #2d1b4e 100%);
  --card-border: rgba(212, 175, 55, 0.45);
  --shadow-glow: 0 0 25px rgba(123, 75, 226, 0.3);
  --transition: all 0.4s cubic-bezier(0.23, 1, 0.32, 1);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* Utility class used by JS to show/hide screens. !important ensures it
   reliably overrides display: flex / grid / block on the various sections. */
.hidden {
  display: none !important;
}

html {
  scroll-behavior: smooth;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: radial-gradient(circle at 20% 10%, #1a0f2e 0%, var(--bg-deep) 40%),
              radial-gradient(circle at 80% 90%, #24133d 0%, var(--bg-deep) 40%);
  background-attachment: fixed;
  color: var(--text-main);
  font-family: 'Noto Sans Thai', sans-serif;
  overflow-x: hidden;
  position: relative;
}

/* === Star field — two layers for parallax depth === */
.stars {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image:
    radial-gradient(1.5px 1.5px at 20px 30px, rgba(255,255,255,0.7), transparent),
    radial-gradient(1.2px 1.2px at 50px 120px, rgba(255,255,255,0.5), transparent),
    radial-gradient(1px 1px at 90px 80px, rgba(255,255,255,0.6), transparent),
    radial-gradient(1.8px 1.8px at 150px 40px, rgba(255,255,255,0.4), transparent),
    radial-gradient(1.3px 1.3px at 220px 180px, rgba(255,255,255,0.6), transparent),
    radial-gradient(1.6px 1.6px at 300px 70px, rgba(255,255,255,0.5), transparent),
    radial-gradient(1.1px 1.1px at 380px 220px, rgba(255,255,255,0.4), transparent),
    radial-gradient(1.4px 1.4px at 450px 140px, rgba(255,255,255,0.7), transparent),
    radial-gradient(1px 1px at 520px 60px, rgba(255,255,255,0.5), transparent),
    radial-gradient(1.7px 1.7px at 600px 200px, rgba(255,255,255,0.4), transparent);
  background-size: 650px 300px;
  opacity: 0.45;
  animation: twinkle 12s ease-in-out infinite alternate;
  z-index: 0;
}

.stars::after {
  content: '';
  position: absolute;
  inset: 0;
  background-image:
    radial-gradient(0.8px 0.8px at 100px 50px, rgba(199, 125, 255, 0.6), transparent),
    radial-gradient(0.6px 0.6px at 250px 200px, rgba(243, 215, 124, 0.5), transparent),
    radial-gradient(0.9px 0.9px at 400px 100px, rgba(199, 125, 255, 0.4), transparent),
    radial-gradient(0.7px 0.7px at 550px 250px, rgba(243, 215, 124, 0.6), transparent),
    radial-gradient(0.5px 0.5px at 180px 280px, rgba(199, 125, 255, 0.5), transparent);
  background-size: 700px 350px;
  opacity: 0.5;
  animation: twinkle 8s ease-in-out infinite alternate-reverse;
}

@keyframes twinkle {
  from { opacity: 0.35; }
  to { opacity: 0.6; }
}

/* Shooting star */
.shooting-star {
  position: fixed;
  top: 10%;
  left: -10%;
  width: 2px;
  height: 2px;
  background: linear-gradient(45deg, rgba(255,255,255,0.9), rgba(199,125,255,0));
  border-radius: 50%;
  box-shadow: 0 0 6px rgba(255,255,255,0.8);
  z-index: 0;
  pointer-events: none;
  animation: shoot 7s ease-in infinite;
  animation-delay: 3s;
}

.shooting-star::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 80px;
  height: 1px;
  background: linear-gradient(90deg, rgba(255,255,255,0.8), transparent);
  transform: translateX(-80px) rotate(0deg);
}

@keyframes shoot {
  0% { transform: translate(0, 0) rotate(35deg); opacity: 0; }
  5% { opacity: 1; }
  20% { opacity: 1; }
  30% { transform: translate(400px, 280px) rotate(35deg); opacity: 0; }
  100% { transform: translate(400px, 280px) rotate(35deg); opacity: 0; }
}

/* === Header === */
.app-header {
  text-align: center;
  padding: clamp(1.25rem, 4vh, 2.5rem) 1rem 1rem;
  position: relative;
  z-index: 1;
}

.app-header h1 {
  font-family: 'Noto Serif Thai', serif;
  font-size: clamp(2rem, 5vw, 3.5rem);
  color: var(--primary-light);
  letter-spacing: 0.12em;
  text-shadow: 0 0 20px rgba(212, 175, 55, 0.35);
  margin-bottom: 0.5rem;
}

.app-header .tagline {
  color: var(--text-muted);
  font-style: italic;
  font-size: 1.05rem;
}

.app-main {
  position: relative;
  z-index: 1;
  flex: 1 0 auto;
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 max(1rem, env(safe-area-inset-right)) 3rem max(1rem, env(safe-area-inset-left));
}

/* === Intro screen === */
.intro-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: min(calc(100svh - 13rem), 32rem);
  padding: 2rem 0;
  text-align: center;
}

.intro-content {
  width: min(560px, 100%);
  max-width: 560px;
  padding: 2.5rem;
  background: var(--bg-panel);
  border: 1px solid rgba(212, 175, 55, 0.2);
  border-radius: 20px;
  box-shadow: var(--shadow-glow);
  backdrop-filter: blur(12px);
  position: relative;
  overflow: hidden;
}

.intro-content::before {
  content: '';
  position: absolute;
  inset: -1px;
  border-radius: 20px;
  padding: 1px;
  background: linear-gradient(135deg, rgba(212,175,55,0.4), transparent 40%, rgba(199,125,255,0.4));
  -webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
  -webkit-mask-composite: xor;
  mask-composite: exclude;
  opacity: 0.6;
  animation: borderShimmer 6s ease-in-out infinite alternate;
  pointer-events: none;
}

@keyframes borderShimmer {
  from { opacity: 0.3; }
  to { opacity: 0.7; }
}

.moon {
  font-size: 4.5rem;
  margin-bottom: 1rem;
  filter: drop-shadow(0 0 20px rgba(199, 125, 255, 0.5));
  animation: float 5s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-12px); }
}

.intro-content h2 {
  font-family: 'Noto Serif Thai', serif;
  font-size: 1.8rem;
  color: var(--primary);
  margin-bottom: 1rem;
  letter-spacing: 0.04em;
}

.intro-content > p {
  color: var(--text-muted);
  line-height: 1.7;
  margin-bottom: 1.8rem;
}

/* Idle shuffle deck — plays on a loop until the client draws their spread */
.shuffle-deck {
  position: relative;
  width: 92px;
  height: 142px;
  margin: 0 auto 1.8rem;
}

.shuffle-card {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  background: var(--card-back);
  background-image:
    radial-gradient(circle at 50% 50%, rgba(199, 125, 255, 0.12) 0%, transparent 60%),
    repeating-linear-gradient(45deg, transparent, transparent 8px, rgba(212, 175, 55, 0.04) 8px, rgba(212, 175, 55, 0.04) 16px);
  border: 1px solid var(--card-border);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
}

.shuffle-card span {
  font-size: 1.5rem;
  color: rgba(212, 175, 55, 0.4);
  text-shadow: 0 0 10px rgba(199, 125, 255, 0.3);
}

/* A loose resting stack (before/without JS animation) instead of one flat card */
.shuffle-card:nth-child(1) { transform: rotate(-7deg) translate(-5px, 2px); }
.shuffle-card:nth-child(2) { transform: rotate(-3deg) translate(-2px, 0); }
.shuffle-card:nth-child(3) { transform: rotate(1deg); }
.shuffle-card:nth-child(4) { transform: rotate(3deg) translate(2px, 0); }
.shuffle-card:nth-child(5) { transform: rotate(7deg) translate(5px, 2px); }

/* === Buttons === */
.btn-primary {
  font-family: 'Noto Serif Thai', serif;
  font-size: 1.05rem;
  letter-spacing: 0.08em;
  color: #1a0f2e;
  background: linear-gradient(135deg, var(--primary-light) 0%, var(--primary) 50%, var(--primary-light) 100%);
  background-size: 200% 200%;
  border: none;
  border-radius: 50px;
  padding: 0.9rem 2.2rem;
  cursor: pointer;
  transition: var(--transition);
  box-shadow: 0 4px 20px rgba(212, 175, 55, 0.3);
  animation: shimmer 3s ease-in-out infinite;
}

@keyframes shimmer {
  0%, 100% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
}

.btn-primary:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 30px rgba(212, 175, 55, 0.5);
}

.btn-primary:active {
  transform: translateY(-1px);
}

/* === Spread screen === */
.spread-screen {
  animation: fadeIn 0.8s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(15px); }
  to { opacity: 1; transform: translateY(0); }
}

.spread-header {
  text-align: center;
  margin-bottom: 2rem;
}

.spread-header h2 {
  font-family: 'Noto Serif Thai', serif;
  font-size: clamp(1.5rem, 3vw, 2rem);
  color: var(--primary);
  margin-bottom: 0.4rem;
}

.spread-header p {
  color: var(--text-muted);
  font-style: italic;
}

.spread-layout {
  position: relative; /* scopes the card zoom overlay to the spread area */
  width: fit-content; /* hug the cards instead of stretching to the grid column */
  margin-inline: auto;
  display: flex;
  gap: clamp(1rem, 3vw, 2rem);
  justify-content: center;
  align-items: flex-start;
  flex-wrap: nowrap;
  margin-bottom: 2rem;
}

.cross-area {
  display: grid;
  grid-template-columns: repeat(3, var(--card-width, 110px));
  grid-template-rows: repeat(3, var(--card-height, 170px));
  gap: 2.2rem 0.6rem;
  padding: 1rem;
}

.staff-area {
  display: grid;
  grid-template-columns: var(--card-width, 110px);
  grid-template-rows: repeat(4, var(--card-height, 170px));
  gap: 2.2rem 0.6rem;
}

/* === Card slots === */
.card-slot {
  perspective: 1000px;
  width: var(--card-width, 110px);
  height: var(--card-height, 170px);
  cursor: pointer;
  position: relative;
}

.card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transition: transform 0.7s cubic-bezier(0.4, 0, 0.2, 1);
  transform-style: preserve-3d;
  border-radius: 10px;
}

.card-front,
.card-back {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--card-border);
}

.card-front {
  background: var(--card-back);
  display: flex;
  align-items: center;
  justify-content: center;
}

.card-pattern {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background:
    radial-gradient(circle at 50% 50%, rgba(199, 125, 255, 0.12) 0%, transparent 60%),
    repeating-linear-gradient(45deg, transparent, transparent 8px, rgba(212, 175, 55, 0.04) 8px, rgba(212, 175, 55, 0.04) 16px);
}

.card-pattern span {
  font-size: 1.8rem;
  color: rgba(212, 175, 55, 0.35);
  text-shadow: 0 0 15px rgba(199, 125, 255, 0.3);
}

.card-back {
  transform: rotateY(180deg);
  background: #1a0f2e;
  display: flex;
  align-items: center;
  justify-content: center;
}

.card-back img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  opacity: 0;
  transition: opacity 0.5s ease;
}

.card-back img.loaded {
  opacity: 1;
}

.card-back.reversed img {
  transform: rotate(180deg);
}

.card-slot.revealed .card-inner {
  transform: rotateY(180deg);
}

.card-slot:not(.overlap):hover .card-inner {
  transform: translateY(-6px);
}

.card-slot.revealed:not(.overlap):hover .card-inner {
  transform: rotateY(180deg) translateY(-6px);
}

.card-slot:hover .card-inner {
  filter: brightness(1.1);
}

.position-label {
  position: absolute;
  top: calc(100% + 0.3rem);
  left: 50%;
  transform: translateX(-50%);
  font-family: 'Noto Serif Thai', serif;
  font-size: 0.7rem;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  width: max-content;
  max-width: calc(var(--card-width, 110px) + 1.8rem);
  line-height: 1.3;
  text-align: center;
  opacity: 0;
  transition: opacity 0.4s ease;
  pointer-events: none;
}

.card-slot.revealed .position-label {
  opacity: 1;
}

/* Overlap (crossing card) */
.card-slot.overlap {
  align-self: center;
  height: var(--card-height, 170px);
  width: var(--card-width, 110px);
  z-index: 2;
  /* This slot shares its grid cell with the present card underneath. Its
     own box is the full unrotated square, which would otherwise block every
     click there — hand hit-testing to .card-inner below, which IS rotated,
     so clicks outside its actual (rotated) shape fall through to the
     present card's slot instead of being eaten here. */
  pointer-events: none;
}

.card-slot.overlap .card-inner {
  height: var(--card-height, 170px);
  width: var(--card-width, 110px);
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%) rotateZ(90deg);
  position: absolute;
  pointer-events: auto;
}

.card-slot.revealed.overlap .card-inner {
  transform: translate(-50%, -50%) rotateY(180deg) rotateZ(90deg);
}

.card-slot.overlap .card-back.reversed,
.card-slot.revealed.overlap .card-back.reversed {
  transform: rotateY(180deg) rotateZ(180deg);
}

.card-slot.overlap:hover .card-inner {
  filter: brightness(1.15);
}

.card-slot.overlap .position-label {
  opacity: 0;
  pointer-events: none;
}

/* Selected card */
.card-slot.selected .card-inner {
  box-shadow: 0 0 28px rgba(212, 175, 55, 0.55), 0 8px 30px rgba(0, 0, 0, 0.5);
}

.card-slot.revealed.selected:not(.overlap) .card-inner {
  transform: rotateY(180deg) translateY(-8px) scale(1.03);
}

.card-slot.revealed.selected.overlap .card-inner {
  transform: translate(-50%, -50%) rotateY(180deg) rotateZ(90deg) scale(1.03);
  filter: brightness(1.2);
}

/* === Reading panel === */
.reading-panel {
  max-width: 720px;
  margin: 2rem auto;
  padding: 0 1rem;
  animation: fadeIn 0.6s ease;
}

.reading-card {
  background: var(--bg-panel);
  border: 1px solid var(--card-border);
  border-radius: 16px;
  padding: 2rem;
  box-shadow: var(--shadow-glow);
  backdrop-filter: blur(10px);
}

.reading-placeholder {
  text-align: center;
  padding: 1.5rem;
}

.reading-placeholder .icon {
  font-size: 3rem;
  margin-bottom: 1rem;
  opacity: 0.6;
  animation: float 4s ease-in-out infinite;
}

.reading-placeholder h3 {
  font-family: 'Noto Serif Thai', serif;
  color: var(--primary);
  font-size: 1.3rem;
  margin-bottom: 0.5rem;
}

.reading-placeholder p {
  color: var(--text-muted);
}

.reading-header {
  display: flex;
  align-items: center;
  gap: 1.2rem;
  margin-bottom: 1.5rem;
}

.reading-icon {
  flex-shrink: 0;
  width: 80px;
  height: 120px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--card-border);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.4);
}

.reading-icon img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.reading-icon img.reversed {
  transform: rotate(180deg);
}

.reading-title h3 {
  font-family: 'Noto Serif Thai', serif;
  font-size: 1.4rem;
  color: var(--primary-light);
  margin-bottom: 0.3rem;
}

.reading-position {
  color: var(--accent-pink);
  font-style: italic;
  font-size: 0.9rem;
  margin-bottom: 0.5rem;
}

.reading-tags {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-top: 0.4rem;
}

.reading-tag {
  font-size: 0.75rem;
  padding: 0.25rem 0.6rem;
  border-radius: 50px;
  background: rgba(123, 75, 226, 0.2);
  color: var(--accent-pink);
  border: 1px solid rgba(199, 125, 255, 0.3);
}

.reading-divider {
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--card-border), transparent);
  margin: 1.2rem 0;
}

.reading-section h4 {
  font-family: 'Noto Serif Thai', serif;
  color: var(--primary);
  font-size: 0.95rem;
  margin-bottom: 0.5rem;
}

.reading-section p {
  line-height: 1.7;
  color: var(--text-main);
}

.reading-section .meaning {
  margin-bottom: 1rem;
}

.orientation-badge {
  display: inline-block;
  padding: 0.3rem 0.7rem;
  border-radius: 50px;
  font-family: 'Noto Serif Thai', serif;
  font-size: 0.75rem;
  margin-bottom: 1rem;
}

.orientation-badge.upright {
  background: rgba(123, 211, 123, 0.15);
  color: #a2f0a2;
  border: 1px solid rgba(123, 211, 123, 0.35);
}

.orientation-badge.reversed {
  background: rgba(231, 111, 111, 0.15);
  color: #ff9e9e;
  border: 1px solid rgba(231, 111, 111, 0.35);
}

/* === AI prompt panel === */
.ai-prompt-panel {
  max-width: 720px;
  margin: 0 auto 3rem;
  text-align: center;
  animation: fadeIn 0.8s ease;
}

.btn-ask-gemini {
  font-family: 'Noto Serif Thai', serif;
  font-size: 0.95rem;
  letter-spacing: 0.06em;
  color: var(--primary-light);
  background: rgba(123, 75, 226, 0.15);
  border: 1px solid rgba(199, 125, 255, 0.35);
  border-radius: 50px;
  padding: 0.75rem 1.8rem;
  cursor: pointer;
  transition: var(--transition);
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  animation: pulseGlow 3s ease-in-out infinite;
}

@keyframes pulseGlow {
  0%, 100% { box-shadow: 0 0 10px rgba(199, 125, 255, 0.15); }
  50% { box-shadow: 0 0 25px rgba(199, 125, 255, 0.35); }
}

.btn-ask-gemini:hover {
  background: rgba(123, 75, 226, 0.3);
  box-shadow: 0 0 25px rgba(199, 125, 255, 0.4);
  transform: translateY(-2px);
}

/* === Chat panel === */
.chat-panel {
  max-width: 720px;
  margin: 0 auto 3rem;
  animation: fadeIn 0.8s ease;
}

.chat-card {
  background: var(--bg-panel);
  border: 1px solid var(--card-border);
  border-radius: 16px;
  box-shadow: var(--shadow-glow);
  backdrop-filter: blur(10px);
  overflow: hidden;
}

.chat-title {
  font-family: 'Noto Serif Thai', serif;
  color: var(--primary-light);
  font-size: 1.1rem;
  padding: 1rem 1.5rem 0.2rem;
  text-align: center;
}

.chat-subtitle {
  color: var(--text-muted);
  font-size: 0.8rem;
  font-style: italic;
  padding: 0 1.5rem 0.8rem;
  text-align: center;
  border-bottom: 1px solid rgba(212, 175, 55, 0.1);
}

.chat-messages {
  max-height: 380px;
  overflow-y: auto;
  padding: 1rem 1.2rem;
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
}

.chat-messages::-webkit-scrollbar {
  width: 6px;
}

.chat-messages::-webkit-scrollbar-track {
  background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
  background: rgba(123, 75, 226, 0.3);
  border-radius: 3px;
}

.chat-placeholder {
  text-align: center;
  padding: 2rem 1rem;
  color: var(--text-muted);
}

.chat-placeholder-icon {
  font-size: 2.5rem;
  margin-bottom: 0.8rem;
  opacity: 0.5;
  animation: float 4s ease-in-out infinite;
}

.chat-placeholder p {
  font-size: 0.9rem;
  font-style: italic;
}

.chat-message {
  max-width: 85%;
  padding: 0.7rem 1rem;
  border-radius: 14px;
  line-height: 1.6;
  font-size: 0.92rem;
  white-space: pre-wrap;
  word-wrap: break-word;
  animation: messageIn 0.3s ease;
}

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

.chat-message.user {
  align-self: flex-end;
  background: rgba(212, 175, 55, 0.18);
  color: var(--primary-light);
  border: 1px solid rgba(212, 175, 55, 0.3);
  border-bottom-right-radius: 4px;
}

.chat-message.ai {
  align-self: flex-start;
  background: rgba(123, 75, 226, 0.15);
  color: var(--text-main);
  border: 1px solid rgba(199, 125, 255, 0.25);
  border-bottom-left-radius: 4px;
}

.chat-message.ai strong,
.chat-message.ai em {
  color: var(--accent-pink);
}

.chat-message.ai.markdown-content {
  white-space: normal;
}

.chat-message.ai.markdown-content > :first-child {
  margin-top: 0;
}

.chat-message.ai.markdown-content > :last-child {
  margin-bottom: 0;
}

.chat-message.ai.markdown-content p {
  margin: 0.65em 0;
}

/* Hovering/touching a card's name (not the whole paragraph) shows it large */
.card-name-trigger {
  /* Plain magnifying glass, no +/- badge (that's what the native zoom-in/
     zoom-out keywords render as — there's no bare-lens keyword in CSS). */
  cursor: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><circle cx="10" cy="10" r="6" fill="none" stroke="black" stroke-width="3"/><circle cx="10" cy="10" r="6" fill="none" stroke="white" stroke-width="1.4"/><line x1="14.6" y1="14.6" x2="21" y2="21" stroke="black" stroke-width="3" stroke-linecap="round"/><line x1="14.6" y1="14.6" x2="21" y2="21" stroke="white" stroke-width="1.4" stroke-linecap="round"/></svg>') 10 10, zoom-in;
  border-radius: 4px;
  padding: 0 0.15em;
  text-decoration: underline dotted rgba(199, 125, 255, 0.6);
  text-underline-offset: 0.15em;
  transition: background 0.2s ease;
}

.card-name-trigger:hover {
  background: rgba(199, 125, 255, 0.18);
}

/* === Card zoom overlay — scoped to the spread area, not the full screen === */
.card-zoom-overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(6, 3, 14, 0.85);
  backdrop-filter: blur(6px);
  border-radius: 20px;
  padding: 1.5rem;
  animation: fadeIn 0.25s ease;
}

.card-zoom-frame {
  width: min(75%, 260px);
  max-height: 90%;
}

.card-zoom-frame img {
  width: 100%;
  height: auto;
  display: block;
  border-radius: 14px;
  border: 1px solid var(--card-border);
  box-shadow: 0 0 60px rgba(199, 125, 255, 0.4), 0 20px 60px rgba(0, 0, 0, 0.6);
  /* The touch-and-hold that opens this view would otherwise land on the
     image itself once it appears, triggering the phone's native "Save
     Image" long-press menu — this is just a preview, not a photo to save. */
  -webkit-touch-callout: none;
  -webkit-user-drag: none;
  user-select: none;
  pointer-events: none;
}

.card-zoom-frame img.reversed {
  transform: rotate(180deg);
}

.card-zoom-close {
  position: absolute;
  top: 0.6rem;
  right: 0.6rem;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid rgba(199, 125, 255, 0.4);
  background: rgba(123, 75, 226, 0.25);
  color: var(--text-main);
  font-size: 1.15rem;
  cursor: pointer;
  transition: var(--transition);
}

.card-zoom-close:hover {
  background: rgba(123, 75, 226, 0.4);
}

.card-zoom-close:focus-visible {
  outline: 2px solid var(--accent-pink);
  outline-offset: 3px;
}

.chat-message.ai.markdown-content h1,
.chat-message.ai.markdown-content h2,
.chat-message.ai.markdown-content h3,
.chat-message.ai.markdown-content h4 {
  margin: 1.15em 0 0.45em;
  color: var(--primary-light);
  font-family: 'Noto Serif Thai', serif;
  line-height: 1.35;
}

.chat-message.ai.markdown-content h1 { font-size: 1.25rem; }
.chat-message.ai.markdown-content h2 { font-size: 1.16rem; }
.chat-message.ai.markdown-content h3 { font-size: 1.08rem; }
.chat-message.ai.markdown-content h4 { font-size: 1rem; }

.chat-message.ai.markdown-content ul,
.chat-message.ai.markdown-content ol {
  margin: 0.65em 0;
  padding-left: 1.5rem;
}

.chat-message.ai.markdown-content li + li {
  margin-top: 0.35em;
}

.chat-message.ai.markdown-content hr {
  margin: 1.1em 0;
  border: 0;
  border-top: 1px solid rgba(199, 125, 255, 0.3);
}

.chat-message.ai.markdown-content blockquote {
  margin: 0.75em 0;
  padding: 0.2em 0 0.2em 0.8em;
  border-left: 3px solid rgba(212, 175, 55, 0.65);
  color: var(--text-muted);
}

.chat-message.ai.markdown-content code {
  padding: 0.1em 0.35em;
  border-radius: 4px;
  background: rgba(10, 8, 28, 0.5);
  font-size: 0.88em;
}

.chat-message.ai.markdown-content pre {
  overflow-x: auto;
  padding: 0.75em;
  border-radius: 8px;
  background: rgba(10, 8, 28, 0.65);
}

.chat-message.ai.markdown-content pre code {
  padding: 0;
  background: transparent;
}

.chat-message.ai.markdown-content a {
  color: var(--primary-light);
  text-decoration-color: rgba(212, 175, 55, 0.55);
  text-underline-offset: 0.15em;
}

.typing-indicator {
  align-self: flex-start;
  display: flex;
  gap: 5px;
  padding: 0.7rem 1rem;
  background: rgba(123, 75, 226, 0.15);
  border: 1px solid rgba(199, 125, 255, 0.25);
  border-radius: 14px;
  border-bottom-left-radius: 4px;
}

.typing-indicator span {
  width: 7px;
  height: 7px;
  background: var(--accent-pink);
  border-radius: 50%;
  animation: typingBounce 1.2s ease-in-out infinite;
  opacity: 0.4;
}

.typing-indicator span:nth-child(2) {
  animation-delay: 0.15s;
}

.typing-indicator span:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes typingBounce {
  0%, 60%, 100% { opacity: 0.4; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-5px); }
}

.chat-input-row {
  display: flex;
  gap: 0.6rem;
  padding: 0.8rem 1.2rem 1rem;
  border-top: 1px solid rgba(212, 175, 55, 0.1);
}

.chat-input {
  flex: 1;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(212, 175, 55, 0.2);
  border-radius: 50px;
  padding: 0.6rem 1.2rem;
  color: var(--text-main);
  font-family: 'Noto Sans Thai', sans-serif;
  font-size: 0.92rem;
  outline: none;
  transition: var(--transition);
}

.chat-input::placeholder {
  color: var(--text-muted);
  font-style: italic;
}

.chat-input:focus {
  border-color: var(--accent-pink);
  box-shadow: 0 0 12px rgba(199, 125, 255, 0.15);
}

.btn-chat-send {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: none;
  background: linear-gradient(135deg, var(--accent-purple), #9b6bf0);
  color: #fff;
  font-size: 1.3rem;
  cursor: pointer;
  transition: var(--transition);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  box-shadow: 0 2px 12px rgba(123, 75, 226, 0.3);
}

.btn-chat-send:hover {
  transform: scale(1.08);
  box-shadow: 0 4px 20px rgba(123, 75, 226, 0.5);
}

.btn-chat-send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.btn-chat-send:focus-visible {
  outline: 2px solid var(--accent-pink);
  outline-offset: 3px;
}

/* === Footer === */
.app-footer {
  text-align: center;
  color: var(--text-muted);
  padding: 2rem max(1rem, env(safe-area-inset-right)) max(2rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
  font-size: 0.85rem;
  position: relative;
  z-index: 1;
}

/* === Focus states for accessibility === */
.btn-primary:focus-visible,
.btn-ask-gemini:focus-visible {
  outline: 2px solid var(--accent-pink);
  outline-offset: 3px;
}

.card-slot:focus-visible {
  outline: 2px solid var(--accent-pink);
  outline-offset: 4px;
  border-radius: 12px;
}

/* === Reduced motion === */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
  }
}

/* === Responsive === */
body.reading-mode {
  --card-height: clamp(108px, 16dvh, 160px);
  --card-width: calc(var(--card-height) * 0.647);
}

body.reading-mode .app-header {
  padding-block: 0.85rem 0.6rem;
}

body.reading-mode .app-header h1 {
  font-size: clamp(1.7rem, 3vw, 2.35rem);
  margin-bottom: 0.2rem;
}

body.reading-mode .app-header .tagline {
  font-size: 0.9rem;
}

body.reading-mode .spread-header {
  margin-bottom: 1rem;
}

body.reading-mode .spread-layout {
  margin-bottom: 0;
}

@media (min-width: 1100px) {
  body.reading-mode .app-main {
    display: grid;
    grid-template-columns: minmax(560px, 1.08fr) minmax(400px, 0.92fr);
    grid-template-areas:
      "spread chat"
      "reading chat";
    align-items: start;
    gap: 1.25rem clamp(1.5rem, 3vw, 3rem);
    max-width: 1440px;
    padding-inline: clamp(1.25rem, 3vw, 3rem);
  }

  body.reading-mode .spread-screen {
    grid-area: spread;
    min-width: 0;
  }

  body.reading-mode .reading-panel {
    grid-area: reading;
    width: 100%;
    max-width: none;
    margin: 0;
    padding: 0;
  }

  body.reading-mode .chat-panel {
    grid-area: chat;
    position: sticky;
    top: 1rem;
    width: 100%;
    max-width: none;
    margin: 0;
  }

  body.reading-mode .chat-card {
    display: grid;
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    height: clamp(420px, calc(100dvh - 8rem), 760px);
  }

  body.reading-mode .chat-messages {
    min-height: 0;
    max-height: none;
  }

  body.reading-mode .app-footer {
    padding-top: 1.25rem;
  }
}

@media (max-width: 1099px) {
  body.reading-mode .app-main {
    max-width: 920px;
  }

  body.reading-mode .chat-panel {
    margin-top: 1.5rem;
  }
}

@media (max-width: 620px) {
  body.reading-mode {
    --card-width: clamp(56px, 16vw, 72px);
    --card-height: calc(var(--card-width) * 1.55);
  }

  body {
    background-attachment: scroll;
  }

  .app-header {
    padding: 1.15rem 0.75rem 0.65rem;
  }

  .app-header h1 {
    font-size: clamp(1.65rem, 9vw, 2.15rem);
    letter-spacing: 0.08em;
  }

  .app-header .tagline {
    max-width: 32rem;
    margin-inline: auto;
    font-size: 0.88rem;
    line-height: 1.55;
  }

  .app-main {
    padding-left: max(0.75rem, env(safe-area-inset-left));
    padding-right: max(0.75rem, env(safe-area-inset-right));
    padding-bottom: 2rem;
  }

  .intro-screen {
    min-height: min(calc(100svh - 10rem), 30rem);
    padding-block: 1.25rem;
  }

  .spread-layout {
    flex-direction: row;
    align-items: flex-start;
    flex-wrap: nowrap;
    gap: clamp(0.35rem, 2vw, 0.75rem);
    margin-bottom: 1.25rem;
  }

  .cross-area {
    gap: 2rem clamp(0.2rem, 1vw, 0.4rem);
    padding: 0.35rem;
  }

  .staff-area {
    grid-template-areas:
      "outcome hopes"
      "external self" !important;
    grid-template-columns: repeat(2, var(--card-width)) !important;
    grid-template-rows: repeat(2, var(--card-height)) !important;
    gap: 2rem clamp(0.2rem, 1vw, 0.4rem);
  }

  .spread-header {
    margin-bottom: 1rem;
  }

  .spread-header h2 {
    font-size: 1.35rem;
  }

  .spread-header p {
    font-size: 0.82rem;
  }

  .position-label {
    top: calc(100% + 0.2rem);
    font-size: clamp(0.62rem, 2.8vw, 0.7rem);
    max-width: calc(var(--card-width) + 1rem);
  }

  .intro-content {
    padding: clamp(1.35rem, 6vw, 1.8rem);
  }

  .reading-header {
    flex-direction: column;
    text-align: center;
  }

  .reading-panel {
    margin: 1.25rem auto;
    padding: 0;
  }

  .chat-panel {
    margin-bottom: 1.5rem;
  }

  .chat-card {
    display: grid;
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    border-radius: 14px;
  }

  .chat-title {
    padding: 0.85rem 0.9rem 0.15rem;
    font-size: 1rem;
  }

  .chat-subtitle {
    padding: 0 0.9rem 0.7rem;
  }

  .chat-messages {
    height: clamp(220px, 46dvh, 420px);
    min-height: 0;
    max-height: none;
    padding: 0.85rem;
    gap: 0.7rem;
  }

  .chat-message {
    max-width: 94%;
    padding: 0.65rem 0.8rem;
    font-size: 0.95rem;
  }

  .chat-input-row {
    gap: 0.5rem;
    padding: 0.7rem 0.75rem max(0.75rem, env(safe-area-inset-bottom));
    background: rgba(18, 11, 32, 0.92);
  }

  .chat-input {
    min-width: 0;
    padding: 0.7rem 1rem;
    font-size: 1rem;
  }

  .btn-chat-send {
    width: 48px;
    height: 48px;
  }

  .reading-card {
    padding: 1.2rem;
  }

  .reading-icon {
    width: 65px;
    height: 100px;
  }

  .card-pattern span {
    font-size: 1.3rem;
  }

  .moon {
    font-size: 3.5rem;
  }
}

@media (max-height: 560px) and (max-width: 1099px) {
  body.reading-mode {
    --card-width: clamp(56px, 12dvh, 72px);
    --card-height: calc(var(--card-width) * 1.55);
  }

  body.reading-mode .app-header {
    padding-block: 0.45rem;
  }

  body.reading-mode .app-header .tagline {
    display: none;
  }

  body.reading-mode .chat-messages {
    height: clamp(180px, 42dvh, 260px);
  }

  body.reading-mode .spread-header {
    margin-bottom: 0.6rem;
  }

  body.reading-mode .spread-layout {
    flex-direction: row;
    align-items: flex-start;
    flex-wrap: nowrap;
    gap: 0.75rem;
  }

  body.reading-mode .cross-area {
    gap: 2.1rem 0.3rem;
    padding: 0.35rem;
  }

  body.reading-mode .staff-area {
    grid-template-areas:
      "outcome hopes"
      "external self" !important;
    grid-template-columns: repeat(2, var(--card-width)) !important;
    grid-template-rows: repeat(2, var(--card-height)) !important;
    gap: 2.1rem 0.3rem;
  }
}
