/**
 * CarTech Accounting — Main Stylesheet
 * Flat, minimal, RTL, Light/Dark mode
 */

:root {
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --success: #16a34a;
  --warning: #f59e0b;
  --danger: #dc2626;
  --info: #0ea5e9;

  --bg: #f8fafc;
  --bg-card: #ffffff;
  --bg-sidebar: #0f172a;
  --bg-topbar: #ffffff;
  --text: #0f172a;
  --text-muted: #64748b;
  --text-sidebar: #cbd5e1;
  --border: #e2e8f0;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.06);
  --radius: 6px;
  --sidebar-width: 260px;
  --topbar-height: 60px;
  --font: "Vazirmatn", "Tahoma", sans-serif;
  --transition: 0.2s ease;
  color-scheme: light;
}

/* Prevent white flash before CSS variables resolve */
html {
  background-color: #f8fafc;
}
html[data-theme="dark"] {
  background-color: #0b1220;
  color-scheme: dark;
}

[data-theme="dark"] {
  --bg: #0b1220;
  --bg-card: #111827;
  --bg-sidebar: #020617;
  --bg-topbar: #111827;
  --text: #e5e7eb;
  --text-muted: #94a3b8;
  --text-sidebar: #94a3b8;
  --border: #1f2937;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
}

*, *::before, *::after { box-sizing: border-box; }

html { font-size: 14px; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  direction: rtl;
  min-height: 100vh;
}

a { color: var(--primary); text-decoration: none; }
a:hover { color: var(--primary-hover); }

img { max-width: 100%; height: auto; }

/* Layout */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-sidebar);
  color: var(--text-sidebar);
  position: fixed;
  inset-block: 0;
  inset-inline-start: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  transition: transform var(--transition);
}

.sidebar-brand {
  height: var(--topbar-height);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 20px;
  border-bottom: 1px solid rgba(255,255,255,0.06);
  color: #fff;
  font-weight: 700;
  font-size: 1.05rem;
}

.sidebar-brand .logo-mark {
  width: 32px; height: 32px;
  background: transparent;
  border-radius: var(--radius);
  display: grid; place-items: center;
  overflow: hidden;
  flex-shrink: 0;
}
.sidebar-brand .logo-mark img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: 12px 10px;
}

.nav-section {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #64748b;
  padding: 12px 12px 6px;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius);
  color: var(--text-sidebar);
  margin-bottom: 2px;
  transition: background var(--transition), color var(--transition);
}

.nav-link:hover, .nav-link.active {
  background: rgba(37, 99, 235, 0.18);
  color: #fff;
}

.nav-link svg { width: 18px; height: 18px; flex-shrink: 0; opacity: 0.85; }

.main-wrap {
  flex: 1;
  margin-inline-start: var(--sidebar-width);
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  height: var(--topbar-height);
  background: var(--bg-topbar);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: var(--shadow);
}

.topbar-right, .topbar-left { display: flex; align-items: center; gap: 12px; }

.page-content { padding: 20px; flex: 1; }

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.page-header h1 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
}

/* Cards */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}

.card-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.card-body { padding: 16px; }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 16px;
}

.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}

.stat-card .label { color: var(--text-muted); font-size: 0.85rem; margin-bottom: 6px; }
.stat-card .value { font-size: 1.35rem; font-weight: 700; }
.stat-card.income .value { color: var(--success); }
.stat-card.expense .value { color: var(--danger); }
.stat-card.balance .value { color: var(--primary); }

/* Forms */
.form-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px;
}

.form-group { margin-bottom: 14px; }
.form-group.full { grid-column: 1 / -1; }

.form-label {
  display: block;
  margin-bottom: 6px;
  font-weight: 500;
  font-size: 0.9rem;
}

.form-control, .form-select {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: var(--text);
  font-family: inherit;
  font-size: 0.95rem;
  transition: border-color var(--transition);
}

.form-control:focus, .form-select:focus {
  outline: none;
  border-color: var(--primary);
}

.form-control.is-invalid { border-color: var(--danger); }
.invalid-feedback { color: var(--danger); font-size: 0.8rem; margin-top: 4px; }

.form-check {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  background: var(--bg);
  color: var(--text);
  transition: background var(--transition), border-color var(--transition), opacity var(--transition);
  text-decoration: none;
  white-space: nowrap;
}

.btn:disabled { opacity: 0.6; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); color: #fff; }
.btn-success { background: var(--success); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-warning { background: var(--warning); color: #111; }
.btn-outline {
  background: transparent;
  border-color: var(--border);
  color: var(--text);
}
.btn-outline:hover { border-color: var(--primary); color: var(--primary); }
.btn-sm { padding: 5px 10px; font-size: 0.8rem; }
.btn-icon {
  width: 36px; height: 36px; padding: 0;
  border-radius: var(--radius);
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}

/* Tables */
.table-responsive { overflow-x: auto; }

.table {
  width: 100%;
  border-collapse: collapse;
}

.table th, .table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  text-align: right;
  white-space: nowrap;
}

.table th {
  font-weight: 600;
  color: var(--text-muted);
  font-size: 0.8rem;
  background: transparent;
}

.table tbody tr:hover { background: rgba(37, 99, 235, 0.04); }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 600;
}
.badge-success { background: rgba(22,163,74,.12); color: var(--success); }
.badge-danger { background: rgba(220,38,38,.12); color: var(--danger); }
.badge-warning { background: rgba(245,158,11,.15); color: #b45309; }
.badge-info { background: rgba(14,165,233,.12); color: var(--info); }
.badge-muted { background: rgba(100,116,139,.12); color: var(--text-muted); }

/* Auth pages */
.auth-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: var(--bg);
  padding: 20px;
}

.auth-card {
  width: 100%;
  max-width: 420px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px;
}

.auth-card h1 {
  margin: 0 0 6px;
  font-size: 1.3rem;
  text-align: center;
}

.auth-card .subtitle {
  text-align: center;
  color: var(--text-muted);
  margin-bottom: 24px;
}

.auth-logo {
  width: 64px; height: 64px;
  background: transparent;
  border-radius: 14px;
  display: grid; place-items: center;
  margin: 0 auto 16px;
  overflow: hidden;
  box-shadow: 0 8px 20px rgba(15, 23, 42, 0.12);
}
.auth-logo img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

/* Toast */
.toast-container {
  position: fixed;
  top: 16px;
  inset-inline-start: 16px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  min-width: 280px;
  max-width: 400px;
  padding: 12px 14px;
  border-radius: var(--radius);
  background: var(--bg-card);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  display: flex;
  align-items: flex-start;
  gap: 10px;
  animation: toastIn 0.25s ease;
}

.toast.success { border-inline-start: 3px solid var(--success); }
.toast.error, .toast.danger { border-inline-start: 3px solid var(--danger); }
.toast.warning { border-inline-start: 3px solid var(--warning); }
.toast.info { border-inline-start: 3px solid var(--info); }

@keyframes toastIn {
  from { opacity: 0; transform: translateY(-8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Dropzone */
.dropzone {
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  padding: 24px;
  text-align: center;
  color: var(--text-muted);
  cursor: pointer;
  transition: border-color var(--transition), background var(--transition);
}
.dropzone.dragover {
  border-color: var(--primary);
  background: rgba(37,99,235,0.05);
}

/* Attachment uploader */
.attachment-uploader { margin-top: 4px; }
.attach-list {
  list-style: none;
  margin: 12px 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.attach-item {
  display: grid;
  grid-template-columns: 56px 1fr auto;
  gap: 12px;
  align-items: center;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
}
.attach-item.is-uploading { border-color: var(--primary); }
.attach-item.is-done { border-color: color-mix(in srgb, var(--success) 45%, var(--border)); }
.attach-item.is-error { border-color: var(--danger); background: color-mix(in srgb, var(--danger) 6%, var(--bg-card)); }
.attach-thumb {
  width: 56px;
  height: 56px;
  border-radius: 8px;
  overflow: hidden;
  background: var(--bg-muted, #f3f4f6);
  display: flex;
  align-items: center;
  justify-content: center;
}
.attach-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  cursor: zoom-in;
}
.attach-file-icon { font-size: 1.4rem; line-height: 1; }
.attach-meta { min-width: 0; }
.attach-name {
  font-size: .92rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.attach-sub, .attach-status {
  font-size: .8rem;
  color: var(--text-muted);
  margin-top: 2px;
}
.attach-item.is-error .attach-status { color: var(--danger); }
.attach-item.is-done .attach-status { color: var(--success); }
.attach-progress {
  height: 4px;
  border-radius: 999px;
  background: var(--border);
  margin-top: 6px;
  overflow: hidden;
}
.attach-progress > span {
  display: block;
  height: 100%;
  width: 0;
  background: var(--primary);
  transition: width .15s ease;
}
.attach-item.is-error .attach-progress > span { background: var(--danger); }
.attach-item.is-done .attach-progress > span { background: var(--success); }
.attach-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: flex-end;
}
.attach-summary {
  margin-top: 8px;
  font-size: .85rem;
  color: var(--text-muted);
}

/* Attachment gallery (show page) */
.attach-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 12px;
  margin-top: 12px;
}
.attach-card {
  margin: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--bg-card);
  display: flex;
  flex-direction: column;
}
.attach-card-preview {
  border: 0;
  padding: 0;
  background: var(--bg-muted, #f3f4f6);
  cursor: zoom-in;
  aspect-ratio: 4 / 3;
  overflow: hidden;
}
.attach-card-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.attach-card-file {
  aspect-ratio: 4 / 3;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  background: var(--bg-muted, #f3f4f6);
}
.attach-card figcaption {
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.attach-card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
}

/* Lightbox */
.attach-lightbox {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
}
.attach-lightbox[hidden] { display: none !important; }
.attach-lightbox-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.82);
}
.attach-lightbox-dialog {
  position: relative;
  z-index: 1;
  max-width: min(96vw, 1100px);
  max-height: 92vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}
.attach-lightbox-img {
  max-width: 96vw;
  max-height: calc(92vh - 56px);
  object-fit: contain;
  border-radius: 8px;
  box-shadow: 0 20px 50px rgba(0,0,0,.35);
  background: #0b1220;
}
.attach-lightbox-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: min(96vw, 720px);
  color: #fff;
  font-size: .9rem;
}
.attach-lightbox-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.attach-lightbox-close,
.attach-lightbox-nav {
  position: absolute;
  border: 0;
  background: rgba(255,255,255,.12);
  color: #fff;
  width: 40px;
  height: 40px;
  border-radius: 999px;
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
}
.attach-lightbox-close { top: -8px; inset-inline-end: -8px; }
.attach-lightbox-nav.prev { inset-inline-start: -52px; top: 50%; transform: translateY(-50%); }
.attach-lightbox-nav.next { inset-inline-end: -52px; top: 50%; transform: translateY(-50%); }
@media (max-width: 720px) {
  .attach-lightbox-nav.prev { inset-inline-start: 8px; }
  .attach-lightbox-nav.next { inset-inline-end: 8px; }
  .attach-lightbox-close { inset-inline-end: 8px; top: 8px; }
  .attach-item { grid-template-columns: 48px 1fr; }
  .attach-actions { grid-column: 1 / -1; justify-content: flex-start; }
}

/* Searchable select */
.search-select { position: relative; }
.search-select-results {
  position: absolute;
  inset-inline: 0;
  top: 100%;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  max-height: 220px;
  overflow-y: auto;
  z-index: 20;
  display: none;
  box-shadow: var(--shadow);
}
.search-select-results.open { display: block; }
.search-select-results div {
  padding: 8px 12px;
  cursor: pointer;
}
.search-select-results div:hover { background: rgba(37,99,235,0.08); }

/* Pagination */
.pagination {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
  margin-top: 16px;
}
.pagination a, .pagination span {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
}
.pagination .active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

/* Charts */
.chart-box {
  height: 260px;
  position: relative;
}
.chart-box canvas { width: 100% !important; height: 100% !important; }
.chart-tooltip {
  position: absolute;
  z-index: 5;
  pointer-events: none;
  background: var(--bg-elevated, var(--card-bg, #0f172a));
  color: var(--text, #f8fafc);
  border: 1px solid var(--border, #334155);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 0.8rem;
  line-height: 1.5;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.18);
  white-space: nowrap;
  max-width: min(240px, 90%);
}
.chart-tooltip-label {
  color: var(--text-muted, #94a3b8);
  font-size: 0.75rem;
  margin-bottom: 2px;
}
.chart-tooltip-value {
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 6px;
}
.chart-tooltip-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.filters-bar {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 10px;
  margin-bottom: 16px;
}

.user-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
}
.user-avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  display: grid; place-items: center;
  font-weight: 700; font-size: 0.8rem;
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 90;
}

.color-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  display: inline-block;
  margin-inline-end: 6px;
}

.empty-state {
  text-align: center;
  padding: 40px 16px;
  color: var(--text-muted);
}

.form-locked { pointer-events: none; opacity: 0.7; }

/* Error pages */
.error-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 20px;
}
.error-page .code {
  font-size: 5rem;
  font-weight: 800;
  color: var(--primary);
  line-height: 1;
}

@media (max-width: 1100px) {
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .grid-2 { grid-template-columns: 1fr; }
}

@media (max-width: 900px) {
  .sidebar { transform: translateX(100%); }
  .sidebar.open { transform: translateX(0); }
  .main-wrap { margin-inline-start: 0; }
  .sidebar-overlay.open { display: block; }
  .form-grid { grid-template-columns: 1fr; }
}

@media (max-width: 600px) {
  .stat-grid { grid-template-columns: 1fr; }
  .page-content { padding: 14px; }
}

.amount-toman-hint {
  margin-top: 6px;
  font-size: 0.82rem;
  color: var(--text-muted);
}

.amount-toman-hint strong {
  color: var(--primary);
  font-weight: 600;
}

.amount-toman-sub {
  opacity: 0.75;
  margin-inline-start: 4px;
}

.text-success { color: var(--success); }
.text-danger { color: var(--danger); }

@media print {
  .sidebar, .topbar, .btn, .no-print { display: none !important; }
  .main-wrap { margin: 0 !important; }
  .page-content { padding: 0 !important; }
  .card { box-shadow: none; border: none; }
}
