:root {
  --ink: #1c2b27;
  --paper: #f4f6f5;
  --panel: #ffffff;
  --line: #e2e6e4;
  --accent: #1f8a6f;
  --accent-dark: #146b56;
  --muted: #6b7b76;
  --bubble-in: #ffffff;
  --bubble-out: #dcf3ea;
  --danger: #b23b3b;
  --chat-bg: #eef2f0;
  font-size: 15px;
}

html[data-theme="dark"] {
  --ink: #e7f1ec;
  --paper: #0e1815;
  --panel: #16221e;
  --line: #263832;
  --accent: #34b78e;
  --accent-dark: #2a9575;
  --muted: #83988f;
  --bubble-in: #1d2b26;
  --bubble-out: #1f4438;
  --danger: #e08079;
  --chat-bg: #0b1512;
}

body,
.contacts-pane,
.chat-pane,
.bubble,
.login-card,
.modal-card,
.chat-header,
.chat-input-row,
.pane-header {
  transition: background-color 0.2s ease, color 0.2s ease, border-color 0.2s ease;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--paper);
  color: var(--ink);
  height: 100vh;
  overflow: hidden;
}

.hidden { display: none !important; }

/* ---------- Login ---------- */
.login-screen {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, #0f2921 0%, #163f33 55%, #1f8a6f 140%);
}

.login-card {
  background: var(--panel);
  padding: 40px 36px;
  border-radius: 14px;
  width: 320px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
}

.login-mark {
  width: 44px;
  height: 44px;
  border-radius: 10px;
  background: var(--accent);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 20px;
  margin-bottom: 16px;
}

.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-sub { margin: 0 0 24px; color: var(--muted); font-size: 13px; }

.login-card label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 14px;
}

.login-card input {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  font-size: 14px;
}

.login-card button {
  width: 100%;
  padding: 11px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: white;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  margin-top: 6px;
}
.login-card button:hover { background: var(--accent-dark); }

.login-error { color: var(--danger); font-size: 13px; min-height: 18px; margin: 10px 0 0; }

/* ---------- App shell ---------- */
.app {
  display: grid;
  grid-template-columns: 84px 300px 1fr;
  height: 100vh;
}

.accounts-rail {
  background: #0f2921;
  padding: 16px 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  overflow-y: auto;
}

.account-tab {
  width: 52px;
  height: 52px;
  border-radius: 14px;
  background: #17392f;
  color: #cfe9df;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 15px;
  cursor: pointer;
  border: 2px solid transparent;
  position: relative;
}
.account-tab.active {
  background: var(--accent);
  color: white;
  border-color: #cfe9df;
}
.account-tab .badge {
  position: absolute;
  top: -4px;
  right: -4px;
  background: #e35b4f;
  color: white;
  font-size: 10px;
  min-width: 16px;
  height: 16px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
}

/* ---------- Contacts pane ---------- */
.contacts-pane {
  border-right: 1px solid var(--line);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.pane-header {
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 600;
}

.ghost-btn {
  background: none;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 10px;
  font-size: 12px;
  color: var(--muted);
  cursor: pointer;
}
.ghost-btn:hover { background: var(--paper); }

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

.empty-hint { color: var(--muted); padding: 20px; font-size: 13px; }

.contact-row {
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.contact-row:hover { background: var(--paper); }
.contact-row.active { background: var(--bubble-out); }

.contact-name { font-weight: 600; font-size: 14px; }
.contact-meta { font-size: 12px; color: var(--muted); display: flex; justify-content: space-between; }

/* ---------- Chat pane ---------- */
.chat-pane {
  display: flex;
  flex-direction: column;
  background: var(--chat-bg, #eef2f0);
}

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

.chat-active { display: flex; flex-direction: column; height: 100%; }
.chat-active.hidden { display: none; }

.chat-header {
  padding: 16px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  column-gap: 2px;
}
.chat-contact-name { font-weight: 600; grid-column: 2; }
.chat-window-status { font-size: 12px; color: var(--muted); margin-top: 2px; grid-column: 2; }
.chat-window-status.closed { color: var(--danger); }
.back-btn { grid-row: 1 / 3; }

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.bubble {
  max-width: 62%;
  padding: 9px 13px;
  border-radius: 12px;
  font-size: 14px;
  line-height: 1.4;
  box-shadow: 0 1px 1px rgba(0,0,0,0.05);
}
.bubble.in { align-self: flex-start; background: var(--bubble-in); border-top-left-radius: 3px; }
.bubble.out { align-self: flex-end; background: var(--bubble-out); border-top-right-radius: 3px; }
.bubble .time { display: block; font-size: 10px; color: var(--muted); margin-top: 4px; text-align: right; }
.msg-media { font-style: italic; color: var(--muted); }
.msg-image, .msg-video {
  display: block;
  max-width: 260px;
  max-height: 260px;
  border-radius: 8px;
}
.msg-audio { display: block; max-width: 240px; }
.msg-caption { margin-top: 6px; font-size: 13px; }
.bubble:has(.msg-image), .bubble:has(.msg-video) { padding: 6px; }

.chat-input-row {
  display: flex;
  gap: 10px;
  padding: 14px 18px;
  background: var(--panel);
  border-top: 1px solid var(--line);
}
.chat-input-row input {
  flex: 1;
  padding: 11px 14px;
  border-radius: 20px;
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  font-size: 14px;
}
.chat-input-row button {
  padding: 0 20px;
  border: none;
  border-radius: 20px;
  background: var(--accent);
  color: white;
  font-weight: 600;
  cursor: pointer;
}
.chat-input-row button:hover { background: var(--accent-dark); }

.mic-btn,
.chat-input-row .mic-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  font-size: 17px;
  cursor: pointer;
  padding: 0;
  flex-shrink: 0;
}
.chat-input-row .mic-btn:hover { background: var(--paper); }
.chat-input-row .mic-btn.active { background: #e35b4f; border-color: #e35b4f; color: white; }

.recording-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 18px;
  background: #fff5f4;
  border-top: 1px solid #f3c9c4;
  font-size: 13px;
  color: var(--ink);
}
.recording-bar.hidden { display: none; }
.rec-dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: #e35b4f;
  animation: rec-pulse 1s infinite;
}
@keyframes rec-pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.3; } }
.recording-label { color: var(--muted); margin-right: auto; }
.send-rec-btn {
  padding: 7px 16px;
  border: none;
  border-radius: 16px;
  background: var(--accent);
  color: white;
  font-weight: 600;
  font-size: 12px;
  cursor: pointer;
}
.send-rec-btn:hover { background: var(--accent-dark); }

.back-btn {
  display: none;
  background: none;
  border: none;
  font-size: 20px;
  padding: 0 12px 0 0;
  cursor: pointer;
  color: var(--ink);
}

@media (max-width: 860px) {
  .app { grid-template-columns: 64px 1fr; }

  /* On mobile we show ONE of {contacts, chat} at a time,
     controlled by the .show-chat class toggled in app.js */
  .contacts-pane { display: flex; }
  .chat-pane { display: none; }
  .app.show-chat .contacts-pane { display: none; }
  .app.show-chat .chat-pane { display: flex; }

  .back-btn { display: inline-block; }
}

/* ---------- Add account ---------- */
.add-account-tab {
  width: 52px;
  height: 52px;
  border-radius: 14px;
  background: transparent;
  border: 2px dashed #2f4f44;
  color: #7fb8a5;
  font-size: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.add-account-tab:hover { border-color: var(--accent); color: white; }

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 41, 33, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}
.modal-overlay.hidden { display: none; }

.modal-card {
  background: var(--panel);
  width: 360px;
  max-width: calc(100vw - 40px);
  border-radius: 14px;
  padding: 28px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.modal-card h2 { margin: 0 0 6px; font-size: 18px; }
.modal-sub { margin: 0 0 20px; font-size: 12.5px; color: var(--muted); line-height: 1.5; }
.modal-card label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 14px;
}
.modal-card .optional { font-weight: 400; opacity: 0.7; }
.modal-card input {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 9px 11px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  font-size: 14px;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 6px;
}
.modal-actions button[type="submit"] {
  padding: 9px 18px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: white;
  font-weight: 600;
  cursor: pointer;
}
.modal-actions button[type="submit"]:hover { background: var(--accent-dark); }

.account-tab-wrap { position: relative; }
.remove-account-btn {
  position: absolute;
  top: -4px;
  right: -4px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #e35b4f;
  color: white;
  border: none;
  font-size: 10px;
  line-height: 1;
  cursor: pointer;
  display: none;
}
.account-tab-wrap:hover .remove-account-btn { display: block; }

html[data-theme="dark"] .recording-bar {
  background: #2a1613;
  border-top-color: #4a2420;
}

/* ---------- Theme toggle switch ---------- */
.header-actions { display: flex; align-items: center; gap: 10px; }

.theme-switch {
  width: 46px;
  height: 26px;
  border-radius: 13px;
  border: 1px solid var(--line);
  background: var(--paper);
  position: relative;
  cursor: pointer;
  padding: 0;
  flex-shrink: 0;
}
.theme-switch-knob {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--panel);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.2);
  transition: left 0.2s ease;
}
html[data-theme="dark"] .theme-switch-knob {
  left: 22px;
}
