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

body {
  margin: 0;
  font-family: system-ui, sans-serif;
  font-size: 1rem;
  color: #1a1a1a;
  background: #fafafa;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

header {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid #ddd;
  background: #fff;
}

.wordmark { font-weight: 700; font-size: 1.1rem; }

nav {
  display: flex;
  gap: 0;
  border-bottom: 1px solid #ddd;
  background: #fff;
}

nav .tab {
  flex: 1;
  padding: 0.6rem;
  border: none;
  border-bottom: 3px solid transparent;
  background: none;
  cursor: pointer;
  font-size: 0.95rem;
}

nav .tab.active {
  border-bottom-color: #2563eb;
  font-weight: 600;
}

main {
  flex: 1;
  padding: 1rem;
  max-width: 680px;
  width: 100%;
  margin: 0 auto;
}

footer {
  padding: 0.75rem 1rem;
  text-align: center;
  font-size: 0.8rem;
  color: #666;
  border-top: 1px solid #ddd;
}

footer a { color: inherit; }

/* Shared */
.toolbar {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.hint { color: #666; }

button {
  padding: 0.4rem 0.9rem;
  border: 1px solid #bbb;
  border-radius: 4px;
  background: #fff;
  cursor: pointer;
  font-size: 0.9rem;
}

button:hover { background: #f0f0f0; }

input[type="text"],
input[type="email"],
input[type="number"],
select,
textarea {
  width: 100%;
  padding: 0.5rem;
  border: 1px solid #bbb;
  border-radius: 4px;
  font-size: 0.95rem;
  font-family: inherit;
  background: #fff;
}

#build-title { margin-bottom: 0.5rem; }

#build-body {
  min-height: 280px;
  resize: vertical;
}

#build-import-json {
  resize: vertical;
  font-family: monospace;
  font-size: 0.85rem;
}

/* Browse */
.browse-section {
  margin-top: 1.25rem;
}

.browse-section-heading {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #888;
}

.lesson-loading {
  list-style: none;
  padding: 0.4rem 0;
}

.lesson-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 0.75rem;
}

.lesson-card {
  display: flex;
  flex-direction: column;
  border: 1px solid #ddd;
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
}

.lesson-card-actions {
  display: flex;
  align-items: center;
  gap: 0.1rem;
  padding: 0.35rem 0.5rem;
  border-bottom: 1px solid #eee;
  background: #f9fafb;
}

.lesson-card-actions .lesson-status {
  margin-left: auto;
}

.lesson-card-actions .lesson-sync {
  margin-left: auto;
}

.lesson-link {
  display: block;
  padding: 0.6rem 0.75rem;
  border: none;
  background: none;
  text-align: left;
  font-size: 1.05rem;
  font-weight: 500;
  cursor: pointer;
  color: #1a1a1a;
  line-height: 1.3;
}

.lesson-link:hover { background: #f0f6ff; color: #2563eb; }

.lesson-delete, .lesson-edit {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 1rem;
  padding: 0.2rem 0.35rem;
  border-radius: 4px;
}

.lesson-delete:hover, .lesson-edit:hover { background: #f3f4f6; }

/* Explore — lesson viewer */
.lesson-body {
  margin-top: 1rem;
}

.lesson-text {
  margin-bottom: 1.25rem;
}

.lesson-text h3 {
  margin: 0 0 0.25rem;
}

.lesson-text p {
  margin: 0;
  white-space: pre-wrap;
}

.lesson-highlight {
  border-left: 4px solid #2563eb;
  padding: 0.6rem 1rem;
  background: #eff6ff;
  border-radius: 0 4px 4px 0;
  margin-bottom: 1.25rem;
}

.lesson-highlight strong {
  display: block;
  margin-bottom: 0.25rem;
}

.lesson-highlight p {
  margin: 0;
  white-space: pre-wrap;
}

.element-prompt {
  margin: 0 0 0.5rem;
  font-weight: 500;
  white-space: pre-wrap;
}

.lesson-choice {
  margin-bottom: 1.25rem;
}

.choice-options {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.choice-option {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
}

.lesson-question {
  margin-bottom: 1.25rem;
}

.lesson-question textarea {
  width: 100%;
  padding: 0.5rem;
  border: 1px solid #bbb;
  border-radius: 4px;
  font-size: 0.95rem;
  font-family: inherit;
  resize: vertical;
}

.question-count {
  text-align: right;
  font-size: 0.8rem;
  margin-top: 0.2rem;
}

/* Image */
.lesson-image {
  margin: 0 0 1.25rem;
  padding: 0;
}

.lesson-image img {
  display: block;
  width: 100%;
  border-radius: 4px;
}

.lesson-image figcaption {
  margin-top: 0.35rem;
  font-size: 0.85rem;
  color: #555;
}

.lesson-image--empty {
  margin-bottom: 1.25rem;
  color: #999;
  font-size: 0.9rem;
}

/* Sketch */
.lesson-sketch {
  margin-bottom: 1.25rem;
}

.sketch-toolbar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.4rem;
}

.sketch-color-wrap,
.sketch-size-wrap {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.9rem;
  color: #444;
}

.sketch-color {
  width: 2rem;
  height: 2rem;
  padding: 0;
  border: 1px solid #bbb;
  border-radius: 4px;
  cursor: pointer;
  background: none;
}

.sketch-size {
  max-width: 8rem;
  cursor: pointer;
}

.sketch-wrap {
  position: relative;
  width: 100%;
  border: 1px solid #bbb;
  border-radius: 4px;
  overflow: hidden;
}

.sketch-canvas {
  display: block;
  width: 100%;
  aspect-ratio: 2 / 1;
  background: #fff;
  cursor: crosshair;
  touch-action: none;
}

.sketch-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none;
}

/* Memory match */
.lesson-memory { margin-bottom: 1.25rem; }

.memory-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 0.5rem;
}

.memory-card {
  min-height: 64px;
  border: 2px solid #bbb;
  border-radius: 6px;
  background: #e2e8f0;
  cursor: pointer;
  font-size: 0.9rem;
  padding: 0.4rem;
  position: relative;
  overflow: hidden;
  transition: background 0.15s, border-color 0.15s;
}

.memory-card .memory-front { display: none; }
.memory-card .memory-back  { display: block; font-size: 1.4rem; color: #94a3b8; }

.memory-card.flipped .memory-front { display: block; }
.memory-card.flipped .memory-back  { display: none; }

.memory-card.matched {
  border-color: #16a34a;
  background: #dcfce7;
  cursor: default;
}

.memory-status { margin-top: 0.5rem; }

/* Sequence */
.lesson-sequence { margin-bottom: 1.25rem; }

.sequence-list {
  list-style: none;
  padding: 0;
  margin: 0 0 0.5rem;
}

.sequence-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.6rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  margin-bottom: 0.3rem;
  background: #fff;
  cursor: grab;
  user-select: none;
}

.sequence-item.dragging  { opacity: 0.4; }
.sequence-item.drag-over { border-color: #2563eb; background: #eff6ff; }
.sequence-item.correct   { border-color: #16a34a; background: #dcfce7; }
.sequence-item.incorrect { border-color: #c00;    background: #fff1f2; }

.sequence-handle { color: #94a3b8; font-size: 1rem; }
.sequence-status { margin-top: 0.35rem; }

/* Bin sort */
.lesson-binsort { margin-bottom: 1.25rem; }

.binsort-tray {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  min-height: 2.5rem;
  padding: 0.5rem;
  border: 2px dashed #bbb;
  border-radius: 6px;
  margin-bottom: 0.75rem;
  background: #fafafa;
  transition: border-color 0.15s, background 0.15s;
}

.binsort-tray.drag-over { border-color: #2563eb; background: #eff6ff; }

.binsort-bins {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 0.6rem;
}

.binsort-bin {
  border: 2px solid #ddd;
  border-radius: 6px;
  padding: 0.5rem;
  background: #fff;
  min-height: 80px;
  transition: border-color 0.15s, background 0.15s;
}

.binsort-bin.drag-over { border-color: #2563eb; background: #eff6ff; }
.binsort-bin.bin-correct { border-color: #16a34a; background: #dcfce7; }

.binsort-bin-title {
  font-weight: 600;
  font-size: 0.85rem;
  margin-bottom: 0.4rem;
  color: #334155;
}

.binsort-bin-items {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
}

.binsort-item {
  padding: 0.25rem 0.55rem;
  border: 1px solid #bbb;
  border-radius: 4px;
  background: #f1f5f9;
  cursor: grab;
  font-size: 0.88rem;
  user-select: none;
  transition: background 0.12s;
}

.binsort-item.dragging  { opacity: 0.4; }
.binsort-item.item-correct { border-color: #16a34a; background: #dcfce7; }
.binsort-item.item-wrong   { border-color: #f87171; background: #fff1f2; }

/* Scratch to reveal */
.lesson-scratch { margin-bottom: 1.25rem; }

.scratch-wrap {
  position: relative;
  border-radius: 4px;
  overflow: hidden;
  line-height: 0;
}

.scratch-img {
  display: block;
  width: 100%;
}

.scratch-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: none;
}

.scratch-hint { margin-top: 0.3rem; }

/* Image quiz */
.lesson-imagequiz { margin-bottom: 1.25rem; }

.iq-wrap {
  position: relative;
  border-radius: 4px;
  overflow: hidden;
  line-height: 0;
  margin-bottom: 0.6rem;
}

.iq-img {
  display: block;
  width: 100%;
}

.iq-grid {
  position: absolute;
  inset: 0;
  display: grid;
}

.iq-tile {
  background: #334155;
  border: 1px solid #1e293b;
  cursor: pointer;
  transition: background 0.2s;
}

.iq-tile:hover:not(.revealed) { background: #475569; }
.iq-tile.revealed { background: transparent; pointer-events: none; }

.iq-controls {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.35rem;
}

.iq-controls input { flex: 1; }

.iq-status { margin: 0; }

/* Build editor — new type rows */
.pair-row, .seq-row, .bin-editor-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-bottom: 0.35rem;
}

.pair-row input, .seq-row input { flex: 1; margin-bottom: 0; }
.pair-sep { color: #94a3b8; flex-shrink: 0; }
.seq-num  { color: #94a3b8; font-size: 0.85rem; min-width: 1.4rem; flex-shrink: 0; }

.bin-editor {
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  padding: 0.6rem;
  margin-bottom: 0.5rem;
  background: #f8fafc;
}

.bin-editor-row input { flex: 1; margin-bottom: 0; }

/* Markdown element */
.lesson-markdown {
  margin-bottom: 1.25rem;
}

.markdown-body { line-height: 1.6; }
.markdown-body h1, .markdown-body h2, .markdown-body h3,
.markdown-body h4, .markdown-body h5, .markdown-body h6 {
  margin: 1rem 0 0.4rem;
  line-height: 1.25;
}
.markdown-body p  { margin: 0 0 0.75rem; }
.markdown-body ul,
.markdown-body ol { padding-left: 1.5rem; margin: 0 0 0.75rem; }
.markdown-body li { margin-bottom: 0.2rem; }
.markdown-body a  { color: #2563eb; }
.markdown-body code {
  font-family: monospace;
  font-size: 0.88em;
  background: #f1f5f9;
  padding: 0.1em 0.35em;
  border-radius: 3px;
}
.markdown-body pre {
  background: #1e293b;
  color: #e2e8f0;
  padding: 0.85rem 1rem;
  border-radius: 6px;
  overflow-x: auto;
  margin: 0 0 0.75rem;
}
.markdown-body pre code {
  background: none;
  padding: 0;
  color: inherit;
  font-size: 0.88rem;
}
.markdown-body blockquote {
  border-left: 4px solid #cbd5e1;
  margin: 0 0 0.75rem;
  padding: 0.4rem 1rem;
  color: #64748b;
}
.markdown-body hr {
  border: none;
  border-top: 1px solid #e2e8f0;
  margin: 1rem 0;
}
.markdown-body table {
  border-collapse: collapse;
  width: 100%;
  margin-bottom: 0.75rem;
}
.markdown-body th, .markdown-body td {
  border: 1px solid #e2e8f0;
  padding: 0.35rem 0.6rem;
  text-align: left;
}
.markdown-body th { background: #f8fafc; font-weight: 600; }

/* Link element */
.lesson-link-el {
  margin-bottom: 1.25rem;
}

.link-heading {
  margin: 0 0 0.3rem;
  font-weight: 500;
}

.lesson-link-anchor {
  color: #2563eb;
  font-size: 1rem;
}

.lesson-link-el--empty {
  color: #999;
  font-size: 0.9rem;
  margin-bottom: 1.25rem;
}

/* TTS bar */
#tts-bar {
  display: flex;
  gap: 0.35rem;
  align-items: center;
  margin-bottom: 0.75rem;
}

#tts-bar button {
  padding: 0.25rem 0.65rem;
  font-size: 0.85rem;
  border: 1px solid #bbb;
  border-radius: 4px;
  background: #fff;
  cursor: pointer;
  line-height: 1.5;
}

#tts-bar button:hover:not(:disabled) { background: #f0f0f0; }
#tts-bar button:disabled { opacity: 0.35; cursor: default; }

#tts-play {
  min-width: 5rem;
  font-weight: 500;
}

/* Image asset manager */
.img-assets-panel {
  border: 1px solid #d1d5db;
  border-radius: 6px;
  padding: 0.75rem;
  margin-bottom: 1rem;
  background: #f8fafc;
}

.img-assets-label {
  font-size: 0.85rem;
  color: #374151;
  display: block;
  margin-bottom: 0.5rem;
}

.img-asset-row {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  margin-bottom: 0.6rem;
}

.img-asset-seq {
  font-size: 0.8rem;
  font-weight: 600;
  color: #6b7280;
  min-width: 2rem;
  flex-shrink: 0;
  padding-top: 0.45rem;
}

.img-asset-thumb {
  width: 3rem;
  height: 3rem;
  object-fit: cover;
  border-radius: 3px;
  border: 1px solid #ddd;
  flex-shrink: 0;
}

.img-asset-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.img-asset-desc {
  margin-bottom: 0;
}

.img-asset-thumb--empty {
  background: #e5e7eb;
  border: 2px dashed #9ca3af;
}

.img-asset-upload {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.img-asset-upload input[type="file"] {
  flex: 1;
  min-width: 0;
  width: auto;
  padding: 0.3rem;
  font-size: 0.85rem;
}

/* Build — element editor */
.element-editor {
  border: 1px solid #ddd;
  border-radius: 6px;
  padding: 0.75rem;
  margin-bottom: 0.75rem;
  background: #fff;
}

.el-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}

.el-type-badge {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  color: #555;
  letter-spacing: 0.04em;
}

.el-controls {
  display: flex;
  gap: 0.25rem;
}

.el-controls button,
.el-choice-add,
.el-choice-del {
  padding: 0.15rem 0.45rem;
  font-size: 0.8rem;
  border: 1px solid #ccc;
  border-radius: 3px;
  background: #fafafa;
  cursor: pointer;
  line-height: 1.4;
}

.el-controls button:hover,
.el-choice-add:hover,
.el-choice-del:hover { background: #f0f0f0; }

.el-del { color: #c00; }

.el-preview > * { margin-bottom: 0; }

.element-editor input[type="text"],
.element-editor textarea {
  margin-bottom: 0.4rem;
}

.el-label {
  display: flex;
  align-items: center;
  font-size: 0.9rem;
  color: #444;
  margin-top: 0.5rem;
}

.choice-row {
  display: flex;
  gap: 0.4rem;
  margin-bottom: 0.35rem;
  align-items: center;
}

.choice-row input { margin-bottom: 0; flex: 1; }

/* Lesson status badges */
.lesson-status {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.1rem 0.4rem;
  border-radius: 3px;
  white-space: nowrap;
}

.lesson-status.published { background: #dcfce7; color: #166534; }
.lesson-status.draft     { background: #f1f5f9; color: #64748b; }

.lesson-sync {
  font-size: 0.8rem;
  padding: 0.15rem 0.5rem;
  border: 1px solid #2563eb;
  border-radius: 4px;
  background: #fff;
  color: #2563eb;
  cursor: pointer;
  font-family: inherit;
}
.lesson-sync:hover { background: #eff6ff; }
.lesson-sync:disabled { opacity: 0.5; cursor: default; }

/* Submit section in reader */
.submit-section {
  margin-top: 2rem;
  padding-top: 1rem;
  border-top: 1px solid #ddd;
}

.submit-label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.9rem;
  margin-bottom: 0.5rem;
}

.submit-id { margin-bottom: 0.25rem; }

/* Response viewer */
.response-meta {
  margin-bottom: 1rem;
  font-size: 0.85rem;
}

.response-answer {
  padding: 0.6rem 0;
  border-bottom: 1px solid #eee;
}

.response-prompt {
  font-weight: 500;
  margin-bottom: 0.25rem;
  font-size: 0.9rem;
  color: #444;
}

.response-value {
  white-space: pre-wrap;
  font-size: 0.95rem;
}

/* Submission config in build editor */
.submission-config {
  border: 1px solid #ddd;
  border-radius: 6px;
  padding: 0.6rem 0.75rem;
  margin-bottom: 0.75rem;
  background: #fff;
}

.submission-toggle {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
}

.submission-options {
  margin-top: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  padding-left: 0.25rem;
}

.submission-option {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.85rem;
  color: #444;
  cursor: pointer;
}

/* AI editing */
.ai-doc-card {
  border: 1px solid #a5b4fc;
  border-radius: 6px;
  padding: 0.75rem;
  margin-bottom: 1rem;
  background: #eef2ff;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.ai-doc-label {
  font-size: 0.85rem;
  color: #3730a3;
}

.el-ai-panel {
  margin-top: 0.5rem;
  padding-top: 0.5rem;
  border-top: 1px solid #e0e7ff;
  background: #f5f3ff;
  border-radius: 0 0 4px 4px;
  padding: 0.5rem;
}

/* Home portal */
.portal {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 1rem;
  margin-top: 1rem;
}

.portal-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
  padding: 1.5rem 1rem;
  border: 1px solid #ddd;
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  text-decoration: none;
  color: #1a1a1a;
  font-family: inherit;
  font-size: inherit;
  text-align: center;
  transition: box-shadow 0.15s, border-color 0.15s;
}

.portal-card:hover {
  border-color: #2563eb;
  box-shadow: 0 2px 8px rgba(37, 99, 235, 0.12);
  background: #fff;
}

.portal-icon { font-size: 2rem; }

.portal-label {
  font-weight: 600;
  font-size: 1rem;
}

.portal-desc {
  font-size: 0.85rem;
  color: #555;
}

/* Home account section */
.home-account {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-top: 1.25rem;
  flex-wrap: wrap;
}

.home-login-btn {
  padding: 0.45rem 1.2rem;
  border: 1px solid #2563eb;
  border-radius: 6px;
  background: #fff;
  color: #2563eb;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
}

.home-login-btn:hover { background: #eff6ff; }

.home-tier {
  font-size: 0.9rem;
  font-weight: 500;
  padding: 0.3rem 0.75rem;
  border-radius: 999px;
}

.home-tier--free {
  color: #555;
  background: #f3f4f6;
}

.home-tier--paid {
  color: #065f46;
  background: #d1fae5;
}

.home-upgrade-btn {
  padding: 0.45rem 1.1rem;
  border: none;
  border-radius: 6px;
  background: #2563eb;
  color: #fff;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
}

.home-upgrade-btn:hover { background: #1d4ed8; }
.home-upgrade-btn:disabled { opacity: 0.6; cursor: default; }

/* Auth view */
.auth-view {
  max-width: 380px;
}

.auth-view h2 { margin-top: 0; }

.auth-view form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.error { color: #c00; font-size: 0.9rem; min-height: 1.2em; }
