/* === Reset & Variabili === */
:root {
  --bg: #f0f2f5;
  --bg-chat: #e5ddd5;
  --bg-header: #075e54;
  --bg-input: #ffffff;
  --bg-bubble-mine: #dcf8c6;
  --bg-bubble-other: #ffffff;
  --bg-card: #ffffff;
  --text: #111b21;
  --text-secondary: #667781;
  --text-header: #ffffff;
  --accent: #25d366;
  --accent-hover: #1ebe57;
  --border: #e0e0e0;
  --shadow: rgba(0,0,0,0.08);
  --bubble-shadow: rgba(0,0,0,0.04);
}

[data-theme="dark"] {
  --bg: #111b21;
  --bg-chat: #0b141a;
  --bg-header: #202c33;
  --bg-input: #202c33;
  --bg-bubble-mine: #005c4b;
  --bg-bubble-other: #202c33;
  --bg-card: #1f2c34;
  --text: #e9edef;
  --text-secondary: #8696a0;
  --text-header: #e9edef;
  --accent: #00a884;
  --accent-hover: #00c996;
  --border: #2a3942;
  --shadow: rgba(0,0,0,0.3);
  --bubble-shadow: rgba(0,0,0,0.1);
}

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

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

/* === Setup === */
#setup-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 20px;
  background: linear-gradient(135deg, #075e54 0%, #128c7e 50%, #25d366 100%);
}

.setup-card {
  background: var(--bg-card);
  border-radius: 16px;
  padding: 40px 32px;
  max-width: 420px;
  width: 100%;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}

.setup-logo { color: var(--accent); margin-bottom: 16px; }
.setup-card h1 { font-size: 28px; font-weight: 700; margin-bottom: 8px; }
.setup-card > p { color: var(--text-secondary); margin-bottom: 28px; }

.setup-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 28px;
}

.setup-form label { text-align: left; font-weight: 600; font-size: 14px; }

.setup-form input {
  padding: 12px 16px;
  border: 2px solid var(--border);
  border-radius: 10px;
  font-size: 16px;
  outline: none;
  background: var(--bg);
  color: var(--text);
  transition: border-color 0.2s;
}

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

#btn-start {
  padding: 14px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

#btn-start:hover { background: var(--accent-hover); }

.qr-section { border-top: 1px solid var(--border); padding-top: 20px; }
.qr-section > p:first-child { color: var(--text-secondary); font-size: 13px; margin-bottom: 12px; }
.qr-section img { width: 180px; height: 180px; border-radius: 8px; }

.qr-url {
  font-family: 'SF Mono', 'Consolas', monospace;
  font-size: 14px;
  color: var(--accent);
  margin-top: 8px;
  word-break: break-all;
  font-weight: 600;
}

/* === Chat Screen === */
#chat-screen { display: flex; flex-direction: column; height: 100dvh; }
.hidden { display: none !important; }

/* === Header === */
.chat-header {
  background: var(--bg-header);
  color: var(--text-header);
  padding: 10px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  z-index: 10;
  box-shadow: 0 2px 4px var(--shadow);
}

.header-left { display: flex; align-items: center; gap: 12px; }
.header-icon { opacity: 0.9; }
.header-left h2 { font-size: 17px; font-weight: 600; }

.status-online { font-size: 12px; opacity: 0.8; color: #7fff7f; }
.status-offline { font-size: 12px; color: #ff6b6b; }
.status-reconnecting { font-size: 12px; color: #ffa726; animation: pulse 1.5s infinite; }

@keyframes pulse { 0%, 100% { opacity: 0.5; } 50% { opacity: 1; } }

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

.icon-btn {
  background: none;
  border: none;
  color: var(--text-header);
  cursor: pointer;
  padding: 6px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
}

.icon-btn:hover { background: rgba(255,255,255,0.15); }

.user-count {
  background: rgba(255,255,255,0.2);
  padding: 2px 10px;
  border-radius: 12px;
  font-size: 13px;
  font-weight: 600;
}

/* === Connection Banner === */
.connection-banner {
  background: #ff5252;
  color: #fff;
  padding: 8px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
  font-weight: 600;
  flex-shrink: 0;
  animation: slideDown 0.3s ease;
}

.connection-banner button {
  background: rgba(255,255,255,0.25);
  border: none;
  color: #fff;
  padding: 4px 12px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.connection-banner button:hover { background: rgba(255,255,255,0.4); }

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

/* === Toast === */
.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: rgba(0,0,0,0.8);
  color: #fff;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 14px;
  z-index: 500;
  opacity: 0;
  transition: opacity 0.3s, transform 0.3s;
  pointer-events: none;
}

.toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* === Batch Progress === */
.batch-progress {
  background: var(--bg-header);
  padding: 6px 16px 8px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.batch-bar { flex: 1; height: 4px; background: rgba(255,255,255,0.2); border-radius: 2px; overflow: hidden; }
.batch-fill { height: 100%; background: var(--accent); border-radius: 2px; width: 0%; transition: width 0.3s; }
.batch-text { color: rgba(255,255,255,0.8); font-size: 12px; font-weight: 600; white-space: nowrap; }

/* === Area Messaggi === */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  background: var(--bg-chat);
  display: flex;
  flex-direction: column;
  gap: 4px;
  position: relative;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

.messages::-webkit-scrollbar { width: 6px; }
.messages::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.2); border-radius: 3px; }

/* === Messaggi === */
.msg {
  max-width: 75%;
  padding: 8px 12px;
  border-radius: 10px;
  position: relative;
  word-wrap: break-word;
  box-shadow: 0 1px 1px var(--bubble-shadow);
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

.msg.mine { background: var(--bg-bubble-mine); align-self: flex-end; border-bottom-right-radius: 3px; }
.msg.other { background: var(--bg-bubble-other); align-self: flex-start; border-bottom-left-radius: 3px; }
.msg-sender { font-size: 12px; font-weight: 700; color: var(--accent); margin-bottom: 2px; }
.msg-text { font-size: 14.5px; line-height: 1.4; }
.msg-text a { color: #039be5; text-decoration: underline; }

.msg-footer { display: flex; align-items: center; justify-content: flex-end; gap: 6px; margin-top: 4px; }
.msg-time { font-size: 11px; color: var(--text-secondary); }

.copy-btn {
  background: none; border: none; color: var(--text-secondary);
  cursor: pointer; padding: 2px; border-radius: 4px;
  display: flex; align-items: center;
  opacity: 0; transition: opacity 0.2s, color 0.2s;
}
.msg:hover .copy-btn { opacity: 1; }
.copy-btn:hover { color: var(--accent); }
.copy-btn.copied { color: var(--accent); opacity: 1; }

/* File */
.msg-file { margin-top: 6px; }
.file-preview { max-width: 100%; max-height: 300px; border-radius: 8px; margin-bottom: 6px; cursor: pointer; }
.file-preview-video { max-width: 100%; max-height: 300px; border-radius: 8px; margin-bottom: 6px; }

.file-download {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 10px 14px; background: rgba(0,0,0,0.05);
  border-radius: 8px; text-decoration: none; color: var(--text);
  font-size: 13px; transition: background 0.2s;
}
.file-download:hover { background: rgba(0,0,0,0.1); }
.file-download svg { flex-shrink: 0; }
.file-size { color: var(--text-secondary); font-size: 11px; }
.file-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }

.copy-link-btn {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 8px 12px; background: rgba(0,0,0,0.05);
  border: none; border-radius: 8px; color: var(--accent);
  font-size: 13px; font-weight: 600; cursor: pointer;
  transition: background 0.2s;
}
.copy-link-btn:hover { background: rgba(0,0,0,0.1); }
.copy-link-btn.copied { background: var(--accent); color: #fff; }

.save-gallery-btn {
  display: block; margin-top: 6px; padding: 8px 14px;
  background: var(--accent); color: #fff; border: none;
  border-radius: 8px; font-size: 13px; font-weight: 600;
  cursor: pointer; transition: background 0.2s;
  width: 100%; text-align: center;
}
.save-gallery-btn:hover { background: var(--accent-hover); }
.save-gallery-btn:active { transform: scale(0.97); }

.msg-system {
  text-align: center; font-size: 12px; color: var(--text-secondary);
  background: rgba(0,0,0,0.05); padding: 4px 12px;
  border-radius: 8px; align-self: center; margin: 8px 0;
}

.typing-indicator { font-size: 13px; color: var(--text-secondary); padding: 4px 0; align-self: flex-start; }
.typing-dots span { animation: blink 1.4s infinite; }
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 60%, 100% { opacity: 0; } 30% { opacity: 1; } }

/* Drag overlay */
.drag-overlay {
  position: absolute; inset: 0;
  background: rgba(37, 211, 102, 0.15);
  border: 3px dashed var(--accent); border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  z-index: 20; backdrop-filter: blur(4px);
}
.drag-content { text-align: center; color: var(--accent); }
.drag-content p { margin-top: 8px; font-weight: 600; font-size: 16px; }

/* === Input Bar === */
.input-bar {
  background: var(--bg-input);
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
  border-top: 1px solid var(--border);
}

.input-actions { display: flex; gap: 2px; }

.action-btn {
  background: none; border: none; color: var(--text-secondary);
  cursor: pointer; padding: 8px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  transition: all 0.2s;
}
.action-btn:hover { background: rgba(0,0,0,0.05); color: var(--accent); }

.hd-btn {
  background: linear-gradient(135deg, #ff9800, #f57c00) !important;
  color: #fff !important;
  font-weight: 900;
  font-size: 13px;
  letter-spacing: 1px;
  border-radius: 8px !important;
  padding: 6px 10px !important;
  box-shadow: 0 2px 8px rgba(255, 152, 0, 0.35);
  min-width: 36px;
}
.hd-btn:hover { background: linear-gradient(135deg, #ffa726, #ff9800) !important; color: #fff !important; }

.input-wrap {
  flex: 1; display: flex; align-items: center;
  background: var(--bg); border-radius: 24px;
  padding: 4px 4px 4px 16px; border: 1px solid var(--border);
}

.input-wrap input {
  flex: 1; border: none; outline: none;
  font-size: 15px; background: transparent;
  color: var(--text); padding: 8px 0;
}
.input-wrap input::placeholder { color: var(--text-secondary); }

#btn-send {
  width: 40px; height: 40px; border: none;
  background: var(--accent); color: #fff;
  border-radius: 50%; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.2s, transform 0.1s; flex-shrink: 0;
}
#btn-send:hover { background: var(--accent-hover); }
#btn-send:active { transform: scale(0.92); }

/* === Modal === */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 100; backdrop-filter: blur(4px);
}

.modal-content {
  background: var(--bg-card); border-radius: 16px;
  padding: 32px; text-align: center;
  max-width: 360px; width: 90%;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.modal-content h3 { margin-bottom: 16px; }
.modal-content img { width: 200px; height: 200px; border-radius: 8px; margin-bottom: 8px; }
.modal-content button {
  margin-top: 16px; padding: 10px 32px;
  background: var(--accent); color: #fff;
  border: none; border-radius: 8px; font-size: 15px; cursor: pointer;
}

/* === Upload Progress === */
.upload-progress {
  position: fixed; bottom: 80px; left: 50%;
  transform: translateX(-50%); z-index: 50;
}

.progress-card {
  background: var(--bg-card); border-radius: 12px;
  padding: 16px 24px; min-width: 280px;
  box-shadow: 0 4px 20px var(--shadow); text-align: center;
}
.progress-card p { font-size: 13px; margin-bottom: 8px; }
.progress-bar { height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; }
.progress-fill { height: 100%; background: var(--accent); border-radius: 3px; width: 0%; transition: width 0.3s; }
#upload-percent { font-weight: 600; color: var(--accent); margin-top: 6px; margin-bottom: 0; }

/* === Fullscreen === */
.fullscreen-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.95);
  z-index: 200; display: flex; align-items: center;
  justify-content: center; cursor: zoom-out;
}
.fullscreen-overlay img { max-width: 95vw; max-height: 95vh; object-fit: contain; border-radius: 4px; }

.fullscreen-close {
  position: absolute; top: 16px; right: 16px;
  background: rgba(255,255,255,0.15); border: none;
  color: #fff; font-size: 32px; width: 48px; height: 48px;
  border-radius: 50%; cursor: pointer;
  display: flex; align-items: center; justify-content: center; line-height: 1;
}
.fullscreen-close:hover { background: rgba(255,255,255,0.3); }

/* === Annotazione === */
.annot-screen {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  background: #1a1b26; z-index: 300; display: none; flex-direction: column;
}
.annot-screen.active { display: flex; }

.annot-wrap {
  flex: 1; position: relative; overflow: hidden;
  display: flex; align-items: center; justify-content: center; background: #000;
}
.annot-wrap canvas { max-width: 100%; max-height: 100%; touch-action: none; }

.annot-bar {
  background: #1a1b26; padding: 10px 12px;
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; flex-shrink: 0; flex-wrap: wrap; border-top: 1px solid #2a2b3a;
}

.annot-colors { display: flex; gap: 6px; }
.annot-c { width: 28px; height: 28px; border-radius: 50%; cursor: pointer; transition: transform 0.15s, box-shadow 0.15s; }
.annot-c:hover { transform: scale(1.15); }
.annot-c.sel { box-shadow: 0 0 0 3px #1a1b26, 0 0 0 5px #fff; transform: scale(1.1); }

.annot-widths { display: flex; gap: 4px; }
.annot-w {
  width: 32px; height: 32px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; color: #888; transition: all 0.15s;
}
.annot-w:hover { color: #ccc; background: rgba(255,255,255,0.1); }
.annot-w.sel { color: #fff; background: rgba(255,255,255,0.15); }

.annot-btns { display: flex; gap: 6px; }
.annot-btns button {
  padding: 8px 14px; border: none; border-radius: 8px;
  font-size: 13px; font-weight: 600; cursor: pointer; transition: all 0.15s;
}
.a-cancel { background: #3b3b4f; color: #aaa; }
.a-cancel:hover { background: #4a4a5f; color: #fff; }
.a-clear { background: #3b3b4f; color: #ff6b6b; }
.a-clear:hover { background: #4a4a5f; }
.a-skip { background: #3b3b4f; color: #7aa2f7; }
.a-skip:hover { background: #4a4a5f; }
.a-send { background: var(--accent); color: #fff; }
.a-send:hover { background: var(--accent-hover); }

.annot-counter {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%);
  background: rgba(0,0,0,0.6); color: #fff; padding: 4px 14px;
  border-radius: 16px; font-size: 13px; font-weight: 600;
  pointer-events: none; display: none;
}
.annot-counter.visible { display: block; }

/* === Mobile === */
@media (max-width: 600px) {
  .setup-card { padding: 28px 20px; }
  .msg { max-width: 85%; }
  .copy-btn { opacity: 0.6; }
  .input-actions { gap: 0; }
  .action-btn { padding: 6px; }
}
