:root {
  --bg: #f6f7fb;
  --card: #ffffff;
  --text: #1e293b;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-soft: #eff6ff;
  --success: #16a34a;
  --success-soft: #f0fdf4;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --warning: #d97706;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 8px 24px rgba(15, 23, 42, .05);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Sarabun", system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

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

.container { max-width: 860px; margin: 0 auto; padding: 24px 18px 64px; }
.container-wide { max-width: 1080px; }

/* Header */
.site-header {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}
.site-header .inner {
  max-width: 1080px; margin: 0 auto; padding: 14px 18px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.brand { font-weight: 700; font-size: 1.15rem; display: flex; align-items: center; gap: 8px; color: var(--text); }
.brand:hover { text-decoration: none; }
.brand .logo { width: 30px; height: 30px; background: var(--primary); color: #fff; border-radius: 8px; display: grid; place-items: center; font-size: .9rem; }

h1 { font-size: 1.6rem; margin: 0 0 4px; }
h2 { font-size: 1.2rem; margin: 28px 0 12px; }
.subtitle { color: var(--muted); margin: 0 0 24px; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 11px 18px; border-radius: 10px; border: 1px solid transparent;
  background: var(--primary); color: #fff; font: inherit; font-weight: 600;
  cursor: pointer; transition: background .15s, opacity .15s; text-decoration: none;
}
.btn:hover { background: var(--primary-dark); text-decoration: none; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: #f1f5f9; }
.btn-danger { background: var(--danger); }
.btn-danger:hover { background: #b91c1c; }
.btn-success { background: var(--success); }
.btn-success:hover { background: #15803d; }
.btn-sm { padding: 7px 12px; font-size: .88rem; border-radius: 8px; }
.btn-block { width: 100%; }

/* Cards / exam list */
.card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px;
}
.exam-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; }
.exam-card { display: flex; flex-direction: column; gap: 10px; }
.exam-card .title { font-weight: 700; font-size: 1.08rem; }
.exam-card .meta { color: var(--muted); font-size: .9rem; display: flex; flex-wrap: wrap; gap: 6px 14px; }
.exam-card .desc { color: var(--muted); font-size: .92rem; flex: 1; }
.badge {
  display: inline-block; padding: 2px 10px; border-radius: 999px;
  background: var(--primary-soft); color: var(--primary-dark); font-size: .8rem; font-weight: 600;
}

.cat-title { display: flex; align-items: center; gap: 10px; }
.empty { text-align: center; color: var(--muted); padding: 48px 12px; }

/* Forms */
label { display: block; font-weight: 600; margin: 14px 0 6px; font-size: .95rem; }
input[type=text], input[type=password], input[type=number], textarea, select {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px;
  font: inherit; background: #fff; color: var(--text);
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
textarea { resize: vertical; min-height: 70px; }
.field-row { display: flex; gap: 12px; flex-wrap: wrap; }
.field-row > div { flex: 1; min-width: 120px; }
.hint { color: var(--muted); font-size: .85rem; margin-top: 4px; }

/* Exam taking */
.exam-topbar {
  position: sticky; top: 0; z-index: 10; background: var(--card); border-bottom: 1px solid var(--border);
  padding: 12px 18px; display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.timer { font-weight: 700; font-variant-numeric: tabular-nums; padding: 6px 14px; border-radius: 10px; background: var(--primary-soft); color: var(--primary-dark); }
.timer.warn { background: var(--danger-soft); color: var(--danger); }
.progress-pill { color: var(--muted); font-size: .9rem; }

.question-image, .explain-image { max-width: 100%; height: auto; border-radius: 10px; border: 1px solid var(--border); margin: 12px 0; display: block; }
.question-text { font-size: 1.12rem; font-weight: 600; margin: 4px 0 16px; }
.q-number { color: var(--primary); font-weight: 700; }

.choices { display: flex; flex-direction: column; gap: 10px; }
.choice {
  display: flex; align-items: flex-start; gap: 12px; padding: 13px 15px;
  border: 1px solid var(--border); border-radius: 12px; cursor: pointer; background: #fff; transition: border-color .12s, background .12s;
}
.choice:hover { border-color: var(--primary); background: var(--primary-soft); }
.choice.selected { border-color: var(--primary); background: var(--primary-soft); }
.choice input { margin-top: 4px; }
.choice .key { font-weight: 700; color: var(--muted); min-width: 20px; }
.choice.correct { border-color: var(--success); background: var(--success-soft); }
.choice.wrong { border-color: var(--danger); background: var(--danger-soft); }

.nav-row { display: flex; justify-content: space-between; gap: 12px; margin-top: 24px; }
.q-grid { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.q-dot {
  width: 40px; height: 40px; border-radius: 10px; border: 1px solid var(--border);
  background: #fff; cursor: pointer; font-weight: 600; color: var(--text);
}
.q-dot.answered { background: var(--primary); color: #fff; border-color: var(--primary); }
.q-dot.current { outline: 2px solid var(--primary); }

/* Result */
.score-hero { text-align: center; padding: 28px; }
.score-big { font-size: 3rem; font-weight: 800; color: var(--primary); }
.score-sub { color: var(--muted); }
.result-q { border-top: 1px solid var(--border); padding: 18px 0; }
.tag { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: .82rem; font-weight: 600; }
.tag-correct { background: var(--success-soft); color: var(--success); }
.tag-wrong { background: var(--danger-soft); color: var(--danger); }
.explain-box { background: var(--primary-soft); border-radius: 10px; padding: 12px 14px; margin-top: 10px; font-size: .95rem; }

/* Admin */
.admin-layout { display: grid; grid-template-columns: 240px 1fr; gap: 20px; align-items: start; }
.admin-side { position: sticky; top: 80px; }
.admin-side .card { padding: 14px; }
.side-item { display: block; padding: 9px 12px; border-radius: 8px; cursor: pointer; color: var(--text); }
.side-item:hover { background: #f1f5f9; text-decoration: none; }
.side-item.active { background: var(--primary-soft); color: var(--primary-dark); font-weight: 600; }

.q-editor { border: 1px solid var(--border); border-radius: 12px; padding: 16px; margin-bottom: 16px; background: #fbfcfe; }
.q-editor .q-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.choice-edit { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }

.cat-row { display: grid; grid-template-columns: 70px 1fr 120px 70px 60px; gap: 10px; align-items: center; margin-bottom: 8px; }
.cat-row .cat-order { text-align: center; }
.cat-row .cat-id { font-size: .82rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-head { font-size: .82rem; color: var(--muted); font-weight: 600; margin-bottom: 4px; }
.cat-head span { padding: 0 2px; }
@media (max-width: 560px) {
  .cat-row { grid-template-columns: 56px 1fr 56px; }
  .cat-row .cat-id, .cat-head span:nth-child(3) { display: none; }
}
.choice-edit input[type=radio] { width: auto; margin: 0; }
.choice-edit input[type=text] { flex: 1; }
.img-preview { max-height: 120px; border-radius: 8px; border: 1px solid var(--border); margin-top: 8px; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #1e293b; color: #fff; padding: 12px 20px; border-radius: 10px; box-shadow: var(--shadow);
  z-index: 100; opacity: 0; transition: opacity .2s, transform .2s; pointer-events: none; max-width: 90vw;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }
.toast.error { background: var(--danger); }
.toast.success { background: var(--success); }

.row-between { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.stack { display: flex; flex-direction: column; gap: 8px; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.mt { margin-top: 16px; }
.hidden { display: none !important; }

@media (max-width: 720px) {
  .admin-layout { grid-template-columns: 1fr; }
  .admin-side { position: static; }
}
