/* Mesh Messenger - WhatsApp-style dark theme */
:root {
  --bg-primary: #111b21;
  --bg-secondary: #202c33;
  --bg-chat: #0b141a;
  --bg-bubble-sent: #005c4b;
  --bg-bubble-recv: #202c33;
  --bg-input: #2a3942;
  --bg-hover: #182229;
  --bg-active: #2a3942;
  --text-primary: #e9edef;
  --text-secondary: #8696a0;
  --text-bubble: #e9edef;
  --text-time: rgba(233,237,239,0.6);
  --accent: #00a884;
  --accent-hover: #00c49a;
  --border: #2a3942;
  --danger: #ef4444;
  --online: #00a884;
  --offline: #8696a0;
  --unread: #00a884;
  --shadow: rgba(0,0,0,0.3);
}

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

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

/* App container */
#app {
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* Setup modal */
#setup-modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.8);
  padding: 20px;
}

#setup-modal.hidden { display: none; }

.setup-card {
  background: var(--bg-secondary);
  border-radius: 16px;
  padding: 40px 32px;
  max-width: 400px;
  width: 100%;
  text-align: center;
}

.setup-card h1 {
  font-size: 24px;
  margin-bottom: 8px;
  color: var(--accent);
}

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

.setup-card input {
  width: 100%;
  padding: 12px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text-primary);
  font-size: 16px;
  outline: none;
  margin-bottom: 16px;
}

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

.setup-card .setup-mqtt {
  text-align: left;
  margin-bottom: 16px;
}

.setup-card .setup-mqtt label {
  display: block;
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 4px;
  margin-top: 8px;
}

.setup-card button {
  width: 100%;
  padding: 12px;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: white;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.setup-card button:hover { background: var(--accent-hover); }
.setup-card button:disabled { opacity: 0.5; cursor: not-allowed; }

/* PIN gate */
#pin-modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.9);
  padding: 20px;
}

#pin-modal.hidden { display: none; }

/* Main layout */
#main {
  display: flex;
  height: 100%;
  overflow: hidden;
}

#main.hidden { display: none; }

/* Sidebar */
#sidebar {
  width: 360px;
  min-width: 300px;
  background: var(--bg-primary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100%;
}

#sidebar-header {
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg-secondary);
  min-height: 56px;
}

#sidebar-header h1 {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
}

#connection-badge {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  padding: 4px 10px;
  border-radius: 12px;
  background: var(--bg-input);
}

#connection-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--offline);
  transition: background 0.3s;
}

#connection-dot.connected { background: var(--online); }
#connection-dot.reconnecting { background: #f59e0b; animation: pulse 1s infinite; }
#connection-dot.error { background: var(--danger); }

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

#contact-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.contact-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid rgba(134,150,160,0.08);
  transition: background 0.15s;
  gap: 12px;
}

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

.contact-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--bg-active);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
  position: relative;
}

.contact-avatar .online-dot {
  position: absolute;
  bottom: 1px;
  right: 1px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--online);
  border: 2px solid var(--bg-primary);
}

.contact-avatar .online-dot.offline { background: transparent; }

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

.contact-name {
  font-size: 16px;
  font-weight: 500;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.contact-name .time {
  font-size: 12px;
  color: var(--text-secondary);
  font-weight: 400;
}

.contact-preview {
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 2px;
}

.unread-badge {
  background: var(--unread);
  color: white;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 10px;
  min-width: 20px;
  text-align: center;
  flex-shrink: 0;
}

/* Chat area */
#chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg-chat);
  height: 100%;
}

#chat-area.hidden { display: none; }

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

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

#chat-header-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--bg-active);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
}

#chat-header-info {
  flex: 1;
}

#chat-header-name {
  font-size: 16px;
  font-weight: 600;
}

#chat-header-status {
  font-size: 12px;
  color: var(--text-secondary);
}

/* Messages */
#messages {
  flex: 1;
  overflow-y: auto;
  padding: 8px 16px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  -webkit-overflow-scrolling: touch;
}

.msg-date-divider {
  text-align: center;
  padding: 8px 0;
}

.msg-date-divider span {
  background: var(--bg-secondary);
  color: var(--text-secondary);
  font-size: 12px;
  padding: 4px 12px;
  border-radius: 8px;
}

.msg-bubble {
  max-width: 75%;
  padding: 6px 8px 4px 8px;
  border-radius: 8px;
  position: relative;
  word-wrap: break-word;
  line-height: 1.35;
}

.msg-bubble.sent {
  align-self: flex-end;
  background: var(--bg-bubble-sent);
  border-top-right-radius: 2px;
}

.msg-bubble.received {
  align-self: flex-start;
  background: var(--bg-bubble-recv);
  border-top-left-radius: 2px;
}

.msg-sender {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 2px;
}

.msg-text {
  font-size: 14.5px;
  color: var(--text-bubble);
  padding-right: 48px;
}

.msg-meta {
  float: right;
  font-size: 11px;
  color: var(--text-time);
  margin-top: 2px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.msg-check { font-size: 14px; }

/* System messages */
.msg-system {
  align-self: center;
  background: var(--bg-secondary);
  color: var(--text-secondary);
  font-size: 12px;
  padding: 4px 12px;
  border-radius: 8px;
  opacity: 0.8;
}

/* Input area */
#input-area {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  gap: 8px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
}

#msg-input {
  flex: 1;
  padding: 10px 16px;
  border-radius: 8px;
  border: none;
  background: var(--bg-input);
  color: var(--text-primary);
  font-size: 15px;
  outline: none;
  min-height: 42px;
  max-height: 100px;
  resize: none;
  font-family: inherit;
}

#msg-input::placeholder { color: var(--text-secondary); }

#send-btn {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: white;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, transform 0.1s;
  flex-shrink: 0;
}

#send-btn:hover { background: var(--accent-hover); }
#send-btn:active { transform: scale(0.95); }
#send-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* Empty state */
#empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--bg-chat);
  color: var(--text-secondary);
  gap: 12px;
}

#empty-state.hidden { display: none; }

#empty-state svg {
  width: 80px;
  height: 80px;
  opacity: 0.3;
}

#empty-state p {
  font-size: 14px;
}

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(134,150,160,0.3); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: rgba(134,150,160,0.5); }

/* Mobile responsive */
@media (max-width: 768px) {
  #sidebar {
    width: 100%;
    min-width: unset;
    position: absolute;
    z-index: 10;
    left: 0;
    top: 0;
    bottom: 0;
    transition: transform 0.25s ease;
  }

  #sidebar.slide-out {
    transform: translateX(-100%);
  }

  #back-btn { display: block; }

  #empty-state { display: none !important; }

  #chat-area {
    position: absolute;
    inset: 0;
    z-index: 5;
  }

  .msg-bubble { max-width: 85%; }
}

/* Notification toast */
.toast {
  position: fixed;
  top: 16px;
  right: 16px;
  background: var(--bg-secondary);
  color: var(--text-primary);
  padding: 12px 20px;
  border-radius: 8px;
  box-shadow: 0 4px 12px var(--shadow);
  z-index: 1000;
  animation: slideIn 0.3s ease, slideOut 0.3s ease 2.7s;
  border-left: 3px solid var(--accent);
}

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

/* Settings gear */
#settings-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 18px;
  padding: 4px;
}

#settings-btn:hover { color: var(--text-primary); }

/* Settings panel */
#settings-panel {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

#settings-panel.hidden { display: none; }

.settings-card {
  background: var(--bg-secondary);
  border-radius: 12px;
  padding: 24px;
  max-width: 400px;
  width: 100%;
}

.settings-card h2 {
  margin-bottom: 16px;
  font-size: 18px;
}

.settings-card label {
  display: block;
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 4px;
  margin-top: 12px;
}

.settings-card input, .settings-card select {
  width: 100%;
  padding: 8px 12px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text-primary);
  font-size: 14px;
  outline: none;
}

.settings-card .btn-row {
  display: flex;
  gap: 8px;
  margin-top: 20px;
}

.settings-card button {
  flex: 1;
  padding: 10px;
  border-radius: 8px;
  border: none;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
}

.btn-primary { background: var(--accent); color: white; }
.btn-secondary { background: var(--bg-input); color: var(--text-primary); }
.btn-danger { background: var(--danger); color: white; }
