/* Apple-inspired design system */
:root {
  --bg: #f5f5f7;
  --card: #ffffff;
  --text: #1d1d1f;
  --text-secondary: #86868b;
  --accent: #0071e3;
  --accent-hover: #0077ed;
  --danger: #ff3b30;
  --success: #34c759;
  --warning: #ff9500;
  --border: #d2d2d7;
  --shadow: 0 2px 12px rgba(0,0,0,0.08);
  --shadow-lg: 0 4px 24px rgba(0,0,0,0.12);
  --radius: 12px;
  --radius-sm: 8px;
  --font: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Segoe UI', Roboto, sans-serif;
  --transition: all 0.2s ease;
}

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}

/* Login */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 20px;
}

.login-card {
  background: var(--card);
  border-radius: 20px;
  padding: 48px 40px;
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow-lg);
  text-align: center;
}

.login-card h1 {
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 8px;
  letter-spacing: -0.5px;
}

.login-card p {
  color: var(--text-secondary);
  margin-bottom: 32px;
  font-size: 15px;
}

.form-group {
  margin-bottom: 16px;
  text-align: left;
}

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

.form-group input {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 16px;
  font-family: var(--font);
  transition: var(--transition);
  background: var(--bg);
  outline: none;
}

.form-group input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(0,113,227,0.15);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 12px 24px;
  border: none;
  border-radius: 980px;
  font-size: 15px;
  font-weight: 600;
  font-family: var(--font);
  cursor: pointer;
  transition: var(--transition);
  text-decoration: none;
}

.btn-primary {
  background: var(--accent);
  color: white;
  width: 100%;
  margin-top: 8px;
}

.btn-primary:hover { background: var(--accent-hover); transform: scale(1.01); }

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

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

.btn-sm { padding: 8px 16px; font-size: 13px; }

.error-msg {
  color: var(--danger);
  font-size: 13px;
  margin-top: 12px;
}

/* App layout */
.app { display: none; min-height: 100vh; }
.app.active { display: flex; flex-direction: column; }

/* Top nav */
.topbar {
  background: rgba(255,255,255,0.72);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 100;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 24px;
}

.topbar-logo {
  height: 26px;
  display: block;
}

.topbar-tabs {
  display: flex;
  gap: 4px;
}

.topbar-tab {
  padding: 8px 16px;
  border-radius: 980px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  transition: var(--transition);
  border: none;
  background: none;
  font-family: var(--font);
}

.topbar-tab:hover { color: var(--text); background: rgba(0,0,0,0.04); }
.topbar-tab.active { color: var(--text); background: rgba(0,0,0,0.06); font-weight: 600; }

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

.topbar-user {
  font-size: 14px;
  color: var(--text-secondary);
}

.topbar-btn {
  padding: 6px 14px;
  border-radius: 980px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border: none;
  font-family: var(--font);
  transition: var(--transition);
}

/* Content area */
.content {
  flex: 1;
  padding: 24px;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
}

/* Multi-view grid */
.device-grid {
  display: grid;
  grid-template-columns: repeat(var(--grid-cols, 3), 1fr);
  gap: 16px;
}

.device-card {
  background: var(--card);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  transition: var(--transition);
  cursor: pointer;
}

.device-card:hover { box-shadow: var(--shadow-lg); transform: translateY(-2px); }

.device-video-container {
  position: relative;
  background: #000;
  aspect-ratio: 9/19.5;
  overflow: hidden;
  border-radius: var(--radius) var(--radius) 0 0;
}

.device-video-container video, .device-video-container iframe {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.device-status {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 2px solid rgba(0,0,0,0.3);
}

.device-status.online { background: var(--success); }
.device-status.offline { background: var(--danger); }

.device-info-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
}

.device-name-sm {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.device-code-sm {
  font-size: 11px;
  color: var(--text-secondary);
  font-family: 'SF Mono', 'Menlo', monospace;
  white-space: nowrap;
}

.device-open-btn {
  margin-left: auto;
  padding: 4px 12px !important;
  font-size: 12px !important;
}

/* Single view */
.single-view { display: none; }
.single-view.active { display: flex; flex-direction: column; gap: 16px; }

.single-header {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
}

.single-back {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
  color: var(--accent);
  cursor: pointer;
  font-weight: 500;
  border: none;
  background: none;
  font-family: var(--font);
}

.single-layout {
  display: flex;
  flex-direction: column;
  gap: 16px;
  align-items: center;
}

@media (min-width: 900px) {
  .single-layout {
    flex-direction: row;
    align-items: flex-start;
    justify-content: center;
    gap: 28px;
    margin: 0 auto;
    width: fit-content;
  }
  .single-layout .device-settings {
    width: 380px;
    position: sticky;
    top: 68px;
  }
}

.single-video-wrap {
  position: relative;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  width: 340px;
  touch-action: none;
  flex-shrink: 0;
}

.single-video-wrap video, .single-video-wrap iframe {
  width: 100%;
  display: block;
  aspect-ratio: 9/19.5;
  object-fit: contain;
}

.single-controls {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: center;
}

.control-group {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}

.control-group h3 {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

/* Device settings panel */
.device-settings {
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  max-width: 500px;
  margin: 0 auto;
  width: 100%;
}

.device-settings .form-group { margin-bottom: 12px; }

.device-settings input, .device-settings textarea, .device-settings select {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-family: var(--font);
  background: var(--bg);
  outline: none;
  transition: var(--transition);
}

.device-settings textarea { resize: vertical; min-height: 60px; }
.device-settings input:focus, .device-settings textarea:focus, .device-settings select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(0,113,227,0.15);
}

/* Admin panel */
.admin-section {
  background: var(--card);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--shadow);
  margin-bottom: 20px;
}

.admin-section h2 {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 16px;
}

.admin-table {
  width: 100%;
  border-collapse: collapse;
}

.admin-table th, .admin-table td {
  padding: 10px 16px;
  text-align: left;
  border-bottom: 1px solid var(--bg);
  font-size: 14px;
}

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

.admin-row-actions { display: flex; gap: 8px; }

.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 980px;
  font-size: 12px;
  font-weight: 600;
}

.badge-admin { background: #e8d5f5; color: #7c3aed; }
.badge-user { background: #dbeafe; color: #2563eb; }
.badge-online { background: #dcfce7; color: #16a34a; }
.badge-offline { background: #fee2e2; color: #dc2626; }

/* Cursor overlay for control */
.touch-overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  cursor: crosshair;
  z-index: 10;
}

/* Modal */
.modal-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.4);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 20px;
}

.modal {
  background: var(--card);
  border-radius: 16px;
  padding: 32px;
  max-width: 440px;
  width: 100%;
  box-shadow: var(--shadow-lg);
}

.modal h2 { font-size: 22px; margin-bottom: 20px; }
.modal .btn { margin-top: 12px; }

/* Responsive */
@media (max-width: 768px) {
  .topbar { padding: 0 16px; }
  .topbar-tabs { display: none; }
  .content { padding: 16px; }
  .device-grid { grid-template-columns: 1fr; }
  .single-video-wrap { max-width: 100%; }
}

/* Team cards */
.team-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

.team-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: var(--transition);
  display: flex;
  align-items: center;
  gap: 16px;
}

.team-card:hover { box-shadow: var(--shadow-lg); transform: translateY(-2px); }

.team-logo {
  width: 56px;
  height: 56px;
  border-radius: 14px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: white;
  font-weight: 700;
  overflow: hidden;
  flex-shrink: 0;
}

.team-logo img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

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

.team-card-name {
  font-size: 17px;
  font-weight: 600;
  margin-bottom: 4px;
}

.team-card-meta {
  font-size: 13px;
  color: var(--text-secondary);
  display: flex;
  gap: 12px;
}

.team-detail-back {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 15px;
  color: var(--accent);
  cursor: pointer;
  font-weight: 500;
  border: none;
  background: none;
  font-family: var(--font);
  margin-bottom: 16px;
}

.logo-upload {
  position: relative;
  width: 80px;
  height: 80px;
  border-radius: 18px;
  overflow: hidden;
  cursor: pointer;
  background: linear-gradient(135deg, #667eea, #764ba2);
  display: flex;
  align-items: center;
  justify-content: center;
}

.logo-upload img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.logo-upload:hover::after {
  content: 'Change';
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.5);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 600;
}

.avatar-sm {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--bg);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.avatar-sm img { width: 100%; height: 100%; object-fit: cover; }

.avatar-topbar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  overflow: hidden;
  cursor: pointer;
}

.avatar-topbar img { width: 100%; height: 100%; object-fit: cover; }

/* Loading overlay */
.stream-loading {
  position: absolute;
  inset: 0;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 5;
  flex-direction: column;
  gap: 12px;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid rgba(255,255,255,0.2);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

.stream-loading span {
  color: rgba(255,255,255,0.7);
  font-size: 13px;
}

/* Utility */
.hidden { display: none !important; }
.flex { display: flex; }
.gap-8 { gap: 8px; }
.gap-16 { gap: 16px; }
.mt-16 { margin-top: 16px; }
.text-center { text-align: center; }
