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

:root {
  --bg: #111b21;
  --bg-secondary: #182229;
  --bg-chat: #0b141a;
  --bg-input: #2a3942;
  --bg-hover: #202c33;
  --bg-selected: #2a3942;
  --text: #e9edef;
  --text-secondary: #8696a0;
  --text-muted: #667781;
  --border: #313d45;
  --primary: #00a884;
  --primary-hover: #06cf9c;
  --danger: #ef5350;
  --msg-incoming: #202c33;
  --msg-outgoing: #005c4b;
  --shadow: rgba(0,0,0,0.3);
  --radius: 8px;
  --sidebar-width: 380px;
}

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

#app {
  height: 100%;
  display: flex;
}

a {
  color: var(--primary);
  text-decoration: none;
}

/* ── Login ── */

.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  width: 100%;
  background: var(--bg);
}

.login-box {
  background: var(--bg-secondary);
  padding: 40px;
  border-radius: 12px;
  width: 360px;
  box-shadow: 0 8px 32px var(--shadow);
}

.login-box h1 {
  font-size: 24px;
  margin-bottom: 8px;
  color: var(--primary);
}

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

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

.login-box input:focus {
  border-color: var(--primary);
}

.login-box button {
  width: 100%;
  padding: 12px;
  background: var(--primary);
  border: none;
  border-radius: var(--radius);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.login-box button:hover {
  background: var(--primary-hover);
}

.login-error {
  color: var(--danger);
  font-size: 13px;
  margin-top: 12px;
  text-align: center;
}

/* ── Dashboard ── */

.dashboard {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
}

.dashboard-header {
  background: var(--bg-secondary);
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.dashboard-header h1 {
  font-size: 18px;
  font-weight: 600;
}

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

.btn {
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

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

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

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

.btn-outline:hover {
  background: var(--bg-hover);
}

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

.device-grid {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
}

.device-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  cursor: pointer;
  transition: border-color 0.2s, transform 0.1s;
}

.device-card:hover {
  border-color: var(--primary);
  transform: translateY(-1px);
}

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

.device-card .device-model {
  color: var(--text-secondary);
  font-size: 13px;
  margin-bottom: 12px;
}

.device-card .device-meta {
  display: flex;
  justify-content: space-between;
  color: var(--text-muted);
  font-size: 12px;
}

.device-card .status {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.device-card .status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}

.device-card .status-dot.online {
  background: var(--primary);
}

.device-card .status-dot.offline {
  background: var(--text-muted);
}

/* ── Layout: sidebar + chat ── */

.main-layout {
  display: flex;
  width: 100%;
  height: 100%;
}

.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.sidebar-header h2 {
  font-size: 16px;
  font-weight: 600;
}

.sidebar-search {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}

.sidebar-search input {
  width: 100%;
  padding: 8px 12px;
  background: var(--bg-input);
  border: none;
  border-radius: var(--radius);
  color: var(--text);
  font-size: 13px;
  outline: none;
}

.sidebar-search input::placeholder {
  color: var(--text-muted);
}

.contact-list {
  flex: 1;
  overflow-y: auto;
}

.contact-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  cursor: pointer;
  gap: 12px;
  border-bottom: 1px solid transparent;
  transition: background 0.15s;
}

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

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

.contact-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 600;
  color: #fff;
  flex-shrink: 0;
}

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

.contact-info .name {
  font-weight: 500;
  font-size: 14px;
  margin-bottom: 2px;
}

.contact-info .last-msg {
  color: var(--text-secondary);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.contact-info .time {
  color: var(--text-muted);
  font-size: 11px;
  flex-shrink: 0;
}

.contact-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
}

.platform-badge {
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--bg-hover);
  color: var(--text-secondary);
  text-transform: uppercase;
}

/* ── Chat View ── */

.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg-chat);
}

.chat-placeholder {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: 15px;
}

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

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

.chat-header .contact-name {
  font-weight: 600;
  font-size: 15px;
}

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

.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 20px 60px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.message-wrapper {
  display: flex;
  flex-direction: column;
  max-width: 65%;
}

.message-wrapper.incoming {
  align-self: flex-start;
}

.message-wrapper.outgoing {
  align-self: flex-end;
}

.message-bubble {
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.5;
  position: relative;
  word-wrap: break-word;
}

.message-wrapper.incoming .message-bubble {
  background: var(--msg-incoming);
  border-top-left-radius: 2px;
}

.message-wrapper.outgoing .message-bubble {
  background: var(--msg-outgoing);
  border-top-right-radius: 2px;
}

.message-time {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
  padding: 0 4px;
  text-align: right;
}

.message-wrapper.incoming .message-time {
  text-align: left;
}

.message-media {
  max-width: 300px;
  border-radius: 6px;
  margin-bottom: 4px;
}

.message-text {
  margin: 0;
}

/* ── Responsive ── */

@media (max-width: 768px) {
  .sidebar {
    width: 100%;
  }
  .chat-area {
    display: none;
  }
  .chat-area.visible {
    display: flex;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 10;
  }
  .chat-header .back-btn {
    display: block;
  }
  .messages-container {
    padding: 12px 16px;
  }
}

/* ── Loading ── */

.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: var(--text-muted);
}

.loading::after {
  content: '';
  width: 20px;
  height: 20px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
  margin-left: 8px;
}

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

/* ── OTA ── */

.ota-section {
  padding: 16px;
  border-top: 1px solid var(--border);
}

.ota-section h3 {
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.ota-controls {
  display: flex;
  gap: 8px;
}
