:root {
  color-scheme: light;
  --s1: #2563EB; --s2: #0D9488; --s3: #16A34A; --s4: #D97706; --s5: #7C3AED;
  --cobalt: #1B3A6B;
  --ink: #14213D; --muted: #4B5E7A; --line: #DCE5F2;
  --bg: #F4F7FC; --surface-2: #EAF0F9; --card: #FFFFFF;
  --good: #15803D; --good-bg: #DCFCE7;
  --bad: #DC2626; --bad-bg: #FEE2E2;
  --warn: #B45309; --warn-bg: #FEF3C7;
  --accent: #2563EB; --accent-hover: #1D4ED8;
  --radius: 14px;
  --shadow-float: 0 10px 28px -8px rgb(20 33 61 / 0.14);
  --spring-easing: linear(0, 0.016 0.5%, 0.06 1%, 0.226 2%, 1.116 5.4%, 1.375 6.6%, 1.527 7.7%, 1.565 8.2%, 1.585 8.8%, 1.581 9.3%, 1.559 9.8%, 1.458 10.9%, 0.937 14.3%, 0.784 15.5%, 0.693 16.6%, 0.67 17.1%, 0.657 17.7%, 0.671 18.7%, 0.729 19.8%, 1.042 23.3%, 1.13 24.5%, 1.182 25.6%, 1.201 26.7%, 1.192 27.7%, 1.156 28.8%, 0.977 32.2%, 0.925 33.4%, 0.894 34.5%, 0.882 35.6%, 0.887 36.6%, 0.907 37.7%, 1.045 42.4%, 1.069 44.5%, 1.059 46.3%, 0.979 50.9%, 0.96 53.4%, 0.966 55.3%, 1.013 59.9%, 1.024 62.3%, 0.986 71.2%, 1.008 79.9%, 0.995 88.9%, 1);
  --bounce-easing: linear(0, 0.214 14.7%, 0.386 23.7%, 0.598 31.9%, 0.999 44.7%, 0.807 52.6%, 0.762 56%, 0.747 59.4%, 0.758 62.4%, 0.793 65.6%, 0.999 77.4%, 0.961 81.2%, 0.949 84.8%, 0.956 88%, 0.993 95.5%, 1);
  accent-color: var(--accent);
  caret-color: var(--accent);
  scrollbar-color: #94A3B8 var(--surface-2);
}

*, *::before, *::after { box-sizing: border-box; }

::selection { background: #DBEAFE; color: var(--ink); }

body {
  margin: 0;
  font-family: 'Sarabun', system-ui, -apple-system, sans-serif;
  font-size: 16.5px;
  color: var(--ink);
  background: var(--bg);
  line-height: 1.6;
}

h1, h2, h3, h4 {
  font-family: 'Anuphan', 'Sarabun', system-ui, sans-serif;
  line-height: 1.3;
  margin: 0 0 0.4em;
  letter-spacing: -0.015em;
  text-wrap: balance;
}

button, input, textarea, select { font: inherit; }

:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.ico { flex: none; vertical-align: middle; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 0.875rem; }
.wrap { max-width: 1180px; margin: 0 auto; padding: 20px 18px 40px; }
.hidden { display: none !important; }
.divider { border: 0; border-top: 1px solid var(--line); margin: 14px 0; }

/* ---------- topbar ---------- */
.topbar {
  background: var(--cobalt);
  color: #FFFFFF;
  position: sticky;
  top: 0;
  z-index: 20;
}
.topbar-inner {
  max-width: 1180px;
  margin: 0 auto;
  padding: 11px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 9px;
  background: rgb(255 255 255 / 0.14);
  flex: none;
}
.topbar .title {
  font-family: 'Anuphan', 'Sarabun', sans-serif;
  font-weight: 700;
  font-size: 1.05rem;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.top-right { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.user-chip {
  font-size: 0.875rem;
  background: rgb(255 255 255 / 0.14);
  padding: 4px 12px;
  border-radius: 999px;
  color: #F8FAFC;
}
.topbar .btn.ghost {
  background: rgb(255 255 255 / 0.12);
  border-color: rgb(255 255 255 / 0.35);
  color: #FFFFFF;
}
.topbar .btn.ghost:hover { background: rgb(255 255 255 / 0.22); }
.topbar :focus-visible { outline-color: #93C5FD; }

.cuisenaire-stripe {
  display: grid;
  grid-template-columns: 3fr 2fr 4fr 2fr 3fr;
  height: 3px;
}
.cuisenaire-stripe span:nth-child(1) { background: var(--s1); }
.cuisenaire-stripe span:nth-child(2) { background: var(--s2); }
.cuisenaire-stripe span:nth-child(3) { background: var(--s3); }
.cuisenaire-stripe span:nth-child(4) { background: var(--s4); }
.cuisenaire-stripe span:nth-child(5) { background: var(--s5); }

.banner {
  background: var(--warn-bg);
  color: #78350F;
  border-bottom: 1px solid #FDE68A;
  padding: 8px 16px;
  font-size: 0.9rem;
  text-align: center;
  font-weight: 500;
}

/* ---------- buttons & inputs ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1.5px solid var(--accent);
  background: var(--accent);
  color: #FFFFFF;
  border-radius: 999px;
  padding: 9px 18px;
  min-height: 44px;
  cursor: pointer;
  font-weight: 600;
  transition: background-color 0.16s ease, border-color 0.16s ease, transform 0.35s ease-out;
  transition-timing-function: ease, ease, var(--spring-easing);
}
.btn:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn:active:not(:disabled) { transform: scale(0.97); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn.full { width: 100%; }
.btn.ghost { background: var(--card); color: var(--ink); border-color: var(--line); }
.btn.ghost:hover:not(:disabled) { background: var(--surface-2); border-color: #C5D2E6; }
.btn.good { background: var(--good-bg); border-color: #86EFAC; color: #14532D; }
.btn.good:hover:not(:disabled) { background: #BBF7D0; }
.btn.bad { background: var(--bad-bg); border-color: #FCA5A5; color: #7F1D1D; }
.btn.bad:hover:not(:disabled) { background: #FECACA; }
.btn.sm { padding: 6px 14px; min-height: 36px; font-size: 0.9rem; }

.link {
  background: none;
  border: 0;
  color: var(--accent);
  cursor: pointer;
  padding: 6px 2px;
  font-weight: 600;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.mini {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink);
  border-radius: 8px;
  min-width: 36px;
  min-height: 36px;
  padding: 0 10px;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.85rem;
}
.mini:hover { background: var(--surface-2); }
.mini.ghost { color: var(--muted); border-color: transparent; background: transparent; }
.mini.ghost:hover { background: var(--bad-bg); color: var(--bad); }
.mini.active { border-color: var(--accent); background: #DBEAFE; color: var(--cobalt); }

.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 14px; }
.field > label { font-weight: 600; font-size: 0.95rem; color: var(--ink); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

input[type=text], input[type=password], input[type=number], input:not([type]), textarea, select {
  width: 100%;
  padding: 10px 12px;
  min-height: 44px;
  font-size: max(1rem, 1em);
  border: 1.5px solid #C8D4E6;
  border-radius: 10px;
  background: var(--card);
  color: var(--ink);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
textarea { min-height: 98px; resize: vertical; line-height: 1.55; }
input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgb(37 99 235 / 0.18);
}
input:user-invalid, textarea:user-invalid { border-color: var(--bad); }

.pw-wrap { position: relative; display: flex; align-items: center; }
.pw-wrap input { padding-inline-end: 46px; }
.pw-toggle {
  position: absolute;
  inset-inline-end: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.pw-toggle:hover { background: var(--surface-2); color: var(--ink); }

.err { color: var(--bad); font-weight: 600; min-height: 1.4em; font-size: 0.92rem; margin: 4px 0 8px; }

/* ---------- cards ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
}

.section-heading { font-size: 1.35rem; margin: 22px 0 12px; }
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.act-card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  transform: perspective(900px) rotateX(var(--rx, 0deg)) rotateY(var(--ry, 0deg));
  transition: transform 0.45s ease-out, box-shadow 0.2s ease, border-color 0.2s ease;
  transition-timing-function: var(--spring-easing), ease, ease;
}
.act-card:hover {
  transform: perspective(900px) rotateX(var(--rx, 0deg)) rotateY(var(--ry, 0deg)) translateY(-3px);
  box-shadow: var(--shadow-float);
  border-color: color-mix(in srgb, var(--c, var(--accent)) 45%, var(--line));
}
.act-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.act-emoji {
  font-size: 1.5rem;
  line-height: 1;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: color-mix(in srgb, var(--c, var(--accent)) 12%, #FFFFFF);
  border: 1px solid color-mix(in srgb, var(--c, var(--accent)) 26%, var(--line));
}
.act-card h2 { font-size: 1.12rem; margin: 2px 0; }
.act-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: auto; padding-top: 8px; }

.pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 11px;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 600;
  background: var(--surface-2);
  color: var(--ink);
}
.pill.done { background: var(--good-bg); color: #14532D; }
.pill.draft { background: var(--warn-bg); color: #78350F; }

/* ---------- home & auth ---------- */
.auth { max-width: 480px; margin: 28px auto; }
.auth-hero {
  background: var(--cobalt);
  color: #FFFFFF;
  border-radius: var(--radius);
  padding: 14px 18px;
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 14px;
}
.auth-hero h1 { font-size: 1.25rem; color: #FFFFFF; margin-bottom: 4px; }
.auth-hero p { margin: 0; font-size: 0.92rem; color: #DBEAFE; }

.tabs { display: flex; gap: 6px; margin-bottom: 16px; background: var(--surface-2); padding: 4px; border-radius: 10px; }
.tabs button {
  flex: 1;
  padding: 9px 12px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  cursor: pointer;
  font-weight: 600;
  color: var(--muted);
}
.tabs button.on { background: var(--card); color: var(--ink); border: 1px solid var(--line); }

.home-banner {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 14px;
  align-items: center;
}
.progress-summary {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 16px;
  display: flex;
  gap: 18px;
  align-items: center;
}
.progress-summary strong { display: block; font-size: 1.1rem; color: var(--cobalt); }

/* ---------- blocky & 3D stage ---------- */
.blocky-row { display: flex; gap: 12px; align-items: flex-start; }
.blocky { flex: none; }
.bubble {
  background: var(--card);
  border: 1.5px solid #BFDBFE;
  border-radius: 14px;
  padding: 12px 15px;
  position: relative;
  flex: 1;
}
.bubble::before {
  content: '';
  position: absolute;
  left: -8px;
  top: 18px;
  border: 8px solid transparent;
  border-left: 0;
  border-right-color: #BFDBFE;
}
.toast {
  position: fixed;
  right: 16px;
  bottom: 16px;
  max-width: min(400px, calc(100vw - 32px));
  z-index: 50;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  box-shadow: var(--shadow-float);
  @media (prefers-reduced-motion: no-preference) {
    animation: pop 0.28s ease-out;
    animation-timing-function: var(--spring-easing);
  }
}
@keyframes pop { from { transform: translateY(18px); opacity: 0; } }

.stage3d {
  position: relative;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: radial-gradient(circle at 50% 25%, #FFFFFF 0%, #EEF4FD 65%, #E3ECFB 100%);
  overflow: hidden;
  content-visibility: auto;
  contain-intrinsic-size: auto none auto 280px;
}
.stage3d.bar-stage3d {
  height: 290px;
  margin-bottom: 12px;
}
.stage3d.diorama {
  height: 225px;
  contain-intrinsic-size: auto none auto 225px;
  margin-bottom: 12px;
}
.stage3d.blocky3d {
  width: 104px;
  height: 118px;
  flex: none;
  border-radius: 12px;
  border: 0;
  background: transparent;
  display: grid;
  place-items: center;
  contain-intrinsic-size: auto none auto 118px;
}
.stage3d.blocky3d.lg {
  width: 156px;
  height: 168px;
  margin: 0 auto 8px;
  contain-intrinsic-size: auto none auto 168px;
}
.stage3d.blocky3d svg {
  transition: opacity 0.25s ease;
}
.stage3d.blocky3d.ready3d svg {
  opacity: 0;
  pointer-events: none;
}
.stage3d.blocky3d .canvas3d {
  position: absolute;
  inset: 0;
}

.stage3d-canvas-host {
  position: absolute;
  inset: 0;
}
.canvas3d {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}
.stage3d-bar {
  position: relative;
  z-index: 3;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 8px 10px;
  pointer-events: none;
}
.stage3d-bar > * { pointer-events: auto; }
.stage3d-badge {
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--cobalt);
  background: rgb(255 255 255 / 0.88);
  backdrop-filter: blur(4px);
  border: 1px solid var(--line);
  padding: 3px 10px;
  border-radius: 999px;
}
.stage3d-actions { display: flex; gap: 4px; flex-wrap: wrap; }
.stage3d-actions .mini { min-height: 30px; min-width: 30px; padding: 0 8px; font-size: 0.78rem; background: rgb(255 255 255 / 0.9); }

.pins3d {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 2;
}
.pin3d {
  position: absolute;
  top: 0;
  left: 0;
  background: rgb(255 255 255 / 0.92);
  border: 1.5px solid var(--c, var(--accent));
  color: var(--ink);
  font-size: 0.76rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  white-space: nowrap;
  box-shadow: 0 2px 8px rgb(20 33 61 / 0.1);
  transition: transform 0.08s linear, opacity 0.15s ease;
}
.pin3d.is-hl {
  background: var(--c, var(--accent));
  color: #FFFFFF;
}
.hint3d {
  position: absolute;
  left: 10px;
  bottom: 8px;
  z-index: 2;
  pointer-events: none;
  font-size: 0.75rem;
  background: rgb(255 255 255 / 0.82);
  padding: 2px 8px;
  border-radius: 999px;
}
.diorama-fallback {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 14px;
  text-align: center;
}

/* ---------- CSS 3D Isometric Fallback Cubes ---------- */
.iso-stage {
  padding: 8px 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-x: auto;
}
.iso-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 6px;
  border-radius: 8px;
}
.iso-row.is-hl { background: rgb(37 99 235 / 0.08); }
.iso-label { width: 110px; flex: none; font-size: 0.85rem; font-weight: 600; }
.iso-cubes { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.iso-cube {
  position: relative;
  width: 20px;
  height: 20px;
  display: inline-block;
  transform-style: preserve-3d;
  @media (prefers-reduced-motion: no-preference) {
    animation: cubeDrop 0.45s ease-out backwards;
    animation-timing-function: var(--bounce-easing);
    animation-delay: var(--delay, 0ms);
  }
}
.iso-stage.is-3d .iso-cube { transform: rotateX(-24deg) rotateY(32deg); }
.iso-cube i { position: absolute; inset: 0; border-radius: 3px; border: 1px solid rgb(255 255 255 / 0.5); }
.iso-cube .f-front { background: var(--c); transform: translateZ(10px); }
.iso-cube .f-top { background: color-mix(in srgb, var(--c) 72%, #FFFFFF); transform: rotateX(90deg) translateZ(10px); }
.iso-cube .f-right { background: color-mix(in srgb, var(--c) 78%, #000000); transform: rotateY(90deg) translateZ(10px); }
@keyframes cubeDrop {
  from { opacity: 0; transform: translateY(-20px) scale(0.75); }
}

/* ---------- stepper ---------- */
.stepper { margin: 8px 0 20px; }
.stepper ol {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  justify-content: space-between;
}
.stepper li { flex: 1; position: relative; }
.stepper li::after {
  content: '';
  position: absolute;
  top: 19px;
  left: calc(50% + 22px);
  right: calc(-50% + 22px);
  height: 3px;
  background: var(--line);
  z-index: 0;
}
.stepper li:last-child::after { display: none; }
.stepper li:has(button.done)::after { background: var(--accent); }
.stepper button {
  width: 100%;
  background: none;
  border: 0;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  position: relative;
  z-index: 1;
  color: var(--muted);
  font-size: 0.85rem;
  padding: 2px 4px;
}
.stepper .dot {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 2px solid var(--c);
  background: var(--card);
  display: grid;
  place-items: center;
  font-weight: 700;
  color: var(--ink);
  transition: transform 0.55s ease-out, background-color 0.16s ease;
  transition-timing-function: var(--spring-easing), ease;
}
.stepper .done .dot { background: color-mix(in srgb, var(--c) 18%, #FFFFFF); color: var(--c); }
.stepper .cur .dot { background: var(--c); color: #FFFFFF; transform: scale(1.12); }
.stepper .cur { color: var(--ink); font-weight: 700; }

/* ---------- directional view transitions ---------- */
.panel {
  border: 1px solid color-mix(in srgb, var(--c, var(--accent)) 28%, var(--line));
  border-radius: var(--radius);
  background: var(--card);
  padding: 22px;
  view-transition-name: step-panel;
}
@keyframes slide-to-left { to { opacity: 0; transform: translateX(-28px); } }
@keyframes slide-from-right { from { opacity: 0; transform: translateX(28px); } }
@keyframes slide-to-right { to { opacity: 0; transform: translateX(28px); } }
@keyframes slide-from-left { from { opacity: 0; transform: translateX(-28px); } }

::view-transition-group(step-panel) {
  animation: 0.34s cubic-bezier(0.16, 1, 0.3, 1) both;
}
html:active-view-transition-type(forward)::view-transition-old(step-panel) {
  animation-name: slide-to-left;
}
html:active-view-transition-type(forward)::view-transition-new(step-panel) {
  animation-name: slide-from-right;
}
html:active-view-transition-type(backward)::view-transition-old(step-panel) {
  animation-name: slide-to-right;
}
html:active-view-transition-type(backward)::view-transition-new(step-panel) {
  animation-name: slide-from-left;
}
@media (prefers-reduced-motion: reduce) {
  ::view-transition-group(step-panel),
  ::view-transition-old(step-panel),
  ::view-transition-new(step-panel) {
    animation: none !important;
  }
}

/* ---------- worksheet layout ---------- */
.work { display: grid; grid-template-columns: 350px 1fr; gap: 20px; align-items: start; }
.side { position: sticky; top: 74px; display: flex; flex-direction: column; gap: 14px; }
.problem-card { background: var(--surface-2); border-color: #CBD7EB; }
.problem { white-space: pre-line; font-size: 0.95rem; max-width: 70ch; }
.problem .q { font-weight: 700; margin-top: 10px; color: var(--cobalt); }
.panel-head { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.step-badge {
  background: color-mix(in srgb, var(--c, var(--accent)) 14%, #FFFFFF);
  color: var(--c, var(--accent));
  font-weight: 700;
  font-size: 0.85rem;
  padding: 5px 12px;
  border-radius: 999px;
  white-space: nowrap;
}
.panel h2 { color: var(--ink); font-size: 1.25rem; margin: 0; }
.panel .en { color: var(--muted); font-size: 0.85rem; }
.q-group { margin-top: 16px; }
.q-label { display: block; font-weight: 600; margin-bottom: 6px; color: var(--ink); }
.finals { display: grid; gap: 10px; }
.finals .row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.finals label { font-weight: 600; }
.finals input { width: 150px; }
.finals .unit { color: var(--muted); font-weight: 500; }

.checkbox-zone {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 15px;
  margin-top: 18px;
}
.zone-title { font-weight: 600; }
.choice-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 10px; }
.choice-row .btn.sel { box-shadow: 0 0 0 3px rgb(20 33 61 / 0.16); }
.nav { display: flex; justify-content: space-between; align-items: center; margin-top: 22px; gap: 10px; padding-top: 16px; border-top: 1px solid var(--line); }
.saved { font-size: 0.84rem; color: var(--muted); }
.hint-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.hint-box ol { padding-left: 20px; margin: 8px 0; }
.hint-box li { margin-bottom: 6px; }

/* ---------- bar model ---------- */
.tool-surface {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
}
.bar-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.bar-total { font-size: 0.9rem; background: var(--card); border: 1px solid var(--line); padding: 2px 10px; border-radius: 999px; font-variant-numeric: tabular-nums; }
.bar-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  flex-wrap: wrap;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px;
  transition: border-color 0.15s ease, background-color 0.15s ease;
}
.bar-row.is-hl { border-color: var(--c, var(--accent)); background: color-mix(in srgb, var(--c, var(--accent)) 6%, #FFFFFF); }
.bar-swatch { width: 10px; height: 24px; border-radius: 4px; background: var(--c); flex: none; }
.bar-label { width: 120px !important; min-height: 36px !important; padding: 6px 10px !important; }
.bar-controls { display: inline-flex; align-items: center; gap: 4px; }
.bar-blocks { display: flex; flex-wrap: wrap; gap: 2px; min-width: 60px; flex: 1; align-items: center; }
.bar-blocks span {
  width: 20px;
  height: 24px;
  border-radius: 4px;
  background: color-mix(in srgb, var(--c) 38%, #FFFFFF);
  border: 1.5px solid var(--c);
  box-shadow: inset 0 2px 0 rgb(255 255 255 / 0.65), 2px 2px 0 color-mix(in srgb, var(--c) 30%, transparent);
}
.bar-blocks.sm span { width: 12px; height: 13px; border-width: 1px; border-radius: 2px; box-shadow: 1.5px 1.5px 0 color-mix(in srgb, var(--c) 28%, transparent); }
.bar-blocks em { color: var(--muted); font-size: 0.85rem; font-style: normal; }
.bar-count { font-weight: 700; min-width: 26px; text-align: center; font-variant-numeric: tabular-nums; }
.bar-preview div { display: flex; align-items: center; gap: 6px; font-size: 0.85rem; margin: 4px 0; }
.bar-preview .lbl { width: 78px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mode-tabs { display: flex; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }

/* ---------- sketch ---------- */
.sketch-wrap {
  background: radial-gradient(circle, #CBD5E1 1px, transparent 1px) 0 0 / 16px 16px #FFFFFF;
  border: 1.5px solid var(--line);
  border-radius: 10px;
  padding: 4px;
}
.sketch { width: 100%; display: block; border-radius: 8px; touch-action: none; background: #FFFFFF; cursor: crosshair; }
.sketch-tools { display: flex; gap: 8px; margin-bottom: 8px; flex-wrap: wrap; align-items: center; justify-content: space-between; }
.sketch-pens, .sketch-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.pen {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 3px solid #FFFFFF;
  background: var(--pc);
  box-shadow: 0 0 0 1.5px #CBD5E1;
  cursor: pointer;
}
.pen.active { box-shadow: 0 0 0 3px var(--pc); transform: scale(1.06); }

/* ---------- result ---------- */
.result-hero { display: flex; gap: 20px; align-items: center; flex-wrap: wrap; }
.result-summary { flex: 1; min-width: 240px; }
.result-overall { margin: 8px 0; max-width: 65ch; }
.score-big { font-family: 'Anuphan', sans-serif; font-size: 2.85rem; font-weight: 700; line-height: 1.1; color: var(--cobalt); }
.step-score {
  display: grid;
  grid-template-columns: 220px 1fr 62px;
  gap: 10px;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
}
.step-score:last-child { border-bottom: 0; }
.step-title { display: flex; align-items: center; gap: 8px; }
.step-pill {
  display: inline-grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: color-mix(in srgb, var(--c, var(--accent)) 16%, #FFFFFF);
  color: var(--c, var(--accent));
  font-weight: 700;
  font-size: 0.85rem;
  flex: none;
}
.step-score .bar { height: 11px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.step-score .bar i { display: block; height: 100%; background: var(--c); border-radius: 999px; }
.step-score .fb { grid-column: 1 / -1; font-size: 0.94rem; color: var(--ink); padding-left: 34px; }
.delta.up { color: var(--good); }
.delta.down { color: var(--bad); }
.history-list { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.history-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--card);
  cursor: pointer;
  text-align: left;
}
.history-item:hover { background: var(--surface-2); }
.history-item.active { border-color: var(--accent); background: #EFF6FF; }
.loading { text-align: center; padding: 44px 16px; }
.loading .blocky {
  @media (prefers-reduced-motion: no-preference) {
    animation: bob 1s ease-in-out infinite alternate;
  }
}
@keyframes bob { to { transform: translateY(-8px); } }

/* ---------- teacher ---------- */
.ttabs { display: flex; gap: 8px; flex-wrap: wrap; margin: 4px 0 16px; }
.ttabs button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 9px 16px;
  min-height: 44px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink);
  cursor: pointer;
  font-weight: 600;
}
.ttabs button:hover { background: var(--surface-2); }
.ttabs button.on { background: var(--cobalt); color: #FFFFFF; border-color: var(--cobalt); }

.live-head { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; justify-content: space-between; }
.live-stats { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.stat-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 12px;
  border-radius: 999px;
  background: var(--card);
  border: 1px solid var(--line);
  font-size: 0.9rem;
  font-weight: 600;
}
.stat-chip i { width: 9px; height: 9px; border-radius: 50%; background: var(--st); }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.size-group { display: inline-flex; gap: 4px; }

.monitor { display: grid; grid-template-columns: 1fr 350px; gap: 16px; align-items: start; }
.mgrid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(var(--cw, 190px), 1fr)); }
.mcard {
  background: var(--card);
  border: 1px solid color-mix(in srgb, var(--st) 35%, var(--line));
  border-radius: 12px;
  padding: 12px;
  cursor: pointer;
  text-align: left;
  transition: transform 0.14s ease, box-shadow 0.14s ease;
}
.mcard:hover { transform: translateY(-1px); box-shadow: var(--shadow-float); }
.mcard.sel { outline: 3px solid var(--accent); }
.mcard .nm { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mcard .dots { display: flex; gap: 5px; margin: 6px 0; }
.mcard .dots i { width: 10px; height: 10px; border-radius: 50%; border: 1.5px solid #CBD5E1; }
.mcard .dots i.on { background: var(--st); border-color: var(--st); }
.mcard .preview { font-size: 0.8rem; color: var(--muted); max-height: 3.2em; overflow: hidden; margin-top: 4px; }
.mgrid.L .mcard .preview { max-height: 9em; }
.mgrid.S .mcard .preview { display: none; }

.st-working { --st: var(--good); }
.st-stuck { --st: var(--bad); }
.st-idle { --st: var(--warn); }
.st-submitted { --st: var(--accent); }
.st-home { --st: #64748B; }

.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 0.85rem; margin: 10px 0 12px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: var(--st); margin-right: 5px; }
.detail { position: sticky; top: 74px; }
.live-text { background: var(--surface-2); border: 1px solid var(--line); border-radius: 8px; padding: 10px; white-space: pre-wrap; font-size: 0.9rem; max-height: 240px; overflow: auto; }
.quick-msgs { display: flex; flex-direction: column; gap: 6px; margin: 8px 0 12px; }
.quick-msgs .btn { justify-content: flex-start; text-align: left; }

/* ---------- responsive tables ---------- */
.table-wrapper {
  overflow: auto;
  max-inline-size: 100%;
  max-block-size: min(560px, 75vh);
  container-type: inline-size;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--card);
}
table.scores {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background: var(--card);
  font-size: 0.92rem;
}
table.scores th, table.scores td {
  border-bottom: 1px solid var(--line);
  border-right: 1px solid var(--line);
  padding: 8px 10px;
  text-align: center;
}
table.scores th:last-child, table.scores td:last-child { border-right: 0; }
table.scores tr:last-child th, table.scores tr:last-child td { border-bottom: 0; }
table.scores thead th {
  position: sticky;
  inset-block-start: 0;
  z-index: 2;
  background: var(--surface-2);
  font-weight: 700;
}
table.scores tbody th {
  position: sticky;
  inset-inline-start: 0;
  z-index: 1;
  background: var(--card);
  font-weight: 600;
}
table.scores th.name, table.scores td.name { text-align: left; white-space: nowrap; }
table.scores td.cell { padding: 4px; }
.cell-btn {
  width: 100%;
  background: transparent;
  border: 0;
  border-radius: 8px;
  padding: 5px 6px;
  cursor: pointer;
  color: inherit;
}
.cell-btn:hover { background: #EFF6FF; }
.heat { display: inline-block; min-width: 38px; padding: 2px 7px; border-radius: 6px; font-weight: 700; color: var(--ink); }

/* ---------- dialog & top-layer animation ---------- */
dialog {
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 0;
  width: min(920px, 95vw);
  max-height: 90vh;
  background: var(--card);
  color: var(--ink);
  box-shadow: var(--shadow-float);
  opacity: 0;
  transition-property: opacity, transform, display, overlay;
  transition-duration: 0.22s;
  transition-timing-function: ease-out;
  transition-behavior: allow-discrete;
}
dialog:modal { overscroll-behavior: contain; }
dialog::backdrop {
  overflow: hidden;
  overscroll-behavior: contain;
  background-color: rgb(15 23 42 / 0);
  transition-property: background-color, backdrop-filter, display, overlay;
  transition-duration: 0.22s;
  transition-timing-function: ease-out;
  transition-behavior: allow-discrete;
}
@media (prefers-reduced-motion: no-preference) {
  dialog { transform: translateY(10px) scale(0.97); }
}
:is(dialog[open]):where(:not([data-closing])) {
  opacity: 1;
  @starting-style { opacity: 0; }
  @media (prefers-reduced-motion: no-preference) {
    transform: translateY(0) scale(1);
    @starting-style { transform: translateY(10px) scale(0.97); }
  }
  &::backdrop {
    background-color: rgb(15 23 42 / 0.5);
    backdrop-filter: blur(2px);
    @starting-style { background-color: rgb(15 23 42 / 0); }
  }
}
@media (prefers-reduced-motion: reduce) {
  dialog, dialog::backdrop { transition-duration: 0.1s; }
  .act-card, .btn, .stepper .dot { transition-timing-function: ease; }
}

dialog .dlg { padding: 22px; overflow: auto; max-height: 90vh; }
.dlg-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; margin-bottom: 12px; }
.dlg-sketch { max-width: 100%; border: 1px solid var(--line); border-radius: 8px; margin-top: 8px; }
.ans-block {
  background: color-mix(in srgb, var(--c, var(--accent)) 5%, #FFFFFF);
  border: 1px solid color-mix(in srgb, var(--c, var(--accent)) 24%, var(--line));
  border-radius: 12px;
  padding: 12px 14px;
  margin: 12px 0;
}
.ans-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.ans-block pre {
  white-space: pre-wrap;
  font-family: inherit;
  margin: 4px 0 0;
  background: var(--card);
  border: 1px solid var(--line);
  padding: 8px 10px;
  border-radius: 8px;
}
.ans-fb { display: flex; align-items: flex-start; gap: 6px; margin-top: 8px; color: var(--muted); }
.grade-editor {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 16px;
  margin-top: 16px;
}
.score-edit { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; margin-bottom: 12px; }
.score-edit label { display: flex; flex-direction: column; gap: 4px; font-weight: 600; text-align: center; }
.score-edit input { text-align: center; }

@media (max-width: 900px) {
  .work, .monitor, .home-banner { grid-template-columns: 1fr; }
  .side, .detail { position: static; }
  .stepper button span.lab { display: none; }
  .step-score { grid-template-columns: 1fr 62px; }
  .step-score .bar { grid-column: 1 / -1; order: 3; }
  .step-score .fb { padding-left: 0; order: 4; }
}
@media (max-width: 560px) {
  .field-row, .score-edit { grid-template-columns: 1fr 1fr; }
  .progress-summary { width: 100%; justify-content: space-between; }
}
