:root {
  color-scheme: light;
  --bg: #f4f4f2; --surface: #fcfcfb; --surface-2: #f0efec; --border: #e2e1dc;
  --text: #0b0b0b; --text-2: #52514e; --muted: #6d6c67;
  --accent: #1f7a33; --accent-ink: #ffffff; --accent-soft: #e6f4ec;
  --gold: #a88412; --good: #0b7d0b; --warning: #a86b00; --critical: #c0312f;
  --bubble-ai: #ffffff; --bubble-me: #e6f4ec;
  --radius: 12px; --shadow: 0 1px 2px rgba(0,0,0,.04), 0 2px 8px rgba(0,0,0,.05);
  --font: 'Sarabun', 'Noto Sans Thai', system-ui, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211; --surface: #1b1b1a; --surface-2: #242422; --border: #34342f;
    --text: #f2f2ef; --text-2: #c4c3bd; --muted: #9a9993;
    --accent: #4cbb68; --accent-ink: #08210f; --accent-soft: #183a22;
    --gold: #d8b440; --good: #5fcf5f; --warning: #f0b440; --critical: #f07070;
    --bubble-ai: #22221f; --bubble-me: #183a22;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1b1b1a; --surface-2: #242422; --border: #34342f;
  --text: #f2f2ef; --text-2: #c4c3bd; --muted: #9a9993;
  --accent: #4cbb68; --accent-ink: #08210f; --accent-soft: #183a22;
  --gold: #d8b440; --good: #5fcf5f; --warning: #f0b440; --critical: #f07070;
  --bubble-ai: #22221f; --bubble-me: #183a22;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font: 16px/1.6 var(--font); }
h1, h2, h3 { line-height: 1.3; margin: 0 0 .5em; }
h1 { font-size: 1.5rem; } h2 { font-size: 1.2rem; } h3 { font-size: 1.05rem; }
p { margin: 0 0 .75em; }
a { color: var(--accent); }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }

.topbar { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border); }
.topbar .btn, .topbar .badge { white-space: nowrap; }
.brand { white-space: nowrap; display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--text); text-decoration: none; }
.mark { display: grid; place-items: center; width: 32px; height: 32px; border-radius: 8px; background: var(--accent);
  color: var(--accent-ink); font-size: .8rem; }
.spacer { flex: 1; }
.who { color: var(--text-2); font-size: .9rem; }

main { max-width: 920px; margin: 0 auto; padding: 20px 16px 48px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 20px; margin-bottom: 16px; }
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.muted { color: var(--muted); }
.small { font-size: .9rem; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.stack > * + * { margin-top: 12px; }

.btn { font: inherit; font-weight: 600; border: 1px solid transparent; border-radius: 10px; padding: 10px 18px;
  background: var(--accent); color: var(--accent-ink); cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: 6px; }
.btn:hover { filter: brightness(1.07); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn.danger { background: transparent; color: var(--critical); border-color: var(--critical); }
.btn.small { padding: 6px 12px; font-size: .9rem; }
.btn.rec { background: var(--critical); color: #fff; }

label { display: block; font-weight: 600; margin-bottom: 4px; }
.hint { font-weight: 400; color: var(--muted); font-size: .88rem; }
input[type=text], input[type=password], input[type=number], select, textarea {
  width: 100%; font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 12px; }
textarea { resize: vertical; min-height: 90px; }
fieldset { border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; margin: 0; }
legend { font-weight: 600; padding: 0 6px; }
.choices { display: flex; flex-wrap: wrap; gap: 8px; }
.choice { display: inline-flex; align-items: center; gap: 6px; padding: 8px 12px; border: 1px solid var(--border);
  border-radius: 999px; font-weight: 400; cursor: pointer; margin: 0; }
.choice:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); font-weight: 600; }
.choice input { accent-color: var(--accent); }

.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: .82rem; font-weight: 600;
  background: var(--surface-2); color: var(--text-2); }
.badge.demo { background: #fff3cd; color: #6b4e00; }
.badge.good { background: var(--accent-soft); color: var(--accent); }
.badge.warn { background: var(--surface-2); color: var(--warning); }
.err { color: var(--critical); min-height: 1.4em; }

.login { max-width: 420px; margin: 6vh auto 0; }
.login h1 { text-align: center; }
.notice { border-left: 4px solid var(--gold); background: var(--surface-2); padding: 10px 14px; border-radius: 8px; font-size: .92rem; }

.kpis { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.kpi { background: var(--surface-2); border-radius: 10px; padding: 12px 14px; }
.kpi .v { font-size: 1.6rem; font-weight: 700; }
.kpi .l { color: var(--text-2); font-size: .88rem; }

table { width: 100%; border-collapse: collapse; font-size: .94rem; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--text-2); font-weight: 600; background: var(--surface-2); }
.table-wrap { overflow-x: auto; }

/* สัมภาษณ์ */
.chat-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.progress { height: 6px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin: 10px 0 0; }
.progress > span { display: block; height: 100%; background: var(--accent); transition: width .3s; }
.chat { display: flex; flex-direction: column; gap: 10px; min-height: 320px; max-height: 58vh; overflow-y: auto; padding: 4px 2px 8px; }
.msg { max-width: 82%; padding: 10px 14px; border-radius: 14px; border: 1px solid var(--border); white-space: pre-wrap; }
.msg.ai { align-self: flex-start; background: var(--bubble-ai); border-top-left-radius: 4px; }
.msg.me { align-self: flex-end; background: var(--bubble-me); border-top-right-radius: 4px; }
.msg .meta { display: block; font-size: .78rem; color: var(--muted); margin-bottom: 2px; }
.typing { color: var(--muted); font-style: italic; }
.composer { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
.composer textarea { min-height: 110px; }

/* ผลประเมิน */
.score-row { display: grid; grid-template-columns: minmax(160px, 1.2fr) 70px 2fr; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.dots { display: flex; gap: 4px; }
.dots i { width: 12px; height: 12px; border-radius: 50%; background: var(--surface-2); border: 1px solid var(--border); }
.dots i.on { background: var(--accent); border-color: var(--accent); }
.feedback li { margin-bottom: 4px; }
details summary { cursor: pointer; font-weight: 600; }

/* แบบวัด D2 */
.likert { width: 100%; }
.likert td.q { width: 55%; }
.likert td.c { text-align: center; }
.likert input { width: 20px; height: 20px; accent-color: var(--accent); }
.likert td.c span { display: none; }

#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--text); color: var(--bg);
  padding: 10px 18px; border-radius: 10px; opacity: 0; pointer-events: none; transition: opacity .2s; max-width: 90vw; }
#toast.show { opacity: 1; }

@media (max-width: 640px) {
  main { padding: 14px 16px 40px; }
  .score-row { grid-template-columns: 1fr 60px; }
  .score-row .ev { grid-column: 1 / -1; }
  .msg { max-width: 92%; }
  .likert thead { display: none; }
  .likert tr { display: block; padding: 8px 0; border-bottom: 1px solid var(--border); }
  .likert td { display: inline-block; border: 0; padding: 4px 6px; }
  .likert td.q { display: block; width: auto; }
  .likert td.c span { display: inline; }
  .who { display: none; }
}
