:root {
  --bg: #f7f7f8;
  --bg-panel: #ffffff;
  --bg-soft: #f1f2f4;
  --bg-hover: #ebecef;
  --bg-active: #e4e6ea;
  --border: #e3e5e8;
  --border-strong: #d2d5da;
  --text: #1c1d1f;
  --text-muted: #6b7280;
  --text-faint: #9ca3af;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-soft: #e8efff;
  --danger: #dc2626;
  --radius: 8px;
  --radius-sm: 6px;
}

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

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI',
    sans-serif;
  font-size: 14px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
}

#app {
  width: 100vw;
  height: 100vh;
  height: 100dvh;
}

button {
  font: inherit;
}

/* ---------- Buttons ---------- */

.btn {
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 8px 14px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: var(--bg-soft);
  color: var(--text);
  transition: background 0.12s ease, border-color 0.12s ease, color 0.12s ease;
}

.btn:hover:not(:disabled) {
  background: var(--bg-hover);
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn--sm {
  padding: 6px 10px;
  font-size: 13px;
}

.btn--primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.btn--primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

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

.btn--ghost:hover:not(:disabled) {
  background: var(--bg-hover);
  color: var(--text);
}

/* ---------- Inputs ---------- */

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.field-label {
  font-size: 12px;
  color: var(--text-muted);
}

.field-input {
  width: 100%;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  background: #fff;
  color: var(--text);
  font: inherit;
  outline: none;
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}

.field-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.field-input::placeholder {
  color: var(--text-faint);
}

.field-input--sm {
  padding: 6px 9px;
  font-size: 13px;
}

/* ---------- Auth ---------- */

.auth-root {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.auth-card {
  width: min(380px, 100%);
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.auth-title {
  font-size: 18px;
  font-weight: 600;
}

.auth-subtitle {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: -8px;
}

.auth-tabs {
  display: flex;
  gap: 4px;
  padding: 3px;
  background: var(--bg-soft);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

.auth-tab {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 7px 10px;
  border-radius: 5px;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
}

.auth-tab--active {
  background: var(--bg-panel);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.auth-error {
  font-size: 13px;
  color: var(--danger);
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: var(--radius-sm);
  padding: 8px 10px;
}

.auth-submit {
  margin-top: 4px;
}

/* ---------- Shell ---------- */

.messenger-shell {
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  background: var(--bg);
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  overflow: hidden;
}

.messenger-shell--details-open {
  grid-template-columns: 280px minmax(0, 1fr) 280px;
}

.column {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg-panel);
}

.column--sidebar {
  border-right: 1px solid var(--border);
}

.column--chat {
  background: var(--bg);
}

.column--details {
  border-left: 1px solid var(--border);
}

/* ---------- Sidebar ---------- */

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.sidebar-user {
  flex: 1;
  min-width: 0;
}

.sidebar-user-name {
  font-weight: 600;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-user-sub {
  font-size: 12px;
  color: var(--text-muted);
}

.sidebar-toolbar {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

.sidebar-toolbar .field-input {
  flex: 1;
}

.sidebar-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px;
}

.sidebar-empty {
  padding: 20px 16px;
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
}

.chat-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.1s ease;
}

.chat-item:hover {
  background: var(--bg-hover);
}

.chat-item--active,
.chat-item--active:hover {
  background: var(--accent-soft);
}

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

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

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

.chat-item-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
}

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

.badge {
  min-width: 18px;
  height: 18px;
  padding: 0 6px;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* ---------- Chat header ---------- */

.chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
}

.chat-header-main {
  flex: 1;
  min-width: 0;
  cursor: pointer;
}

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

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

/* ---------- Messages ---------- */

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

.messages-empty {
  margin: auto;
  color: var(--text-muted);
  font-size: 13px;
  text-align: center;
}

.day-divider {
  text-align: center;
  margin: 10px 0 6px;
}

.day-divider span {
  display: inline-block;
  padding: 2px 10px;
  font-size: 11px;
  color: var(--text-muted);
  background: var(--bg-soft);
  border-radius: 999px;
}

.message-row {
  display: flex;
  margin-bottom: 4px;
}

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

.message-bubble {
  max-width: 70%;
  padding: 8px 12px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.message-row--me .message-bubble {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.message-author {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 2px;
}

.message-text {
  white-space: pre-wrap;
  word-wrap: break-word;
}

.message-edit-input {
  margin-top: 4px;
  resize: vertical;
  min-height: 40px;
  color: var(--text);
}

.message-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: flex-end;
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

.message-row--me .message-meta {
  color: rgba(255, 255, 255, 0.85);
}

.message-meta-edited {
  font-style: italic;
}

.message-edit-btn {
  border: none;
  background: transparent;
  cursor: pointer;
  color: inherit;
  opacity: 0.7;
  font-size: 12px;
  padding: 0 2px;
}

.message-edit-btn:hover {
  opacity: 1;
}

/* ---------- Input bar ---------- */

.message-input-bar {
  padding: 10px 16px 14px;
  background: var(--bg-panel);
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.edit-hint {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: var(--text-muted);
}

.message-input-inner {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.message-input-textarea {
  flex: 1;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 9px 11px;
  background: #fff;
  color: var(--text);
  font: inherit;
  outline: none;
  resize: none;
  max-height: 140px;
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}

.message-input-textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.message-input-textarea::placeholder {
  color: var(--text-faint);
}

/* ---------- Empty state ---------- */

.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
  gap: 10px;
}

.empty-state-title {
  font-size: 16px;
  font-weight: 600;
}

.empty-state-text {
  font-size: 13px;
  color: var(--text-muted);
  max-width: 320px;
}

/* ---------- Details panel ---------- */

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

.details-header-title {
  font-weight: 600;
  font-size: 14px;
}

.details-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.details-section-title {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.details-card {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}

.details-card--stack {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.details-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  padding: 4px 0;
}

.details-row-label {
  color: var(--text-muted);
}

.details-row-value {
  color: var(--text);
  font-weight: 500;
}

.details-empty {
  font-size: 12px;
  color: var(--text-muted);
  padding: 4px 0;
}

.member-row,
.user-select-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: var(--radius-sm);
}

.member-row + .member-row,
.user-select-row + .user-select-row {
  margin-top: 2px;
}

.user-select-row {
  cursor: pointer;
  transition: background 0.1s ease;
}

.user-select-row:hover {
  background: var(--bg-hover);
}

.member-main {
  flex: 1;
  min-width: 0;
}

.member-name {
  font-size: 13px;
}

.member-role {
  font-size: 12px;
  color: var(--text-muted);
}

.member-remove-btn {
  border: none;
  background: transparent;
  color: var(--danger);
  cursor: pointer;
  font-size: 12px;
  padding: 2px 4px;
}

.member-remove-btn:hover {
  text-decoration: underline;
}

.user-select-add {
  font-size: 12px;
  color: var(--accent);
}

/* ---------- Modal ---------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 15, 15, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 40;
}

.modal-card {
  width: min(400px, 100%);
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-title {
  font-weight: 600;
  font-size: 15px;
}

.modal-body {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* ---------- Notice ---------- */

.notice {
  position: fixed;
  right: 16px;
  bottom: 16px;
  width: min(320px, calc(100vw - 32px));
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.notice-title {
  font-weight: 600;
  font-size: 14px;
}

.notice-text {
  font-size: 13px;
  color: var(--text-muted);
}

.notice-footer {
  display: flex;
  justify-content: flex-end;
}

/* ---------- Mobile back button (hidden on desktop) ---------- */

.chat-header-back {
  display: none;
  padding: 4px 10px;
  font-size: 18px;
  line-height: 1;
}

/* ---------- Responsive ---------- */

/* On medium screens, details column becomes an overlay panel
   so the chat itself keeps its space. */
@media (max-width: 1024px) {
  .messenger-shell,
  .messenger-shell--details-open {
    grid-template-columns: 260px minmax(0, 1fr);
  }
  .messenger-shell--details-open .column--details {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(320px, 90vw);
    z-index: 30;
    box-shadow: -8px 0 24px rgba(0, 0, 0, 0.08);
  }
  .messenger-shell:not(.messenger-shell--details-open) .column--details {
    display: none;
  }
}

/* Phones: single-column with sidebar/chat swapped by selection.
   Details panel becomes full-screen overlay. */
@media (max-width: 720px) {
  .messenger-shell,
  .messenger-shell--details-open {
    grid-template-columns: minmax(0, 1fr);
  }

  .column--sidebar {
    border-right: none;
  }

  .messenger-shell--has-chat .column--sidebar {
    display: none;
  }

  .messenger-shell:not(.messenger-shell--has-chat) .column--chat {
    display: none;
  }

  .chat-header-back {
    display: inline-flex;
  }

  .messenger-shell--details-open .column--details {
    position: fixed;
    inset: 0;
    width: 100vw;
    z-index: 30;
    box-shadow: none;
    border-left: none;
  }

  .message-bubble {
    max-width: 85%;
  }

  .modal-card,
  .auth-card {
    width: 100%;
  }

  .notice {
    left: 16px;
    right: 16px;
    width: auto;
  }

  .messages-scroll {
    padding: 12px;
  }

  .message-input-bar {
    padding: 8px 12px 10px;
  }
}

/* Avoid iOS auto-zoom on focus by keeping inputs ≥ 16px on small screens */
@media (max-width: 720px) {
  .field-input,
  .field-input--sm,
  .message-input-textarea {
    font-size: 16px;
  }
}
