:root {
  --bg: #F6F4FB;
  --surface: #FFFFFF;
  --surface-alt: #F1ECFB;
  --ink: #241B36;
  --ink-muted: #6E6280;
  --primary: #5B2A9D;
  --primary-strong: #431F75;
  --primary-soft: #EDE4FB;
  --on-primary: #FFFFFF;
  --border: #E3DEF2;
  --good: #1F7A4D;
  --good-bg: #E4F3EA;
  --warn: #92600C;
  --warn-bg: #FBF0DC;
  --danger: #A5303B;
  --danger-bg: #FBE7E9;
  --shadow: 0 8px 30px rgba(66, 33, 110, 0.12);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15111D;
    --surface: #1E1829;
    --surface-alt: #282036;
    --ink: #ECE7F5;
    --ink-muted: #A79CB9;
    --primary: #A57CE8;
    --primary-strong: #C2A4F2;
    --primary-soft: #33264A;
    --on-primary: #16101F;
    --border: #342A44;
    --good: #6FD19F;
    --good-bg: #173327;
    --warn: #E9B764;
    --warn-bg: #3A2C14;
    --danger: #F08A94;
    --danger-bg: #3D1C21;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: "Vazirmatn", system-ui, -apple-system, sans-serif;
  font-size: 15px;
}
body { display: flex; min-height: 100vh; }

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input { font-family: inherit; font-size: inherit; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
svg { display: block; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--ink-muted); }

/* ---------------- Buttons & badges ---------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 16px; border: 1.5px solid transparent; border-radius: 9px;
  font-weight: 600; cursor: pointer; white-space: nowrap;
  transition: background .15s, border-color .15s, color .15s;
}
.btn-primary { background: var(--primary); color: var(--on-primary); }
.btn-primary:hover:not(:disabled) { background: var(--primary-strong); }
.btn-outline { background: var(--surface); color: var(--primary); border-color: var(--primary); }
.btn-outline:hover:not(:disabled) { background: var(--primary-soft); }
.btn-ghost { background: none; color: var(--ink-muted); border-color: var(--border); }
.btn-ghost:hover:not(:disabled) { color: var(--primary); border-color: var(--primary); }
.btn-sm { padding: 5px 11px; font-size: .85rem; border-radius: 7px; }
.btn-block { width: 100%; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: .78rem; font-weight: 600; white-space: nowrap; }
.badge-good { background: var(--good-bg); color: var(--good); }
.badge-warn { background: var(--warn-bg); color: var(--warn); }
.badge-muted { background: var(--surface-alt); color: var(--ink-muted); }
.badge-danger { background: var(--danger-bg); color: var(--danger); }

.spinner {
  width: 14px; height: 14px; border: 2.5px solid var(--border); border-top-color: var(--primary);
  border-radius: 50%; animation: spin .7s linear infinite; display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } }

/* ---------------- Login ---------------- */
#loginView {
  margin: auto; width: calc(100% - 32px); max-width: 720px;
  display: grid; grid-template-columns: 1fr 1fr;
  border-radius: 16px; overflow: hidden; box-shadow: var(--shadow);
}
#loginView[hidden] { display: none; }
.brand-panel {
  background: linear-gradient(165deg, #5B2A9D 0%, #431F75 100%); color: #fff;
  padding: 40px 30px; display: flex; flex-direction: column; justify-content: space-between; gap: 32px;
}
.brand-panel h1 { font-size: 1.4rem; line-height: 1.6; margin: 0; }
.brand-panel p { color: rgba(255, 255, 255, .8); line-height: 1.8; margin: 12px 0 0; }
.brand-mark { display: flex; align-items: center; gap: 9px; font-weight: 700; color: inherit; }
.brand-mark svg { width: 22px; height: 22px; }
.brand-mark:hover { text-decoration: none; }
.login-form { background: var(--surface); padding: 40px 30px; display: flex; flex-direction: column; justify-content: center; }
.login-form h2 { margin: 0 0 4px; }
.login-form .sub { color: var(--ink-muted); margin: 0 0 22px; font-size: .9rem; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: .82rem; font-weight: 600; color: var(--ink-muted); margin-bottom: 6px; }
.field input {
  width: 100%; padding: 10px 12px; border: 1.5px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--ink);
}
.field input:focus { border-color: var(--primary); background: var(--surface); outline: none; }
.error-text { color: var(--danger); font-size: .85rem; margin: 10px 0 0; min-height: 1.2em; }

/* ---------------- App shell ---------------- */
#appView { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#appView[hidden] { display: none; }
.app-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 24px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.app-header .brand-mark { color: var(--primary); }
.user-chip { display: flex; align-items: center; gap: 12px; }
.user-role { font-size: .78rem; font-weight: 700; color: var(--primary); background: var(--primary-soft); padding: 3px 10px; border-radius: 999px; }
.notice { background: var(--warn-bg); color: var(--warn); padding: 9px 24px; font-size: .88rem; }
.page { width: 100%; max-width: 1100px; margin: 0 auto; padding: 26px 24px 60px; }

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head h1 { margin: 0; font-size: 1.35rem; }
.back-link { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; margin-bottom: 10px; }
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.search {
  width: 100%; max-width: 360px; padding: 10px 14px; border: 1.5px solid var(--border);
  border-radius: 9px; background: var(--surface); color: var(--ink);
}
.search:focus { border-color: var(--primary); outline: none; }
.status-line { color: var(--ink-muted); font-size: .88rem; min-height: 1.4em; margin: 0 0 12px; }

/* ---------------- Tables ---------------- */
.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: right; padding: 12px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
th { font-size: .8rem; font-weight: 700; color: var(--ink-muted); white-space: nowrap; background: var(--surface-alt); }
.sort-button {
  all: unset; cursor: pointer; display: inline-flex; align-items: center; gap: 5px;
  font-size: inherit; font-weight: inherit; color: inherit;
}
.sort-button:focus-visible { outline: 2px solid var(--primary); outline-offset: 3px; border-radius: 3px; }
.sort-button:hover, th[aria-sort="ascending"] .sort-button, th[aria-sort="descending"] .sort-button { color: var(--primary); }
.sort-arrow { font-size: .7rem; opacity: .35; }
th[aria-sort="ascending"] .sort-arrow, th[aria-sort="descending"] .sort-arrow { opacity: 1; }

tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-alt); }
td.price { font-weight: 700; white-space: nowrap; }
td.price .unit { font-weight: 400; color: var(--ink-muted); font-size: .8rem; }
td.time { color: var(--ink-muted); font-size: .85rem; white-space: nowrap; }
.row-error { color: var(--danger); font-size: .8rem; margin-top: 4px; max-width: 30ch; }
.cell-note { font-size: .78rem; color: var(--ink-muted); margin-top: 3px; }
tr.busy td { opacity: .7; }
.empty { padding: 40px 20px; text-align: center; color: var(--ink-muted); }

@media (max-width: 720px) {
  #loginView { grid-template-columns: 1fr; }
  .page { padding: 18px 14px 50px; }
  th, td { padding: 10px 9px; }
}

.product-actions { margin-bottom: 12px; }
input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--primary); cursor: pointer; }

/* ---------------- Level badges ---------------- */
.badge-level2 { background: color-mix(in srgb, var(--primary) 12%, transparent); color: var(--primary); }
.level-cell { min-width: 140px; }
.site-names { margin-top: 4px; font-size: .75rem; color: var(--ink-muted); line-height: 1.6; }
#siteSearch { margin-bottom: 12px; }

/* ---------------- Administration ---------------- */
.admin-section { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 20px; margin-bottom: 24px; }
.admin-section h2 { margin-top: 0; font-size: 1.15rem; }
.create-user-form { display: flex; gap: 16px; align-items: end; flex-wrap: wrap; }
.create-user-form .field { flex: 1; min-width: 180px; margin: 0; }
.ai-permission { border: 0; margin: 0; padding: 4px 0; }
.ai-permission legend { font-size: .82rem; color: var(--ink-muted); }
.ai-permission label { display: inline-flex; align-items: center; gap: 4px; margin-inline-end: 12px; cursor: pointer; }
input[type="radio"] { width: 17px; height: 17px; accent-color: var(--primary); }
.admin-user { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; padding: 14px 0; border-bottom: 1px solid var(--border); }
.admin-user:last-child { border-bottom: 0; }
.site-board { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.site-level { min-width: 0; }
.site-level h3 { font-size: 1rem; }
.site-dropzone { min-height: 160px; height: calc(100% - 50px); padding: 10px; border: 2px dashed var(--border); border-radius: 10px; background: var(--bg); }
.site-card { padding: 12px; margin-bottom: 10px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; }
.site-card[draggable="true"] { cursor: grab; }
.site-card-title { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.drag-handle { color: var(--ink-muted); font-size: 1.4rem; }
.dragging { opacity: .45; }
.drop-active { border-color: var(--primary); background: var(--primary-soft); }
.drop-before { border-top: 3px solid var(--primary); }
.drop-after { border-bottom: 3px solid var(--primary); }
.empty-drop { text-align: center; color: var(--ink-muted); padding: 30px 0; }
@media (max-width: 720px) {
  .site-board { grid-template-columns: 1fr; }
  .admin-section { padding: 14px; }
  .app-header { padding: 12px; gap: 8px; flex-wrap: wrap; }
}
