:root {
  --bg: #f4f6fb; --surface: #ffffff; --surface-2: #f8f9fc; --line: #e6e9f0; --text: #141824; --muted: #6b7385;
  --pri: #4f46e5; --pri-soft: #eef0ff; --ok: #059669; --ok-soft: #e7f8f1; --warn: #d97706; --warn-soft: #fff6e6;
  --bad: #e11d48; --bad-soft: #ffeef2; --shadow: 0 1px 2px rgb(16 24 40 / .05), 0 4px 16px rgb(16 24 40 / .06);
  --radius: 14px; --nav-w: 232px;
}
:root[data-theme="dark"] {
  --bg: #0d1017; --surface: #161a23; --surface-2: #1c212c; --line: #262c39; --text: #e8ebf2; --muted: #8b93a7;
  --pri: #818cf8; --pri-soft: #1f2340; --ok: #34d399; --ok-soft: #0f2a22; --warn: #fbbf24; --warn-soft: #2d2412;
  --bad: #fb7185; --bad-soft: #301820; --shadow: 0 1px 2px rgb(0 0 0 / .3), 0 6px 20px rgb(0 0 0 / .25);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1017; --surface: #161a23; --surface-2: #1c212c; --line: #262c39; --text: #e8ebf2; --muted: #8b93a7;
    --pri: #818cf8; --pri-soft: #1f2340; --ok: #34d399; --ok-soft: #0f2a22; --warn: #fbbf24; --warn-soft: #2d2412;
    --bad: #fb7185; --bad-soft: #301820; --shadow: 0 1px 2px rgb(0 0 0 / .3), 0 6px 20px rgb(0 0 0 / .25);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { font-family: Vazirmatn, Tahoma, system-ui, sans-serif; background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.6; -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: var(--nav-w) 1fr; min-height: 100vh; }
.nav { position: sticky; top: 0; height: 100vh; background: var(--surface); border-inline-end: 1px solid var(--line); display: flex; flex-direction: column; padding: 18px 12px; gap: 18px; }
.brand { display: flex; align-items: center; gap: 10px; padding: 0 8px; }
.brand .logo { width: 38px; height: 38px; border-radius: 11px; display: grid; place-items: center; background: linear-gradient(135deg, var(--pri), #06b6d4); color: #fff; font-size: 20px; }
.brand b { display: block; font-size: 15px; }
.brand small { color: var(--muted); font-size: 12px; }
.nav nav { display: flex; flex-direction: column; gap: 4px; }
.nav nav button { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border: 0; background: none; border-radius: 10px; cursor: pointer; color: var(--muted); text-align: start; transition: background .15s, color .15s; }
.nav nav button:hover { background: var(--surface-2); color: var(--text); }
.nav nav button.active { background: var(--pri-soft); color: var(--pri); font-weight: 600; }
.nav nav i { font-style: normal; width: 22px; text-align: center; }
.nav nav span { flex: 1; }
.nav nav em { font-style: normal; font-size: 11px; font-weight: 700; background: var(--pri); color: #fff; border-radius: 999px; padding: 0 7px; min-width: 20px; text-align: center; }
.nav nav em:empty { display: none; }
.nav-foot { margin-top: auto; display: flex; align-items: center; justify-content: space-between; padding: 0 8px; }
.live { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.live .dot { width: 8px; height: 8px; border-radius: 50%; background: #9ca3af; }
.live.on .dot { background: #10b981; box-shadow: 0 0 0 0 rgb(16 185 129 / .6); animation: pulse 2s infinite; }
@keyframes pulse { 70% { box-shadow: 0 0 0 8px rgb(16 185 129 / 0); } 100% { box-shadow: 0 0 0 0 rgb(16 185 129 / 0); } }
.icon-btn { border: 1px solid var(--line); background: var(--surface-2); border-radius: 9px; width: 34px; height: 34px; cursor: pointer; }

main { padding: 24px clamp(14px, 3vw, 36px) 40px; width: 100%; }
.view-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.view-head h1 { margin: 0; font-size: 22px; }

/* ---------- progress ---------- */
.progress { display: flex; align-items: center; gap: 12px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px; margin-bottom: 16px; font-size: 13px; color: var(--muted); box-shadow: var(--shadow); }
.progress-bar { flex: 0 0 120px; height: 6px; border-radius: 99px; background: var(--surface-2); overflow: hidden; position: relative; }
.progress-bar span { position: absolute; inset-block: 0; width: 40%; border-radius: 99px; background: var(--pri); animation: slide 1.2s ease-in-out infinite; }
@keyframes slide { from { inset-inline-start: -40%; } to { inset-inline-start: 100%; } }

/* ---------- stats ---------- */
.stats { display: flex; gap: 10px; flex-wrap: wrap; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 8px 14px; min-width: 104px; box-shadow: var(--shadow); }
.stat b { display: block; font-size: 20px; line-height: 1.3; }
.stat small { color: var(--muted); font-size: 12px; }
.stat.accent b { color: var(--pri); }
.stat.ok b { color: var(--ok); }

/* ---------- toolbar ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 14px; }
select, input, textarea { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; outline: none; transition: border-color .15s, box-shadow .15s; }
select:focus, input:focus, textarea:focus { border-color: var(--pri); box-shadow: 0 0 0 3px var(--pri-soft); }
.search { flex: 1; min-width: 180px; display: flex; align-items: center; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding-inline-start: 10px; }
.search:focus-within { border-color: var(--pri); box-shadow: 0 0 0 3px var(--pri-soft); }
.search input { border: 0; box-shadow: none !important; flex: 1; background: transparent; }
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 3px; }
.seg button { border: 0; background: none; padding: 5px 12px; border-radius: 8px; cursor: pointer; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: var(--shadow); }

.btn { border: 1px solid transparent; border-radius: 10px; padding: 8px 14px; cursor: pointer; font-weight: 500; white-space: nowrap; transition: transform .05s, background .15s, border-color .15s; }
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--pri); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: var(--surface); border-color: var(--line); }
.btn.ghost:hover { border-color: var(--pri); color: var(--pri); }
.btn.sm { padding: 5px 10px; font-size: 13px; border-radius: 8px; }
.btn.ok { background: var(--ok-soft); color: var(--ok); }
.btn.danger { background: var(--bad-soft); color: var(--bad); }
.btn:disabled { opacity: .45; cursor: default; }

.fresh { display: block; margin: 0 auto 12px; border: 0; background: var(--pri); color: #fff; border-radius: 999px; padding: 6px 16px; cursor: pointer; box-shadow: var(--shadow); animation: pop .25s ease-out; }
@keyframes pop { from { transform: translateY(-6px); opacity: 0; } }

/* ---------- cards ---------- */
.list { display: flex; flex-direction: column; gap: 12px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); position: relative; transition: border-color .2s, opacity .2s; }
.card.new::before { content: ''; position: absolute; inset-block: 14px; inset-inline-start: 0; width: 4px; border-radius: 0 4px 4px 0; background: var(--pri); }
.card.ignored { opacity: .55; }
.card.flash { animation: flash 1.6s ease-out; }
@keyframes flash { 0% { border-color: var(--pri); box-shadow: 0 0 0 4px var(--pri-soft); } 100% { border-color: var(--line); } }
.card-top { display: flex; gap: 12px; align-items: flex-start; justify-content: space-between; }
.title { font-weight: 600; font-size: 15px; line-height: 1.8; margin: 0; white-space: pre-wrap; word-break: break-word; color: inherit; text-decoration: none; }
a.title:hover { color: var(--pri); }
.meta { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 8px; color: var(--muted); font-size: 13px; }
.meta b { color: var(--text); font-weight: 500; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.chip { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; padding: 2px 10px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--line); color: var(--muted); }
.chip.filter { background: var(--pri-soft); color: var(--pri); border-color: transparent; }
.chip.item { color: var(--text); }
.pill { font-size: 12px; font-weight: 600; padding: 2px 10px; border-radius: 999px; white-space: nowrap; }
.pill.new { background: var(--pri-soft); color: var(--pri); }
.pill.seen { background: var(--surface-2); color: var(--muted); }
.pill.sent { background: var(--ok-soft); color: var(--ok); }
.pill.ignored { background: var(--surface-2); color: var(--muted); }
.deadline { font-weight: 600; }
.deadline.urgent { color: var(--bad); }
.deadline.soon { color: var(--warn); }
.actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.panel { margin-top: 12px; padding-top: 12px; border-top: 1px dashed var(--line); }
.panel textarea { width: 100%; min-height: 90px; resize: vertical; }
.panel table { width: 100%; border-collapse: collapse; font-size: 13px; margin: 8px 0; }
.panel th, .panel td { border-bottom: 1px solid var(--line); padding: 6px 8px; text-align: start; }
.panel th { color: var(--muted); font-weight: 500; }
.panel .note { color: var(--muted); font-size: 12px; }
.empty { text-align: center; color: var(--muted); padding: 48px 16px; background: var(--surface); border: 1px dashed var(--line); border-radius: var(--radius); }
.skeleton { height: 120px; border-radius: var(--radius); background: linear-gradient(90deg, var(--surface) 0%, var(--surface-2) 50%, var(--surface) 100%); background-size: 200% 100%; animation: shimmer 1.2s infinite; border: 1px solid var(--line); }
@keyframes shimmer { to { background-position: -200% 0; } }
.pager { display: flex; justify-content: center; align-items: center; gap: 10px; margin-top: 18px; color: var(--muted); font-size: 13px; }
.hint { color: var(--muted); margin: -6px 0 14px; font-size: 13px; }
.err { color: var(--bad); }

/* ---------- forms ---------- */
.split { display: grid; grid-template-columns: minmax(300px, 420px) 1fr; gap: 16px; align-items: start; }
.form { display: flex; flex-direction: column; gap: 12px; position: sticky; top: 20px; }
.form h2 { margin: 0; font-size: 16px; }
.form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 500; }
.form label small { color: var(--muted); font-weight: 400; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-items: end; }
.row-btns { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 4px; }
.switch { flex-direction: row !important; align-items: center; gap: 8px !important; cursor: pointer; padding-bottom: 8px; }
.switch input { display: none; }
.switch span { width: 38px; height: 22px; border-radius: 99px; background: var(--line); position: relative; transition: background .2s; }
.switch span::after { content: ''; position: absolute; top: 3px; inset-inline-start: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: inset-inline-start .2s; }
.switch input:checked + span { background: var(--pri); }
.switch input:checked + span::after { inset-inline-start: 19px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: 13px; margin-top: 10px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-weight: 500; }
.card h3 { margin: 0; font-size: 15px; display: flex; align-items: center; gap: 8px; }

/* ---------- toasts ---------- */
.toasts { position: fixed; inset-block-end: 20px; inset-inline-start: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 50; }
.toast { background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; box-shadow: var(--shadow); font-size: 13px; animation: pop .25s ease-out; max-width: 340px; }
.toast.ok { background: var(--ok); color: #fff; }

/* ---------- responsive ---------- */
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .nav { position: fixed; inset: auto 0 0 0; height: auto; flex-direction: row; padding: 6px 8px calc(6px + env(safe-area-inset-bottom)); border-inline-end: 0; border-top: 1px solid var(--line); z-index: 40; gap: 0; }
  .brand, .nav-foot .live span:last-child { display: none; }
  .nav nav { flex-direction: row; flex: 1; justify-content: space-around; }
  .nav nav button { flex-direction: column; gap: 0; padding: 6px 8px; font-size: 11px; position: relative; flex: 1; align-items: center; }
  .nav nav i { font-size: 18px; }
  .nav nav em { position: absolute; top: 0; inset-inline-end: calc(50% - 22px); font-size: 10px; }
  .nav-foot { margin: 0; flex-direction: column; gap: 4px; padding: 0 4px; }
  main { padding-bottom: 96px; }
  .split { grid-template-columns: 1fr; }
  .form { position: static; }
  .toasts { inset-block-end: 84px; inset-inline: 12px; }
  .toast { max-width: none; }
}
@media (max-width: 560px) {
  .view-head h1 { font-size: 18px; }
  .stat { min-width: 0; flex: 1; padding: 6px 10px; }
  .stat b { font-size: 17px; }
  .toolbar select { flex: 1; }
  .grid2 { grid-template-columns: 1fr; }
  .card { padding: 14px; }
}

.kv dd.err { overflow-wrap: anywhere; direction: ltr; text-align: right; }

@media (min-width: 1500px) {
  #orders, #webList { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); align-items: start; }
}

/* ---------- auth & roles ---------- */
.auth { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 16px;
  background: radial-gradient(1200px 600px at 80% -10%, color-mix(in srgb, var(--pri) 25%, transparent), transparent), var(--bg); }
.auth-card { width: min(380px, 100%); display: flex; flex-direction: column; gap: 12px; padding: 28px; }
.auth-card .brand { padding: 0 0 6px; }
.auth-card h2 { margin: 0; font-size: 18px; }
.auth-card .err { min-height: 1em; margin: 0; font-size: 13px; }
.auth-card .hint { margin: 0; }
body.locked .app { display: none; }
.me { display: flex; align-items: center; gap: 6px; padding: 10px 8px; border-top: 1px solid var(--line); margin-top: auto; }
.me span { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.me small { color: var(--muted); font-size: 11px; margin-inline-end: auto; }
.me + .nav-foot { margin-top: 8px; }
/* Hide what the signed-in role cannot do (the server enforces it regardless). */
body:not([data-role="admin"]) [data-need="admin"],
body[data-role="viewer"] [data-need="member"],
body[data-role="viewer"] :is([data-act="sent"], [data-act="seen"], [data-act="ignored"], [data-act="saveNote"], #markAllSeen, #crawlNow, #webNow),
body:not([data-role="admin"]) #filterList [data-act] { display: none !important; }
body[data-role="viewer"] [data-box="note"] textarea { pointer-events: none; opacity: .8; }
@media (max-width: 900px) {
  .me { border: 0; padding: 0; margin: 0; flex-direction: column; gap: 4px; }
  .me span, .me small { display: none; }
}
.auth-card label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; }
.auth-card label.hidden { display: none; }
.auth-card input { width: 100%; padding: 10px 12px; font-size: 14px; }
.auth-card .btn { padding: 11px; font-size: 14px; margin-top: 4px; }
