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

:root {
  --primary: #25D366;
  --primary-dark: #128C7E;
  --secondary: #34B7F1;
  --success: #25D366;
  --warning: #FFC107;
  --error: #DC3545;
  --dark: #1a1a2e;
  --darker: #16213e;
  --light: #f8f9fa;
  --gray: #6c757d;
  --gray-light: #e9ecef;
  --border: #dee2e6;
  --text: #212529;
  --text-muted: #6c757d;
  --sidebar-width: 240px;
  --header-height: 60px;
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
  background-color: var(--light);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
  overflow-x: hidden;
}

/* Layout */
.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* Header/Sidebar - Mobile First */
.sidebar {
  width: 100%;
  background: var(--dark);
  color: white;
  position: relative;
  z-index: 1000;
}

.sidebar-header {
  padding: 15px 20px;
  background: var(--darker);
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid rgba(255,255,255,0.1);
}

.sidebar-header h2 {
  font-size: 1.1rem;
  color: var(--primary);
}

/* Mobile Menu Toggle */
.menu-toggle {
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 5px;
}

.menu-toggle span {
  display: block;
  width: 24px;
  height: 2px;
  background: white;
  transition: all 0.3s;
}

.nav-menu {
  list-style: none;
  padding: 0;
  display: none;
  flex-direction: column;
}

.nav-menu.show {
  display: flex;
}

.nav-menu li {
  border-bottom: 1px solid rgba(255,255,255,0.05);
}

.nav-menu li a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 20px;
  color: rgba(255,255,255,0.8);
  text-decoration: none;
  transition: all 0.3s;
  font-size: 0.95rem;
}

.nav-menu li a:hover,
.nav-menu li.active a {
  background: rgba(37, 211, 102, 0.1);
  color: var(--primary);
}

.nav-menu li a svg {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

/* Main Content */
.main-content {
  flex: 1;
  min-height: 0;
  width: 100%;
}

.page-header {
  background: white;
  padding: 15px 20px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}

.page-header h1 {
  font-size: 1.25rem;
  color: var(--dark);
}

.content-wrapper {
  padding: 15px;
  max-width: 100%;
  overflow-x: hidden;
}

/* Cards */
.card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  margin-bottom: 15px;
  overflow: hidden;
}

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

.card-header h3 {
  font-size: 1rem;
  color: var(--dark);
  margin: 0;
}

.card-body {
  padding: 15px;
}

/* Grids - Mobile First */
.dashboard-grid,
.session-grid,
.user-grid,
.group-grid,
.webhook-grid,
.admin-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 15px;
}

/* Forms */
.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: 500;
  color: var(--dark);
  font-size: 0.9rem;
}

.form-control {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 16px; /* Prevent iOS zoom */
  transition: border-color 0.3s;
  -webkit-appearance: none;
  appearance: none;
}

.form-control:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 211, 102, 0.1);
}

textarea.form-control {
  resize: vertical;
  min-height: 100px;
}

select.form-control {
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e");
  background-position: right 10px center;
  background-repeat: no-repeat;
  background-size: 20px;
  padding-right: 40px;
}

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

.form-row .form-group {
  flex: 1;
  margin-bottom: 0;
}

.col-4, .col-6 {
  flex: 1;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 16px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
  transition: all 0.3s;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}

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

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

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

.btn-secondary:hover {
  background: #5a6268;
}

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

.btn-warning {
  background: var(--warning);
  color: var(--dark);
}

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

.btn-block {
  display: flex;
  width: 100%;
}

.btn-sm {
  padding: 6px 12px;
  font-size: 13px;
}

.btn svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

/* Alerts */
.alert {
  padding: 12px 15px;
  border-radius: 8px;
  margin-bottom: 15px;
  font-size: 0.9rem;
}

.alert-success {
  background: #d4edda;
  color: #155724;
  border: 1px solid #c3e6cb;
}

.alert-error {
  background: #f8d7da;
  color: #721c24;
  border: 1px solid #f5c6cb;
}

.alert-warning {
  background: #fff3cd;
  color: #856404;
  border: 1px solid #ffeeba;
}

/* Status Badges */
.status-badge {
  display: inline-block;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

.badge-success {
  background: rgba(37, 211, 102, 0.15);
  color: var(--primary-dark);
}

.badge-error {
  background: rgba(220, 53, 69, 0.15);
  color: var(--error);
}

.badge-warning {
  background: rgba(255, 193, 7, 0.15);
  color: #856404;
}

/* Status Grid */
.status-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

.status-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  background: var(--light);
  border-radius: 8px;
}

.status-label {
  font-weight: 500;
  color: var(--gray);
  font-size: 0.9rem;
}

/* Info Rows */
.info-row {
  display: flex;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid var(--gray-light);
  flex-wrap: wrap;
  gap: 5px;
}

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

/* Tables */
.table-responsive {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 0 -15px;
  padding: 0 15px;
}

.table {
  width: 100%;
  border-collapse: collapse;
  min-width: 500px;
}

.table th,
.table td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}

.table th {
  background: var(--light);
  font-weight: 600;
  color: var(--dark);
  white-space: nowrap;
}

.table tbody tr:hover {
  background: rgba(0,0,0,0.02);
}

/* Tabs */
.tabs {
  display: flex;
  gap: 5px;
  margin-bottom: 15px;
  border-bottom: 2px solid var(--border);
  padding-bottom: 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.tab-btn {
  padding: 10px 15px;
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 14px;
  color: var(--gray);
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: all 0.3s;
  white-space: nowrap;
  flex-shrink: 0;
}

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

.tab-btn.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

.tab-content {
  display: none;
}

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

/* QR Code */
.qr-image {
  max-width: 200px;
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
}

/* Avatar */
.avatar-image {
  max-width: 100px;
  border-radius: 50%;
  border: 3px solid var(--primary);
}

/* Quick Actions */
.quick-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* Checkbox Group */
.checkbox-group {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.checkbox-group label {
  display: flex;
  align-items: center;
  gap: 5px;
  font-weight: normal;
  cursor: pointer;
  font-size: 0.9rem;
}

/* Code Block */
.code-block {
  background: var(--dark);
  color: #e9ecef;
  padding: 15px;
  border-radius: 8px;
  overflow-x: auto;
  font-family: 'Courier New', monospace;
  font-size: 12px;
  line-height: 1.5;
  -webkit-overflow-scrolling: touch;
}

code {
  background: rgba(0,0,0,0.05);
  padding: 2px 6px;
  border-radius: 3px;
  font-family: 'Courier New', monospace;
  font-size: 12px;
  word-break: break-all;
}

/* Participant List */
.participant-list {
  list-style: none;
  max-height: 200px;
  overflow-y: auto;
  margin-top: 10px;
  padding: 10px;
  background: var(--light);
  border-radius: 8px;
}

.participant-list li {
  padding: 5px 0;
  font-size: 13px;
  border-bottom: 1px solid var(--border);
}

.participant-list li:last-child {
  border-bottom: none;
}

/* Modal */
.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.5);
  z-index: 1000;
  align-items: flex-end;
  justify-content: center;
  padding: 0;
}

.modal.show {
  display: flex;
}

.modal-content {
  background: white;
  border-radius: 16px 16px 0 0;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  animation: slideUp 0.3s ease;
}

@keyframes slideUp {
  from {
    transform: translateY(100%);
  }
  to {
    transform: translateY(0);
  }
}

.modal-header {
  padding: 15px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  background: white;
  z-index: 10;
}

.modal-header h3 {
  font-size: 1.1rem;
  margin: 0;
}

.modal-close {
  background: none;
  border: none;
  font-size: 28px;
  cursor: pointer;
  color: var(--gray);
  padding: 0 5px;
  line-height: 1;
}

.modal-body {
  padding: 20px;
}

.modal-footer {
  padding: 15px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  flex-wrap: wrap;
  position: sticky;
  bottom: 0;
  background: white;
}

.modal-content.modal-lg {
  max-width: 100%;
}

/* Error Page */
.error-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 60vh;
  padding: 20px;
}

.error-content {
  text-align: center;
}

.error-content h2 {
  font-size: 1.5rem;
  color: var(--error);
  margin-bottom: 10px;
}

.error-content p {
  color: var(--gray);
  margin-bottom: 20px;
}

/* Utility Classes */
.text-center {
  text-align: center;
}

.text-muted {
  color: var(--text-muted);
}

.text-success {
  color: var(--success);
}

.text-error {
  color: var(--error);
}

.text-break {
  word-break: break-all;
}

.text-sm {
  font-size: 12px;
}

.mt-2 { margin-top: 10px; }
.mt-3 { margin-top: 15px; }
.mt-4 { margin-top: 20px; }
.mb-2 { margin-bottom: 10px; }
.mb-3 { margin-bottom: 15px; }
.mb-4 { margin-bottom: 20px; }
.ml-2 { margin-left: 10px; }

.info-box {
  background: var(--light);
  padding: 15px;
  border-radius: 8px;
}

.d-flex {
  display: flex;
}

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

.gap-2 {
  gap: 10px;
}

.gap-3 {
  gap: 15px;
}

/* Hide on mobile */
.hide-mobile {
  display: none;
}

/* Show on mobile */
.show-mobile {
  display: block;
}

/* ========================================
   TABLET STYLES (min-width: 600px)
   ======================================== */
@media (min-width: 600px) {
  .content-wrapper {
    padding: 20px;
  }

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

  .col-4 {
    flex: 0 0 calc(33.333% - 10px);
  }

  .col-6 {
    flex: 0 0 calc(50% - 7.5px);
  }

  .status-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .modal {
    align-items: center;
    padding: 20px;
  }

  .modal-content {
    border-radius: 12px;
    max-width: 500px;
    max-height: 85vh;
    animation: fadeIn 0.2s ease;
  }

  @keyframes fadeIn {
    from {
      opacity: 0;
      transform: scale(0.95);
    }
    to {
      opacity: 1;
      transform: scale(1);
    }
  }

  .modal-content.modal-lg {
    max-width: 700px;
  }

  .dashboard-grid,
  .session-grid,
  .user-grid,
  .group-grid,
  .webhook-grid,
  .admin-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* ========================================
   DESKTOP STYLES (min-width: 992px)
   ======================================== */
@media (min-width: 992px) {
  .app-container {
    flex-direction: row;
  }

  /* Sidebar - Fixed on desktop */
  .sidebar {
    width: var(--sidebar-width);
    position: fixed;
    height: 100vh;
    overflow-y: auto;
  }

  .sidebar-header {
    justify-content: center;
  }

  .menu-toggle {
    display: none;
  }

  .nav-menu {
    display: flex !important;
    flex-direction: column;
  }

  .nav-menu li a {
    padding: 15px 20px;
  }

  .nav-menu li a:hover,
  .nav-menu li.active a {
    border-left: 3px solid var(--primary);
  }

  /* Main Content - Offset for sidebar */
  .main-content {
    margin-left: var(--sidebar-width);
  }

  .page-header {
    padding: 20px 30px;
  }

  .page-header h1 {
    font-size: 1.5rem;
  }

  .content-wrapper {
    padding: 30px;
  }

  .card-header,
  .card-body {
    padding: 20px;
  }

  .dashboard-grid,
  .session-grid,
  .user-grid,
  .group-grid,
  .webhook-grid,
  .admin-grid {
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 20px;
  }

  .hide-mobile {
    display: block;
  }

  .show-mobile {
    display: none;
  }

  .qr-image {
    max-width: 250px;
  }

  .avatar-image {
    max-width: 150px;
  }
}

/* ========================================
   LARGE DESKTOP (min-width: 1200px)
   ======================================== */
@media (min-width: 1200px) {
  .dashboard-grid,
  .session-grid,
  .user-grid,
  .group-grid,
  .webhook-grid,
  .admin-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

/* ========================================
   TOUCH DEVICE OPTIMIZATIONS
   ======================================== */
@media (hover: none) and (pointer: coarse) {
  .btn {
    min-height: 44px;
    min-width: 44px;
  }

  .btn-sm {
    min-height: 36px;
    padding: 8px 14px;
  }

  .form-control {
    min-height: 44px;
  }

  .nav-menu li a {
    min-height: 48px;
  }

  .table th,
  .table td {
    padding: 12px;
  }
}

/* Prevent text selection on interactive elements */
.btn,
.tab-btn,
.nav-menu li a,
.modal-close {
  -webkit-user-select: none;
  user-select: none;
}

/* Safe area insets for notched phones */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .modal-footer {
    padding-bottom: calc(15px + env(safe-area-inset-bottom));
  }

  .sidebar {
    padding-bottom: env(safe-area-inset-bottom);
  }
}

/* ========================================
   USER INFO & AUTH
   ======================================== */
.nav-divider {
  height: 1px;
  background: rgba(255,255,255,0.1);
  margin: 10px 15px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 15px;
  background: rgba(255,255,255,0.05);
  margin: 5px 10px;
  border-radius: 8px;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 1.1rem;
  color: white;
  flex-shrink: 0;
}

.user-details {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.user-name {
  font-weight: 600;
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-instance {
  font-size: 0.75rem;
  color: rgba(255,255,255,0.6);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.logout-link {
  color: #ff6b6b !important;
}

.logout-link:hover {
  background: rgba(255,107,107,0.15) !important;
}

/* Page header with instance badge */
.page-header {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}

.page-header h1 {
  margin: 0;
  font-size: 1.5rem;
  flex-shrink: 0;
}

.header-instances {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}

.header-instance-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  background: rgba(37, 211, 102, 0.1);
  color: var(--primary-dark);
  border-radius: 15px;
  font-size: 0.75rem;
  font-weight: 500;
  white-space: nowrap;
}

.header-instance-badge svg {
  flex-shrink: 0;
}

/* Mobile responsive for page header */
@media (max-width: 768px) {
  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  .page-header h1 {
    font-size: 1.3rem;
  }

  .header-instances {
    width: 100%;
    gap: 5px;
  }

  .header-instance-badge {
    padding: 3px 8px;
    font-size: 0.7rem;
    gap: 4px;
  }

  .header-instance-badge svg {
    width: 12px;
    height: 12px;
  }
}

@media (max-width: 480px) {
  .page-header h1 {
    font-size: 1.2rem;
  }

  .header-instance-badge {
    padding: 2px 6px;
    font-size: 0.65rem;
  }
}
