:root {
  --paper:      #F5F7F9;
  --surface:    #FFFFFF;
  --ink:        #1B2732;
  --muted:      #56626E;
  --faint:      #8A95A1;
  --line:       #E1E7ED;
  --line-soft:  #EEF2F5;
  --accent:     #345B7A;
  --accent-ink: #24405A;
  --accent-soft:#E9F0F5;
  --ok:         #2F7D5B;
  --ok-soft:    #E7F2EC;
  --warn:       #AF423A;
  --warn-soft:  #F7EAE8;
  --spell:      #C4443B;

  --reading: Georgia, "Iowan Old Style", "Times New Roman", serif;
  --ui: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  --radius: 10px;
  --shadow: 0 1px 2px rgba(27, 39, 50, .04), 0 6px 20px rgba(27, 39, 50, .06);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--ui);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Topbar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: .7rem 1.25rem;
  background: rgba(245, 247, 249, .9);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.topbar .brand { display: flex; flex-direction: column; min-width: 0; }
.topbar .titel {
  font-family: var(--reading);
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: .1px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.topbar .fag { font-size: .78rem; color: var(--muted); letter-spacing: .3px; }
.topbar .spacer { flex: 1; }

.meta {
  display: flex; align-items: center; gap: 1.1rem;
  font-size: .82rem; color: var(--muted);
}
.meta .who { font-weight: 600; color: var(--ink); }
.meta .clock { font-variant-numeric: tabular-nums; }
.meta .clock.lav { color: var(--warn); font-weight: 600; }

/* Gemme-status: sidens signatur — stille når alt er vel, tydelig når ikke */
.savestate {
  display: inline-flex; align-items: center; gap: .45rem;
  font-size: .8rem; padding: .3rem .7rem; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface); color: var(--muted);
  font-variant-numeric: tabular-nums; white-space: nowrap;
  transition: background .2s, color .2s, border-color .2s;
}
.savestate .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--faint); }
.savestate.saving .dot { background: var(--accent); animation: pulse 1s ease-in-out infinite; }
.savestate.saved  { color: var(--ok); border-color: var(--ok-soft); background: var(--ok-soft); }
.savestate.saved  .dot { background: var(--ok); }
.savestate.unsaved{ color: var(--warn); border-color: var(--warn-soft); background: var(--warn-soft); font-weight: 600; }
.savestate.unsaved .dot { background: var(--warn); }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .35; } }

/* ---------- Knapper ---------- */
.btn {
  font: inherit; font-size: .88rem; font-weight: 600;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  padding: .55rem 1rem; border-radius: 8px; cursor: pointer;
  transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { border-color: var(--accent); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-ink); border-color: var(--accent-ink); }
.btn.ghost { background: transparent; }
.btn.warn { background: var(--warn-soft); border-color: var(--warn); color: var(--warn); }
.btn.warn:hover { background: var(--warn); color: #fff; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn:focus-visible, a:focus-visible, textarea:focus-visible, input:focus-visible {
  outline: 3px solid var(--accent-soft); outline-offset: 1px;
}

/* ---------- Layout ---------- */
.stage {
  display: grid;
  grid-template-columns: minmax(320px, 6fr) 7fr;
  gap: 1px;
  background: var(--line);
  height: calc(100dvh - 57px);
}
.panel { background: var(--paper); overflow: auto; min-width: 0; }

.opgave { padding: 1.75rem clamp(1.25rem, 3vw, 2.5rem) 4rem; }
.opgave .eyebrow {
  font-family: var(--ui); font-size: .72rem; font-weight: 700; letter-spacing: 1.4px;
  text-transform: uppercase; color: var(--accent); margin: 0 0 .9rem;
}
.opgave-body { font-family: var(--reading); font-size: 1.02rem; line-height: 1.68; color: #253038; }
.opgave-body h1, .opgave-body h2, .opgave-body h3 { line-height: 1.3; margin: 1.6em 0 .5em; }
.opgave-body h1 { font-size: 1.5rem; }
.opgave-body h2 { font-size: 1.22rem; }
.opgave-body h3 { font-size: 1.05rem; }
.opgave-body p { margin: 0 0 1em; }
.opgave-body ul, .opgave-body ol { margin: 0 0 1em; padding-left: 1.3em; }
.opgave-body li { margin: .25em 0; }
.opgave-body table { border-collapse: collapse; margin: 1em 0; font-size: .92rem; width: 100%; }
.opgave-body th, .opgave-body td { border: 1px solid var(--line); padding: .4em .6em; text-align: left; }
.opgave-body th { background: var(--line-soft); }
.opgave-body img { max-width: 100%; height: auto; display: block; margin: 1em 0; border: 1px solid var(--line); }
.opgave-body blockquote {
  margin: 1em 0; padding: .4em 1em; border-left: 3px solid var(--accent);
  color: var(--muted); background: var(--surface);
}
.opgave-body code {
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  font-size: .88em; background: var(--surface); padding: .1em .35em; border-radius: 4px;
}

/* ---------- Skriveflade ---------- */
.besvarelse { display: flex; flex-direction: column; overflow: hidden; }
.besvarelse .kant {
  display: flex; align-items: center; gap: .75rem;
  padding: .65rem 1.25rem; border-bottom: 1px solid var(--line); background: var(--paper);
}
.besvarelse .kant .label {
  font-size: .72rem; font-weight: 700; letter-spacing: 1.4px; text-transform: uppercase; color: var(--faint);
}
.besvarelse .kant .spacer { flex: 1; }
.wordcount { font-size: .8rem; color: var(--muted); font-variant-numeric: tabular-nums; }

.toggle {
  display: inline-flex; align-items: center; gap: .4rem;
  font-size: .8rem; color: var(--muted); cursor: pointer; user-select: none;
}
.toggle input { accent-color: var(--accent); width: 15px; height: 15px; }

.editor-wrap {
  position: relative; flex: 1; overflow: hidden; background: var(--surface);
}
/* backdrop og textarea SKAL dele nøjagtig samme box, ellers rammer understregningen skævt */
.backdrop, #editor {
  margin: 0;
  padding: 1.75rem clamp(1.25rem, 3vw, 2.5rem);
  border: 0;
  width: 100%; height: 100%;
  font-family: var(--reading);
  font-size: 1.05rem;
  line-height: 1.7;
  letter-spacing: normal;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: normal;
  tab-size: 4;
}
.backdrop {
  position: absolute; inset: 0;
  color: transparent;
  pointer-events: none;
  overflow: hidden;
  user-select: none;
}
.backdrop mark {
  background: transparent; color: transparent;
  text-decoration: underline wavy var(--spell);
  text-decoration-skip-ink: none;
  text-underline-offset: 2px;
  cursor: pointer;
}
#editor {
  position: relative;
  background: transparent;
  color: var(--ink);
  caret-color: var(--accent);
  resize: none;
  overflow: auto;
  outline: none;
}
#editor::placeholder { color: var(--faint); }
#editor:disabled { color: var(--muted); }

/* ---------- Stavekontrol-panel ---------- */
.spell-panel {
  border-top: 1px solid var(--line); background: var(--paper);
  max-height: 34%; overflow: auto; padding: .6rem 1.25rem 1rem;
}
.spell-panel.tom { display: none; }
.spell-panel h4 {
  margin: .6rem 0 .5rem; font-size: .72rem; font-weight: 700; letter-spacing: 1.2px;
  text-transform: uppercase; color: var(--faint);
}
.spell-word {
  display: inline-flex; align-items: center; gap: .4rem;
  margin: 0 .5rem .5rem 0; padding: .3rem .55rem;
  border: 1px solid var(--line); border-radius: 7px; background: var(--surface);
  font-size: .85rem; cursor: pointer;
}
.spell-word:hover { border-color: var(--accent); }
.spell-word .fejl { text-decoration: underline wavy var(--spell); text-underline-offset: 2px; }
.spell-word .cnt { color: var(--faint); font-size: .75rem; }
.suggest-row { margin: 0 0 .6rem; }
.suggest-row .sug {
  display: inline-block; margin: 0 .35rem .35rem 0; padding: .25rem .6rem;
  border: 1px solid var(--accent-soft); background: var(--accent-soft); color: var(--accent-ink);
  border-radius: 6px; font-size: .82rem; cursor: pointer;
}
.suggest-row .sug:hover { background: var(--accent); color: #fff; }
.suggest-row .ingen { color: var(--muted); font-size: .82rem; font-style: italic; }

/* ---------- Overlays (start + kvittering) ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center;
  background: rgba(27, 39, 50, .35); backdrop-filter: blur(3px);
  padding: 1.25rem;
}
.overlay.skjult { display: none; }
.card {
  width: 100%; max-width: 440px; background: var(--surface);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 2rem; border: 1px solid var(--line);
}
.card h2 { font-family: var(--reading); margin: 0 0 .3rem; font-size: 1.4rem; }
.card .sub { color: var(--muted); font-size: .9rem; margin: 0 0 1.4rem; }
.field { margin: 0 0 1rem; }
.field label { display: block; font-size: .8rem; font-weight: 600; color: var(--muted); margin: 0 0 .3rem; }
.field input, .field select {
  width: 100%; font: inherit; padding: .6rem .75rem;
  border: 1px solid var(--line); border-radius: 8px; background: var(--paper); color: var(--ink);
}
.card .fejl { color: var(--warn); font-size: .85rem; margin: -.5rem 0 0; }
.card .fejl.skjult { display: none; }
.card .row { display: flex; gap: .6rem; margin-top: 1.4rem; }
.card .row .btn { flex: 1; }
.card .kvit-linje { display: flex; justify-content: space-between; padding: .4rem 0; border-bottom: 1px solid var(--line-soft); font-size: .9rem; }
.card .kvit-linje span:first-child { color: var(--muted); }
.card .kvit-linje span:last-child { font-weight: 600; font-variant-numeric: tabular-nums; }
.card .stor-ok { color: var(--ok); font-size: 2.2rem; text-align: center; margin: 0 0 .5rem; }

/* ---------- Index (flere prøver) ---------- */
.index-wrap { max-width: 720px; margin: 0 auto; padding: 3rem 1.5rem; }
.index-wrap h1 { font-family: var(--reading); font-size: 1.6rem; }
.exam-link {
  display: block; text-decoration: none; color: inherit;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 1.1rem 1.25rem; margin: .75rem 0; box-shadow: var(--shadow);
}
.exam-link:hover { border-color: var(--accent); }
.exam-link .t { font-family: var(--reading); font-size: 1.1rem; font-weight: 600; }
.exam-link .f { color: var(--muted); font-size: .85rem; }

/* ---------- Lærer ---------- */
.laerer-wrap { max-width: 900px; margin: 0 auto; padding: 2.5rem 1.5rem; }
.laerer-wrap h1 { font-family: var(--reading); }
.laerer-header { display: flex; align-items: baseline; gap: 1rem; justify-content: space-between; }
.laerer-header .badge {
  background: var(--accent-soft); color: var(--accent-ink); border-radius: 999px;
  padding: .2rem .7rem; font-size: .78rem; font-weight: 600;
}
.laerer-hvem { color: var(--muted); font-size: .9rem; }
.logud-form { display: inline; }
.hent-alle { margin: 1.2rem 0 0; }
.hent-alle .btn { display: inline-block; text-decoration: none; }
table.subs { width: 100%; border-collapse: collapse; font-size: .9rem; margin-top: 1rem; }
table.subs th, table.subs td { text-align: left; padding: .55rem .6rem; border-bottom: 1px solid var(--line); }
table.subs th { color: var(--muted); font-size: .78rem; text-transform: uppercase; letter-spacing: .6px; }
.laerer-wrap .skjult { display: none; }

/* ---------- Status/statistik-snapshot ---------- */
.laerer-wrap .snapshot {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 1px; background: var(--line); border: 1px solid var(--line);
  border-radius: 8px; overflow: hidden; margin: 1rem 0 1.6rem;
}
.laerer-wrap .stat { background: var(--surface); padding: .9rem 1rem; }
.laerer-wrap .stat .n {
  font-family: var(--reading); font-size: 1.3rem; font-variant-numeric: tabular-nums; line-height: 1;
}
.laerer-wrap .stat .l { font-size: .76rem; color: var(--muted); margin-top: .3rem; }

/* ---------- Login ---------- */
.login-wrap { max-width: 380px; margin: 15vh auto; padding: 0 1.5rem; }
.login-wrap .card .fejl { color: var(--warn); font-size: .85rem; margin: 0 0 1rem; }

/* ---------- Prøve-formular ---------- */
.laerer-wrap .fejl { color: var(--warn); font-size: .9rem; margin: 0 0 1rem; }
.exam-form .field { margin: 0 0 1.1rem; }
.exam-form-textarea {
  width: 100%; font: inherit; padding: .7rem .85rem; resize: vertical;
  border: 1px solid var(--line); border-radius: 8px; background: var(--paper); color: var(--ink);
}
.preview-boks {
  border: 1px solid var(--line); border-radius: 8px; padding: 1rem 1.2rem;
  background: var(--surface); min-height: 3rem;
}

/* ---------- Fejl ---------- */
.error-wrap { max-width: 480px; margin: 12vh auto; text-align: center; padding: 1.5rem; }
.error-wrap .code { font-family: var(--reading); font-size: 3rem; color: var(--accent); }

/* ---------- Responsivt ---------- */
@media (max-width: 820px) {
  .stage { grid-template-columns: 1fr; height: auto; }
  .panel { height: auto; }
  .opgave { max-height: 42vh; }
  .besvarelse { min-height: 58vh; }
  .meta .who, .topbar .fag { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
