:root {
  /* ---- required theme variables ---- */
  --background: #f4f6fb;
  --surface: #ffffff;
  --surface-alt: #eef1f7;
  --card: #ffffff;
  --input-bg: #ffffff;
  --input-text: #1a1d23;
  --text-primary: #14181f;
  --text-secondary: #5b6573;
  --border: #e6e9f0;
  --badge-bg: #eef2ff;
  --badge-text: #4f46e5;
  --button-bg: #ffffff;
  --button-text: #1a1d23;

  /* ---- semantic accents (theme aware) ---- */
  --primary: #6366f1;
  --primary-strong: #4f46e5;
  --primary-soft: #eef2ff;
  --primary-grad: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
  --success: #10b981;
  --success-soft: #d1fae5;
  --warn: #f59e0b;
  --warn-soft: #fef3c7;
  --danger: #ef4444;
  --danger-soft: #fee2e2;
  --info: #06b6d4;
  --info-soft: #cffafe;
  --disabled-text: #9aa1ad;

  /* ---- shape / elevation ---- */
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 1px 2px rgba(16,24,40,.04), 0 4px 12px rgba(16,24,40,.05);
  --shadow-md: 0 6px 18px rgba(16,24,40,.08);
  --shadow-lg: 0 18px 40px rgba(16,24,40,.12);
  --ring: 0 0 0 4px rgba(99,102,241,.16);
  --font: "Inter", "Segoe UI", system-ui, -apple-system, "Tahoma", "Noto Sans Arabic", sans-serif;
}

/* legacy aliases so older rules keep working */
:root {
  --bg: var(--background);
  --surface-2: var(--surface-alt);
  --text: var(--text-primary);
  --muted: var(--text-secondary);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--background); color: var(--text-primary); font-family: var(--font); -webkit-font-smoothing: antialiased; line-height: 1.55; }
body { background-image: radial-gradient(1200px 600px at 100% -10%, rgba(99,102,241,.07), transparent), radial-gradient(900px 500px at -10% 110%, rgba(139,92,246,.06), transparent); background-attachment: fixed; }
a { color: var(--primary); text-decoration: none; }
button { font-family: inherit; cursor: pointer; }

/* ===================== Layout ===================== */
.layout { display: grid; grid-template-columns: 256px 1fr; min-height: 100vh; }

.sidebar {
  background: linear-gradient(180deg, #ffffff 0%, #fbfcff 100%);
  border-inline-end: 1px solid var(--border);
  padding: 24px 16px; display: flex; flex-direction: column; gap: 4px;
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
  box-shadow: 2px 0 18px rgba(16,24,40,.04);
}
.brand { font-weight: 800; font-size: 17px; display: flex; align-items: center; gap: 10px; margin-bottom: 22px; color: var(--text-primary); letter-spacing: -.2px; }
.brand .dot { width: 12px; height: 12px; border-radius: 50%; background: var(--primary-grad); box-shadow: 0 0 0 4px rgba(99,102,241,.16); }
.brand-logo { height: 30px; width: auto; border-radius: 9px; background: var(--surface-alt); padding: 3px; box-shadow: var(--shadow); }

.site-footer { margin-top: auto; padding: 16px 0 4px; border-top: 1px solid var(--border); font-size: 12px; color: var(--text-secondary); text-align: center; }
.site-footer a { color: var(--primary); font-weight: 700; }

.nav-link {
  padding: 11px 14px; border-radius: 12px; color: var(--text-secondary); font-weight: 600; font-size: 14px;
  background: transparent; border: 1px solid transparent; text-align: start; transition: all .18s ease;
  display: flex; align-items: center; gap: 10px;
}
.nav-link span:last-child { flex: 1; }
.nav-link:hover { background: var(--surface-alt); color: var(--primary-strong); transform: translateX(2px); }
.nav-link.active { background: var(--primary-soft); color: var(--primary-strong); border-color: rgba(99,102,241,.25); box-shadow: inset 3px 0 0 var(--primary); }
.nav-link small { display: block; font-weight: 400; font-size: 11px; opacity: .7; }

.content { padding: 30px 36px; max-width: 1240px; color: var(--text-primary); }
.page-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px; flex-wrap: wrap; gap: 14px; }
.page-head h1 { font-size: 24px; margin: 0; color: var(--text-primary); letter-spacing: -.4px; font-weight: 800; }
.page-head p { margin: 5px 0 0; color: var(--text-secondary); font-size: 13px; }

/* ===================== Cards ===================== */
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); transition: box-shadow .2s ease, transform .2s ease; }
.card-pad { padding: 22px; }

/* ===================== Admin security ===================== */
.security-warning {
  margin-bottom: 18px; padding: 13px 16px; border-radius: 12px;
  color: #854d0e; background: var(--warn-soft); border: 1px solid rgba(245,158,11,.35);
  font-size: 13px; font-weight: 700;
}
.security-panel { max-width: 620px; }
.security-form { display: grid; gap: 10px; }
.security-form label { margin-top: 4px; font-weight: 700; font-size: 13px; }
.security-form .btn { justify-self: start; margin-top: 8px; }
.security-note { color: var(--text-secondary); font-size: 12px; margin: 2px 0; }
.form-message { min-height: 22px; margin: 4px 0 0; font-size: 13px; font-weight: 700; }
.form-message.error { color: var(--danger); }
.form-message.success { color: var(--success); }

.login-page { min-height: 100vh; color-scheme: dark; }
.login-shell {
  min-height: 100vh; display: grid; place-items: center; padding: 24px;
}
.login-card {
  width: min(100%, 430px); padding: 34px;
  background: var(--card); border: 1px solid var(--border); border-radius: 22px;
  box-shadow: var(--shadow-lg);
}
.login-brand {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  font-size: 18px; font-weight: 800; margin-bottom: 24px;
}
.login-card h1 { margin: 0; text-align: center; font-size: 25px; }
.login-card > p { color: var(--text-secondary); text-align: center; margin: 8px 0 24px; }
.login-card form { display: grid; gap: 10px; }
.login-card label { font-weight: 700; font-size: 13px; }
.login-card .input { width: 100%; padding: 12px 14px; font-size: 17px; letter-spacing: 2px; }
.login-submit { width: 100%; justify-content: center; margin-top: 6px; }
.public-return { display: block; text-align: center; margin-top: 20px; font-size: 13px; font-weight: 700; }

.stats-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 16px; margin-bottom: 26px; }
.stat {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px 18px 16px; box-shadow: var(--shadow); position: relative; overflow: hidden;
  transition: transform .18s ease, box-shadow .18s ease;
}
.stat::before { content: ""; position: absolute; inset: 0 0 auto 0; height: 3px; background: var(--primary-grad); opacity: .9; }
.stat:hover { transform: translateY(-3px); box-shadow: var(--shadow-md); }
.stat .label { color: var(--text-secondary); font-size: 12px; margin-bottom: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: .4px; }
.stat .value { font-size: 28px; font-weight: 800; color: var(--text-primary); letter-spacing: -.5px; }
.stat.accent .value { background: var(--primary-grad); -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; }

/* ===================== Uploader ===================== */
.uploader {
  border: 2px dashed var(--border); border-radius: var(--radius); padding: 32px; text-align: center;
  transition: .2s; background: linear-gradient(180deg, #ffffff, #fafbff);
}
.uploader.drag { border-color: var(--primary); background: var(--primary-soft); transform: scale(1.01); }
.uploader h3 { margin: 0 0 8px; color: var(--text-primary); font-size: 17px; }
.uploader p { color: var(--text-secondary); font-size: 13px; margin: 5px 0; }

/* ===================== Buttons ===================== */
.btn {
  border: 1px solid var(--border); background: var(--button-bg); color: var(--button-text);
  padding: 10px 16px; border-radius: 12px; font-size: 13px; font-weight: 700; transition: all .16s ease;
  display: inline-flex; align-items: center; gap: 6px;
}
.btn:hover { background: var(--surface-alt); box-shadow: var(--shadow); transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn.primary { background: var(--primary-grad); color: #fff; border-color: transparent; box-shadow: 0 6px 16px rgba(99,102,241,.32); }
.btn.primary:hover { filter: brightness(1.06); box-shadow: 0 8px 22px rgba(99,102,241,.4); }
.btn.danger { color: var(--danger); border-color: var(--danger-soft); background: #fff; }
.btn.danger:hover { background: var(--danger-soft); }
.btn.sm { padding: 6px 12px; font-size: 12px; border-radius: 10px; }
.btn:disabled, .btn[disabled] { opacity: .55; color: var(--disabled-text); cursor: not-allowed; background: var(--surface-alt); transform: none; box-shadow: none; }

/* ===================== Progress ===================== */
.progress-wrap { margin-top: 14px; }
.progress { height: 9px; background: var(--surface-alt); border-radius: 99px; overflow: hidden; box-shadow: inset 0 1px 2px rgba(0,0,0,.05); }
.progress > span { display: block; height: 100%; width: 0; background: var(--primary-grad); border-radius: 99px; transition: width .3s ease; }

/* ===================== Tables ===================== */
table { width: 100%; border-collapse: collapse; font-size: 13px; color: var(--text-primary); }
th, td { text-align: start; padding: 13px 12px; border-bottom: 1px solid var(--border); }
th { color: var(--text-secondary); font-weight: 700; font-size: 12px; text-transform: uppercase; letter-spacing: .3px; background: var(--surface-alt); }
th:first-child { border-top-start-radius: 10px; }
th:last-child { border-top-end-radius: 10px; }
tbody tr { transition: background .15s ease; }
tbody tr:hover { background: var(--surface-alt); }
tr:last-child td { border-bottom: none; }
.row-actions { display: flex; gap: 7px; flex-wrap: wrap; }

/* ===================== Badges ===================== */
.badge { display: inline-flex; align-items: center; padding: 4px 11px; border-radius: 99px; font-size: 11px; font-weight: 700; background: var(--badge-bg); color: var(--badge-text); letter-spacing: .2px; }
.badge.pending, .badge.processing { background: var(--warn-soft); color: var(--warn); }
.badge.completed { background: var(--success-soft); color: var(--success); }
.badge.error { background: var(--danger-soft); color: var(--danger); }
.badge.duplicate { background: var(--info-soft); color: var(--info); }
.badge.type { background: var(--primary-soft); color: var(--primary-strong); }

/* ===================== Toolbar / Inputs ===================== */
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 16px; }
.input, select.input, textarea.input {
  border: 1px solid var(--border); border-radius: 12px; padding: 10px 14px; font-size: 13px;
  background: var(--input-bg); color: var(--input-text); transition: all .16s ease; box-shadow: var(--shadow);
}
.input:focus, select.input:focus, textarea.input:focus { outline: none; box-shadow: var(--ring); border-color: var(--primary); }

textarea, input, select {
  background: var(--input-bg); color: var(--input-text);
  border: 1px solid var(--border); border-radius: 12px; transition: all .16s ease; box-shadow: var(--shadow);
}
textarea { padding: 9px 12px; font-family: inherit; font-size: 13px; width: 100%; }
input:focus, select:focus, textarea:focus { outline: none; box-shadow: var(--ring); border-color: var(--primary); }
select option { background: var(--input-bg); color: var(--input-text); }
select optgroup { background: var(--input-bg); color: var(--text-secondary); }

/* chips */
.type-filter { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 18px; }
.chip { padding: 8px 15px; border-radius: 99px; border: 1px solid var(--border); background: var(--card); font-size: 13px; font-weight: 700; color: var(--text-secondary); transition: all .16s ease; cursor: pointer; box-shadow: var(--shadow); }
.chip:hover { color: var(--primary-strong); border-color: rgba(99,102,241,.35); }
.chip.active { background: var(--primary-grad); color: #fff; border-color: transparent; box-shadow: 0 6px 14px rgba(99,102,241,.3); }

/* ===================== Search results ===================== */
.results { display: grid; grid-template-columns: repeat(auto-fill, minmax(330px, 1fr)); gap: 18px; }
.result-card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 11px; color: var(--text-primary); transition: transform .2s ease, box-shadow .2s ease; }
.result-card:hover { transform: translateY(-4px); box-shadow: var(--shadow-lg); }
.result-card .top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.result-card h3 { margin: 0; font-size: 15px; line-height: 1.45; color: var(--text-primary); }
.result-card .answer { color: var(--text-primary); font-size: 13px; line-height: 1.7; white-space: pre-wrap; }
.result-card .meta { display: flex; flex-wrap: wrap; gap: 6px; font-size: 11px; color: var(--text-secondary); }
.result-card .kw { font-size: 11px; color: var(--primary-strong); font-weight: 600; }
.result-card details { font-size: 12px; color: var(--text-secondary); border-top: 1px solid var(--border); padding-top: 11px; }
.result-card summary { cursor: pointer; font-weight: 700; color: var(--text-primary); }
.result-preview { background: var(--surface-alt); border: 1px solid var(--border); border-radius: 12px; padding: 12px; color: var(--text-primary); font-size: 13px; line-height: 1.7; }

.pagination { display: flex; gap: 8px; justify-content: center; margin-top: 24px; }

.empty { text-align: center; color: var(--text-secondary); padding: 48px; font-size: 14px; }

.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(10px); background: var(--text-primary); color: #fff; padding: 12px 20px; border-radius: 12px; font-size: 13px; font-weight: 600; box-shadow: var(--shadow-lg); z-index: 50; opacity: 0; transition: opacity .2s ease, transform .2s ease; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ===================== Tabs ===================== */
.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 22px; border-bottom: 1px solid var(--border); padding-bottom: 10px; }
.tab { padding: 9px 16px; border-radius: 11px; font-size: 13px; font-weight: 700; color: var(--text-secondary); background: transparent; border: none; transition: all .16s ease; }
.tab:hover { color: var(--primary-strong); background: var(--surface-alt); }
.tab.active { background: var(--primary-soft); color: var(--primary-strong); }
.tab-content { display: none; animation: fade .25s ease; }
.tab-content.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }

/* ===================== Topic tree ===================== */
.tree, .tree ul { list-style: none; padding-inline-start: 18px; margin: 4px 0; }
.tree > li { margin: 6px 0; }
.tree .node { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-radius: 11px; color: var(--text-primary); border: 1px solid transparent; transition: all .16s ease; }
.tree .node:hover { background: var(--surface-alt); border-color: var(--border); }
.tree .count { font-size: 11px; color: var(--text-secondary); background: var(--surface-alt); padding: 2px 9px; border-radius: 99px; font-weight: 700; }
.tree .act { font-size: 11px; padding: 4px 10px; border-radius: 9px; }

/* ===================== Confidence / review ===================== */
.conf { font-size: 11px; padding: 3px 9px; border-radius: 99px; font-weight: 700; }
.conf.ok { background: var(--success-soft); color: var(--success); }
.conf.low { background: var(--warn-soft); color: var(--warn); }
.conf.bad { background: var(--danger-soft); color: var(--danger); }

.review-card { border: 2px solid rgba(129,140,248,.25); border-radius: var(--radius); padding: 18px; margin-bottom: 24px; background: var(--card); box-shadow: var(--shadow); color: var(--text-primary); transition: box-shadow .2s ease, transform .2s ease, border-color .2s ease; }
.review-card:hover { box-shadow: var(--shadow-md); transform: translateY(-2px); border-color: rgba(129,140,248,.5); }
.new-item-card { margin-bottom: 24px; border: 2px solid rgba(129,140,248,.25); }
.review-card .meta { font-size: 12px; color: var(--text-secondary); margin: 8px 0; }
.review-card .answer-body { color: var(--text-primary); font-size: 13px; line-height: 1.7; white-space: pre-wrap; background: var(--surface-alt); border: 1px solid var(--border); border-radius: 10px; padding: 12px; margin: 10px 0; }
.review-card textarea { width: 100%; min-height: 72px; border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; font-family: inherit; background: var(--input-bg); color: var(--input-text); }
.review-card .title-field { width: 100%; border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; font-family: inherit; font-size: 17px; line-height: 1.65; font-weight: 800; background: var(--input-bg); color: var(--text-primary); margin-bottom: 12px; }
.review-card .title-field:focus { outline: none; box-shadow: var(--ring); border-color: var(--primary); }
.review-card .note { font-size: 11px; color: var(--text-secondary); margin: 5px 0; font-weight: 600; }
.review-card .editor-label { display: block; margin: 12px 0 7px; color: var(--text-primary); font-size: 13px; font-weight: 800; }
.review-card .cat-select { min-width: 220px; font-size: 15px; font-weight: 700; }
.review-card .knowledge-text-field { min-height: 170px; font-size: 16px; line-height: 1.95; font-weight: 500; color: var(--text-primary); resize: vertical; }

.queue-row .logs { font-size: 11px; color: var(--text-secondary); max-height: 90px; overflow: auto; white-space: pre-wrap; background: var(--surface-alt); border: 1px solid var(--border); padding: 8px; border-radius: 8px; margin-top: 6px; font-family: "SFMono-Regular", Consolas, monospace; }

.filters-row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 16px; }
.topic-select { max-height: 200px; }

.relationship-box { background: var(--surface-alt); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; margin: 8px 0; color: var(--text-primary); }
.relationship-box .rel-type { color: var(--primary-strong); font-weight: 700; }

/* dropdowns / selects get full theme styling */
select { padding: 9px 12px; }

/* modals (optional, future-proofed) */
.modal-backdrop { position: fixed; inset: 0; background: rgba(15,17,21,.5); backdrop-filter: blur(3px); display: flex; align-items: center; justify-content: center; z-index: 100; animation: fade .2s ease; }
.modal { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-lg); padding: 24px; max-width: 540px; width: 92%; color: var(--text-primary); }
.modal h2 { margin: 0 0 14px; font-size: 19px; color: var(--text-primary); font-weight: 800; }
.modal .modal-body { color: var(--text-primary); font-size: 14px; line-height: 1.7; }

.lang-switch { margin-top: auto; display: flex; gap: 6px; }

/* ===================== Theme toggle ===================== */
.theme-toggle { display: inline-flex; align-items: center; gap: 6px; margin-top: 12px; flex-wrap: wrap; }
.theme-toggle #themeBtn { width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--border); background: var(--button-bg); color: var(--button-text); font-size: 16px; cursor: pointer; transition: all .16s ease; box-shadow: var(--shadow); display: flex; align-items: center; justify-content: center; }
.theme-toggle #themeBtn:hover { background: var(--surface-alt); transform: translateY(-1px); }

/* ===================== RTL ===================== */
[dir="rtl"] .content { text-align: right; }
[dir="rtl"] th, [dir="rtl"] td { text-align: right; }
[dir="rtl"] .nav-link:hover { transform: translateX(-2px); }
[dir="rtl"] .nav-link.active { box-shadow: inset -3px 0 0 var(--primary); }

/* ===================== Dark mode ===================== */
:root[data-theme="dark"] {
  --background: #0d1016;
  --surface: #171b22;
  --surface-alt: #1f242d;
  --card: #171b22;
  --input-bg: #0f131a;
  --input-text: #e6e9ef;
  --text-primary: #eef1f6;
  --text-secondary: #9aa4b2;
  --border: #2a313c;
  --badge-bg: #232a44;
  --badge-text: #a5b4fc;
  --button-bg: #1f242d;
  --button-text: #e6e9ef;

  --primary: #818cf8;
  --primary-strong: #a5b4fc;
  --primary-soft: #232a44;
  --primary-grad: linear-gradient(135deg, #6366f1 0%, #a855f7 100%);
  --success: #34d399;
  --success-soft: #103226;
  --warn: #fbbf24;
  --warn-soft: #3a2e10;
  --danger: #f87171;
  --danger-soft: #3a1d1d;
  --info: #38bdf8;
  --info-soft: #0c2a3a;
  --disabled-text: #6b7280;

  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 12px rgba(0,0,0,.35);
  --shadow-md: 0 6px 18px rgba(0,0,0,.45);
  --shadow-lg: 0 18px 40px rgba(0,0,0,.55);
  --ring: 0 0 0 4px rgba(129,140,248,.22);
}
:root[data-theme="dark"] body { background: var(--background); background-image: radial-gradient(1200px 600px at 100% -10%, rgba(99,102,241,.12), transparent), radial-gradient(900px 500px at -10% 110%, rgba(168,85,247,.1), transparent); }
:root[data-theme="dark"] .sidebar { background: linear-gradient(180deg, #171b22 0%, #13161d 100%); }
:root[data-theme="dark"] .queue-row .logs { background: #0c0e12; }
:root[data-theme="dark"] .result-preview { background: #0c0e12; }
:root[data-theme="dark"] .review-card .answer-body { background: #0c0e12; }
:root[data-theme="dark"] .modal-backdrop { background: rgba(0,0,0,.65); }
:root[data-theme="dark"] .uploader { background: linear-gradient(180deg, #1a1f28 0%, #13161d 100%); border-color: var(--border); }
:root[data-theme="dark"] .uploader.drag { background: var(--primary-soft); border-color: var(--primary); }

@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; flex-direction: row; flex-wrap: wrap; }
  .nav-link small { display: none; }
  .content { padding: 20px; }
  .lang-switch { margin: 0; }
  .stats-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
}

.hidden { display: none !important; }

