:root {
  --bg: #eaf2fb;
  --bg2: #d7e7f8;
  --card: #ffffff;
  --ink: #14213d;
  --muted: #5b6782;
  --brand: #1f4fa3;
  --brand-2: #14306b;
  --accent: #f2a541;
  --ok: #1fa35b;
  --danger: #c0392b;
  --line: #06c755;
  --radius: 22px;
  --shadow: 0 6px 24px rgba(20, 33, 61, .10);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  background: radial-gradient(1200px 600px at 20% 10%, #fff 0, var(--bg) 45%, var(--bg2) 100%);
  color: var(--ink);
  font-family: Sarabun, system-ui, sans-serif;
  overflow: hidden;
  user-select: none;
}
button { font-family: inherit; cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .9rem; }

#app {
  height: 100%;
  display: grid;
  grid-template-columns: 1.25fr 1fr;
  grid-template-rows: auto 1fr;
  grid-template-areas: "top top" "stage side";
  gap: 0 24px;
  padding: 16px 24px 24px;
}
.topbar { grid-area: top; display: flex; justify-content: space-between; align-items: center; padding-bottom: 8px; }
.brand { font-weight: 800; font-size: 1.3rem; color: var(--brand-2); display: flex; gap: 8px; align-items: center; }
.brand .dam { font-weight: 600; color: var(--muted); font-size: 1.05rem; }
.top-actions { display: flex; gap: 12px; align-items: center; }
.chip { border: 2px solid var(--brand); background: #fff; color: var(--brand); border-radius: 999px; padding: 4px 14px; font-weight: 700; }
.dot { width: 12px; height: 12px; border-radius: 50%; background: var(--ok); display: inline-block; }
.dot.off { background: var(--danger); }

.stage { grid-area: stage; position: relative; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; min-height: 0; }
.avatar-box { flex: 1; width: 100%; min-height: 0; display: flex; align-items: flex-end; justify-content: center; }
.avatar-box svg, .avatar-box canvas { height: 100%; max-height: 78vh; width: auto; max-width: 100%; }
.subtitle {
  min-height: 3.6em; width: 100%; max-width: 680px; margin-top: 8px;
  background: rgba(255,255,255,.92); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px 20px; font-size: 1.35rem; font-weight: 600; line-height: 1.45; text-align: center;
  transition: opacity .2s;
}
.subtitle:empty { opacity: 0; }

.side { grid-area: side; display: flex; align-items: center; min-height: 0; }
.panel { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 24px; width: 100%; }
.idle-panel { text-align: center; }
.idle-panel h2 { margin: 0 0 4px; font-size: 1.6rem; color: var(--brand-2); }
.qr { width: min(300px, 60vw); aspect-ratio: 1; margin: 16px auto 8px; background: #fff; border-radius: 16px; padding: 8px; border: 3px solid var(--line); }
.qr svg { width: 100%; height: 100%; display: block; }

.btn { border: 0; border-radius: 14px; padding: 14px 20px; font-weight: 700; font-size: 1.05rem; width: 100%; margin-top: 14px; }
.btn.primary { background: var(--brand); color: #fff; }
.btn.ghost { background: #eef3fb; color: var(--brand-2); }
.small-btn { padding: 10px; font-size: .95rem; }

.chat-panel { display: flex; flex-direction: column; gap: 14px; }
.who { display: flex; justify-content: space-between; align-items: center; font-weight: 800; font-size: 1.25rem; color: var(--brand-2); }
.badge { background: #fff3e0; color: #a35b00; border-radius: 999px; padding: 4px 12px; font-size: .95rem; font-weight: 700; }
.user-said { min-height: 1.6em; color: var(--muted); font-style: italic; font-size: 1.05rem; }
.user-said:not(:empty)::before { content: "คุณ: "; font-style: normal; font-weight: 700; }
.quick { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.quick button { border: 2px solid #dbe6f5; background: #f7faff; border-radius: 14px; padding: 14px 10px; font-size: 1.05rem; font-weight: 600; color: var(--ink); }
.quick button:active { transform: scale(.97); }

.mic {
  position: relative; border: 0; border-radius: 999px; padding: 22px; font-size: 1.3rem; font-weight: 800;
  background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff;
  display: flex; align-items: center; justify-content: center; gap: 12px; overflow: hidden;
  box-shadow: 0 8px 20px rgba(31,79,163,.35);
}
.mic .mic-icon { display: inline-flex; }
.mic.recording { background: linear-gradient(135deg, #e74c3c, #a93226); animation: pulse 1.2s infinite; }
.mic.busy { background: #8a97b1; box-shadow: none; }
.mic .level { position: absolute; left: 0; bottom: 0; height: 6px; width: 0; background: #fff8; transition: width .08s; }
@keyframes pulse { 0%,100% { box-shadow: 0 0 0 0 rgba(231,76,60,.5); } 50% { box-shadow: 0 0 0 16px rgba(231,76,60,0); } }

.overlay { position: fixed; inset: 0; z-index: 50; background: rgba(234,242,251,.96); display: flex; align-items: center; justify-content: center; padding: 16px; }
.overlay .panel { max-width: 420px; }
.overlay form label { display: block; margin: 12px 0; font-weight: 600; }
.overlay input { width: 100%; padding: 12px; border-radius: 10px; border: 2px solid #dbe6f5; font-size: 1rem; margin-top: 4px; }
.hint { color: var(--muted); font-size: .85rem; }
.boot-btn { border: 0; display: flex; flex-direction: column; align-items: center; gap: 10px; font-size: 1.3rem; padding: 40px; }
.boot-btn .big { font-size: 4rem; }

/* ---------- ฉากประทับตรา ---------- */
.stamp-scene { position: fixed; inset: 0; z-index: 40; display: flex; align-items: center; justify-content: center; background: rgba(20,33,61,.35); }
.passport-book { position: relative; display: flex; width: min(640px, 92vw); aspect-ratio: 1.6; perspective: 1400px; animation: bookIn .6s cubic-bezier(.2,1.4,.4,1) both; }
.passport-cover, .passport-page { flex: 1; border-radius: 14px; }
.passport-cover {
  background: linear-gradient(160deg, #1f4fa3, #10285a); color: #f3d27a; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 10px; box-shadow: var(--shadow); border-right: 4px solid #0c1f47;
}
.dam-emblem { width: 55%; }
.cover-title { text-align: center; font-weight: 800; letter-spacing: .12em; font-size: 1.4rem; line-height: 1.3; }
.cover-title small { letter-spacing: 0; font-weight: 600; font-size: .85rem; }
.passport-page { background: repeating-linear-gradient(0deg, #fbf7ee 0 26px, #f1ead9 26px 27px); position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; box-shadow: var(--shadow); }
.stamp-mark {
  width: 62%; aspect-ratio: 1; border: 6px double var(--danger); border-radius: 50%; color: var(--danger);
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
  transform: rotate(-12deg) scale(2.2); opacity: 0; font-weight: 800; padding: 10px; background: rgba(255,240,235,.4);
}
.stamp-mark b { font-size: 1.15rem; letter-spacing: .06em; }
.stamp-mark span { font-size: .85rem; font-weight: 600; }
.stamp-scene.go .stamp-mark { animation: stampIn .25s .9s ease-in forwards; }
.stamp-total { margin-top: 14px; font-weight: 800; color: var(--brand-2); opacity: 0; font-size: 1.1rem; }
.stamp-scene.go .stamp-total { animation: fadeIn .4s 1.3s forwards; }
.rubber {
  position: absolute; right: 18%; top: -30%; width: 90px; height: 120px;
  background: linear-gradient(#8b5a2b 0 60%, #333 60% 70%, var(--danger) 70%); border-radius: 18px 18px 6px 6px;
  opacity: 0;
}
.stamp-scene.go .rubber { animation: rubber 1.3s .2s ease-in-out forwards; }
@keyframes bookIn { from { transform: translateY(40px) scale(.7); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes stampIn { to { transform: rotate(-12deg) scale(1); opacity: 1; } }
@keyframes fadeIn { to { opacity: 1; } }
@keyframes rubber {
  0% { opacity: 0; transform: translateY(-40px); }
  30% { opacity: 1; transform: translateY(0); }
  55% { transform: translate(-60%, 105%); }
  65% { transform: translate(-60%, 92%); }
  100% { opacity: 0; transform: translate(-60%, -60%); }
}

/* ---------- แนวตั้ง / จอเล็ก ---------- */
@media (orientation: portrait), (max-width: 820px) {
  #app { grid-template-columns: 1fr; grid-template-rows: auto minmax(0, 1fr) auto; grid-template-areas: "top" "stage" "side"; padding: 12px 16px 16px; }
  .avatar-box svg, .avatar-box canvas { max-height: 46vh; }
  .subtitle { font-size: 1.15rem; }
  .qr { width: min(220px, 50vw); }
  .panel { padding: 16px; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

.ic { width: 1.25em; height: 1.25em; vertical-align: -.25em; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.quick button { display: flex; align-items: center; justify-content: center; gap: 8px; color: var(--brand-2); }
.mic .mic-icon .ic { width: 1.6rem; height: 1.6rem; }
