:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #faf8f3;
  --ink: #23201b;
  --ink-soft: #6b6459;
  --border: #e8e2d5;
  --accent: #0f6e5c;
  --accent-dark: #0a4d40;
  --accent-soft: #e3f1ed;
  --gold: #b8863f;
  --gold-soft: #f7eedc;
  --shadow: 0 1px 2px rgba(35,32,27,.04), 0 8px 24px -12px rgba(35,32,27,.12);
  --radius: 14px;
  --radius-sm: 9px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161512;
    --surface: #1f1e1a;
    --surface-2: #242219;
    --ink: #f0ece1;
    --ink-soft: #a9a094;
    --border: #34311f;
    --accent: #3fb99b;
    --accent-dark: #59d1b3;
    --accent-soft: #1c3a33;
    --gold: #d4a856;
    --gold-soft: #332a17;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px -12px rgba(0,0,0,.5);
  }
}
:root[data-theme="dark"] {
  --bg: #161512;
  --surface: #1f1e1a;
  --surface-2: #242219;
  --ink: #f0ece1;
  --ink-soft: #a9a094;
  --border: #34311f;
  --accent: #3fb99b;
  --accent-dark: #59d1b3;
  --accent-soft: #1c3a33;
  --gold: #d4a856;
  --gold-soft: #332a17;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px -12px rgba(0,0,0,.5);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  background: var(--bg);
  color: var(--ink);
  font-family: 'Tajawal', 'Segoe UI', sans-serif;
  margin: 0;
}

body { padding-bottom: 4rem; }

.matn { font-family: 'Amiri', 'Traditional Arabic', serif; }

/* ---------- Header ---------- */
.topbar {
  background: linear-gradient(135deg, var(--accent-dark), var(--accent));
  color: #fff;
}
.topbar-inner {
  max-width: 980px;
  margin: 0 auto;
  padding: 1.6rem 1.25rem;
}
.brand { display: flex; align-items: center; gap: .9rem; }
.brand-mark {
  width: 48px; height: 48px;
  display: flex; align-items: center; justify-content: center;
  background: rgba(255,255,255,.15);
  border: 1px solid rgba(255,255,255,.3);
  border-radius: 12px;
  font-family: 'Amiri', serif;
  font-size: 1.6rem;
  flex-shrink: 0;
}
.brand-text h1 { margin: 0; font-size: 1.4rem; font-weight: 900; }
.brand-text p { margin: .15rem 0 0; font-size: .85rem; opacity: .85; }

.topbar-inner { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.auth-area { display: flex; align-items: center; gap: .6rem; }
.auth-area .btn-ghost-light {
  font-family: inherit; font-weight: 700; font-size: .85rem;
  padding: .5rem 1rem; border-radius: 999px;
  background: rgba(255,255,255,.12); border: 1px solid rgba(255,255,255,.35);
  color: #fff; cursor: pointer; transition: background .15s ease;
  text-decoration: none; display: inline-flex; align-items: center; gap: .3rem;
}
.auth-area .btn-ghost-light:hover { background: rgba(255,255,255,.22); }
.auth-user-email { font-size: .82rem; opacity: .9; }

.btn-ghost {
  font-family: inherit; font-weight: 600; font-size: .85rem;
  padding: .5rem 1rem; border-radius: var(--radius-sm);
  background: var(--surface-2); border: 1px solid var(--border);
  color: var(--ink); cursor: pointer; transition: background .15s ease, border-color .15s ease;
}
.btn-ghost:hover { border-color: var(--accent); background: var(--accent-soft); }

/* ---------- Auth modal ---------- */
.auth-panel { max-width: 380px; }
.auth-tabs { display: flex; gap: .4rem; margin-bottom: 1.1rem; }
.auth-tab {
  flex: 1; font-family: inherit; font-weight: 700; font-size: .9rem;
  padding: .6rem; border-radius: var(--radius-sm); border: 1px solid var(--border);
  background: var(--surface-2); color: var(--ink-soft); cursor: pointer;
}
.auth-tab.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.auth-form { display: flex; flex-direction: column; gap: .8rem; }
.auth-submit { justify-content: center; padding: .75rem; }
.auth-error { color: #c0392b; font-size: .85rem; margin: 0; }
.save-search-actions { display: flex; flex-direction: column; gap: .6rem; margin-top: .5rem; }

/* ---------- Bookmark / note controls on cards ---------- */
.card-actions { display: flex; gap: .4rem; align-items: center; }
.icon-btn {
  width: 32px; height: 32px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--ink-soft);
  cursor: pointer; transition: all .15s ease;
}
.icon-btn:hover { border-color: var(--accent); color: var(--accent-dark); }
.icon-btn.active { background: var(--gold-soft); border-color: var(--gold); color: var(--gold); }
.note-box { margin-top: .9rem; }
.note-box textarea {
  width: 100%; min-height: 80px; font-family: inherit; font-size: .9rem;
  padding: .6rem .75rem; border-radius: var(--radius-sm); border: 1.5px solid var(--border);
  background: var(--surface-2); color: var(--ink); resize: vertical;
}
.note-box .btn-ghost { margin-top: .5rem; }

/* ---------- Cabinet / admin pages ---------- */
.cabinet-tabs, .admin-tabs {
  display: flex; gap: .5rem; margin: 1.2rem 0 1rem;
  border-bottom: 1px solid var(--border);
}
.cabinet-tab, .admin-tab {
  font-family: inherit; font-weight: 700; font-size: .92rem;
  padding: .7rem 1.1rem; background: none; border: none;
  color: var(--ink-soft); cursor: pointer; border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.cabinet-tab.active, .admin-tab.active { color: var(--accent-dark); border-bottom-color: var(--accent); }
.remove-btn { background: none; border: 1px solid var(--border); cursor: pointer; font-family: inherit; }
.remove-btn:hover { border-color: #c0392b; color: #c0392b; }

/* ---------- Layout ---------- */
.wrap {
  max-width: 980px;
  margin: -1.75rem auto 0;
  padding: 0 1.25rem;
}

/* ---------- Search card ---------- */
.search-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  padding: 1.1rem;
}

.main-search {
  display: flex;
  gap: .6rem;
}
.main-search input {
  flex: 1;
  font-family: inherit;
  font-size: 1.05rem;
  padding: .85rem 1rem;
  border-radius: var(--radius-sm);
  border: 1.5px solid var(--border);
  background: var(--surface-2);
  color: var(--ink);
  transition: border-color .15s ease, box-shadow .15s ease;
}
.main-search input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.btn-primary {
  display: flex;
  align-items: center;
  gap: .4rem;
  font-family: inherit;
  font-weight: 700;
  font-size: 1rem;
  padding: 0 1.4rem;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background .15s ease, transform .1s ease;
}
.btn-primary:hover { background: var(--accent-dark); }
.btn-primary:active { transform: scale(.97); }

.toggle-advanced {
  display: flex;
  align-items: center;
  gap: .35rem;
  margin-top: .75rem;
  background: none;
  border: none;
  color: var(--accent);
  font-family: inherit;
  font-weight: 700;
  font-size: .9rem;
  cursor: pointer;
  padding: .3rem .2rem;
}
.toggle-advanced .chev { transition: transform .2s ease; }
.toggle-advanced.open .chev { transform: rotate(180deg); }

.advanced-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: .85rem;
  margin-top: .9rem;
  padding-top: .9rem;
  border-top: 1px solid var(--border);
  animation: fadeSlide .22s ease both;
}
@keyframes fadeSlide {
  from { opacity: 0; transform: translateY(-6px); }
  to { opacity: 1; transform: translateY(0); }
}

.field { position: relative; display: flex; flex-direction: column; gap: .35rem; }
.field label { font-size: .8rem; color: var(--ink-soft); font-weight: 500; }
.field input, .field select {
  font-family: inherit;
  font-size: .92rem;
  padding: .6rem .75rem;
  border-radius: var(--radius-sm);
  border: 1.5px solid var(--border);
  background: var(--surface-2);
  color: var(--ink);
}
.field input:focus, .field select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.ac-list {
  position: absolute;
  top: calc(100% + 4px);
  right: 0; left: 0;
  z-index: 20;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  max-height: 240px;
  overflow-y: auto;
  display: none;
}
.ac-list.open { display: block; }
.ac-item {
  padding: .55rem .8rem;
  cursor: pointer;
  font-size: .9rem;
  display: flex;
  justify-content: space-between;
  gap: .5rem;
}
.ac-item:hover, .ac-item.active { background: var(--accent-soft); }
.ac-item .extra { color: var(--ink-soft); font-size: .78rem; white-space: nowrap; }
.ac-empty { padding: .6rem .8rem; color: var(--ink-soft); font-size: .85rem; }

.field.has-value input { border-color: var(--gold); background: var(--gold-soft); }

/* ---------- Stats ---------- */
.stats-bar {
  margin-top: 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .9rem 1.1rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1.2rem;
  animation: fadeSlide .25s ease both;
}
.stat-total {
  display: flex;
  align-items: baseline;
  gap: .4rem;
  font-weight: 700;
  white-space: nowrap;
}
.stat-total span:first-child {
  font-size: 1.5rem;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}
.stat-breakdown { display: flex; flex-wrap: wrap; gap: .4rem; }
.stat-chip {
  font-size: .78rem;
  padding: .3rem .65rem;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--ink-soft);
  white-space: nowrap;
}
.stat-chip b { color: var(--ink); }

/* ---------- Results ---------- */
.empty-state {
  text-align: center;
  color: var(--ink-soft);
  padding: 4rem 1rem;
}
.empty-state svg { opacity: .4; margin-bottom: .75rem; }
.empty-state p { max-width: 380px; margin: 0 auto; line-height: 1.7; }

.loading { display: flex; justify-content: center; padding: 3rem; }
.spinner {
  width: 34px; height: 34px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.results-list {
  display: flex;
  flex-direction: column;
  gap: .75rem;
  margin-top: 1rem;
}
.result-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.15rem;
  cursor: pointer;
  transition: box-shadow .18s ease, transform .12s ease, border-color .18s ease;
  animation: fadeSlide .3s ease both;
}
.result-card:hover {
  box-shadow: var(--shadow);
  border-color: var(--accent);
  transform: translateY(-1px);
}
.result-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: .75rem;
  margin-bottom: .5rem;
  flex-wrap: wrap;
}
.result-book {
  font-weight: 700;
  color: var(--accent-dark);
  font-size: .88rem;
}
.result-loc { color: var(--ink-soft); font-size: .78rem; }
.result-matn {
  font-size: 1.08rem;
  line-height: 2;
  color: var(--ink);
}
.result-tags {
  display: flex;
  flex-wrap: wrap;
  gap: .35rem;
  margin-top: .65rem;
}
.tag {
  font-size: .74rem;
  padding: .22rem .6rem;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-dark);
  white-space: nowrap;
}
.tag.gold { background: var(--gold-soft); color: var(--gold); }

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: .5rem;
  margin-top: 1.5rem;
}
.pagination button {
  font-family: inherit;
  min-width: 38px;
  padding: .45rem .7rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  transition: background .15s ease, color .15s ease;
}
.pagination button:hover:not(:disabled) { background: var(--accent-soft); }
.pagination button.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.pagination button:disabled { opacity: .4; cursor: default; }

/* ---------- Detail overlay ---------- */
.detail-overlay {
  position: fixed; inset: 0;
  background: rgba(20,18,14,.5);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 3vh 1rem;
  z-index: 100;
  animation: fadeSlide .18s ease both;
  overflow-y: auto;
}
.detail-panel {
  background: var(--surface);
  border-radius: var(--radius);
  max-width: 720px;
  width: 100%;
  padding: 1.75rem;
  position: relative;
  box-shadow: 0 24px 60px -12px rgba(0,0,0,.35);
}
.detail-close {
  position: absolute;
  top: .8rem; left: .8rem;
  width: 32px; height: 32px;
  border-radius: 50%;
  border: none;
  background: var(--surface-2);
  color: var(--ink-soft);
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
}
.detail-close:hover { background: var(--accent-soft); color: var(--accent-dark); }
.detail-content h2 { margin-top: 0; font-size: 1.15rem; color: var(--accent-dark); }
.detail-matn {
  font-size: 1.25rem;
  line-height: 2.1;
  padding: 1rem;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  margin: .75rem 0 1.25rem;
}
.detail-section { margin-bottom: 1.1rem; }
.detail-section h3 {
  font-size: .85rem;
  color: var(--ink-soft);
  margin: 0 0 .5rem;
  font-weight: 700;
}
.narrator-chain {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .3rem;
  font-size: .92rem;
}
.narrator-chain .arrow { color: var(--ink-soft); }
.commentary-item {
  padding: .7rem .85rem;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  margin-bottom: .5rem;
  font-size: .9rem;
  line-height: 1.8;
}
.commentary-item b { color: var(--accent-dark); }

@media (max-width: 640px) {
  .wrap { margin-top: -1.2rem; }
  .main-search { flex-direction: column; }
  .btn-primary { justify-content: center; padding: .8rem; }
}
