/* ---------- Font bawaan (dibundel, dipakai bila tidak terpasang di komputer) ---------- */
@font-face {
  font-family: "KFGQPC Uthman Taha Naskh";
  font-weight: 400;
  src: local("KFGQPC Uthman Taha Naskh"), local("KFGQPC_Uthman_Taha_Naskh_H"),
       url("fonts/UthmanTN1Ver10.ttf") format("truetype");
}
@font-face {
  font-family: "KFGQPC Uthman Taha Naskh";
  font-weight: 700;
  src: local("KFGQPC Uthman Taha Naskh Bold"), local("KFGQPC_Uthman_Taha_Naskh_HB"),
       url("fonts/UthmanTN1BVer10.ttf") format("truetype");
}
@font-face {
  font-family: "Aref Ruqaa";
  font-weight: 400;
  src: local("Aref Ruqaa"), local("ArefRuqaa-Regular"), url("fonts/ArefRuqaa-Regular.ttf") format("truetype");
}
@font-face {
  font-family: "Aref Ruqaa";
  font-weight: 700;
  src: local("Aref Ruqaa Bold"), local("ArefRuqaa-Bold"), url("fonts/ArefRuqaa-Bold.ttf") format("truetype");
}

:root {
  --bg: #f4f1ea;
  --paper: #ffffff;
  --ink: #1d1a16;
  --muted: #6b645a;
  --line: #d9d2c4;
  --accent: #0f6b5c;
  --accent-soft: #e0f0ec;
  --danger: #a33a2a;

  /* Diisi dari app.js sesuai pengaturan kitab. Satuan pt seperti di Word. */
  --font-ar: "KFGQPC Uthman Taha Naskh";
  --font-mk: "Aref Ruqaa";
  --font-kd: "Aref Ruqaa";
  --font-hf: "Amiri";
  --ar-size: 18pt;
  --mk-size: 8pt;
  --kd-size: 8pt;
  --kd-gap: -2.5pt;    /* jarak kedudukan ke lafal; negatif = merapat/menempel ke atas harakat */
  --hf-size: 10pt;
  --ar-color: #000;
  --mk-ink: #000;
  --kd-ink: #000;
  --hf-color: #000;
  --mk-style: italic;
  --kd-style: italic;
  --angle: 28;
  --gap: 40pt;       /* jarak bawah kata bermakna (menentukan jarak baris) */
  --gap-empty: 6pt;  /* jarak bawah kata tanpa makna */
  --space: 0pt;
  --mk-dx: 21pt; /* geser ke kiri tiap baris makna lanjutan */
  --mk-dy: 0pt;

  --paper-w: 210mm;
  --paper-h: 297mm;
  --m-top: 25mm;
  --m-right: 20mm;
  --m-bottom: 25mm;
  --m-left: 20mm;
  --hf-top-gap: 3mm;
  --hf-bot-gap: 3mm;

  --font-ui: "Segoe UI", system-ui, sans-serif;
  --fallback-ar: "Amiri", "Traditional Arabic", "Scheherazade New", serif;
  --fallback-mk: "Scheherazade New", "Amiri", "Traditional Arabic", serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 14px;
  padding-bottom: 320px;
}

button, .btn, select {
  font: inherit;
  padding: 6px 12px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  border-radius: 6px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
  min-height: 34px;
}
button:hover, .btn:hover { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.danger { color: var(--danger); }
button.danger:hover { border-color: var(--danger); }
button.icon { font-size: 20px; line-height: 1; padding: 2px 10px; }
.grow { flex: 1; }
.hint { margin: 0; color: var(--muted); font-size: 13px; }
.hint code { background: #f1ede4; padding: 0 4px; border-radius: 3px; }
.check { display: inline-flex; align-items: center; gap: 6px; }

/* ---------- Sidebar (menu tetap di kiri) ---------- */
:root { --sidebar-w: 224px; }
.sidebar {
  position: fixed; left: 0; top: 0; bottom: 0; z-index: 40;
  width: var(--sidebar-w);
  background: #fff;
  border-right: 1px solid var(--line);
  overflow-y: auto;
  display: flex; flex-direction: column; gap: 14px;
  padding: 14px 12px 20px;
}
.sb-brand {
  display: flex; align-items: center; justify-content: space-between;
  font-weight: 700; font-size: 17px; color: var(--accent); padding: 0 4px;
}
.sb-group { display: flex; flex-direction: column; gap: 4px; }
.sb-title { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 0 4px 2px; }
.sidebar button, .sidebar .btn { width: 100%; justify-content: flex-start; text-align: left; }
.sb-brand span { white-space: nowrap; }
.sb-brand button { width: auto; min-height: 0; }
.only-mobile { display: none; }
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 39; }
.backdrop[hidden] { display: none; }

.main { margin-left: var(--sidebar-w); min-width: 0; }

/* Bar atas: hanya pilihan kitab + status simpan, supaya tidak ada tombol yang berpindah */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 10px;
  padding: 8px 16px;
  background: #fff;
  border-bottom: 1px solid var(--line);
}
.topbar-label { color: var(--muted); font-size: 13px; }
#docSelect { flex: 1; min-width: 0; max-width: 420px; }
.save-status { font-size: 12px; color: var(--muted); white-space: nowrap; min-width: 9em; }
.save-status.ok { color: var(--accent); }
.save-status.busy { color: #8a6d1d; }
.save-status.err { color: var(--danger); font-weight: 600; }

/* ---------- Panel pengaturan (panel geser di kanan) ---------- */
.settings {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 45;
  width: min(440px, 100vw);
  display: flex; flex-direction: column; gap: 10px;
  padding: 0 16px 16px;
  background: #fff;
  border-left: 1px solid var(--line);
  box-shadow: -6px 0 24px rgba(0,0,0,.12);
  overflow-y: auto;
}
.settings[hidden] { display: none; }
.settings-head {
  position: sticky; top: 0; z-index: 1;
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 0 8px; margin: 0 -16px 0; padding-left: 16px; padding-right: 10px;
  background: #fff; border-bottom: 1px solid var(--line);
  font-size: 16px;
}
.settings fieldset {
  border: 1px solid var(--line); border-radius: 6px;
  padding: 6px 12px 10px; margin: 0;
  display: flex; flex-direction: column; gap: 6px;
  min-width: 0;
}
.settings legend { font-weight: 600; color: var(--accent); padding: 0 4px; }
.settings label { display: flex; align-items: center; gap: 8px; }
.settings input[type=range] { flex: 1; min-width: 80px; }
.settings label { flex-wrap: wrap; }
.settings .font-input, .settings .hf-input { flex: 1; min-width: 140px; }
.settings input[type=number] { width: 64px; font: inherit; padding: 3px 6px; border: 1px solid var(--line); border-radius: 4px; }
.settings input[type=color] { width: 36px; height: 24px; padding: 0; border: 1px solid var(--line); border-radius: 4px; }
.settings output { min-width: 2.5em; color: var(--muted); }
.font-input, .hf-input { font: inherit; padding: 4px 6px; border: 1px solid var(--line); border-radius: 4px; width: 180px; }
.hf-input { font-family: "Amiri", var(--font-ui); font-size: 15px; }
.margins { display: grid; grid-template-columns: auto auto; gap: 4px 12px; }
.margins > span { grid-column: 1 / -1; }
.margins label { justify-content: space-between; }
.hf-img { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.hf-img img { max-height: 36px; max-width: 160px; border: 1px solid var(--line); background: #fff; }
.ornaments { display: flex; flex-wrap: wrap; gap: 3px; }
.ornaments button { font-family: "Amiri", "Segoe UI Symbol", serif; font-size: 18px; min-width: 34px; padding: 0 6px; justify-content: center; }
.settings-actions { display: flex; align-items: center; gap: 6px; }

/* ---------- Halaman ---------- */
.desk { overflow-x: auto; padding: 24px 16px; }
.page {
  position: relative;
  width: var(--paper-w);
  min-height: var(--paper-h);
  margin: 0 auto;
  padding: var(--m-top) var(--m-right) var(--m-bottom) var(--m-left);
  background: var(--paper);
  border: 1px solid var(--line);
  box-shadow: 0 1px 4px rgba(0,0,0,.08);
}
body.fit .desk { padding: 8px; overflow: visible; }
body.fit .page {
  width: auto; min-height: 0;
  padding: 8px 12px 24px;
  border-radius: 6px;
}
.empty { text-align: center; color: var(--muted); padding: 40px 0; }

/* Pratinjau header/footer di layar (saat cetak diganti margin box @page) */
.hf-preview {
  position: absolute; left: var(--m-left); right: var(--m-right);
  display: grid; grid-template-columns: 1fr auto 1fr; gap: 8px;
  font-family: var(--font-hf), var(--fallback-ar);
  font-size: var(--hf-size); color: var(--hf-color);
  pointer-events: none;
}
.hf-preview[hidden] { display: none; }
.hf-preview > span { min-width: 0; overflow: hidden; white-space: nowrap; }
.hf-preview > span:nth-child(1) { text-align: left; }
.hf-preview > span:nth-child(2) { text-align: center; }
.hf-preview > span:nth-child(3) { text-align: right; }
.hf-preview img { display: block; margin: 0 auto; }
.hf-top { top: 0; height: var(--m-top); align-items: end; padding-bottom: var(--hf-top-gap); }
.hf-top.line { border-bottom: 0.5pt solid var(--hf-color); }
.hf-bottom { bottom: 0; height: var(--m-bottom); align-items: start; padding-top: var(--hf-bot-gap); }
.hf-bottom.line { border-top: 0.5pt solid var(--hf-color); }
body.fit .hf-preview { position: static; height: auto; padding: 4px 0; margin: 0 0 8px; }
body.fit .hf-bottom { margin: 12px 0 0; }

/* Teks: paragraf rata kanan-kiri, kata dipisah spasi biasa seperti di Word */
.text { direction: rtl; }
.para {
  text-align: justify;
  text-align-last: auto;
  word-spacing: var(--space);
  font-family: var(--font-ar), var(--fallback-ar);
  font-size: var(--ar-size);
  line-height: 1;
  margin: 0 0 4pt;
}

/* Satu kata: kedudukan / lafal / makna (bisa beberapa baris miring) */
.tok {
  position: relative;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  vertical-align: top;
  margin: 0 0 var(--gap);
  cursor: pointer;
  border-radius: 3px;
}
/* Paragraf yang sama sekali tidak bermakna memakai jarak baris yang lebih rapat */
.para.no-mk .tok { margin-bottom: var(--gap-empty); }
.tok:hover .ar { background: var(--accent-soft); }
.tok.active .ar { background: #ffe9a8; outline: 1px solid #e0b93b; }

.kd {
  font-family: var(--font-kd), var(--fallback-mk);
  font-style: var(--kd-style);
  font-size: var(--kd-size);
  color: var(--kd-ink);
  min-height: calc(var(--kd-size) * 1.3);
  line-height: 1.3;
  white-space: nowrap;
  direction: rtl;
  position: relative; z-index: 1;
  margin-bottom: var(--kd-gap);
}
.ar {
  font-family: var(--font-ar), var(--fallback-ar);
  font-size: var(--ar-size);
  color: var(--ar-color);
  line-height: 2;
  margin-top: calc(var(--ar-size) * -0.25);
  white-space: nowrap;
  border-radius: 3px;
}
.mk {
  position: absolute;
  top: calc(100% - var(--ar-size) * 0.35 + var(--n, 0) * var(--mk-dy));
  right: calc(20% + var(--n, 0) * var(--mk-dx));
  transform-origin: right top;
  transform: rotate(calc(var(--angle) * -1deg));
  font-family: var(--font-mk), var(--fallback-mk);
  font-style: var(--mk-style);
  font-size: var(--mk-size);
  color: var(--mk-ink);
  white-space: nowrap;
  direction: rtl;
  line-height: 1.2;
  pointer-events: none;
}
.tok.no-mk .mk::before {
  content: "";
  display: inline-block;
  width: 10pt; height: 1px;
  background: var(--line);
  vertical-align: middle;
}

/* ---------- Editor ---------- */
.editor {
  position: fixed; left: var(--sidebar-w); right: 0; bottom: 0; z-index: 30;
  background: #fff;
  border-top: 2px solid var(--accent);
  box-shadow: 0 -4px 16px rgba(0,0,0,.08);
  padding: 8px 16px 10px;
  display: flex; flex-direction: column; gap: 6px;
  max-height: 70vh; overflow-y: auto;
}
.editor[hidden] { display: none; }
.ed-row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.ed-head { justify-content: space-between; flex-wrap: nowrap; }
.ed-word {
  flex: 1; text-align: center; min-width: 0;
  font-family: var(--font-ar), var(--fallback-ar); font-size: 26px; line-height: 1.5;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.ed-grid {
  display: grid;
  grid-template-columns: 2fr 1fr 1fr;
  gap: 8px;
  direction: rtl;
}
.ed-field { display: flex; flex-direction: column; gap: 2px; direction: ltr; min-width: 0; }
.ed-field span { font-size: 12px; color: var(--muted); }
.ed-field input {
  font-family: var(--font-mk), var(--fallback-mk);
  font-size: 22px;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  direction: rtl;
  width: 100%;
  min-width: 0;
}
.ed-field input.ar-input { font-family: var(--font-ar), var(--fallback-ar); }
.ed-field input:focus { outline: 2px solid var(--accent); border-color: var(--accent); }

.chips { gap: 4px; }
.chip { padding: 2px 8px; font-size: 12px; border-radius: 999px; background: #faf7f0; min-height: 30px; }
.chip b { font-family: var(--font-kd), var(--fallback-mk); font-size: 16px; color: var(--kd-ink); font-weight: 400; }
.chip.on { background: var(--accent-soft); border-color: var(--accent); }

.pegon-keys { display: flex; gap: 3px; flex-wrap: wrap; }
.pegon-keys button {
  font-family: var(--font-mk), var(--fallback-mk);
  font-size: 20px; min-width: 36px; justify-content: center; padding: 0 6px;
}

/* ---------- Dialog & toast ---------- */
dialog { border: 1px solid var(--line); border-radius: 8px; padding: 0; width: min(760px, 94vw); }
dialog::backdrop { background: rgba(0,0,0,.3); }
.dlg { padding: 16px 20px; display: flex; flex-direction: column; gap: 10px; }
.dlg h2 { margin: 0; font-size: 18px; }
.dlg textarea {
  font-family: var(--font-ar), var(--fallback-ar);
  font-size: 24px; line-height: 1.8; padding: 10px;
  border: 1px solid var(--line); border-radius: 6px; resize: vertical; width: 100%;
}
.dlg-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.toast {
  position: fixed; top: 60px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 8px 16px; border-radius: 6px; z-index: 50;
  max-width: 90vw; text-align: center;
}

/* ---------- Layar kecil (HP / tablet) ---------- */
@media (max-width: 820px) {
  body { padding-bottom: 65vh; }
  .only-mobile { display: inline-flex; }
  .main { margin-left: 0; }
  .sidebar { transform: translateX(-100%); transition: transform .2s ease; box-shadow: none; width: min(280px, 85vw); }
  .sidebar.open { transform: none; box-shadow: 6px 0 24px rgba(0,0,0,.2); }
  .topbar { padding: 6px 10px; gap: 8px; }
  .topbar-label { display: none; }
  #docSelect { max-width: none; }
  .save-status { font-size: 11px; min-width: 0; }

  .editor { left: 0; padding: 6px 10px 8px; max-height: 62vh; gap: 5px; }
  .ed-word { font-size: 22px; }
  .ed-head button { padding: 4px 8px; font-size: 13px; }
  .ed-grid { grid-template-columns: 1fr 1fr; gap: 6px; }
  .ed-makna { grid-column: 1 / -1; }
  .ed-field input { font-size: 20px; padding: 5px 8px; }
  .scroll-row { flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch; padding-bottom: 2px; }
  .scroll-row > * { flex: 0 0 auto; }
  .pegon-keys { flex-wrap: nowrap; }
  .chip { font-size: 12px; }

  dialog { width: 96vw; }
  .dlg { padding: 12px; }
  .dlg textarea { font-size: 20px; }
}

/* ---------- Cetak ---------- */
/* Ukuran kertas, margin, header & footer (@page) diatur dari app.js lewat #pageStyle. */
@media print {
  body { background: #fff; padding: 0; }
  .no-print { display: none !important; }
  .main { margin-left: 0; }
  .desk, body.fit .desk { padding: 0; overflow: visible; }
  .page, body.fit .page { border: none; box-shadow: none; margin: 0; padding: 0; width: auto; min-height: 0; overflow: visible; border-radius: 0; }
  .tok.active .ar, .tok:hover .ar { background: none; outline: none; }
  body.hide-empty .tok.no-mk .mk::before { display: none; }
  .tok { break-inside: avoid; }
}
