:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --border: #dfe3e8;
  --text: #1a1d21;
  --muted: #6b7280;
  --accent: #2f3a45;
  --accent-soft: #e8ebef;
  --danger: #b42318;
  --ok: #067647;
  --mine: #e7edf3;
  --theirs: #ffffff;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
  --font: "IBM Plex Sans", "Segoe UI", sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  color: var(--text);
  background:
    radial-gradient(1200px 600px at 10% -10%, #e9eef4 0%, transparent 55%),
    radial-gradient(900px 500px at 100% 0%, #edf1f5 0%, transparent 50%),
    var(--bg);
  line-height: 1.45;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.auth-wrap {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 2rem 1rem;
}
.auth-card {
  width: min(420px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1.75rem;
  box-shadow: var(--shadow);
}
.brand {
  font-weight: 600;
  letter-spacing: 0.02em;
  font-size: 1.35rem;
  margin: 0 0 0.25rem;
}
.brand span { color: var(--muted); font-weight: 500; }
.lead { color: var(--muted); margin: 0 0 1.25rem; font-size: 0.95rem; }

label {
  display: block;
  font-size: 0.85rem;
  color: var(--muted);
  margin: 0.85rem 0 0.35rem;
}
input[type="text"],
input[type="password"],
input[type="email"],
input[type="tel"],
textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.65rem 0.75rem;
  font: inherit;
  background: #fff;
  color: var(--text);
}
input:focus, textarea:focus {
  outline: 2px solid #c5ced8;
  border-color: #9aa7b5;
}
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  border-radius: 8px;
  padding: 0.6rem 1rem;
  font: inherit;
  font-size: 0.95rem;
  cursor: pointer;
}
.btn:hover { filter: brightness(1.08); }
.btn-secondary {
  background: #fff;
  color: var(--accent);
}
.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--muted);
}
.btn-danger {
  background: #fff;
  color: var(--danger);
  border-color: #f0b4ae;
}
.btn-block { width: 100%; margin-top: 1rem; }
.muted { color: var(--muted); font-size: 0.9rem; }
.error {
  background: #fef3f2;
  color: var(--danger);
  border: 1px solid #fecdca;
  border-radius: 8px;
  padding: 0.6rem 0.75rem;
  margin: 0 0 0.75rem;
  font-size: 0.9rem;
}
.flash-ok {
  background: #ecfdf3;
  color: var(--ok);
  border: 1px solid #abefc6;
  border-radius: 8px;
  padding: 0.55rem 0.75rem;
  margin-bottom: 0.75rem;
  font-size: 0.9rem;
}
.flash-error {
  background: #fef3f2;
  color: var(--danger);
  border: 1px solid #fecdca;
  border-radius: 8px;
  padding: 0.55rem 0.75rem;
  margin-bottom: 0.75rem;
  font-size: 0.9rem;
}
.flash-info {
  background: var(--accent-soft);
  color: var(--accent);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.55rem 0.75rem;
  margin-bottom: 0.75rem;
  font-size: 0.9rem;
}

.app-shell {
  height: 100dvh;
  display: grid;
  grid-template-columns: 300px 1fr;
  max-width: 1200px;
  margin: 0 auto;
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  background: var(--surface);
}
.sidebar {
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: #fafbfc;
}
.sidebar-head {
  padding: 1rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}
.sidebar-head .who {
  font-size: 0.85rem;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sidebar-body {
  padding: 0.75rem;
  overflow: auto;
  flex: 1;
  min-height: 0;
}
.section-title {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin: 0.75rem 0 0.4rem;
}
.friend-list { list-style: none; margin: 0; padding: 0; }
.friend-list li a,
.friend-item {
  display: block;
  padding: 0.65rem 0.7rem;
  border-radius: 8px;
  color: inherit;
  text-decoration: none;
  border: 1px solid transparent;
}
.friend-list li a:hover { background: var(--accent-soft); }
.friend-list li a.active {
  background: var(--accent-soft);
  border-color: var(--border);
  font-weight: 560;
}
.pending-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.65rem;
  margin-bottom: 0.5rem;
  background: #fff;
}
.pending-card .actions {
  display: flex;
  gap: 0.4rem;
  margin-top: 0.5rem;
}
.pending-card .actions .btn { padding: 0.35rem 0.6rem; font-size: 0.85rem; }

.main {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}
.chat-head {
  padding: 0.9rem 1.1rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}
.chat-head h1 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
}
.chat-head .delete-chat-form .btn {
  padding: 0.4rem 0.7rem;
  font-size: 0.85rem;
  white-space: nowrap;
}
.messages {
  flex: 1;
  overflow: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  background:
    linear-gradient(180deg, #f7f8fa 0%, #f3f5f7 100%);
}
.msg {
  max-width: min(72%, 520px);
  padding: 0.65rem 0.8rem;
  border-radius: 12px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  background: var(--theirs);
}
.msg.mine {
  align-self: flex-end;
  background: var(--mine);
}
.msg.theirs { align-self: flex-start; }
.msg .meta {
  font-size: 0.72rem;
  color: var(--muted);
  margin-bottom: 0.25rem;
  font-family: var(--mono);
}
.msg .body { white-space: pre-wrap; word-break: break-word; }
.msg img.preview {
  display: block;
  max-width: 100%;
  max-height: 280px;
  border-radius: 8px;
  margin-top: 0.35rem;
}
.msg audio { width: 100%; margin-top: 0.35rem; }
.msg .file-link {
  display: inline-flex;
  margin-top: 0.35rem;
  padding: 0.4rem 0.55rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  font-size: 0.9rem;
}
.load-more {
  align-self: center;
  margin-bottom: 0.5rem;
}
.composer {
  border-top: 1px solid var(--border);
  padding: 0.75rem 1rem 1rem;
  background: #fff;
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 0.5rem;
  align-items: end;
}
.composer textarea {
  min-height: 44px;
  max-height: 140px;
  resize: vertical;
}
.composer .file-btn {
  position: relative;
  overflow: hidden;
}
.composer .file-btn input[type="file"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}
.file-name {
  grid-column: 1 / -1;
  font-size: 0.8rem;
  color: var(--muted);
  min-height: 1.1em;
}
.empty-state {
  margin: auto;
  text-align: center;
  color: var(--muted);
  padding: 2rem;
}
.add-form {
  display: grid;
  gap: 0.4rem;
  margin-bottom: 0.75rem;
}
.add-form .row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.4rem;
}

@media (max-width: 800px) {
  .app-shell {
    grid-template-columns: 1fr;
    border: none;
    max-width: none;
  }
  .sidebar { max-height: 38dvh; border-right: none; border-bottom: 1px solid var(--border); }
  .msg { max-width: 88%; }
}
