/* ==========================================================
   Pengajian Web - Stylesheet
   ========================================================== */

/* Font Arab: ditambah Noto Naskh Arabic (mirip yang dipakai Gemini/ChatGPT di HP),
   Lateef (tradisional naskh), Reem Kufi (modern kufi). Inter untuk teks Latin. */
@import url('https://fonts.googleapis.com/css2?family=Amiri:wght@400;700&family=Scheherazade+New:wght@400;700&family=Noto+Naskh+Arabic:wght@400;500;600;700&family=Lateef:wght@400;700&family=Reem+Kufi:wght@400;500;600;700&family=Inter:wght@400;500;600;700&display=swap');

:root {
  --bg: #faf7f2;
  --bg-soft: #fff;
  --bg-elev: #ffffff;
  --text: #1f2430;
  --muted: #6b7280;
  --border: #e6e2da;
  --primary: #0f766e;
  --primary-text: #ffffff;
  --danger: #b91c1c;
  --accent: #b45309;
  --shadow: 0 6px 18px rgba(20, 20, 20, 0.06);
  --radius: 12px;
  --arabic-size: 2.4rem;
  --id-size: 1.05rem;
  --font-sans: 'Inter', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  /* Default: stack font sistem Arab (Android: Noto Naskh, iOS: SF Arabic).
     Mirip dengan yang dipakai Gemini & ChatGPT di HP — bersih, modern, nyaman dibaca. */
  --font-arab: 'Noto Naskh Arabic', 'SF Arabic', 'Geeza Pro', 'Segoe UI', 'Tahoma', system-ui, serif;
}

/* Pilihan font Arab via class di <body> */
body.font-arab-system .ar, body.font-arab-system textarea.arabic, body.font-arab-system .arabic {
  font-family: 'Noto Naskh Arabic', 'SF Arabic', 'Geeza Pro', 'Segoe UI', 'Tahoma', system-ui, serif;
}
body.font-arab-amiri .ar, body.font-arab-amiri textarea.arabic, body.font-arab-amiri .arabic {
  font-family: 'Amiri', 'Traditional Arabic', serif;
}
body.font-arab-scheherazade .ar, body.font-arab-scheherazade textarea.arabic, body.font-arab-scheherazade .arabic {
  font-family: 'Scheherazade New', 'Traditional Arabic', serif;
}
body.font-arab-lateef .ar, body.font-arab-lateef textarea.arabic, body.font-arab-lateef .arabic {
  font-family: 'Lateef', 'Traditional Arabic', serif;
}
body.font-arab-kufi .ar, body.font-arab-kufi textarea.arabic, body.font-arab-kufi .arabic {
  font-family: 'Reem Kufi', 'Tahoma', sans-serif;
}

[data-theme="dark"] {
  --bg: #0f1419;
  --bg-soft: #161c23;
  --bg-elev: #1c232b;
  --text: #e8eaed;
  --muted: #9aa3af;
  --border: #2a323d;
  --primary: #14b8a6;
  --primary-text: #06241f;
  --danger: #ef4444;
  --accent: #f59e0b;
  --shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
}

/* Saat dipasang sebagai PWA, cegah seleksi teks tak sengaja kecuali pada area konten */
@media (display-mode: standalone) {
  body { user-select: none; -webkit-user-select: none; }
  .ar, .id, .catatan, .input, textarea, p, h1, h2, h3 { user-select: text; -webkit-user-select: text; }
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 1rem;
}

/* ===== Topbar ===== */
.topbar {
  background: var(--bg-soft);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
  box-shadow: var(--shadow);
  padding-top: env(safe-area-inset-top);
}
.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: .75rem 1rem;
  padding-left: max(1rem, env(safe-area-inset-left));
  padding-right: max(1rem, env(safe-area-inset-right));
  flex-wrap: wrap;
}
.brand {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: .2px;
}
.brand-link {
  font-weight: 600;
  color: var(--text);
}
.subtitle {
  margin: .15rem 0 0;
  color: var(--muted);
  font-size: .9rem;
}
.view-title { text-align: center; flex: 1; min-width: 200px; }
.actions {
  display: flex;
  gap: .4rem;
  align-items: center;
}

/* ===== Buttons ===== */
.btn {
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .6rem 1rem;
  min-height: 44px; /* tap target ramah HP */
  font-size: .92rem;
  font-weight: 500;
  cursor: pointer;
  transition: transform .05s ease, background .15s, border-color .15s;
  font-family: inherit;
  touch-action: manipulation; /* hilangkan delay 300ms */
}
.btn:hover { border-color: var(--primary); }
.btn:active { transform: translateY(1px); }
.btn-primary {
  background: var(--primary);
  color: var(--primary-text);
  border-color: var(--primary);
}
.btn-primary:hover { filter: brightness(1.05); }
.btn-danger {
  background: transparent;
  color: var(--danger);
  border-color: var(--danger);
}
.btn-danger:hover { background: var(--danger); color: #fff; }
.btn-ghost {
  background: transparent;
  border-color: transparent;
}
.btn-ghost:hover { background: var(--bg-elev); border-color: var(--border); }

/* ===== Inputs ===== */
.input, .select {
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .65rem .8rem;
  min-height: 44px;
  font-size: 16px; /* ≥16px supaya iOS tidak auto-zoom saat focus */
  width: 100%;
  font-family: inherit;
}
.input:focus, .select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, .15);
}
textarea.input { resize: vertical; }
textarea.arabic {
  font-family: var(--font-arab);
  font-size: 1.6rem;
  line-height: 2;
}

/* ===== Panels ===== */
.panel {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.1rem 1.2rem;
  margin: 1rem 0;
  box-shadow: var(--shadow);
}
.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: .5rem;
  margin-bottom: .75rem;
}
.panel-head h2 { margin: 0; font-size: 1.1rem; }
.panel-actions { display: flex; gap: .4rem; flex-wrap: wrap; }

/* ===== Daftar Kitab (index) ===== */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: .9rem;
}
.kitab-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: .4rem;
  transition: transform .1s ease, border-color .15s;
}
.kitab-card:hover { border-color: var(--primary); transform: translateY(-2px); }
.kitab-card h3 { margin: 0; font-size: 1.05rem; }
.kitab-card .meta { color: var(--muted); font-size: .85rem; }
.kitab-card .bab-list {
  margin: .5rem 0 0;
  padding-left: 1.1rem;
  font-size: .92rem;
}
.kitab-card .bab-list li { margin: .1rem 0; }

/* ===== Form rows ===== */
.form-row {
  display: flex;
  gap: .5rem;
  margin: .5rem 0;
  flex-wrap: wrap;
  align-items: center;
}
.form-row .input, .form-row .select { flex: 1; min-width: 200px; }
.form-col { flex-direction: column; align-items: stretch; }
.form-col label { font-weight: 600; font-size: .9rem; color: var(--muted); }

/* ===== Editor: Daftar Kalimat ===== */
.kalimat-edit-list {
  list-style: none;
  padding: 0;
  margin: 1rem 0 0;
  counter-reset: kal;
}
.kalimat-edit-list li {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .8rem 1rem;
  margin-bottom: .6rem;
  cursor: pointer;
  transition: border-color .15s;
  position: relative;
  counter-increment: kal;
}
.kalimat-edit-list li::before {
  content: counter(kal);
  position: absolute;
  top: .8rem; left: -1.6rem;
  background: var(--primary);
  color: var(--primary-text);
  width: 1.4rem; height: 1.4rem;
  border-radius: 50%;
  font-size: .75rem;
  display: flex; align-items: center; justify-content: center;
}
.kalimat-edit-list { padding-left: 1.6rem; }
.kalimat-edit-list li:hover { border-color: var(--primary); }
.kalimat-edit-list li.active { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(15, 118, 110, .15); }
.kalimat-edit-list .ar {
  font-family: var(--font-arab);
  font-size: 1.5rem;
  direction: rtl;
  text-align: right;
  line-height: 1.9;
}
.kalimat-edit-list .id { color: var(--muted); font-size: .95rem; margin-top: .25rem; }
.kalimat-edit-list .row-actions {
  display: flex;
  gap: .3rem;
  margin-top: .5rem;
}
.kalimat-edit-list .row-actions .btn { padding: .25rem .55rem; font-size: .8rem; }

/* ===== View page ===== */
.view-page {
  /* ruang bawah untuk bottom-nav + safe area iPhone */
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}
.view-main { padding-top: 1rem; }

.bab-nav {
  display: flex;
  gap: .5rem;
  align-items: center;
  margin: 1rem 0;
  flex-wrap: nowrap;
}
.bab-nav .select { flex: 1; min-width: 0; }
.bab-nav .btn { flex: 0 0 auto; padding: .6rem .8rem; }

/* ===== Bottom navigation untuk HP ===== */
.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: .35rem;
  padding: .5rem .75rem;
  padding-bottom: calc(.5rem + env(safe-area-inset-bottom));
  padding-left: max(.75rem, env(safe-area-inset-left));
  padding-right: max(.75rem, env(safe-area-inset-right));
  background: color-mix(in srgb, var(--bg-soft) 92%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(10px);
  backdrop-filter: saturate(180%) blur(10px);
  border-top: 1px solid var(--border);
  box-shadow: 0 -6px 18px rgba(0,0,0,.08);
}
.bottom-nav .btn {
  flex: 1;
  min-width: 0;
  border: 1px solid transparent;
  background: transparent;
  font-weight: 600;
}
.bottom-nav .btn:hover { background: var(--bg-elev); border-color: var(--border); }
.bottom-nav .btn:disabled { opacity: .35; cursor: not-allowed; }
.bottom-nav #btnTopScroll { flex: 0 0 44px; padding: .5rem; }

.kalimat-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.kalimat-item {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-left: 4px solid var(--primary);
  border-radius: var(--radius);
  padding: 1.1rem 1.25rem;
  box-shadow: var(--shadow);
}
.kalimat-item .nomor {
  display: inline-block;
  background: var(--primary);
  color: var(--primary-text);
  border-radius: 999px;
  font-size: .8rem;
  padding: .15rem .55rem;
  margin-bottom: .5rem;
  font-weight: 600;
}
.kalimat-item .ar {
  font-family: var(--font-arab);
  font-size: var(--arabic-size);
  direction: rtl;
  text-align: right;
  line-height: 2.1;
  margin: .5rem 0 .8rem;
  word-spacing: .15rem;
}
.kalimat-item .id {
  font-size: var(--id-size);
  line-height: 1.7;
}
.kalimat-item .catatan {
  margin-top: .6rem;
  padding: .6rem .8rem;
  background: var(--bg-elev);
  border-radius: 8px;
  border: 1px dashed var(--border);
  color: var(--muted);
  font-size: .95rem;
}

/* Mode fokus: sembunyikan elemen non-essential */
body.fokus .topbar .actions .btn:not(#btnFokus):not(#btnTema),
body.fokus .footer,
body.fokus .bottom-nav { display: none; }
body.fokus .view-page { padding-bottom: env(safe-area-inset-bottom); }
body.fokus .kalimat-item { border-left-width: 6px; }

/* ===== Empty state ===== */
.empty {
  text-align: center;
  padding: 2rem 1rem;
  color: var(--muted);
}
.hidden { display: none !important; }

/* ===== Footer ===== */
.footer {
  margin-top: 3rem;
  padding: 1rem 0;
  border-top: 1px solid var(--border);
  color: var(--muted);
  text-align: center;
}

/* ===== Hint ===== */
.hint {
  color: var(--muted);
  font-size: .85rem;
  margin: .5rem 0;
}
pre.hint {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .6rem .8rem;
  white-space: pre-wrap;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: .8rem;
}

/* ===== Modal ===== */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  padding: 1rem;
}
.modal-content {
  background: var(--bg-soft);
  border-radius: var(--radius);
  padding: 1.2rem;
  padding-top: 2.4rem;
  max-width: 600px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
  position: relative;
}
.modal-content h3 { margin-top: 0; }
.modal-close {
  position: absolute;
  top: .5rem;
  right: .5rem;
  width: 2.2rem;
  height: 2.2rem;
  min-height: 0;
  padding: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--text);
  border: 1px solid transparent;
  font-size: 1.05rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: inherit;
  transition: background .15s, border-color .15s;
}
.modal-close:hover { background: var(--bg-elev); border-color: var(--border); }

/* ===== Font picker (modal pilih font Arab) ===== */
.font-options { display: flex; flex-direction: column; gap: .5rem; margin-top: .75rem; }
.font-option {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  text-align: left;
  white-space: normal;
  padding: .75rem 1rem;
  gap: .35rem;
  cursor: pointer;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: border-color .15s, background .15s;
  width: 100%;
  font-family: inherit;
  color: var(--text);
}
.font-option:hover { border-color: var(--primary); }
.font-option.active {
  border-color: var(--primary);
  background: color-mix(in srgb, var(--primary) 10%, var(--bg-elev));
  box-shadow: 0 0 0 3px rgba(15, 118, 110, .15);
}
.font-option-label {
  font-weight: 600;
  font-size: .92rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: .5rem;
}
.font-option-label .badge {
  font-size: .7rem;
  font-weight: 500;
  background: var(--primary);
  color: var(--primary-text);
  padding: .1rem .5rem;
  border-radius: 999px;
}
.font-option-preview {
  direction: rtl;
  text-align: right;
  font-size: 1.65rem;
  line-height: 2;
  color: var(--text);
  word-spacing: .15rem;
}

/* ===== Sync pill (status sinkronisasi cloud) ===== */
.sync-pill {
  position: relative;
  font-size: 1rem;
  min-width: 40px;
}
.sync-pill[data-state="syncing"], .sync-pill[data-state="loading"] {
  animation: syncSpin 1.4s linear infinite;
  color: var(--primary);
}
.sync-pill[data-state="synced"] {
  color: var(--primary);
}
.sync-pill[data-state="loggedOut"], .sync-pill[data-state="offline"] {
  color: var(--muted);
}
.sync-pill[data-state="conflict"] {
  color: var(--accent);
  animation: syncPulse 1.2s ease-in-out infinite;
}
.sync-pill[data-state="error"] {
  color: var(--danger);
}
@keyframes syncSpin {
  to { transform: rotate(360deg); }
}
@keyframes syncPulse {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.15); }
}
@media (prefers-reduced-motion: reduce) {
  .sync-pill[data-state="syncing"], .sync-pill[data-state="loading"], .sync-pill[data-state="conflict"] {
    animation: none !important;
  }
}

/* Tombol pilihan dengan judul + deskripsi (mis. modal Import) */
.import-options {
  display: flex;
  flex-direction: column;
  gap: .5rem;
  margin-top: .75rem;
}
.import-option {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  text-align: left;
  white-space: normal;
  padding: .85rem 1rem;
  gap: .25rem;
  line-height: 1.4;
}
.import-option-title {
  font-weight: 700;
  font-size: 1rem;
  color: inherit;
}
.import-option-desc {
  font-size: .82rem;
  font-weight: 400;
  color: inherit;
  opacity: .85;
}
.import-option-cancel { align-self: stretch; text-align: center; }

/* ===== Toast (PWA update / install hint) ===== */
.pwa-toast {
  position: fixed;
  left: 50%;
  bottom: calc(80px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: var(--bg);
  padding: .7rem 1rem;
  border-radius: 999px;
  font-size: .9rem;
  font-weight: 500;
  box-shadow: 0 8px 24px rgba(0,0,0,.25);
  opacity: 0;
  transition: opacity .25s ease, transform .25s ease;
  z-index: 1000;
  max-width: calc(100vw - 2rem);
  text-align: center;
}
.pwa-toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ===== Responsive HP ===== */
@media (max-width: 640px) {
  .brand { font-size: 1.05rem; }
  :root { --arabic-size: 2rem; --id-size: 1rem; }
  .container { padding: 0 .75rem; }
  .panel { padding: .85rem .9rem 1rem; margin: .75rem 0; border-radius: 14px; }
  .panel-head h2 { font-size: 1rem; }
  .kalimat-item { padding: 1rem; border-radius: 14px; }
  .topbar-inner { gap: .5rem; padding: .55rem .75rem; }
  .actions { gap: .25rem; flex-wrap: nowrap; }
  .actions .btn { padding: .5rem .65rem; font-size: .85rem; min-height: 40px; }
  .view-title { flex: 1 1 100%; order: 3; min-width: 0; }
  .view-title .brand { font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .view-title .subtitle { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .grid { grid-template-columns: 1fr; }
  .form-row { gap: .4rem; }
  .form-row .input, .form-row .select { min-width: 0; }
  .modal-content { padding: 1rem; border-radius: 14px; max-height: 92dvh; }
  .kalimat-edit-list { padding-left: 1.4rem; }
  .kalimat-edit-list li::before { left: -1.4rem; }
  .bottom-nav .btn { font-size: .9rem; padding: .55rem .5rem; }
}

/* HP kecil sekali */
@media (max-width: 380px) {
  .actions .btn { padding: .45rem .5rem; font-size: .8rem; }
  .view-title .brand { font-size: .95rem; }
}

/* Layar tinggi terbatas (landscape HP): kompakkan topbar */
@media (max-height: 480px) and (orientation: landscape) {
  .topbar-inner { padding: .35rem .75rem; }
  .bottom-nav { padding: .3rem .5rem; padding-bottom: calc(.3rem + env(safe-area-inset-bottom)); }
}

/* Hormati preferensi reduce motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
