/*
 * Palette: "Automotive/Car Dealership" from the ui-ux-pro-max design-system
 * database (slate navy + action red), adapted for this app's semantics —
 * the source palette reuses red for both the primary accent and destructive
 * actions, which would collide with this app's existing "red = delete"
 * convention. Navy is used as the primary action color instead; red is
 * reserved strictly for destructive/danger.
 */
:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #475569;
  --primary: #1e293b;
  --primary-dark: #0f172a;
  --primary-soft: #e9edf1;
  --danger: #dc2626;
  --danger-dark: #b91c1c;
  --success-bg: #dcfce7;
  --success-text: #166534;
  --info-bg: #dbeafe;
  --info-text: #1e40af;
  --neutral-bg: #e9edf1;
  --neutral-text: #475569;
  --header-bg: #1e293b;
  --header-text: #f8fafc;
  --header-text-muted: #94a3b8;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08), 0 1px 2px rgba(15, 23, 42, 0.06);
  --font: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-ar: "Cairo", "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Tahoma, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
}

html[dir="rtl"] body {
  font-family: var(--font-ar);
}

.hidden {
  display: none !important;
}

/* Error banner */
.error-banner {
  background: #fee2e2;
  color: #7f1d1d;
  padding: 0.75rem 1.5rem;
  font-size: 0.9rem;
  border-bottom: 1px solid #fecaca;
}

.error-banner.info {
  background: var(--info-bg);
  color: var(--info-text);
  border-bottom: 1px solid #bfdbfe;
}

/* Shell / sidebar nav */
.app-shell {
  display: flex;
  align-items: stretch;
  min-height: 100vh;
}

.app-sidebar {
  display: flex;
  flex-direction: column;
  width: 232px;
  flex-shrink: 0;
  padding: 1.5rem 1rem;
  background: var(--header-bg);
  border-right: 1px solid #334155;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

html[dir="rtl"] .app-sidebar {
  border-right: none;
  border-left: 1px solid #334155;
}

.app-sidebar.hidden {
  display: none;
}

.app-sidebar .brand {
  font-weight: 800;
  font-size: 1.1rem;
  color: var(--header-text);
  letter-spacing: -0.01em;
  padding: 0 0.6rem;
  margin-bottom: 1.75rem;
}

.app-nav {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  flex: 1;
}

.app-nav a {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  color: var(--header-text-muted);
  text-decoration: none;
  font-size: 0.92rem;
  font-weight: 600;
  padding: 0.6rem 0.7rem;
  border-radius: var(--radius);
}

.app-nav a svg {
  width: 19px;
  height: 19px;
  flex-shrink: 0;
}

.app-nav a:hover {
  color: var(--header-text);
  background: rgba(248, 250, 252, 0.06);
}

.app-nav a.active {
  color: var(--header-text);
  background: rgba(248, 250, 252, 0.12);
}

.sidebar-footer {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding-top: 1rem;
  margin-top: 1rem;
  border-top: 1px solid #334155;
}

.lock-btn {
  background: none;
  border: 1px solid #334155;
  border-radius: var(--radius);
  padding: 0.5rem 0.9rem;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  color: var(--header-text-muted);
  width: 100%;
}

.lock-btn:hover {
  background: #334155;
  color: var(--header-text);
}

/* Page layout */
.app-main {
  flex: 1;
  min-width: 0;
}

#app-root {
  padding: 1.75rem;
}

.page {
  max-width: 1100px;
  margin: 0 auto;
}

@media (max-width: 720px) {
  .app-sidebar {
    width: 76px;
    padding: 1.25rem 0.5rem;
  }

  .app-sidebar .brand,
  .nav-label {
    display: none;
  }

  .app-nav a {
    justify-content: center;
    padding: 0.7rem;
  }
}

.page h1 {
  font-size: 1.55rem;
  font-weight: 800;
  letter-spacing: -0.01em;
  margin: 0 0 1.1rem;
}

.page h2 {
  font-size: 1.1rem;
  font-weight: 700;
  margin: 2rem 0 0.85rem;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.1rem;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.page-header h1,
.page-header h2 {
  margin: 0;
}

.page-header.section-header {
  margin-top: 2rem;
}

/* Buttons */
button {
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius);
  padding: 0.5rem 1rem;
  color: var(--text);
}

button:hover {
  background: var(--bg);
}

button.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

button.primary:hover {
  background: var(--primary-dark);
}

button.danger {
  color: var(--danger);
  border-color: #fecaca;
}

button.danger:hover {
  background: #fef2f2;
  border-color: var(--danger);
}

button.primary.danger-solid {
  background: var(--danger);
  border-color: var(--danger);
}

button.primary.danger-solid:hover {
  background: var(--danger-dark);
}

/* Stats grid */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.2rem;
  box-shadow: var(--shadow);
}

.stat-value {
  font-size: 1.7rem;
  font-weight: 800;
  letter-spacing: -0.01em;
}

.stat-label {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: 0.2rem;
  font-weight: 600;
}

/* Trend + breakdown charts (dashboard) */
.chart-row {
  display: flex;
  gap: 1.5rem;
  flex-wrap: wrap;
}

.chart-col {
  flex: 1 1 260px;
  min-width: 0;
}

.chart-col-wide {
  flex: 2 1 420px;
}

.chart-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem 1.4rem 1rem;
  margin-bottom: 1.5rem;
  height: 260px;
}

/* Period picker (dashboard) */
.period-picker {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
  margin-bottom: 1rem;
}

.period-toggle {
  display: flex;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.period-toggle button {
  border: none;
  border-radius: 0;
  background: var(--surface);
  color: var(--text-muted);
  padding: 0.45rem 0.9rem;
}

.period-toggle button:hover {
  background: var(--bg);
}

.period-toggle button.active {
  background: var(--primary-soft);
  color: var(--primary-dark);
}

.month-picker,
.range-picker {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.month-picker select,
.period-picker input[type="date"] {
  font: inherit;
  padding: 0.45rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
}

.period-label {
  color: var(--text-muted);
  font-size: 0.85rem;
  font-weight: 600;
}

/* Toolbar */
.toolbar {
  display: flex;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.toolbar input,
.toolbar select {
  font: inherit;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.toolbar input[type="search"] {
  flex: 1;
  min-width: 200px;
}

/* Table */
.table-wrap {
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.data-table th,
.data-table td {
  text-align: start;
  padding: 0.55rem 0.85rem;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.data-table th {
  color: var(--text-muted);
  font-weight: 700;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.th-sort-btn {
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  font-weight: 700;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  cursor: pointer;
  white-space: nowrap;
}

.th-sort-btn:hover {
  color: var(--text);
  background: none;
}

.aging-note {
  display: block;
  margin-top: 0.2rem;
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--danger);
  white-space: nowrap;
}

.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.85rem;
  padding: 1rem 0 0;
  font-size: 0.85rem;
  color: var(--text-muted);
  font-weight: 600;
}

.pagination button {
  padding: 0.4rem 0.8rem;
}

.pagination button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.data-table tbody tr:last-child td {
  border-bottom: none;
}

.row-actions {
  display: flex;
  gap: 0.4rem;
}

.icon-btn {
  width: 34px;
  height: 34px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.icon-btn svg {
  width: 16px;
  height: 16px;
}

/* Status badges */
.status-badge {
  display: inline-block;
  padding: 0.22rem 0.6rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 700;
  background: var(--neutral-bg);
  color: var(--neutral-text);
}

.status-in_stock,
.status-paid {
  background: var(--success-bg);
  color: var(--success-text);
}

.status-reserved,
.status-pending {
  background: var(--info-bg);
  color: var(--info-text);
}

.status-sold {
  background: var(--neutral-bg);
  color: var(--neutral-text);
}

.status-part-oil {
  background: var(--info-bg);
  color: var(--info-text);
}

.status-part-part {
  background: var(--success-bg);
  color: var(--success-text);
}

.status-part-tire,
.status-part-other {
  background: var(--neutral-bg);
  color: var(--neutral-text);
}

/* Empty state */
.empty-state {
  text-align: center;
  padding: 2.5rem 1rem;
  color: var(--text-muted);
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

/* Login */
.login-wrap {
  min-height: calc(100vh - 3rem);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 3rem 2.75rem;
  width: 100%;
  max-width: 460px;
}

.login-card h1 {
  font-size: 1.65rem;
  font-weight: 800;
  letter-spacing: -0.01em;
  margin: 0 0 0.5rem;
  text-align: center;
}

.login-subtitle {
  color: var(--text-muted);
  font-size: 1rem;
  text-align: center;
  margin: 0 0 2rem;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 1.1rem;
}

.login-form input {
  font: inherit;
  font-size: 1.05rem;
  padding: 0.9rem 1.1rem;
  border: 1px solid var(--border);
  border-radius: 10px;
}

.login-form input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

.login-form button {
  margin-top: 0.35rem;
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
  font-size: 1.05rem;
  padding: 0.85rem;
  border-radius: 10px;
}

.login-form button:hover {
  background: var(--primary-dark);
}

/* Forms / dialogs */
.form-error {
  color: var(--danger);
  font-size: 0.85rem;
}

.page-loading {
  color: var(--text-muted);
  padding: 2rem 0;
  text-align: center;
}

.app-dialog {
  border: none;
  border-radius: var(--radius);
  padding: 0;
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.25);
  max-width: 620px;
  width: 90vw;
}

.app-dialog::backdrop {
  background: rgba(15, 23, 42, 0.5);
}

.dialog-form {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.65rem 1rem;
  padding: 1.25rem 1.5rem;
  max-height: 90vh;
  overflow-y: auto;
}

.dialog-form > h2,
.dialog-form > .form-error,
.dialog-form > .form-field-wide,
.dialog-form > .photo-field,
.dialog-form > .table-wrap,
.dialog-form > .confirm-message,
.dialog-form > .dialog-actions {
  grid-column: 1 / -1;
}

.confirm-dialog {
  max-width: 380px;
}

.confirm-box {
  padding: 1.5rem;
}

.confirm-box h2 {
  margin: 0 0 0.6rem;
  font-size: 1.1rem;
  font-weight: 800;
}

.confirm-message {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.92rem;
  line-height: 1.45;
}

.confirm-box .dialog-actions {
  margin-top: 1.25rem;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%) translateY(12px);
  background: var(--header-bg);
  color: var(--header-text);
  padding: 0.7rem 1.25rem;
  border-radius: 999px;
  font-size: 0.88rem;
  font-weight: 600;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.28);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 60;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.dialog-form h2 {
  margin: 0 0 0.25rem;
  font-size: 1.15rem;
  font-weight: 800;
}

.form-field {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
}

.form-field input,
.form-field select,
.form-field textarea {
  font: inherit;
  font-weight: 400;
  color: var(--text);
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.form-field textarea {
  min-height: 52px;
  resize: vertical;
}

.cart-field {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.cart-field-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
}

.cart-list {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.cart-empty {
  color: var(--text-muted);
  font-size: 0.85rem;
  padding: 0.5rem 0;
}

.cart-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.45rem 0.6rem;
}

.cart-row-label {
  flex: 1;
  font-size: 0.9rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.cart-qty-input {
  width: 56px;
  font: inherit;
  padding: 0.35rem 0.4rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  text-align: center;
}

.cart-price-input {
  width: 90px;
  font: inherit;
  padding: 0.35rem 0.4rem;
  border: 1px solid var(--border);
  border-radius: 6px;
}

.cart-remove-btn {
  width: 26px;
  height: 26px;
  padding: 0;
  border-radius: 999px;
  border: none;
  background: none;
  color: var(--danger);
  font-size: 1rem;
  line-height: 1;
  flex-shrink: 0;
}

.cart-remove-btn:hover {
  background: #fee2e2;
}

.cart-add-row {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.cart-add-row select {
  flex: 1;
  min-width: 160px;
  font: inherit;
  padding: 0.5rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
}

.cart-total {
  text-align: right;
  font-weight: 800;
  font-size: 1rem;
  padding-top: 0.2rem;
}

.photo-field {
  display: flex;
  flex-direction: column;
}

.photo-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
}

.photo-thumb {
  position: relative;
  width: 84px;
  height: 84px;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--bg);
}

.photo-thumb img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

.photo-thumb-remove {
  position: absolute;
  top: 3px;
  inset-inline-end: 3px;
  width: 20px;
  height: 20px;
  padding: 0;
  line-height: 1;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.65);
  border: none;
  color: #fff;
  font-size: 0.9rem;
  cursor: pointer;
}

.photo-thumb-remove:hover {
  background: rgba(15, 23, 42, 0.85);
}

.photo-add-tile {
  width: 84px;
  height: 84px;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text-muted);
  font-size: 0.78rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 0.4rem;
}

.photo-add-tile:hover {
  background: var(--bg);
}

.table-thumb-wrap {
  position: relative;
  width: 44px;
  height: 44px;
  cursor: pointer;
}

.table-thumb {
  width: 44px;
  height: 44px;
  object-fit: contain;
  border-radius: 6px;
  display: block;
  background: var(--bg);
  border: 1px solid var(--border);
}

.table-thumb-empty {
  width: 44px;
  height: 44px;
  border-radius: 6px;
  background: var(--neutral-bg);
}

.table-thumb-count {
  position: absolute;
  bottom: -4px;
  inset-inline-end: -4px;
  background: var(--header-bg);
  color: var(--header-text);
  font-size: 0.62rem;
  font-weight: 700;
  padding: 1px 4px;
  border-radius: 999px;
  line-height: 1.3;
}

.photo-viewer-dialog {
  max-width: 90vw;
  width: auto;
  background: transparent;
  box-shadow: none;
}

.photo-viewer-dialog::backdrop {
  background: rgba(2, 6, 23, 0.82);
}

.photo-viewer {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--header-bg);
  border-radius: var(--radius);
  padding: 1rem;
  min-width: 320px;
  min-height: 240px;
}

.photo-viewer-img {
  max-width: min(80vw, 900px);
  max-height: 80vh;
  object-fit: contain;
  display: block;
  border-radius: 6px;
}

.photo-viewer-close {
  position: absolute;
  top: 0.75rem;
  inset-inline-end: 0.75rem;
  width: 34px;
  height: 34px;
  border-radius: 999px;
  border: none;
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  font-size: 1.2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.photo-viewer-close:hover {
  background: rgba(0, 0, 0, 0.7);
}

.photo-viewer-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 44px;
  height: 44px;
  border-radius: 999px;
  border: none;
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  font-size: 1.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.photo-viewer-nav:hover {
  background: rgba(0, 0, 0, 0.7);
}

.photo-viewer-prev {
  left: 0.75rem;
}

.photo-viewer-next {
  right: 0.75rem;
}

.photo-viewer-counter {
  position: absolute;
  bottom: 0.75rem;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 0.8rem;
  font-weight: 700;
  padding: 0.25rem 0.7rem;
  border-radius: 999px;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
  margin-top: 0.5rem;
}

/* Responsive */
@media (max-width: 640px) {
  .app-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.6rem;
  }

  .app-nav {
    flex-wrap: wrap;
    gap: 0.75rem;
  }

  #app-root {
    padding: 1rem;
  }

  .toolbar {
    flex-direction: column;
  }

  .login-card {
    padding: 2.25rem 1.5rem;
  }

  .dialog-form {
    grid-template-columns: 1fr;
  }
}
