/* HTML's `hidden` attribute should always win over any class that sets
 * `display: flex/grid/block`. Without this, an element marked
 * `<div hidden class="foo">` where `.foo { display: flex; }` stays
 * visible in the UI — that's how the update banner shipped visible on
 * every fresh load. */
[hidden] { display: none !important; }

:root {
  --bg: #f4f5f3;
  --card: #ffffff;
  --border: #d9dce3;
  --text: #0f1419;
  --muted: #656770;
  --accent: #0f5a5e;
  --accent-bg: #e7f3f3;
  --signal: #c82333;
  --correct-bg: #e4f3ea;
  --correct-border: #2f7c49;
  --wrong-bg: #fbe9ea;
  --wrong-border: #c82333;
  --bar-height: 52px;
  --font-display: "Fraunces", Georgia, "Times New Roman", serif;
  --font-body: "Inter Tight", -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }

body {
  font-family: var(--font-body);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
  padding-top: calc(var(--bar-height) + env(safe-area-inset-top, 0px));
  letter-spacing: -0.005em;
}

/* ───── Top bar ───── */
.topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: calc(var(--bar-height) + env(safe-area-inset-top, 0px));
  padding-top: env(safe-area-inset-top, 0px);
  background: var(--card);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding-left: 0.5rem;
  padding-right: 0.5rem;
  z-index: 100;
}

.bar-btn {
  font: inherit;
  font-size: 0.95rem;
  padding: 0.4rem 0.7rem;
  min-height: 40px;
  min-width: 40px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.bar-btn:active { background: #eef2ff; }
.bar-btn:disabled { opacity: 0.35; cursor: default; }
.icon-btn { font-size: 1.2rem; padding: 0.4rem 0.55rem; }

.lang-toggle {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--card);
}
.lang-btn {
  font: inherit;
  font-size: 0.85rem;
  padding: 0.45rem 0.6rem;
  min-height: 40px;
  border: none;
  border-right: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  cursor: pointer;
}
.lang-btn:last-child { border-right: none; }
.lang-btn[aria-pressed="true"] {
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}

.nav-group {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  margin-left: auto;
}
.position {
  font-variant-numeric: tabular-nums;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text);
  letter-spacing: -0.01em;
  min-width: 5.5ch;
  text-align: center;
}

#toggle-reveal .reveal-on,
#toggle-reveal .reveal-off {
  width: 22px;
  height: 22px;
  display: inline-block;
  font-size: 0;
  background-color: currentColor;
  -webkit-mask-size: contain;
          mask-size: contain;
  -webkit-mask-repeat: no-repeat;
          mask-repeat: no-repeat;
  -webkit-mask-position: center;
          mask-position: center;
}
#toggle-reveal .reveal-on {
  -webkit-mask-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z'/><circle cx='12' cy='12' r='3'/></svg>");
          mask-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z'/><circle cx='12' cy='12' r='3'/></svg>");
}
#toggle-reveal .reveal-off {
  -webkit-mask-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='M17.94 17.94A10.07 10.07 0 0 1 12 20c-7 0-11-8-11-8a18.45 18.45 0 0 1 5.06-5.94M9.9 4.24A9.12 9.12 0 0 1 12 4c7 0 11 8 11 8a18.5 18.5 0 0 1-2.16 3.19m-6.72-1.07a3 3 0 1 1-4.24-4.24'/><line x1='1' y1='1' x2='23' y2='23'/></svg>");
          mask-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='M17.94 17.94A10.07 10.07 0 0 1 12 20c-7 0-11-8-11-8a18.45 18.45 0 0 1 5.06-5.94M9.9 4.24A9.12 9.12 0 0 1 12 4c7 0 11 8 11 8a18.5 18.5 0 0 1-2.16 3.19m-6.72-1.07a3 3 0 1 1-4.24-4.24'/><line x1='1' y1='1' x2='23' y2='23'/></svg>");
}
#toggle-reveal .reveal-off { display: none; }
#toggle-reveal[aria-pressed="false"] .reveal-on { display: none; }
#toggle-reveal[aria-pressed="false"] .reveal-off { display: inline-block; }
#toggle-reveal[aria-pressed="false"] {
  background: #fef3c7;
  border-color: #d6a923;
  color: #8a6d1a;
}

.status {
  padding: 0.6rem 1rem;
  color: var(--muted);
  font-size: 0.9rem;
  text-align: center;
}
.status:empty { display: none; }

/* ───── Filters drawer ───── */
.filters {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 0.75rem 1rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  position: sticky;
  top: calc(var(--bar-height) + env(safe-area-inset-top, 0px));
  z-index: 50;
  box-shadow: 0 4px 12px rgba(0,0,0,0.06);
}
.filters[hidden] { display: none; }
.row {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
  align-items: flex-end;
}
label {
  display: flex;
  flex-direction: column;
  font-size: 0.8rem;
  color: var(--muted);
  gap: 0.2rem;
  flex: 1;
  min-width: 160px;
}
label.grow { flex: 1; min-width: 100%; }
label.checkbox { flex-direction: row; align-items: center; gap: 0.5rem; color: var(--text); flex: 0 0 auto; min-width: 0; }

select, input[type="search"] {
  font: inherit;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  width: 100%;
}

.meta { color: var(--muted); font-size: 0.85rem; }

.filters .row:last-child {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 0.5rem;
}
.filters .row:last-child > .bar-btn { width: 100%; }

/* ───── Main view ───── */
main#view {
  padding: 0.75rem;
  max-width: 780px;
  margin: 0 auto;
}

.q-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1rem 1.1rem;
  box-shadow: 0 1px 0 rgba(15,20,30,0.04), 0 2px 10px rgba(15,20,30,0.04);
}

.q-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: var(--muted);
  font-size: 0.78rem;
  margin-bottom: 0.5rem;
  gap: 0.5rem;
  flex-wrap: wrap;
}
.q-id { font-family: ui-monospace, monospace; }

.q-classes { display: flex; gap: 0.25rem; flex-wrap: wrap; }
.q-classes .badge {
  background: var(--accent-bg);
  color: var(--accent);
  border-radius: 4px;
  padding: 1px 6px;
  font-size: 0.72rem;
}

.q-text {
  font-family: var(--font-display);
  font-size: 1.2rem;
  font-weight: 600;
  line-height: 1.3;
  letter-spacing: -0.015em;
  margin: 0 0 0.5rem;
  color: #121820;
}
.q-text-ar { font-size: 1.1rem; margin: 0 0 0.4rem; direction: rtl; text-align: right; color: #0f172a; }
.q-missing-trans { color: #a16207; font-size: 0.85rem; font-style: italic; }

.q-image {
  max-width: 100%;
  max-height: 320px;
  border-radius: 8px;
  margin: 0.6rem auto;
  display: block;
  background: #fafbfc;
  border: 1px solid var(--border);
  padding: 0.4rem;
  box-sizing: border-box;
  object-fit: contain;
}

.answers { list-style: none; padding: 0; margin: 0.75rem 0 0; display: flex; flex-direction: column; gap: 0.4rem; }

.answers li {
  border: 1px solid var(--border);
  border-left-width: 4px;
  border-radius: 8px;
  padding: 0.65rem 0.8rem;
  background: #ffffff;
  display: flex;
  gap: 0.6rem;
  align-items: flex-start;
  user-select: none;
  font-size: 0.97rem;
  font-weight: 500;
  letter-spacing: -0.008em;
  color: #1b2028;
  box-shadow: 0 1px 0 rgba(15,20,30,0.03);
  transition: background-color 0.12s, border-color 0.12s, transform 0.08s;
}
body.reveal-off .answers li:not(.revealed):hover {
  border-color: var(--accent);
}

/* Revealed states */
.answers li.revealed.correct { background: var(--correct-bg); border-left-color: var(--correct-border); }
.answers li.revealed.wrong   { background: var(--wrong-bg);   border-left-color: var(--wrong-border); }

/* Checkbox-styled answer mark. The .real-mark glyph inside holds the true
   answer (✓ / ✗) — we only show it once the row is revealed, otherwise it
   would leak the answer. */
.answers .mark {
  width: 22px;
  height: 22px;
  min-width: 22px;
  border: 1.5px solid #b9bfc9;
  border-radius: 5px;
  background: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  line-height: 1;
  flex-shrink: 0;
  margin-top: 2px;
  transition: background-color 0.12s, border-color 0.12s, color 0.12s;
}
.answers .mark .real-mark { display: none; color: inherit; }

/* Hidden (learning) state: empty checkbox, row is clickable. */
body.reveal-off .answers li:not(.revealed) { cursor: pointer; }
body.reveal-off .answers li:not(.revealed):active { background: #eef2ff; }

/* Selected pre-check: teal-filled box with white ✓. */
body.reveal-off .answers li:not(.revealed).selected {
  background: var(--accent-bg);
  border-left-color: var(--accent);
  border-color: var(--accent);
}
body.reveal-off .answers li:not(.revealed).selected .mark {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
body.reveal-off .answers li:not(.revealed).selected .mark::before { content: "✓"; }

/* Revealed (reveal on OR after Prüfen): colored checkbox with real glyph. */
.answers li.revealed .mark .real-mark { display: inline; }
.answers li.revealed.correct .mark {
  background: var(--correct-border);
  border-color: var(--correct-border);
  color: #fff;
}
.answers li.revealed.wrong .mark {
  background: var(--wrong-border);
  border-color: var(--wrong-border);
  color: #fff;
}

/* Selected + revealed: add accent outline around the row so user can see
   which answers they picked, independent of correctness. */
.answers li.revealed.selected {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.a-text { flex: 1; }
.a-text-ar { direction: rtl; text-align: right; font-size: 0.98rem; margin-top: 0.15rem; }

.a-image {
  max-width: 140px;
  max-height: 100px;
  border-radius: 4px;
  object-fit: contain;
}

.empty {
  text-align: center;
  color: var(--muted);
  padding: 3rem 1rem;
}

.reveal-hint {
  font-size: 0.8rem;
  color: var(--muted);
  text-align: center;
  margin-top: 0.5rem;
}

.check-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  margin-top: 0.9rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
}

.primary-btn {
  font: inherit;
  font-weight: 600;
  letter-spacing: -0.01em;
  padding: 0.55rem 1.2rem;
  min-height: 42px;
  border: 1.5px solid var(--accent);
  background: var(--accent);
  color: #fff;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 0.12s, color 0.12s, border-color 0.12s;
}
.primary-btn:disabled {
  background: transparent;
  color: var(--muted);
  border-color: var(--border);
  cursor: default;
}
.primary-btn:active:not(:disabled) { filter: brightness(0.92); }

.check-bar .reveal-hint { flex: 1 1 100%; margin: 0; text-align: left; }

.check-result {
  flex: 1 1 auto;
  padding: 0.55rem 0.8rem;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  font-size: 0.9rem;
}
.check-result strong { font-size: 1rem; }
.check-result.ok  { background: var(--correct-bg); color: #1f6f3a; }
.check-result.bad { background: var(--wrong-bg);   color: #8a2c2a; }

/* ───── Stats + chips ───── */
.stats {
  color: var(--text);
  font-size: 0.85rem;
  background: var(--accent-bg);
  border-radius: 6px;
  padding: 0.5rem 0.7rem;
}
.stats:empty { display: none; }

.class-selection {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}
.class-selection-label {
  font-size: 0.8rem;
  color: var(--muted);
  flex: 0 0 100%;
}
.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  flex: 1;
  min-width: 0;
}
.chip {
  background: var(--accent-bg);
  color: var(--accent);
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 0.8rem;
  font-weight: 500;
}
.chip.empty {
  background: #fff3cd;
  color: #8a6d1a;
}

.answered-badge {
  font-size: 0.72rem;
  border-radius: 4px;
  padding: 1px 6px;
  font-family: ui-monospace, monospace;
}
.answered-badge.ok       { background: var(--correct-bg); color: #1f6f3a; }
.answered-badge.bad      { background: var(--wrong-bg);   color: #8a2c2a; }
.answered-badge.mastered { background: #0ea5e9; color: #fff; font-weight: 600; }

.followup-tag {
  font-size: 0.72rem;
  border-radius: 4px;
  padding: 1px 6px;
  background: #f5f5f0;
  color: #6b6b40;
  border: 1px solid #e2e0d0;
}

/* ───── Modal / dialog ───── */
dialog.modal {
  border: none;
  border-radius: 12px;
  padding: 1.25rem 1.25rem 1rem;
  max-width: min(92vw, 440px);
  width: 92vw;
  box-shadow: 0 20px 60px rgba(0,0,0,0.35);
  color: var(--text);
  background: var(--card);
}
dialog.modal::backdrop {
  background: rgba(0,0,0,0.45);
  backdrop-filter: blur(2px);
}
dialog.modal h2 {
  margin: 0 0 0.3rem;
  font-family: var(--font-display);
  font-size: 1.25rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.25;
  color: #121820;
}
dialog.modal .modal-hint,
dialog.modal .modal-hint-ar {
  font-size: 0.88rem;
  line-height: 1.45;
  margin: 0;
  padding: 0 0.9rem;
}
dialog.modal .modal-hint { color: var(--muted); padding-top: 0.65rem; }
dialog.modal .modal-hint-ar {
  color: #3f4550;
  direction: rtl;
  text-align: right;
}
dialog.modal h2 + .modal-hint-ar {
  background: var(--accent-bg);
  border-radius: 8px 8px 0 0;
  padding-top: 0.7rem;
  margin-top: 0.5rem;
}
dialog.modal .modal-hint + .modal-hint-ar {
  background: var(--accent-bg);
  border-radius: 0 0 8px 8px;
  padding-bottom: 0.7rem;
  margin-bottom: 1rem;
}
dialog.modal h2 + .modal-hint-ar + .modal-hint {
  background: var(--accent-bg);
}

.class-list {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  max-height: 50vh;
  overflow-y: auto;
  padding: 0.2rem 0;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.9rem;
}
.class-list label {
  display: grid;
  grid-template-columns: auto 6rem 1fr;
  column-gap: 0.7rem;
  align-items: center;
  padding: 0.5rem 0.3rem;
  cursor: pointer;
  min-width: 0;
  color: var(--text);
  font-size: 0.95rem;
}
.class-list label .code-block {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  align-items: flex-start;
  min-width: 0;
}
.class-list label .count {
  font-variant-numeric: tabular-nums;
  font-size: 0.78rem;
  color: var(--text);
  background: #f1f3f7;
  padding: 2px 10px;
  border-radius: 999px;
  white-space: nowrap;
  font-weight: 500;
  letter-spacing: -0.01em;
}
.class-list label .count.done {
  background: var(--correct-bg);
  color: #1f6f3a;
  font-weight: 600;
}
.class-list input { width: 18px; height: 18px; margin: 0; flex: 0 0 auto; }
.class-list label .code {
  font-weight: 600;
  white-space: nowrap;
}
.class-list label .text-wrap {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  min-width: 0;
}
.class-list label .desc {
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.25;
}
.class-list label .desc-ar {
  color: #374151;
  font-size: 0.85rem;
  line-height: 1.35;
  direction: rtl;
  text-align: right;
}

.modal-actions {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
  flex-wrap: wrap;
}

/* ───── Image viewer (zoom lightbox) ───── */
dialog.image-viewer {
  border: none;
  padding: 0;
  background: transparent;
  max-width: 100vw;
  max-height: 100vh;
  width: 100vw;
  height: 100vh;
  margin: 0;
  overflow: hidden;
}
dialog.image-viewer::backdrop {
  background: rgba(10, 14, 20, 0.88);
  backdrop-filter: blur(4px);
}
dialog.image-viewer img {
  position: absolute;
  inset: 0;
  margin: auto;
  max-width: 96vw;
  max-height: 96vh;
  object-fit: contain;
  cursor: zoom-out;
  background: #fafbfc;
  border-radius: 6px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.5);
}

.q-image, .a-image { cursor: zoom-in; }

/* ───── Small screens ───── */
@media (max-width: 520px) {
  .lang-btn { font-size: 0.8rem; padding: 0.45rem 0.5rem; }
  .bar-btn { padding: 0.4rem 0.55rem; }
  .position { min-width: 4.5ch; font-size: 0.8rem; }
  main#view { padding: 0.5rem; }
  .q-card { padding: 0.85rem 0.9rem; border-radius: 8px; }
  .q-text, .q-text-ar { font-size: 1rem; }
  .a-image { max-width: 100px; max-height: 80px; }
}

/* ───── Print (A5, one question per page) ───── */
@media print {
  @page { size: A5 portrait; margin: 10mm; }

  html, body {
    background: #fff;
    color: #000;
    font-size: 10.5pt;
    line-height: 1.4;
    padding-top: 0;
  }

  .topbar, .filters, .status, #empty, .reveal-hint { display: none !important; }

  main#view { padding: 0; max-width: none; margin: 0; }

  .q-card {
    background: #fff;
    border: none;
    border-radius: 0;
    padding: 0;
    margin: 0;
    box-shadow: none;
    page-break-after: always;
    break-after: page;
    page-break-inside: avoid;
    break-inside: avoid;
    display: flex;
    flex-direction: column;
    min-height: 180mm;
  }

  .q-card:last-of-type { page-break-after: auto; break-after: auto; }

  .q-header {
    font-size: 8.5pt;
    margin-bottom: 3mm;
    padding-bottom: 2mm;
    border-bottom: 1px solid #ccc;
    color: #444;
  }
  .q-classes .badge {
    background: transparent;
    color: #000;
    border: 1px solid #999;
    font-size: 7.5pt;
  }
  .q-text, .q-text-ar { font-size: 12pt; font-weight: 600; margin: 0 0 3mm; }
  .q-image { max-height: 70mm; max-width: 100%; margin: 2mm auto 4mm; object-fit: contain; display: block; }

  .answers { margin-top: auto; padding-top: 3mm; }
  .answers li {
    background: #fff !important;
    border: 1px solid #bbb;
    border-left-width: 3px;
    padding: 2mm 3mm;
    margin-bottom: 1.5mm;
    font-size: 10pt;
    page-break-inside: avoid;
    break-inside: avoid;
  }
  .answers li.revealed.correct,
  body:not(.reveal-off) .answers li.correct { border-left-color: #000; border-left-width: 4px; }
  .answers li.revealed.wrong,
  body:not(.reveal-off) .answers li.wrong   { border-left-color: #bbb; }

  .answers li.revealed.correct .mark { background: #000; border-color: #000; color: #fff; }
  .answers li.revealed.wrong   .mark { background: #fff; border-color: #888; color: #888; }

  .a-image { max-width: 30mm; max-height: 22mm; }

  a, a:visited { color: inherit; text-decoration: none; }
}

/* ────────── PWA: update banner + offline images ────────── */

.update-banner {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 1rem;
  background: #c41e1e;
  color: #fff;
  font-weight: 500;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.update-banner button {
  background: #fff;
  color: #c41e1e;
  border: 0;
  border-radius: 6px;
  padding: 0.35rem 0.9rem;
  font-weight: 600;
  cursor: pointer;
}

.update-banner button:hover {
  background: #f5f5f5;
}

.offline-row {
  margin-top: 1rem;
  padding: 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
}

.offline-label {
  font-weight: 600;
  margin-bottom: 0.25rem;
}

.offline-status {
  font-size: 0.9em;
  color: #555;
  margin-bottom: 0.5rem;
}

.offline-progress {
  margin-top: 0.5rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.offline-progress progress {
  flex: 1;
  height: 14px;
}

.offline-progress span {
  font-size: 0.85em;
  color: #555;
  font-variant-numeric: tabular-nums;
  min-width: 6ch;
  text-align: right;
}

.version-footer {
  position: fixed;
  right: 6px;
  bottom: max(4px, env(safe-area-inset-bottom, 4px));
  font-size: 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: #999;
  opacity: 0.55;
  pointer-events: none;
  user-select: none;
  z-index: 1;
}

.version-footer:empty {
  display: none;
}

@media print {
  .version-footer { display: none; }
}
