:root {
  --bg: #050814;
  --panel: #0e1320;
  --panel-soft: #12172a;
  --line: #1e2635;
  --muted: #9aa6b2;
  --text: #e7edf3;
  --primary: #2ea8ff;
  --ok: #35d399;
  --danger: #ff6b81;
  --bubble: #151b2e;
  --bubble-me: #0b5dd5;
  --bubble-me-2: #0846a7;
  --shadow-lg: 0 18px 45px rgba(0, 0, 0, 0.6);
}

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

html,
body,
#app {
  height: 100%;
}

body {
  margin: 0;
  background: radial-gradient(circle at top, #10172a 0, #050814 45%, #020312);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, BlinkMacSystemFont, Segoe UI,
    Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* Layout */

#app {
  display: flex;
  height: 100vh;
  max-height: 100vh;
  overflow: hidden;
}

/* Sidebar */

.sidebar {
  width: 320px;
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  background: linear-gradient(180deg, #0e1320, #050814);
}

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

.brand-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
  gap: 8px;
}

.brand {
  font-weight: 800;
  letter-spacing: 0.2px;
  font-size: 1.05rem;
}

.me-row {
  display: flex;
  gap: 10px;
  align-items: center;
  margin: 10px 0;
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: radial-gradient(circle at 30% 20%, #334155, #020617);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 12px;
  color: #e5e7eb;
  flex-shrink: 0;
  overflow: hidden;
}

.chat-avatar {
  width: 40px;
  height: 40px;
}

.me-name {
  font-weight: 700;
}

.tiny {
  color: var(--muted);
  font-size: 12px;
}

.sidebar-header input {
  width: 100%;
  padding: 8px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #050814;
  color: var(--text);
  font-size: 12px;
}

/* Chat list */

.chat-list {
  flex: 1;
  overflow: auto;
  padding: 8px;
}

.chat-item {
  display: grid;
  grid-template-columns: 44px 1fr auto;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 12px;
  cursor: pointer;
  align-items: center;
  transition: background 0.15s ease, transform 0.07s ease;
}

.chat-item:hover {
  background: #101727;
  transform: translateY(-1px);
}

.chat-item.active {
  background: #152132;
  box-shadow: 0 0 0 1px rgba(46, 168, 255, 0.25);
}

.chat-item-main {
  min-width: 0;
}

.chat-item-title {
  font-weight: 700;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.chat-item-preview {
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12px;
  margin-top: 2px;
}

.chat-item-time {
  font-size: 11px;
  color: var(--muted);
}

.badge {
  display: inline-block;
  padding: 0 6px;
  border-radius: 999px;
  background: #b91c1c;
  color: #fff;
  font-size: 11px;
  margin-left: 4px;
}

/* Sidebar footer */

.sidebar-footer {
  padding: 10px;
  border-top: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.row {
  display: flex;
  gap: 6px;
}

/* Buttons & controls */

.btn {
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: #0b1120;
  color: var(--text);
  cursor: pointer;
  font-size: 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  transition: background 0.12s ease, border-color 0.12s ease, transform 0.05s;
}

.btn.primary {
  background: radial-gradient(circle at top, #38bdf8, #0f172a);
  border-color: #0ea5e9;
}

.btn.ok {
  background: radial-gradient(circle at top, #34d399, #064e3b);
  border-color: #34d399;
}

.btn.danger {
  background: radial-gradient(circle at top, #f97373, #450a0a);
  border-color: #f97373;
  color: #fee2e2;
}

.btn.ghost {
  background: transparent;
  border-color: transparent;
  color: var(--muted);
}

.btn.ghost:hover {
  background: rgba(148, 163, 184, 0.08);
  border-color: rgba(148, 163, 184, 0.1);
}

.btn.small {
  padding: 5px 10px;
  font-size: 11px;
}

.btn.full {
  width: 100%;
}

.btn:disabled {
  opacity: 0.6;
  cursor: default;
}

.btn:hover:not(:disabled) {
  transform: translateY(-0.5px);
}

input,
select,
textarea {
  background: #050814;
  border-radius: 10px;
  border: 1px solid var(--line);
  padding: 8px 10px;
  color: var(--text);
  font-size: 12px;
  outline: none;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 1px rgba(46, 168, 255, 0.3);
}

input::placeholder,
textarea::placeholder {
  color: #6b7280;
}

/* Main chat */

.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: radial-gradient(circle at top, #111827, #020617);
}

/* Chat header */

.chat-header {
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--panel);
  gap: 12px;
  flex-wrap: wrap;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.4);
  z-index: 1;
}

.chat-header-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.chat-header-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.chat-header-title-row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.chat-title {
  font-weight: 600;
  font-size: 14px;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}

.chat-header-meta {
  font-size: 12px;
  color: var(--muted);
}

.chat-header-right {
  display: flex;
  flex-direction: column;
  gap: 2px;
  align-items: flex-end;
  min-width: max-content;
}

.chat-header-right .tiny span {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
    "Liberation Mono", "Courier New", monospace;
}

/* Messages */

.messages {
  flex: 1;
  overflow: auto;
  padding: 16px 18px 12px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* Custom scrollbar */

.messages::-webkit-scrollbar,
.chat-list::-webkit-scrollbar {
  width: 6px;
}

.messages::-webkit-scrollbar-track,
.chat-list::-webkit-scrollbar-track {
  background: transparent;
}

.messages::-webkit-scrollbar-thumb,
.chat-list::-webkit-scrollbar-thumb {
  background: #1f2937;
  border-radius: 999px;
}

/* Message row: right for me, left for others */

.msg-row {
  display: flex;
  gap: 6px;
}

.msg-row.me {
  justify-content: flex-end;
}

.msg-bubble {
  max-width: 72%;
  background: var(--bubble);
  border-radius: 14px;
  padding: 8px 10px;
  font-size: 13px;
  line-height: 1.35;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.45);
  position: relative;
}

.msg-bubble.me {
  background: linear-gradient(180deg, var(--bubble-me), var(--bubble-me-2));
}

.msg-sender {
  font-size: 11px;
  color: #cfe0ff;
  margin-bottom: 4px;
  font-weight: 700;
}

.msg-text {
  white-space: pre-wrap;
  word-wrap: break-word;
  color: #eaf2ff;
}

.msg-meta {
  margin-top: 4px;
  font-size: 11px;
  color: var(--muted);
  text-align: right;
}

/* Attachments */

.msg-attachments {
  margin-top: 4px;
}

.msg-image {
  border-radius: 10px;
  border: 1px solid rgba(15, 23, 42, 0.8);
  object-fit: cover;
}

/* Message actions */

.msg-bubble .btn.small {
  border-radius: 999px;
}

.msg-bubble .tiny {
  margin-top: 2px;
}

/* Composer */

.composer {
  padding: 10px 12px 12px;
  border-top: 1px solid var(--line);
  background: #050814;
  display: flex;
  flex-direction: column;
  gap: 6px;
  box-shadow: 0 -12px 30px rgba(0, 0, 0, 0.5);
}

.composer-status {
  color: var(--muted);
}

.composer-row {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.composer-row textarea {
  resize: none;
  min-height: 64px;
  flex: 1 1 100%;
}

.composer-row input {
  flex: 1 1 0;
  min-width: 0;
}

.composer-row-actions {
  justify-content: space-between;
}

.composer-row-actions input {
  max-width: 220px;
}

.composer-actions-buttons {
  display: flex;
  gap: 8px;
}

/* Login overlay */

.login-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at top, rgba(15, 23, 42, 0.98), #020617);
  backdrop-filter: blur(8px);
  z-index: 999;
}

.login-overlay.hidden {
  display: none;
}

.login-card {
  width: min(420px, 92vw);
  display: grid;
  gap: 14px;
  padding: 24px 22px 20px;
  border-radius: 16px;
  border: 1px solid var(--line);
  background: radial-gradient(circle at top left, #111827, #020617);
  box-shadow: var(--shadow-lg);
}

.login-title {
  font-size: 20px;
  font-weight: 700;
}

.login-description {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.login-field {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
}

.login-field input {
  width: 100%;
}

.login-status {
  min-height: 14px;
  color: var(--muted);
  font-size: 12px;
}

.login-error {
  min-height: 14px;
  color: var(--danger);
  font-size: 12px;
}

.login-actions {
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

/* Modal (Add chat / Edit conversation) */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(5, 8, 15, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}

.modal-overlay.hidden {
  display: none;
}

.modal {
  width: min(360px, 90vw);
  background: #0b1120;
  border-radius: 16px;
  padding: 16px 16px 14px;
  border: 1px solid var(--line);
  box-shadow: var(--shadow-lg);
}

.modal-title {
  font-weight: 700;
  margin-bottom: 10px;
}

.modal-section {
  margin-bottom: 8px;
}

.modal-section label {
  font-size: 12px;
  margin-bottom: 3px;
  display: block;
  color: var(--muted);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 8px;
}

.modal-error {
  font-size: 12px;
  color: var(--danger);
}

/* Utility */

.hidden {
  display: none !important;
}

/* Responsive */

@media (max-width: 900px) {
  #app {
    flex-direction: column;
  }

  .sidebar {
    width: 100%;
    max-height: 38vh;
  }

  .chat-main {
    flex: 1;
  }

  .chat-header {
    padding-inline: 12px;
  }
}

@media (max-width: 720px) {
  .chat-item {
    grid-template-columns: 40px 1fr;
    grid-template-rows: auto auto;
  }

  .chat-item-time {
    grid-column: 2 / 3;
    justify-self: flex-end;
  }

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

  .composer-row-actions {
    flex-direction: column;
    align-items: stretch;
  }

  .composer-row-actions input {
    max-width: 100%;
  }

  .composer-actions-buttons {
    justify-content: flex-end;
  }

  .chat-header-right {
    flex-direction: row;
    align-items: center;
    gap: 10px;
  }
}

@media (max-width: 520px) {
  .sidebar-header {
    padding-inline: 10px;
  }

  .chat-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .chat-header-right {
    align-items: flex-start;
  }

  .messages {
    padding-inline: 10px;
  }

  .composer {
    padding-inline: 10px;
  }

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