:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-alt: #eef0f3;
  --border: #dde1e6;
  --text: #1c1f24;
  --text-muted: #6b7280;
  --primary: #128c7e;
  --primary-dark: #0e6f64;
  --bubble-in: #ffffff;
  --bubble-out: #d9fdd3;
  --danger: #d0342c;
  --focus: #128c7e;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a21;
    --surface-alt: #1f232c;
    --border: #2a2f3a;
    --text: #e7e9ec;
    --text-muted: #9aa1ac;
    --primary: #1db6a3;
    --primary-dark: #17a190;
    --bubble-in: #232733;
    --bubble-out: #124a3f;
    --danger: #ff6b64;
    --focus: #1db6a3;
  }
}

* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
}

body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overflow-x: hidden;
}

button, input {
  font: inherit;
  color: inherit;
}

.view {
  min-height: 100vh;
  width: 100%;
}

/* ---------- Login ---------- */

#login-view {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
}

.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px 24px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.login-card h1 {
  margin: 0 0 2px;
  font-size: 1.4rem;
}

.subtitle {
  margin: 0 0 18px;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.login-card label {
  font-size: 0.85rem;
  margin-top: 10px;
  margin-bottom: 4px;
  color: var(--text-muted);
}

.login-card input {
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

.login-card input:focus {
  outline: 2px solid var(--focus);
  outline-offset: 1px;
}

.login-card button {
  margin-top: 18px;
  padding: 10px 12px;
  border: none;
  border-radius: 8px;
  background: var(--primary);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

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

.error-text {
  color: var(--danger);
  font-size: 0.85rem;
  margin: 8px 0 0;
}

/* ---------- App shell ---------- */

#app-view {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.app-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--primary);
  color: #fff;
  flex: 0 0 auto;
}

.app-title {
  font-weight: 600;
  flex: 1 1 auto;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conn-status {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #c9c9c9;
  flex: 0 0 auto;
}

.conn-status.online {
  background: #4ade80;
}

.conn-status.offline {
  background: #f87171;
}

.link-btn {
  background: none;
  border: none;
  color: #fff;
  text-decoration: underline;
  cursor: pointer;
  padding: 4px;
  flex: 0 0 auto;
}

.app-body {
  flex: 1 1 auto;
  display: flex;
  min-height: 0;
}

/* ---------- Conversation list ---------- */

.conversation-pane {
  width: 320px;
  max-width: 45%;
  border-right: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.pane-header {
  padding: 12px 16px;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}

.conversation-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1 1 auto;
}

.conversation-item {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.conversation-item:hover {
  background: var(--surface-alt);
}

.conversation-item.active {
  background: var(--surface-alt);
  border-left: 3px solid var(--primary);
}

.conversation-item .row1 {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-weight: 600;
}

.conversation-item .row1 time {
  font-weight: 400;
  color: var(--text-muted);
  font-size: 0.78rem;
  white-space: nowrap;
}

.conversation-item .preview {
  color: var(--text-muted);
  font-size: 0.85rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.empty-hint {
  color: var(--text-muted);
  padding: 16px;
  font-size: 0.9rem;
  text-align: center;
}

/* ---------- Chat pane ---------- */

.chat-pane {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
}

.chat-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  font-weight: 600;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
}

.back-btn {
  display: none;
  background: none;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  padding: 0 4px;
}

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

.msg {
  max-width: 75%;
  padding: 8px 10px;
  border-radius: 10px;
  font-size: 0.92rem;
  line-height: 1.35;
  word-wrap: break-word;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.08);
}

.msg.inbound {
  align-self: flex-start;
  background: var(--bubble-in);
  border: 1px solid var(--border);
  border-top-left-radius: 2px;
}

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

.msg .msg-media img {
  display: block;
  max-width: 100%;
  border-radius: 8px;
  margin-bottom: 4px;
}

.msg .msg-media a {
  color: var(--primary-dark);
}

.msg time {
  display: block;
  margin-top: 4px;
  font-size: 0.72rem;
  color: var(--text-muted);
  text-align: right;
}

.send-form {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  flex: 0 0 auto;
}

.send-form input {
  flex: 1 1 auto;
  padding: 10px 12px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: var(--bg);
  min-width: 0;
}

.send-form input:focus {
  outline: 2px solid var(--focus);
  outline-offset: 1px;
}

.send-form button {
  padding: 10px 18px;
  border: none;
  border-radius: 20px;
  background: var(--primary);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

.send-form button:hover {
  background: var(--primary-dark);
}

/* ---------- Responsive (~400px and down) ---------- */

@media (max-width: 700px) {
  .app-body {
    position: relative;
  }

  .conversation-pane {
    width: 100%;
    max-width: 100%;
  }

  .chat-pane {
    position: absolute;
    inset: 0;
    transform: translateX(100%);
    transition: transform 0.15s ease;
    background: var(--bg);
  }

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

  body.conversation-open .conversation-pane {
    display: none;
  }

  body.conversation-open .chat-pane {
    position: static;
    transform: none;
  }
}
