:root {
  --bg: #f6f7fb;
  --card: #ffffff;
  --text: #1f2333;
  --muted: #6b7185;
  --line: #e3e6ef;
  --primary: #4f46e5;
  --primary-ink: #ffffff;
  --primary-soft: #eef0ff;
  --accent: #0e9f6e;
  --danger: #d92d20;
  --ok: #12a150;
  --ok-soft: #e3f8ec;
  --bad: #e5484d;
  --bad-soft: #fde8e8;
  --amber: #b45309;
  --amber-soft: #fef3c7;
  --field: rgba(255, 255, 255, 0.92);
  --field-border: #6d7cff;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12141c;
    --card: #1b1e29;
    --text: #e8eaf2;
    --muted: #9aa0b4;
    --line: #2c3040;
    --primary: #7c74ff;
    --primary-soft: #262a4a;
    --ok-soft: #0f2d1f;
    --bad-soft: #3a1618;
    --amber-soft: #3a2a0a;
    --amber: #f5b454;
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}
[hidden] {
  display: none !important;
}
body {
  margin: 0;
  font: 16px/1.5 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
a {
  color: var(--primary);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}
h1 {
  font-size: 1.8rem;
  line-height: 1.2;
  margin: 0.4em 0 0.5em;
}
h2 {
  font-size: 1.3rem;
}
code {
  background: var(--primary-soft);
  padding: 0.1em 0.4em;
  border-radius: 6px;
  font-size: 0.9em;
}
input,
select,
textarea {
  font: inherit;
  color: inherit;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.5em 0.65em;
  width: 100%;
}
input:focus,
select:focus,
textarea:focus,
button:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}
input[type='checkbox'],
input[type='radio'] {
  width: auto;
  accent-color: var(--primary);
}
label {
  display: flex;
  flex-direction: column;
  gap: 0.3em;
  font-weight: 500;
  font-size: 0.95rem;
}
label.check {
  flex-direction: row;
  align-items: center;
  gap: 0.5em;
  font-weight: 400;
}

/* ---------- Layout ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.7rem max(16px, calc(50% - 600px));
  background: var(--card);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
  flex-wrap: wrap;
}
.brand {
  font-weight: 800;
  font-size: 1.25rem;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 0.45em;
}
.brand:hover {
  text-decoration: none;
}
.logo {
  display: inline-grid;
  place-items: center;
  width: 1.7em;
  height: 1.7em;
  border-radius: 8px;
  background: var(--primary);
  color: #fff;
  font-size: 0.9em;
}
#nav {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  flex-wrap: wrap;
}
#nav > a:not(.btn) {
  color: var(--muted);
  font-weight: 500;
}
#nav > a.active {
  color: var(--text);
}
main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1rem 16px 5rem;
}
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.2rem;
}
.row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}
.gap {
  gap: 0.5rem;
}
.center {
  justify-content: center;
}
.stack {
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
}
.spacer {
  margin-left: auto;
  display: flex;
  gap: 0.25rem;
}
.muted {
  color: var(--muted);
}
.small {
  font-size: 0.85rem;
}
.hint {
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 400;
}
.error {
  color: var(--danger);
  min-height: 1em;
  margin: 0;
}
.empty {
  text-align: center;
  padding: 2.5rem 1rem;
  color: var(--muted);
}
.notice {
  background: var(--amber-soft);
  color: var(--amber);
  padding: 0.7em 1em;
  border-radius: 8px;
  margin: 0;
}
.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}
.tag {
  display: inline-block;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.15em 0.55em;
  border-radius: 99px;
  background: var(--primary-soft);
  color: var(--primary);
}
.tag.green {
  background: var(--ok-soft);
  color: var(--ok);
}
.tag.amber {
  background: var(--amber-soft);
  color: var(--amber);
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35em;
  font: inherit;
  font-weight: 600;
  padding: 0.5em 1em;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  text-decoration: none !important;
}
.btn:hover {
  border-color: var(--primary);
}
.btn:disabled {
  opacity: 0.55;
  cursor: default;
}
.btn.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--primary-ink);
}
.btn.primary:hover {
  filter: brightness(1.08);
}
.btn.accent {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn.danger {
  color: var(--danger);
}
.btn.ghost {
  background: transparent;
  border-color: transparent;
}
.btn.ghost:hover {
  background: var(--primary-soft);
}
.btn.small {
  font-size: 0.85rem;
  padding: 0.3em 0.7em;
}
.btn.big {
  font-size: 1.05rem;
  padding: 0.7em 1.4em;
}

/* ---------- Home ---------- */
.hero {
  text-align: center;
  padding: 3rem 0 2rem;
}
.hero h1 {
  font-size: clamp(2rem, 5vw, 3.2rem);
  letter-spacing: -0.02em;
}
.lead {
  font-size: 1.15rem;
  color: var(--muted);
  max-width: 680px;
  margin: 0 auto 1.6rem;
}
.steps {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  gap: 1rem;
  margin: 1rem 0 2.5rem;
}
.step h3 {
  margin: 0.4em 0 0.2em;
}
.step p {
  margin: 0;
  color: var(--muted);
}
.step-icon {
  font-size: 1.8rem;
}

/* ---------- Worksheet cards ---------- */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 1rem;
}
.ws-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
}
.thumb {
  display: block;
  aspect-ratio: 4 / 3;
  background: var(--primary-soft);
  overflow: hidden;
  border-bottom: 1px solid var(--line);
}
.thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: top;
}
.thumb-doc {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--primary);
  font-weight: 700;
}
.thumb-doc span:first-child {
  font-size: 2.4rem;
}
.ws-body {
  padding: 0.8rem 0.9rem;
  display: flex;
  flex-direction: column;
  flex: 1;
}
.ws-body h3 {
  margin: 0;
  font-size: 1.02rem;
}
.ws-body h3 a {
  color: var(--text);
}
.meta {
  color: var(--muted);
  font-size: 0.85rem;
  margin: 0.2em 0 0.6em;
}
.ws-actions {
  display: flex;
  gap: 0.35rem;
  flex-wrap: wrap;
  align-items: center;
  margin-top: auto;
}
.filters {
  display: grid;
  grid-template-columns: 1fr 180px 180px auto;
  gap: 0.5rem;
  margin-bottom: 1.2rem;
}

/* ---------- Auth ---------- */
.auth {
  max-width: 420px;
  margin: 2rem auto;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}
.auth h1 {
  margin: 0;
}
.code-input {
  font-size: 1.8rem;
  text-align: center;
  letter-spacing: 0.2em;
  text-transform: uppercase;
}

/* ---------- Create ---------- */
.tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.6rem;
  margin-bottom: 1rem;
}
.tab {
  font: inherit;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: 0.9rem 1rem;
  border-radius: var(--radius);
  border: 2px solid var(--line);
  background: var(--card);
  color: var(--text);
  cursor: pointer;
}
.tab span {
  color: var(--muted);
  font-size: 0.85rem;
}
.tab.on {
  border-color: var(--primary);
  background: var(--primary-soft);
}
.meta-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 0.7rem;
  align-items: end;
}
.meta-grid .span2 {
  grid-column: 1 / -1;
}
.working {
  text-align: center;
  padding: 3rem 1rem;
}
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.7rem;
  padding: 2rem;
  color: var(--muted);
}
.working .loading {
  padding: 0;
  font-size: 1.1rem;
  color: var(--text);
}
.spin {
  width: 1.3em;
  height: 1.3em;
  border: 3px solid var(--line);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ---------- Worksheet player ---------- */
.play-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 1rem;
}
.play-head h1 {
  margin-bottom: 0.1em;
}
.play-head p {
  margin: 0.2em 0;
}
.worksheet {
  display: flex;
  flex-direction: column;
  gap: 1.2rem;
  max-width: 900px;
  margin: 0 auto;
}
.page {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.img-page {
  position: relative;
  width: 100%;
  container-type: inline-size;
  background: #fff;
  user-select: none;
}
.img-page > img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
.abs {
  position: absolute;
  margin: 0;
}
.img-page .fld-text,
.img-page .fld-select,
.img-page .drop-zone,
.img-page .fld-open {
  background: var(--field);
  border: 1.5px solid var(--field-border);
  border-radius: 4px;
  padding: 0 0.25em;
  color: #1c2c8c;
  font-family: 'Comic Sans MS', 'Chalkboard SE', 'Segoe Print', system-ui, sans-serif;
  line-height: 1.1;
}
.img-page .fld-open {
  resize: none;
  padding: 0.15em 0.25em;
}
.img-page .fld-select {
  padding: 0 0.1em;
  cursor: pointer;
  background: #fff;
}
.fld-choice {
  border: 2px solid var(--field-border);
  background: rgba(255, 255, 255, 0.35);
  border-radius: 4px;
  cursor: pointer;
  padding: 0;
}
.fld-choice.checked {
  background: rgba(79, 70, 229, 0.25);
}
.fld-choice.checked::after {
  content: '✓';
  color: #1c2c8c;
  font-weight: 900;
  display: grid;
  place-items: center;
  height: 100%;
  font-size: max(14px, 1em);
  line-height: 1;
}
.fld-join {
  display: inline-grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid var(--field-border);
  background: #fff;
  cursor: pointer;
  padding: 0;
  z-index: 2;
}
.fld-join span {
  width: 45%;
  height: 45%;
  border-radius: 50%;
  background: var(--field-border);
}
.fld-join.selected {
  box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.4);
  background: #ffe066;
}
.fld-join.joined span {
  background: #1c2c8c;
}
.img-page .fld-join.abs {
  width: auto;
  height: auto;
}
.join-lines {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  pointer-events: none;
}
.join-lines line {
  stroke: #3b3fd1;
  stroke-width: 3;
  stroke-linecap: round;
  pointer-events: stroke;
  cursor: pointer;
}
.review .join-lines line {
  cursor: default;
}
.join-lines line.ok {
  stroke: var(--ok);
}
.join-lines line.bad {
  stroke: var(--bad);
}
.join-lines line.expected {
  stroke: var(--ok);
  stroke-dasharray: 6 6;
  opacity: 0.7;
}
.drop-zone {
  cursor: pointer;
  min-height: 1.6em;
  font: inherit;
}
.drop-zone:not(.filled) {
  background: repeating-linear-gradient(45deg, #eef0ff, #eef0ff 6px, #f8f8ff 6px, #f8f8ff 12px) !important;
  border-style: dashed !important;
}
.word-bank {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  align-items: center;
  padding: 0.7rem 1rem;
  background: var(--primary-soft);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 58px;
  z-index: 5;
}
.bank-label {
  font-weight: 700;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--primary);
  margin-right: 0.3rem;
}
.chip {
  font: inherit;
  padding: 0.25em 0.75em;
  border-radius: 99px;
  border: 1.5px solid var(--primary);
  background: var(--card);
  color: var(--text);
  cursor: grab;
}
.chip.selected {
  background: var(--primary);
  color: #fff;
}

/* doc pages */
.doc-page {
  padding: 1.4rem 1.6rem 1.8rem;
  font-size: 1.05rem;
}
.doc-page .word-bank {
  margin: -1.4rem -1.6rem 1rem;
}
.blk-heading {
  font-size: 1.15rem;
  margin: 1.3em 0 0.5em;
  color: var(--primary);
}
.blk-heading:first-child {
  margin-top: 0;
}
.blk-text p {
  margin: 0.45em 0;
  line-height: 2.1;
}
.blk-text .fld-text,
.blk-text .fld-select,
.blk-text .drop-zone {
  width: auto;
  display: inline-block;
  padding: 0.1em 0.45em;
  border: none;
  border-bottom: 2px solid var(--field-border);
  border-radius: 4px 4px 0 0;
  background: var(--primary-soft);
  vertical-align: baseline;
  min-width: 6em;
  line-height: 1.4;
}
.blk-text .fld-select {
  min-width: 4em;
}
.blk-text .drop-zone {
  border: 1.5px solid var(--field-border);
  border-radius: 6px;
  min-width: 7em;
  color: var(--text);
}
.blk-text .fld-open {
  display: block;
}
.blk-mcq {
  border: none;
  padding: 0;
  margin: 0.8em 0;
}
.blk-mcq legend {
  font-weight: 600;
  padding: 0;
  margin-bottom: 0.4em;
}
.options {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem 0.6rem;
}
.opt {
  flex-direction: row;
  align-items: center;
  gap: 0.5em;
  font-weight: 400;
  padding: 0.45em 0.9em;
  border: 1.5px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
}
.opt:has(input:checked) {
  border-color: var(--primary);
  background: var(--primary-soft);
}
.blk-match,
.blk-open {
  margin: 0.8em 0;
}
.prompt {
  font-weight: 600;
  margin: 0 0 0.5em;
}
.match-grid {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(40px, 120px) minmax(0, 1fr);
  max-width: 640px;
}
.match-col {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}
.match-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.45em 0.7em;
  border: 1.5px solid var(--line);
  border-radius: 10px;
  background: var(--card);
  min-height: 2.6em;
}
.match-col.left .match-item {
  justify-content: space-between;
}
.fld-open {
  resize: vertical;
}
.blk-image {
  margin: 1em 0;
  text-align: center;
}
.blk-image img {
  max-width: 100%;
  border-radius: 8px;
}
.blk-image figcaption {
  color: var(--muted);
  font-size: 0.9rem;
}

/* marks */
.mark-ok {
  border-color: var(--ok) !important;
  background-color: var(--ok-soft) !important;
  color: var(--ok) !important;
}
.mark-bad {
  border-color: var(--bad) !important;
  background-color: var(--bad-soft) !important;
  color: var(--bad) !important;
}
.mark-pending {
  border-color: var(--amber) !important;
  background-color: var(--amber-soft) !important;
}
.img-page .mark-ok,
.img-page .mark-bad {
  background-color: #fff !important;
  box-shadow: 0 0 0 2px var(--ok);
}
.img-page .mark-bad {
  box-shadow: 0 0 0 2px var(--bad);
}
.fld-choice.should-tick {
  box-shadow: 0 0 0 3px var(--ok) !important;
}
.blk-mcq.mark-ok,
.blk-mcq.mark-bad,
.blk-open.mark-ok,
.blk-open.mark-bad,
.blk-open.mark-pending {
  background: none !important;
  border-left: 4px solid;
  padding-left: 0.8em;
}
.blk-mcq.mark-ok legend,
.blk-mcq.mark-bad legend {
  color: var(--text);
}
.expected-tag {
  display: inline-block;
  margin-left: 0.3em;
  font-size: 0.8em;
  font-weight: 700;
  color: var(--ok);
  background: var(--ok-soft);
  border-radius: 4px;
  padding: 0 0.35em;
}
.expected-tag.abs-tag {
  position: absolute;
  margin: 2px 0 0;
  z-index: 3;
  font-size: clamp(9px, 1.6cqw, 14px);
  white-space: nowrap;
}
.blk-mcq .expected-tag,
.blk-open .expected-tag {
  display: table;
  margin: 0.5em 0 0;
}
.mark-tools {
  display: inline-flex;
  gap: 2px;
  margin-left: 0.3em;
  vertical-align: middle;
}
.mark-tools.abs-tools {
  position: absolute;
  margin-left: 3px;
  z-index: 4;
}
.mt {
  font: inherit;
  font-size: 12px;
  width: 22px;
  height: 22px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--muted);
  cursor: pointer;
  padding: 0;
}
.mt.ok.on {
  background: var(--ok);
  color: #fff;
  border-color: var(--ok);
}
.mt.bad.on {
  background: var(--bad);
  color: #fff;
  border-color: var(--bad);
}
.finish-bar {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.8rem;
  flex-wrap: wrap;
  margin: 1.5rem auto 0;
  max-width: 900px;
}
.score-bar {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1rem;
  margin: 1.2rem auto 0;
  flex-wrap: wrap;
}
.score {
  display: inline-block;
  font-weight: 800;
  font-size: 1.1rem;
  padding: 0.15em 0.6em;
  border-radius: 8px;
  white-space: nowrap;
}
.score small {
  font-weight: 600;
  opacity: 0.8;
}
.score.good {
  background: var(--ok-soft);
  color: var(--ok);
}
.score.ok {
  background: var(--amber-soft);
  color: var(--amber);
}
.score.low {
  background: var(--bad-soft);
  color: var(--bad);
}
.done {
  text-align: center;
  max-width: 900px;
  margin: 0 auto 1.2rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.6rem;
}
.done .score {
  font-size: 2rem;
}
.done h1 {
  margin: 0;
}
.result-summary {
  text-align: right;
}
.result-summary .score {
  font-size: 1.6rem;
}
.result-summary p {
  margin: 0.4em 0 0;
}

/* ---------- Assignments ---------- */
.table-wrap {
  overflow-x: auto;
  padding: 0;
}
table {
  width: 100%;
  border-collapse: collapse;
}
th,
td {
  text-align: left;
  padding: 0.7rem 1rem;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}
th {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}
tr.clickable {
  cursor: pointer;
}
tr.clickable:hover td {
  background: var(--primary-soft);
}
.share {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  margin-bottom: 1rem;
}
.share p {
  margin: 0.2em 0 0;
}
.share-row {
  display: flex;
  gap: 0.5rem;
}
.big-code {
  font-size: 1.2em;
  font-weight: 700;
  letter-spacing: 0.1em;
}
.stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
  margin-bottom: 1rem;
}
.stat {
  display: flex;
  flex-direction: column;
  padding: 0.9rem 1.1rem;
}
.stat b {
  font-size: 1.6rem;
}
.stat span {
  color: var(--muted);
  font-size: 0.85rem;
}
.two-col {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 280px;
  gap: 1rem;
  align-items: start;
}
.two-col h2 {
  margin-top: 0;
}
.settings h3 {
  margin: 0;
}

/* ---------- Editor ---------- */
.editor {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.editor > p {
  margin: 0;
}
.editor-head {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}
.editor-title-row {
  display: flex;
  gap: 1rem;
  align-items: center;
  flex-wrap: wrap;
}
.title-input {
  flex: 1;
  min-width: 240px;
  font-size: 1.4rem;
  font-weight: 700;
  border-color: transparent;
  padding-left: 0.3em;
}
.title-input:hover {
  border-color: var(--line);
}
.page-head {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  margin-bottom: 0.8rem;
}
.add-page {
  display: flex;
  gap: 0.6rem;
  align-items: center;
  flex-wrap: wrap;
}
.img-editor {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 270px;
  gap: 1rem;
  align-items: start;
}
.stage-wrap {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.toolbar {
  display: flex;
  gap: 0.3rem;
  flex-wrap: wrap;
  align-items: center;
  position: sticky;
  top: 58px;
  z-index: 6;
  background: var(--card);
  padding: 0.35rem 0;
}
.tool {
  font: inherit;
  font-size: 0.85rem;
  padding: 0.3em 0.7em;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
  cursor: pointer;
}
.tool.on {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.img-page.edit {
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
  touch-action: none;
}
.img-page.edit:not(.tool-select) {
  cursor: crosshair;
}
.ebox {
  position: absolute;
  border: 2px solid;
  border-radius: 3px;
  background: rgba(79, 70, 229, 0.12);
  cursor: move;
  touch-action: none;
}
.tool-select .ebox:hover {
  box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.15);
}
.img-page.edit:not(.tool-select) .ebox {
  cursor: crosshair;
}
.ebox .lbl {
  position: absolute;
  left: 2px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 11px;
  font-weight: 700;
  line-height: 1.1;
  white-space: nowrap;
  overflow: hidden;
  max-width: calc(100% - 4px);
  pointer-events: none;
  color: #111;
  text-shadow: 0 0 2px #fff, 0 0 2px #fff;
}
.ebox.t-choice .lbl,
.ebox.t-join .lbl {
  left: 50%;
  transform: translate(-50%, -50%);
}
.ebox .handle {
  position: absolute;
  right: -5px;
  bottom: -5px;
  width: 10px;
  height: 10px;
  background: #fff;
  border: 2px solid currentColor;
  border-radius: 2px;
  cursor: nwse-resize;
  display: none;
}
.ebox.sel {
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.45);
  z-index: 3;
}
.ebox.sel .handle {
  display: block;
}
.ebox.ghost {
  pointer-events: none;
}
.t-text {
  border-color: #4f46e5;
  color: #4f46e5;
}
.t-dropdown {
  border-color: #0891b2;
  color: #0891b2;
  background-color: rgba(8, 145, 178, 0.12);
}
.t-choice {
  border-color: #d97706;
  color: #d97706;
  background-color: rgba(217, 119, 6, 0.12);
}
.t-choice.correct {
  background-color: rgba(18, 161, 80, 0.3);
}
.t-join {
  border-color: #db2777;
  color: #db2777;
  border-radius: 50%;
  background-color: rgba(219, 39, 119, 0.15);
}
.t-drop {
  border-color: #7c3aed;
  color: #7c3aed;
  border-style: dashed;
  background-color: rgba(124, 58, 237, 0.12);
}
.t-open {
  border-color: #64748b;
  color: #64748b;
  background-color: rgba(100, 116, 139, 0.12);
}
.field-panel {
  position: sticky;
  top: 70px;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.9rem;
}
.field-panel h4 {
  margin: 0;
}
.field-panel p {
  margin: 0;
}
.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem 0.7rem;
}
.legend span {
  display: inline-flex;
  align-items: center;
  gap: 0.3em;
}
.sw {
  display: inline-block;
  width: 12px;
  height: 12px;
  border: 2px solid;
  border-radius: 3px;
}
.blocks {
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
}
.blk-edit {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.7rem 0.8rem;
  background: var(--bg);
}
.blk-tools {
  display: flex;
  align-items: center;
  margin-bottom: 0.5rem;
}
.heading-input {
  font-weight: 700;
  font-size: 1.1rem;
}
.add-block {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  align-items: center;
  margin-top: 0.8rem;
}
.bank-input {
  margin-top: 0.8rem;
}
.inline-fields {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}
.inline-field {
  display: grid;
  grid-template-columns: auto 130px 1fr auto auto;
  gap: 0.4rem;
  align-items: center;
}
.inline-field:has(> input + input) {
  grid-template-columns: auto 130px 1fr 1fr auto;
}
.opt-row input:not([type='checkbox']),
.pair-row input {
  flex: 1;
  width: auto;
}
.blk-img-preview {
  max-width: 260px;
  border-radius: 8px;
}

/* ---------- Feedback ---------- */
.feedback-form {
  max-width: 720px;
  margin: 1rem auto;
}
.feedback-form h1 {
  margin: 0;
}
.fb-label {
  font-weight: 500;
  font-size: 0.95rem;
  margin: 0 0 0.4rem;
}
.fb-kinds {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 0.5rem;
}
.fb-stars {
  display: flex;
  gap: 0.2rem;
}
.fb-stars button {
  font-size: 1.9rem;
  line-height: 1;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--line);
  padding: 0 0.1rem;
}
.fb-stars button.on {
  color: #f5a524;
}
.fb-item.is-done {
  opacity: 0.65;
}
.fb-meta {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
}
.fb-rating {
  color: #f5a524;
  letter-spacing: 1px;
}
.fb-message {
  white-space: pre-wrap;
  margin: 0.7rem 0;
}
.fb-shot {
  max-width: 320px;
  max-height: 220px;
  border-radius: 8px;
  border: 1px solid var(--line);
}
.tag.fb-bug {
  background: var(--bad-soft);
  color: var(--bad);
}
.tag.fb-praise {
  background: var(--ok-soft);
  color: var(--ok);
}
.tag.fb-question {
  background: var(--amber-soft);
  color: var(--amber);
}

/* ---------- Picture picker ---------- */
.modal.picker {
  width: min(760px, 100%);
  max-height: calc(100vh - 32px);
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
}
.picker-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.picker-head h2 {
  margin: 0;
}
.picker-tabs {
  display: flex;
  gap: 0.3rem;
}
.picker-search {
  display: flex;
  gap: 0.4rem;
}
.picker-search select {
  width: auto;
}
.picker-status {
  margin: 0;
  min-height: 1.2em;
}
.picker-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(92px, 1fr));
  gap: 0.5rem;
  overflow-y: auto;
  min-height: 200px;
  padding: 2px;
}
.picker-grid .pic {
  aspect-ratio: 1;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  padding: 8px;
  cursor: pointer;
}
.picker-grid .pic.pixabay {
  padding: 0;
  overflow: hidden;
}
.picker-grid .pic img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}
.picker-grid .pic.pixabay img {
  object-fit: cover;
}
.picker-grid .pic:hover {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px var(--primary);
}
.picker-grid .pic.loading-pic {
  opacity: 0.5;
}
.picker-upload {
  align-self: flex-start;
}
.pic-mini {
  width: 30px;
  height: 30px;
  object-fit: contain;
  border-radius: 4px;
  background: #fff;
}
.cover-control {
  display: flex;
  flex-direction: column;
  gap: 0.3em;
}
.cover-label {
  font-weight: 500;
  font-size: 0.95rem;
}

/* ---------- Modal & toasts ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 12, 20, 0.5);
  display: grid;
  place-items: center;
  z-index: 50;
  padding: 16px;
}
.modal {
  background: var(--card);
  border-radius: var(--radius);
  padding: 1.4rem;
  width: min(460px, 100%);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
}
.modal h2 {
  margin-top: 0;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 1.2rem;
}
#toasts {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  z-index: 60;
  width: min(480px, calc(100% - 32px));
}
.toast {
  background: #1f2333;
  color: #fff;
  padding: 0.7em 1em;
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
  transition: opacity 0.4s, transform 0.4s;
}
.toast.success {
  background: #0d7a4f;
}
.toast.error {
  background: #b42318;
}
.toast.hide {
  opacity: 0;
  transform: translateY(8px);
}

/* ---------- Embedded (LMS iframe) ---------- */
body.embed .topbar {
  display: none;
}
body.embed main {
  padding: 8px 8px 2rem;
}
body.embed .word-bank,
body.embed .toolbar {
  top: 0;
}
.embed-box {
  border-top: 1px solid var(--line);
  padding-top: 0.6rem;
}
.embed-box summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--primary);
}
.embed-box ol {
  margin: 0.5rem 0;
  padding-left: 1.2rem;
}
.embed-code {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 0.8rem;
  margin-bottom: 0.5rem;
}

/* ---------- Paper worksheet styles ---------- */
/* Paper stays white in dark mode, like a real sheet. */
.theme-kids .page,
.theme-classic .page {
  --card: #ffffff;
  --text: #1f2333;
  --muted: #6b7185;
  --line: #d4d8e3;
  --primary-soft: #eef0ff;
  --ok-soft: #e3f8ec;
  --bad-soft: #fde8e8;
  --amber-soft: #fef3c7;
  --amber: #b45309;
  --pen: #1d3fb8;
  color: var(--text);
  background: #fff;
  border: none;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.08), 0 12px 32px rgba(15, 20, 40, 0.14);
}
.theme-kids .doc-page,
.theme-classic .doc-page {
  font-family: 'Andika', 'Comic Sans MS', system-ui, sans-serif;
  font-size: 1.12rem;
  padding: 2.4rem 3rem 3rem;
  min-height: 60vh;
}
.theme-kids .doc-page {
  margin: 14px;
  border: 5px solid #f4a259;
  border-radius: 26px;
}
.theme-classic .doc-page {
  font-family: Georgia, 'Times New Roman', serif;
  margin: 14px;
  border: 1.5px solid #222;
}
.sheet-head {
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-areas: 'cover title' 'fields fields';
  align-items: center;
  gap: 0.4rem 1rem;
  margin-bottom: 1.4rem;
  padding-bottom: 1rem;
  border-bottom: 2px dashed #f0c9a4;
}
.theme-classic .sheet-head {
  border-bottom: 1.5px solid #222;
}
.sheet-cover {
  grid-area: cover;
  width: 76px;
  height: 76px;
  object-fit: contain;
}
.sheet-title {
  grid-area: title;
}
.sheet-head:not(:has(.sheet-cover)) .sheet-title {
  grid-column: 1 / -1;
  text-align: center;
}
.sheet-title h2 {
  margin: 0;
  font-size: 2rem;
  line-height: 1.15;
  color: #2b2d42;
}
.theme-kids .sheet-title h2 {
  color: #bc4b51;
}
.sheet-title p {
  margin: 0.2rem 0 0;
  color: var(--muted);
  font-size: 0.95rem;
}
.sheet-fields {
  grid-area: fields;
  display: flex;
  gap: 1.5rem;
  flex-wrap: wrap;
  margin-top: 0.6rem;
}
.sheet-field {
  display: flex;
  align-items: baseline;
  gap: 0.3rem;
  flex: 1 1 200px;
}
.sheet-field:last-child {
  flex: 0 1 200px;
}
.sheet-line {
  flex: 1;
  border-bottom: 1.5px solid #333;
  min-width: 90px;
  padding: 0 0.3rem;
}
.sheet-line.filled {
  font-family: 'Patrick Hand', 'Comic Sans MS', cursive;
  color: var(--pen);
  font-size: 1.25rem;
}
.theme-kids .blk-heading,
.theme-classic .blk-heading {
  font-size: 1.25rem;
  margin: 1.6em 0 0.5em;
}
.theme-kids .blk-heading {
  color: #bc4b51;
}
.theme-classic .blk-heading {
  color: #111;
  text-transform: none;
}
.with-pic {
  display: flex;
  align-items: flex-start;
  gap: 1rem;
}
.with-pic > :first-child {
  flex: 1;
  min-width: 0;
}
.blk-pic {
  width: 84px;
  height: 84px;
  object-fit: contain;
  flex: none;
  margin-top: 0.4rem;
}
.blk-pic[src^='/uploads/'] {
  width: 130px;
  height: auto;
  max-height: 130px;
  border-radius: 6px;
}
/* blanks written on a line, in "pen" */
.theme-kids .blk-text .fld-text,
.theme-kids .blk-text .fld-select,
.theme-classic .blk-text .fld-text,
.theme-classic .blk-text .fld-select {
  background: transparent;
  border: none;
  border-bottom: 2px solid #333;
  border-radius: 0;
  font-family: 'Patrick Hand', 'Comic Sans MS', cursive;
  font-size: 1.3rem;
  color: var(--pen);
  padding: 0 0.3em;
  line-height: 1.2;
}
.theme-kids .blk-text .drop-zone,
.theme-classic .blk-text .drop-zone {
  font-family: 'Patrick Hand', 'Comic Sans MS', cursive;
  font-size: 1.3rem;
  color: var(--pen);
  background: #fffdf5;
  border: 2px dashed #999;
  border-radius: 8px;
  line-height: 1.1;
}
.theme-kids .drop-zone:not(.filled),
.theme-classic .drop-zone:not(.filled) {
  background: #fffdf5 !important;
}
.theme-kids .word-bank,
.theme-classic .word-bank {
  position: static;
  margin: 0 0 1.2rem !important;
  background: #fff8ec;
  border: 2px dashed #f4a259;
  border-radius: 14px;
}
.theme-classic .word-bank {
  background: #fff;
  border: 1.5px solid #222;
  border-radius: 0;
}
.theme-kids .bank-label,
.theme-classic .bank-label {
  color: #bc4b51;
}
.theme-classic .bank-label {
  color: #111;
}
.theme-kids .chip,
.theme-classic .chip {
  font-family: 'Andika', system-ui, sans-serif;
  border-color: #888;
}
/* multiple choice as ( ) circles / [ ] boxes on paper */
.theme-kids .opt,
.theme-classic .opt {
  border: none;
  padding: 0.2em 0.5em 0.2em 0;
  margin-right: 1rem;
  font-weight: 400;
}
.theme-kids .opt:has(input:checked),
.theme-classic .opt:has(input:checked) {
  background: none;
}
.theme-kids .opt input,
.theme-classic .opt input {
  appearance: none;
  width: 1.15em;
  height: 1.15em;
  border: 2px solid #333;
  border-radius: 50%;
  display: inline-grid;
  place-items: center;
  margin: 0;
  cursor: pointer;
}
.theme-kids .opt input[type='checkbox'],
.theme-classic .opt input[type='checkbox'] {
  border-radius: 3px;
}
.theme-kids .opt input:checked::after,
.theme-classic .opt input:checked::after {
  content: '✓';
  color: var(--pen);
  font-family: 'Patrick Hand', cursive;
  font-size: 1.4em;
  line-height: 0;
  transform: translate(2px, -2px);
}
.theme-kids .blk-mcq legend,
.theme-classic .blk-mcq legend {
  font-weight: 400;
}
.theme-kids .match-item,
.theme-classic .match-item {
  border: none;
  background: transparent;
  padding: 0.3em 0;
}
.theme-kids .prompt,
.theme-classic .prompt {
  font-weight: 400;
}
/* open answers on ruled lines */
.theme-kids .fld-open,
.theme-classic .fld-open {
  font-family: 'Patrick Hand', 'Comic Sans MS', cursive;
  font-size: 1.3rem;
  color: var(--pen);
  line-height: 2rem;
  border: none;
  border-radius: 0;
  padding: 0 0.3rem;
  background: repeating-linear-gradient(to bottom, transparent 0, transparent calc(2rem - 1.5px), #9aa3b5 calc(2rem - 1.5px), #9aa3b5 2rem);
  background-attachment: local;
}
.theme-kids .blk-open .fld-open {
  min-height: 6rem;
}
.theme-kids .mark-ok.fld-text,
.theme-kids .mark-bad.fld-text,
.theme-classic .mark-ok.fld-text,
.theme-classic .mark-bad.fld-text {
  background: transparent !important;
  border-bottom-width: 3px;
}

/* library card: mini paper page */
.thumb-paper {
  height: 100%;
  background: #fff;
  color: #2b2d42;
  margin: 10px 18px 0;
  border-radius: 6px 6px 0 0;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.06), 0 6px 18px rgba(0, 0, 0, 0.12);
  padding: 12px 14px;
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-rows: auto auto 1fr;
  gap: 4px 8px;
  font-family: 'Andika', system-ui, sans-serif;
  overflow: hidden;
}
.thumb-paper.theme-kids {
  outline: 3px solid #f4a259;
  outline-offset: -7px;
  border-radius: 12px 12px 0 0;
  padding: 16px 18px;
}
.tp-title {
  font-weight: 700;
  font-size: 1rem;
  line-height: 1.15;
  color: #bc4b51;
}
.theme-classic .tp-title {
  color: #111;
  font-family: Georgia, serif;
}
.tp-name {
  grid-column: 1;
  font-size: 0.65rem;
  color: #777;
}
.thumb-paper img {
  grid-column: 2;
  grid-row: 1 / 3;
  width: 54px;
  height: 54px;
  object-fit: contain;
}
.tp-lines {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  gap: 11px;
  padding-top: 10px;
}
.tp-lines i {
  display: block;
  height: 2px;
  background: #e2e5ec;
  width: 92%;
}
.tp-lines i:nth-child(2) {
  width: 70%;
}
.tp-lines i:nth-child(4) {
  width: 55%;
}

.footer-contact {
  font-size: 0.9rem;
  margin: 0 0 0.5rem;
}
body.embed .footer-contact {
  display: none;
}
.site-footer {
  text-align: center;
  font-size: 0.75rem;
  color: var(--muted);
  padding: 1.5rem 16px 2rem;
}
body.embed .site-footer {
  padding: 0.5rem;
}

/* ---------- Printing ---------- */
@media print {
  .topbar,
  .site-footer,
  .finish-bar,
  .score-bar,
  .play-head,
  #toasts,
  .word-bank .chip.selected {
    display: none !important;
  }
  body,
  main {
    background: #fff;
    padding: 0;
  }
  .page {
    box-shadow: none !important;
    break-after: page;
  }
  .worksheet {
    max-width: none;
  }
}

/* ---------- Responsive ---------- */
@media (max-width: 800px) {
  .img-editor,
  .two-col {
    grid-template-columns: 1fr;
  }
  .field-panel {
    position: static;
  }
  .filters {
    grid-template-columns: 1fr 1fr;
  }
  .filters input {
    grid-column: 1 / -1;
  }
  .tabs {
    grid-template-columns: 1fr;
  }
  .stats {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.5rem;
  }
  .doc-page {
    padding: 1rem;
  }
  .doc-page .word-bank {
    margin: -1rem -1rem 1rem;
  }
  .theme-kids .doc-page,
  .theme-classic .doc-page {
    margin: 6px;
    padding: 1.2rem 1.1rem 1.6rem;
    border-width: 3px;
  }
  .sheet-title h2 {
    font-size: 1.5rem;
  }
  .sheet-cover {
    width: 56px;
    height: 56px;
  }
  .blk-pic {
    width: 56px;
    height: 56px;
  }
  .inline-field,
  .inline-field:has(> input + input) {
    grid-template-columns: 1fr 1fr;
  }
  .result-summary {
    text-align: left;
  }
  .share-row {
    flex-direction: column;
  }
  .word-bank,
  .toolbar {
    top: 0;
  }
  .topbar {
    position: static;
  }
}
