@property --p { syntax: '<number>'; inherits: false; initial-value: 0; }

/* ธีมกลาง "MTC Advisor" — โหลดหลัง <style> ของแต่ละหน้าเพื่อทับสไตล์เดิม
   สีหลักเปลี่ยนตามบทบาทด้วย class บน <body>: role-student / role-teacher / role-admin */

:root {
  --ink: #2b2d42;
  --ink-soft: #6b6f86;
  --line: #ece8f4;
  --paper: #ffffff;
  --radius: 20px;
  --radius-sm: 12px;
  --shadow: 0 2px 4px rgba(60, 40, 120, .04), 0 10px 30px rgba(60, 40, 120, .08);
  --shadow-hover: 0 4px 8px rgba(60, 40, 120, .06), 0 18px 40px rgba(60, 40, 120, .14);
  --font-body: 'IBM Plex Sans Thai Looped', 'Sarabun', system-ui, sans-serif;
  --font-fun: 'Mali', 'IBM Plex Sans Thai Looped', system-ui, sans-serif;

  /* ค่าเริ่มต้น (หน้า login) */
  --accent: #7c5cff;
  --accent-deep: #5b3fe0;
  --accent-soft: #efeaff;
  --accent-2: #ff7eb6;
  --bg-a: #f3efff;
  --bg-b: #ffeef6;
  --owl: #9b87ff;
  --owl-belly: #efeaff;
}
body.role-student {
  --accent: #8b5cf6; --accent-deep: #6d3fe0; --accent-soft: #f1ebff; --accent-2: #f472b6;
  --bg-a: #f4efff; --bg-b: #ffeef7; --owl: #a78bfa; --owl-belly: #f3eeff;
}
body.role-teacher {
  --accent: #10b981; --accent-deep: #07966a; --accent-soft: #e3f8ef; --accent-2: #38bdf8;
  --bg-a: #eafaf3; --bg-b: #e9f6ff; --owl: #34d399; --owl-belly: #e6fbf2;
}
body.role-admin {
  --accent: #3b82f6; --accent-deep: #1f63d8; --accent-soft: #e6efff; --accent-2: #a78bfa;
  --bg-a: #ecf3ff; --bg-b: #fff4e6; --owl: #60a5fa; --owl-belly: #e8f1ff;
}

/* สีเดิมของหน้าย่อย (--primary, --text, ...) ให้ใช้สีของธีม */
body[class*="role-"] {
  --primary: var(--accent); --primary-dark: var(--accent-deep); --primary-light: var(--accent-soft);
  --text: var(--ink); --muted: var(--ink-soft); --text-muted: var(--ink-soft);
  --border: var(--line); --card: #fff; --bg: transparent;
}

/* ---------- พื้นฐาน ---------- */
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: var(--font-body) !important;
  color: var(--ink);
  background-color: var(--bg-a) !important;
  background-image:
    radial-gradient(circle, rgba(124, 92, 255, .07) 1.5px, transparent 1.6px),
    linear-gradient(160deg, var(--bg-a) 0%, var(--bg-b) 100%) !important;
  background-size: 22px 22px, auto !important;
  background-attachment: fixed !important;
  min-height: 100vh;
}
h1, h2, h3, h4 { font-family: var(--font-fun); letter-spacing: 0; }
h3 { color: var(--ink); }
h4 { color: var(--accent-deep) !important; border-bottom-color: var(--accent-soft) !important; }
a { color: var(--accent-deep); }
::selection { background: var(--accent-soft); }

/* ---------- แถบบน ---------- */
.top-nav {
  position: sticky; top: 0; z-index: 100;
  background: rgba(255, 255, 255, .82) !important;
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
  color: var(--ink) !important;
  padding: 10px 18px !important;
  min-height: 60px; height: auto !important;
  box-shadow: 0 1px 0 var(--line), 0 6px 20px rgba(60, 40, 120, .06) !important;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.top-nav #u-info {
  margin-right: auto;
  display: flex; align-items: center; gap: 10px;
  font-family: var(--font-fun); font-weight: 600 !important; font-size: 15px !important;
  min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.top-nav #u-info::before {
  content: "🦉";
  flex: 0 0 auto;
  display: inline-grid; place-items: center;
  width: 36px; height: 36px; border-radius: 12px;
  background: var(--accent-soft); font-size: 20px;
}
.top-nav .fa-bell { color: var(--accent-deep); }
.top-nav > div:last-child { flex: 0 0 auto; }
.top-nav button {
  background: var(--paper) !important; color: var(--ink-soft) !important;
  border: 1.5px solid var(--line) !important; box-shadow: none !important;
  padding: 8px 14px !important; border-radius: 999px !important; font-size: 14px !important;
}
.top-nav button:hover { color: #e11d48 !important; border-color: #fecdd3 !important; background: #fff1f2 !important; }

/* ---------- แถบบนของหน้าย่อย + ปุ่มกลับหน้าหลัก (อยู่มุมซ้ายบนเหมือนกันทุกหน้า) ---------- */
.app-bar {
  position: sticky; top: 0; z-index: 100;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  min-height: 60px; padding: 10px 18px !important; box-sizing: border-box;
  background: rgba(255, 255, 255, .86);
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
  box-shadow: 0 1px 0 var(--line), 0 6px 20px rgba(60, 40, 120, .06);
  color: var(--ink);
}
.home-btn, .top-nav .home-btn {
  order: -1; flex: 0 0 auto; align-self: flex-start;
  display: inline-flex; align-items: center; gap: 6px; box-sizing: border-box;
  width: auto !important; height: 40px; margin: 0 !important;
  padding: 0 16px !important; border: 0 !important; border-radius: 999px !important;
  background: var(--accent) !important; color: #fff !important;
  font-family: var(--font-fun); font-weight: 700; font-size: 15px !important; line-height: 1.2;
  text-decoration: none; cursor: pointer; white-space: nowrap;
  box-shadow: 0 4px 12px rgba(0, 0, 0, .12);
}
.home-btn:hover, .top-nav .home-btn:hover { filter: brightness(1.06); transform: translateY(-1px); color: #fff !important; }
.app-title {
  flex: 1 1 200px; min-width: 0;
  font-family: var(--font-fun); font-weight: 700; font-size: 17px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.app-user { font-size: 13px; color: var(--ink-soft); white-space: nowrap; }
.app-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.app-actions select { width: auto; margin: 0; }

/* หน้าหลักที่มีหลายมุมมองในไฟล์เดียว: ปุ่มหน้าหลักอยู่ในแถบบน แทนปุ่ม "กลับ" ที่เคยอยู่คนละที่ในแต่ละมุมมอง */
body.on-dash:not(.delegated) .top-nav .home-btn { display: none; }
body.has-home-btn .btn-back[onclick="showV('dash')"] { display: none !important; }

@media print {
  .app-bar, .top-nav, .home-btn { display: none !important; }
  body { background: #fff !important; }
}

/* ---------- มอบหมายงาน ---------- */
.perm-filters { display: grid; grid-template-columns: 1fr 200px 230px; gap: 10px; }
.perm-filters input, .perm-filters select { margin: 0 !important; }
.perm-summary { margin: 12px 2px 8px; font-size: 13.5px; color: var(--ink-soft); }
.perm-table { width: 100%; }
.perm-table td { vertical-align: middle; padding: 10px 12px; }
.perm-num { color: var(--ink-soft); font-size: 13px; }
.perm-user { font-size: 12.5px; color: var(--ink-soft); }
.perm-badge { display: inline-block; margin: 2px 4px 2px 0; padding: 2px 9px; border-radius: 999px; font-size: 12.5px; background: var(--accent-soft); color: var(--accent-deep); font-weight: 600; }
.perm-more { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12.5px; background: #eef1f6; color: var(--ink-soft); }
.perm-none { color: #b8bccb; }
.perm-btn { width: auto !important; margin: 0 !important; padding: 7px 14px !important; white-space: nowrap; font-size: 13.5px !important; border-radius: 999px !important; }
.perm-btn-ghost { width: auto !important; margin: 0 !important; padding: 7px 14px !important; background: #fff !important; color: var(--ink) !important; border: 1.5px solid var(--line) !important; border-radius: 999px !important; }
.perm-btn-ghost:disabled { opacity: .45; cursor: default; }
.perm-pager { display: flex; justify-content: center; align-items: center; gap: 12px; margin-top: 14px; font-size: 14px; color: var(--ink-soft); }
.perm-pager:empty { display: none; }
.perm-dialog { margin: auto !important; max-height: calc(100vh - 32px); overflow: auto; border: 0; border-radius: 24px; padding: 22px 24px 18px; width: min(440px, calc(100vw - 32px)); box-shadow: 0 30px 80px rgba(30, 20, 80, .3); }
.perm-dialog::backdrop { background: rgba(30, 30, 60, .35); -webkit-backdrop-filter: blur(2px); backdrop-filter: blur(2px); }
.perm-dlg-list { display: grid; gap: 6px; }
.perm-opt { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border: 1.5px solid var(--line); border-radius: 14px; cursor: pointer; font-size: 14.5px; }
.perm-opt input { width: 18px !important; height: 18px; margin: 0 !important; flex: 0 0 auto; }
.perm-opt:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); color: var(--accent-deep); font-weight: 600; }
.perm-dlg-tools { margin: 10px 2px 0; font-size: 13px; color: var(--ink-soft); }
.perm-dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.perm-dlg-actions button[type=submit] { width: auto !important; margin: 0 !important; padding: 9px 18px !important; border-radius: 999px !important; }

/* ---------- ทะเบียนกลุ่มเรียน ---------- */
.cls-add { display: grid; grid-template-columns: 130px 110px 110px 1fr auto; gap: 10px; align-items: center; }
.cls-add input, .cls-add select { margin: 0 !important; }
.cls-legacy-box { margin-top: 16px; padding: 14px 16px; border-radius: 16px; background: #fff8e6; border: 1.5px solid #fde3a7; font-size: 14px; }

/* ---------- บัตรเปิดใช้บัญชี ---------- */
.act-dialog { width: min(560px, calc(100vw - 32px)); }
.act-note { font-size: 13px; line-height: 1.5; background: var(--accent-soft); border-radius: 14px; padding: 10px 12px; color: var(--ink); }
.act-list { max-height: 46vh; overflow: auto; margin-top: 6px; }
.act-row .perm-user { display: block; }
.act-chip { flex: 0 0 auto; font-size: 12px; font-weight: 600; border-radius: 999px; padding: 3px 10px; white-space: nowrap; }
@media (max-width: 560px) { .act-row { flex-wrap: wrap; } .act-chip { margin-left: 28px; } }

/* ---------- การ์ด ปุ่ม ช่องกรอก ตาราง ---------- */
.container { box-sizing: border-box; }
.card:has(> .card-header) { overflow: hidden; }
.card {
  background: var(--paper) !important;
  border-radius: var(--radius) !important;
  border: 1px solid var(--line);
  box-shadow: var(--shadow) !important;
}
button { font-family: var(--font-body); border-radius: var(--radius-sm) !important; transition: transform .15s, box-shadow .15s, filter .15s, background .15s; }
button:not(:disabled):hover { filter: brightness(1.05); transform: translateY(-1px); }
button:not(:disabled):active { transform: translateY(1px); }
.btn-back {
  background: var(--paper) !important; color: var(--accent-deep) !important;
  border: 1.5px solid var(--accent-soft) !important;
  padding: 8px 16px !important; border-radius: 999px !important; font-weight: 600;
}
input, select, textarea {
  font-family: var(--font-body) !important;
  border-radius: var(--radius-sm) !important;
  border: 1.5px solid var(--line) !important;
  background-color: #fff;
  transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--accent) !important;
  box-shadow: 0 0 0 4px var(--accent-soft) !important;
}
input[type="checkbox"], input[type="radio"] { accent-color: var(--accent); box-shadow: none !important; }
table { border-radius: var(--radius-sm); overflow: hidden; }
th { background: var(--accent-soft) !important; color: var(--accent-deep) !important; font-weight: 700; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---------- หน้าแรก: การ์ดทักทาย ---------- */
.hero {
  position: relative; overflow: hidden;
  display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 12px;
  padding: 26px 28px; margin-bottom: 22px;
  border-radius: 28px;
  color: #fff;
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-2) 100%);
  box-shadow: 0 14px 40px rgba(60, 40, 120, .18);
}
.hero::before, .hero::after {
  content: ""; position: absolute; border-radius: 50%; background: rgba(255, 255, 255, .14); pointer-events: none;
}
.hero::before { width: 220px; height: 220px; right: -60px; top: -80px; }
.hero::after { width: 120px; height: 120px; left: 38%; bottom: -70px; }
.hero-greet { font-size: 15px; opacity: .95; }
.hero-name {
  font-family: var(--font-fun); font-size: clamp(22px, 4.6vw, 30px); font-weight: 700;
  margin: 2px 0 8px; line-height: 1.25; color: #fff;
}
.hero-quote {
  display: inline-block; margin: 0; padding: 6px 14px;
  background: rgba(255, 255, 255, .2); border-radius: 999px;
  font-size: 14px; line-height: 1.5;
}
.hero-art { width: 132px; height: 132px; position: relative; z-index: 1; }
.hero-art svg { width: 100%; height: 100%; display: block; overflow: visible; }
.hero-chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  width: auto; margin: 0; padding: 8px 14px; border-radius: 999px; cursor: pointer;
  background: #fff; color: var(--accent-deep);
  font-weight: 600; font-size: 14px; border: 0;
  box-shadow: 0 4px 12px rgba(0, 0, 0, .08);
  transition: transform .15s;
}
.chip:hover { transform: translateY(-2px); }

/* ---------- สรุปตัวเลขเล็ก ๆ ---------- */
.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 22px; }
.stat {
  background: var(--paper); border: 1px solid var(--line); border-radius: 18px;
  padding: 14px 16px; box-shadow: var(--shadow);
  display: flex; align-items: center; gap: 12px;
}
.stat-emoji { font-size: 26px; width: 46px; height: 46px; border-radius: 14px; display: grid; place-items: center; background: var(--accent-soft); flex: 0 0 auto; }
.stat b { display: block; font-family: var(--font-fun); font-size: 22px; line-height: 1.1; color: var(--ink); }
.stat span { font-size: 13px; color: var(--ink-soft); }

/* ---------- ภารกิจ (นักเรียน) ---------- */
.quest {
  background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px 20px; margin-bottom: 22px;
  display: grid; grid-template-columns: auto 1fr; gap: 18px; align-items: center;
}
.ring { --p: 0; width: 86px; height: 86px; border-radius: 50%; display: grid; place-items: center;
  background: conic-gradient(var(--accent) calc(var(--p) * 1%), var(--accent-soft) 0); transition: --p .8s; }
.ring > div { width: 66px; height: 66px; border-radius: 50%; background: #fff; display: grid; place-items: center;
  font-family: var(--font-fun); font-weight: 700; font-size: 19px; color: var(--accent-deep); }
.quest h3 { margin: 0 0 4px; font-size: 18px; }
.quest p { margin: 0; color: var(--ink-soft); font-size: 14px; }
.quest .chip { background: var(--accent); color: #fff; margin-top: 10px; box-shadow: none; }

/* ---------- เมนูแบบการ์ด ---------- */
.dash-title {
  display: flex; align-items: center; gap: 8px;
  font-family: var(--font-fun); font-weight: 700; font-size: 17px; color: var(--ink);
  margin: 6px 4px 12px;
}
.tile-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(165px, 1fr)); gap: 14px; margin-bottom: 22px; }
.tile {
  --tile: var(--accent); --tile-bg: var(--accent-soft);
  position: relative; cursor: pointer; user-select: none;
  background: var(--paper); border: 1px solid var(--line); border-radius: 22px;
  padding: 18px 14px 16px; text-align: center;
  box-shadow: var(--shadow);
  transition: transform .18s cubic-bezier(.3, 1.6, .5, 1), box-shadow .18s, border-color .18s;
}
.tile:hover { transform: translateY(-5px) rotate(-.6deg); box-shadow: var(--shadow-hover); border-color: var(--tile-bg); }
.tile:active { transform: translateY(-1px) scale(.98); }
.tile-emoji {
  width: 64px; height: 64px; margin: 0 auto 10px; border-radius: 22px;
  display: grid; place-items: center; font-size: 32px; line-height: 1;
  background: var(--tile-bg);
  transition: transform .25s cubic-bezier(.3, 1.8, .5, 1);
}
.tile:hover .tile-emoji { transform: scale(1.1) rotate(-8deg); }
.tile-title { font-family: var(--font-fun); font-weight: 700; font-size: 15px; color: var(--ink); line-height: 1.35; }
.tile-sub { font-size: 12.5px; color: var(--ink-soft); margin-top: 3px; line-height: 1.45; }
.tile-step {
  position: absolute; top: 10px; left: 10px;
  font-size: 11px; font-weight: 700; color: var(--tile); background: var(--tile-bg);
  padding: 2px 8px; border-radius: 999px;
}
.tile-badge {
  position: absolute; top: 10px; right: 10px;
  min-width: 22px; height: 22px; padding: 0 7px; border-radius: 999px;
  background: #ff4d79; color: #fff; font-size: 12px; font-weight: 700;
  display: none; align-items: center; justify-content: center;
  box-shadow: 0 3px 8px rgba(255, 77, 121, .4);
  animation: pop .5s cubic-bezier(.3, 1.8, .5, 1);
}
.tile-highlight { border: 2px dashed var(--tile); }

/* สีของแต่ละการ์ด */
.t-blue   { --tile: #3b82f6; --tile-bg: #e3edff; }
.t-yellow { --tile: #d99a00; --tile-bg: #fff4cc; }
.t-green  { --tile: #16a34a; --tile-bg: #dcf8e6; }
.t-purple { --tile: #8b5cf6; --tile-bg: #efe8ff; }
.t-pink   { --tile: #e0457b; --tile-bg: #ffe4ef; }
.t-orange { --tile: #ea6a0c; --tile-bg: #ffe9d6; }
.t-sky    { --tile: #0891b2; --tile-bg: #dcf5fb; }
.t-red    { --tile: #e11d48; --tile-bg: #ffe4e8; }
.t-gray   { --tile: #64748b; --tile-bg: #eef1f6; }

/* ---------- มาสคอตนกฮูก ---------- */
.owl-body { fill: var(--owl); }
.owl-belly { fill: var(--owl-belly); }
.owl-lid { fill: var(--owl); transform-box: fill-box; transform-origin: center top; transform: scaleY(0); animation: blink 5s infinite; }
.owl-wing { fill: var(--owl); filter: brightness(.9); transform-box: fill-box; }
.owl-wing-r { transform-origin: 20% 20%; animation: wave 3.2s ease-in-out infinite; }
.owl-float { animation: float 4s ease-in-out infinite; }

@keyframes blink { 0%, 92%, 100% { transform: scaleY(0); } 95% { transform: scaleY(1); } }
@keyframes wave { 0%, 60%, 100% { transform: rotate(0); } 70% { transform: rotate(-28deg); } 80% { transform: rotate(-8deg); } 90% { transform: rotate(-24deg); } }
@keyframes float { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-6px); } }
@keyframes pop { 0% { transform: scale(0); } 100% { transform: scale(1); } }
@keyframes rise { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }

#v-dash > * { animation: rise .45s ease both; }
#v-dash > *:nth-child(2) { animation-delay: .05s; }
#v-dash > *:nth-child(3) { animation-delay: .1s; }
#v-dash > *:nth-child(n+4) { animation-delay: .15s; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* ---------- มือถือ ---------- */
@media (max-width: 640px) {
  .container { padding: 0 14px !important; max-width: 100% !important; margin: 16px auto !important; }
  .hero { grid-template-columns: 1fr; padding: 22px 20px 20px; }
  .hero-art { position: absolute; right: 8px; top: 10px; width: 84px; height: 84px; opacity: .95; }
  .hero-text { padding-right: 84px; }
  .hero-quote { font-size: 13px; }
  .tile-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .tile { padding: 16px 10px 14px; }
  .tile-emoji { width: 56px; height: 56px; font-size: 28px; border-radius: 18px; }
  .quest { grid-template-columns: 1fr; text-align: center; justify-items: center; }
  .perm-filters { grid-template-columns: 1fr; }
  .cls-add { grid-template-columns: 1fr 1fr; }
  .perm-badge, .perm-more { white-space: nowrap; font-size: 11.5px; padding: 1px 7px; }
  .perm-table td { padding: 9px 8px; }
  .perm-btn { padding: 6px 10px !important; font-size: 12.5px !important; }
  .perm-table th:nth-child(1), .perm-table td:nth-child(1), .perm-table th:nth-child(3), .perm-table td:nth-child(3) { display: none; }
  .top-nav #u-info { font-size: 13px !important; }
  .app-bar, .top-nav { padding: 10px 14px !important; gap: 8px; }
  .app-title { font-size: 15px; flex-basis: 0; }
  .app-user { display: none; }
  .app-actions { width: 100%; }
  .home-btn, .top-nav .home-btn { padding: 0 12px !important; font-size: 14px !important; }

  /* หน้าย่อยเดิม: ฟอร์มหลายคอลัมน์เรียงเป็นคอลัมน์เดียว ตารางเลื่อนซ้าย-ขวาได้ */
  .container [style*="grid-template-columns"] { grid-template-columns: 1fr !important; }
  .container table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .container img { max-width: 100%; height: auto; }

  .page-wrap, .wrap { padding-left: 12px !important; padding-right: 12px !important; max-width: 100% !important; box-sizing: border-box; }
  .page-wrap [style*="grid-template-columns"], .wrap [style*="grid-template-columns"] { grid-template-columns: 1fr !important; }
  .stat-grid { grid-template-columns: repeat(2, 1fr) !important; }
  .chart-grid, .info-grid, .form-grid { grid-template-columns: 1fr !important; }
  .form-group, .stat-card { min-width: 0; }
  .page-wrap table, .wrap table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .tabs { display: flex; overflow-x: auto; }
  .tab-btn { flex: 0 0 auto; }
}
.class-hint { font-size: 11.5px; color: var(--ink-soft); margin-top: 4px; }
