:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --border: #cbd5e1;
  --text: #020617;
  --text-muted: #334155;
  --primary: #0f766e;
  --primary-hover: #0d9488;
  --primary-soft: #ccfbf1;
  --success: #15803d;
  --success-bg: #dcfce7;
  --danger: #b91c1c;
  --danger-bg: #fee2e2;
  --warning: #b45309;
  --warning-bg: #fef3c7;
  --sidebar-bg: #0f172a;
  --sidebar-text: #e2e8f0;
  --radius: 10px;
  --font-size: 14px;
  --cell-pad-y: 8px;
  --cell-pad-x: 10px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
}

body.theme-dark {
  --bg: #0b1120;
  --surface: #111827;
  --surface-alt: #1e293b;
  --border: #334155;
  --text: #f8fafc;
  --text-muted: #cbd5e1;
  --primary-soft: rgba(20, 184, 166, 0.18);
  --success-bg: rgba(21, 128, 61, 0.22);
  --danger-bg: rgba(185, 28, 28, 0.22);
  --warning-bg: rgba(180, 83, 9, 0.22);
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
}

body.theme-dark input,
body.theme-dark select {
  background: #0f172a;
  color: var(--text);
}

body.theme-dark tbody tr:hover {
  background: rgba(255, 255, 255, 0.04);
}

body.density-compact {
  --cell-pad-y: 4px;
  --cell-pad-x: 8px;
}

body.density-spacious {
  --cell-pad-y: 12px;
  --cell-pad-x: 14px;
}

body.layout-topbar .app-shell {
  flex-direction: column;
}

body.layout-topbar .sidebar {
  width: 100%;
  height: auto;
  position: static;
  flex-direction: row;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  padding: 10px 18px;
}

body.layout-topbar .brand {
  border-bottom: none;
  padding: 0;
  margin: 0;
}

body.layout-topbar .nav {
  flex-direction: row;
  flex-wrap: wrap;
}

body.layout-topbar .sidebar-footer {
  border-top: none;
  padding-top: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

body.layout-topbar .sync-mini {
  margin-top: 0;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: 'Cairo', 'Segoe UI', Tahoma, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: var(--font-size);
  font-weight: 600;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 250px;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  padding: 18px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px 18px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  margin-bottom: 16px;
}

.brand-icon {
  font-size: 26px;
  background: rgba(20, 184, 166, 0.2);
  width: 42px;
  height: 42px;
  border-radius: 10px;
  display: grid;
  place-items: center;
}

.brand-title {
  font-weight: 800;
  font-size: 16px;
  color: #fff;
}

.brand-sub {
  font-size: 11px;
  color: #94a3b8;
}

.nav {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
}

.nav-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  background: transparent;
  border: none;
  color: var(--sidebar-text);
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  text-align: right;
  transition: 0.15s;
}

.nav-btn:hover {
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
}

.nav-btn.active {
  background: var(--primary);
  color: #fff;
}

.sidebar-footer {
  padding-top: 14px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.sync-mini {
  margin-top: 8px;
  font-size: 11px;
  color: #94a3b8;
  text-align: center;
}

.main-content {
  flex: 1;
  padding: 20px 24px;
  min-width: 0;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}

.topbar h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 800;
}

.tab-panel {
  display: none;
  flex-direction: column;
  gap: 16px;
}

.tab-panel.active {
  display: flex;
}

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

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.card-header.wrap {
  flex-wrap: wrap;
}

.card-header h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
}

.form-row {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  align-items: flex-end;
}

.form-row.compact {
  margin-bottom: 12px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 14px;
  margin-bottom: 16px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 140px;
  flex: 1;
}

.form-group.grow-2 {
  flex: 2;
  min-width: 240px;
}

.form-group label {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
}

input, select {
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #fff;
  color: var(--text);
  outline: none;
}

input:focus, select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(15, 118, 110, 0.15);
}

.btn {
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  padding: 8px 14px;
  border-radius: 7px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: 0.15s;
  white-space: nowrap;
}

.btn-block {
  width: 100%;
}

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

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

.btn-success {
  background: var(--success);
  color: #fff;
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.btn-outline {
  background: #fff;
  border-color: var(--border);
  color: var(--text);
}

.btn-outline:hover {
  background: var(--surface-alt);
}

.btn-sm {
  padding: 4px 8px;
  font-size: 12px;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(175px, 1fr));
  gap: 12px;
}

.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  box-shadow: var(--shadow);
}

.kpi-label {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 600;
}

.kpi-value {
  font-size: clamp(15px, 1.4vw, 19px);
  font-weight: 800;
  margin-top: 2px;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.kpi.danger .kpi-value {
  color: var(--danger);
}

.kpi.success .kpi-value {
  color: var(--success);
}

.kpi.warning .kpi-value {
  color: var(--warning);
}

.table-wrap {
  overflow-x: auto;
  max-height: 540px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

thead th {
  position: sticky;
  top: 0;
  background: var(--surface-alt);
  color: var(--text);
  font-weight: 800;
  text-align: right;
  padding: calc(var(--cell-pad-y) + 1px) var(--cell-pad-x);
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
  z-index: 1;
}

tbody td {
  padding: var(--cell-pad-y) var(--cell-pad-x);
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
  color: var(--text);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

input[type="number"] {
  font-variant-numeric: tabular-nums;
  direction: ltr;
  text-align: right;
}

tbody tr:hover {
  background: #f8fafc;
}

tbody tr.clickable {
  cursor: pointer;
}

tbody tr.selected-row {
  background: var(--primary-soft);
}

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  background: var(--surface-alt);
  border: 1px solid var(--border);
}

.badge-ok {
  background: var(--success-bg);
  color: var(--success);
  border-color: #bbf7d0;
}

.badge-danger {
  background: var(--danger-bg);
  color: var(--danger);
  border-color: #fecaca;
}

.badge-warn {
  background: var(--warning-bg);
  color: var(--warning);
  border-color: #fde68a;
}

.filter-bar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

.search-input {
  min-width: 220px;
}

.split-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.sub-panel h3 {
  margin: 0 0 10px;
  font-size: 14px;
  font-weight: 700;
}

.actions-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.sync-log {
  margin-top: 14px;
  padding: 12px;
  border-radius: 8px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  font-size: 13px;
}

.toast {
  position: fixed;
  bottom: 20px;
  left: 20px;
  background: #0f172a;
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  font-weight: 600;
  z-index: 100;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
}

.hidden {
  display: none !important;
}

.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
}

@media (max-width: 980px) {
  .app-shell {
    flex-direction: column;
  }
  .sidebar {
    width: 100%;
    height: auto;
    position: static;
  }
  .nav {
    flex-direction: row;
    flex-wrap: wrap;
  }
  .split-grid {
    grid-template-columns: 1fr;
  }
}

/* Design Customizer Drawer */
.customizer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  z-index: 90;
}

.customizer-drawer {
  position: fixed;
  top: 0;
  left: 0;
  width: 310px;
  max-width: 92vw;
  height: 100vh;
  background: var(--surface);
  color: var(--text);
  border-right: 1px solid var(--border);
  box-shadow: 0 12px 35px rgba(0, 0, 0, 0.25);
  z-index: 95;
  display: flex;
  flex-direction: column;
}

.customizer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.customizer-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 800;
}

.customizer-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.customizer-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

.cust-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.cust-group label {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
}

.seg-control {
  display: flex;
  gap: 4px;
  background: var(--surface-alt);
  padding: 4px;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.seg-control button {
  flex: 1;
  font-family: inherit;
  font-size: 12px;
  font-weight: 700;
  padding: 6px 8px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}

.seg-control button.active {
  background: var(--primary);
  color: #fff;
}

.color-swatches {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.swatch {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
}

.swatch.active {
  border-color: var(--text);
  transform: scale(1.1);
}

#cust-primary-picker {
  width: 32px;
  height: 28px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
}

/* Login Overlay */
.login-screen {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: grid;
  place-items: center;
  z-index: 200;
  padding: 16px;
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 24px;
  box-shadow: 0 20px 45px rgba(15, 23, 42, 0.14);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.login-logo {
  font-size: 36px;
  text-align: center;
}

.login-card h2 {
  margin: 0;
  text-align: center;
  font-size: 20px;
  font-weight: 800;
}

.login-sub {
  margin: -6px 0 4px;
  text-align: center;
  font-size: 12px;
  color: var(--text-muted);
}

.login-error {
  background: var(--danger-bg);
  color: var(--danger);
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 700;
  text-align: center;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.name-input-wide {
  min-width: 260px;
  width: 100%;
  text-overflow: ellipsis;
}

/* Stylish Floating Mirror Tooltip on Row Hover */
.row-mirror {
  position: fixed;
  z-index: 150;
  max-width: 480px;
  background: rgba(15, 23, 42, 0.94);
  backdrop-filter: blur(8px);
  color: #f8fafc;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  box-shadow: 0 14px 30px rgba(2, 6, 23, 0.32);
  pointer-events: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
  transition: opacity 0.12s ease;
}

.row-mirror-code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
  font-weight: 700;
  color: #5eead4;
  letter-spacing: 0.4px;
}

.row-mirror-name {
  font-size: 14px;
  font-weight: 700;
  line-height: 1.45;
  word-break: break-word;
}

.row-mirror-wh {
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px solid rgba(255, 255, 255, 0.14);
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 12px;
}

.row-mirror-wh-row {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  color: #e2e8f0;
}

.row-mirror-wh-row b {
  color: #5eead4;
}

.row-manual-mode {
  background: #fef9c3 !important;
}

body.theme-dark .row-manual-mode {
  background: rgba(234, 179, 8, 0.18) !important;
}

.row-unpriced-bom {
  background: #fee2e2 !important;
}

body.theme-dark .row-unpriced-bom {
  background: rgba(239, 68, 68, 0.22) !important;
}

.bom-hover-tooltip {
  position: fixed;
  z-index: 9999;
  max-width: 380px;
  background: #1e293b;
  color: #f8fafc;
  border: 1px solid #ef4444;
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 13px;
  line-height: 1.6;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.25);
  pointer-events: none;
  direction: rtl;
  text-align: right;
}

.bom-hover-tooltip .tt-title {
  font-weight: 700;
  color: #fca5a5;
  margin-bottom: 4px;
}

.item-usage-tooltip {
  position: fixed;
  z-index: 9999;
  max-width: 560px;
  max-height: 320px;
  overflow-y: auto;
  overflow-x: auto;
  background: #0f172a;
  color: #f8fafc;
  border: 1px solid var(--primary);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 12.5px;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.3);
  pointer-events: auto;
  direction: rtl;
  text-align: right;
  scrollbar-width: auto;
  scrollbar-color: #64748b #1e293b;
}

.item-usage-tooltip::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

.item-usage-tooltip::-webkit-scrollbar-track {
  background: #1e293b;
  border-radius: 6px;
}

.item-usage-tooltip::-webkit-scrollbar-thumb {
  background: #64748b;
  border-radius: 6px;
}

.item-usage-tooltip::-webkit-scrollbar-thumb:hover {
  background: #94a3b8;
}

.item-ac-popup {
  position: fixed;
  z-index: 10000;
  min-width: 320px;
  max-width: 540px;
  max-height: 270px;
  overflow-y: auto;
  background: var(--surface, #ffffff);
  color: var(--text);
  border: 1px solid var(--primary);
  border-radius: 8px;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.22);
  direction: rtl;
}

.item-ac-option {
  padding: 7px 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  border-bottom: 1px solid var(--border);
  font-size: 12.5px;
}

.item-ac-option:hover {
  background: rgba(15, 118, 110, 0.12);
}

.item-ac-option .ac-main {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.item-ac-option .ac-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.bom-cell-input {
  padding: 4px 7px;
  font-size: 12.5px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
}

.bom-cell-input.bom-code-inp {
  width: 118px;
}

.bom-cell-input.bom-name-inp {
  width: 100%;
  min-width: 190px;
}

.item-usage-tooltip .tt-title {
  font-weight: 700;
  color: #5eead4;
  margin-bottom: 6px;
}

.item-usage-tooltip table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  background: transparent !important;
  box-shadow: none !important;
}

.item-usage-tooltip thead th,
.item-usage-tooltip th {
  padding: 5px 8px;
  background: rgba(255, 255, 255, 0.08) !important;
  color: #cbd5e1 !important;
  font-weight: 600;
  border-bottom: 1px solid rgba(148, 163, 184, 0.28) !important;
  text-align: right;
  white-space: nowrap;
}

.item-usage-tooltip tbody td,
.item-usage-tooltip td {
  padding: 5px 8px;
  background: transparent !important;
  color: #f8fafc !important;
  border-bottom: 1px solid rgba(148, 163, 184, 0.16) !important;
  text-align: right;
  white-space: nowrap;
}

.item-usage-tooltip tbody tr {
  background: transparent !important;
}

.item-usage-tooltip tbody tr:hover,
.item-usage-tooltip tbody tr:hover td {
  background: rgba(255, 255, 255, 0.08) !important;
}
