/* Diction Masters rich text editor (static/js/rich_text_editor.js) and the
   learner-facing .dm-rich-content it produces. */

.dm-rich-editor {
  --rt-bg: #fff;
  --rt-bar: #f7f8fa;
  --rt-line: #cbd1dd;
  --rt-line-soft: #e2e5eb;
  --rt-ink: #202b46;
  --rt-heading: #0b1a4a;
  --rt-muted: #788197;
  --rt-accent: #ffc72c;
  --rt-accent-soft: rgba(233, 174, 0, .14);
  --rt-danger: #1846E0;
  --rt-quote: #FFFAEB;
  position: relative;
  box-sizing: border-box;
  width: 100%;
  min-width: 0;
  margin: 8px 0 2px;
  border: 1px solid var(--rt-line);
  border-radius: 12px;
  background: var(--rt-bg);
  box-shadow: 0 3px 12px rgba(11, 26, 74, .06);
  color: var(--rt-ink);
  transition: border-color .15s, box-shadow .15s;
}
.dm-rich-editor *, .dm-rich-editor *::before, .dm-rich-editor *::after { box-sizing: border-box; }
.dm-rich-editor:focus-within { border-color: var(--rt-accent); box-shadow: 0 0 0 3px var(--rt-accent-soft); }
.dm-rich-editor.dm-rt-invalid { border-color: var(--rt-danger); box-shadow: 0 0 0 3px rgba(24, 70, 224, .12); }

/* Toolbar ------------------------------------------------------------- */
.dm-rt-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 6px;
  padding: 6px 8px;
  border-bottom: 1px solid var(--rt-line-soft);
  border-radius: 12px 12px 0 0;
  background: var(--rt-bar);
}
.dm-rt-group { display: flex; align-items: center; gap: 2px; padding-right: 6px; border-right: 1px solid var(--rt-line-soft); }
.dm-rt-group:last-child { margin-left: auto; padding-right: 0; border-right: 0; }
.dm-rt-btn, .dm-rt-select {
  height: 32px;
  min-width: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  margin: 0;
  padding: 0 7px;
  border: 1px solid transparent;
  border-radius: 7px;
  background: transparent;
  color: var(--rt-ink);
  font: 600 13px/1 "Plus Jakarta Sans", system-ui, Arial, sans-serif;
  text-transform: none;
  letter-spacing: 0;
  box-shadow: none;
  cursor: pointer;
}
.dm-rt-btn:hover, .dm-rt-select:hover { border-color: var(--rt-line); background: var(--rt-bg); }
.dm-rt-btn:focus-visible, .dm-rt-select:focus-visible { outline: 2px solid var(--rt-accent); outline-offset: 1px; }
.dm-rt-btn[aria-pressed="true"], .dm-rt-btn[aria-expanded="true"] { border-color: var(--rt-accent); background: var(--rt-accent-soft); color: var(--rt-heading); }
.dm-rt-icon, .dm-rt-icon svg { display: block; }
.dm-rt-select { max-width: 140px; padding: 0 6px; border-color: var(--rt-line-soft); background: var(--rt-bg); }
.dm-rt-block { width: 118px; }
.dm-rt-size { width: 72px; }
.dm-rt-font { width: 104px; }

/* Colour buttons show the colour they will apply. */
.dm-rt-swatch { position: relative; flex-direction: column; gap: 1px; font-size: 15px; font-weight: 800; }
.dm-rt-swatch::after { content: ""; width: 16px; height: 3px; border-radius: 2px; background: var(--swatch, currentColor); }
.dm-rt-swatch--mark .dm-rt-btn__text { padding: 0 2px; border-radius: 3px; background: var(--swatch); color: #202b46; font-size: 13px; }
.dm-rt-swatch--mark::after { display: none; }
.dm-rt-color-input {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  border: 0;
  opacity: 0;
  pointer-events: none;
}

/* Link, table and symbol panels ---------------------------------------- */
.dm-rt-panel { padding: 10px 12px; border-bottom: 1px solid var(--rt-line-soft); background: var(--rt-bar); }
.dm-rt-panel[hidden] { display: none; }
.dm-rt-panel__body { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 10px; }
.dm-rt-field { display: flex; flex-direction: column; gap: 4px; margin: 0; color: var(--rt-muted); font-size: 12px; font-weight: 600; }
.dm-rt-field:first-child { flex: 1 1 240px; }
.dm-rt-input {
  width: 100%;
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--rt-line);
  border-radius: 7px;
  background: var(--rt-bg);
  color: var(--rt-ink);
  font: 14px "Plus Jakarta Sans", system-ui, Arial, sans-serif;
}
.dm-rt-input:focus { outline: 2px solid var(--rt-accent); outline-offset: 0; border-color: transparent; }
.dm-rt-input--num { width: 76px; }
.dm-rt-field:has(.dm-rt-input--num) { flex: 0 0 auto; }
.dm-rt-check { display: inline-flex; align-items: center; gap: 6px; height: 34px; margin: 0; color: var(--rt-ink); font-size: 13px; font-weight: 500; white-space: nowrap; cursor: pointer; }
.dm-rt-check input { margin: 0; }
.dm-rt-pbtn {
  height: 34px;
  margin: 0;
  padding: 0 14px;
  border: 1px solid var(--rt-line);
  border-radius: 7px;
  background: var(--rt-bg);
  color: var(--rt-ink);
  font: 600 13px/1 "Plus Jakarta Sans", system-ui, Arial, sans-serif;
  text-transform: none;
  cursor: pointer;
}
.dm-rt-pbtn:hover { border-color: var(--rt-accent); }
.dm-rt-pbtn:focus-visible { outline: 2px solid var(--rt-accent); outline-offset: 1px; }
.dm-rt-pbtn--primary { border-color: var(--rt-heading); background: var(--rt-heading); color: #fff; }
.dm-rt-pbtn--primary:hover { border-color: var(--rt-accent); background: var(--rt-accent); }
.dm-rt-symbol-grid { display: grid; flex: 1 1 100%; grid-template-columns: repeat(auto-fill, minmax(38px, 1fr)); gap: 4px; }
.dm-rt-symbol {
  height: 36px;
  margin: 0;
  padding: 0;
  border: 1px solid var(--rt-line-soft);
  border-radius: 7px;
  background: var(--rt-bg);
  color: var(--rt-ink);
  font: 17px/1 "Noto Sans", "Segoe UI", "Lucida Sans Unicode", Arial, sans-serif;
  cursor: pointer;
}
.dm-rt-symbol:hover, .dm-rt-symbol:focus-visible { border-color: var(--rt-accent); background: var(--rt-accent-soft); outline: none; }

/* Writing area ----------------------------------------------------------- */
.dm-rt-surface {
  min-height: 230px;
  max-height: min(70vh, 760px);
  overflow: auto;
  padding: 16px 18px;
  outline: none;
  color: var(--rt-ink);
  background: var(--rt-bg);
  font: 16px/1.65 "Plus Jakarta Sans", system-ui, Arial, sans-serif;
  overflow-wrap: anywhere;
  cursor: text;
}
.dm-rt-empty .dm-rt-surface > :first-child::before {
  content: var(--rt-placeholder, "Start writing…");
  float: left;
  height: 0;
  color: var(--rt-muted);
  pointer-events: none;
}
.dm-rt-surface > :first-child { margin-top: 0; }
.dm-rt-surface p { margin: 0 0 .75em; }
.dm-rt-surface h2, .dm-rt-surface h3, .dm-rt-surface h4 { margin: .85em 0 .4em; line-height: 1.25; color: var(--rt-heading); }
.dm-rt-surface blockquote { margin: .8em 0; padding: .5em 1em; border-left: 4px solid var(--rt-accent); background: var(--rt-quote); }
.dm-rt-surface pre { padding: .7em 1em; border-radius: 8px; background: var(--rt-bar); font: 14px/1.5 "Courier New", monospace; white-space: pre-wrap; }
.dm-rt-surface ul, .dm-rt-surface ol { margin: 0 0 .75em; padding-left: 2em; }
.dm-rt-surface a { color: #1d5fbf; text-decoration: underline; }
.dm-rt-surface hr { margin: 1.1em 0; border: 0; border-top: 1px solid var(--rt-line); }
.dm-rt-surface table { width: auto; max-width: 100%; margin: .5em 0 1em; border-collapse: collapse; }
.dm-rt-surface td, .dm-rt-surface th { min-width: 60px; padding: 6px 9px; border: 1px solid var(--rt-line); vertical-align: top; }
.dm-rt-surface th { background: var(--rt-bar); font-weight: 700; text-align: left; }

/* Footer ------------------------------------------------------------------ */
.dm-rt-footer {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 7px 12px;
  border-top: 1px solid var(--rt-line-soft);
  border-radius: 0 0 12px 12px;
  color: var(--rt-muted);
  font: 12px/1.4 "Plus Jakarta Sans", system-ui, Arial, sans-serif;
}
.dm-rt-hint--error { color: var(--rt-danger); font-weight: 600; }
.dm-rt-count { white-space: nowrap; }
.dm-rich-editor .dm-rich-text-source, .dm-rich-editor > textarea { display: none !important; }

/* Full screen --------------------------------------------------------- */
.dm-rich-editor--full {
  position: fixed;
  inset: 12px;
  z-index: 10000;
  display: flex;
  flex-direction: column;
  width: auto;
  margin: 0;
  box-shadow: 0 0 0 100vmax rgba(10, 16, 32, .55), 0 20px 50px rgba(11, 26, 74, .3);
}
.dm-rich-editor--full .dm-rt-surface { flex: 1; max-height: none; padding: 28px max(18px, calc((100% - 780px) / 2)); }
html.dm-rt-noscroll { overflow: hidden; }

/* Django Admin: fill the row beside the field label. */
.form-row .dm-rich-editor, .flex-container .dm-rich-editor { flex: 1 1 auto; max-width: 100%; }
.aligned .form-row .dm-rich-editor + .help, .aligned .dm-rich-editor ~ .help { margin-left: 0; }

/* Django Admin dark theme. */
html[data-theme="dark"] .dm-rich-editor {
  --rt-bg: #1f2430;
  --rt-bar: #272d3a;
  --rt-line: #3d4658;
  --rt-line-soft: #333b4b;
  --rt-ink: #e6e9ef;
  --rt-heading: #fff;
  --rt-muted: #9aa3b5;
  --rt-quote: #0B1A4A;
}
@media (prefers-color-scheme: dark) {
  html[data-theme="auto"] .dm-rich-editor {
    --rt-bg: #1f2430;
    --rt-bar: #272d3a;
    --rt-line: #3d4658;
    --rt-line-soft: #333b4b;
    --rt-ink: #e6e9ef;
    --rt-heading: #fff;
    --rt-muted: #9aa3b5;
    --rt-quote: #0B1A4A;
  }
}
html[data-theme="dark"] .dm-rt-surface a { color: #8ab4ff; }
html[data-theme="dark"] .dm-rt-pbtn--primary { background: var(--rt-accent); border-color: var(--rt-accent); color: #0b1a4a; }
@media (prefers-color-scheme: dark) {
  html[data-theme="auto"] .dm-rt-surface a { color: #8ab4ff; }
  html[data-theme="auto"] .dm-rt-pbtn--primary { background: var(--rt-accent); border-color: var(--rt-accent); color: #0b1a4a; }
}

@media (max-width: 720px) {
  .dm-rt-toolbar { gap: 3px; padding: 5px; }
  .dm-rt-group { padding-right: 3px; }
  .dm-rt-group:last-child { margin-left: 0; }
  .dm-rt-btn { min-width: 30px; height: 30px; padding: 0 5px; }
  .dm-rt-select { height: 30px; }
  .dm-rt-surface { min-height: 190px; padding: 13px; }
  .dm-rt-hint { display: none; }
  .dm-rt-hint--error { display: inline; }
  .dm-rich-editor--full { inset: 0; border-radius: 0; }
}
@media (prefers-reduced-motion: reduce) { .dm-rich-editor { transition: none; } }

/* Learner-facing rendering ----------------------------------------------- */
.dm-rich-content { overflow-wrap: anywhere; }
.dm-rich-content > :first-child { margin-top: 0; }
.dm-rich-content p { margin: 0 0 .8em; }
.dm-rich-content p:last-child, .dm-rich-content > :last-child { margin-bottom: 0; }
.dm-rich-content h2, .dm-rich-content h3, .dm-rich-content h4 { margin: .8em 0 .45em; line-height: 1.25; }
.dm-rich-content ul, .dm-rich-content ol { margin: 0 0 .8em; padding-left: 1.6em; }
.dm-rich-content blockquote { margin: .8em 0; padding: .45em .9em; border-left: 3px solid #ffc72c; background: rgba(255, 199, 44, .11); }
.dm-rich-content pre { overflow-x: auto; padding: .6em .9em; border-radius: 8px; background: rgba(11, 26, 74, .06); white-space: pre-wrap; }
.dm-rich-content a { text-decoration: underline; }
.dm-rich-content hr { margin: 1em 0; border: 0; border-top: 1px solid rgba(11, 26, 74, .18); }
.dm-rich-content table { display: block; max-width: 100%; overflow-x: auto; margin: .5em 0 1em; border-collapse: collapse; }
.dm-rich-content td, .dm-rich-content th { padding: 5px 8px; border: 1px solid #bac2d0; vertical-align: top; }
.dm-rich-content th { font-weight: 700; text-align: left; }
