:root {
  --bg-page: radial-gradient(circle at 50% 50%, #161920 0%, #0b0c10 100%);
  --bg-navbar: rgba(17, 20, 28, 0.8);
  --border-glass: rgba(255, 255, 255, 0.08);
  --text-main: #ffffff;
  --text-muted: rgba(255, 255, 255, 0.45);
  --accent: #007aff;
  --accent-hover: #3498ff;
  --accent-bg: rgba(0, 122, 255, 0.1);
  --danger: #ff3b30;
  --danger-bg: rgba(255, 59, 48, 0.15);
  --success: #34c759;
  --warning: #ffcc00;
  --bg-chat-bubble-me: #007aff;
  --bg-chat-bubble-other: rgba(255, 255, 255, 0.08);
}

body.light-theme {
  --bg-page: radial-gradient(circle at 50% 50%, #f7f9fc 0%, #e9ecf0 100%);
  --bg-navbar: rgba(255, 255, 255, 0.8);
  --border-glass: rgba(0, 0, 0, 0.08);
  --text-main: #1c1c1e;
  --text-muted: rgba(0, 0, 0, 0.45);
  --accent: #007aff;
  --accent-hover: #0062cc;
  --accent-bg: rgba(0, 122, 255, 0.06);
  --bg-chat-bubble-me: #007aff;
  --bg-chat-bubble-other: rgba(0, 0, 0, 0.05);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body { display: flex; flex-direction: column; height: 100vh; background: var(--bg-page); font-family: -apple-system, system-ui, sans-serif; overflow: hidden; color: var(--text-main); }

/* OVERLAY MODALI E LOBBY D'ATTESA */
.full-overlay {
  position: fixed; top: 0; left: 0; width: 100vw; height: 100vh;
  background: rgba(10, 12, 16, 0.93); -webkit-backdrop-filter: blur(20px); backdrop-filter: blur(20px);
  display: flex; align-items: center; justify-content: center; z-index: 99999;
  opacity: 0; pointer-events: none; transition: opacity 0.3s ease; text-align: center;
}
.full-overlay.show { opacity: 1; pointer-events: auto; }

.full-card { background: #151821; border: 1px solid rgba(255, 255, 255, 0.1); padding: 30px; border-radius: 16px; max-width: 400px; width: 90%; color: #fff; }
.modal-input { width: 100%; height: 40px; background: rgba(255,255,255,0.05); border: 1px solid rgba(255,255,255,0.15); border-radius: 8px; padding: 0 10px; color: #fff; text-align: center; margin-bottom: 15px; outline: none; }
.modal-btn-group { display: flex; gap: 10px; justify-content: center; margin-top: 10px; }

.ui-action-btn { padding: 10px 20px; border-radius: 8px; border: none; font-weight: 600; cursor: pointer; font-size: 13px; }
.ui-action-btn.primary { background: var(--accent); color: #fff; }
.ui-action-btn.danger { background: var(--danger); color: #fff; }
.success-color { background: var(--success) !important; }

/* NAVBAR */
.navbar { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; height: 65px; padding: 0 20px; background: var(--bg-navbar); border-bottom: 1px solid var(--border-glass); -webkit-backdrop-filter: blur(20px); backdrop-filter: blur(20px); z-index: 100; }
.nav-left { display: flex; align-items: center; gap: 12px; }
.brand-title { font-weight: 800; text-transform: uppercase; letter-spacing: 0.5px; font-size: 15px; }
.room-container { display: flex; align-items: center; background: var(--accent-bg); padding: 4px 10px; border-radius: 6px; border: 1px solid var(--border-glass); }
.room-label { font-size: 11px; color: var(--text-muted); text-transform: uppercase; margin-right: 4px; }
.room-value { font-size: 12px; font-family: monospace; font-weight: 700; color: var(--accent); }
.copy-btn-link { background: none; border: none; color: var(--accent); margin-left: 6px; cursor: pointer; display: flex; align-items: center; }
.nav-center { display: flex; align-items: center; justify-content: center; gap: 8px; }
.nav-clock { font-size: 13px; font-weight: 600; padding: 4px 12px; background: rgba(128,128,128,0.08); border-radius: 15px; border: 1px solid var(--border-glass); white-space: nowrap; }
.nav-right { display: flex; justify-content: flex-end; gap: 8px; }
.nav-action-btn { display: flex; align-items: center; justify-content: center; width: 34px; height: 34px; border-radius: 6px; border: 1px solid var(--border-glass); background: rgba(128,128,128,0.05); color: var(--text-main); cursor: pointer; }
.btn-danger-ui { background: var(--danger-bg) !important; color: var(--danger) !important; }

/* VIDEO LAYOUT WORKSPACE */
.app-workspace { flex: 1; display: flex; position: relative; height: calc(100vh - 65px); }
.main-container { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 15px; position: relative; }
.video-grid { position: relative; width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; background: #000; border-radius: 12px; overflow: hidden; margin-bottom: 15px; }

#wrapper-remote { position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 1; }
#wrapper-local { position: absolute; bottom: 15px; right: 15px; width: 180px; aspect-ratio: 16/9; z-index: 5; border-radius: 8px; overflow: hidden; border: 2px solid rgba(255,255,255,0.2); }
video { width: 100%; height: 100%; object-fit: cover; }

.video-placeholder-overlay { position: absolute; top:0; left:0; width:100%; height:100%; background:#12141a; display:flex; flex-direction:column; align-items:center; justify-content:center; gap:10px; z-index:2; color:#fff; font-size:13px; }
.video-placeholder-overlay.hidden { display: none; }
.video-privacy-overlay { position: absolute; top:0; left:0; width:100%; height:100%; background:#151821; display:none; flex-direction:column; align-items:center; justify-content:center; z-index:3; color: var(--text-muted); }
.video-privacy-overlay.show { display: flex; }

/* REQUISITI BADGES IN-VIDEO */
.remote-mute-status-badge { position: absolute; top: 15px; left: 15px; background: rgba(255, 59, 48, 0.85); color: white; padding: 4px 8px; border-radius: 4px; font-size: 11px; display: none; align-items: center; gap: 6px; z-index: 4; }
.remote-rec-alert { position: absolute; top: 15px; right: 15px; background: rgba(0, 0, 0, 0.7); border: 1px solid red; color: #ff4d4d; padding: 4px 8px; border-radius: 4px; font-size: 11px; display: none; z-index: 4; }

/* CONTROLLI BARRA */
.controls-bar { display: flex; gap: 12px; background: var(--bg-navbar); border: 1px solid var(--border-glass); padding: 10px 20px; border-radius: 50px; z-index: 20; position: relative; }
.glass-btn { position: relative; display: flex; align-items: center; justify-content: center; width: 42px; height: 42px; border-radius: 50%; background: rgba(255,255,255,0.05); border: 1px solid var(--border-glass); color: var(--text-main); cursor: pointer; }
.glass-btn:hover { background: rgba(255,255,255,0.15); color: var(--accent); }
.glass-btn.active-off { background: var(--danger-bg) !important; color: var(--danger) !important; }
.glass-btn.rec-active { background: var(--danger-bg) !important; color: var(--danger) !important; animation: pulse 1s infinite alternate; }

/* POPUP INTERNI MENU */
.more-options-popup { position: absolute; bottom: 60px; left: 50%; transform: translateX(-50%); background: var(--bg-navbar); border: 1px solid var(--border-glass); backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px); padding: 6px; border-radius: 12px; display: none; flex-direction: column; gap: 2px; min-width: 200px; z-index: 95; }
.more-options-popup.show { display: flex; }
.menu-item-btn { display: flex; align-items: center; gap: 10px; background: none; border: none; padding: 8px 12px; border-radius: 6px; color: var(--text-main); font-size: 12px; cursor: pointer; text-align: left; width: 100%; }
.menu-item-btn:hover { background: var(--accent-bg); color: var(--accent); }
.menu-item-btn svg { width: 14px; height: 14px; opacity: 0.7; }

.reactions-popup-menu { position: absolute; bottom: 60px; left: 50%; transform: translateX(-50%); background: var(--bg-navbar); border: 1px solid var(--border-glass); padding: 6px 12px; border-radius: 20px; display: none; gap: 8px; z-index: 96; }
.reactions-popup-menu.show { display: flex; }
.reactions-popup-menu button { font-size: 18px; background: none; border: none; cursor: pointer; }

/* PANNELLO STRUTTURALE CHAT */
.chat-panel { position: absolute; top: 0; right: -320px; width: 300px; height: 100%; background: var(--bg-navbar); border-left: 1px solid var(--border-glass); display: flex; flex-direction: column; transition: right 0.3s cubic-bezier(0.16, 1, 0.3, 1); z-index: 90; }
body.chat-open .chat-panel { right: 0; }
body.chat-open .main-container { margin-right: 300px; }
.chat-header { padding: 15px; border-bottom: 1px solid var(--border-glass); display: flex; justify-content: space-between; align-items: center; }
.chat-history { flex: 1; padding: 15px; overflow-y: auto; display: flex; flex-direction: column; gap: 10px; }
.chat-message { max-width: 85%; padding: 8px 12px; border-radius: 12px; font-size: 12px; line-height: 1.4; word-wrap: break-word; }
.chat-message.msg-me { align-self: flex-end; background: var(--bg-chat-bubble-me); color: #fff; }
.chat-message.msg-other { align-self: flex-start; background: var(--bg-chat-bubble-other); color: var(--text-main); }
.chat-input-area { padding: 12px; border-top: 1px solid var(--border-glass); display: flex; gap: 6px; }
.chat-input-area input { flex: 1; height: 34px; border-radius: 17px; border: 1px solid var(--border-glass); background: rgba(0,0,0,0.1); color: var(--text-main); padding: 0 12px; font-size: 12px; outline: none; }
.chat-send-btn { width: 34px; height: 34px; border-radius: 50%; border: none; background: var(--accent); color: #fff; display: flex; align-items: center; justify-content: center; cursor: pointer; }
.unread-chat-badge { position: absolute; top: -2px; right: -2px; background: var(--danger); color: white; font-size: 9px; font-weight: 700; min-width: 16px; height: 16px; border-radius: 50%; display: none; align-items: center; justify-content: center; }

/* COMPONENTI GRAFICI ANIMATI */
.spinner-loader { width: 20px; height: 20px; border: 2px solid rgba(255,255,255,0.1); border-radius: 50%; border-top-color: var(--accent); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse { to { box-shadow: 0 0 10px var(--danger); } }

.toast-notification { position: fixed; top: 75px; right: 20px; background: var(--bg-navbar); border: 1px solid var(--border-glass); color: var(--text-main); padding: 10px 18px; border-radius: 6px; opacity: 0; transition: opacity 0.3s; z-index: 1000; font-size: 11px; pointer-events: none; }
.toast-notification.show { opacity: 1; }
.reaction-container-lane { position: absolute; bottom: 70px; left: 50%; transform: translateX(-50%); width: 100%; height: 60%; pointer-events: none; z-index: 8; overflow: hidden; }
.floating-emoji { position: absolute; bottom: -40px; font-size: 28px; animation: fUp 2s ease-in forwards; opacity: 0; }
@keyframes fUp { 0% { transform: translateY(0); opacity:0; } 10% { opacity:1; } 100% { transform: translateY(-300px); opacity:0; } }

.cowatch-box {
  position: absolute; top: 15px; left: 15px; width: 380px;
  background: #11141c; border: 1.5px solid var(--accent);
  border-radius: 10px; z-index: 20; display: none;
  box-shadow: 0 12px 40px rgba(0,0,0,0.6);
  transition: all 0.25s ease;
  overflow: hidden;
}
.cowatch-box.cowatch-maximized {
  top: 0 !important; left: 0 !important;
  width: 100% !important; height: 100% !important;
  border-radius: 0 !important; border: none !important;
  z-index: 25 !important;
  display: flex !important; flex-direction: column;
}
.cowatch-box.cowatch-maximized #coWatchVideo { max-height: none !important; flex: 1; min-height: 0; }
.cowatch-box.cowatch-maximized #pdf-render-wrapper { max-height: none !important; flex: 1; min-height: 0; overflow: auto; }
.cowatch-header {
  background: rgba(0,0,0,0.65); padding: 7px 12px;
  font-size: 10px; font-weight: 700; letter-spacing: 1px;
  display: flex; justify-content: space-between; align-items: center;
  color: #fff; flex-shrink: 0;
}
.close-cowatch-btn { background: none; border: none; color: rgba(255,255,255,0.45); cursor: pointer; font-size: 16px; line-height: 1; padding: 2px; display: flex; align-items: center; }
.close-cowatch-btn:hover { color: #fff; }
.cowatch-load-bar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; background: rgba(255,255,255,0.03); border-bottom: 1px solid var(--border-glass); flex-shrink: 0; }
.cowatch-load-btn { font-size: 11px; padding: 5px 12px; white-space: nowrap; flex-shrink: 0; }
.cowatch-hint { font-size: 10px; color: rgba(255,255,255,0.35); line-height: 1.3; }
#coWatchVideo { width: 100%; display: block; max-height: 210px; background: #000; object-fit: contain; }
#cowork-container { top: 15px; }
#cowork-container #pdf-render-wrapper { max-height: 340px; }

.prompter-box { position: fixed; top: 75px; left: 50%; transform: translateX(-50%); width: 40%; background: #11141c; border: 1px solid var(--accent); border-radius: 8px; padding: 10px; z-index: 999; display: none; }

/* ── GRIGLIA VIDEO MULTI-UTENTE ───────────────────────────── */
#remote-videos-container {
  position: absolute; top: 0; left: 0; width: 100%; height: 100%;
  display: grid; gap: 2px; background: #000; z-index: 1;
  grid-auto-rows: 1fr;
}
#remote-videos-container.peers-1 { grid-template-columns: 1fr; }
#remote-videos-container.peers-2 { grid-template-columns: 1fr 1fr; }
#remote-videos-container.peers-3 { grid-template-columns: repeat(3, 1fr); }
#remote-videos-container.peers-4 { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; }
#remote-videos-container.peers-9 { grid-template-columns: repeat(3, 1fr); }

.remote-video-slot { position: relative; overflow: hidden; background: #12141a; transition: outline 0.15s; }
.remote-video-slot video { width: 100%; height: 100%; object-fit: cover; }
.remote-video-slot.speaking { outline: 3px solid #00e676; outline-offset: -3px; }
#wrapper-local.speaking { outline: 3px solid #00e676; outline-offset: -2px; }

.remote-video-label {
  position: absolute; bottom: 8px; left: 10px;
  background: rgba(0,0,0,0.65); color: #fff;
  font-size: 11px; font-weight: 600;
  padding: 2px 8px; border-radius: 4px; z-index: 5;
}

/* Indicatore qualità segnale — visibile all'hover */
.signal-badge {
  position: absolute; top: 8px; right: 10px;
  display: flex; align-items: flex-end; gap: 2px;
  padding: 4px 5px; background: rgba(0,0,0,0.55); border-radius: 4px;
  opacity: 0; transition: opacity 0.2s; z-index: 6; cursor: default;
}
.remote-video-slot:hover .signal-badge { opacity: 1; }
.signal-badge .bar { width: 3px; border-radius: 1px; background: rgba(255,255,255,0.2); }
.signal-badge .b1 { height: 4px; }
.signal-badge .b2 { height: 7px; }
.signal-badge .b3 { height: 10px; }
.signal-badge .b4 { height: 14px; }
.signal-badge.sig-good .bar                                        { background: #00cc66; }
.signal-badge.sig-ok .b1,.signal-badge.sig-ok .b2,.signal-badge.sig-ok .b3 { background: #ffcc00; }
.signal-badge.sig-bad .b1,.signal-badge.sig-bad .b2               { background: #ff7700; }
.signal-badge.sig-poor .b1                                         { background: #ff2200; }

/* Badge remoti sovrapposti al container (z-index sopra i video) */
#remote-mic-muted-badge { position: absolute; top: 15px; left: 15px; z-index: 30; }
#remote-rec-badge       { position: absolute; top: 15px; right: 15px; z-index: 30; }
#privacy-remote         { position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 29; }

/* Badge per-slot multi-peer (microfono e camera) */
.slot-mic-badge {
  position: absolute; top: 8px; left: 8px;
  background: rgba(255,59,48,0.85); color: white;
  padding: 3px 7px; border-radius: 4px; font-size: 10px; font-weight: 600;
  display: none; z-index: 6; pointer-events: none;
}
.slot-cam-off {
  position: absolute; top: 0; left: 0; width: 100%; height: 100%;
  background: #151821; color: rgba(255,255,255,0.45);
  display: none; flex-direction: column; align-items: center; justify-content: center;
  font-size: 13px; gap: 4px; z-index: 4;
}

/* ── FIX#8: INDICATORE QUALITÀ DI RETE ──────────────────────────── */
#net-quality {
  font-size: 11px; font-weight: 600;
  padding: 3px 10px; border-radius: 12px;
  border: 1px solid var(--border-glass);
  background: rgba(128,128,128,0.08);
  margin-left: 8px; cursor: default;
  display: none; /* mostrato da startNetworkMonitor() */
}
.prompter-box textarea { width: 100%; height: 80px; background: rgba(0,0,0,0.2); border: 1px solid var(--border-glass); color: #fff; padding: 6px; font-size: 12px; resize: none; }