/* ── Reset & Base ── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --primary: #1e40af;
  --primary-light: #3b82f6;
  --primary-dark: #1e3a8a;
  --success: #059669;
  --warning: #d97706;
  --danger: #dc2626;
  --gray-50: #f9fafb;
  --gray-100: #f3f4f6;
  --gray-200: #e5e7eb;
  --gray-300: #d1d5db;
  --gray-400: #9ca3af;
  --gray-500: #6b7280;
  --gray-600: #4b5563;
  --gray-700: #374151;
  --gray-800: #1f2937;
  --gray-900: #111827;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.07), 0 2px 4px rgba(0,0,0,0.06);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--gray-100);
  color: var(--gray-800);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ── Header ── */
.app-header {
  background: var(--primary-dark);
  color: white;
  padding: 0 1.5rem;
  box-shadow: var(--shadow-md);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 60px;
}

.brand-link {
  text-decoration: none;
  color: inherit;
}

.brand-name {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.brand-tagline {
  font-size: 0.85rem;
  font-weight: 400;
  opacity: 0.8;
  margin-left: 0.5rem;
}

.main-nav {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.nav-link {
  color: rgba(255,255,255,0.85);
  text-decoration: none;
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius);
  font-size: 0.875rem;
  transition: background 0.15s;
}

.nav-link:hover {
  background: rgba(255,255,255,0.15);
  color: white;
}

.nav-user {
  color: rgba(255,255,255,0.7);
  font-size: 0.8rem;
  padding: 0 0.5rem;
}

.nav-logout {
  color: rgba(255,255,255,0.6);
}

/* ── Main Content ── */
.main-content {
  flex: 1;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
  padding: 1.5rem;
}

/* ── Footer ── */
.app-footer {
  text-align: center;
  padding: 1rem;
  color: var(--gray-400);
  font-size: 0.8rem;
  border-top: 1px solid var(--gray-200);
  margin-top: auto;
}

.footer-contact {
  color: var(--gray-500);
  font-size: 0.85rem;
  margin-bottom: 0.25rem;
}
.footer-link {
  color: var(--primary);
  text-decoration: none;
}
.footer-link:hover {
  text-decoration: underline;
}

/* ── Cards ── */
.card {
  background: white;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.5rem;
}

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.625rem 1.25rem;
  border: none;
  border-radius: var(--radius);
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  text-decoration: none;
  line-height: 1.4;
}

.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.6; cursor: not-allowed; }

.btn-primary {
  background: var(--primary);
  color: white;
}
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); }

.btn-secondary {
  background: var(--gray-200);
  color: var(--gray-700);
}
.btn-secondary:hover:not(:disabled) { background: var(--gray-300); }

.btn-full { width: 100%; }

.btn-sm {
  padding: 0.375rem 0.75rem;
  font-size: 0.8rem;
}

.role-select {
  padding: 0.3rem 0.5rem;
  font-size: 0.85rem;
  border: 1px solid var(--gray-300);
  border-radius: var(--radius);
  background: white;
  cursor: pointer;
}

.inline-edit {
  padding: 0.3rem 0.5rem;
  font-size: 0.85rem;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: transparent;
  width: 100%;
  min-width: 120px;
}
.inline-edit:hover {
  border-color: var(--gray-300);
  background: white;
}
.inline-edit:focus {
  border-color: var(--primary-light);
  background: white;
  outline: none;
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

/* ── Forms ── */
.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--gray-600);
  margin-bottom: 0.375rem;
}

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 0.625rem 0.75rem;
  border: 1px solid var(--gray-300);
  border-radius: var(--radius);
  font-size: 0.9rem;
  transition: border-color 0.15s;
  background: white;
}

.form-group input:focus,
.form-group select:focus {
  outline: none;
  border-color: var(--primary-light);
  box-shadow: 0 0 0 3px rgba(59,130,246,0.15);
}

.form-row {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
}

.form-row .form-group {
  flex: 1;
  min-width: 150px;
}

.form-group-sm {
  max-width: 120px;
}

.form-group-btn {
  display: flex;
  align-items: flex-end;
  max-width: fit-content;
}

/* ── Tables ── */
.table-wrapper {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

thead th {
  text-align: left;
  padding: 0.75rem;
  background: var(--gray-50);
  color: var(--gray-600);
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  border-bottom: 2px solid var(--gray-200);
}

tbody td {
  padding: 0.625rem 0.75rem;
  border-bottom: 1px solid var(--gray-100);
}

tbody tr:hover {
  background: var(--gray-50);
}

.row-active {
  background: #eff6ff !important;
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.table-controls {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
}

.table-controls select {
  padding: 0.375rem 0.5rem;
  border: 1px solid var(--gray-300);
  border-radius: var(--radius);
  font-size: 0.85rem;
}

.loading, .empty {
  text-align: center;
  color: var(--gray-400);
  padding: 2rem !important;
}

.code-cell {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--primary);
}

.details-cell {
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.75rem;
  color: var(--gray-500);
}

/* ── Badges ── */
.badge {
  display: inline-block;
  padding: 0.2rem 0.6rem;
  border-radius: 9999px;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.badge-pending { background: #fef3c7; color: #92400e; }
.badge-opened { background: #d1fae5; color: #065f46; }
.badge-validated { background: #dbeafe; color: #1e40af; }
.badge-expired { background: #fee2e2; color: #991b1b; }
.badge-gate { background: #dbeafe; color: #1e40af; }
.badge-auth { background: #ede9fe; color: #5b21b6; }
.badge-config { background: #fef3c7; color: #92400e; }
.badge-system { background: var(--gray-100); color: var(--gray-600); }

/* ── Pagination ── */
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  margin-top: 1rem;
  font-size: 0.85rem;
}

.page-info {
  color: var(--gray-500);
}

/* ── Guest Page ── */
.guest-container {
  max-width: 480px;
}

.lang-buttons {
  display: flex;
  gap: 0.5rem;
  justify-content: center;
  margin-bottom: 1rem;
}
.lang-btn {
  padding: 0.5rem 1rem;
  border: 2px solid var(--gray-300);
  border-radius: var(--radius);
  background: white;
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--gray-600);
  cursor: pointer;
  transition: all 0.15s;
  flex: 1;
}
.lang-btn:hover {
  border-color: var(--primary);
  color: var(--primary);
}
.lang-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

.guest-warning {
  background: #fef3c7;
  border: 2px solid #f59e0b;
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  margin-bottom: 1rem;
  font-size: 1rem;
  font-weight: 600;
  color: #92400e;
  text-align: center;
  line-height: 1.5;
}

.guest-card {
  text-align: center;
}

.guest-card h2 {
  margin-bottom: 0.5rem;
}

.gate-info {
  color: var(--gray-500);
  margin-bottom: 1.5rem;
}

.guest-card .form-group {
  text-align: left;
}

.access-code {
  font-size: 4rem;
  font-weight: 800;
  color: var(--primary);
  font-family: 'SF Mono', 'Fira Code', monospace;
  letter-spacing: 0.1em;
  margin: 1rem 0;
}

.result-section {
  margin-top: 1.5rem;
  padding: 1.5rem;
  background: #eff6ff;
  border-radius: var(--radius);
}

.expiry-msg {
  color: var(--gray-600);
  font-size: 0.9rem;
  margin-top: 0.5rem;
}

.countdown {
  font-weight: 700;
  font-family: 'SF Mono', 'Fira Code', monospace;
}

.expiry-msg.expired {
  color: var(--danger);
  font-weight: 600;
}

.approved-section {
  margin-top: 1.5rem;
  padding: 1.5rem;
  background: #f0fdf4;
  border-radius: var(--radius);
  text-align: center;
}
.approved-icon {
  font-size: 3rem;
  color: var(--success);
  margin-bottom: 0.5rem;
}
.approved-section h3 {
  color: var(--success);
  margin-bottom: 0.75rem;
}
.approved-section p {
  color: var(--gray-600);
  line-height: 1.5;
}
.redirect-msg {
  margin-top: 1rem;
  font-size: 0.85rem;
  color: var(--gray-400);
}
.error-section {
  margin-top: 1rem;
  padding: 1rem;
  background: #fef2f2;
  border-radius: var(--radius);
  color: var(--danger);
}

/* ── 2N Call Status Bar ── */
.call-status-bar {
  background: white;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem 1.25rem;
}

.call-status-title {
  font-size: 0.85rem;
  color: var(--gray-500);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 0.75rem;
}

.call-status-gates {
  display: flex;
  gap: 0.75rem;
}

.call-gate-card {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
  padding: 0.75rem;
  border-radius: var(--radius);
  border: 2px solid var(--gray-200);
  background: var(--gray-50);
  transition: border-color 0.3s, background 0.3s;
}

.call-gate-label {
  font-weight: 700;
  font-size: 0.9rem;
  color: var(--gray-700);
}

.call-gate-status {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--gray-400);
}

.call-gate-card.call-idle .call-gate-status {
  color: var(--gray-400);
}

.call-gate-card.call-ringing {
  border-color: #f59e0b;
  background: #fef3c7;
  animation: call-pulse 1.5s ease-in-out infinite;
}

.call-gate-card.call-ringing .call-gate-status {
  color: #92400e;
}

.call-gate-card.call-connected {
  border-color: var(--success);
  background: #ecfdf5;
}

.call-gate-card.call-connected .call-gate-status {
  color: #065f46;
}

.call-gate-last {
  font-size: 0.7rem;
  color: var(--gray-400);
  font-style: italic;
}
.disconnected-banner {
  background: var(--danger);
  color: white;
  text-align: center;
  padding: 0.5rem 1rem;
  font-weight: 600;
  font-size: 0.85rem;
  position: sticky;
  top: 0;
  z-index: 100;
}

@keyframes call-pulse {
  0%, 100% { border-color: #f59e0b; }
  50% { border-color: #dc2626; }
}

@media (max-width: 768px) {
  .call-status-gates {
    flex-direction: column;
  }
}

/* ── Guard Page ── */
.guard-container {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.guard-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.5rem;
}

.action-section h3 {
  font-size: 1rem;
  color: var(--gray-700);
  margin-bottom: 0.75rem;
  padding-bottom: 0.5rem;
  border-bottom: 2px solid var(--gray-100);
}

.address-error {
  color: var(--danger);
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: var(--radius);
  padding: 0.625rem 1rem;
  font-size: 1rem;
  font-weight: 600;
  text-align: center;
  margin-bottom: 0.75rem;
}

.gate-buttons {
  display: flex;
  gap: 0.75rem;
}
.btn-gate-active {
  background: var(--success) !important;
  color: white !important;
}
.btn-gate-active:hover:not(:disabled) {
  background: #15803d !important;
}
.btn-gate-idle {
  background: var(--gray-300) !important;
  color: var(--gray-600) !important;
}
.btn-gate-idle:hover:not(:disabled) {
  background: var(--gray-400) !important;
}

.refresh-indicator {
  font-size: 0.75rem;
  color: var(--gray-400);
}

/* ── Active Codes List ── */
.active-codes-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
}

.active-codes-list .empty {
  width: 100%;
  padding: 1.5rem !important;
}

.code-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.35rem;
  padding: 1rem 1.25rem;
  background: #eff6ff;
  border: 2px solid #bfdbfe;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
  min-width: 150px;
}

.code-btn:hover:not(:disabled) {
  background: #dbeafe;
  border-color: var(--primary-light);
}

.code-btn:active:not(:disabled) {
  transform: scale(0.97);
}

.code-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.code-btn-code {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 1.75rem;
  font-weight: 800;
  color: var(--primary);
  line-height: 1;
}

.code-btn-timer {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--warning);
}

.code-btn-timer.expired {
  color: var(--danger);
}

.code-btn-name {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--gray-700);
  text-align: center;
}

.code-btn-address {
  font-size: 0.75rem;
  color: var(--gray-500);
  text-align: center;
}

/* ── Status Toast ── */
.status-toast {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  padding: 1rem 1.5rem;
  border-radius: var(--radius);
  font-weight: 600;
  font-size: 0.9rem;
  box-shadow: var(--shadow-md);
  z-index: 1000;
}

.status-toast.success {
  background: #ecfdf5;
  color: #065f46;
  border: 1px solid #a7f3d0;
}

.status-toast.error {
  background: #fef2f2;
  color: #991b1b;
  border: 1px solid #fecaca;
}

/* ── Honeypot ── */
.hp-field {
  position: absolute;
  left: -9999px;
  opacity: 0;
  height: 0;
  overflow: hidden;
}

/* ── Login Page ── */
.login-container {
  max-width: 400px;
  margin: 4rem auto;
}

.login-card {
  text-align: center;
}

.login-card h2 {
  margin-bottom: 0.5rem;
}

.login-card p {
  color: var(--gray-500);
  font-size: 0.9rem;
  margin-bottom: 1.5rem;
}

.login-divider {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin: 1.5rem 0;
  color: var(--gray-400);
  font-size: 0.85rem;
}

.login-divider::before,
.login-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--gray-200);
}

/* ── Config Page ── */
.config-container h2 {
  margin-bottom: 1rem;
}

.card.config-section {
  margin-top: 5px;
  margin-bottom: 5px;
}

.config-section h3 {
  font-size: 1rem;
  margin-bottom: 0.25rem;
}

.section-desc {
  font-size: 0.8rem;
  color: var(--gray-400);
  margin-bottom: 1rem;
}

.config-actions {
  margin-top: 1.5rem;
}

.config-status {
  margin-top: 1rem;
  padding: 1rem;
  border-radius: var(--radius);
  font-weight: 600;
  font-size: 0.9rem;
}

.config-status.success {
  background: #ecfdf5;
  color: #065f46;
}

.config-status.error {
  background: #fef2f2;
  color: #991b1b;
}

.inline-test-status {
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0.375rem 0.5rem;
  border-radius: var(--radius);
  margin-bottom: 0.5rem;
}

.inline-test-status.success {
  background: #ecfdf5;
  color: #065f46;
}

.inline-test-status.error {
  background: #fef2f2;
  color: #991b1b;
}

.inline-test-status.loading {
  color: var(--gray-400);
}

.mqtt-status-table td, .mqtt-status-table th {
  vertical-align: top;
}

/* ── Admin Page ── */
.admin-container h2 {
  margin-bottom: 1rem;
}

.summary-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 1rem;
  margin-bottom: 1rem;
}

.summary-card {
  background: white;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
  text-align: center;
}

.summary-value {
  display: block;
  font-size: 2rem;
  font-weight: 800;
  color: var(--primary);
}

.summary-label {
  font-size: 0.8rem;
  color: var(--gray-500);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.filter-section {
  margin-bottom: 1rem;
}

.charts-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin-bottom: 1rem;
}

.chart-card {
  min-height: 250px;
}

.chart-card h3 {
  font-size: 0.9rem;
  color: var(--gray-600);
  margin-bottom: 0.75rem;
}

/* ── Setup Wizard ── */
.setup-container {
  max-width: 800px;
  margin: 0 auto;
  padding-bottom: 2rem;
}

.setup-welcome {
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  margin-bottom: 1rem;
}

.setup-welcome h2 {
  color: var(--primary-dark);
  margin-bottom: 0.5rem;
}

.setup-welcome p {
  color: var(--gray-600);
  font-size: 0.9rem;
}

.azure-notice {
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
  margin-bottom: 1rem;
  font-size: 0.85rem;
  color: #92400e;
}

.azure-notice code {
  background: rgba(0,0,0,0.06);
  padding: 0.1rem 0.3rem;
  border-radius: 3px;
  font-size: 0.8rem;
}

.setup-section {
  margin-bottom: 0.75rem;
}

.setup-section details {
  background: white;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.setup-section summary {
  padding: 1rem 1.25rem;
  font-weight: 700;
  font-size: 0.95rem;
  color: var(--gray-800);
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  user-select: none;
  transition: background 0.15s;
}

.setup-section summary:hover {
  background: var(--gray-50);
}

.setup-section summary::before {
  content: '\25B6';
  font-size: 0.7rem;
  color: var(--gray-400);
  transition: transform 0.2s;
}

.setup-section details[open] > summary::before {
  transform: rotate(90deg);
}

.setup-section summary::-webkit-details-marker {
  display: none;
}

.section-body {
  padding: 0 1.25rem 1.25rem;
}

.section-help {
  font-size: 0.85rem;
  color: var(--gray-500);
  margin-bottom: 1rem;
  line-height: 1.5;
}

.section-help a {
  color: var(--primary-light);
  text-decoration: none;
  font-weight: 600;
}

.section-help a:hover {
  text-decoration: underline;
}

.section-help-list {
  font-size: 0.85rem;
  color: var(--gray-500);
  margin: 0.5rem 0 1rem 1.25rem;
  line-height: 1.7;
}

.section-help-list li {
  margin-bottom: 0.15rem;
}

.section-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-top: 0.5rem;
}

.btn-test {
  background: white;
  color: var(--primary);
  border: 2px solid var(--primary-light);
  padding: 0.5rem 1rem;
  border-radius: var(--radius);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

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

.btn-test:active {
  transform: scale(0.98);
}

.test-result {
  font-size: 0.85rem;
  font-weight: 600;
  transition: opacity 0.2s;
}

.test-result.success {
  color: var(--success);
}

.test-result.error {
  color: var(--danger);
}

.test-result.loading {
  color: var(--gray-400);
}

.gate-row {
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--gray-100);
}

.gate-row:last-child {
  border-bottom: none;
}

.gate-row h4 {
  font-size: 0.85rem;
  color: var(--gray-600);
  margin-bottom: 0.5rem;
}

.setup-save {
  margin-top: 1.5rem;
}

.btn-lg {
  padding: 0.875rem 1.5rem;
  font-size: 1rem;
}

/* ── Address Autocomplete ── */
.address-dropdown {
  position: absolute;
  left: 0;
  right: 0;
  top: 100%;
  background: white;
  border: 1px solid var(--gray-300);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  max-height: 240px;
  overflow-y: auto;
  z-index: 200;
  box-shadow: var(--shadow-md);
}

.address-option {
  padding: 0.5rem 0.75rem;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.1s;
}

.address-option:hover,
.address-option.active {
  background: var(--gray-100);
  color: var(--primary-dark);
}

/* ── Responsive ── */
@media (max-width: 768px) {
  .header-content {
    flex-direction: column;
    height: auto;
    padding: 0.75rem 0;
    gap: 0.5rem;
  }

  .main-nav {
    flex-wrap: wrap;
    justify-content: center;
  }

  .guard-actions {
    grid-template-columns: 1fr;
  }

  .charts-grid {
    grid-template-columns: 1fr;
  }

  .form-row {
    flex-direction: column;
  }

  .form-row .form-group {
    min-width: auto;
  }

  .form-group-sm {
    max-width: none;
  }
}
