:root {
  --bg-dark: #0f172a;
  --bg-card: #1e293b;
  --border-color: #334155;
  --primary: #6366f1;
  --primary-hover: #4f46e5;
  --accent: #10b981;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --danger: #ef4444;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, sans-serif;
}

body {
  background-color: var(--bg-dark);
  color: var(--text-main);
  min-height: 100vh;
}

.login-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: #090d16;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.login-box {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 16px;
  padding: 40px;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.5);
}

.brand-logo {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 8px;
}

.login-subtitle {
  text-align: center;
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 24px;
}

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

/* Sidebar */
.sidebar {
  width: 260px;
  background-color: #0b0f19;
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  padding: 24px 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 32px;
  padding: 0 8px;
}

.logo-icon {
  width: 40px;
  height: 40px;
  background: linear-gradient(135deg, var(--primary), #a855f7);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 20px;
  color: white;
}

.brand-text h2 {
  font-size: 20px;
  font-weight: 700;
}

.cert-badge {
  font-size: 10px;
  color: var(--accent);
  background: rgba(16, 185, 129, 0.1);
  padding: 2px 6px;
  border-radius: 4px;
  border: 1px solid rgba(16, 185, 129, 0.2);
}

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

.menu-section {
  font-size: 10px;
  font-weight: 700;
  color: var(--text-muted);
  letter-spacing: 0.8px;
  margin: 16px 8px 4px 8px;
  text-transform: uppercase;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-radius: 8px;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  text-align: left;
}

.nav-item:hover, .nav-item.active {
  background-color: rgba(99, 102, 241, 0.15);
  color: var(--text-main);
}

.nav-item.active {
  color: var(--primary);
  font-weight: 600;
}

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

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--border-color);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: 700;
}

.user-info {
  display: flex;
  flex-direction: column;
}

.user-info .name {
  font-size: 13px;
  font-weight: 600;
}

.user-info .nif {
  font-size: 11px;
  color: var(--text-muted);
}

.btn-logout {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  margin-left: auto;
  display: flex;
  align-items: center;
}

.btn-logout:hover {
  color: var(--danger);
}

/* Main Content */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

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

.top-header h1 {
  font-size: 24px;
  font-weight: 700;
}

.status-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-muted);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.status-dot.online {
  background-color: var(--accent);
  box-shadow: 0 0 10px var(--accent);
}

.content-body {
  padding: 32px;
}

.tab-content {
  display: none;
}

.tab-content.active {
  display: block;
}

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

.stat-card {
  background: var(--bg-card);
  padding: 20px;
  border-radius: 12px;
  border: 1px solid var(--border-color);
}

.stat-card .label {
  font-size: 13px;
  color: var(--text-muted);
  display: block;
  margin-bottom: 8px;
}

.stat-card h3 {
  font-size: 26px;
  font-weight: 700;
}

.stat-card.accent {
  background: linear-gradient(135deg, rgba(99, 102, 241, 0.2), rgba(16, 185, 129, 0.2));
  border-color: rgba(99, 102, 241, 0.4);
}

.card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 24px;
}

.card h2, .card h3 {
  margin-bottom: 16px;
}

/* Table */
.table-container {
  width: 100%;
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
}

th, td {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-color);
  font-size: 14px;
}

th {
  color: var(--text-muted);
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

tr:hover {
  background-color: rgba(255, 255, 255, 0.02);
}

/* Forms */
.form-row {
  display: flex;
  gap: 16px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.form-group {
  flex: 1;
  min-width: 200px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 12px;
}

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

input, select {
  background-color: #0f172a;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 12px 14px;
  color: var(--text-main);
  font-size: 14px;
  outline: none;
}

input:focus, select:focus {
  border-color: var(--primary);
}

.line-row {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 12px;
}

.line-row .line-desc { flex: 3; }
.line-row .line-qty { flex: 1; }
.line-row .line-price { flex: 1; }
.line-row .line-vat { flex: 1.5; }

.btn {
  padding: 12px 20px;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  border: none;
  font-size: 14px;
  transition: all 0.2s ease;
}

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

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

.btn-full {
  width: 100%;
  margin-top: 16px;
}

.btn-secondary {
  background-color: transparent;
  border: 1px solid var(--border-color);
  color: var(--text-main);
}

.btn-secondary:hover {
  background-color: rgba(255, 255, 255, 0.05);
}

.btn-icon {
  background: transparent;
  border: none;
  color: var(--danger);
  font-size: 20px;
  cursor: pointer;
}

.error-msg {
  color: var(--danger);
  font-size: 13px;
  margin-top: 8px;
  text-align: center;
}

.demo-credentials {
  margin-top: 24px;
  padding-top: 16px;
  border-top: 1px solid var(--border-color);
  color: var(--text-muted);
  text-align: center;
  font-size: 12px;
}

/* Modal Moderno & Usabilidade */
.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(3, 7, 18, 0.75);
  backdrop-filter: blur(8px);
  align-items: center;
  justify-content: center;
  z-index: 10000;
  padding: 20px;
}

.modal-content {
  background: #1e293b;
  color: #f8fafc;
  width: 100%;
  max-width: 820px;
  border-radius: 16px;
  padding: 32px;
  position: relative;
  max-height: 85vh;
  overflow-y: auto;
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.7);
  animation: modalFadeIn 0.25s ease-out;
}

@keyframes modalFadeIn {
  from { opacity: 0; transform: scale(0.96) translateY(10px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

/* Custom Sleek Scrollbar */
.modal-content::-webkit-scrollbar {
  width: 6px;
}
.modal-content::-webkit-scrollbar-track {
  background: rgba(15, 23, 42, 0.5);
  border-radius: 10px;
}
.modal-content::-webkit-scrollbar-thumb {
  background: #475569;
  border-radius: 10px;
}
.modal-content::-webkit-scrollbar-thumb:hover {
  background: #6366f1;
}

.close-modal {
  position: absolute;
  top: 20px;
  right: 24px;
  font-size: 24px;
  font-weight: 700;
  cursor: pointer;
  color: #94a3b8;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.05);
  transition: all 0.2s ease;
}

.close-modal:hover {
  background: rgba(239, 68, 68, 0.2);
  color: #ef4444;
}

/* Header Left & Mobile Toggle */
.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.btn-menu-toggle {
  display: none;
  background: transparent;
  border: 1px solid var(--border-color);
  color: var(--text-main);
  padding: 6px 10px;
  border-radius: 8px;
  cursor: pointer;
}

.sidebar-backdrop {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  z-index: 99;
}

.sidebar-backdrop.active {
  display: block;
}

/* Mobile Layout Optimization */
html, body {
  overflow-x: hidden;
  max-width: 100vw;
}

.app-layout {
  max-width: 100vw;
  overflow-x: hidden;
}

.main-content {
  max-width: 100%;
  box-sizing: border-box;
}

/* Responsive Breakpoints (Tablet & Mobile) */
@media (max-width: 1024px) {
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 768px) {
  body {
    font-size: 13px;
  }

  .btn-menu-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 8px 12px;
    font-size: 14px;
    border-radius: 8px;
    background: rgba(99, 102, 241, 0.1);
    border: 1px solid rgba(99, 102, 241, 0.3);
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: -280px;
    height: 100vh;
    z-index: 100;
    transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 10px 0 25px rgba(0, 0, 0, 0.5);
    overflow-y: auto;
  }

  .sidebar.open {
    left: 0;
  }

  .main-content {
    width: 100%;
    padding: 0 !important;
  }

  .top-header {
    padding: 12px 16px;
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    background: #0b0f19;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  }

  .top-header h1 {
    font-size: 17px !important;
    font-weight: 700;
  }

  .header-left {
    width: 100%;
    justify-content: space-between;
  }

  .status-indicator {
    width: 100%;
    justify-content: space-between;
    font-size: 11px;
    gap: 8px !important;
  }

  #tenant-switcher-container {
    flex: 1;
    max-width: 100%;
  }

  #tenant-switcher-select {
    max-width: 200px;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
    padding: 4px 8px !important;
    font-size: 11.5px !important;
  }

  .content-body {
    padding: 14px 12px !important;
  }

  .card {
    padding: 16px !important;
    margin-bottom: 16px !important;
    border-radius: 12px;
  }

  .stats-grid {
    grid-template-columns: 1fr;
    gap: 12px;
    margin-bottom: 20px;
  }

  .stat-card {
    padding: 16px;
  }

  .stat-card h3 {
    font-size: 22px;
  }

  .form-row {
    flex-direction: column;
    gap: 12px;
  }

  .line-row {
    flex-wrap: wrap;
    background: rgba(255, 255, 255, 0.02);
    padding: 12px;
    border-radius: 8px;
    border: 1px solid var(--border-color);
  }

  .line-row .line-desc { flex: 100%; }
  .line-row .line-qty { flex: 1; }
  .line-row .line-price { flex: 1; }
  .line-row .line-vat { flex: 1.5; }

  /* Native Mobile Card Transformations for Tables */
  .table-container {
    overflow-x: visible !important;
    border: none !important;
    background: transparent !important;
  }

  .table-container table {
    width: 100% !important;
    min-width: 100% !important;
    display: block !important;
  }

  .table-container thead {
    display: none !important; /* Esconde os cabeçalhos das tabelas no ecrã do telemóvel */
  }

  .table-container tbody {
    display: flex !important;
    flex-direction: column !important;
    gap: 12px !important;
  }

  .table-container tr {
    display: flex !important;
    flex-direction: column !important;
    background: rgba(30, 41, 59, 0.75) !important;
    border: 1px solid rgba(255, 255, 255, 0.1) !important;
    border-radius: 14px !important;
    padding: 14px !important;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25) !important;
    gap: 8px !important;
  }

  .table-container tr:hover {
    background: rgba(30, 41, 59, 0.95) !important;
  }

  .table-container td {
    display: flex !important;
    justify-content: space-between !important;
    align-items: center !important;
    padding: 6px 0 !important;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05) !important;
    font-size: 13px !important;
    white-space: normal !important;
    text-align: right !important;
  }

  .table-container td:last-child {
    border-bottom: none !important;
    padding-top: 10px !important;
    justify-content: flex-end !important;
    gap: 8px !important;
  }

  /* Rótulos automáticos para cartões mobile */
  .table-container td::before {
    content: attr(data-label);
    font-weight: 600;
    color: #94a3b8;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    text-align: left;
    margin-right: 12px;
  }

  .tenant-controls-bar {
    flex-direction: column;
    align-items: stretch !important;
    gap: 12px !important;
  }

  .tenant-controls-row {
    width: 100% !important;
    justify-content: space-between !important;
    flex-wrap: wrap !important;
  }

  #tenants-grid-container {
    grid-template-columns: 1fr !important;
  }

  /* Modal e Tabs Responsivos no Mobile & Tablet */
  .modal {
    padding: 10px;
  }

  .modal-content {
    padding: 18px 14px;
    border-radius: 14px;
    max-height: 90vh;
    width: 100% !important;
    box-sizing: border-box;
  }

  .mtab-btn {
    font-size: 11px !important;
    padding: 8px 12px !important;
    white-space: nowrap;
    flex-shrink: 0;
  }
}

/* Benchmark Module Styles */
.stat-card-clickable {
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1) !important;
}
.stat-card-clickable:hover {
  transform: translateY(-4px);
  border-color: rgba(99, 102, 241, 0.5) !important;
  box-shadow: 0 12px 28px rgba(99, 102, 241, 0.25) !important;
}
.stat-card-clickable.active {
  border-color: #6366f1 !important;
  box-shadow: 0 0 20px rgba(99, 102, 241, 0.35) !important;
}

.th-sortable:hover {
  color: #f8fafc !important;
  background: rgba(99, 102, 241, 0.1) !important;
}

.batch-toolbar {
  animation: slideDownFade 0.25s ease-out;
}

@keyframes slideDownFade {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

.pagination-btn {
  padding: 6px 12px;
  border-radius: 8px;
  background: #0f172a;
  border: 1px solid #334155;
  color: #94a3b8;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}
.pagination-btn:hover:not(:disabled) {
  background: rgba(99, 102, 241, 0.2);
  color: #818cf8;
  border-color: rgba(99, 102, 241, 0.4);
}
.pagination-btn.active {
  background: #6366f1;
  color: white;
  border-color: #6366f1;
}
.pagination-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
