/* ==========================================================================
 * Cadenza — design system
 * ฟอนต์ประกาศแยกไว้ที่ assets/css/fonts.css (self-hosted, โหลดก่อนไฟล์นี้)
 * ========================================================================== */

:root {
  /* ---- โทนสี (ดาร์กเป็นค่าเริ่มต้น) ---- */
  --bg:        #0a0e18;
  --bg-alt:    #0f1424;
  --panel:     #141b2e;
  --card:      #1a2238;
  --card-hi:   #212b46;
  --line:      #2b3350;
  --line-soft: #1f273e;
  --txt:       #eef1f8;
  --muted:     #93a0bf;
  --accent:      #cda24b;
  --accent-ink:  #1c1608;
  --accent-hover:#ddb264;
  --accent-soft: #cda24b26;
  --ok:        #35b39e;
  --ok-soft:   #35b39e22;
  --err:       #e2685c;
  --err-soft:  #e2685c22;

  --font-display: 'Playfair Display', Georgia, 'Times New Roman', serif;
  --font-body: 'Inter', 'Noto Sans Thai', 'Segoe UI', system-ui, sans-serif;

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --radius-full: 999px;

  --shadow-sm: 0 1px 2px #00000040;
  --shadow-md: 0 8px 24px -8px #00000066;
  --shadow-lg: 0 20px 48px -16px #000000aa;

  --ease: cubic-bezier(.2, .8, .2, 1);
  --dur-fast: 140ms;
  --dur: 240ms;
  --dur-slow: 420ms;

  color-scheme: dark;
}

/* โหมดสว่าง — ตาม OS (prefers-color-scheme) เมื่อผู้ใช้ยังไม่เลือกเอง (ไม่มี data-theme="dark") */
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg:        #faf7f0;
    --bg-alt:    #f4efe3;
    --panel:     #ffffff;
    --card:      #ffffff;
    --card-hi:   #fbf7ee;
    --line:      #e2dbc8;
    --line-soft: #ece5d5;
    --txt:       #211d15;
    --muted:     #6c6555;
    --accent:      #a87a1f;
    --accent-ink:  #fffaf0;
    --accent-hover:#8f6819;
    --accent-soft: #a87a1f1a;
    --ok:        #1f7d6c;
    --ok-soft:   #1f7d6c1a;
    --err:       #b23c30;
    --err-soft:  #b23c301a;
    color-scheme: light;
  }
}

/* โหมดสว่าง — ผู้ใช้เลือกเอง (ปุ่มสลับธีม) ชนะทุกกรณี รวมถึง OS ตั้งเป็นมืด */
:root[data-theme="light"] {
  --bg:        #faf7f0;
  --bg-alt:    #f4efe3;
  --panel:     #ffffff;
  --card:      #ffffff;
  --card-hi:   #fbf7ee;
  --line:      #e2dbc8;
  --line-soft: #ece5d5;
  --txt:       #211d15;
  --muted:     #6c6555;
  --accent:      #a87a1f;
  --accent-ink:  #fffaf0;
  --accent-hover:#8f6819;
  --accent-soft: #a87a1f1a;
  --ok:        #1f7d6c;
  --ok-soft:   #1f7d6c1a;
  --err:       #b23c30;
  --err-soft:  #b23c301a;
  color-scheme: light;
}

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

/* ---------- reset / base ---------- */
* { box-sizing: border-box; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
code { background: #00000026; padding: 1px 6px; border-radius: 5px; font-size: .92em; }

body {
  margin: 0; background: var(--bg); color: var(--txt);
  font-family: var(--font-body); line-height: 1.6; font-size: 15.5px;
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease);
}
h1, h2, h3, .brand-name { font-family: var(--font-display); letter-spacing: .01em; }
button, input, select { font-family: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

main { max-width: 1100px; margin: 0 auto; padding: 28px 20px 64px; }
@media (max-width: 480px) { main { padding: 18px 14px 56px; } }

/* ---------- แถบบน ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px calc(env(safe-area-inset-right, 0px) + 22px) 14px calc(env(safe-area-inset-left, 0px) + 22px);
  padding-top: calc(env(safe-area-inset-top, 0px) + 14px);
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
  gap: 12px;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { font-size: 22px; line-height: 1; }
.brand-name {
  font-size: 21px; font-weight: 700; color: var(--txt);
  background: linear-gradient(95deg, var(--txt), var(--accent) 130%);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.topbar-actions { display: flex; align-items: center; gap: 8px; position: relative; flex-wrap: wrap; justify-content: flex-end; }

@media (max-width: 480px) {
  .topbar { flex-wrap: wrap; row-gap: 10px; }
  .topbar-actions { width: 100%; justify-content: space-between; }
  .auth-area { gap: 8px; }
  .auth-user { max-width: 96px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
}

.icon-btn {
  width: 38px; height: 38px; display: inline-flex; align-items: center; justify-content: center;
  background: transparent; border: 1px solid var(--line); color: var(--txt);
  border-radius: var(--radius-full); cursor: pointer; font-size: 16px;
  transition: border-color var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease), background var(--dur-fast) var(--ease);
}
.icon-btn:hover { border-color: var(--accent); background: var(--accent-soft); }
.icon-btn:active { transform: scale(.92); }

.settings-panel {
  position: absolute; right: calc(env(safe-area-inset-right, 0px) + 18px); top: 64px; z-index: 25;
  display: flex; flex-direction: column; gap: 12px;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius-lg);
  padding: 18px; box-shadow: var(--shadow-lg); width: min(300px, calc(100vw - 32px));
  animation: popIn var(--dur) var(--ease) both;
}
.settings-panel label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); }
.settings-panel select {
  background: var(--card); color: var(--txt); border: 1px solid var(--line);
  border-radius: var(--radius-sm); padding: 8px 10px; font-size: 14.5px;
}
.settings-panel .chk { flex-direction: row; align-items: center; gap: 8px; font-size: 14px; color: var(--txt); }
@media (max-width: 480px) {
  .settings-panel { position: fixed; left: 12px; right: 12px; top: auto; bottom: 12px; width: auto; }
}

.chk { display: flex; align-items: center; gap: 6px; font-size: 14px; }
.link-btn {
  background: transparent; border: none; color: var(--muted);
  cursor: pointer; font-size: 13px; padding: 6px 8px; border-radius: var(--radius-sm); text-align: left;
  transition: color var(--dur-fast) var(--ease);
}
.link-btn:hover { color: var(--err); background: var(--err-soft); }

@keyframes popIn { from { opacity: 0; transform: translateY(-6px) scale(.98); } to { opacity: 1; transform: none; } }

/* ---------- บัญชีผู้ใช้ ---------- */
.auth-area { display: flex; align-items: center; gap: 10px; }
.auth-user { font-size: 14px; color: var(--txt); white-space: nowrap; }
.auth-modal {
  border: 1px solid var(--line); border-radius: var(--radius-lg); padding: 26px;
  background: var(--panel); color: var(--txt); width: min(380px, 90vw);
  box-shadow: var(--shadow-lg);
}
.auth-modal[open] { animation: popIn var(--dur) var(--ease) both; }
.auth-modal::backdrop { background: #05070ccc; backdrop-filter: blur(2px); }
.auth-tabs { display: flex; gap: 8px; margin-bottom: 18px; background: var(--bg-alt); padding: 4px; border-radius: var(--radius-full); }
.auth-tab {
  flex: 1; background: transparent; border: none; color: var(--muted);
  border-radius: var(--radius-full); padding: 9px; cursor: pointer; font-size: 14px;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.auth-tab.active { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.auth-form { display: flex; flex-direction: column; gap: 11px; }
.auth-form input, .auth-form select {
  padding: 10px 13px; border-radius: var(--radius-sm); border: 1px solid var(--line);
  background: var(--card); color: var(--txt); font-size: 14.5px;
  transition: border-color var(--dur-fast) var(--ease);
}
.auth-form input:focus, .auth-form select:focus { outline: none; border-color: var(--accent); }
.auth-err { color: var(--err); font-size: 13px; min-height: 16px; }
#authClose { display: block; margin: 16px auto 0; }

/* ---------- นำทางหลัก ---------- */
.main-nav { display: flex; gap: 8px; margin-bottom: 22px; border-bottom: 1px solid var(--line); }
.nav-tab {
  background: transparent; border: none; border-bottom: 2px solid transparent;
  color: var(--muted); padding: 10px 6px; margin-bottom: -1px; cursor: pointer;
  font-size: 15px; display: flex; align-items: center; gap: 6px;
  transition: color var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease);
}
.nav-tab:hover { color: var(--txt); }
.nav-tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.due-badge {
  background: var(--accent); color: var(--accent-ink); font-size: 11px; font-weight: 700;
  border-radius: var(--radius-full); padding: 1px 7px; line-height: 1.5;
}

/* ---------- หน้านำเข้าบทเรียน ---------- */
.setup { text-align: center; padding: 40px 0 20px; animation: fadeUp var(--dur-slow) var(--ease) both; }
.lead { font-family: var(--font-display); font-size: 22px; font-weight: 600; max-width: 46em; margin: 0 auto; }
.lead .muted { font-family: var(--font-body); font-size: 14px; display: inline-block; margin-top: 8px; }

.presets { margin: 2rem 0 1.5rem; }
.preset-label { font-size: .82rem; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); margin-bottom: .7rem; }
.preset-grid { display: flex; gap: 1rem; flex-wrap: wrap; justify-content: center; }
.preset-card {
  display: flex; flex-direction: column; align-items: center; gap: .35rem;
  padding: 1.1rem 1.6rem; border: 1px solid var(--line); border-radius: var(--radius-lg);
  background: var(--card); color: var(--txt); cursor: pointer;
  transition: border-color var(--dur) var(--ease), transform var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
  min-width: 220px; max-width: 280px; flex: 1; text-align: center;
}
.preset-card:hover { border-color: var(--accent); transform: translateY(-3px); box-shadow: var(--shadow-md); }
.preset-card.selected { border-color: var(--accent); background: var(--accent-soft); box-shadow: var(--shadow-md); }
.preset-icon { font-size: 2rem; }
.preset-title { font-size: .88rem; font-weight: 600; }
.preset-tag {
  font-size: .68rem; color: var(--muted); background: var(--bg-alt);
  padding: 2px 10px; border-radius: var(--radius-full); border: 1px solid var(--line-soft);
}

.custom-import { margin-top: 1.2rem; color: var(--muted); }
.custom-import summary { cursor: pointer; font-size: 14px; }
.custom-import summary:hover { color: var(--accent); }
.custom-import .drops { margin-top: 1rem; }

.drops { display: flex; gap: 18px; justify-content: center; margin: 24px 0; flex-wrap: wrap; }
.drop {
  display: inline-flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  min-width: 220px; padding: 26px; border: 2px dashed var(--line);
  border-radius: var(--radius-lg); cursor: pointer; background: var(--panel); font-size: 16px;
  transition: border-color var(--dur) var(--ease), background var(--dur) var(--ease);
}
.drop:hover { border-color: var(--accent); }
.drop .drop-text { font-weight: 600; }
.drop .drop-sub { font-size: 13px; color: var(--muted); max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.drop.done { border-style: solid; border-color: var(--ok); background: var(--ok-soft); }
.drop.done .drop-text { color: var(--ok); }

/* ---------- ปุ่ม ---------- */
.btn {
  background: var(--card); color: var(--txt); border: 1px solid var(--line);
  border-radius: var(--radius-sm); padding: 10px 18px; cursor: pointer; font-size: 15px;
  transition: border-color var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease), background var(--dur-fast) var(--ease);
}
.btn:hover:not(:disabled) { border-color: var(--accent); box-shadow: var(--shadow-sm); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.ghost { background: transparent; }

/* ---------- หน้าเรียน ---------- */
.lesson { animation: fadeUp var(--dur-slow) var(--ease) both; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 26px; align-items: start; }
@media (max-width: 860px) { .grid { grid-template-columns: 1fr; } }

.left { position: sticky; top: 84px; }
@media (max-width: 860px) { .left { position: static; } }

.left video {
  width: 100%; border-radius: var(--radius-lg); background: #000; aspect-ratio: 16/9;
  box-shadow: var(--shadow-md); border: 1px solid var(--line);
}
.player-btns { margin-top: 14px; display: flex; flex-wrap: wrap; align-items: center; gap: 12px; }
.speeds { display: inline-flex; align-items: center; gap: 6px; }
.speed-label { font-size: 13px; color: var(--muted); margin-right: 2px; }
.speed {
  background: var(--card); color: var(--muted); border: 1px solid var(--line);
  border-radius: var(--radius-full); padding: 6px 12px; cursor: pointer; font-size: 13px; line-height: 1;
  transition: all var(--dur-fast) var(--ease);
}
.speed:hover { border-color: var(--accent); color: var(--txt); }
.speed.active { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }

.progress { color: var(--muted); font-size: 13.5px; letter-spacing: .02em; }
.cue-text {
  font-family: var(--font-display); font-size: 27px; font-weight: 600; margin: 8px 0 18px; padding: 18px 20px;
  background: var(--panel); border-radius: var(--radius-lg); border-left: 4px solid var(--accent);
  box-shadow: var(--shadow-sm);
}
.nav { display: flex; gap: 10px; align-items: center; margin-bottom: 12px; flex-wrap: wrap; }
.nav .primary { flex: 1; }

/* ---------- โน้ตส่วนตัว ---------- */
.note-box {
  margin-top: 14px; background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius-md); padding: 12px 16px;
}
.note-box summary { cursor: pointer; font-weight: 600; font-size: 14px; }
.note-box summary:hover { color: var(--accent); }
.note-textarea { min-height: 70px; resize: vertical; margin-top: 10px; font-family: inherit; }
.note-status { margin-left: 10px; font-size: 13px; }

/* ---------- แผงคำสอน / การ์ด ---------- */
.panel { margin-top: 18px; }
.card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius-lg); padding: 18px 20px; margin-bottom: 16px;
  box-shadow: var(--shadow-sm);
}
.card pre { white-space: pre-wrap; word-break: break-word; margin: 0; font-family: var(--font-body); }
.cached-tag { font-size: 12px; color: var(--ok); margin-bottom: 10px; }
.error-card { border-color: var(--err); }
.err-msg { color: var(--err); margin-bottom: 10px; }
.err-retry { margin-left: 6px; }
.tts-row { display: flex; gap: 8px; margin-bottom: 14px; flex-wrap: wrap; }
.btn.tts { padding: 7px 14px; font-size: 13px; border-radius: var(--radius-full); }
.pron-result { margin: -6px 0 14px; font-size: 14px; }
.pron-score { font-weight: 700; margin-bottom: 3px; }
.pron-score.good { color: var(--ok); }
.pron-score.ok { color: var(--accent); }
.pron-score.bad { color: var(--err); }
.row { display: flex; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line-soft); }
.row:last-child { border-bottom: 0; }
.row .ic { font-size: 18px; }
.row b { color: var(--accent); }
.row p { margin: 4px 0 0; }
.row ul { margin: 6px 0 0; padding-left: 18px; }
.reading {
  font-size: 18px; font-weight: 600; color: var(--accent);
  background: var(--accent-soft); border: 1px solid color-mix(in srgb, var(--accent) 40%, transparent);
  border-radius: var(--radius-md); padding: 11px 15px; margin-bottom: 14px; line-height: 1.7;
}
.reading span { letter-spacing: .3px; }
.ipa-line { font-size: 13.5px; margin-top: 4px; font-family: 'Inter', system-ui, sans-serif; }
.ipa-word { font-size: 12.5px; }
.vocab-list li { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 6px; padding: 4px 0; }
.wb-add-btn {
  background: transparent; border: 1px solid var(--line); border-radius: var(--radius-full);
  width: 24px; height: 24px; cursor: pointer; font-size: 12px; line-height: 1; color: var(--txt);
  transition: border-color var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
  margin-left: auto;
}
.wb-add-btn:hover:not(:disabled) { border-color: var(--accent); transform: scale(1.1); }
.wb-add-btn:disabled { opacity: .7; cursor: default; }

/* ---------- แดชบอร์ด ---------- */
.dash h2 { font-family: var(--font-display); font-size: 22px; margin: 0 0 20px; }
.dash-tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 14px; margin-bottom: 22px; }
.dash-tile {
  display: flex; align-items: center; gap: 12px; background: var(--card);
  border: 1px solid var(--line); border-radius: var(--radius-lg); padding: 16px 18px;
}
.dash-tile-icon { font-size: 24px; }
.dash-tile-value { font-size: 18px; font-weight: 700; }
.dash-tile-label { font-size: 12.5px; margin-top: 2px; }
.dash-chart-card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius-lg); padding: 18px 20px; }
.dash-chart-head { display: flex; justify-content: space-between; align-items: center; font-weight: 600; margin-bottom: 10px; font-size: 14px; }
.dash-svg { width: 100%; height: auto; display: block; }
.dash-axis-label { fill: var(--muted); font-size: 10px; font-family: var(--font-body); }
.dash-svg rect { transition: opacity var(--dur-fast) var(--ease); }
.dash-svg rect:hover { opacity: 1 !important; }

/* ---------- คลังคำศัพท์ ---------- */
.wb-header { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 12px; margin-bottom: 20px; }
.wb-header h2 { font-family: var(--font-display); font-size: 22px; margin: 0; }

.wb-review { text-align: center; max-width: 420px; margin: 0 auto 32px; }
.flip-card { perspective: 1200px; height: 220px; cursor: pointer; }
.flip-inner {
  position: relative; width: 100%; height: 100%; transition: transform .5s var(--ease);
  transform-style: preserve-3d;
}
.flip-card.flipped .flip-inner { transform: rotateY(180deg); }
.flip-face {
  position: absolute; inset: 0; backface-visibility: hidden;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius-lg);
  padding: 20px; box-shadow: var(--shadow-md);
}
.flip-back { transform: rotateY(180deg); }
.flip-word { font-family: var(--font-display); font-size: 30px; font-weight: 700; }
.flip-meaning { font-size: 22px; font-weight: 600; color: var(--accent); }
.flip-ipa { font-size: 15px; }
.flip-example { font-size: 14px; max-width: 90%; }
.wb-progress { margin-top: 14px; font-size: 13.5px; }
.wb-grades { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 18px; }
.wb-grades .grade { padding: 10px 6px; font-size: 13px; }
.wb-grades .grade.again:hover { border-color: var(--err); }
.wb-grades .grade.hard:hover { border-color: var(--accent); }
.wb-grades .grade.good:hover, .wb-grades .grade.easy:hover { border-color: var(--ok); }

.wb-list { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.wb-row {
  display: flex; align-items: center; gap: 12px; background: var(--card);
  border: 1px solid var(--line); border-radius: var(--radius-md); padding: 12px 14px;
}
.wb-row .wb-word { font-weight: 700; min-width: 100px; }
.wb-row .wb-meaning { flex: 1; color: var(--muted); font-size: 14px; }
.wb-row .wb-due { font-size: 12px; color: var(--muted); white-space: nowrap; }
.wb-row .wb-due.due-now { color: var(--accent); font-weight: 600; }
.wb-row .wb-del { background: transparent; border: none; color: var(--muted); cursor: pointer; font-size: 16px; }
.wb-row .wb-del:hover { color: var(--err); }
.wb-empty { color: var(--muted); text-align: center; padding: 30px 0; }
@media (max-width: 480px) {
  .wb-row { flex-wrap: wrap; }
  .wb-row .wb-meaning { width: 100%; order: 3; }
}

/* ---------- แบบฝึกหัด ---------- */
.ex-type { color: var(--accent); font-size: 14px; font-weight: 600; }
.ex-how {
  font-size: 13px; color: var(--muted); margin: 8px 0 12px;
  background: var(--bg-alt); border-left: 3px solid var(--accent);
  padding: 9px 13px; border-radius: 0 var(--radius-sm) var(--radius-sm) 0; line-height: 1.5;
}
.ex-hint { font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.ex-q { font-family: var(--font-display); font-size: 19px; font-weight: 600; margin: 8px 0 16px; }
.choices { display: flex; flex-direction: column; gap: 9px; }
.choice {
  text-align: left; background: var(--panel); border: 1px solid var(--line); color: var(--txt);
  padding: 11px 15px; border-radius: var(--radius-sm); cursor: pointer;
  transition: border-color var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
}
.choice:hover:not(:disabled) { border-color: var(--accent); transform: translateX(2px); }
.choice.right { border-color: var(--ok); background: var(--ok-soft); }
.choice.wrong { border-color: var(--err); background: var(--err-soft); }
.ex-input {
  width: 100%; padding: 11px; border-radius: var(--radius-sm); border: 1px solid var(--line);
  background: var(--panel); color: var(--txt); margin-bottom: 12px;
}
.ex-result { margin-top: 14px; animation: fadeUp var(--dur) var(--ease) both; }

.status { text-align: center; margin: 20px 0; color: var(--muted); min-height: 22px; font-size: 14px; }
.status.err { color: var(--err); }

/* ---------- แชตถามครู ---------- */
.chat {
  margin-top: 20px; background: var(--panel);
  border: 1px solid var(--line); border-radius: var(--radius-lg); overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.chat-head { padding: 12px 16px; border-bottom: 1px solid var(--line); font-weight: 600; }
.chat-head .muted { font-weight: 400; font-size: 12px; }
.chat-log { max-height: 320px; overflow-y: auto; padding: 14px 16px; display: flex; flex-direction: column; gap: 9px; }
.chat-log:empty::before { content: "ถามอะไรก็ได้เกี่ยวกับประโยคที่เรียน 😊"; color: var(--muted); font-size: 14px; }
.msg { display: flex; }
.msg.user { justify-content: flex-end; }
.msg .bubble {
  max-width: 80%; padding: 10px 14px; border-radius: 16px; line-height: 1.55;
  white-space: pre-wrap; word-break: break-word; animation: fadeUp var(--dur-fast) var(--ease) both;
}
.msg.user .bubble { background: var(--accent); color: var(--accent-ink); border-bottom-right-radius: 4px; }
.msg.ai .bubble { background: var(--card); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.msg.typing .dot {
  display: inline-block; width: 6px; height: 6px; margin: 0 2px; border-radius: 50%;
  background: var(--muted); animation: pulse 1s infinite;
}
.msg.typing .dot:nth-child(2) { animation-delay: .2s; }
.msg.typing .dot:nth-child(3) { animation-delay: .4s; }
.chat-input { display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line); }
.chat-input input {
  flex: 1; padding: 10px 13px; border-radius: var(--radius-sm);
  border: 1px solid var(--line); background: var(--card); color: var(--txt);
}
.chat-input input:focus { outline: none; border-color: var(--accent); }

/* ---------- Loading ---------- */
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes fadeUp { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes pulse { 0%,100% { opacity: .4; } 50% { opacity: .85; } }
@keyframes shimmer { 0% { background-position: -400px 0; } 100% { background-position: 400px 0; } }

.card { animation: fadeUp .3s var(--ease) both; }

.loading-box {
  display: flex; align-items: center; gap: 12px;
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius-lg); padding: 18px; color: var(--muted);
}
.spinner {
  width: 22px; height: 22px; flex: none; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
.skeleton-lines { flex: 1; }
.skeleton-lines span {
  display: block; height: 12px; border-radius: 6px; margin: 7px 0;
  background: linear-gradient(90deg, var(--line) 25%, var(--card-hi) 37%, var(--line) 63%);
  background-size: 800px 100%; animation: shimmer 1.4s infinite linear;
}
.skeleton-lines span:nth-child(1) { width: 90%; }
.skeleton-lines span:nth-child(2) { width: 70%; }
.skeleton-lines span:nth-child(3) { width: 80%; }

.btn.busy { position: relative; color: transparent !important; pointer-events: none; }
.btn.busy::after {
  content: ""; position: absolute; inset: 0; margin: auto;
  width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid color-mix(in srgb, var(--accent-ink) 40%, transparent); border-top-color: var(--accent-ink);
  animation: spin .7s linear infinite;
}
.btn.ghost.busy::after, .btn:not(.primary).busy::after {
  border-color: var(--line); border-top-color: var(--accent);
}
