* { box-sizing: border-box; }
:root {
  --blue: #4f46e5; --blue-dark: #4338ca; --violet: #7c3aed; --pink: #db2777;
  --green: #16a34a; --red: #dc2626;
  --grad: linear-gradient(135deg, #4f46e5, #7c3aed 55%, #db2777);
  --bg: #f5f4ff; --card: #ffffff; --text: #1e1b2e; --muted: #675e80; --border: #e6e1f7;
  font-family: "Sarabun", "Noto Sans Thai", system-ui, sans-serif;
}
body {
  margin: 0; color: var(--text); min-height: 100vh;
  background:
    radial-gradient(600px 400px at 8% -5%, rgba(124, 58, 237, .13), transparent),
    radial-gradient(560px 380px at 100% 0%, rgba(219, 39, 119, .10), transparent),
    var(--bg);
  background-attachment: fixed;
}
.hidden { display: none !important; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  background: rgba(255,255,255,.85); backdrop-filter: blur(8px);
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.brand {
  font-weight: 800; font-size: 18px; display: flex; align-items: center; gap: 6px;
  background: var(--grad); -webkit-background-clip: text; background-clip: text; color: transparent;
}
.brandIcon { -webkit-text-fill-color: initial; }
.brandAi {
  font-size: 11px; font-weight: 800; padding: 2px 7px; border-radius: 999px;
  background: var(--grad); color: #fff; -webkit-text-fill-color: #fff; letter-spacing: .5px;
}
.nav { display: flex; align-items: center; gap: 8px; }
.navbtn {
  background: none; border: none; padding: 8px 12px; border-radius: 8px; font-size: 15px;
  cursor: pointer; color: var(--text);
}
.navbtn:hover { background: var(--bg); }
.whoami { color: var(--muted); font-size: 14px; margin: 0 4px; }

main { max-width: 820px; margin: 0 auto; padding: 16px; }
.view { display: none; }
.view.active { display: block; }

.card {
  background: var(--card); border: 1px solid var(--border); border-radius: 20px;
  padding: 22px; margin-bottom: 16px; box-shadow: 0 8px 30px -12px rgba(79, 70, 229, .15);
}
.card.narrow { max-width: 380px; margin: 60px auto; }
.card.hero { padding-bottom: 8px; }

h1 { font-size: 22px; margin: 0 0 12px; }
h1 .sparkle { filter: drop-shadow(0 0 6px rgba(124, 58, 237, .5)); }
h2 { font-size: 17px; margin: 20px 0 8px; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: 14px; margin-top: 16px; }

form label { display: block; margin-bottom: 12px; font-size: 14px; color: var(--muted); }
form input, form select, form textarea {
  display: block; width: 100%; margin-top: 6px; padding: 11px 12px; font-size: 15px;
  border: 1px solid var(--border); border-radius: 10px; font-family: inherit; background: #fbfaff; color: var(--text);
}
form input:focus, form select:focus, form textarea:focus {
  outline: none; border-color: var(--blue); box-shadow: 0 0 0 3px rgba(79, 70, 229, .12);
}
form textarea { resize: vertical; }
.btn {
  display: inline-block; border: none; border-radius: 10px; padding: 12px 20px;
  font-size: 16px; cursor: pointer; background: var(--bg); color: var(--text);
}
.btn.primary { background: var(--grad); color: #fff; box-shadow: 0 6px 18px -6px rgba(124, 58, 237, .55); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: #fff; border: 1px solid var(--border); color: var(--text); width: 100%; }
.btn.ghost:hover { border-color: var(--blue); }
.btn.big { width: 100%; padding: 16px; font-size: 18px; font-weight: 700; margin-top: 6px; }
.btn:disabled { opacity: .5; cursor: default; }
.error { color: var(--red); min-height: 20px; font-size: 14px; }

/* รายวิชา */
.miniStats { margin: 14px 0 4px; }
.miniTitle { font-size: 16px; margin: 10px 0 8px; }
.miniTiles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 8px; }
.miniTile {
  background: linear-gradient(180deg, #fff, #fdfcff); border: 1px solid var(--border);
  border-radius: 12px; padding: 10px 8px; text-align: center;
}
.miniTile .n { font-size: 22px; font-weight: 700; background: var(--grad); -webkit-background-clip: text; background-clip: text; color: transparent; }
.miniTile .k { font-size: 12px; color: var(--muted); margin-top: 2px; }
.miniSubject {
  display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 8px 12px; border: 1px solid var(--border); border-radius: 10px; margin-bottom: 6px;
  background: #fbfaff; font-size: 14px;
}
.miniSubject .name { font-weight: 600; }
.miniSubject .detail { color: var(--muted); }

.subjectGrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; margin: 14px 0; }
.subjectCard {
  border: 1px solid var(--border); border-radius: 16px; padding: 16px;
  background: linear-gradient(180deg, #fff, #fdfcff);
}
.cardHead { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.cardIcon {
  width: 40px; height: 40px; border-radius: 12px; display: flex; align-items: center; justify-content: center;
  font-size: 20px; background: var(--grad); flex-shrink: 0;
}
.cardHeadText .name { font-weight: 700; font-size: 16px; }
.badge { font-size: 12px; padding: 3px 9px; border-radius: 999px; display: inline-block; margin-top: 3px; }
.badge.done { background: #dcfce7; color: var(--green); }
.badge.todo { background: #fef3c7; color: #92400e; }
.badge.glow { background: linear-gradient(90deg, #ede9fe, #fce7f3); color: var(--violet); }
.setupForm { margin-top: 6px; }
.setupForm label { margin-bottom: 10px; }
.setupForm textarea { min-height: 40px; }
.fieldsRow { display: flex; gap: 8px; margin-bottom: 10px; }
.fieldsRow .field { flex: 1; margin-bottom: 0; min-width: 0; }
.fieldsRow select { padding: 9px 8px; font-size: 13px; }
.formError { margin: 6px 0 0; }

/* ทำข้อสอบ */
.quizbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.timer { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.timer.low { color: var(--red); }
.progress { color: var(--muted); font-size: 14px; }
.aiNote {
  font-size: 12px; color: var(--muted); background: linear-gradient(90deg, #f5f3ff, #fdf2f8);
  border: 1px solid var(--border); padding: 8px 12px; border-radius: 10px; margin-bottom: 14px;
}
.aiNote a { color: var(--blue); text-decoration: none; margin-left: 6px; font-weight: 600; }
.loadingAi { animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: .55; } 50% { opacity: 1; } }

.quizNav { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 4px; }
.navdot {
  width: 32px; height: 32px; border-radius: 8px; border: 1px solid var(--border); background: #fff;
  color: var(--muted); font-size: 13px; cursor: not-allowed;
}
.navdot.done-correct { background: #dcfce7; border-color: #bbf7d0; color: var(--green); cursor: pointer; }
.navdot.done-wrong { background: #fef2f2; border-color: #fecaca; color: var(--red); cursor: pointer; }
.navdot.current { outline: 2px solid var(--blue); outline-offset: 1px; }

.question { font-size: 18px; line-height: 1.6; margin-bottom: 16px; white-space: pre-wrap; }
.choice {
  display: block; width: 100%; text-align: left; padding: 14px 16px; margin-bottom: 10px;
  border: 2px solid var(--border); border-radius: 12px; background: #fff; font-size: 16px; cursor: pointer;
}
.choice:hover { border-color: var(--blue); }
.choice.chosen { border-color: var(--blue); background: #eef2ff; }
.choice.correct { border-color: var(--green); background: #f0fdf4; }
.choice.wrong { border-color: var(--red); background: #fef2f2; }
.choice:disabled { cursor: default; }

.feedback { border-radius: 12px; padding: 14px; margin: 14px 0; }
.feedback.correct { background: #f0fdf4; border: 1px solid #bbf7d0; }
.feedback.wrong { background: #fef2f2; border: 1px solid #fecaca; }
.feedback h3 { margin: 0 0 6px; font-size: 15px; }
.feedback p { margin: 4px 0; font-size: 15px; line-height: 1.5; }

/* สถิติ */
.statsControls { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 16px; }
.inlineLabel { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); }
.inlineLabel select { margin-top: 0; padding: 8px 10px; font-size: 14px; width: auto; }
.statRow { display: flex; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid var(--border); }
.statRow .k { color: var(--muted); }
.statRow .v { font-weight: 600; }
.bar { display: flex; align-items: center; gap: 10px; margin: 8px 0; }
.bar .label { width: 200px; font-size: 14px; flex-shrink: 0; }
.bar .track { flex: 1; background: var(--bg); border-radius: 999px; height: 14px; overflow: hidden; }
.bar .fill { height: 100%; background: var(--grad); }
.bar .pct { width: 44px; text-align: right; font-size: 13px; color: var(--muted); }

table.simple { width: 100%; border-collapse: collapse; font-size: 14px; }
table.simple th, table.simple td { text-align: left; padding: 8px; border-bottom: 1px solid var(--border); }
input.emailInput { padding: 6px 8px; font-size: 14px; border: 1px solid var(--border); border-radius: 6px; }

.modal {
  position: fixed; inset: 0; background: rgba(30, 27, 46, .55); backdrop-filter: blur(2px); display: flex;
  align-items: center; justify-content: center; padding: 16px; z-index: 20;
}
.modalbox { background: #fff; border-radius: 16px; padding: 20px; max-width: 640px; max-height: 80vh; overflow: auto; position: relative; }
.modalclose { position: absolute; top: 12px; right: 12px; }
#promptText { white-space: pre-wrap; font-size: 13px; color: var(--muted); }

@media (max-width: 480px) {
  .subjectGrid { grid-template-columns: 1fr; }
  .miniTiles { grid-template-columns: repeat(2, 1fr); }
  .fieldsRow { flex-wrap: wrap; }
  .fieldsRow .field { min-width: 45%; }
  .bar .label { width: 120px; }
  .nav { gap: 2px; }
  .navbtn { padding: 8px 8px; font-size: 14px; }
}
