*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg-primary: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-card: #0f3460;
  --accent: #6C63FF;
  --accent-hover: #5a52d5;
  --text-primary: #e8e8e8;
  --text-secondary: #a0a0b0;
  --text-muted: #6a6a7a;
  --success: #4caf50;
  --danger: #e53935;
  --warning: #ff9800;
  --border: #2a2a4a;
  --radius: 12px;
  --radius-sm: 8px;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
}

#app {
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* Header */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  min-height: 56px;
}

.header h1 {
  font-size: 18px;
  font-weight: 600;
  color: var(--accent);
}

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

/* Page container */
.page {
  display: none;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 16px;
}

.page.active {
  display: flex;
  flex-direction: column;
}

#conversation-page.active {
  overflow: hidden;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, opacity 0.2s;
  text-decoration: none;
  color: white;
}

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

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

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

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

.btn-success:hover:not(:disabled) {
  background: #388e3c;
}

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

.btn-danger:hover:not(:disabled) {
  background: #c62828;
}

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

.btn-outline:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
}

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

.btn-icon {
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 50%;
  font-size: 18px;
}

.btn-icon.large {
  width: 56px;
  height: 56px;
  font-size: 24px;
}

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

.form-group label {
  display: block;
  margin-bottom: 6px;
  font-size: 13px;
  color: var(--text-secondary);
  font-weight: 500;
}

.form-input {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-size: 16px;
  outline: none;
  transition: border-color 0.2s;
}

.form-input:focus {
  border-color: var(--accent);
}

/* Cards */
.card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}

/* Login page */
.login-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px;
  text-align: center;
}

.login-container .logo {
  font-size: 48px;
  margin-bottom: 8px;
}

.login-container h2 {
  font-size: 24px;
  margin-bottom: 4px;
  color: var(--accent);
}

.login-container p {
  color: var(--text-secondary);
  margin-bottom: 24px;
  font-size: 14px;
}

.login-form {
  width: 100%;
  max-width: 360px;
}

/* Pending page */
.pending-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 16px;
}

.pending-container .spinner {
  width: 48px;
  height: 48px;
  border: 4px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Contact list */
.contact-list {
  list-style: none;
}

.contact-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.2s;
  border-bottom: 1px solid var(--border);
}

.contact-item:hover {
  background: var(--bg-card);
}

.contact-avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--bg-card);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 16px;
  color: var(--accent);
  flex-shrink: 0;
}

.contact-info {
  flex: 1;
  min-width: 0;
}

.contact-name {
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.contact-email {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.contact-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--text-muted);
  flex-shrink: 0;
}

.status-dot.online {
  background: var(--success);
}

/* Unread badge */
.unread-badge {
  background: var(--accent);
  color: white;
  font-size: 11px;
  font-weight: 700;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

/* Chat page */
.chat-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  overflow: hidden;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
}

.chat-header .back-btn {
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 20px;
  cursor: pointer;
  padding: 4px;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.message {
  max-width: 80%;
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 14px;
  line-height: 1.4;
  word-wrap: break-word;
}

.message.sent {
  align-self: flex-end;
  background: var(--accent);
  color: white;
  border-bottom-right-radius: 4px;
}

.message.received {
  align-self: flex-start;
  background: var(--bg-card);
  color: var(--text-primary);
  border-bottom-left-radius: 4px;
}

.message .time {
  font-size: 11px;
  opacity: 0.7;
  margin-top: 4px;
  display: flex;
  align-items: center;
  gap: 4px;
}

/* Read indicators */
.read-indicator {
  font-size: 12px;
  opacity: 0.6;
}

.read-indicator.read {
  color: #4caf50;
  opacity: 1;
}

.message.sent .read-indicator.read {
  color: #8fff8f;
}

/* Call log messages */
.message.call-message {
  align-self: center;
  max-width: 60%;
  background: var(--bg-secondary);
  color: var(--text-muted);
  font-size: 13px;
  text-align: center;
  border-radius: 16px;
  padding: 6px 16px;
}

.chat-input-area {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom, 0px)) 16px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
}

.chat-input-area input[type="text"] {
  flex: 1;
  padding: 10px 16px;
  border: 1px solid var(--border);
  border-radius: 24px;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 14px;
  outline: none;
}

.chat-input-area input[type="text"]:focus {
  border-color: var(--accent);
}

/* Attachment & mic buttons */
.btn-icon-sm {
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  color: var(--text-secondary);
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  flex-shrink: 0;
  transition: color 0.2s, background 0.2s;
}

.btn-icon-sm:hover {
  color: var(--accent);
  background: rgba(108, 99, 255, 0.1);
}

/* Recording indicator */
.recording-indicator {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
}

.recording-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--danger);
  animation: pulse-recording 1s ease-in-out infinite;
}

@keyframes pulse-recording {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

.recording-text {
  color: var(--danger);
  font-size: 14px;
  font-weight: 600;
}

.recording-time {
  color: var(--text-secondary);
  font-size: 14px;
  font-family: monospace;
  margin-right: auto;
}

/* Media messages */
.message.media-message {
  padding: 6px;
}

.media-image {
  max-width: 100%;
  max-height: 300px;
  border-radius: 8px;
  cursor: pointer;
  display: block;
}

.media-video {
  max-width: 100%;
  max-height: 300px;
  border-radius: 8px;
  display: block;
}

.media-voice audio {
  max-width: 100%;
  min-width: 200px;
  height: 36px;
}

.media-uploading,
.media-upload-error {
  padding: 8px;
  font-size: 13px;
  color: var(--text-muted);
  font-style: italic;
}

.media-upload-error {
  color: var(--danger);
}

.media-decrypting {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  font-size: 13px;
  color: var(--text-muted);
  font-style: italic;
}

.spinner-small {
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  flex-shrink: 0;
}

/* Fullscreen media viewer */
.media-viewer-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.95);
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.media-viewer-overlay img {
  max-width: 95%;
  max-height: 95%;
  object-fit: contain;
}

/* Call page */
.call-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--bg-primary);
  position: relative;
}

.call-container .remote-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #000;
}

.call-container .local-video {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 120px;
  height: 160px;
  border-radius: var(--radius);
  object-fit: cover;
  border: 2px solid var(--border);
  background: #000;
  z-index: 10;
}

.call-info {
  text-align: center;
  z-index: 5;
}

.call-info h3 {
  font-size: 24px;
  margin-bottom: 8px;
}

.call-info p {
  color: var(--text-secondary);
  font-size: 14px;
}

.call-controls {
  position: absolute;
  bottom: calc(32px + env(safe-area-inset-bottom, 0px));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 16px;
  z-index: 10;
}

/* Incoming call modal */
.incoming-call-modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.85);
  z-index: 100;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 24px;
}

.incoming-call-modal.active {
  display: flex;
}

.incoming-call-modal h3 {
  font-size: 20px;
}

.incoming-call-modal p {
  color: var(--text-secondary);
}

.incoming-call-modal .actions {
  display: flex;
  gap: 32px;
}

/* Admin page */
.admin-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 16px;
  background: var(--bg-secondary);
  border-radius: var(--radius-sm);
  padding: 4px;
}

.admin-tab {
  flex: 1;
  padding: 8px;
  border: none;
  background: none;
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  border-radius: 6px;
  transition: all 0.2s;
}

.admin-tab.active {
  background: var(--accent);
  color: white;
}

.user-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin-bottom: 8px;
}

.user-item .user-info {
  flex: 1;
}

.user-item .user-email {
  font-weight: 600;
  font-size: 14px;
}

.user-item .user-date {
  font-size: 12px;
  color: var(--text-muted);
}

.user-item .user-actions {
  display: flex;
  gap: 8px;
}

/* Admin section tabs */
.admin-section-tabs {
  display: flex;
  gap: 0;
  margin-bottom: 16px;
  border-bottom: 2px solid var(--border);
}

.admin-section-tab {
  padding: 10px 20px;
  border: none;
  background: none;
  color: var(--text-secondary);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: color 0.2s, border-color 0.2s;
}

.admin-section-tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

/* Config form */
.config-warning {
  background: rgba(255, 152, 0, 0.1);
  border: 1px solid var(--warning);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  font-size: 13px;
  color: var(--warning);
  margin-bottom: 16px;
}

.config-group {
  margin-bottom: 20px;
}

.config-group-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 10px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}

.config-field {
  margin-bottom: 12px;
}

.config-field label {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 4px;
  font-size: 13px;
  color: var(--text-secondary);
  font-weight: 500;
}

.config-field .config-source {
  font-size: 10px;
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--bg-card);
  color: var(--text-muted);
}

.config-field input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-size: 14px;
  outline: none;
  transition: border-color 0.2s;
  font-family: monospace;
}

.config-field input:focus {
  border-color: var(--accent);
}

/* System dashboard */
.system-section-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 12px;
}

.service-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  font-size: 14px;
  border-bottom: 1px solid var(--border);
}

.service-item:last-child {
  border-bottom: none;
}

.service-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.service-dot.running {
  background: var(--success);
}

.service-dot.stopped {
  background: var(--danger);
}

.service-name {
  font-weight: 600;
  min-width: 100px;
}

.service-status {
  color: var(--text-secondary);
  font-size: 13px;
}

.service-detail {
  margin-left: auto;
  color: var(--text-muted);
  font-size: 12px;
  font-family: monospace;
}

.system-metrics {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
  font-size: 13px;
  color: var(--text-secondary);
}

.system-metric {
  display: flex;
  gap: 4px;
}

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

.system-metric .value {
  color: var(--text-primary);
  font-family: monospace;
}

.log-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}

.log-tab {
  padding: 6px 12px;
  border: 1px solid var(--border);
  background: none;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  border-radius: 6px;
  transition: all 0.2s;
}

.log-tab.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.log-viewer {
  background: #0a0a1a;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
  font-family: 'Courier New', Courier, monospace;
  font-size: 12px;
  line-height: 1.5;
  color: var(--text-secondary);
  max-height: 400px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-all;
  margin-bottom: 8px;
}

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

.log-auto-refresh {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-secondary);
  cursor: pointer;
}

.log-auto-refresh input[type="checkbox"] {
  accent-color: var(--accent);
}

/* Usage table */
.usage-table-scroll {
  overflow-x: auto;
}

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

.usage-table th,
.usage-table td {
  padding: 8px 10px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

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

.usage-table td {
  color: var(--text-primary);
}

.usage-table td:first-child {
  font-weight: 600;
}

.usage-table tbody tr:hover {
  background: var(--bg-card);
}

/* E2E Test Screenshots */
.e2e-screenshot-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
  margin-top: 8px;
}

.e2e-screenshot-item {
  text-align: center;
}

.e2e-thumbnail {
  width: 100%;
  border-radius: 6px;
  border: 2px solid var(--border);
  cursor: pointer;
  transition: transform 0.15s, border-color 0.15s;
}

.e2e-thumbnail:hover {
  transform: scale(1.03);
  border-color: var(--primary);
}

.e2e-caption {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 4px;
}

.e2e-timestamp {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 8px;
}

/* Status badge */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
}

.badge-pending {
  background: var(--warning);
  color: #000;
}

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

.badge-blocked {
  background: var(--danger);
  color: white;
}

/* Toast notifications */
.toast-container {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  background: var(--bg-card);
  color: var(--text-primary);
  font-size: 14px;
  border: 1px solid var(--border);
  animation: slideIn 0.3s ease;
  max-width: 320px;
}

.toast.success {
  border-color: var(--success);
}

.toast.error {
  border-color: var(--danger);
}

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* Add contact form */
.add-contact-form {
  padding: 12px;
  border-bottom: 1px solid var(--border);
}

/* Pending request items */
.request-item {
  border-left: 3px solid var(--warning);
}

.section-label {
  list-style: none;
}

/* Empty state */
.empty-state {
  text-align: center;
  padding: 48px 16px;
  color: var(--text-muted);
}

.empty-state .icon {
  font-size: 48px;
  margin-bottom: 12px;
}

/* Utility */
.hidden {
  display: none !important;
}

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

.mt-8 { margin-top: 8px; }
.mt-16 { margin-top: 16px; }
.mb-8 { margin-bottom: 8px; }
.mb-16 { margin-bottom: 16px; }

/* Device cards (settings page) */
.device-card { margin-bottom: 12px; }
.device-header { display: flex; align-items: flex-start; justify-content: space-between; margin-bottom: 12px; }
.device-name { font-weight: 600; font-size: 15px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.device-date { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.device-toggles { display: flex; flex-direction: column; gap: 10px; margin-bottom: 12px; }
.device-toggle { display: flex; align-items: center; gap: 10px; font-size: 14px; color: var(--text-secondary); cursor: pointer; }
.device-toggle input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); }

/* Settings dropdown menu */
.settings-menu-wrap {
  position: relative;
}

.settings-menu {
  display: none;
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 4px;
  min-width: 160px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  z-index: 50;
  overflow: hidden;
}

.settings-menu-wrap.open .settings-menu {
  display: block;
}

.settings-menu-item {
  display: block;
  width: 100%;
  padding: 10px 16px;
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 14px;
  text-align: left;
  cursor: pointer;
  transition: background 0.15s;
  text-decoration: none;
}

.settings-menu-item:hover {
  background: var(--bg-card);
}

.settings-menu-item.danger {
  color: var(--danger);
}

/* Responsive */
@media (min-width: 768px) {
  .page {
    max-width: 600px;
    margin: 0 auto;
    width: 100%;
  }

  .call-container .local-video {
    width: 180px;
    height: 240px;
  }
}
