:root {
  --primary: #1d4ed8;
  --primary-dark: #1e3a8a;
  --bg: #f3f4f6;
  --surface: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --radius: 14px;
  font-family: "Segoe UI", "Heebo", Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  direction: rtl;
  -webkit-tap-highlight-color: transparent;
}

button, input, select, textarea { font-family: inherit; font-size: 15px; }

/* Number inputs inherit the page's RTL direction like everything else, but
   digits are inherently LTR — under RTL the browser's caret handling for
   type="number" scrambles digit order on fast typing. Force LTR editing
   while keeping right-alignment so it still looks at home in the RTL form. */
input[type="number"], input[type="tel"] { direction: ltr; text-align: right; }

#root { min-height: 100vh; }

.loading-screen {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 15px; text-align: center; padding: 20px;
}

/* ---------- Login ---------- */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, var(--primary-dark), var(--primary));
  padding: 20px;
}
.login-box {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 28px 22px;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 20px 50px rgba(0,0,0,0.25);
}
.login-title { margin: 0 0 2px; font-size: 26px; text-align: center; color: var(--primary-dark); }
.login-sub { text-align: center; color: var(--muted); margin-bottom: 20px; font-size: 14px; }
.login-demo-title { margin-top: 22px; font-size: 13px; color: var(--muted); text-align: center; }
.demo-list { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; max-height: 260px; overflow-y: auto; }
.demo-user {
  display: flex; align-items: center; gap: 10px;
  border: 1px solid var(--border); background: #fafafa; border-radius: 10px;
  padding: 8px 10px; text-align: right; cursor: pointer;
}
.demo-user:hover { background: #eef2ff; }
.demo-role {
  font-size: 11px; padding: 3px 8px; border-radius: 999px; font-weight: 600; white-space: nowrap;
}
.role-admin { background: #fee2e2; color: #b91c1c; }
.role-technician { background: #dbeafe; color: #1d4ed8; }
.role-customer { background: #dcfce7; color: #15803d; }

/* ---------- App shell ---------- */
.app-shell { min-height: 100vh; display: flex; flex-direction: column; padding-bottom: 70px; }
.topbar {
  position: sticky; top: 0; z-index: 10;
  background: var(--primary-dark); color: white;
  padding: 14px 16px; display: flex; align-items: center; justify-content: space-between;
}
.topbar-title { font-weight: 700; font-size: 17px; }
.topbar-sub { font-size: 12px; opacity: 0.85; }
.app-main { flex: 1; padding: 16px; max-width: 720px; width: 100%; margin: 0 auto; }
.admin-shell .app-main.admin-main { max-width: 960px; padding-bottom: 40px; }

.admin-tabs {
  display: flex; gap: 4px; overflow-x: auto; background: var(--surface);
  border-bottom: 1px solid var(--border); padding: 0 10px; position: sticky; top: 52px; z-index: 9;
}
.admin-tab {
  border: none; background: none; padding: 12px 10px; white-space: nowrap; cursor: pointer;
  color: var(--muted); font-weight: 600; border-bottom: 3px solid transparent;
}
.admin-tab.active { color: var(--primary); border-bottom-color: var(--primary); }

.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  display: flex; background: var(--surface); border-top: 1px solid var(--border);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
}
.bottom-nav-item {
  flex: 1; border: none; background: none; display: flex; flex-direction: column; align-items: center;
  gap: 2px; padding: 6px 2px; color: var(--muted); font-size: 12px; cursor: pointer;
}
.bottom-nav-item.active { color: var(--primary); font-weight: 700; }
.bn-icon { font-size: 20px; }

/* ---------- Layout helpers ---------- */
.stack { display: flex; flex-direction: column; gap: 14px; }
.stack-sm { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.btn-col { display: flex; flex-direction: column; gap: 10px; }
.section-title { font-size: 16px; margin: 4px 0 0; }
.section-title-sm { font-size: 14px; margin: 10px 0 0; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ---------- Cards / grids ---------- */
.card-grid { display: grid; grid-template-columns: 1fr; gap: 10px; }
@media (min-width: 520px) { .card-grid { grid-template-columns: 1fr 1fr; } }
.machine-card {
  text-align: right; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; cursor: pointer; display: flex; flex-direction: column; gap: 4px;
}
.machine-card:hover { border-color: var(--primary); }
.machine-card-top { display: flex; justify-content: space-between; align-items: baseline; }
.machine-card-last { font-size: 13px; margin-top: 4px; }
.link-hint { font-size: 12px; color: var(--primary); margin-top: 4px; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; display: flex; flex-direction: column; gap: 8px; min-width: 0;
}

.call-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 12px 14px; text-align: right; cursor: pointer; width: 100%;
}
.call-row:hover { border-color: var(--primary); }
.call-row-title { font-weight: 600; font-size: 14px; }

.visit-row { border-inline-start: 3px solid var(--primary); padding-inline-start: 10px; }
.perm-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding: 8px 0; border-top: 1px solid var(--border); }
.perm-row:first-child { border-top: none; }
.perm-row-col { flex-direction: column; align-items: stretch; gap: 8px; min-width: 0; }
.perm-row-disabled { opacity: 0.55; }

/* ---------- Buttons ---------- */
.btn {
  border: none; border-radius: 10px; padding: 11px 16px; font-weight: 600; cursor: pointer;
}
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { background: #eef2ff; color: var(--primary-dark); }
.btn-ghost { background: transparent; color: var(--text); border: 1px solid var(--border); }
.btn-block { width: 100%; }
.btn-sm { padding: 7px 12px; font-size: 13px; }
.icon-btn { border: none; background: none; cursor: pointer; font-size: 14px; color: var(--muted); }

.fab {
  position: fixed; bottom: 84px; left: 20px; width: 54px; height: 54px; border-radius: 50%;
  background: var(--primary); color: white; border: none; font-size: 22px; cursor: pointer;
  box-shadow: 0 8px 20px rgba(29,78,216,0.4); z-index: 11;
}

/* ---------- Forms ---------- */
.field { display: flex; flex-direction: column; gap: 5px; flex: 1; }
.field span { font-size: 13px; color: var(--muted); }
.field input, .field select, .field textarea,
input, select, textarea {
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; background: var(--surface); color: var(--text);
}
textarea { resize: vertical; font-family: inherit; }
.field-row { display: flex; gap: 10px; flex-wrap: wrap; min-width: 0; max-width: 100%; }
.field-row > * { min-width: 140px; }
.search-input { width: 100%; }
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; }
.toolbar .search-input { flex: 1; min-width: 200px; }
.month-browser { display: flex; align-items: center; gap: 8px; }
.month-browser-input { min-width: 0; }
.qty-input { max-width: 70px; }
.part-add-row { display: flex; gap: 6px; align-items: center; }
.part-add-row input:first-child { flex: 1; }
.part-list { list-style: none; padding: 0; margin: 6px 0 0; display: flex; flex-direction: column; gap: 4px; }
.part-list li { display: flex; justify-content: space-between; background: #f9fafb; border-radius: 8px; padding: 6px 10px; font-size: 13px; }
.check { display: flex; align-items: center; gap: 6px; font-size: 13px; }

/* ---------- Notices / badges ---------- */
.notice { background: #fffbeb; border: 1px solid #fde68a; color: #92400e; border-radius: 10px; padding: 10px 12px; font-size: 13px; }
.notice-info { background: #eff6ff; border-color: #bfdbfe; color: #1e40af; }
.notice-warn { background: #fff7ed; border-color: #fed7aa; color: #9a3412; }
.notice-error { background: #fef2f2; border-color: #fecaca; color: #b91c1c; }
.badge { padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 700; white-space: nowrap; }

.chip-row { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; min-width: 0; max-width: 100%; }
.chip-row-stacked { flex-direction: column; overflow-x: visible; }
.chip-row-stacked .chip { white-space: normal; text-align: right; width: 100%; }
.chip { border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 6px 14px; font-size: 13px; white-space: nowrap; cursor: pointer; }
.chip-active { background: var(--primary); color: white; border-color: var(--primary); }
.chip-static { cursor: default; background: #f9fafb; }

/* ---------- Printable barcode stickers (inventory) ---------- */
.label-sheet { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
@media (max-width: 600px) { .label-sheet { grid-template-columns: repeat(2, 1fr); } }
.label-card {
  border: 1px dashed var(--border); border-radius: 8px; padding: 8px; text-align: center;
  display: flex; flex-direction: column; align-items: center; gap: 2px; break-inside: avoid;
}
.label-card svg { max-width: 100%; }
.label-sku { font-weight: 700; font-size: 13px; }
.label-name { font-size: 12px; }
.label-location { font-size: 11px; color: var(--muted); }

.check-row { display: flex; gap: 14px; flex-wrap: nowrap; }

.empty-state { text-align: center; color: var(--muted); padding: 40px 10px; }
.empty-icon { font-size: 34px; margin-bottom: 8px; }

.kpi-row { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
@media (min-width: 600px) { .kpi-row { grid-template-columns: repeat(4, 1fr); } }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; text-align: center; }
.kpi-num { font-size: 22px; font-weight: 800; }

.bar-row { display: grid; grid-template-columns: 120px 1fr 90px; align-items: center; gap: 8px; font-size: 13px; }
.bar-track { background: #f3f4f6; border-radius: 999px; height: 10px; overflow: hidden; }
.bar-fill { background: var(--primary); height: 100%; }
.bar-value { text-align: left; font-weight: 600; }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(17,24,39,0.55); z-index: 100;
  display: flex; align-items: flex-end; justify-content: center;
}
.modal-box {
  background: var(--surface); width: 100%; max-width: 560px; max-height: 92vh; overflow-y: auto;
  border-radius: 16px 16px 0 0; padding: 0;
}
@media (min-width: 640px) {
  .modal-overlay { align-items: center; }
  .modal-box { border-radius: 16px; margin: 20px; }
}
.modal-wide { max-width: 720px; }
.modal-header {
  position: sticky; top: 0; background: var(--surface); display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--border);
}
.modal-header h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 16px; }

/* ---------- Photo capture ---------- */
.photo-list { display: flex; gap: 8px; flex-wrap: wrap; }
.photo-thumb { position: relative; width: 76px; height: 76px; border-radius: 10px; overflow: hidden; border: 1px solid var(--border); }
.photo-thumb img { width: 100%; height: 100%; object-fit: cover; }
.photo-remove { position: absolute; top: 2px; left: 2px; background: rgba(0,0,0,0.6); color: white; border: none; border-radius: 50%; width: 20px; height: 20px; cursor: pointer; font-size: 11px; }
.photo-add {
  width: 76px; height: 76px; border: 2px dashed var(--border); border-radius: 10px; background: none;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 20px; cursor: pointer; color: var(--muted);
}
.photo-add span { font-size: 10px; }
.photo-view { width: 100%; max-width: 200px; border-radius: 10px; border: 1px solid var(--border); }

/* ---------- Signature ---------- */
.sigpad { display: flex; flex-direction: column; gap: 10px; }
.sigpad-canvas { width: 100%; height: 180px; background: #fafafa; border: 1px dashed var(--border); border-radius: 10px; touch-action: none; }
.sigpad-actions { display: flex; gap: 8px; }
.sigpad-actions .btn { flex: 1; }
.sign-img { max-width: 200px; border: 1px solid var(--border); border-radius: 8px; background: white; }

/* ---------- Report / invoice document ---------- */
.report-modal-actions { display: flex; gap: 10px; margin-bottom: 14px; }
.report-doc { position: relative; background: white; padding: 10px; font-size: 13px; }
.report-watermark {
  position: absolute; inset: 0; z-index: 0; pointer-events: none;
  background-repeat: repeat; background-size: 130px auto; opacity: 0.06;
  -webkit-print-color-adjust: exact; print-color-adjust: exact;
}
.report-body { position: relative; z-index: 1; }
.report-head { display: flex; justify-content: space-between; align-items: flex-start; border-bottom: 2px solid var(--primary); padding-bottom: 10px; margin-bottom: 14px; }
.report-head h1 { margin: 0; font-size: 20px; }
.report-head h2 { margin: 0; font-size: 16px; }
.report-head-brand { display: flex; flex-direction: column; gap: 2px; }
.report-title { text-align: left; font-size: 13px; }
.report-info, .report-table, .report-summary { width: 100%; border-collapse: collapse; margin-bottom: 14px; }
.report-info td { padding: 5px 6px; border-bottom: 1px solid var(--border); }
.report-info td:first-child { color: var(--muted); width: 140px; }
.report-table th, .report-table td { border: 1px solid var(--border); padding: 6px 8px; text-align: right; font-size: 12px; }
.report-table th { background: #f3f4f6; }
.report-kpi-table th, .report-kpi-table td { text-align: center; }
.report-kpi-table td { font-weight: 800; font-size: 16px; color: var(--primary-dark); }
.table-input { width: 100%; border: 1px solid transparent; background: transparent; padding: 4px; border-radius: 6px; font: inherit; color: inherit; }
.table-input:hover { border-color: var(--border); }
.table-input:focus { border-color: var(--primary); background: var(--surface); outline: none; }
.report-summary td { padding: 5px 8px; }
.report-summary .total-row td { font-weight: 800; font-size: 15px; border-top: 2px solid var(--primary); padding-top: 8px; }
.report-visit-block { border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; margin-bottom: 8px; break-inside: avoid; }
.report-visit-head { font-size: 13px; font-weight: 600; margin-bottom: 4px; }
.report-visit-notes { font-size: 12px; margin-bottom: 4px; }
.report-visit-parts { font-size: 12px; }
.report-visit-parts > div:first-child { color: var(--text); margin-bottom: 2px; }
.report-visit-photos { font-size: 12px; margin-top: 6px; break-inside: avoid; }
.report-photo-grid { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 4px; }
.report-photo { width: 180px; max-height: 180px; object-fit: cover; border: 1px solid var(--border); border-radius: 6px; }
.report-parts-list { margin: 0; padding-inline-start: 18px; color: var(--muted); }
.report-parts-list li { margin-bottom: 2px; }
.report-sign { margin-top: 20px; }
.report-page-break { break-after: page; page-break-after: always; }
.report-embedded { border-top: 2px dashed var(--border); margin-top: 20px; padding-top: 16px; break-inside: avoid; }
.report-embedded + .report-embedded { break-before: page; page-break-before: always; }

.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* ---------- Print ---------- */
@media print {
  body * { visibility: hidden; }
  #print-area, #print-area * { visibility: visible; }
  #print-area { position: absolute; top: 0; right: 0; left: 0; width: 100%; }
  .modal-overlay, .modal-box {
    position: static !important; overflow: visible !important;
    max-height: none !important; height: auto !important; width: auto !important;
  }
  .no-print { display: none !important; }
  .report-photo, .report-visit-photos { break-inside: avoid; page-break-inside: avoid; }
}

/* ---------- QR part stickers (size is set in js/inventory.js -> STICKER) ---------- */
.sticker-sheet { display: flex; flex-wrap: wrap; gap: 10px; }
.part-sticker {
  position: relative; background: #fff; color: #000; overflow: hidden; box-sizing: border-box;
  border: 1px dashed var(--border); font-family: Arial, Helvetica, sans-serif;
}
.part-sticker svg { width: 100%; height: 100%; display: block; }
@media print {
  .sticker-sheet { display: block; gap: 0; }
  .part-sticker { border: none; break-after: page; page-break-after: always; }
}

/* ---------- Sticker printing: only the stickers are printed (no blank extra pages) ----------
   The print button copies the stickers into #sticker-print-root and hides everything else. */
#sticker-print-root { display: none; }
@media print {
  body.stickers-only > *:not(#sticker-print-root) { display: none !important; }
  body.stickers-only #sticker-print-root { display: block !important; position: static; }
  #sticker-print-root, #sticker-print-root * { visibility: visible; }
  html, body.stickers-only { height: auto !important; min-height: 0 !important; margin: 0 !important; padding: 0 !important; background: #fff !important; }
  #sticker-print-root .part-sticker:last-child { break-after: auto; page-break-after: auto; }
}
