:root {
  --font-sans: "IRANSans", "Vazirmatn", "Tahoma", system-ui, sans-serif;
  --bg: #f7f8fa;
  --surface: #ffffff;
  --surface-subtle: #fafbfc;
  --text: #17191e;
  --muted: #747b86;
  --border: #e5e8ed;
  --border-strong: #d6dbe2;
  --accent: #2563eb;
  --accent-soft: #eff5ff;
  --accent-contrast: #ffffff;
  --success: #147a48;
  --success-bg: #eefaf3;
  --warning: #9a6700;
  --warning-bg: #fff8e6;
  --danger: #b42318;
  --danger-bg: #fff2f0;
  --info: #175cd3;
  --info-bg: #eff6ff;
  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 10px;
  --bottom-nav-height: 72px;
  --sidebar-width: 252px;
  --content-width: 1240px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
}

* { box-sizing: border-box; }
html { direction: rtl; background: var(--bg); color-scheme: light; }
body { margin: 0; color: var(--text); background: var(--bg); font-family: var(--font-sans); -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; font-size: 16px; }
button { color: inherit; }
a { color: inherit; text-decoration: none; }
svg { width: 1.15em; height: 1.15em; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
[dir="ltr"], .numeric-input, .money-value, .date-value, .money-cell { font-variant-numeric: tabular-nums; }

.app-shell { min-height: 100dvh; }
.app-main { min-width: 0; min-height: 100dvh; }
.page-container { width: min(100%, var(--content-width)); margin: 0 auto; padding: 18px 14px calc(var(--bottom-nav-height) + 28px + env(safe-area-inset-bottom)); }
.page-stack { display: grid; gap: 14px; }
.section-stack { display: grid; gap: 12px; }
.responsive-grid { display: grid; grid-template-columns: 1fr; gap: 12px; }
.form-grid { display: grid; grid-template-columns: 1fr; gap: 12px; }
.inline-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.toolbar { display: flex; flex-wrap: wrap; align-items: end; justify-content: space-between; gap: 10px; }
.toolbar-fields { display: grid; grid-template-columns: 1fr; gap: 10px; flex: 1 1 540px; }

.topbar { min-height: 62px; padding: 8px 14px; display: flex; align-items: center; justify-content: space-between; gap: 12px; border-bottom: 1px solid var(--border); background: rgba(255,255,255,.97); position: sticky; top: 0; z-index: 20; }
.topbar-title { min-width: 0; }
.topbar-title strong, .topbar-title small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-title strong { font-size: .95rem; }
.topbar-title small { color: var(--muted); margin-top: 2px; font-size: .72rem; }
.topbar-meta { display: flex; align-items: center; gap: 8px; }

.page-header { display: flex; gap: 12px; align-items: flex-start; justify-content: space-between; margin-bottom: 2px; }
.page-header h1 { margin: 0; font-size: 1.32rem; font-weight: 800; letter-spacing: -.02em; }
.page-header p { margin: 6px 0 0; color: var(--muted); font-size: .88rem; line-height: 1.8; }

.ui-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 16px; }
.ui-card > h2 { margin: 0 0 14px; font-size: 1rem; font-weight: 760; }
.stat-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.stat-card { min-width: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 15px; }
.stat-label { display: block; color: var(--muted); font-size: .78rem; }
.stat-value { display: block; margin-top: 7px; font-size: 1.35rem; font-weight: 800; overflow-wrap: anywhere; }
.stat-hint { display: block; margin-top: 6px; color: var(--muted); line-height: 1.65; }

.ui-button { min-height: 44px; display: inline-flex; align-items: center; justify-content: center; gap: 7px; border: 1px solid transparent; border-radius: var(--radius-md); padding: 0 16px; font-weight: 720; cursor: pointer; transition: background-color .15s ease, border-color .15s ease, color .15s ease; }
.ui-button.variant-primary { background: var(--accent); color: var(--accent-contrast); }
.ui-button.variant-primary:hover { background: #1d55c9; }
.ui-button.variant-secondary { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
.ui-button.variant-danger { background: var(--danger-bg); color: var(--danger); border-color: #f2c7c3; }
.ui-button.variant-ghost { background: transparent; color: var(--muted); border-color: transparent; }
.ui-button.is-full { width: 100%; }
.ui-button:disabled { opacity: .5; cursor: not-allowed; }

.field { display: grid; gap: 7px; font-size: .84rem; font-weight: 650; min-width: 0; }
.field > span:first-child { color: #343840; }
.ui-input, .ui-select, .ui-textarea { width: 100%; min-height: 48px; border: 1px solid var(--border-strong); border-radius: var(--radius-md); background: var(--surface); color: var(--text); padding: 0 12px; outline: none; }
.ui-select { appearance: auto; }
.ui-textarea { min-height: 92px; padding-top: 11px; padding-bottom: 11px; resize: vertical; line-height: 1.7; }
.ui-input:focus, .ui-select:focus, .ui-textarea:focus { border-color: #8fb2ff; box-shadow: 0 0 0 3px #edf3ff; }
.field-error { color: var(--danger); font-weight: 550; line-height: 1.6; }
.field-hint { color: var(--muted); font-weight: 450; line-height: 1.6; }
.form-stack { display: grid; gap: 13px; }
.check-field { display: flex; align-items: flex-start; gap: 9px; font-size: .84rem; cursor: pointer; }
.check-field input { width: 18px; height: 18px; margin-top: 2px; accent-color: var(--accent); }
.check-field strong, .check-field small { display: block; }
.check-field small { color: var(--muted); font-weight: 450; margin-top: 3px; line-height: 1.6; }
.ui-file-input { width: 100%; min-height: 48px; border: 1px dashed var(--border-strong); border-radius: var(--radius-md); background: var(--surface-subtle); padding: 9px 11px; }

.input-with-suffix { position: relative; }
.input-with-suffix .ui-input { padding-left: 66px; direction: ltr; text-align: left; }
.input-with-suffix > span { position: absolute; left: 12px; top: 50%; translate: 0 -50%; color: var(--muted); font-size: .74rem; pointer-events: none; }
.date-field { position: relative; }
.date-field .ui-input { padding-left: 46px; direction: ltr; text-align: left; }
.field-icon-button { position: absolute; left: 6px; top: 50%; translate: 0 -50%; width: 36px; height: 36px; display: grid; place-items: center; border: 0; border-radius: 10px; background: transparent; color: var(--muted); cursor: pointer; }
.field-icon-button:hover { background: var(--surface-subtle); color: var(--text); }

.ui-badge { display: inline-flex; align-items: center; min-height: 26px; padding: 0 9px; border: 1px solid var(--border); border-radius: 999px; font-size: .73rem; color: var(--muted); background: var(--surface-subtle); white-space: nowrap; }
.ui-badge.tone-success { color: var(--success); background: var(--success-bg); border-color: #bfe7cf; }
.ui-badge.tone-warning { color: var(--warning); background: var(--warning-bg); border-color: #ead8a8; }
.ui-badge.tone-danger { color: var(--danger); background: var(--danger-bg); border-color: #f0c5c1; }
.ui-badge.tone-info { color: var(--info); background: var(--info-bg); border-color: #c4d8fb; }
.ui-alert { display: grid; gap: 4px; border: 1px solid var(--border); border-radius: var(--radius-md); padding: 12px 13px; background: var(--surface); line-height: 1.75; font-size: .86rem; }
.ui-alert.tone-success { color: var(--success); background: var(--success-bg); border-color: #bfe7cf; }
.ui-alert.tone-danger { color: var(--danger); background: var(--danger-bg); border-color: #f0c5c1; }
.ui-alert.tone-warning { color: var(--warning); background: var(--warning-bg); border-color: #ead8a8; }
.ui-alert.tone-info { color: var(--info); background: var(--info-bg); border-color: #c4d8fb; }

.table-wrap { width: 100%; overflow-x: auto; overscroll-behavior-inline: contain; border: 1px solid var(--border); border-radius: var(--radius-md); background: var(--surface); }
.ui-table { width: 100%; border-collapse: collapse; min-width: 720px; font-size: .82rem; }
.ui-table th, .ui-table td { padding: 11px 12px; text-align: right; border-bottom: 1px solid var(--border); vertical-align: middle; white-space: nowrap; }
.ui-table th { color: var(--muted); font-weight: 650; background: var(--surface-subtle); position: sticky; top: 0; z-index: 1; }
.ui-table tbody tr:last-child td { border-bottom: 0; }
.ui-table .money-cell { direction: ltr; text-align: left; }
.ui-table .wrap-cell { white-space: normal; min-width: 220px; line-height: 1.7; }
.table-link { color: var(--accent); font-weight: 700; }

.list-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.list-row:last-child { border-bottom: 0; }
.meta-list { display: grid; gap: 0; }
.meta-list .list-row > span:first-child { color: var(--muted); }

.quick-links { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 10px; }
.quick-link { min-height: 82px; display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 13px; border: 1px solid var(--border); border-radius: var(--radius-md); background: var(--surface); }
.quick-link:hover { border-color: #b9c9e9; background: #fbfdff; }
.quick-link-main { display: flex; align-items: center; gap: 10px; min-width: 0; }
.quick-link-icon { width: 36px; height: 36px; display: grid; place-items: center; border-radius: 11px; background: var(--accent-soft); color: var(--accent); flex: 0 0 auto; }
.quick-link strong, .quick-link small { display: block; }
.quick-link small { color: var(--muted); margin-top: 3px; font-size: .72rem; }

.empty-state { min-height: 180px; display: grid; justify-items: center; align-content: center; gap: 8px; text-align: center; padding: 24px; border: 1px dashed var(--border-strong); border-radius: var(--radius-md); background: var(--surface-subtle); }
.empty-state p { margin: 0; color: var(--muted); max-width: 520px; line-height: 1.75; }
.empty-icon { width: 42px; height: 42px; display: grid; place-items: center; border-radius: 12px; background: var(--surface); border: 1px solid var(--border); color: var(--muted); }

.line-editor { display: grid; gap: 10px; }
.line-editor-row { display: grid; grid-template-columns: 1fr; gap: 10px; padding: 12px; border: 1px solid var(--border); border-radius: var(--radius-md); background: var(--surface-subtle); }
.summary-bar { display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: 8px; }
.summary-item { padding: 11px; border: 1px solid var(--border); border-radius: var(--radius-md); background: var(--surface); min-width: 0; }
.summary-item span, .summary-item strong { display: block; }
.summary-item span { color: var(--muted); font-size: .72rem; }
.summary-item strong { margin-top: 6px; font-size: .9rem; direction: ltr; text-align: left; overflow-wrap: anywhere; }
.summary-item.is-ok { border-color: #bfe7cf; background: var(--success-bg); color: var(--success); }
.summary-item.is-danger { border-color: #f0c5c1; background: var(--danger-bg); color: var(--danger); }

.muted, .helper-text { color: var(--muted); }
.helper-text { margin: 14px 0 0; font-size: .78rem; line-height: 1.8; }
.money-value { white-space: nowrap; }
code { direction: ltr; display: inline-block; background: #f2f4f7; padding: 4px 7px; border-radius: 8px; }

.brand-block { display: flex; align-items: center; gap: 11px; margin-bottom: 24px; }
.brand-symbol { width: 40px; height: 40px; display: grid; place-items: center; border-radius: 13px; background: var(--accent); color: white; font-weight: 900; }
.brand-block strong, .brand-block small { display: block; }
.brand-block small { color: var(--muted); margin-top: 2px; font-size: .73rem; }
.desktop-sidebar { display: none; }
.nav-stack { display: grid; gap: 5px; }
.nav-link { width: 100%; min-height: 44px; display: flex; align-items: center; gap: 10px; padding: 0 11px; border-radius: 12px; color: var(--muted); font-size: .86rem; }
.nav-link:hover, .nav-link.is-active { background: var(--surface-subtle); color: var(--text); }
.nav-link.is-active { font-weight: 720; }
.nav-icon { width: 28px; height: 28px; display: grid; place-items: center; }
.nav-button { border: 0; background: transparent; cursor: pointer; font-family: inherit; text-align: right; }
.sidebar-footer { margin-top: auto; padding-top: 18px; border-top: 1px solid var(--border); }

.bottom-nav { position: fixed; right: 10px; left: 10px; bottom: max(8px, env(safe-area-inset-bottom)); min-height: var(--bottom-nav-height); z-index: 30; display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; align-items: stretch; background: var(--surface); border: 1px solid var(--border); border-radius: 22px; padding: 6px; }
.bottom-nav-link { min-width: 0; display: grid; place-items: center; align-content: center; gap: 3px; color: var(--muted); font-size: .68rem; border-radius: 16px; padding: 4px 2px; }
.bottom-nav-link.is-active { color: var(--accent); background: var(--accent-soft); font-weight: 700; }
.bottom-nav-icon { display: grid; place-items: center; font-size: 1.12rem; }

.guest-shell { min-height: 100dvh; display: grid; place-items: center; padding: 20px; }
.auth-card { width: min(100%, 420px); background: var(--surface); border: 1px solid var(--border); border-radius: 24px; padding: 22px; }
.brand-lockup { display: flex; align-items: center; gap: 11px; margin-bottom: 24px; }
.brand-lockup strong, .brand-lockup small { display: block; }
.brand-lockup small { color: var(--muted); margin-top: 3px; }
.brand-dot { width: 38px; height: 38px; border-radius: 13px; background: var(--accent); display: block; }
.skeleton { min-height: 18px; border-radius: 10px; background: #eef0f3; animation: skeleton-pulse 1.2s ease-in-out infinite alternate; }
@keyframes skeleton-pulse { to { opacity: .48; } }

.jalali-popover { position: absolute; z-index: 60; top: calc(100% + 6px); left: 0; width: min(296px, calc(100vw - 36px)); padding: 10px; background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius-md); box-shadow: 0 12px 30px rgba(20, 26, 38, .10); }
.calendar-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.calendar-head button { width: 34px; height: 34px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); cursor: pointer; }
.calendar-title { font-size: .85rem; font-weight: 750; }
.calendar-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 3px; direction: rtl; }
.calendar-weekday { min-height: 28px; display: grid; place-items: center; color: var(--muted); font-size: .66rem; }
.calendar-day { aspect-ratio: 1; display: grid; place-items: center; border: 0; border-radius: 9px; background: transparent; cursor: pointer; font-size: .75rem; }
.calendar-day:hover { background: var(--surface-subtle); }
.calendar-day.is-selected { background: var(--accent); color: white; }
.calendar-day.is-today { outline: 1px solid #9cb7ec; }
.calendar-day.is-empty { pointer-events: none; }

@media (min-width: 640px) {
  .responsive-grid { grid-template-columns: repeat(2, minmax(0,1fr)); }
  .form-grid { grid-template-columns: repeat(2, minmax(0,1fr)); }
  .toolbar-fields { grid-template-columns: repeat(3, minmax(0,1fr)); }
  .line-editor-row { grid-template-columns: minmax(170px,1.5fr) minmax(130px,1fr) minmax(130px,1fr) minmax(180px,1.3fr) auto; align-items: end; }
}

@media (min-width: 900px) {
  .app-shell { display: grid; grid-template-columns: var(--sidebar-width) minmax(0,1fr); }
  .desktop-sidebar { display: flex; flex-direction: column; position: sticky; top: 0; height: 100dvh; background: var(--surface); border-left: 1px solid var(--border); padding: 22px 16px; }
  .bottom-nav { display: none; }
  .page-container { padding: 28px 28px 52px; }
  .topbar { padding: 8px 28px; }
  .stat-grid { grid-template-columns: repeat(4, minmax(0,1fr)); }
  .responsive-grid { grid-template-columns: repeat(3, minmax(0,1fr)); }
  .quick-links { grid-template-columns: repeat(4, minmax(0,1fr)); }
}

/* Phase 2 central commercial/admin patterns — still one Design System source */
.theme-blue { --accent: #2563eb; --accent-soft: #eff5ff; }
.theme-indigo { --accent: #4f46e5; --accent-soft: #f1f0ff; }
.theme-emerald { --accent: #0f8a63; --accent-soft: #edf9f4; }
.theme-amber { --accent: #b7791f; --accent-soft: #fff8e8; }
.theme-rose { --accent: #c24162; --accent-soft: #fff0f4; }
.theme-slate { --accent: #475569; --accent-soft: #f1f5f9; }
.brand-logo { width: 40px; height: 40px; display: grid; place-items: center; border-radius: 13px; overflow: hidden; border: 1px solid var(--border); background: var(--surface); flex: 0 0 auto; }
.brand-logo img { width: 100%; height: 100%; object-fit: cover; }
.code-log { direction: ltr; text-align: left; max-height: 560px; overflow: auto; border: 1px solid var(--border); border-radius: var(--radius-md); background: #111827; color: #e5e7eb; padding: 12px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .74rem; line-height: 1.7; }
.code-log > div { white-space: pre-wrap; overflow-wrap: anywhere; border-bottom: 1px solid rgba(255,255,255,.06); padding: 5px 0; }
.code-log > div:last-child { border-bottom: 0; }
.button-secondary { display: inline-flex; }
.ui-button.size-sm { min-height: 36px; padding-inline: 11px; font-size: .78rem; border-radius: var(--radius-sm); }

/* Phase 3 central responsive/data-management patterns */
body { overflow-x: hidden; }
img, video, canvas { max-width: 100%; height: auto; }
.ui-card, .page-header, .toolbar, .filter-bar, .record-card, .responsive-records { min-width: 0; }
.page-header-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; justify-content: flex-end; }
.filter-bar { display: grid; grid-template-columns: 1fr; gap: 10px; align-items: end; }
.search-box { display: grid; gap: 7px; min-width: 0; font-size: .84rem; font-weight: 650; }
.search-box-label { color: #343840; }
.search-box-control { position: relative; display: block; }
.search-box-control > svg { position: absolute; right: 12px; top: 50%; translate: 0 -50%; color: var(--muted); pointer-events: none; z-index: 1; }
.search-box-control .ui-input { padding-right: 40px; }
.responsive-records-mobile { display: grid; gap: 10px; }
.responsive-records-desktop { display: none; }
.record-card { display: grid; gap: 11px; padding: 14px; border: 1px solid var(--border); border-radius: var(--radius-md); background: var(--surface); }
.record-main { min-width: 0; }
.record-title, .record-subtitle { display: block; }
.record-title { font-size: .9rem; overflow-wrap: anywhere; }
.record-subtitle { margin-top: 4px; color: var(--muted); font-size: .76rem; line-height: 1.65; overflow-wrap: anywhere; }
.record-meta { display: flex; flex-wrap: wrap; gap: 7px; align-items: center; }
.record-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding-top: 3px; }
.tag-list { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.tag-chip { display: inline-flex; align-items: center; min-height: 28px; padding: 0 9px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface-subtle); color: var(--muted); font-size: .72rem; }
.ui-tabs { display: flex; gap: 6px; overflow-x: auto; padding: 4px; border: 1px solid var(--border); border-radius: var(--radius-md); background: var(--surface); scrollbar-width: thin; }
.ui-tab { min-height: 38px; display: inline-flex; align-items: center; justify-content: center; flex: 0 0 auto; padding: 0 12px; border: 0; border-radius: var(--radius-sm); background: transparent; color: var(--muted); font: inherit; font-size: .8rem; cursor: pointer; white-space: nowrap; }
.ui-tab.is-active { background: var(--accent-soft); color: var(--accent); font-weight: 750; }
.pagination { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; padding-top: 4px; }
.pagination-info { color: var(--muted); font-size: .78rem; }
.pagination-actions { display: flex; align-items: center; gap: 7px; }
.profile-grid { display: grid; grid-template-columns: 1fr; gap: 12px; }
.profile-summary { display: grid; gap: 8px; }
.detail-line { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.timeline { display: grid; gap: 0; }
.timeline-item { display: grid; grid-template-columns: 12px minmax(0,1fr); gap: 10px; padding: 11px 0; border-bottom: 1px solid var(--border); }
.timeline-item:last-child { border-bottom: 0; }
.timeline-marker { width: 9px; height: 9px; margin-top: 7px; border-radius: 999px; background: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.timeline-content { min-width: 0; }
.timeline-content strong, .timeline-content small { display: block; }
.timeline-content small { margin-top: 4px; color: var(--muted); line-height: 1.7; }
.permission-grid { display: grid; grid-template-columns: 1fr; gap: 8px; }
.permission-item { min-width: 0; padding: 10px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface-subtle); }
.split-layout { display: grid; grid-template-columns: 1fr; gap: 14px; }
.compact-meta { display: flex; flex-wrap: wrap; gap: 6px 12px; color: var(--muted); font-size: .76rem; line-height: 1.7; }
.data-summary { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

@media (max-width: 479px) {
  .page-header { flex-direction: column; align-items: stretch; }
  .page-header-actions, .page-header > .inline-actions { width: 100%; justify-content: stretch; }
  .page-header-actions .ui-button, .page-header > .inline-actions .ui-button { flex: 1 1 auto; }
  .list-row { align-items: flex-start; flex-direction: column; }
  .summary-bar { grid-template-columns: 1fr; }
  .topbar-meta { flex: 0 0 auto; }
  .topbar-title { max-width: calc(100vw - 112px); }
}

@media (min-width: 640px) {
  .filter-bar { grid-template-columns: minmax(240px, 2fr) repeat(2, minmax(150px, 1fr)); }
  .permission-grid { grid-template-columns: repeat(2, minmax(0,1fr)); }
  .profile-grid { grid-template-columns: repeat(2, minmax(0,1fr)); }
}

@media (min-width: 760px) {
  .responsive-records-mobile { display: none; }
  .responsive-records-desktop { display: block; }
}

@media (min-width: 1000px) {
  .split-layout { grid-template-columns: minmax(0, 1.65fr) minmax(300px, .75fr); align-items: start; }
  .permission-grid { grid-template-columns: repeat(3, minmax(0,1fr)); }
}
