:root {
  --brand: #1f5f8b;
  --brand-dark: #174a6d;
  --ink: #1d232a;
  --muted: #6b7480;
  --line: #dde3ea;
  --bg: #f5f7fa;
  --card: #ffffff;
  --ok: #1e7f4f;
  --warn: #b7791f;
  --err: #b42318;
  --radius: 12px;
  font-size: 16px;
  /* Giao diện chỉ thiết kế cho nền sáng: giữ màu hệ thống (ButtonText, input...) ở bản sáng kể cả khi máy bật Dark Mode. */
  color-scheme: light;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body { font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; color: var(--ink); background: var(--bg); line-height: 1.45; }
a { color: var(--brand); }
h1 { font-size: 1.5rem; margin: 0 0 .75rem; }
h2 { font-size: 1.15rem; margin: 1.25rem 0 .5rem; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.inline { display: inline; }
code { background: #eef2f6; padding: .1em .35em; border-radius: 4px; font-size: .9em; }

/* ---------- admin ---------- */
.topbar { display: flex; flex-wrap: wrap; align-items: center; gap: 1rem; padding: .6rem 1rem; background: var(--brand); color: #fff; }
.topbar .brand { color: #fff; font-weight: 700; text-decoration: none; font-size: 1.1rem; }
.topbar nav { display: flex; flex-wrap: wrap; gap: .25rem; flex: 1; }
.topbar nav a { color: #e6f0f7; text-decoration: none; padding: .3rem .6rem; border-radius: 6px; }
.topbar nav a:hover { background: rgba(255,255,255,.15); }
.topbar .muted { color: #cfe0ec; margin-right: .5rem; }
.topbar button.link { background: none; border: 0; color: #fff; text-decoration: underline; cursor: pointer; font: inherit; }
.container { max-width: 1100px; margin: 0 auto; padding: 1rem; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; margin-bottom: 1rem; }
.grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; }
.stat .n { font-size: 1.8rem; font-weight: 700; }
table { width: 100%; border-collapse: collapse; background: var(--card); font-size: .925rem; }
th, td { text-align: left; padding: .5rem .6rem; border-bottom: 1px solid var(--line); vertical-align: top; }
th { background: #eef2f6; font-weight: 600; }
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); }
.badge { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .78rem; font-weight: 600; background: #e6ebf1; color: #384250; }
.badge.ok, .badge.active, .badge.published, .badge.open, .badge.finished, .badge.committed { background: #dff3e7; color: var(--ok); }
.badge.warn, .badge.draft, .badge.inprogress, .badge.validated { background: #fbeed6; color: var(--warn); }
.badge.err, .badge.retired, .badge.rejected, .badge.closed, .badge.archived, .badge.abandoned { background: #fbe1de; color: var(--err); }
.flash { padding: .7rem 1rem; border-radius: var(--radius); margin-bottom: 1rem; }
.flash.ok { background: #dff3e7; color: var(--ok); }
.flash.err { background: #fbe1de; color: var(--err); }
form.stack { display: flex; flex-direction: column; gap: .75rem; max-width: 560px; }
label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: .2rem; }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=datetime-local], select, textarea {
  width: 100%; padding: .55rem .7rem; border: 1px solid var(--line); border-radius: 8px; font: inherit; background: #fff;
}
textarea { min-height: 90px; }
.btn { display: inline-block; padding: .55rem 1rem; border-radius: 8px; border: 1px solid var(--brand); background: var(--brand); color: #fff; font: inherit; font-weight: 600; cursor: pointer; text-decoration: none; }
.btn:hover { background: var(--brand-dark); }
.btn.secondary { background: #fff; color: var(--brand); }
.btn.danger { background: var(--err); border-color: var(--err); }
.btn.sm { padding: .3rem .7rem; font-size: .85rem; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.field-error { color: var(--err); font-size: .875rem; }
del { background: #fbe1de; text-decoration: line-through; }
ins { background: #dff3e7; text-decoration: none; }
.level-Error { color: var(--err); font-weight: 600; }
.level-Warning { color: var(--warn); font-weight: 600; }
.level-Info { color: var(--muted); }
.filters { display: flex; gap: .5rem; flex-wrap: wrap; align-items: end; margin-bottom: 1rem; }
.filters > div { min-width: 160px; }
.qr { width: 180px; height: 180px; image-rendering: pixelated; border: 1px solid var(--line); border-radius: 8px; }
.copy { font-family: ui-monospace, Menlo, Consolas, monospace; background: #eef2f6; padding: .4rem .6rem; border-radius: 8px; word-break: break-all; }

/* ---------- quiz (mobile-first) ---------- */
body.quiz { background: #f2f5f8; }
.quiz-wrap { max-width: 560px; margin: 0 auto; min-height: 100dvh; display: flex; flex-direction: column; padding: max(12px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom)); }
.quiz-card { background: #fff; border-radius: 16px; padding: 1.25rem 1.1rem; box-shadow: 0 2px 12px rgba(20,40,60,.06); flex: 1; display: flex; flex-direction: column; }
.progress { height: 6px; background: #e3e9ef; border-radius: 999px; overflow: hidden; margin-bottom: .5rem; }
.progress > span { display: block; height: 100%; background: var(--brand); transition: width .25s; }
.progress-text { display: flex; justify-content: space-between; font-size: .85rem; color: var(--muted); margin-bottom: 1rem; }
.stem { font-size: 1.25rem; font-weight: 600; line-height: 1.4; margin: .5rem 0 1.25rem; min-height: 3.5em; }
.scale { display: grid; gap: .5rem; }
.scale button { display: flex; align-items: center; gap: .75rem; width: 100%; text-align: left; padding: .85rem .9rem; border-radius: 12px; border: 1.5px solid var(--line); background: #fff; color: var(--ink); -webkit-text-fill-color: currentColor; -webkit-appearance: none; appearance: none; font: inherit; font-size: 1rem; cursor: pointer; min-height: 52px; transition: border-color .12s, background .12s; }
.scale button .num { flex: 0 0 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; background: #eef2f6; font-weight: 700; font-size: .95rem; }
.scale button:active, .scale button.selected { border-color: var(--brand); background: #e8f1f8; }
.scale button.selected .num { background: var(--brand); color: #fff; }
.scale.disabled button { pointer-events: none; opacity: .7; }
.quiz-foot { display: flex; justify-content: space-between; align-items: center; gap: .5rem; margin-top: 1.25rem; }
.ghost { background: none; border: 1px solid var(--line); border-radius: 10px; padding: .55rem .9rem; font: inherit; color: var(--ink); cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: .4rem; }
.ghost.flagged { border-color: var(--warn); color: var(--warn); background: #fff8ec; }
.htmx-request .scale button { pointer-events: none; }
.htmx-indicator { display: none; }
.htmx-request .htmx-indicator { display: inline; }
.net-error { color: var(--err); font-size: .9rem; margin-top: .75rem; }
/* Nhịp trả lời: thanh mờ cạn dần trong 10s rồi hiện lời nhắc. Chỉ khuyến khích, không chặn hay bỏ câu. */
.stem { animation: q-in .22s ease-out; }
.pace { height: 3px; background: #eef2f6; border-radius: 999px; overflow: hidden; margin: -.6rem 0 1rem; }
.pace > span { display: block; height: 100%; background: #b9cfdf; transform-origin: left; animation: q-drain 10s linear forwards; }
.pace-hint { margin: -.5rem 0 .9rem; font-size: .9rem; color: var(--brand); opacity: 0; animation: q-fade .5s ease 11s forwards; }
.scale button.selected { animation: q-pick .22s ease-out; }
.cheer { align-self: center; background: #e7f5ee; color: var(--ok); font-weight: 600; font-size: .9rem; padding: .4rem .9rem; border-radius: 999px; margin: -.35rem 0 .25rem; animation: q-cheer 3.2s ease forwards; }
.progress.milestone > span { animation: q-glow 1.2s ease-out 2; }
@keyframes q-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes q-drain { from { transform: scaleX(1); } to { transform: scaleX(0); } }
@keyframes q-fade { to { opacity: 1; } }
@keyframes q-pick { 0% { transform: scale(1); } 45% { transform: scale(1.025); } 100% { transform: scale(1); } }
@keyframes q-cheer { 0% { opacity: 0; transform: translateY(-4px); } 10%, 80% { opacity: 1; transform: none; } 100% { opacity: 0; } }
@keyframes q-glow { 0%, 100% { filter: none; } 50% { filter: brightness(1.35) drop-shadow(0 0 4px var(--brand)); } }
@media (prefers-reduced-motion: reduce) {
  .stem, .scale button.selected, .progress.milestone > span { animation: none; }
  .pace { display: none; }
  .pace-hint { animation-duration: 0s; }
  .cheer { animation: none; }
}
.intro h1 { font-size: 1.45rem; }
.intro p { margin: .5rem 0; }
.intro form.stack { flex: 1; max-width: none; }
.intro .example { border: 1px dashed var(--line); border-radius: 12px; padding: .75rem; margin: 1rem 0; }
.consent-box { display: flex; gap: .6rem; align-items: flex-start; margin: 1rem 0; font-weight: 500; }
.consent-box input { width: 22px; height: 22px; margin-top: 2px; flex: 0 0 auto; }
.btn.block { display: block; width: 100%; text-align: center; padding: .85rem; font-size: 1.05rem; }
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: flex; align-items: flex-end; z-index: 10; }
.sheet { background: #fff; width: 100%; max-width: 560px; margin: 0 auto; border-radius: 16px 16px 0 0; padding: 1rem 1rem max(1rem, env(safe-area-inset-bottom)); }
.sheet h3 { margin: 0 0 .75rem; font-size: 1.05rem; }
.check { display: flex; gap: .6rem; align-items: center; padding: .5rem 0; font-weight: 500; }
.check input { width: 20px; height: 20px; }
.done { text-align: center; padding: 2rem 0; }

/* ---------- peer rating ---------- */
.peer-trait { border: 1px solid var(--line); border-radius: 12px; padding: .6rem .8rem .5rem; margin: 0; }
.peer-trait legend { padding: 0 .3rem; }
.peer-scale { display: grid; grid-template-columns: repeat(6, 1fr); gap: .35rem; }
.peer-opt { position: relative; display: block; }
.peer-opt input { position: absolute; opacity: 0; inset: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.peer-opt .num { display: grid; place-items: center; height: 42px; border: 1.5px solid var(--line); border-radius: 10px; font-weight: 700; background: #fff; }
.peer-opt input:checked + .num { background: var(--brand); color: #fff; border-color: var(--brand); }
.peer-opt input:focus-visible + .num { outline: 2px solid var(--brand); outline-offset: 2px; }
.peer-ends { display: flex; justify-content: space-between; margin-top: .25rem; }

/* ---------- tuning ---------- */
table.tuning td, table.tuning th { white-space: nowrap; }
table.tuning td:nth-child(4) { white-space: normal; }
td.dist { display: flex; gap: 2px; align-items: flex-end; height: 34px; }
td.dist span { display: flex; align-items: flex-end; width: 9px; height: 26px; background: #eef2f6; border-radius: 2px; }
td.dist span i { display: block; width: 100%; background: var(--brand); border-radius: 2px; }
.done .big { font-size: 3rem; }
