:root {
  --bg: #0a0a0a;
  --surface: #111111;
  --border: #222222;
  --border-muted: #333333;
  --input-bg: #1a1a1a;
  --text: #e0e0e0;
  --text-muted: #888888;
  --text-dim: #666666;
  --id-color: #4ade80;
  --msg-received-bg: #222222;
  --msg-received-text: #e0e0e0;
}

body.light {
  --bg: #f0f4f8;
  --surface: #ffffff;
  --border: #dde1e7;
  --border-muted: #c8cdd5;
  --input-bg: #f8f9fa;
  --text: #1a1a2e;
  --text-muted: #6c757d;
  --text-dim: #adb5bd;
  --id-color: #16a34a;
  --msg-received-bg: #e9ecef;
  --msg-received-text: #1a1a2e;
}

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

body {
  font-family: system-ui, -apple-system, sans-serif;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

/* ── Header ──────────────────────────────────────────────────── */
#top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#identity {
  display: flex;
  align-items: center;
  gap: 8px;
}

#identity-label {
  font-size: 13px;
  color: var(--text-muted);
}

#my-id {
  font-family: monospace;
  font-size: 14px;
  color: var(--id-color);
}

#header-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

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

/* ── App layout ──────────────────────────────────────────────── */
#app {
  flex: 1;
  display: flex;
  overflow: hidden;
}

/* ── Sidebar ─────────────────────────────────────────────────── */
#sidebar {
  width: 240px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
}

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

#sidebar-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

#btn-add-contact {
  padding: 4px 10px;
  font-size: 18px;
  line-height: 1;
  background: transparent;
  border: 1px solid var(--border-muted);
}

#contact-list {
  flex: 1;
  overflow-y: auto;
  list-style: none;
  padding: 6px 0;
}

.contact-item {
  display: flex;
  align-items: center;
  padding: 10px 14px;
  cursor: pointer;
  gap: 8px;
  border-radius: 6px;
  margin: 2px 6px;
}

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

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

.contact-item.unsaved .contact-name {
  color: var(--text-muted);
  font-style: italic;
}

.contact-name {
  flex: 1;
  font-size: 14px;
  font-family: monospace;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.contact-actions {
  display: flex;
  gap: 4px;
  opacity: 0;
  flex-shrink: 0;
}

.contact-item:hover .contact-actions {
  opacity: 1;
}

.contact-save,
.contact-delete {
  padding: 2px 7px;
  font-size: 12px;
  line-height: 1.4;
  background: transparent;
  border: 1px solid var(--border-muted);
}

.contact-save {
  color: #4ade80;
  border-color: #4ade8040;
}

.contact-delete {
  color: var(--text-muted);
}

.unread-badge {
  background: #1d4ed8;
  color: #fff;
  border-radius: 10px;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  font-size: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

/* ── Chat panel ──────────────────────────────────────────────── */
#chat-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#chat-placeholder {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  font-size: 14px;
}

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.msg {
  max-width: 70%;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 14px;
  word-wrap: break-word;
}

.msg-sent {
  align-self: flex-end;
  background: #1d4ed8;
  color: #fff;
}

.msg-received {
  align-self: flex-start;
  background: var(--msg-received-bg);
  color: var(--msg-received-text);
}

.msg-system {
  align-self: center;
  color: var(--text-dim);
  font-size: 12px;
}

/* ── Typing indicator ────────────────────────────────────────── */
#typing-indicator {
  height: 20px;
  padding: 0 20px;
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
  flex-shrink: 0;
}

/* ── Message form ────────────────────────────────────────────── */
#message-form {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#message-input {
  flex: 1;
  padding: 10px 14px;
  background: var(--input-bg);
  border: 1px solid var(--border-muted);
  border-radius: 8px;
  color: var(--text);
  font-size: 14px;
}

#message-input:focus {
  outline: none;
  border-color: rgb(90, 90, 90);
}

/* ── Buttons ─────────────────────────────────────────────────── */
button {
  padding: 8px 16px;
  background: var(--surface);
  border: 1px solid var(--border-muted);
  border-radius: 6px;
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
}

button:hover {
  background: var(--border);
}

#btn-send {
  background: #024a01;
  border-color: #0c4006;
  color: #fff;
}

#btn-send:hover {
  background: #0d6201;
  transition: background .2s ease-in;
}

#btn-refresh {
  background: #531a09;
  border-color: #611f09;
  color: #fff;
}

#btn-refresh:hover {
  background: #9a3412;
  transition: background .2s ease-in;
}

/* ── Add contact modal ───────────────────────────────────────── */
#add-contact-modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

#add-contact-modal[hidden] { display: none; }

#add-contact-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
}

#add-contact-box {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border-muted);
  padding: 24px;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 320px;
}

#add-contact-box h3 {
  font-size: 15px;
  color: var(--text);
}

#add-contact-code,
#add-contact-nickname {
  padding: 9px 12px;
  background: var(--input-bg);
  border: 1px solid var(--border-muted);
  border-radius: 6px;
  color: var(--text);
  font-size: 14px;
  font-family: monospace;
}

#add-contact-code:focus,
#add-contact-nickname:focus {
  outline: none;
  border-color: rgb(90, 90, 90);
}

#add-contact-nickname {
  font-family: system-ui, -apple-system, sans-serif;
}

#add-contact-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

#add-contact-confirm {
  background: #024a01;
  border-color: #0c4006;
  color: #fff;
}

#add-contact-confirm:hover {
  background: #0d6201;
}

/* ── QR modal ────────────────────────────────────────────────── */
#qr-modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

#qr-modal[hidden] { display: none; }

#qr-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
}

#qr-box {
  position: relative;
  background: #ffffff;
  padding: 28px;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

#qr-label {
  font-size: 13px;
  color: #555;
}

#qr-close {
  background: #222;
  border-color: #333;
  color: #fff;
}

#qr-close:hover {
  background: #444;
}

/* ── Chat header (mobile only) ───────────────────────────────── */
#chat-header {
  display: none;
}

/* ── Responsive ──────────────────────────────────────────────── */
@media (max-width: 640px) {
  #identity-label { display: none; }

  #my-id {
    max-width: 110px;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  #sidebar {
    width: 100%;
  }

  #chat-panel {
    display: none;
    width: 100%;
  }

  #app.chat-active #sidebar {
    display: none;
  }

  #app.chat-active #chat-panel {
    display: flex;
  }

  #chat-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
  }

  #btn-back {
    padding: 6px 10px;
    font-size: 16px;
  }

  #chat-contact-name {
    font-size: 14px;
    font-family: monospace;
    color: var(--text);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .msg { max-width: 85%; }

  #add-contact-box { width: calc(100vw - 32px); }
}
