/* ==========================================================================
   AZ Worksheet Studio — Design System
   Phase 1: mobile-safe layout, 44px touch targets, real Thai fonts
   Phase 2: token-based light theme + dark theme, component library
   Phase 3: layout / UX states (skeleton, empty, drawer, bottom bar)
   Phase 4: polish, charts, print fidelity, accessibility
   ========================================================================== */

/* --------------------------------------------------------------------------
   1. TOKENS
   -------------------------------------------------------------------------- */
:root {
  /* Type */
  --font-ui: 'DM Sans', 'Noto Sans Thai', system-ui, -apple-system, sans-serif;
  --font-display: 'Space Grotesk', 'Noto Sans Thai', system-ui, sans-serif;
  --font-paper: 'Sarabun', 'Noto Sans Thai', sans-serif;

  /* Spacing scale (4px base) */
  --s1: 4px;  --s2: 8px;  --s3: 12px; --s4: 16px;
  --s5: 20px; --s6: 24px; --s8: 32px; --s10: 40px;
  --s12: 48px; --s16: 64px; --s20: 80px;

  /* Radii */
  --r-sm: 8px; --r-md: 12px; --r-lg: 16px; --r-xl: 22px; --r-full: 999px;
  --radius: 16px; /* legacy alias */

  /* Motion */
  --ease: cubic-bezier(.22, .61, .36, 1);
  --dur: .22s;

  /* Touch target floor */
  --tap: 44px;

  /* Layout */
  --nav-h: 68px;
  --bottombar-h: 64px;
}

/* ---- Light theme (default) ---- */
:root,
:root[data-theme="light"] {
  color-scheme: light;

  --bg: #F5F7FB;
  --bg-tint: radial-gradient(1100px 520px at 78% -12%, #DDF6F3 0%, transparent 62%),
             radial-gradient(900px 460px at 8% -6%, #FFE9D8 0%, transparent 60%);
  --surface: #FFFFFF;
  --surface-2: #F2F5F9;
  --surface-3: #E6EBF2;

  --fg: #101828;
  --fg-muted: #5A6478;
  --fg-subtle: #8A93A5;

  --border: #E3E8EF;
  --border-strong: #CBD3E1;

  --primary: #0F766E;
  --primary-hover: #115E59;
  --primary-soft: #D5F4F0;
  --on-primary: #FFFFFF;

  --accent: #C2410C;
  --accent-hover: #9A3412;
  --accent-soft: #FFEBDD;

  --gold: #B45309;
  --gold-soft: #FEF3C7;
  --gold-line: #E9B949;

  --blue: #1D4ED8;
  --blue-soft: #DEE9FE;

  --danger: #B42318;
  --danger-soft: #FEE4E2;
  --success: #067647;
  --success-soft: #D3F8E4;
  --warn: #B54708;
  --warn-soft: #FEF0C7;

  --ring: rgba(15, 118, 110, .38);
  --grad-brand: linear-gradient(135deg, #0F766E 0%, #0E7490 46%, #C2410C 100%);
  --grad-brand-text: linear-gradient(105deg, #0F766E 0%, #0E7490 42%, #C2410C 100%);

  --sh-xs: 0 1px 2px rgba(16, 24, 40, .05);
  --sh-sm: 0 1px 3px rgba(16, 24, 40, .08), 0 1px 2px rgba(16, 24, 40, .04);
  --sh-md: 0 6px 16px -4px rgba(16, 24, 40, .10), 0 2px 6px -2px rgba(16, 24, 40, .05);
  --sh-lg: 0 18px 36px -12px rgba(16, 24, 40, .18), 0 6px 14px -6px rgba(16, 24, 40, .07);
  --sh-xl: 0 28px 60px -18px rgba(16, 24, 40, .26);
  --sh-paper: 0 22px 50px -16px rgba(16, 24, 40, .28);
}

/* ---- Dark theme ---- */
:root[data-theme="dark"] {
  color-scheme: dark;

  --bg: #0A1020;
  --bg-tint: radial-gradient(1100px 520px at 78% -12%, rgba(13, 148, 136, .18) 0%, transparent 62%),
             radial-gradient(900px 460px at 8% -6%, rgba(234, 88, 12, .14) 0%, transparent 60%);
  --surface: #101A2E;
  --surface-2: #17243C;
  --surface-3: #22314E;

  --fg: #EEF3FA;
  --fg-muted: #9BA9C2;
  --fg-subtle: #77869F;

  --border: #22314E;
  --border-strong: #33456A;

  --primary: #2DD4BF;
  --primary-hover: #5EEAD4;
  --primary-soft: rgba(45, 212, 191, .14);
  --on-primary: #04241F;

  --accent: #FB923C;
  --accent-hover: #FDBA74;
  --accent-soft: rgba(251, 146, 60, .14);

  --gold: #FBBF24;
  --gold-soft: rgba(251, 191, 36, .13);
  --gold-line: #B7862B;

  --blue: #60A5FA;
  --blue-soft: rgba(96, 165, 250, .14);

  --danger: #F87171;
  --danger-soft: rgba(248, 113, 113, .14);
  --success: #34D399;
  --success-soft: rgba(52, 211, 153, .14);
  --warn: #FBBF24;
  --warn-soft: rgba(251, 191, 36, .14);

  --ring: rgba(45, 212, 191, .45);
  --grad-brand: linear-gradient(135deg, #14B8A6 0%, #22D3EE 46%, #FB923C 100%);
  --grad-brand-text: linear-gradient(105deg, #2DD4BF 0%, #38BDF8 42%, #FDBA74 100%);

  --sh-xs: 0 1px 2px rgba(0, 0, 0, .4);
  --sh-sm: 0 1px 3px rgba(0, 0, 0, .5);
  --sh-md: 0 8px 20px -6px rgba(0, 0, 0, .55);
  --sh-lg: 0 20px 40px -14px rgba(0, 0, 0, .65);
  --sh-xl: 0 30px 64px -20px rgba(0, 0, 0, .72);
  --sh-paper: 0 22px 50px -16px rgba(0, 0, 0, .7);
}

/* --------------------------------------------------------------------------
   2. RESET & BASE
   -------------------------------------------------------------------------- */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
  }
}

body {
  min-height: 100vh;
  background-color: var(--bg);
  background-image: var(--bg-tint);
  background-attachment: fixed;
  color: var(--fg);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
  overflow-wrap: break-word;
  transition: background-color var(--dur) var(--ease), color var(--dur) var(--ease);
}

/* No horizontal escape on any screen */
img, svg, video, canvas { max-width: 100%; height: auto; display: block; }

h1, h2, h3, h4, h5, h6 {
  font-family: var(--font-display);
  line-height: 1.25;
  font-weight: 700;
  letter-spacing: -.01em;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

button, input, select, textarea { font: inherit; color: inherit; }

/* Visible keyboard focus everywhere (a11y) */
:focus-visible {
  outline: 3px solid var(--ring);
  outline-offset: 2px;
  border-radius: 6px;
}

::selection { background: var(--primary); color: var(--on-primary); }

/* Skip link */
.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: 3000;
  background: var(--primary);
  color: var(--on-primary);
  padding: 12px 20px;
  border-radius: 0 0 var(--r-md) 0;
  font-weight: 700;
}
.skip-link:focus { left: 0; text-decoration: none; }

.sr-only {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* --------------------------------------------------------------------------
   3. UTILITIES
   -------------------------------------------------------------------------- */
.color-gold { color: var(--gold); }
.color-blue { color: var(--blue); }
.color-red  { color: var(--danger); }
.color-primary { color: var(--primary); }

.gradient-text {
  background: var(--grad-brand-text);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  color: transparent;
}

.glass {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
}

.container { width: 100%; max-width: 1360px; margin-inline: auto; padding-inline: var(--s5); }

.hide-desktop, .hide-desktop.btn-hamburger { display: none; }

/* --------------------------------------------------------------------------
   4. NAVBAR  (Phase 1: no more 892px overflow on 375px screens)
   -------------------------------------------------------------------------- */
.navbar {
  position: sticky;
  top: 0;
  z-index: 500;
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: var(--s4);
  min-height: var(--nav-h);
  padding: var(--s3) var(--s5);
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(14px) saturate(150%);
  -webkit-backdrop-filter: blur(14px) saturate(150%);
  border-bottom: 1px solid var(--border);
  overflow: hidden;
}

.logo {
  display: flex;
  align-items: center;
  gap: var(--s3);
  min-width: 0;
  flex: 0 1 auto;
}

.logo-icon {
  display: grid;
  place-items: center;
  width: 40px; height: 40px;
  flex: 0 0 40px;
  border-radius: 12px;
  background: var(--grad-brand);
  color: #fff;
  font-size: 1.05rem;
  box-shadow: var(--sh-sm);
}

.logo-text { display: flex; flex-direction: column; min-width: 0; line-height: 1.2; }

.brand-name {
  font-family: var(--font-display);
  font-size: 1.06rem;
  font-weight: 700;
  letter-spacing: -.02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.badge-role {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  align-self: flex-start;
  max-width: 100%;
  padding: 2px 9px;
  border-radius: var(--r-full);
  font-size: .66rem;
  font-weight: 700;
  letter-spacing: .04em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.badge-role.gold { background: var(--gold-soft); color: var(--gold); border: 1px solid color-mix(in srgb, var(--gold) 35%, transparent); }
.badge-role.blue { background: var(--blue-soft); color: var(--blue); border: 1px solid color-mix(in srgb, var(--blue) 35%, transparent); }

.nav-spacer { flex: 1 1 auto; }

/* Desktop nav cluster */
.nav-actions {
  display: flex;
  align-items: center;
  gap: var(--s2);
  flex: 0 0 auto;
}

.user-role-bar {
  display: flex;
  align-items: center;
  gap: var(--s3);
  padding-right: var(--s3);
  margin-right: var(--s1);
  border-right: 1px solid var(--border);
  flex: 0 1 auto;
  min-width: 0;
  overflow: hidden;
}

.user-profile { display: flex; align-items: center; gap: var(--s2); min-width: 0; }
.user-profile > i {
  display: grid; place-items: center;
  width: 34px; height: 34px; flex: 0 0 34px;
  border-radius: var(--r-full);
  background: var(--surface-2);
  color: var(--fg-muted);
  font-size: .85rem;
}
.user-info { display: flex; flex-direction: column; line-height: 1.25; min-width: 0; }
.user-title { font-weight: 700; font-size: .82rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 22ch; }
.user-plan { font-size: .72rem; color: var(--fg-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 24ch; }

/* Hamburger + mobile-only nav bits */
.btn-hamburger, .btn-theme {
  display: inline-grid;
  place-items: center;
  width: var(--tap); height: var(--tap);
  flex: 0 0 var(--tap);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--surface);
  color: var(--fg);
  cursor: pointer;
  font-size: 1.02rem;
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease), transform var(--dur) var(--ease);
}
.btn-hamburger:hover, .btn-theme:hover { background: var(--surface-2); border-color: var(--border-strong); }
.btn-hamburger:active, .btn-theme:active { transform: scale(.94); }
.btn-theme .icon-dark { display: none; }
:root[data-theme="dark"] .btn-theme .icon-light { display: none; }
:root[data-theme="dark"] .btn-theme .icon-dark { display: block; }

/* --------------------------------------------------------------------------
   5. BUTTONS
   -------------------------------------------------------------------------- */
.btn-nav, .btn-switch-role, .btn-lib-action, .btn-export, .btn-price,
.btn-accent, .btn-generate, .btn-ghost, .btn-soft {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s2);
  min-height: var(--tap);
  padding: 10px var(--s4);
  border: 1px solid transparent;
  border-radius: var(--r-md);
  font-family: var(--font-ui);
  font-size: .92rem;
  font-weight: 600;
  line-height: 1.2;
  cursor: pointer;
  white-space: nowrap;
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease),
              border-color var(--dur) var(--ease), transform var(--dur) var(--ease),
              box-shadow var(--dur) var(--ease);
}
.btn-nav:active, .btn-export:active, .btn-accent:active,
.btn-generate:active, .btn-lib-action:active, .btn-price:active { transform: translateY(1px); }

.btn-nav, .btn-switch-role, .btn-export, .btn-ghost {
  background: var(--surface);
  border-color: var(--border);
  color: var(--fg);
  box-shadow: var(--sh-xs);
}
.btn-nav:hover, .btn-switch-role:hover, .btn-export:hover, .btn-ghost:hover {
  background: var(--surface-2);
  border-color: var(--border-strong);
}

.btn-soft { background: var(--primary-soft); color: var(--primary); border-color: transparent; }
.btn-soft:hover { background: color-mix(in srgb, var(--primary-soft) 70%, var(--surface)); }

.btn-accent, .btn-generate {
  background: var(--grad-brand);
  color: #fff;
  border: none;
  box-shadow: 0 6px 18px -8px color-mix(in srgb, var(--primary) 70%, transparent);
}
.btn-accent:hover, .btn-generate:hover {
  filter: brightness(1.06);
  box-shadow: 0 10px 24px -8px color-mix(in srgb, var(--primary) 70%, transparent);
  text-decoration: none;
}

.btn-generate {
  width: 100%;
  min-height: 54px;
  font-size: 1.02rem;
  font-weight: 700;
  margin-top: var(--s4);
  border-radius: var(--r-lg);
}
.btn-generate:disabled { opacity: .72; cursor: progress; filter: none; }

.btn-dashboard { background: var(--surface); color: var(--fg); border-color: var(--border); }

/* Icon-only round button */
.btn-close-modal {
  display: inline-grid;
  place-items: center;
  width: var(--tap); height: var(--tap);
  flex: 0 0 var(--tap);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--surface);
  color: var(--fg-muted);
  font-size: 1.05rem;
  cursor: pointer;
  transition: all var(--dur) var(--ease);
}
.btn-close-modal:hover { background: var(--danger-soft); color: var(--danger); border-color: transparent; }

.btn-cover { background: var(--accent); color: #fff; border-color: transparent; }
.btn-cover:hover { background: var(--accent-hover); }
.btn-save { background: var(--success); color: #fff; border-color: transparent; }
.btn-save:hover { filter: brightness(1.08); }

/* Count pill inside a button */
.count-pill {
  display: inline-grid;
  place-items: center;
  min-width: 22px; height: 22px;
  padding: 0 7px;
  border-radius: var(--r-full);
  background: var(--primary);
  color: var(--on-primary);
  font-size: .74rem;
  font-weight: 700;
  font-family: var(--font-display);
}

/* --------------------------------------------------------------------------
   โหมดใบงานภาพสำหรับอนุบาล (เครื่องยนต์ที่ 2)
   -------------------------------------------------------------------------- */
.kid-mode-switch {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.kid-mode-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px;
  border: 2px solid var(--border);
  border-radius: var(--r-md, 10px);
  background: var(--surface);
  color: var(--fg);
  cursor: pointer;
  text-align: left;
  transition: border-color .15s, background .15s;
}

.kid-mode-btn span { display: flex; flex-direction: column; line-height: 1.25; }
.kid-mode-btn small { color: var(--fg-muted); font-size: .72rem; }
.kid-mode-btn:hover { border-color: var(--border-strong); }

.kid-mode-btn.is-active {
  border-color: var(--primary);
  background: color-mix(in srgb, var(--primary) 8%, transparent);
}

/* ใบงานภาพ — ภาพเต็มหน้ากระดาษ A4 พอดี ไม่มีขอบขาวเกิน */
.a4-sheet.kidsheet-view { padding: 0; overflow: hidden; }

.kidsheet-img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

/* --------------------------------------------------------------------------
   หน้าคลังใบงาน (หน้าเต็ม)
   -------------------------------------------------------------------------- */
.library-section {
  max-width: 1180px;
  margin: 0 auto;
  padding: 24px 20px 80px;
}

.library-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.lib-search {
  position: relative;
  flex: 1 1 260px;
  min-width: 0;
}

.lib-search i {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--fg-muted);
  pointer-events: none;
}

.lib-search input { padding-left: 40px; }

.lib-view-toggle {
  display: flex;
  gap: 6px;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: var(--r-full, 999px);
  background: var(--surface-2);
}

.lib-view-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 40px;             /* พื้นที่กดพอสำหรับนิ้ว */
  padding: 0 14px;
  border: 0;
  border-radius: var(--r-full, 999px);
  background: transparent;
  color: var(--fg-muted);
  font-size: .85rem;
  cursor: pointer;
  transition: background .15s, color .15s;
}

.lib-view-btn.is-active {
  background: var(--surface);
  color: var(--primary);
  font-weight: 600;
  box-shadow: 0 1px 3px rgba(15, 23, 42, .1);
}

/* ตัวกรองแบบชิป — เห็นตัวเลือกทั้งหมดในตาเดียว กดง่ายกว่า dropdown บนมือถือ */
.lib-filters { display: grid; gap: 8px; margin-bottom: 12px; }

.lib-filter-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.lib-filter-row:empty { display: none; }

.lib-filter-label {
  color: var(--fg-muted);
  font-size: .8rem;
  min-width: 62px;
}

.lib-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: 36px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: var(--r-full, 999px);
  background: var(--surface);
  color: var(--fg);
  font-size: .84rem;
  cursor: pointer;
  transition: border-color .15s, background .15s, color .15s;
}

.lib-chip:hover { border-color: var(--border-strong); }

/* ใช้ทั้งสีและเครื่องหมายถูก ไม่พึ่งสีอย่างเดียว */
.lib-chip.is-active {
  border-color: var(--primary);
  background: color-mix(in srgb, var(--primary) 12%, transparent);
  color: var(--primary);
  font-weight: 600;
}

.lib-chip:focus-visible { outline: 3px solid var(--ring); outline-offset: 2px; }

.lib-count-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
}

.btn-clear-filters {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 34px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--r-full, 999px);
  background: transparent;
  color: var(--fg-muted);
  font-size: .8rem;
  cursor: pointer;
}

.btn-clear-filters:hover { border-color: var(--danger, #e5484d); color: var(--danger, #e5484d); }

.lib-count {
  color: var(--fg-muted);
  font-size: .85rem;
}

/* กริด 2 ความหนาแน่น */
.library-grid.is-large {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 18px;
}

.library-grid.is-small {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(132px, 1fr));
  gap: 12px;
}

.library-card {
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  overflow: hidden;
  transition: border-color .15s, box-shadow .15s, transform .15s;
}

.library-card:hover {
  border-color: var(--primary);
  box-shadow: 0 8px 22px rgba(15, 23, 42, .1);
  transform: translateY(-2px);
}

/* ทั้งการ์ดเป็นปุ่ม — กดตรงไหนก็เข้ารายละเอียด และ tab เข้าถึงได้ */
.lib-card-open {
  display: block;
  width: 100%;
  padding: 0;
  border: 0;
  background: none;
  text-align: left;
  cursor: pointer;
  color: inherit;
  font: inherit;
}

.lib-card-open:focus-visible {
  outline: 3px solid var(--ring);
  outline-offset: -3px;
}

/* จองพื้นที่ด้วยสัดส่วน A4 ภาพโหลดช้าก็ไม่ทำให้หน้ากระตุก */
.lib-thumb-wrap {
  display: block;
  aspect-ratio: 1 / 1.414;
  background: #fff;
  border-bottom: 1px solid var(--border);
  overflow: hidden;
}

.lib-thumb {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: top center;
}

.lib-thumb-text {
  display: grid;
  place-items: center;
  background: var(--surface-2);
  color: var(--fg-muted);
  font-size: 2rem;
}

/* ---------- ตัวอย่างใบงานจริงบนการ์ด ----------
   วาดกระดาษ A4 ขนาดเต็มแล้วย่อด้วย transform (JS คำนวณอัตราส่วนให้)
   ไม่ได้เก็บรูปไว้ที่เซิร์ฟเวอร์ จึงใช้ได้กับใบงานเก่าทุกใบทันที */
.lib-thumb-live {
  display: block;
  width: 100%;
  height: 100%;
  background: #fff;
  overflow: hidden;
  position: relative;
}

.lib-thumb-live .mini-sheet {
  transform-origin: top left;
  /* กระดาษจริงกว้าง 210mm — ต้องคงขนาดไว้ก่อนย่อ ไม่งั้นตัวหนังสือจะจัดหน้าคนละแบบกับของจริง */
  width: 210mm;
  min-height: 297mm;
  position: absolute;
  top: 0;
  left: 0;
  margin: 0;
  box-shadow: none;
  border-radius: 0;
  pointer-events: none;
}

/* การ์ดเล็กมีที่น้อย — โชว์แค่หัวใบงานกับโจทย์ต้นๆ ก็พอให้จำได้ว่าใบไหน */
.library-grid.is-small .lib-thumb-live .mini-sheet { min-height: 0; }

.detail-live {
  aspect-ratio: 1 / 1.414;
  border: 1px solid var(--border);
  border-radius: 10px;
}

.lib-body { display: block; padding: 12px; }

.library-grid.is-small .lib-body { padding: 8px; }
.library-grid.is-small .lib-title { font-size: .82rem; }
.library-grid.is-small .lib-date,
.library-grid.is-small .lib-tag { display: none; }
.library-grid.is-small .lib-tag.tag-grade { display: inline-flex; }

.lib-title {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  font-weight: 600;
  font-size: .95rem;
  line-height: 1.4;
  margin: 6px 0 4px;
}

.lib-tag.tag-image {
  background: color-mix(in srgb, var(--primary) 14%, transparent);
  color: var(--primary);
}

/* ---------- หน้ารายละเอียดใบงาน ---------- */
.detail-body { padding: 4px 2px; }

.detail-grid {
  display: grid;
  grid-template-columns: minmax(0, 320px) minmax(0, 1fr);
  gap: 22px;
  align-items: start;
}

.detail-preview img {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
}

.detail-placeholder {
  display: grid;
  place-items: center;
  gap: 10px;
  aspect-ratio: 1 / 1.414;
  border: 1px dashed var(--border-strong);
  border-radius: 10px;
  color: var(--fg-muted);
  text-align: center;
  padding: 20px;
}

.detail-placeholder i { font-size: 2rem; }

.detail-info h3 { font-size: 1.15rem; margin-bottom: 10px; }
.detail-sub { margin: 18px 0 8px; font-size: .95rem; }

.detail-answers { list-style: none; display: grid; gap: 6px; }

.detail-answers li {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: .9rem;
}

.detail-note { color: var(--fg-muted); font-size: .9rem; }

.detail-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 22px;
}

.detail-actions .btn-lib-action { min-height: 44px; }

@media (max-width: 720px) {
  .detail-grid { grid-template-columns: 1fr; }
  .detail-preview { max-width: 260px; margin: 0 auto; }
}

/* แถบแจ้งว่ามีงานวาดค้างอยู่ — ลอยมุมจอ ครูจะได้รู้ว่าปิดหน้าไปได้ */
.job-banner {
  position: fixed;
  left: 16px;
  bottom: 16px;
  z-index: 60;
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: 320px;
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: 0 10px 30px rgba(15, 23, 42, .16);
}

.job-banner i { color: var(--primary); font-size: 1.2rem; }
.job-banner div { display: flex; flex-direction: column; line-height: 1.35; }
.job-banner small { color: var(--fg-muted); font-size: .75rem; }

@media (max-width: 640px) {
  .job-banner { left: 12px; right: 12px; bottom: 76px; max-width: none; }
}

.kid-loading {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 40px;
  text-align: center;
  color: #475569;
}

.kid-loading i { font-size: 2.4rem; color: var(--primary); }
.kid-loading h3 { font-size: 1.05rem; color: #1e293b; }
.kid-loading p { font-size: .88rem; line-height: 1.7; }

.kid-loading-bar {
  width: 220px; height: 6px; border-radius: 999px;
  background: #e2e8f0; overflow: hidden;
}

.kid-loading-bar span {
  display: block; height: 100%; width: 40%;
  border-radius: 999px; background: var(--primary);
  animation: kidLoadSlide 1.6s ease-in-out infinite;
}

@keyframes kidLoadSlide {
  0%   { transform: translateX(-110%); }
  100% { transform: translateX(360%); }
}

.kid-answer-key { padding: 28px 32px; font-family: var(--font-paper, 'Sarabun', sans-serif); }
.kid-answer-key h4 { font-size: 15pt; margin-bottom: 12px; }
.kid-answer-key ul { list-style: none; display: grid; gap: 8px; }
.kid-answer-key li { font-size: 13pt; padding: 8px 12px; border: 1px solid #e2e8f0; border-radius: 8px; }

/* Word bank on fill-in-the-blank worksheets (student view only) */
.word-bank {
  border: 2px dashed #94a3b8;
  border-radius: 10px;
  padding: 8px 12px;
  margin-bottom: 10px;
  background: #f8fafc;
}

.word-bank-title {
  display: block;
  font-weight: 700;
  font-size: 11pt;
  margin-bottom: 5px;
}

.word-bank-items {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 10px;
}

.word-chip {
  border: 1px solid #cbd5e1;
  border-radius: 999px;
  padding: 2px 12px;
  background: #fff;
  font-size: 12pt;
}

/* Account + quota chip in the navbar */
.btn-account { font-variant-numeric: tabular-nums; }

.label-optional {
  font-weight: 400;
  color: var(--fg-muted);
  font-size: .82em;
}

.btn-account.is-empty {
  border-color: var(--danger, #e5484d);
  color: var(--danger, #e5484d);
}

/* --------------------------------------------------------------------------
   6. MOBILE DRAWER + BOTTOM BAR (Phase 1 + 3)
   -------------------------------------------------------------------------- */
.drawer-scrim {
  position: fixed;
  inset: 0;
  z-index: 900;
  background: rgba(9, 14, 26, .55);
  backdrop-filter: blur(3px);
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--dur) var(--ease), visibility var(--dur) var(--ease);
}
.drawer-scrim.active { opacity: 1; visibility: visible; }

.drawer {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  z-index: 950;
  width: min(88vw, 340px);
  display: flex;
  flex-direction: column;
  gap: var(--s2);
  padding: var(--s5) var(--s4) calc(var(--s5) + env(safe-area-inset-bottom));
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: var(--sh-xl);
  transform: translateX(102%);
  transition: transform .28s var(--ease);
  overflow-y: auto;
  overscroll-behavior: contain;
}
.drawer.active { transform: translateX(0); }

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s3);
  margin-bottom: var(--s2);
}
.drawer-head h3 { font-size: 1rem; }

.drawer-user {
  display: flex;
  align-items: center;
  gap: var(--s3);
  padding: var(--s3);
  border-radius: var(--r-md);
  background: var(--surface-2);
  border: 1px solid var(--border);
  margin-bottom: var(--s2);
}
.drawer-user i { color: var(--fg-muted); font-size: 1.15rem; }
.drawer-user .user-info { line-height: 1.3; }
.drawer-user .user-title { font-size: .9rem; max-width: none; white-space: normal; }
.drawer-user .user-plan { font-size: .78rem; max-width: none; white-space: normal; }

.drawer-item {
  display: flex;
  align-items: center;
  gap: var(--s3);
  width: 100%;
  min-height: 52px;
  padding: 10px var(--s4);
  border: 1px solid transparent;
  border-radius: var(--r-md);
  background: transparent;
  color: var(--fg);
  font-size: .96rem;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
  transition: background var(--dur) var(--ease);
}
.drawer-item:hover { background: var(--surface-2); text-decoration: none; }
.drawer-item > i:first-child { width: 22px; text-align: center; color: var(--fg-muted); }
.drawer-item.primary { background: var(--grad-brand); color: #fff; justify-content: center; }
.drawer-item.primary > i:first-child { color: #fff; }
.drawer-sep { height: 1px; background: var(--border); margin: var(--s2) 0; }

/* Bottom action bar (mobile only) */
.bottombar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 600;
  display: none;
  align-items: stretch;
  gap: 2px;
  height: calc(var(--bottombar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--border);
}
.bottombar button {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  min-height: var(--tap);
  border: none;
  background: transparent;
  color: var(--fg-muted);
  font-size: .68rem;
  font-weight: 600;
  cursor: pointer;
  position: relative;
}
.bottombar button i { font-size: 1.05rem; }
.bottombar button.is-primary { color: var(--primary); }
.bottombar button:active { background: var(--surface-2); }
.bottombar .count-pill {
  position: absolute;
  top: 6px;
  left: 50%;
  margin-left: 6px;
  min-width: 18px; height: 18px;
  font-size: .64rem;
}

/* --------------------------------------------------------------------------
   7. VIEW SWITCHING (Phase 3: landing vs studio)
   -------------------------------------------------------------------------- */
/* 3 มุมมอง: landing (หน้าขาย) · studio (ห้องทำงาน) · library (คลังใบงาน) */
body[data-view="studio"] .view-landing,
body[data-view="studio"] .view-library { display: none; }

body[data-view="landing"] .view-studio,
body[data-view="landing"] .view-library { display: none; }

body[data-view="library"] .view-landing,
body[data-view="library"] .view-studio { display: none; }

/* --------------------------------------------------------------------------
   8. HERO / LANDING
   -------------------------------------------------------------------------- */
.hero-section {
  position: relative;
  text-align: center;
  padding: var(--s16) var(--s5) var(--s12);
  max-width: 1000px;
  margin-inline: auto;
}

.hero-tag {
  display: inline-flex;
  align-items: center;
  gap: var(--s2);
  padding: 7px var(--s4);
  background: var(--gold-soft);
  color: var(--gold);
  border: 1px solid color-mix(in srgb, var(--gold) 28%, transparent);
  border-radius: var(--r-full);
  margin-bottom: var(--s5);
  font-size: .85rem;
  font-weight: 600;
  text-align: left;
}

.hero-title {
  font-size: clamp(1.9rem, 5.4vw, 3.4rem);
  line-height: 1.18;
  margin-bottom: var(--s4);
  letter-spacing: -.025em;
}

.hero-desc {
  font-size: clamp(.98rem, 2.1vw, 1.16rem);
  color: var(--fg-muted);
  max-width: 720px;
  margin: 0 auto;
}

.hero-cta {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: var(--s3);
  margin-top: var(--s8);
}
.hero-cta .btn-accent, .hero-cta .btn-ghost { min-height: 52px; padding-inline: var(--s6); font-size: 1rem; }

.hero-proof {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: var(--s3) var(--s6);
  margin-top: var(--s8);
  color: var(--fg-muted);
  font-size: .88rem;
}
.hero-proof span { display: inline-flex; align-items: center; gap: 7px; }
.hero-proof i { color: var(--success); }

/* Feature grid on landing */
.feature-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: var(--s4);
  max-width: 1160px;
  margin: 0 auto;
  padding: 0 var(--s5) var(--s10);
}
.feature-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: var(--s6);
  box-shadow: var(--sh-xs);
  transition: transform var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}
.feature-card:hover { transform: translateY(-3px); box-shadow: var(--sh-md); }
.feature-ico {
  display: grid; place-items: center;
  width: 46px; height: 46px;
  border-radius: 13px;
  background: var(--primary-soft);
  color: var(--primary);
  font-size: 1.15rem;
  margin-bottom: var(--s4);
}
.feature-card h3 { font-size: 1.02rem; margin-bottom: 6px; }
.feature-card p { color: var(--fg-muted); font-size: .9rem; line-height: 1.6; }

/* --------------------------------------------------------------------------
   9. SECTION HEADERS
   -------------------------------------------------------------------------- */
.section-header { text-align: center; margin-bottom: var(--s8); }
.section-header h2 {
  font-size: clamp(1.35rem, 3.4vw, 2rem);
  margin-bottom: 6px;
  display: inline-flex;
  align-items: center;
  gap: var(--s3);
  flex-wrap: wrap;
  justify-content: center;
}
.section-header h2 i { color: var(--primary); }
.section-header p { color: var(--fg-muted); font-size: .96rem; }

/* --------------------------------------------------------------------------
   10. STUDIO LAYOUT
   -------------------------------------------------------------------------- */
.studio-section {
  padding: var(--s8) var(--s5) var(--s12);
  max-width: 1440px;
  margin: 0 auto;
}

.studio-grid {
  display: grid;
  grid-template-columns: minmax(300px, 370px) minmax(0, 1fr);
  gap: var(--s6);
  align-items: start;
}

.controls-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  padding: var(--s6);
  box-shadow: var(--sh-sm);
  position: sticky;
  top: calc(var(--nav-h) + var(--s4));
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s3);
  margin-bottom: var(--s5);
  padding-bottom: var(--s4);
  border-bottom: 1px solid var(--border);
}
.card-header h3 { font-size: 1.05rem; display: flex; align-items: center; gap: var(--s2); }
.card-header h3 i { color: var(--primary); }

/* --------------------------------------------------------------------------
   11. FORMS
   -------------------------------------------------------------------------- */
.form-group { margin-bottom: var(--s5); }

.form-label {
  display: flex;
  align-items: center;
  gap: var(--s2);
  margin-bottom: var(--s2);
  font-size: .87rem;
  font-weight: 600;
  color: var(--fg);
}
.form-label i { color: var(--fg-subtle); width: 16px; text-align: center; }

.form-control {
  width: 100%;
  min-height: var(--tap);
  padding: 11px var(--s4);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  color: var(--fg);
  font-family: inherit;
  font-size: .95rem;
  transition: border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease), background var(--dur) var(--ease);
}
.form-control::placeholder { color: var(--fg-subtle); }
.form-control:hover { border-color: var(--fg-subtle); }
.form-control:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 4px var(--ring);
}
textarea.form-control { min-height: 96px; resize: vertical; line-height: 1.6; }

select.form-control {
  appearance: none;
  -webkit-appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%), linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 19px) calc(50% + 1px), calc(100% - 13px) calc(50% + 1px);
  background-size: 6px 6px, 6px 6px;
  background-repeat: no-repeat;
  padding-right: var(--s10);
}

.form-hint { margin-top: 6px; font-size: .78rem; color: var(--fg-subtle); }

/* Input with trailing button */
.input-with-toggle { display: flex; position: relative; }
.input-with-toggle .form-control { padding-right: 52px; }
.btn-toggle-pwd {
  position: absolute;
  right: 4px; top: 50%;
  transform: translateY(-50%);
  width: 40px; height: 40px;
  display: grid; place-items: center;
  background: none;
  border: none;
  border-radius: var(--r-sm);
  color: var(--fg-muted);
  cursor: pointer;
}
.btn-toggle-pwd:hover { background: var(--surface-2); color: var(--fg); }

/* Segmented theme picker (replaces emoji <select>) */
.theme-picker {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(88px, 1fr));
  gap: var(--s2);
}
.theme-chip {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 62px;
  padding: var(--s2);
  border: 1.5px solid var(--border);
  border-radius: var(--r-md);
  background: var(--surface);
  color: var(--fg-muted);
  font-size: .78rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--dur) var(--ease);
}
.theme-chip:hover { border-color: var(--border-strong); background: var(--surface-2); }
.theme-chip[aria-pressed="true"] {
  border-color: var(--primary);
  background: var(--primary-soft);
  color: var(--primary);
  box-shadow: 0 0 0 3px var(--ring);
}
.theme-swatch {
  width: 26px; height: 26px;
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, .12);
  box-shadow: inset 0 -6px 8px -6px rgba(0, 0, 0, .3);
}
.sw-classic  { background: linear-gradient(135deg, #f8fafc, #cbd5e1); }
.sw-colorful { background: linear-gradient(135deg, #ec4899, #f59e0b); }
.sw-pastel   { background: linear-gradient(135deg, #c4b5fd, #fbcfe8); }
.sw-minimal  { background: linear-gradient(135deg, #ffffff, #94a3b8); }
.sw-cartoon  { background: linear-gradient(135deg, #ff7043, #ffd54f); }

/* Switch (illustration toggle) */
.switch-field {
  display: inline-flex;
  align-items: center;
  gap: var(--s3);
  min-height: var(--tap);
  padding: 6px var(--s3) 6px 6px;
  border: 1px solid var(--border);
  border-radius: var(--r-full);
  background: var(--surface);
  cursor: pointer;
  font-size: .86rem;
  font-weight: 600;
  color: var(--fg);
  user-select: none;
  transition: border-color var(--dur) var(--ease);
}
.switch-field:hover { border-color: var(--border-strong); }
.switch-field input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch-track {
  position: relative;
  width: 44px; height: 26px;
  flex: 0 0 44px;
  border-radius: var(--r-full);
  background: var(--surface-3);
  transition: background var(--dur) var(--ease);
}
.switch-track::after {
  content: '';
  position: absolute;
  top: 3px; left: 3px;
  width: 20px; height: 20px;
  border-radius: 50%;
  background: #fff;
  box-shadow: var(--sh-sm);
  transition: transform var(--dur) var(--ease);
}
.switch-field input:checked + .switch-track { background: var(--primary); }
.switch-field input:checked + .switch-track::after { transform: translateX(18px); }
.switch-field input:focus-visible + .switch-track { outline: 3px solid var(--ring); outline-offset: 2px; }

/* License info box */
.license-info-box {
  display: flex;
  align-items: flex-start;
  gap: var(--s3);
  padding: var(--s4);
  border-radius: var(--r-md);
  background: var(--gold-soft);
  border: 1px solid color-mix(in srgb, var(--gold) 30%, transparent);
  color: var(--gold);
  font-size: .88rem;
}
.license-info-box > i { margin-top: 3px; font-size: 1rem; }
.license-info-box strong { display: block; font-size: .92rem; }
.license-info-box.blue-box {
  background: var(--blue-soft);
  border-color: color-mix(in srgb, var(--blue) 30%, transparent);
  color: var(--blue);
}

/* --------------------------------------------------------------------------
   12. PREVIEW CARD + TOOLBAR (Phase 3: grouped actions)
   -------------------------------------------------------------------------- */
.preview-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  box-shadow: var(--sh-sm);
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

.preview-toolbar {
  display: flex;
  flex-direction: column;
  gap: var(--s3);
  padding: var(--s4) var(--s5);
  border-bottom: 1px solid var(--border);
}

.toolbar-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--s3);
}
.toolbar-row.row-export { justify-content: flex-end; gap: var(--s3); }
.toolbar-group { display: flex; align-items: center; gap: var(--s2); flex-wrap: wrap; min-width: 0; }
.toolbar-group.scroll-x {
  flex-wrap: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 2px;
}
.toolbar-group.scroll-x::-webkit-scrollbar { display: none; }
.toolbar-label {
  font-size: .72rem;
  font-weight: 700;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--fg-subtle);
  flex: 0 0 auto;
}

/* Tabs */
.preview-tabs {
  display: inline-flex;
  gap: 4px;
  padding: 4px;
  border-radius: var(--r-full);
  background: var(--surface-2);
  border: 1px solid var(--border);
  flex: 0 0 auto;
}
.tab-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  /* 44px everywhere: 1024px is also iPad Pro landscape, which is touch-driven */
  min-height: var(--tap);
  padding: 0 var(--s4);
  border: none;
  border-radius: var(--r-full);
  background: transparent;
  color: var(--fg-muted);
  font-family: inherit;
  font-size: .86rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: all var(--dur) var(--ease);
}
.tab-btn:hover { color: var(--fg); }
.tab-btn.active {
  background: var(--surface);
  color: var(--fg);
  box-shadow: var(--sh-xs);
}
.tab-btn.disabled { opacity: .48; cursor: not-allowed; }
.tab-btn.disabled:hover { color: var(--fg-muted); }
.tab-short { display: none; }

.export-buttons { display: flex; align-items: center; gap: var(--s2); flex-wrap: wrap; }
.btn-export { padding: 9px var(--s3); font-size: .86rem; }

/* Paper stage */
.paper-wrapper {
  padding: var(--s6);
  background: var(--surface-2);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overflow-x: auto;
  min-height: 300px;
}

/* before the first generate there is no sheet to show — only the guidance block */
.paper-wrapper.is-empty { align-items: center; }
.paper-wrapper.is-empty .paper-stage { display: none; }
.paper-wrapper:not(.is-empty) #studioEmpty { display: none; }

/* Responsive shrink of the A4 sheet so it never breaks the layout */
.paper-stage {
  width: 210mm;
  flex: 0 0 auto;
  transform-origin: top center;
}

/* --------------------------------------------------------------------------
   13. THE A4 SHEET (always light — it is paper)
   -------------------------------------------------------------------------- */
.a4-sheet {
  width: 210mm;
  min-height: 297mm;
  background: #ffffff;
  color: #111827;
  box-shadow: var(--sh-paper);
  padding: 18mm 18mm 26mm;
  position: relative;
  font-family: var(--font-paper);
  border-radius: 2px;
  print-color-adjust: exact;
  -webkit-print-color-adjust: exact;
}

.sheet-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 14px;
  border-bottom: 2px solid #111827;
  padding-bottom: 10px;
  margin-bottom: 16px;
}

.sheet-title { font-size: 23pt; margin: 8px 0 4px; font-weight: 700; line-height: 1.25; }

.shop-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 9.5pt;
  color: #4b5563;
  border: 1px solid #d1d5db;
  padding: 2px 9px;
  border-radius: 999px;
}

.meta-tag {
  display: inline-block;
  background: #f1f5f9;
  color: #334155;
  padding: 2px 9px;
  border-radius: 5px;
  font-size: 9.5pt;
  margin-right: 5px;
  margin-bottom: 4px;
}

.score-box {
  border: 2px solid #111827;
  padding: 8px 12px;
  text-align: center;
  border-radius: 8px;
  min-width: 108px;
  flex: 0 0 auto;
  font-size: 10pt;
}
.score-input { font-size: 17pt; margin-top: 6px; font-weight: 700; }

.student-info-bar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 18px;
  font-size: 13pt;
  color: #1f2937;
}

.ai-illustration-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 14px 0 18px;
}
.illustration-img {
  max-width: 100%;
  max-height: 250px;
  width: auto;
  border-radius: 10px;
  border: 2px dashed #cbd5e1;
  padding: 8px;
}

.illustration-shimmer {
  width: 100%;
  height: 190px;
  border-radius: 10px;
  background: linear-gradient(90deg, #eef2f7 25%, #dfe6ef 50%, #eef2f7 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite linear;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 8px;
  color: #64748b;
  font-size: 11pt;
  font-family: var(--font-ui);
}

.sheet-body { font-size: 15pt; line-height: 1.95; min-height: 120mm; }

.sheet-footer {
  position: absolute;
  bottom: 14mm;
  left: 18mm;
  right: 18mm;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  border-top: 1px solid #d1d5db;
  padding-top: 8px;
  font-size: 9pt;
  color: #6b7280;
}
.sheet-footer .color-gold { color: #b45309; }
.sheet-footer .color-blue { color: #1d4ed8; }

/* ---- Question blocks ---- */
.question-grid { display: flex; flex-direction: column; gap: 14px; }

.q-item, .question-item {
  display: flex;
  gap: 10px;
  padding: 12px 14px;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  background: #fbfcfe;
  page-break-inside: avoid;
  break-inside: avoid;
}
.q-item { flex-direction: column; gap: 6px; }

.q-number { font-weight: 700; color: #0f172a; flex: 0 0 auto; }
.question-item .q-number { min-width: 74px; }
.q-content { flex: 1 1 auto; min-width: 0; }
.q-text { font-weight: 500; }
.q-hint { color: #64748b; font-size: 12pt; }
.q-answer { margin-top: 6px; font-size: 13pt; }

.answer-line {
  display: inline-block;
  min-width: 140px;
  border-bottom: 1.5px dotted #94a3b8;
  color: #94a3b8;
  letter-spacing: 1px;
}
.answer-lines .answer-line { display: block; min-width: 0; width: 100%; margin-bottom: 10px; }

.ans-key {
  display: inline-block;
  background: #dcfce7;
  color: #166534;
  border: 1px solid #86efac;
  border-radius: 6px;
  padding: 1px 9px;
  font-weight: 700;
  font-size: 13pt;
}

.answer-banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  background: #fee2e2;
  border: 1px solid #fca5a5;
  color: #991b1b;
  padding: 9px 14px;
  border-radius: 9px;
  margin-bottom: 16px;
  font-weight: 700;
  font-size: 13pt;
}

/* True / False */
.tf-options { display: flex; gap: 18px; margin-top: 8px; font-size: 13pt; }
.tf-option {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 3px 16px;
  border: 2px solid #e2e8f0;
  border-radius: 8px;
  font-weight: 600;
  color: #374151;
}
.tf-option .tf-mark {
  display: inline-grid;
  place-items: center;
  width: 20px; height: 20px;
  border-radius: 50%;
  border: 2px solid #94a3b8;
  font-size: 10pt;
  color: #475569;
}
.tf-option.is-true .tf-mark { border-color: #16a34a; color: #16a34a; }
.tf-option.is-false .tf-mark { border-color: #dc2626; color: #dc2626; }

/* Ordering */
.ordering-items { display: flex; flex-wrap: wrap; gap: 8px; margin: 9px 0; }
.ordering-chip {
  padding: 5px 15px;
  background: #eff6ff;
  border: 2px dashed #93c5fd;
  border-radius: 999px;
  font-weight: 600;
  color: #1e40af;
  font-size: 12pt;
}

/* Matching */
.match-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 26px; margin-top: 14px; }
.match-col h4 { font-size: 13pt; margin-bottom: 9px; color: #0f172a; }
.match-item {
  padding: 9px 12px;
  margin-bottom: 8px;
  border-radius: 8px;
  border: 1px solid #e2e8f0;
  background: #f8fafc;
  font-size: 13pt;
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
}
.match-item.right { background: #fffbeb; border-color: #fde68a; }

/* Multiple choice */
.choice-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 9px; }
.choice-opt {
  padding: 6px 11px;
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 7px;
  font-size: 12pt;
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: center;
}
.choice-opt.is-answer { background: #f0fdf4; border-color: #86efac; }

/* Handwriting trace */
.trace-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.trace-card {
  border: 1px dashed #94a3b8;
  padding: 12px;
  border-radius: 10px;
  text-align: center;
  background: #fdfdfd;
}
.trace-word {
  font-size: 23pt;
  color: #94a3b8;
  letter-spacing: 5px;
  border-bottom: 1px dotted #cbd5e1;
  padding-bottom: 6px;
}
.trace-meta { font-size: 9.5pt; color: #475569; margin-top: 6px; }

/* Verification badges */
.verified-badge, .unverified-badge {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  margin-top: 14px;
  padding: 4px 12px;
  border-radius: 999px;
  font-size: 9.5pt;
  font-weight: 600;
}
.verified-badge { background: #dcfce7; color: #166534; border: 1px solid #86efac; }
.unverified-badge { background: #fef3c7; color: #92400e; border: 1px solid #fcd34d; }

.ai-source-note {
  margin-top: 14px;
  font-size: 8pt;
  color: #94a3b8;
  text-align: right;
}

/* ---- Auto density: keep a worksheet on a single A4 page when it can fit ---- */
.a4-sheet.density-compact { padding: 15mm 15mm 22mm; }
.a4-sheet.density-compact .sheet-title { font-size: 20pt; }
.a4-sheet.density-compact .student-info-bar { font-size: 12pt; margin-bottom: 12px; }
.a4-sheet.density-compact .sheet-body { font-size: 13pt; line-height: 1.55; min-height: 0; }
.a4-sheet.density-compact .question-grid { gap: 9px; }
.a4-sheet.density-compact .q-item,
.a4-sheet.density-compact .question-item { padding: 9px 12px; }
.a4-sheet.density-compact .q-hint { font-size: 11pt; }
.a4-sheet.density-compact .illustration-img { max-height: 170px; }
.a4-sheet.density-compact .ai-illustration-container { margin: 10px 0 12px; }

.a4-sheet.density-tight { padding: 13mm 14mm 20mm; }
.a4-sheet.density-tight .sheet-title { font-size: 18pt; margin: 5px 0 3px; }
.a4-sheet.density-tight .student-info-bar { font-size: 11pt; margin-bottom: 10px; }
.a4-sheet.density-tight .sheet-body { font-size: 11.5pt; line-height: 1.42; min-height: 0; }
.a4-sheet.density-tight .question-grid { gap: 6px; }
.a4-sheet.density-tight .q-item,
.a4-sheet.density-tight .question-item { padding: 7px 10px; }
.a4-sheet.density-tight .q-hint { font-size: 10pt; }
.a4-sheet.density-tight .q-answer { font-size: 11pt; }
.a4-sheet.density-tight .ans-key { font-size: 11pt; padding: 1px 7px; }
.a4-sheet.density-tight .illustration-img { max-height: 120px; }
.a4-sheet.density-tight .ai-illustration-container { margin: 8px 0 10px; }
.a4-sheet.density-tight .sheet-header { margin-bottom: 12px; }
.a4-sheet.density-tight .score-box { padding: 6px 10px; font-size: 9pt; }
.a4-sheet.density-tight .score-input { font-size: 14pt; margin-top: 4px; }

/* ---- Certificate view ---- */
.cert-container { text-align: center; padding: 10px 0; }
.cert-header i { font-size: 40pt; color: #d97706; }
.cert-title { font-size: 24pt; margin: 12px 0 4px; color: #92400e; }
.cert-header p { letter-spacing: 4px; font-size: 10pt; color: #a16207; }
.cert-body { margin-top: 22px; font-size: 13pt; color: #1f2937; }
.cert-box {
  text-align: left;
  margin: 22px auto;
  max-width: 88%;
  padding: 18px 22px;
  border: 2px solid #fcd34d;
  border-radius: 12px;
  background: #fffbeb;
  font-size: 12pt;
  line-height: 1.9;
}
.a4-sheet.cert-view { background: #fffdf7; }

/* ---- Worksheet themes (no Comic Sans — real loaded Thai webfonts) ---- */
.theme-classic  .sheet-title { color: #111827; font-family: 'Pridi', var(--font-paper); }
.theme-colorful .sheet-title { color: #db2777; font-family: 'Mali', var(--font-paper); font-weight: 700; }
.theme-pastel   .sheet-title { color: #7c3aed; font-family: 'Mali', var(--font-paper); font-weight: 600; }
.theme-minimal  .sheet-title { color: #1f2937; font-family: var(--font-paper); font-weight: 300; letter-spacing: -.01em; }
.theme-cartoon  .sheet-title { color: #ea580c; font-family: 'Itim', var(--font-paper); text-shadow: 2px 2px 0 #fde68a; }

.theme-colorful .q-item, .theme-colorful .question-item { background: #fff7fb; border-color: #fbcfe8; }
.theme-pastel   .q-item, .theme-pastel .question-item   { background: #faf5ff; border-color: #e9d5ff; }
.theme-cartoon  .q-item, .theme-cartoon .question-item  { background: #fffbeb; border-color: #fde68a; border-width: 2px; }
.theme-minimal  .q-item, .theme-minimal .question-item  { background: #fff; border-color: #eef2f6; }
.theme-classic  .q-item, .theme-classic .question-item  { background: #fff; border-color: #d6d3d1; }

.theme-cartoon .sheet-body, .theme-colorful .sheet-body { font-family: 'Mali', var(--font-paper); }
.theme-classic .sheet-body { font-family: 'Pridi', var(--font-paper); }

/* --------------------------------------------------------------------------
   14. SKELETON + EMPTY STATES (Phase 3)
   -------------------------------------------------------------------------- */
.sk-line {
  height: 14px;
  border-radius: 7px;
  background: linear-gradient(90deg, #eef2f7 25%, #dfe6ef 50%, #eef2f7 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite linear;
  margin-bottom: 12px;
}
.sk-block {
  border-radius: 12px;
  background: linear-gradient(90deg, #eef2f7 25%, #dfe6ef 50%, #eef2f7 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite linear;
}
.sk-note {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin: 20px 0 26px;
  color: #64748b;
  font-family: var(--font-ui);
  font-size: 12pt;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: var(--s3);
  padding: var(--s12) var(--s5);
  color: #64748b;
  font-family: var(--font-ui);
  min-height: 380px;
}
.empty-state .empty-ico {
  display: grid; place-items: center;
  width: 78px; height: 78px;
  border-radius: 24px;
  background: #eef2f7;
  color: #94a3b8;
  font-size: 1.9rem;
}
.empty-state h3 { color: #334155; font-size: 1.12rem; }
.empty-state p { max-width: 380px; font-size: .93rem; line-height: 1.7; }
.empty-steps {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: var(--s3);
  text-align: left;
  font-size: .88rem;
}
.empty-steps span { display: flex; align-items: center; gap: 10px; }
.empty-steps b {
  display: grid; place-items: center;
  width: 24px; height: 24px; flex: 0 0 24px;
  border-radius: 50%;
  background: #0f766e;
  color: #fff;
  font-size: .76rem;
}

.empty-library {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: var(--s3);
  padding: var(--s12) var(--s5);
  color: var(--fg-muted);
}
.empty-library > i {
  display: grid; place-items: center;
  width: 72px; height: 72px;
  border-radius: 22px;
  background: var(--surface-2);
  color: var(--fg-subtle);
  font-size: 1.7rem;
}
.empty-library h3 { color: var(--fg); font-size: 1.08rem; }
.empty-library p { max-width: 420px; font-size: .92rem; }

/* --------------------------------------------------------------------------
   15. MODALS
   -------------------------------------------------------------------------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(9, 14, 26, .55);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: none;
  justify-content: center;
  align-items: center;
  padding: var(--s4);
  overscroll-behavior: contain;
}
.modal-overlay.active { display: flex; animation: fade-in .18s var(--ease); }

.modal-card {
  position: relative;
  width: 100%;
  max-width: 620px;
  max-height: min(88vh, 900px);
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  padding: var(--s6);
  box-shadow: var(--sh-xl);
  animation: pop-in .24s var(--ease);
}
.modal-large { max-width: 1040px; }

.modal-header-bar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: var(--s3);
  margin-bottom: var(--s2);
}
.modal-header-bar h2 {
  font-size: 1.16rem;
  display: flex;
  align-items: center;
  gap: var(--s3);
  min-width: 0;
}
.modal-header-bar h2 i { color: var(--primary); flex: 0 0 auto; }

/* ---------- หน้าชวนอัปเกรดตอนโควตาหมด ----------
   จุดนี้คือวินาทีที่ครูพร้อมจ่ายเงินที่สุด — ต้องอ่านง่ายและมีปุ่มจ่ายเงินอยู่ตรงนี้ */
.quota-wall {
  max-width: 460px;
  margin: 0 auto;
  padding: 26px 20px 30px;
  text-align: center;
}
/* ปุ่มปิดลอยมุมขวาบนของกล่อง ไม่เบียดหัวข้อ */
.qw-close {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 2;
}
.quota-wall .qw-ico {
  font-size: 2.4rem;
  color: var(--primary);
  margin-bottom: 12px;
}
.quota-wall h3 { font-size: 1.25rem; margin: 0 0 8px; }
.quota-wall .qw-praise {
  color: var(--fg-muted);
  font-size: .92rem;
  margin: 0 0 20px;
}
.qw-offer {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 20px 18px;
  text-align: left;
}
.qw-price {
  text-align: center;
  font-size: 2.1rem;
  font-weight: 700;
  color: var(--primary);
  line-height: 1.1;
  margin-bottom: 14px;
}
.qw-price span { font-size: 1.2rem; vertical-align: super; }
.qw-price small { font-size: .95rem; color: var(--fg-muted); font-weight: 500; }
.qw-list { list-style: none; padding: 0; margin: 0 0 18px; display: grid; gap: 9px; }
.qw-list li { display: flex; gap: 9px; align-items: flex-start; font-size: .9rem; }
.qw-list i { color: var(--primary); margin-top: 3px; flex: 0 0 auto; }
.qw-cta { width: 100%; justify-content: center; }
.qw-note {
  margin: 10px 0 0;
  font-size: .78rem;
  color: var(--fg-subtle);
  text-align: center;
}
.qw-secondary {
  margin-top: 16px;
  background: none;
  border: 0;
  color: var(--fg-muted);
  font-family: inherit;
  font-size: .88rem;
  cursor: pointer;
  text-decoration: underline;
}
.qw-secondary:hover { color: var(--primary); }

/**
 * ⚠️ `hidden` ของ HTML ต้องชนะเสมอ
 * เบราว์เซอร์ตั้ง [hidden]{display:none} ไว้ในสไตล์พื้นฐาน ซึ่ง "แพ้" กฎของเราที่ตั้ง
 * display:flex / grid ให้ปุ่มและการ์ด — สั่งซ่อนด้วย el.hidden = true จึงไม่มีผล
 * (เคยพลาดมาแล้ว: ซ่อนปุ่ม "เปลี่ยนบทบาท" ไม่สำเร็จ ปุ่มยังโผล่ให้กดอยู่)
 */
[hidden] { display: none !important; }

.modal-sub { color: var(--fg-muted); margin-bottom: var(--s5); font-size: .91rem; }

/* ---- ฟอร์มสมัคร/เข้าสู่ระบบ (อยู่บนเว็บนี้เอง ไม่เด้งออกไปเว็บอื่น) ---- */
.auth-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  background: var(--bg-subtle, #f1f5f9);
  border-radius: 12px;
  padding: 4px;
  margin-bottom: var(--s4);
}
.auth-tab {
  padding: 9px 10px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--fg-muted);
  font-family: inherit;
  font-size: .9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, color .15s;
}
.auth-tab.is-active {
  background: var(--surface, #fff);
  color: var(--primary);
  box-shadow: 0 1px 3px rgb(15 23 42 / .12);
}
.auth-error {
  margin: 0 0 var(--s3);
  padding: 9px 12px;
  border-radius: 10px;
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: #b91c1c;
  font-size: .88rem;
}
#authForm .form-group { margin-bottom: var(--s3); }
#authSubmitBtn[disabled] { opacity: .65; cursor: progress; }

.btn-google {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  width: 100%;
  padding: 11px 14px;
  border: 1.5px solid var(--border, #e2e8f0);
  border-radius: 12px;
  background: var(--surface, #fff);
  color: var(--fg, #0f172a);
  font-family: inherit;
  font-size: .95rem;
  font-weight: 600;
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.btn-google:hover { border-color: var(--fg-subtle); background: var(--bg-subtle, #f8fafc); }
.btn-google[disabled] { opacity: .65; cursor: progress; }

.auth-divider {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: var(--s4) 0;
  color: var(--fg-subtle);
  font-size: .82rem;
}
.auth-divider::before, .auth-divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border, #e2e8f0);
}

/* Role picker cards (Phase 3: visual preview) */
.role-options { display: grid; grid-template-columns: 1fr 1fr; gap: var(--s4); }
.role-card {
  display: flex;
  flex-direction: column;
  gap: var(--s3);
  padding: var(--s5);
  border: 1.5px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--surface);
  cursor: pointer;
  text-align: left;
  transition: all var(--dur) var(--ease);
}
.role-card:hover { border-color: var(--primary); transform: translateY(-3px); box-shadow: var(--sh-md); }
.role-card h3 { font-size: 1rem; display: flex; align-items: center; gap: var(--s2); }
.role-card p { font-size: .87rem; color: var(--fg-muted); line-height: 1.6; }
.role-card.gold h3 { color: var(--gold); }
.role-card.blue h3 { color: var(--blue); }

/* Mini preview illustration inside role card */
.role-preview {
  height: 116px;
  border-radius: var(--r-md);
  border: 1px solid var(--border);
  background: #fff;
  padding: 10px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.rp-bar { height: 7px; border-radius: 4px; background: #e2e8f0; }
.rp-bar.title { height: 11px; width: 62%; }
.rp-bar.short { width: 40%; }
.rp-row { display: flex; gap: 6px; }
.rp-chip { height: 16px; flex: 1; border-radius: 5px; background: #f1f5f9; }
.role-card.gold .rp-bar.title { background: #f59e0b; }
.role-card.gold .rp-stamp { background: #fef3c7; color: #b45309; }
.role-card.blue .rp-bar.title { background: #3b82f6; }
.role-card.blue .rp-stamp { background: #dbeafe; color: #1d4ed8; }
.rp-stamp {
  align-self: flex-start;
  margin-top: auto;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: .62rem;
  font-weight: 700;
}

.role-feat { display: flex; flex-direction: column; gap: 5px; font-size: .82rem; color: var(--fg-muted); }
.role-feat span { display: flex; align-items: center; gap: 7px; }
.role-feat i { font-size: .72rem; }

/* Settings status box */
.status-box {
  display: flex;
  align-items: center;
  gap: var(--s2);
  flex-wrap: wrap;
  padding: var(--s4);
  margin: var(--s5) 0;
  border-radius: var(--r-md);
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: .9rem;
}
.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 11px;
  border-radius: var(--r-full);
  font-size: .82rem;
  font-weight: 700;
}
.status-pill.ok { background: var(--success-soft); color: var(--success); }
.status-pill.off { background: var(--danger-soft); color: var(--danger); }
.status-pill.pending { background: var(--warn-soft); color: var(--warn); }

.modal-actions { display: flex; justify-content: flex-end; gap: var(--s3); flex-wrap: wrap; }

/* --------------------------------------------------------------------------
   16. LIBRARY
   -------------------------------------------------------------------------- */
.library-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(248px, 1fr));
  gap: var(--s4);
  margin-top: var(--s4);
}

.library-card {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: var(--s4);
  padding: var(--s4);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--surface);
  box-shadow: var(--sh-xs);
  transition: transform var(--dur) var(--ease), box-shadow var(--dur) var(--ease), border-color var(--dur) var(--ease);
}
.library-card:hover { transform: translateY(-3px); box-shadow: var(--sh-md); border-color: var(--border-strong); }

.lib-meta { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: var(--s2); }
.lib-tag {
  padding: 2px 9px;
  border-radius: var(--r-full);
  font-size: .7rem;
  font-weight: 700;
  background: var(--surface-2);
  color: var(--fg-muted);
  border: 1px solid var(--border);
}
.lib-tag.tag-subject { background: var(--primary-soft); color: var(--primary); border-color: transparent; }
.lib-tag.tag-grade { background: var(--blue-soft); color: var(--blue); border-color: transparent; }
.lib-tag.tag-license { background: var(--gold-soft); color: var(--gold); border-color: transparent; }

.lib-title {
  font-size: .97rem;
  line-height: 1.45;
  margin-bottom: 6px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.lib-date { font-size: .76rem; color: var(--fg-subtle); display: flex; align-items: center; gap: 6px; }

.lib-actions { display: flex; gap: var(--s2); }
.btn-lib-load { flex: 1; background: var(--primary-soft); color: var(--primary); border-color: transparent; font-size: .85rem; }
.btn-lib-load:hover { background: var(--primary); color: var(--on-primary); }
.btn-lib-delete {
  flex: 0 0 var(--tap);
  padding: 0;
  background: var(--surface-2);
  color: var(--fg-muted);
  border: 1px solid var(--border);
}
.btn-lib-delete:hover { background: var(--danger-soft); color: var(--danger); border-color: transparent; }

/* --------------------------------------------------------------------------
   17. DASHBOARD / CHARTS (Phase 4)
   -------------------------------------------------------------------------- */
.stats-cards-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: var(--s3);
  margin: var(--s4) 0;
}

.stat-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: var(--s5) var(--s4);
  text-align: center;
}
.stat-icon {
  display: grid; place-items: center;
  width: 40px; height: 40px;
  margin: 0 auto var(--s2);
  border-radius: 12px;
  background: var(--primary-soft);
  color: var(--primary);
  font-size: 1.05rem;
}
.stat-value {
  font-family: var(--font-display);
  font-size: clamp(1.4rem, 4vw, 2rem);
  font-weight: 700;
  line-height: 1.1;
  color: var(--fg);
}
.stat-label { font-size: .8rem; color: var(--fg-muted); margin-top: 4px; }

.stats-charts-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--s4);
  margin-top: var(--s4);
}

.stats-chart-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: var(--s5);
}
.stats-chart-card h3 {
  font-size: .96rem;
  margin-bottom: var(--s4);
  display: flex;
  align-items: center;
  gap: var(--s2);
}
.stats-chart-card h3 i { color: var(--primary); }

/* Horizontal bars */
.chart-bars { display: flex; flex-direction: column; gap: 10px; }
.bar-row { display: flex; align-items: center; gap: 10px; }
.bar-label {
  font-size: .82rem;
  color: var(--fg-muted);
  min-width: 82px;
  text-align: right;
  flex: 0 0 auto;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.bar-track {
  flex: 1 1 auto;
  height: 18px;
  background: var(--surface-3);
  border-radius: var(--r-full);
  overflow: hidden;
  min-width: 0;
}
/* one series = one hue, so bar length is the only thing carrying meaning */
.bar-fill {
  height: 100%;
  background: var(--primary);
  border-radius: var(--r-full);
  min-width: 4px;
  transition: width .7s var(--ease);
}
.bar-value {
  font-family: var(--font-display);
  font-size: .84rem;
  font-weight: 700;
  min-width: 28px;
  text-align: right;
  flex: 0 0 auto;
}

/* 7-day column chart */
.chart-columns {
  display: flex;
  align-items: stretch;
  gap: 8px;
  height: 168px;
  padding-top: var(--s2);
}
.col-item {
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  gap: 6px;
}
.col-track { width: 100%; flex: 1 1 auto; min-height: 0; display: flex; align-items: flex-end; }
.col-fill {
  position: relative;
  width: 100%;
  border-radius: 7px 7px 3px 3px;
  background: var(--primary);
  min-height: 4px;
  transition: height .7s var(--ease);
}
.col-fill[data-v="0"] { background: var(--surface-3); }
.col-cap {
  position: absolute;
  bottom: 100%;
  left: 0; right: 0;
  margin-bottom: 5px;
  text-align: center;
  font-family: var(--font-display);
  font-size: .74rem;
  font-weight: 700;
  color: var(--fg);
}
.col-fill[data-v="0"] .col-cap { color: var(--fg-subtle); }
.col-label { font-size: .7rem; color: var(--fg-subtle); white-space: nowrap; }

/* Donut */
.chart-donut-wrap { display: flex; align-items: center; gap: var(--s5); flex-wrap: wrap; }
.donut { flex: 0 0 auto; }
.donut-legend { display: flex; flex-direction: column; gap: 7px; font-size: .84rem; min-width: 0; }
.donut-legend span { display: flex; align-items: center; gap: 8px; color: var(--fg-muted); }
.legend-dot { width: 10px; height: 10px; border-radius: 3px; flex: 0 0 10px; }

/* Recent activity */
.recent-activity-list { max-height: 300px; overflow-y: auto; padding-right: 4px; }
.activity-item {
  display: flex;
  align-items: flex-start;
  gap: var(--s3);
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.activity-item:last-child { border-bottom: none; }
.activity-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--primary);
  margin-top: 8px;
  flex: 0 0 8px;
}
.activity-info { min-width: 0; }
.activity-title { font-size: .89rem; font-weight: 600; }
.activity-meta { font-size: .78rem; color: var(--fg-muted); margin-top: 2px; }

/* --------------------------------------------------------------------------
   18. PRICING
   -------------------------------------------------------------------------- */
.pricing-section { padding: var(--s10) var(--s5) var(--s16); max-width: 1100px; margin: 0 auto; }

.pricing-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(290px, 1fr));
  gap: var(--s6);
  margin-top: var(--s6);
  align-items: start;
}

.pricing-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  padding: var(--s8) var(--s6);
  position: relative;
  box-shadow: var(--sh-xs);
  transition: transform var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}
.card-featured {
  border-color: var(--gold-line);
  box-shadow: var(--sh-lg);
}
.pricing-card > .plan-ico {
  display: grid; place-items: center;
  width: 52px; height: 52px;
  border-radius: 15px;
  background: var(--surface-2);
  font-size: 1.3rem;
  margin-bottom: var(--s4);
}
.pricing-card h3 { font-size: 1.2rem; }
.pricing-card > p { color: var(--fg-muted); margin: var(--s3) 0; font-size: .92rem; }

.pricing-badge {
  position: absolute;
  top: -13px; left: 50%;
  transform: translateX(-50%);
  background: var(--gold);
  color: #fff;
  padding: 4px 14px;
  border-radius: var(--r-full);
  font-size: .74rem;
  font-weight: 700;
  white-space: nowrap;
  box-shadow: var(--sh-sm);
}

.price-tag {
  font-family: var(--font-display);
  font-size: 2.6rem;
  font-weight: 700;
  margin: var(--s3) 0 var(--s5);
  letter-spacing: -.02em;
}
.price-tag small { font-size: .95rem; font-weight: 500; color: var(--fg-muted); }
/* Space Grotesk has no ฿ glyph — render it from the Thai face with its own metrics */
.price-tag .baht {
  font-family: var(--font-ui);
  letter-spacing: normal;
  margin-right: .12em;
}

.feature-list { list-style: none; margin-bottom: var(--s6); display: flex; flex-direction: column; gap: 10px; }
.feature-list li { display: flex; gap: 10px; align-items: flex-start; font-size: .91rem; line-height: 1.55; }
.feature-list li > i { margin-top: 5px; flex: 0 0 auto; font-size: .82rem; }

.btn-price { width: 100%; }
.btn-price-featured { background: var(--gold); color: #fff; border-color: transparent; }
.btn-price-featured:hover { filter: brightness(1.06); }
.btn-price-outline { background: transparent; border-color: var(--border-strong); color: var(--fg); }
.btn-price-outline:hover { background: var(--surface-2); }

/* --------------------------------------------------------------------------
   19. FOOTER
   -------------------------------------------------------------------------- */
.footer {
  text-align: center;
  padding: var(--s8) var(--s5);
  border-top: 1px solid var(--border);
  color: var(--fg-muted);
  font-size: .88rem;
  margin-top: var(--s10);
}

/* --------------------------------------------------------------------------
   20. TOAST
   -------------------------------------------------------------------------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translate(-50%, 140%);
  z-index: 2000;
  display: flex;
  align-items: center;
  gap: var(--s3);
  max-width: min(92vw, 460px);
  padding: 13px var(--s5);
  border-radius: var(--r-md);
  background: var(--surface);
  color: var(--fg);
  border: 1px solid var(--border);
  box-shadow: var(--sh-lg);
  font-size: .92rem;
  font-weight: 500;
  opacity: 0;
  pointer-events: none;
  transition: transform .3s var(--ease), opacity .3s var(--ease);
}
.toast > i { color: var(--primary); font-size: 1.05rem; flex: 0 0 auto; }
.toast.show, .toast.active { transform: translate(-50%, 0); opacity: 1; }
.toast.is-error { border-color: color-mix(in srgb, var(--danger) 45%, transparent); }
.toast.is-error > i { color: var(--danger); }
.toast.is-warn > i { color: var(--warn); }

@media (min-width: 1025px) {
  .toast { left: auto; right: 24px; transform: translate(0, 140%); }
  .toast.show, .toast.active { transform: translate(0, 0); }
}

/* --------------------------------------------------------------------------
   21. ANIMATIONS
   -------------------------------------------------------------------------- */
@keyframes fade-up { from { opacity: 0; transform: translateY(16px); } to { opacity: 1; transform: none; } }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes pop-in { from { opacity: 0; transform: translateY(12px) scale(.98); } to { opacity: 1; transform: none; } }
@keyframes shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

.fade-up { animation: fade-up .5s var(--ease) both; }

/* --------------------------------------------------------------------------
   22. RESPONSIVE  (Phase 1 — the fix for the 375px horizontal scroll)
   -------------------------------------------------------------------------- */

/* Tablet / small laptop */
@media (max-width: 1180px) {
  .user-title { max-width: 15ch; }
  .user-plan { max-width: 16ch; }
  .btn-nav span.btn-txt { display: none; }
  .btn-nav { padding-inline: var(--s3); }
}

@media (max-width: 1024px) {
  .studio-grid { grid-template-columns: 1fr; }
  .controls-card { position: static; }
  .stats-charts-grid { grid-template-columns: 1fr; }
}

/* Tablet: the form goes full width, so pair the short fields up instead of
   making the user scroll past eight stacked rows to reach the generate button */
@media (min-width: 640px) and (max-width: 1024px) {
  .control-form {
    display: grid;
    grid-template-columns: 1fr 1fr;
    column-gap: var(--s5);
    align-items: start;
  }
  .control-form > .form-group:nth-child(-n+2),  /* license + shop name */
  .control-form > .form-group:nth-child(7),     /* theme picker */
  .control-form > #btnGenerate { grid-column: 1 / -1; }
}

/* Mobile / portrait-tablet: hamburger + bottom bar, everything else collapses */
@media (max-width: 1024px) {
  :root { --nav-h: 60px; }

  .hide-desktop, .hide-desktop.btn-hamburger { display: inline-grid; }
  .hide-mobile { display: none !important; }

  .navbar { padding: var(--s2) var(--s4); gap: var(--s2); }
  .navbar .user-role-bar,
  .navbar .nav-actions { display: none !important; }

  .bottombar { display: flex; }

  /* keep content clear of the fixed bottom bar */
  body { padding-bottom: calc(var(--bottombar-h) + env(safe-area-inset-bottom)); }
  .toast { bottom: calc(var(--bottombar-h) + 16px + env(safe-area-inset-bottom)); }

  .hero-section { padding: var(--s10) var(--s4) var(--s8); }
  .hero-cta { flex-direction: column; }
  .hero-cta .btn-accent, .hero-cta .btn-ghost { width: 100%; }

  .studio-section, .pricing-section { padding-inline: var(--s4); }
  .studio-section { padding-top: var(--s6); }

  .controls-card { padding: var(--s5) var(--s4); border-radius: var(--r-lg); }
  .preview-card { border-radius: var(--r-lg); }
  .preview-toolbar { padding: var(--s3) var(--s4); }
  .toolbar-row { flex-direction: column; align-items: stretch; }
  .preview-tabs { width: 100%; justify-content: space-between; }
  .tab-btn { flex: 1 1 0; justify-content: center; padding-inline: var(--s2); font-size: .8rem; }
  .tab-btn .tab-txt { display: none; }
  .tab-short { display: inline; }
  .toolbar-label { display: none; }
  .export-buttons { display: grid; grid-template-columns: 1fr 1fr; width: 100%; }
  .btn-export { width: 100%; }

  .paper-wrapper { padding: var(--s3); }
  .empty-state { min-height: 260px; padding: var(--s8) var(--s4); }

  .stats-cards-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .modal-card { padding: var(--s5) var(--s4); border-radius: var(--r-lg); max-height: 92vh; }
  .role-options { grid-template-columns: 1fr; }
  .modal-actions { flex-direction: column-reverse; }
  .modal-actions > button { width: 100%; }

  .card-featured { transform: none; }
  .library-grid { grid-template-columns: 1fr; }

  /* ⛔ ห้ามใส่กฎที่เปลี่ยน "หน้าตาในกระดาษ" (.match-grid / .choice-grid / .trace-grid ฯลฯ)
     ลงใน media query ที่วัดจากความกว้างจอ
     กระดาษเป็นเอกสารกว้าง 210mm ตายตัว และถูกย่อให้พอดีจอด้วย transform อยู่แล้ว
     (scalePaperToViewport) การบีบให้เหลือคอลัมน์เดียวจึงไม่ได้ช่วยอะไร
     แต่ทำให้สิ่งที่ครูเห็นบนมือถือ "ไม่ตรงกับ PDF ที่ดาวน์โหลดไป"
     (PDF แปลงที่เซิร์ฟเวอร์ด้วยหน้าจอกว้าง จึงได้ 2 คอลัมน์เสมอ)
     — เคยมีกฎ .match-grid/.choice-grid/.trace-grid { grid-template-columns: 1fr } ตรงนี้
       ใบงานจับคู่บนมือถือจึงโชว์คอลัมน์ A/B ซ้อนกัน แต่พิมพ์ออกมาอยู่ข้างกัน */
}

@media (max-width: 420px) {
  .brand-name { font-size: .95rem; max-width: 42vw; }
  .badge-role { font-size: .6rem; }
  .theme-picker { grid-template-columns: repeat(3, 1fr); }
  .bottombar button { font-size: .64rem; }
}

/* --------------------------------------------------------------------------
   23. PRINT — what you see is exactly what prints (Phase 4)
   -------------------------------------------------------------------------- */
@media print {
  @page { size: A4 portrait; margin: 0; }

  html, body {
    overflow: visible !important;
    background: #fff !important;
    padding: 0 !important;
    margin: 0 !important;
  }

  body * { visibility: hidden !important; }

  .paper-wrapper, .paper-wrapper * { visibility: visible !important; }

  .paper-wrapper {
    position: absolute !important;
    left: 0; top: 0;
    width: 210mm !important;
    padding: 0 !important;
    margin: 0 !important;
    background: #fff !important;
    display: block !important;
    overflow: visible !important;
    min-height: 0 !important;
    border: none !important;
  }

  /* fitPaperToStage() sets an inline scale + negative margins on screen — undo both */
  .paper-stage {
    transform: none !important;
    width: 210mm !important;
    margin: 0 !important;
  }

  .a4-sheet {
    width: 210mm !important;
    min-height: 297mm !important;
    box-shadow: none !important;
    border-radius: 0 !important;
    margin: 0 !important;
    page-break-after: avoid;
  }

  /* never print the loading skeleton or empty state */
  .illustration-shimmer, .sk-line, .sk-block, .sk-note, .empty-state { display: none !important; }

  .navbar, .bottombar, .drawer, .drawer-scrim, .toast,
  .modal-overlay, .footer, .pricing-section, .hero-section,
  .controls-card, .preview-toolbar { display: none !important; }
}

/* --------------------------------------------------------------------------
   COLORING WORKSHEET TYPE
   -------------------------------------------------------------------------- */
.coloring-worksheet {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 8px 0;
}

.coloring-image-area {
  display: flex;
  justify-content: center;
  align-items: center;
}

.coloring-main-image {
  width: 100%;
  max-width: 100%;
  margin: 0 auto;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--clr-border, #eee);
  background: #fff;
  min-height: 580px;
  max-height: 750px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.coloring-main-image img {
  width: 100%;
  height: auto;
  display: block;
  object-fit: contain;
}

.coloring-image-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  color: #aaa;
  font-size: 1.1rem;
  padding: 40px 20px;
  animation: coloring-pulse 2s ease-in-out infinite;
}

.coloring-image-placeholder i {
  font-size: 3rem;
  color: #ccc;
}

@keyframes coloring-pulse {
  0%, 100% { opacity: 0.5; }
  50% { opacity: 1; }
}

.coloring-instructions {
  padding: 12px 16px;
  background: var(--clr-surface-2, #f8f9fa);
  border-radius: 12px;
  border: 1px solid var(--clr-border, #e0e0e0);
}

.coloring-instructions h4 {
  margin: 0 0 12px 0;
  font-size: 1rem;
  font-weight: 700;
  color: var(--clr-text, #333);
  display: flex;
  align-items: center;
  gap: 8px;
}

.coloring-instructions h4 i {
  color: #e67e22;
}

.coloring-items {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.coloring-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: var(--clr-surface, #fff);
  border-radius: 8px;
  border: 1px solid var(--clr-border, #eee);
  font-size: 0.95rem;
}

.coloring-number {
  font-weight: 700;
  color: var(--clr-primary, #4f46e5);
  min-width: 24px;
}

.coloring-text {
  flex: 1;
  color: var(--clr-text, #333);
}

.coloring-color-badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 20px;
  color: #fff;
  font-size: 0.8rem;
  font-weight: 600;
  min-width: 40px;
  text-align: center;
  text-shadow: 0 1px 2px rgba(0,0,0,0.3);
  border: 1px solid rgba(0,0,0,0.1);
}

/* Answer view for coloring */
.answer-view .coloring-item {
  background: #f0faf0;
  border-color: #c8e6c9;
}

.answer-view .coloring-item::after {
  content: '✓';
  color: #27ae60;
  font-weight: bold;
  font-size: 1.1rem;
}

/* Watermark overlay for preview images */
.watermark-overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 100;
}
.watermark-text {
  font-size: 80px;
  font-weight: 900;
  color: rgba(255, 0, 0, 0.15);
  transform: rotate(-35deg);
  text-transform: uppercase;
  letter-spacing: 20px;
  white-space: nowrap;
  user-select: none;
  font-family: 'Arial Black', sans-serif;
}

/* --------------------------------------------------------------------------
   ใบงานที่ยาวเกิน A4 หน้าเดียว
   ครูต้องเห็นเส้นแบ่งหน้าบนจอ ตรงกับที่จะออกมาตอนปรินต์จริง
   (เดิมกระดาษยืดยาวเป็นแผ่นเดียว ครูไม่รู้ว่าจะได้ 2 หน้าจนกดโหลด PDF)
   -------------------------------------------------------------------------- */
.a4-sheet.is-multipage {
  min-height: calc(297mm * var(--sheet-pages, 1));
  background-image: repeating-linear-gradient(
    to bottom,
    transparent 0,
    transparent calc(297mm - 2px),
    #94a3b8 calc(297mm - 2px),
    #94a3b8 297mm
  );
  background-origin: border-box;
  background-clip: border-box;
}

.paper-page-count {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 12px;
  border-radius: 999px;
  background: #fef3c7;
  color: #92400e;
  font-size: .82rem;
  font-weight: 600;
}

/* ---- การ์ดชวนสร้างใบต่อไป (ใต้ใบงาน) ----
   วางใต้กระดาษเพราะเป็นจุดที่ครูสายตาไปถึงพอดีหลังดูใบงานเสร็จ
   เดิมตรงนี้เป็นที่ว่างแล้วต่อด้วยป้ายลิขสิทธิ์ ครูจึงจบการใช้งานตรงนั้น

   ⚠️ #paperWrapper เป็น flex แนวนอน ถ้าไม่บังคับ flex-basis:100%
      การ์ดจะไปยืนข้างกระดาษ แล้วเบียดกระดาษจนตกขอบจอมือถือ (เจอจริงตอนทดสอบ)
      จึงต้องวางเป็นพี่น้องของกรอบกระดาษใน .preview-card ซึ่งเรียงแนวตั้งอยู่แล้ว */
.next-step-card {
  width: 100%;
  margin: 18px auto 8px;
  max-width: 520px;
  padding: 20px 18px;
  border: 1px solid var(--border, #e5e7eb);
  border-radius: 16px;
  background: var(--card, #fff);
  text-align: center;
}
.next-step-card h4 {
  margin: 0 0 6px;
  font-size: 1.05rem;
  color: var(--text, #0f172a);
}
.next-step-card h4 i { color: #0F766E; margin-right: 6px; }
.next-step-card .ns-left {
  margin: 0 0 4px;
  font-size: 1rem;
  color: var(--text, #0f172a);
}
.next-step-card .ns-left b { color: #0F766E; }
.next-step-card .ns-hint {
  margin: 0 0 14px;
  font-size: .86rem;
  color: var(--muted, #64748b);
}
.next-step-card .ns-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}
.next-step-card .ns-actions .btn-accent { flex: 1 1 190px; }
.next-step-card .ns-ghost {
  flex: 1 1 150px;
  padding: 12px 16px;
  border: 1px solid var(--border, #e5e7eb);
  border-radius: 12px;
  background: transparent;
  color: var(--text, #0f172a);
  font: inherit;
  cursor: pointer;
}
.next-step-card .ns-ghost:hover { background: var(--hover, #f1f5f9); }

/* ห้ามติดไปกับกระดาษตอนสั่งพิมพ์หรือส่งออก PDF */
@media print { .next-step-card { display: none !important; } }

/* ตราท้ายกระดาษตอน "ยังไม่ได้ตรวจ" — ต้องเห็นชัดว่าต่างจากใบที่ตรวจผ่าน
   แต่ห้ามแดงจัดจนดูเหมือนใบเสีย เพราะเนื้อหาอาจถูกอยู่ ครูแค่ต้องตรวจเอง */
.sheet-footer #paperVerifyBadge.is-unverified {
  color: #b45309;
  font-weight: 600;
}

/* แถบบอกผลตรวจของ "ใบงานภาพอนุบาล" — วางนอกกระดาษโดยตั้งใจ
   ห้ามย้ายเข้าไปในกระดาษ: ภาพคือตัวใบงานทั้งใบ ทับแล้วบังโจทย์เด็ก
   และจะติดไปในไฟล์ PDF ที่ครูปรินต์แจกด้วย (คนที่ต้องอ่านคือครู ไม่ใช่เด็ก) */
.kid-verify-notice {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 14px;
  line-height: 1.5;
}
.kid-verify-notice.is-ok {
  background: color-mix(in srgb, #16a34a 10%, transparent);
  color: #15803d;
}
/* เตือนแบบส้ม ไม่ใช่แดงจัด — เนื้อหาอาจถูกอยู่ ครูแค่ต้องตรวจเอง (กฎเดียวกับตราใบข้อความ) */
.kid-verify-notice.is-unverified {
  background: color-mix(in srgb, #f59e0b 14%, transparent);
  color: #b45309;
  font-weight: 600;
}
@media print { .kid-verify-notice { display: none !important; } }
