/* پنل مدیریت ایمیل — طراحی RTL، واکنش‌گرا، روشن/تاریک، بدون هیچ وابستگی بیرونی */

:root {
  --bg: #f3f4f7;
  --panel: #ffffff;
  --panel-2: #fafbfc;
  --panel-3: #f1f3f7;
  --text: #14161c;
  --muted: #6b7280;
  --faint: #98a1b0;
  --border: #e4e7ee;
  --border-strong: #d3d8e2;
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-soft: #eef0ff;
  --accent-text: #ffffff;
  --ok: #12855b;
  --ok-soft: #e6f6ee;
  --warn: #9a6700;
  --warn-soft: #fff5e0;
  --bad: #c02626;
  --bad-soft: #fdeaea;
  --info: #1f6feb;
  --info-soft: #e8f0fe;
  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 10px 28px rgba(16, 24, 40, 0.07);
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.07);
  --sidebar-w: 268px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #0d0f13;
    --panel: #171a21;
    --panel-2: #1c1f27;
    --panel-3: #21252e;
    --text: #e9ecf2;
    --muted: #9aa3b2;
    --faint: #78828f;
    --border: #272c36;
    --border-strong: #333a46;
    --accent: #7c78ff;
    --accent-hover: #8b88ff;
    --accent-soft: #23223a;
    --accent-text: #0d0f13;
    --ok: #4ad295;
    --ok-soft: #14291f;
    --warn: #e2b04a;
    --warn-soft: #2c2513;
    --bad: #f08a8a;
    --bad-soft: #2e1717;
    --info: #7cb0ff;
    --info-soft: #16233a;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 12px 30px rgba(0, 0, 0, 0.35);
    color-scheme: dark;
  }
}

html[data-theme='dark'] {
  --bg: #0d0f13;
  --panel: #171a21;
  --panel-2: #1c1f27;
  --panel-3: #21252e;
  --text: #e9ecf2;
  --muted: #9aa3b2;
  --faint: #78828f;
  --border: #272c36;
  --border-strong: #333a46;
  --accent: #7c78ff;
  --accent-hover: #8b88ff;
  --accent-soft: #23223a;
  --accent-text: #0d0f13;
  --ok: #4ad295;
  --ok-soft: #14291f;
  --warn: #e2b04a;
  --warn-soft: #2c2513;
  --bad: #f08a8a;
  --bad-soft: #2e1717;
  --info: #7cb0ff;
  --info-soft: #16233a;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 12px 30px rgba(0, 0, 0, 0.35);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Vazirmatn', 'IRANSans', 'Segoe UI', Tahoma, system-ui, sans-serif;
  font-size: 14.5px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2, h3, h4 { margin: 0 0 8px; line-height: 1.4; font-weight: 700; }
h1 { font-size: 19px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
p { margin: 0 0 10px; }

code, pre, .mono {
  font-family: 'JetBrains Mono', 'Cascadia Mono', Consolas, 'Courier New', monospace;
  font-size: 12.5px;
  direction: ltr;
  text-align: left;
}

code {
  background: var(--panel-3);
  padding: 1px 6px;
  border-radius: 6px;
}

[hidden] { display: none !important; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------------------------- بارگذاری اولیه ---------------------------- */

.boot {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: var(--bg);
  z-index: 50;
}

.boot-box { text-align: center; color: var(--muted); }

.spinner {
  width: 34px;
  height: 34px;
  margin: 0 auto 12px;
  border: 3px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ------------------------------ ورود ------------------------------ */

.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(1000px 480px at 50% -10%, var(--accent-soft), transparent 70%);
}

.login-card {
  width: 100%;
  max-width: 404px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 28px 26px 22px;
  box-shadow: var(--shadow);
}

.login-logo {
  width: 54px;
  height: 54px;
  border-radius: 15px;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 26px;
  margin: 0 auto 14px;
}

.login-card h1 { text-align: center; margin-bottom: 2px; }
.login-card .sub { text-align: center; color: var(--muted); font-size: 13px; margin-bottom: 20px; }
.login-foot { margin-top: 16px; font-size: 12px; color: var(--faint); text-align: center; }
.otp-code { font-size: 22px; letter-spacing: 8px; text-align: center; }
button.link { background: none; border: none; padding: 0; color: var(--accent); font: inherit; font-size: 12px; cursor: pointer; }
button.link:hover { text-decoration: underline; }
.card.flash { animation: card-flash 2.4s ease; border-color: var(--accent); }
@keyframes card-flash {
  0%, 100% { box-shadow: var(--shadow); }
  20%, 60% { box-shadow: 0 0 0 3px var(--accent-soft), var(--shadow); }
}

/* ------------------------------ چیدمان ------------------------------ */

.layout {
  display: flex;
  min-height: 100vh;
  align-items: stretch;
}

.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--panel);
  border-inline-start: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 14px 12px 16px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 6px 12px;
}

.brand-mark {
  width: 36px;
  height: 36px;
  border-radius: 11px;
  background: var(--accent);
  color: var(--accent-text);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 17px;
  flex: 0 0 auto;
}

.brand-text { min-width: 0; }
.brand-title { font-weight: 700; font-size: 15px; }
.brand-sub { font-size: 11.5px; color: var(--faint); direction: ltr; text-align: right; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.nav { display: flex; flex-direction: column; gap: 2px; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 11px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text);
  font: inherit;
  text-align: right;
  cursor: pointer;
  transition: background 0.12s ease;
}

.nav-item:hover { background: var(--panel-3); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-item .ico { width: 20px; text-align: center; flex: 0 0 20px; font-size: 15px; }
.nav-item .label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.nav-sep {
  height: 1px;
  background: var(--border);
  margin: 10px 6px;
}

.nav-title {
  font-size: 11px;
  color: var(--faint);
  padding: 6px 11px 4px;
  letter-spacing: 0.02em;
}

.side-foot { margin-top: auto; padding-top: 12px; }

.me-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--panel-2);
}

.avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  display: grid;
  place-items: center;
  font-weight: 700;
  flex: 0 0 34px;
}

.me-text { min-width: 0; flex: 1; }
.me-address { font-size: 12.5px; direction: ltr; text-align: right; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.me-role { font-size: 11px; color: var(--faint); }

.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}

.view {
  padding: 16px;
  max-width: 1180px;
  width: 100%;
  margin: 0 auto;
  flex: 1;
  min-width: 0;
}

.hamburger { display: none; }

/* ------------------------------ اجزا ------------------------------ */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 8px 13px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--panel);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.12s ease, border-color 0.12s ease;
}

.btn:hover { background: var(--panel-3); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, var(--border-strong)); }
.btn.danger:hover { background: var(--bad-soft); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--panel-3); }
.btn.sm { padding: 5px 9px; font-size: 12.5px; }
.btn.block { width: 100%; }
.btn.icon { padding: 7px 9px; }

input, select, textarea {
  font: inherit;
  padding: 8px 11px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--panel);
  color: var(--text);
  width: 100%;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}

textarea { resize: vertical; min-height: 90px; line-height: 1.8; }

label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 5px; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.row.tight { gap: 6px; }
.grow { flex: 1; min-width: 0; }
.spacer { flex: 1; }
.center { text-align: center; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.small { font-size: 12.5px; }
.mt { margin-top: 12px; }
.mb { margin-bottom: 12px; }

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 16px;
  margin-bottom: 14px;
}

.card-head {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: -4px 0 12px;
}

.card-head h2, .card-head h3 { margin: 0; }
.card-head .spacer { flex: 1; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  background: var(--panel-3);
  color: var(--muted);
  white-space: nowrap;
}

.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.info { background: var(--info-soft); color: var(--info); }
.badge.accent { background: var(--accent-soft); color: var(--accent); }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(148px, 1fr)); gap: 12px; }

.kpi {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 13px 15px;
}

.kpi .k-label { font-size: 12px; color: var(--muted); }
.kpi .k-value { font-size: 24px; font-weight: 700; line-height: 1.3; }
.kpi .k-hint { font-size: 11.5px; color: var(--faint); }

/* --------------------------- فهرست پیام‌ها --------------------------- */

.maillist { display: flex; flex-direction: column; }

.mail-row {
  display: grid;
  grid-template-columns: 22px 32px minmax(110px, 180px) 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  background: transparent;
  border-inline: 0;
  border-top: 0;
  color: inherit;
  font: inherit;
  text-align: right;
  width: 100%;
}

.mail-row:hover { background: var(--panel-2); }
.mail-row:last-child { border-bottom: 0; }
.mail-row.unread { background: var(--accent-soft); }
.mail-row.unread:hover { background: color-mix(in srgb, var(--accent-soft) 70%, var(--panel-3)); }
.mail-row .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--accent); display: block; margin: auto; }
.mail-row .dot.hidden { background: transparent; }
.mail-from { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mail-main { min-width: 0; }
.mail-subject { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mail-preview { font-size: 12.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mail-side { display: flex; align-items: center; gap: 8px; color: var(--faint); font-size: 12px; }
.mail-flags { display: flex; gap: 5px; }

.empty {
  padding: 46px 20px;
  text-align: center;
  color: var(--muted);
}
.empty .big { font-size: 34px; margin-bottom: 8px; }

/* ----------------------------- خواننده ----------------------------- */

.reader-head { border-bottom: 1px solid var(--border); padding-bottom: 12px; margin-bottom: 12px; }
.reader-subject { font-size: 18px; font-weight: 700; margin-bottom: 8px; }
.reader-meta { display: grid; gap: 3px; font-size: 13px; color: var(--muted); }
.reader-meta b { color: var(--text); font-weight: 600; }

.avatar-sm {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  display: grid;
  place-items: center;
  font-weight: 700;
  flex: 0 0 40px;
}

/* ------------------------------ آواتار ------------------------------ */

.avatar-img {
  position: relative;
  border-radius: 50%;
  overflow: hidden;
  background: hsl(var(--ah, 222) 45% 55%);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 700;
  flex: none;
}
.avatar-img img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.avatar-img .avatar-fallback { position: relative; line-height: 1; }
.avatar-xs { width: 30px; height: 30px; font-size: 13px; }
.avatar-me { width: 34px; height: 34px; font-size: 14px; }
button.avatar-click { cursor: pointer; border: none; padding: 0; }
.avatar-preview {
  width: 96px;
  height: 96px;
  border-radius: 50%;
  object-fit: cover;
  display: block;
  margin: 6px auto 12px;
  background: var(--panel-3);
}

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 12px; flex-wrap: wrap; }

.tab {
  padding: 7px 13px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}

.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

.mail-frame {
  width: 100%;
  height: 60vh;
  min-height: 320px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: #fff;
}

.body-text {
  white-space: pre-wrap;
  word-break: break-word;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px;
  max-height: 60vh;
  overflow: auto;
  font-size: 14px;
}

.headers-table { width: 100%; border-collapse: collapse; }
.headers-table th, .headers-table td { text-align: right; padding: 6px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
.headers-table th { color: var(--muted); font-weight: 600; width: 170px; font-size: 12.5px; }
.headers-table td { font-size: 12.5px; word-break: break-word; }

.attach-list { display: flex; flex-wrap: wrap; gap: 8px; }
.attach {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  border-radius: 999px;
  padding: 5px 12px 5px 8px;
  font-size: 12.5px;
}
.attach .size { color: var(--faint); }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--panel-2);
  font-size: 12.5px;
}
.chip button { border: 0; background: transparent; color: var(--bad); cursor: pointer; font-size: 14px; line-height: 1; }

/* ------------------------------ جدول ------------------------------ */

.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius-sm); }

table.data { width: 100%; border-collapse: collapse; font-size: 13px; min-width: 640px; }
table.data th, table.data td { padding: 9px 11px; text-align: right; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.data th { background: var(--panel-2); color: var(--muted); font-weight: 600; font-size: 12.5px; }
table.data tr:last-child td { border-bottom: 0; }
table.data td.wrap { white-space: normal; word-break: break-word; }
table.data tbody tr:hover { background: var(--panel-2); }

/* ------------------------------ توست ------------------------------ */

.toasts {
  position: fixed;
  bottom: 16px;
  left: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 60;
  max-width: min(420px, calc(100vw - 32px));
}

.toast {
  background: var(--panel);
  border: 1px solid var(--border);
  border-inline-start: 4px solid var(--accent);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  padding: 10px 13px;
  font-size: 13px;
  animation: slide-in 0.16s ease-out;
}

.toast.ok { border-inline-start-color: var(--ok); }
.toast.bad { border-inline-start-color: var(--bad); }
.toast.warn { border-inline-start-color: var(--warn); }

@keyframes slide-in { from { transform: translateY(8px); opacity: 0; } }

/* ------------------------------ مودال ------------------------------ */

.modal {
  position: fixed;
  inset: 0;
  z-index: 70;
  background: rgba(10, 12, 16, 0.5);
  display: grid;
  place-items: center;
  padding: 20px;
}

.modal-box {
  background: var(--panel);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 520px;
  padding: 20px;
  max-height: 90vh;
  overflow: auto;
}

/* --------------------------- حالت موبایل --------------------------- */

.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(10, 12, 16, 0.45);
  z-index: 19;
  border: 0;
}

@media (max-width: 900px) {
  .hamburger { display: inline-flex; }

  .sidebar {
    position: fixed;
    inset-block: 0;
    inset-inline-end: 0;
    z-index: 20;
    transform: translateX(-100%);
    transition: transform 0.18s ease;
    box-shadow: var(--shadow);
  }

  html[dir='rtl'] .sidebar { transform: translateX(100%); }
  .sidebar.open, html[dir='rtl'] .sidebar.open { transform: translateX(0); }

  .mail-row { grid-template-columns: 18px 1fr auto; }
  .mail-row .mail-from { font-size: 13px; }
  .mail-row .mail-preview { display: none; }
  .mail-row .avatar-xs { display: none; }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .view { padding: 12px; }
  .topbar { padding: 8px 12px; }
}

@media print {
  .sidebar, .topbar, .toasts { display: none !important; }
}
