/* Corpus.quran.com segment colors */
:root {
  --seg-navy: rgb(19, 1, 184);
  --seg-sky: rgb(84, 141, 212);
  --seg-purple: rgb(129, 38, 192);
  --seg-gold: rgb(129, 116, 24);
  --seg-seagreen: rgb(50, 189, 47);
  --seg-rust: rgb(173, 35, 35);
  --seg-orange: rgb(227, 112, 16);
  --seg-metal: rgb(92, 112, 133);

  --bg: #f5f5f7;
  --surface: #ffffff;
  --text-muted: #6b7280;
  --text-body: #1f2937;
  --translit: #2563eb;
  --border: #e5e7eb;

  /* A4 page: 210mm wide with comfortable inner margins */
  --page-width: 210mm;
  --page-margin: 18mm;

  /* Standard slot for corpus word images (tallest common PNGs are ~91px) */
  --word-image-slot: 96px;
}

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

html {
  font-size: 16px;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text-body);
  font-family: "Inter", system-ui, -apple-system, sans-serif;
  line-height: 1.5;
}

.page-wrapper {
  padding: 1.5rem 1rem 2.5rem;
  display: flex;
  justify-content: center;
}

.page {
  width: var(--page-width);
  max-width: min(100%, var(--page-width));
  margin: 0 auto;
  padding: var(--page-margin);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  box-shadow: 0 1px 3px rgb(0 0 0 / 6%);
  overflow-x: clip;
}

.header {
  margin-bottom: 1.75rem;
  text-align: center;
}

.header h1 {
  margin: 0 0 0.35rem;
  font-size: clamp(1.25rem, 3vw, 1.75rem);
  font-weight: 600;
  letter-spacing: -0.02em;
}

.header .meta {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.header .translation {
  margin: 1.25rem auto 0;
  max-width: 100%;
  color: var(--text-muted);
  font-size: 0.95rem;
  font-style: italic;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1rem;
  justify-content: center;
  align-items: flex-end;
  margin-top: 1.25rem;
}

.control {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.control select {
  min-width: 5.5rem;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text-body);
  font: inherit;
}

.load-btn {
  padding: 0.45rem 1rem;
  border: none;
  border-radius: 6px;
  background: var(--translit);
  color: #fff;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.load-btn:hover {
  filter: brightness(0.95);
}

.print-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem;
  justify-content: center;
  margin-top: 1.25rem;
}

.print-btn {
  padding: 0.45rem 1rem;
  border-radius: 6px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.print-btn--color {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-body);
}

.print-btn--color:hover {
  background: var(--bg);
}

.print-btn--bw {
  border: 1px solid #000;
  background: #000;
  color: #fff;
}

.print-btn--bw:hover {
  background: #222;
}

.status {
  margin: 0.75rem 0 0;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.status-error {
  color: #b91c1c;
}

.verse-panel {
  padding-top: 0.5rem;
}

.verse-flow {
  width: 100%;
}

.verse-translations {
  margin-top: 2rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--border);
}

.verse-translation-ref {
  font-weight: 600;
  font-style: normal;
  color: var(--text-body);
}

.verse-translation {
  margin: 0 0 0.85rem;
  color: var(--text-muted);
  font-size: 0.9rem;
  font-style: italic;
  text-align: start;
  line-height: 1.5;
}

.verse-translation:last-child {
  margin-bottom: 0;
}

.wbw-measure {
  position: fixed;
  left: -10000px;
  top: 0;
  visibility: hidden;
  pointer-events: none;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: flex-end;
  gap: 1.25rem;
  direction: rtl;
  z-index: -1;
}

.wbw-measure-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.25rem;
}

.verse-panel {
  width: 100%;
  max-width: 100%;
  overflow-x: clip;
}

.verse-panel #verses-container,
.verse-flow {
  position: relative;
  width: 100%;
  max-width: 100%;
}

.wbw-line {
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, max-content));
  column-gap: 1.25rem;
  row-gap: 0.4rem;
  direction: rtl;
  justify-content: start;
  margin-bottom: 1.75rem;
  align-items: stretch;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
}

.wbw-cell {
  min-width: 0;
}

.wbw-arabic {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: flex-end;
  justify-content: center;
  gap: 0.35rem;
  direction: rtl;
  min-height: 100%;
}

.wbw-translit {
  font-family: Georgia, "Times New Roman", Times, serif;
  font-size: 0.85rem;
  font-style: italic;
  color: var(--translit);
  text-align: center;
  white-space: nowrap;
  direction: ltr;
}

.wbw-meaning {
  font-size: 0.75rem;
  color: var(--text-body);
  text-align: center;
  width: 100%;
  max-width: 7.5rem;
  justify-self: center;
  direction: ltr;
  line-height: 1.3;
  overflow-wrap: anywhere;
  hyphens: auto;
}

.wbw-grammar-en {
  font-size: 0.68rem;
  color: var(--text-body);
  text-align: start;
  direction: ltr;
  line-height: 1.35;
  max-width: 8.5rem;
  justify-self: center;
}

.grammar-en-line {
  margin-bottom: 0.2rem;
}

.grammar-en-line:last-child {
  margin-bottom: 0;
}

.grammar-pos {
  font-weight: 700;
  color: var(--seg-sky);
}

.grammar-en-line:has(.grammar-pos) .grammar-pos {
  color: inherit;
}

.wbw-grammar-en .grammar-en-line:nth-child(1) .grammar-pos { color: var(--seg-navy); }
.wbw-grammar-en .grammar-en-line:nth-child(2) .grammar-pos { color: var(--seg-sky); }
.wbw-grammar-en .grammar-en-line:nth-child(3) .grammar-pos { color: var(--seg-metal); }

.wbw-grammar-ar {
  font-family: "Amiri", "Noto Naskh Arabic", serif;
  font-size: 0.78rem;
  color: #2f7d32;
  text-align: right;
  direction: rtl;
  line-height: 1.45;
  max-width: 8.5rem;
  justify-self: center;
}

.grammar-ar-line {
  margin-bottom: 0.15rem;
}

.grammar-ar-line:last-child {
  margin-bottom: 0;
}

.ayah-end-marker {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 1.65em;
  height: 1.65em;
  margin-bottom: 0.35em;
  border: 1.5px solid var(--text-body);
  border-radius: 50%;
  font-family: Georgia, "Times New Roman", Times, serif;
  font-size: 0.62rem;
  font-weight: 600;
  line-height: 1;
  color: var(--text-body);
  user-select: none;
}

.arabic-word {
  font-family: "Amiri Quran", "Amiri", "Noto Naskh Arabic", serif;
  font-size: clamp(1.5rem, 4vw, 2.1rem);
  line-height: 1.35;
  text-align: center;
  direction: rtl;
}

.arabic-word--image {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.arabic-word-image {
  display: block;
  width: auto;
  height: auto;
  max-width: none;
  image-rendering: -webkit-optimize-contrast;
}

.seg-navy { color: var(--seg-navy); }
.seg-sky { color: var(--seg-sky); }
.seg-purple { color: var(--seg-purple); }
.seg-gold { color: var(--seg-gold); }
.seg-seagreen { color: var(--seg-seagreen); }
.seg-rust { color: var(--seg-rust); }
.seg-orange { color: var(--seg-orange); }
.seg-metal { color: var(--seg-metal); }

.pos-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  justify-content: center;
  margin-top: 0.15rem;
}

.tag {
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  line-height: 1;
}

.tag-navy { color: var(--seg-navy); }
.tag-sky { color: var(--seg-sky); }
.tag-purple { color: var(--seg-purple); }
.tag-gold { color: var(--seg-gold); }
.tag-seagreen { color: var(--seg-seagreen); }
.tag-rust { color: var(--seg-rust); }
.tag-orange { color: var(--seg-orange); }
.tag-metal { color: var(--seg-metal); }

.legend {
  margin-top: 2rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
}

.legend h2 {
  margin: 0 0 0.75rem;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.legend-items {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1rem;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.legend-item .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  flex-shrink: 0;
}

.dot-navy { background: var(--seg-navy); }
.dot-sky { background: var(--seg-sky); }
.dot-purple { background: var(--seg-purple); }
.dot-gold { background: var(--seg-gold); }
.dot-seagreen { background: var(--seg-seagreen); }
.dot-rust { background: var(--seg-rust); }
.dot-orange { background: var(--seg-orange); }
.dot-metal { background: var(--seg-metal); }

.footer {
  margin-top: 2rem;
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.footer-link {
  color: var(--translit);
}

@media (max-width: 640px) {
  .page-wrapper {
    padding: 0.75rem 0.5rem 1.5rem;
  }

  .page {
    padding: 1rem;
    border-radius: 0;
    border-left: none;
    border-right: none;
  }

  .wbw-line {
    column-gap: 0.85rem;
    margin-bottom: 1.35rem;
  }

  .wbw-meaning {
    display: none;
  }
}

@media print {
  @page {
    size: A4;
    margin: 15mm;
  }

  body {
    background: #fff;
  }

  .page-wrapper {
    padding: 0;
    display: block;
  }

  .page {
    width: 100%;
    max-width: none;
    border: none;
    box-shadow: none;
    padding: 0;
  }

  .header {
    margin-bottom: 1rem;
  }

  .controls,
  .load-btn,
  .print-controls,
  .status,
  .footer {
    display: none;
  }

  .legend {
    break-inside: avoid;
    page-break-inside: avoid;
  }

  .wbw-line {
    break-inside: avoid;
    page-break-inside: avoid;
    margin-bottom: 1.5rem;
  }

  .wbw-arabic,
  .wbw-translit,
  .wbw-meaning {
    break-inside: avoid;
    page-break-inside: avoid;
  }

  .verse-translations {
    break-before: auto;
    margin-top: 1.5rem;
    padding-top: 1rem;
  }

  .verse-translation {
    break-inside: avoid;
    page-break-inside: avoid;
  }

  /* Black & white: pure black (K) for crisp, ink-saving output */
  html.print-bw .page,
  html.print-bw .seg-navy,
  html.print-bw .seg-sky,
  html.print-bw .seg-purple,
  html.print-bw .seg-gold,
  html.print-bw .seg-seagreen,
  html.print-bw .seg-rust,
  html.print-bw .seg-orange,
  html.print-bw .seg-metal,
  html.print-bw .tag-navy,
  html.print-bw .tag-sky,
  html.print-bw .tag-purple,
  html.print-bw .tag-gold,
  html.print-bw .tag-seagreen,
  html.print-bw .tag-rust,
  html.print-bw .tag-orange,
  html.print-bw .tag-metal,
  html.print-bw .wbw-translit,
  html.print-bw .wbw-meaning,
  html.print-bw .wbw-grammar-en,
  html.print-bw .wbw-grammar-ar,
  html.print-bw .grammar-pos,
  html.print-bw .verse-translation,
  html.print-bw .verse-translation-ref,
  html.print-bw .header h1,
  html.print-bw .meta,
  html.print-bw .legend h2,
  html.print-bw .legend-item {
    color: #000 !important;
  }

  html.print-bw .ayah-end-marker {
    color: #000 !important;
    border-color: #000 !important;
  }

  html.print-bw .dot,
  html.print-bw .legend-item .dot {
    background: #000 !important;
  }

  html.print-bw .arabic-word-image {
    filter: grayscale(1) contrast(1.35) brightness(0.85);
  }
}
