:root {
  --bg: #0f1420;
  --panel: #161d2e;
  --panel-2: #1c2536;
  --border: #2a3450;
  --text: #e8ecf5;
  --muted: #93a0b8;
  --accent: #4f8cff;
  --accent-2: #22c55e;
  --danger: #ef4444;
  --radius: 12px;
  --font: "Vazirmatn", "Segoe UI", Tahoma, sans-serif;
  --topbar-h: 52px;
  --toolbar-h: 48px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  direction: rtl;
  /* جلوگیری از زوم دابل‌تپ روی iOS */
  touch-action: manipulation;
}

/* ---------- نوار بالا ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 16px;
  min-height: var(--topbar-h);
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand .logo { font-size: 24px; }
.brand h1 { font-size: 18px; margin: 0; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand small { color: var(--muted); font-weight: 400; margin-inline-start: 8px; }

.actions { display: flex; gap: 8px; align-items: center; flex-shrink: 0; }

.btn-icon {
  width: 38px;
  height: 38px;
  padding: 0;
  font-size: 17px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

#btn-menu { display: none; } /* فقط موبایل */

/* ---------- منوی بیشتر ---------- */
.more-menu {
  position: fixed;
  top: calc(var(--topbar-h) + 4px);
  inset-inline-end: 10px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 12px 40px rgba(0,0,0,.5);
  z-index: 70;
  min-width: 220px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.more-menu button {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 12px 16px;
  background: transparent;
  border: none;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  cursor: pointer;
  text-align: start;
}
.more-menu button:last-child { border-bottom: none; }
.more-menu button:hover { background: var(--panel-2); }

.more-backdrop { position: fixed; inset: 0; z-index: 60; }

/* ---------- دکمه‌ها ---------- */
.btn {
  font-family: inherit;
  font-size: 13px;
  padding: 8px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .05s;
  white-space: nowrap;
}
.btn:hover { background: #243048; border-color: #3a4a70; }
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn-primary:hover { background: #3d7bf0; border-color: #3d7bf0; }
.btn-danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: rgba(239, 68, 68, .12); }
.btn-ghost { background: transparent; }

.hidden { display: none !important; }

/* ---------- چیدمان ---------- */
.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 280px 1fr;
  min-height: 0;
}

/* ---------- سایدبار ---------- */
.sidebar {
  background: var(--panel);
  border-inline-end: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar-head {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}
.sidebar-head h2 { font-size: 14px; margin: 0; color: var(--muted); font-weight: 600; flex: 1; }
.sidebar-head small { color: var(--accent); }

#btn-sidebar-close { display: none; } /* فقط موبایل */

.tree {
  overflow-y: auto;
  flex: 1;
  padding: 8px;
  -webkit-overflow-scrolling: touch;
}

.tree-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13.5px;
  color: var(--text);
  position: relative;
}
.tree-row:hover { background: var(--panel-2); }
.tree-row.active { background: rgba(79, 140, 255, .15); outline: 1px solid rgba(79, 140, 255, .4); }
.tree-row .icon { opacity: .85; flex-shrink: 0; }
.tree-row .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; direction: ltr; text-align: right; }
.tree-row .size { font-size: 11px; color: var(--muted); }
.tree-row.dir-row .name { font-weight: 600; }

.dir-menu {
  display: none;
  gap: 2px;
  position: absolute;
  left: 6px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 2px;
  z-index: 5;
}
/* در دستگاه‌های لمسی منوی پوشه همیشه نمایان باشد */
@media (hover: none) {
  .dir-menu { display: flex; position: static; margin-inline-start: 4px; background: transparent; border: none; padding: 0; }
  .tree-row { flex-wrap: wrap; }
}
.tree-row.dir-row:hover .dir-menu { display: flex; }
.menu-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 12px;
  padding: 3px 5px;
  border-radius: 5px;
  cursor: pointer;
  font-family: inherit;
}
.menu-btn:hover { background: var(--panel-2); color: var(--text); }
.menu-btn.danger:hover { color: var(--danger); }

.empty-list {
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  padding: 24px 8px;
}

/* ---------- پنل اصلی ---------- */
.editor-pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; }

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 14px;
  min-height: var(--toolbar-h);
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.current-file { font-size: 13px; color: var(--muted); direction: ltr; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; min-width: 0; }
.toolbar .spacer { flex: 0; }

.toolbar-actions { display: flex; gap: 8px; align-items: center; }
#btn-file-actions { display: none; } /* فقط موبایل */

.save-state { font-size: 12px; color: var(--muted); flex-shrink: 0; }
.save-state.dirty { color: #f59e0b; }
.save-state.saved { color: var(--accent-2); }

/* ---------- حالت خواندن ---------- */
.read-pane {
  flex: 1;
  overflow-y: auto;
  padding: 28px clamp(16px, 6vw, 80px);
  -webkit-overflow-scrolling: touch;
}

/* ---------- حالت ویرایش (گرید دو ستونه) ---------- */
.edit-pane { flex: 1; min-height: 0; display: flex; }

.editor-grid {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  min-height: 0;
}

#editor {
  width: 100%;
  height: 100%;
  resize: none;
  border: none;
  outline: none;
  padding: 20px;
  background: var(--panel-2);
  color: var(--text);
  font-family: "JetBrains Mono", "Cascadia Code", Consolas, monospace;
  font-size: 14px;
  line-height: 1.9;
  direction: rtl;
  -webkit-overflow-scrolling: touch;
}

#editor::placeholder { color: var(--muted); }

.preview {
  overflow-y: auto;
  padding: 20px 24px;
  border-inline-start: 1px solid var(--border);
  background: var(--bg);
  -webkit-overflow-scrolling: touch;
}

.md-body a, .md-body a:visited {
  color: #93c5fd;
  text-decoration: underline;
  text-underline-offset: 3px;
}
.md-body a:hover, .md-body a:focus-visible { color: #bfdbfe; }

/* ---------- ورود و ثبت مدیر ---------- */
.auth-page {
  min-height: 100vh;
  min-height: 100dvh;
  height: auto;
  padding: clamp(16px, 4vw, 48px);
  justify-content: center;
  background:
    radial-gradient(circle at 15% 15%, rgba(79, 140, 255, .16), transparent 38%),
    radial-gradient(circle at 85% 90%, rgba(34, 197, 94, .07), transparent 35%),
    var(--bg);
}

.auth-shell {
  width: min(100%, 1000px);
  min-height: min(660px, calc(100dvh - 96px));
  margin: auto;
  display: grid;
  grid-template-columns: minmax(0, .95fr) minmax(0, 1fr);
  overflow: hidden;
  border: 1px solid #34415b;
  border-radius: 28px;
  background: var(--panel);
  box-shadow: 0 30px 90px rgba(0, 0, 0, .38);
}

.auth-intro {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: clamp(28px, 4vw, 52px);
  background:
    radial-gradient(circle at 10% 90%, rgba(128, 177, 255, .22), transparent 40%),
    linear-gradient(145deg, #202f53, #15213c 58%, #172039);
  border-inline-end: 1px solid rgba(255, 255, 255, .08);
}

.auth-brand { display: flex; align-items: center; gap: 12px; font-size: 18px; font-weight: 800; letter-spacing: -.02em; }
.auth-mark {
  display: grid; place-items: center;
  width: 42px; height: 42px;
  border-radius: 13px;
  background: #75a6ff;
  color: #111b31;
  font-size: 26px;
  box-shadow: 0 8px 22px rgba(79, 140, 255, .28);
}
.auth-eyebrow, .auth-kicker { color: #a8c9ff; font-size: 13px; font-weight: 700; }
.auth-intro-copy h2 { font-size: clamp(29px, 3vw, 42px); line-height: 1.45; letter-spacing: -.04em; margin: 16px 0; }
.auth-intro-copy p { max-width: 33ch; color: #bdcbe2; line-height: 2; margin: 0; }
.auth-intro-footer { color: #9eafcc; font-size: 12px; }

.auth-content {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 28px;
  padding: clamp(30px, 5vw, 68px);
}
.auth-form { width: 100%; max-width: 380px; }
.auth-form h1 { font-size: clamp(26px, 3vw, 34px); line-height: 1.4; margin: 8px 0 6px; letter-spacing: -.03em; }
.auth-description { color: var(--muted); line-height: 1.9; margin: 0 0 34px; font-size: 14px; }
.auth-field { margin-bottom: 20px; }
.auth-field label { display: block; margin-bottom: 9px; font-size: 13px; font-weight: 700; color: #dbe6f8; }
.auth-field input {
  display: block;
  width: 100%;
  height: 52px;
  padding: 0 16px;
  border: 1px solid #3a4862;
  border-radius: 12px;
  outline: none;
  background: #111a2b;
  color: var(--text);
  font: inherit;
  font-size: 16px;
  text-align: left;
  transition: border-color .2s, box-shadow .2s;
}
.auth-field input::placeholder { color: #8292ac; }
.auth-field input:focus { border-color: #8cb6ff; box-shadow: 0 0 0 3px rgba(79, 140, 255, .2); }
.auth-field small { display: block; margin-top: 8px; color: var(--muted); font-size: 12px; line-height: 1.6; }
.auth-submit {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  width: 100%;
  min-height: 52px;
  margin-top: 28px;
  border-radius: 12px;
  font-size: 15px;
  box-shadow: 0 12px 25px rgba(79, 140, 255, .18);
}
.auth-submit:disabled { opacity: .65; cursor: wait; }
.setup-link { display: flex; justify-content: center; gap: 8px; margin-top: 24px; color: #a8c9ff; font-size: 14px; font-weight: 700; text-decoration: none; }
.setup-link:hover, .setup-link:focus-visible { color: #d2e3ff; text-decoration: underline; text-underline-offset: 4px; }
.auth-setup-action { align-items: center; min-height: 52px; padding: 10px 16px; border-radius: 12px; background: var(--accent); color: #fff; }
.auth-setup-action:hover, .auth-setup-action:focus-visible { background: #3d7bf0; color: #fff; }
.login-err { padding: 12px 14px; border: 1px solid rgba(248, 113, 113, .5); border-radius: 10px; background: rgba(239, 68, 68, .11); color: #fecaca; font-size: 13px; line-height: 1.7; }
.auth-footnote { color: #8391a9; font-size: 12px; text-align: center; margin: 0; }

@media (max-width: 700px) {
  .auth-page { padding: 16px; }
  .auth-shell { max-width: 520px; min-height: 0; grid-template-columns: 1fr; border-radius: 20px; }
  .auth-intro { padding: 20px 24px; gap: 18px; }
  .auth-mark { width: 36px; height: 36px; border-radius: 11px; font-size: 22px; }
  .auth-brand { font-size: 16px; }
  .auth-intro-copy, .auth-intro-footer { display: none; }
  .auth-content { padding: 36px 24px 24px; gap: 32px; }
  .auth-description { margin-bottom: 28px; }
}

@media (max-width: 380px) {
  .auth-page { padding: 10px; }
  .auth-content { padding: 28px 18px 20px; }
  .auth-intro { padding: 16px 18px; }
}

/* ---------- جستجو ---------- */
.search-bar {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
.search-bar input {
  flex: 1;
  min-width: 0;
  font-family: inherit;
  font-size: 16px; /* ≥16px تا iOS زوم نکند */
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  outline: none;
}
.search-bar input:focus { border-color: var(--accent); }
.search-results {
  position: absolute;
  inset-inline: 8px;
  top: 120px;
  max-height: 55vh;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  z-index: 50;
  box-shadow: 0 12px 40px rgba(0,0,0,.5);
}
.search-hit {
  padding: 10px 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  font-size: 12.5px;
}
.search-hit:last-child { border-bottom: none; }
.search-hit:hover { background: var(--panel-2); }
.search-hit .hit-path { color: var(--accent); direction: ltr; display: block; font-size: 11.5px; }
.search-hit .hit-text { color: var(--muted); }
.search-hit .hit-text b { color: #fbbf24; }

/* ---------- مودال ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 90;
  padding: 16px;
}
.modal {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  width: min(560px, 100%);
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.modal-head h3 { margin: 0; font-size: 15px; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.modal-body { padding: 12px 18px; overflow-y: auto; }
.version-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.version-row:last-child { border-bottom: none; }
.version-row .v-time { flex: 1; direction: ltr; text-align: right; color: var(--muted); font-size: 12px; }

/* ---------- Bottom sheet (موبایل) ---------- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  z-index: 95;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.sheet {
  background: var(--panel);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: 20px 20px 0 0;
  width: 100%;
  max-width: 560px;
  padding: 8px 16px calc(16px + var(--safe-bottom));
  animation: sheet-up .22s ease-out;
}
@keyframes sheet-up {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}
.sheet-handle {
  width: 40px;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  margin: 6px auto 12px;
}
.sheet-title {
  margin: 0 0 8px;
  font-size: 13px;
  color: var(--muted);
  direction: ltr;
  text-align: right;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sheet-btn {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 14px 10px;
  background: transparent;
  border: none;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  font-family: inherit;
  font-size: 15px;
  cursor: pointer;
  text-align: start;
  border-radius: 10px;
}
.sheet-btn:last-of-type { border-bottom: none; }
.sheet-btn:active { background: var(--panel-2); }
.sheet-btn.danger { color: var(--danger); }
.sheet-btn.muted { color: var(--muted); }

/* ---------- توست ---------- */
.toast {
  position: fixed;
  bottom: calc(22px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 20px;
  border-radius: 999px;
  font-size: 13px;
  box-shadow: 0 8px 30px rgba(0,0,0,.4);
  z-index: 100;
  max-width: calc(100vw - 32px);
  text-align: center;
}
.toast.ok { border-color: var(--accent-2); }
.toast.err { border-color: var(--danger); }

/* ================================================================ */
/* موبایل — نما متمرکز                                               */
/* ================================================================ */
@media (max-width: 768px) {
  /* دکمه‌ی منو + برند فشرده */
  #btn-menu { display: inline-flex; }
  #btn-sidebar-close { display: inline-flex; }
  .brand h1 { font-size: 15px; }
  .brand small { display: none; }
  .btn-label { display: none; } /* دکمه‌ها فقط آیکون */
  .btn { padding: 8px 11px; }

  /* سایدبار به‌صورت drawer از راست */
  .layout { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    inset-block: 0;
    inset-inline-end: 0;
    width: min(320px, 85vw);
    z-index: 80;
    transform: translateX(100%); /* RTL: بیرون از دید در سمت راست */
    transition: transform .25s ease;
    box-shadow: -8px 0 30px rgba(0,0,0,.45);
  }
  [dir="rtl"] .sidebar { transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0) !important; }

  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.5);
    z-index: 75;
  }

  /* تولبار فشرده: دکمه‌ی اصلی + آیکون ⋯ */
  #btn-file-actions { display: inline-flex; }
  .toolbar-actions { display: none; } /* اقدامات به bottom sheet منتقل می‌شود */
  .toolbar { padding: 6px 10px; gap: 6px; }
  .current-file { font-size: 11.5px; }

  /* حالت ویرایش: فقط ادیتور (بدون پیش‌نمایش کنار) */
  .editor-grid { grid-template-columns: 1fr; }
  #preview { display: none; }
  #editor { padding: 14px; font-size: 15px; }

  .read-pane { padding: 18px 16px; font-size: 15px; }

  /* مودال‌ها فول‌اسکرین‌مانند */
  .modal { max-height: 88vh; border-radius: 16px 16px 0 0; width: 100%; align-self: flex-end; }
  .modal-backdrop { align-items: flex-end; padding: 0; }

  .search-results { top: auto; position: fixed; inset-inline: 8px; bottom: 10px; max-height: 50vh; z-index: 85; }
}

/* دسکتاپ: منوی بیشتر به‌صورت dropdown معمولی (بالا تعریف شده) */
@media (min-width: 769px) {
  .sheet-backdrop { display: none !important; } /* bottom sheet فقط موبایل */
}

/* ---------------- پنل مدیریت ---------------- */
.admin-layout { display: block; }

.admin-new-user {
  display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 8px;
}
.admin-new-user input, .admin-new-user select {
  padding: 8px 10px; border: 1px solid var(--border, #ccc); border-radius: 8px;
  background: var(--bg, #fff); color: inherit; font: inherit;
}
.admin-new-user input { flex: 1 1 160px; min-width: 0; }

.admin-table { width: 100%; border-collapse: collapse; margin-top: 12px; }
.admin-table th, .admin-table td {
  text-align: right; padding: 8px 10px; border-bottom: 1px solid var(--border, #ddd);
  white-space: nowrap;
}
.admin-table th { font-size: 13px; opacity: .7; }
.admin-actions { display: flex; gap: 4px; }

.role-badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  font-size: 12px; border: 1px solid var(--border, #ccc);
}
.role-badge.admin { border-color: var(--accent, #3b82f6); color: var(--accent, #3b82f6); }
.role-badge.warn { border-color: #f59e0b; color: #f59e0b; }
