:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #f0eee9;
  --text: #1d1c1a;
  --muted: #6b6861;
  --line: #e4e1da;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #dcf1ed;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --var-empty: #fde68a;
  --var-empty-ink: #713f12;
  --var-filled: #ccfbf1;
  --var-filled-ink: #134e4a;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 14px rgba(0,0,0,.05);
  --radius: 14px;
  --nav-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141413; --surface: #1e1e1c; --surface-2: #272724; --text: #f2f0eb; --muted: #a19d94;
    --line: #34332f; --accent: #2dd4bf; --accent-ink: #062a26; --accent-soft: #12322e;
    --danger: #f87171; --danger-soft: #3a1a18;
    --var-empty: #5c4a14; --var-empty-ink: #fde68a; --var-filled: #134e4a; --var-filled-ink: #ccfbf1;
    --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #141413; --surface: #1e1e1c; --surface-2: #272724; --text: #f2f0eb; --muted: #a19d94;
  --line: #34332f; --accent: #2dd4bf; --accent-ink: #062a26; --accent-soft: #12322e;
  --danger: #f87171; --danger-soft: #3a1a18;
  --var-empty: #5c4a14; --var-empty-ink: #fde68a; --var-filled: #134e4a; --var-filled-ink: #ccfbf1;
  --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: "Segoe UI", "Noto Sans Arabic", Tahoma, system-ui, sans-serif;
  font-size: 16px; line-height: 1.55;
  padding-bottom: calc(var(--nav-h) + 24px + env(safe-area-inset-bottom));
}
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.ltr { direction: ltr; text-align: left; unicode-bidi: plaintext; }

/* ───── الشريط العلوي والتبويبات ───── */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--bg); /* بلا backdrop-filter: يكسر position:fixed للتبويبات السفلية */
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 18px; margin-inline-end: auto; }
.tabs {
  position: fixed; inset: auto 0 0 0; z-index: 20; display: flex;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--line);
}
.tabs a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 13px; color: var(--muted); font-weight: 600;
}
.tabs a.active { color: var(--accent); }
@media (min-width: 760px) {
  body { padding-bottom: 40px; }
  .tabs { position: static; height: auto; padding: 4px; background: var(--surface-2); border: 0; border-radius: 12px; gap: 4px; order: 0; }
  .tabs a { flex-direction: row; gap: 8px; padding: 6px 16px; border-radius: 9px; font-size: 15px; }
  .tabs a.active { background: var(--surface); box-shadow: var(--shadow); }
  .tabs svg { width: 18px; height: 18px; }
}
.iconbtn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-width: 40px; height: 40px; padding: 0 10px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); cursor: pointer;
}
.iconbtn svg { width: 20px; height: 20px; }
body.admin #lockBtn { background: var(--accent-soft); color: var(--accent); border-color: transparent; font-size: 13px; font-weight: 700; }

.view { max-width: 1100px; margin: 0 auto; padding: 16px; }

/* ───── البحث والتصنيفات ───── */
.search {
  display: flex; align-items: center; gap: 8px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 12px; padding: 0 12px; height: 46px; margin-bottom: 12px;
}
.search svg { color: var(--muted); width: 20px; height: 20px; }
.search input { flex: 1; border: 0; background: transparent; outline: none; min-width: 0; height: 100%; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 14px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; padding: 6px 14px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--surface); font-size: 14px; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.chip.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.tag { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }

/* ───── البطاقات ───── */
.grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(min(100%, 250px), 1fr)); }
.grid.two { grid-template-columns: repeat(2, 1fr); gap: 10px; }
@media (min-width: 560px) { .grid.two { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; } }
.card {
  display: block; background: var(--surface); border-radius: var(--radius); overflow: hidden;
  box-shadow: var(--shadow); border: 1px solid var(--line); transition: transform .12s;
}
.card:active { transform: scale(.985); }
.card-body { padding: 10px 12px 12px; }
.card-title { font-weight: 700; font-size: 15px; line-height: 1.35; margin: 0 0 6px; }
.card-sub { color: var(--muted); font-size: 13px; display: flex; gap: 6px; flex-wrap: wrap; }

.ba { display: grid; grid-template-columns: 1fr 1fr; gap: 2px; background: var(--line); aspect-ratio: 4 / 3; }
.ba.big { aspect-ratio: auto; gap: 8px; background: transparent; }
.ba > div { position: relative; background: var(--surface-2); overflow: hidden; }
.ba img { width: 100%; height: 100%; object-fit: cover; display: block; }
.ba.big > div { border-radius: 12px; aspect-ratio: 1; max-height: 42vh; cursor: zoom-in; }
@media (min-width: 760px) { .ba.big { max-width: 720px; } .ba.big > div { aspect-ratio: 4 / 5; max-height: 60vh; } }
.ba.big img { object-fit: contain; }
.ba .lbl {
  position: absolute; top: 6px; inset-inline-start: 6px; font-size: 11px; font-weight: 700;
  padding: 1px 8px; border-radius: 999px; background: rgba(0,0,0,.55); color: #fff;
}
.ba .lbl.after { background: var(--accent); color: var(--accent-ink); }
.noimg { display: flex; align-items: center; justify-content: center; width: 100%; height: 100%; color: var(--muted); font-size: 12px; }

.thumb { aspect-ratio: 1; background: var(--surface-2); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }

.empty { text-align: center; color: var(--muted); padding: 48px 16px; }
.empty strong { display: block; color: var(--text); font-size: 17px; margin-bottom: 4px; }

/* ───── صفحة التفاصيل ───── */
.back { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); font-weight: 600; margin-bottom: 10px; cursor: pointer; background: none; border: 0; padding: 4px 0; }
.back svg { width: 18px; height: 18px; }
.detail-head { display: flex; align-items: flex-start; gap: 10px; margin-bottom: 12px; }
.detail-head h1 { font-size: 22px; line-height: 1.3; margin: 0 0 6px; flex: 1; }
.admin-only { display: none !important; }
body.admin .admin-only { display: inline-flex !important; }
.desc { color: var(--muted); margin: 0 0 16px; white-space: pre-wrap; }
.section { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 14px; }
.section h2 { font-size: 14px; color: var(--muted); margin: 0 0 10px; font-weight: 700; }
.vars { display: grid; gap: 10px; }
.prompt-text {
  white-space: pre-wrap; word-break: break-word; font-family: ui-monospace, Consolas, "Cascadia Mono", monospace;
  font-size: 14px; line-height: 1.6; background: var(--surface-2); border-radius: 10px; padding: 12px; margin: 0;
  max-height: 50vh; overflow: auto;
}
mark.var { border-radius: 4px; padding: 0 3px; font-weight: 700; }
mark.var.empty { background: var(--var-empty); color: var(--var-empty-ink); }
mark.var.filled { background: var(--var-filled); color: var(--var-filled-ink); }

.sticky-actions {
  position: sticky; bottom: calc(var(--nav-h) + 12px + env(safe-area-inset-bottom)); z-index: 5;
  display: flex; gap: 10px; margin-top: 4px;
}
@media (min-width: 760px) { .sticky-actions { bottom: 16px; } }

/* ───── الكاميو ───── */
.mat-hero { display: flex; gap: 14px; align-items: center; margin-bottom: 14px; }
.mat-hero .thumb { width: 92px; border-radius: 12px; overflow: hidden; flex: none; cursor: zoom-in; }
.seg { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.seg button {
  flex: 1 1 140px; text-align: start; padding: 10px 12px; border-radius: 12px; border: 1.5px solid var(--line);
  background: var(--surface); cursor: pointer;
}
.seg button b { display: block; font-size: 15px; }
.seg button small { color: var(--muted); font-size: 12px; }
.seg button.active { border-color: var(--accent); background: var(--accent-soft); }
.tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; direction: ltr; }
@media (min-width: 560px) { .tiles { grid-template-columns: repeat(3, 1fr); } }
.tile { background: var(--surface-2); border-radius: 12px; padding: 10px 12px; text-align: left; }
.tile.wide { grid-column: 1 / -1; }
.tile .k { display: block; font-size: 12px; color: var(--muted); font-weight: 600; }
.tile .v { display: block; font-size: 26px; font-weight: 800; line-height: 1.25; }
.tile.wide .v { font-size: 20px; }
.tile .v.off { color: var(--muted); font-weight: 600; font-size: 18px; }
.tile .v.on { color: var(--accent); font-size: 18px; }
.cut-ar { font-size: 13px; color: var(--muted); }
details.more { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 14px; }
details.more summary { cursor: pointer; padding: 12px 14px; font-weight: 700; list-style: none; display: flex; justify-content: space-between; }
details.more summary::-webkit-details-marker { display: none; }
details.more summary::after { content: "+"; color: var(--muted); font-size: 20px; line-height: 1; }
details.more[open] summary::after { content: "−"; }
details.more > div { padding: 0 14px 14px; white-space: pre-wrap; }

/* ───── الأزرار ───── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; height: 44px; padding: 0 18px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--surface); font-weight: 700; cursor: pointer;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.big { height: 54px; font-size: 17px; flex: 1; box-shadow: 0 6px 20px color-mix(in srgb, var(--accent) 35%, transparent); }
.btn.danger { color: var(--danger); }
.btn.sm { height: 36px; padding: 0 12px; font-size: 14px; }
.btn:disabled { opacity: .55; cursor: default; }
.btn svg { width: 20px; height: 20px; }
.row-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.fab {
  position: fixed; z-index: 15; inset-inline-start: 18px; bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom));
  width: 58px; height: 58px; border-radius: 18px; border: 0; background: var(--accent); color: var(--accent-ink);
  box-shadow: 0 8px 24px color-mix(in srgb, var(--accent) 45%, transparent); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.fab svg { width: 28px; height: 28px; stroke-width: 2.5; }
.fab[hidden] { display: none; }
@media (min-width: 760px) { .fab { bottom: 24px; inset-inline-start: 24px; } }

/* ───── النماذج ───── */
.form { display: grid; gap: 14px; }
.field { display: grid; gap: 6px; }
.field > span { font-size: 14px; font-weight: 700; }
.field > small, .hint { color: var(--muted); font-size: 13px; }
.input, textarea.input, select.input {
  width: 100%; border: 1px solid var(--line); background: var(--surface); border-radius: 12px;
  padding: 10px 12px; outline: none; min-height: 46px;
}
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea.input { min-height: 140px; resize: vertical; }
textarea.mono { font-family: ui-monospace, Consolas, "Cascadia Mono", monospace; font-size: 14px; }
.input.invalid { border-color: var(--danger); box-shadow: 0 0 0 3px var(--danger-soft); }
.imgs { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.imgfield {
  position: relative; aspect-ratio: 1; max-height: 260px; border-radius: 12px; border: 2px dashed var(--line); background: var(--surface);
  overflow: hidden; cursor: pointer; display: flex; align-items: center; justify-content: center; text-align: center;
  color: var(--muted); font-weight: 600; font-size: 14px;
}
.imgfield.square { aspect-ratio: 1; max-width: 180px; }
.imgfield.has { border-style: solid; }
.imgfield img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.imgfield .x {
  position: absolute; top: 6px; inset-inline-end: 6px; width: 30px; height: 30px; border-radius: 50%; border: 0;
  background: rgba(0,0,0,.6); color: #fff; font-size: 18px; line-height: 1; cursor: pointer; z-index: 2;
}
.imgfield.busy::after { content: "جارٍ الرفع…"; position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: color-mix(in srgb, var(--surface) 80%, transparent); color: var(--text); }
.imgfield .ph { display: grid; gap: 4px; justify-items: center; padding: 8px; }
.setrow { border: 1px solid var(--line); border-radius: 14px; padding: 12px; display: grid; gap: 10px; background: var(--surface); }
.setrow-head { display: flex; justify-content: space-between; align-items: center; font-weight: 700; }
.setgrid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; direction: ltr; }
@media (min-width: 560px) { .setgrid { grid-template-columns: repeat(4, 1fr); } }
.setgrid label { display: grid; gap: 4px; font-size: 12px; font-weight: 700; color: var(--muted); text-align: left; }
.setgrid .span2 { grid-column: span 2; }
.toggle { display: flex !important; align-items: center; gap: 8px; color: var(--text) !important; font-size: 13px !important; min-height: 46px; }
.toggle input { width: 20px; height: 20px; accent-color: var(--accent); }
.form-actions { display: flex; gap: 10px; position: sticky; bottom: calc(var(--nav-h) + 12px + env(safe-area-inset-bottom)); background: var(--bg); padding: 8px 0; }
@media (min-width: 760px) { .form-actions { bottom: 0; } }
.form-actions .btn.primary { flex: 1; }

.list { display: grid; gap: 8px; }
.list-row { display: flex; gap: 8px; align-items: center; }
.list-row .input { flex: 1; }

/* ───── الإشعار والنوافذ ───── */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--nav-h) + 90px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 12px; font-weight: 700;
  opacity: 0; pointer-events: none; transition: .2s; z-index: 60; max-width: calc(100% - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: var(--danger); color: #fff; }
.modal { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.45); display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal[hidden] { display: none; }
.modal-box { background: var(--surface); border-radius: 18px; padding: 20px; width: 100%; max-width: 360px; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal-box h3 { margin: 0 0 4px; font-size: 18px; }
.modal-box p { margin: 0 0 14px; color: var(--muted); font-size: 14px; }
.pin-input { text-align: center; font-size: 26px; letter-spacing: 10px; direction: ltr; }
.modal-actions { display: flex; gap: 8px; margin-top: 14px; }
.modal-actions .btn { flex: 1; }
.lightbox { position: fixed; inset: 0; z-index: 70; background: rgba(0,0,0,.92); display: flex; align-items: center; justify-content: center; cursor: zoom-out; }
.lightbox[hidden] { display: none; }
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; }
.page-title { font-size: 22px; margin: 0 0 14px; }
.loading { color: var(--muted); text-align: center; padding: 40px; }
