:root {
  --primary: #1e3a8a;
  --primary-hover: #1d4ed8;
  --primary-light: #eff6ff;
  --secondary: #0f766e;
  --bg-main: #f8fafc;
  --bg-card: #ffffff;
  --border: #e2e8f0;
  --text-main: #0f172a;
  --text-muted: #64748b;
  --success: #16a34a;
  --warning: #d97706;
  --danger: #dc2626;
  --radius: 8px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

body {
  background-color: var(--bg-main);
  color: var(--text-main);
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* Layout */
.app-container {
  display: flex;
  width: 100vw;
  height: 100vh;
}

/* Sidebar */
.sidebar {
  width: 250px;
  background-color: #0f172a;
  color: #fff;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  border-right: 1px solid #1e293b;
}

.sidebar-header {
  padding: 18px 20px;
  display: flex;
  align-items: center;
  gap: 12px;
  background-color: #020617;
  border-bottom: 1px solid #1e293b;
}

.sidebar-header img {
  height: 38px;
  border-radius: 6px;
  background: white;
  padding: 2px;
}

.sidebar-header .title-box h1 {
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0.5px;
}

.sidebar-header .title-box span {
  font-size: 11px;
  color: #94a3b8;
}

.sidebar-nav {
  flex: 1;
  padding: 16px 12px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  color: #94a3b8;
  text-decoration: none;
  font-size: 13.5px;
  font-weight: 500;
  border-radius: var(--radius);
  cursor: pointer;
  transition: all 0.15s ease;
}

.nav-item:hover {
  background-color: #1e293b;
  color: #fff;
}

.nav-item.active {
  background-color: #2563eb;
  color: #fff;
  font-weight: 600;
}

.nav-icon {
  font-size: 16px;
  width: 20px;
  text-align: center;
}

.sidebar-footer {
  padding: 14px 16px;
  background-color: #020617;
  border-top: 1px solid #1e293b;
  font-size: 11px;
  color: #64748b;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

/* Main Content Area */
.main-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* Header */
.top-header {
  height: 64px;
  background-color: #fff;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.company-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: #f1f5f9;
  border-radius: var(--radius);
  font-size: 12.5px;
  font-weight: 600;
  color: #334155;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.sync-status-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 14px;
  border-radius: 9999px;
  font-size: 12px;
  font-weight: 600;
}

.sync-status-badge.online {
  background-color: #dcfce7;
  color: #166534;
  border: 1px solid #bbf7d0;
}

.sync-status-badge.offline {
  background-color: #fee2e2;
  color: #991b1b;
  border: 1px solid #fecaca;
}

.sync-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}
.sync-dot.online { background-color: #16a34a; }
.sync-dot.offline { background-color: #dc2626; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 8px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid transparent;
  transition: all 0.15s;
}

.btn-primary {
  background-color: #2563eb;
  color: white;
}
.btn-primary:hover { background-color: #1d4ed8; }

.btn-secondary {
  background-color: #f1f5f9;
  color: #334155;
  border-color: #cbd5e1;
}
.btn-secondary:hover { background-color: #e2e8f0; }

.btn-sm {
  padding: 5px 10px;
  font-size: 12px;
}

.user-profile {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-left: 12px;
  border-left: 1px solid var(--border);
}

.user-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background-color: #2563eb;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 13px;
}

.user-info .user-name {
  font-size: 13px;
  font-weight: 600;
  color: #0f172a;
}
.user-info .user-role {
  font-size: 11px;
  color: #64748b;
}

/* Page Content */
.content-area {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
  background-color: var(--bg-main);
}

.page-section {
  display: none;
}
.page-section.active {
  display: block;
}

/* Cards & Grid */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: white;
  padding: 18px 20px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

.stat-card .label {
  font-size: 12.5px;
  color: var(--text-muted);
  font-weight: 500;
}

.stat-card .value {
  font-size: 26px;
  font-weight: 700;
  color: var(--text-main);
  margin-top: 4px;
}

/* Tables & Controls */
.card {
  background: white;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
  overflow: hidden;
}

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

.card-header h2 {
  font-size: 16px;
  font-weight: 600;
}

.table-controls {
  display: flex;
  align-items: center;
  gap: 12px;
}

.search-input {
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  min-width: 280px;
  outline: none;
}
.search-input:focus {
  border-color: #2563eb;
  box-shadow: 0 0 0 3px rgba(37,99,235,0.1);
}

.table-responsive {
  width: 100%;
  overflow-x: auto;
}

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

th {
  background-color: #f8fafc;
  color: #475569;
  font-weight: 600;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

td {
  padding: 12px 16px;
  border-bottom: 1px solid #f1f5f9;
  color: #1e293b;
  vertical-align: middle;
}

tr:hover td {
  background-color: #f8fafc;
}

.badge {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 9999px;
  font-size: 11px;
  font-weight: 600;
}

.badge-success { background: #dcfce7; color: #166534; }
.badge-warning { background: #fef3c7; color: #92400e; }
.badge-danger { background: #fee2e2; color: #991b1b; }
.badge-info { background: #e0f2fe; color: #075985; }
.badge-secondary { background: #f1f5f9; color: #475569; }

.subnav-pills {
  display: flex;
  background: #e2e8f0;
  border-radius: 6px;
  padding: 2px;
  gap: 2px;
}
.pill-btn {
  border: none;
  background: transparent;
  padding: 4px 12px;
  font-size: 12px;
  font-weight: 600;
  color: #64748b;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s ease;
}
.pill-btn.active {
  background: #ffffff;
  color: #1e3a8a;
  box-shadow: 0 1px 2px rgba(0,0,0,0.08);
}
.fac-row-selected {
  background-color: #e0f2fe !important;
  font-weight: 600;
}

/* Modals */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(15, 23, 42, 0.6);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: all 0.2s;
}

.modal-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

.modal-content {
  background: white;
  border-radius: 12px;
  width: 90%;
  max-width: 800px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 25px -5px rgba(0,0,0,0.2);
  overflow: hidden;
}

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

.modal-body {
  padding: 24px;
  overflow-y: auto;
  flex: 1;
}

.modal-footer {
  padding: 16px 24px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  background-color: #f8fafc;
}

.form-group {
  margin-bottom: 16px;
}
.form-group label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: #334155;
  margin-bottom: 6px;
}
.form-control {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13.5px;
  outline: none;
}
.form-control:focus {
  border-color: #2563eb;
  box-shadow: 0 0 0 3px rgba(37,99,235,0.1);
}

.pagination {
  padding: 14px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-top: 1px solid var(--border);
  font-size: 12.5px;
  color: var(--text-muted);
}

/* Searchable Select (universal combobox) */
.ss-native-hidden {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  opacity: 0 !important;
  pointer-events: none !important;
  margin: 0 !important;
  padding: 0 !important;
  border: 0 !important;
}

.ss-wrapper {
  position: relative;
  display: inline-block;
  width: 100%;
  min-width: 0;
  vertical-align: middle;
}

.ss-trigger {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #ffffff;
  color: var(--text-main);
  font-size: 13.5px;
  text-align: left;
  cursor: pointer;
  min-height: 36px;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.ss-trigger:hover:not(:disabled) {
  border-color: #94a3b8;
}

.ss-trigger:focus {
  outline: none;
  border-color: #2563eb;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.ss-trigger:disabled {
  background: #f1f5f9;
  color: var(--text-muted);
  cursor: not-allowed;
}

.ss-trigger-label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ss-trigger-caret {
  font-size: 11px;
  color: var(--text-muted);
  flex-shrink: 0;
}

.ss-dropdown {
  position: fixed;
  z-index: 99999;
  background: #ffffff;
  border: 1px solid #cbd5e1;
  border-radius: var(--radius);
  box-shadow: 0 12px 28px rgba(15, 23, 42, 0.22);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 220px;
}

.ss-search-box {
  padding: 6px;
  background: #f8fafc;
  border-bottom: 1px solid var(--border);
}

.ss-search {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 13px;
  outline: none;
  background: #ffffff;
}

.ss-search:focus {
  border-color: #2563eb;
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.12);
}

.ss-list {
  max-height: 250px;
  overflow-y: auto;
  padding: 4px;
}

.ss-item {
  padding: 7px 10px;
  font-size: 13px;
  border-radius: 5px;
  cursor: pointer;
  color: var(--text-main);
  white-space: normal;
  word-break: break-word;
}

.ss-item:hover,
.ss-item.ss-active {
  background: var(--primary-light);
  color: var(--primary);
}

.ss-item.ss-selected {
  font-weight: 600;
  background: #dbeafe;
  color: #1e3a8a;
}

.ss-empty,
.ss-hint {
  padding: 8px 10px;
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
}

/* Items / Partidas tables inside modals: prevent numeric inputs from collapsing */
.modal-content {
  width: 95%;
}

.items-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: auto;
}

.items-table th,
.items-table td {
  padding: 6px 5px !important;
  vertical-align: middle;
}

.items-table .form-control,
.items-table .ss-trigger {
  padding: 6px 7px;
  font-size: 13px;
  min-height: 32px;
}

.items-table input[type="number"].form-control {
  min-width: 66px;
  text-align: right;
  padding: 6px 6px;
}

.items-table .ord-item-qty,
.items-table .ent-item-qty,
.items-table .fac-prod-qty,
.items-table .cot-prod-qty {
  min-width: 78px !important;
}

.items-table .ord-item-price,
.items-table .ent-item-price,
.items-table .fac-prod-price,
.items-table .cot-prod-price {
  min-width: 102px !important;
}

.items-table .ord-item-prod,
.items-table .ent-item-prod,
.items-table .fac-prod-sel,
.items-table .cot-prod-sel {
  min-width: 210px;
}

/* Remove number spinner arrows so digits have full width */
.items-table input[type="number"]::-webkit-inner-spin-button,
.items-table input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
.items-table input[type="number"] {
  -moz-appearance: textfield;
}

/* Mobile Drawer & Touch-Friendly Responsive Layout */
.mobile-menu-btn {
  display: none;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #f8fafc;
  color: #0f172a;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
  touch-action: manipulation;
}

.sidebar-backdrop {
  display: none;
}

.btn {
  touch-action: manipulation;
}

.table-responsive {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

@media (max-width: 900px) {
  body,
  .app-container,
  .main-wrapper {
    height: 100dvh;
    width: 100%;
    max-width: 100vw;
    overflow-x: hidden;
  }

  .mobile-menu-btn {
    display: inline-flex;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 265px;
    z-index: 1200;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.35);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-backdrop.active {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.55);
    z-index: 1150;
  }

  .top-header {
    height: auto;
    min-height: 56px;
    padding: 8px 12px;
    flex-wrap: wrap;
    gap: 8px;
  }

  .header-left {
    flex: 1 1 auto;
    min-width: 0;
    gap: 8px;
  }

  .company-badge {
    min-width: 0;
    padding: 5px 8px;
    font-size: 11.5px;
  }

  .company-badge span:last-child {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .header-right {
    width: 100%;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 8px;
  }

  .sync-status-badge {
    font-size: 11px;
    padding: 4px 10px;
  }

  .content-area {
    padding: 12px;
    -webkit-overflow-scrolling: touch;
  }

  .stats-grid,
  .dashboard-grid {
    grid-template-columns: 1fr !important;
    gap: 12px !important;
  }

  .toolbar,
  .card-header,
  .pagination {
    flex-wrap: wrap;
    gap: 8px;
    padding: 12px;
  }

  .search-input {
    width: 100% !important;
    min-width: 0 !important;
    flex: 1 1 100%;
  }

  .btn {
    min-height: 38px;
    padding: 8px 12px;
    font-size: 13px;
  }

  .btn-sm {
    min-height: 34px;
    padding: 6px 10px;
    font-size: 12px;
  }

  td .btn,
  td .btn-sm {
    white-space: nowrap;
  }
}

@media (max-width: 768px) {
  .modal-content {
    width: 96% !important;
    max-width: 96vw !important;
    max-height: 92dvh !important;
    border-radius: 10px;
  }

  .modal-header {
    padding: 12px 14px;
  }

  .modal-header h2 {
    font-size: 16px;
  }

  .modal-body {
    padding: 14px;
    -webkit-overflow-scrolling: touch;
  }

  .modal-footer {
    padding: 12px 14px;
    flex-wrap: wrap;
    gap: 8px;
  }

  .modal-footer .btn {
    flex: 1 1 auto;
    min-height: 42px;
    justify-content: center;
  }

  /* Stack multi-column inline grids cleanly on phones */
  .modal-body [style*="grid-template-columns"],
  .content-area [style*="grid-template-columns"] {
    grid-template-columns: 1fr !important;
    gap: 10px !important;
  }

  .cli-tab-btn {
    flex: 1 1 auto;
    text-align: center;
  }

  .ss-dropdown {
    max-width: calc(100vw - 16px) !important;
  }

  /* Prevent iOS auto-zoom on input focus */
  input.form-control,
  select.form-control,
  textarea.form-control {
    font-size: 16px;
  }
}
