/* Prevent hidden attribute from being overridden by display:flex/grid */
[hidden] { display: none !important; }

/* Ensure all elements use border-box sizing on this page */
.chat-shell *,
.chat-shell *::before,
.chat-shell *::after {
  box-sizing: border-box;
}

/* ── Page shell ─────────────────────────────────────────────── */

body.messages-page {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

body.messages-page .site-header {
  flex-shrink: 0;
}

/* Active nav link */
.main-nav a.nav-active {
  color: var(--text, #111827);
  background: rgba(74, 143, 232, 0.12);
}

/* ── Chat shell (sidebar + main) ─────────────────────────────── */

.chat-shell {
  flex: 1;
  display: grid;
  grid-template-columns: 440px 1fr;
  overflow: hidden;
  background: #f4f8ff;
  box-sizing: border-box;
}

/* ── Left sidebar ────────────────────────────────────────────── */

.chat-sidebar {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: #ffffff;
  border-right: 1px solid rgba(47, 114, 201, 0.14);
  min-width: 0;
  box-sizing: border-box;
  width: 440px;
  flex-shrink: 0;
}

.sidebar-header {
  flex-shrink: 0;
  padding: 1rem 0.85rem 0.75rem;
  border-bottom: 1px solid rgba(47, 114, 201, 0.1);
}

.sidebar-title {
  font-family: 'Space Grotesk', 'Segoe UI', sans-serif;
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin: 0;
  color: #0e1e35;
}

/* ── Thread list ─────────────────────────────────────────────── */

.thread-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.6rem;
  display: grid;
  align-content: start;
  gap: 0.3rem;
}

.thread-empty-msg {
  color: #7a96b4;
  font-size: 0.85rem;
  padding: 0.5rem 0.25rem;
  margin: 0;
}

.thread-list-item {
  position: relative;
  width: 100%;
  overflow: hidden;
  border: 1px solid #f5dfc0;
  border-radius: 10px;
  background: #fdf6ee;
  padding: 0.6rem 0.7rem;
  text-align: left;
  color: #0e1e35;
  cursor: pointer;
  transition: border-color 140ms ease, background 140ms ease, box-shadow 150ms ease;
}

.thread-list-item:hover {
  border-color: #e8b87a;
  background: #fceedd;
  box-shadow: 0 4px 12px rgba(180, 100, 20, 0.1);
}

.thread-list-item.is-active {
  border-color: #d4843a;
  background: #fae4c8;
  box-shadow: 0 2px 8px rgba(180, 100, 20, 0.15);
}

.thread-list-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem;
}

.thread-title {
  font-weight: 700;
  font-size: 0.9rem;
  color: #0e1e35;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.thread-time {
  flex-shrink: 0;
  color: #7a96b4;
  font-size: 0.75rem;
}

.thread-racket {
  margin: 0.2rem 0 0;
  color: #4a6a8a;
  font-size: 0.78rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.thread-preview {
  margin: 0.2rem 0 0;
  color: #5a7a9a;
  font-size: 0.8rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.thread-unread {
  position: absolute;
  top: 0.55rem;
  right: 0.55rem;
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: #2f72c9;
}

.thread-list-item.is-completed {
  background: #f0faf4;
  border-color: #c3e6d0;
}

.thread-list-item.is-completed:hover {
  background: #e4f7ec;
  border-color: #8fd4ab;
  box-shadow: 0 4px 12px rgba(31, 111, 88, 0.08);
}

.thread-list-item.is-completed.is-active {
  background: #d6f0e2;
  border-color: #4caf82;
}

.thread-done {
  color: #1a7f4b;
  font-size: 0.85rem;
  font-weight: 700;
}

/* ── Right chat view ─────────────────────────────────────────── */

.chat-view {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
  box-sizing: border-box;
}

/* ── Active thread panel ─────────────────────────────────────── */

.thread-panel {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
  min-width: 0;
  box-sizing: border-box;
  width: 100%;
}

.chat-view-header {
  flex-shrink: 0;
  padding: 0.6rem 1rem;
  background: #ffffff;
  border-bottom: 1px solid rgba(47, 114, 201, 0.12);
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-height: 52px;
}

.chat-header-info {
  font-size: 1rem;
  font-weight: 700;
  color: #0e1e35;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0; /* allow shrinking */
  overflow: hidden;
}

.chat-header-info strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.header-racket {
  font-size: 0.82rem;
  font-weight: 500;
  color: #4a6a8a;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex-shrink: 1;
}

/* ── Messages scroll area ────────────────────────────────────── */

.messages-list {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

.message-item {
  border: 1px solid rgba(47, 114, 201, 0.12);
  border-radius: 14px;
  background: #ffffff;
  padding: 0.6rem 0.8rem;
  max-width: 68%;
  align-self: flex-start;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.04);
}

.message-item.is-own {
  margin-left: auto;
  align-self: flex-end;
  background: #e6f0fd;
  border-color: #b8d4f8;
}

.message-head {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  color: #7a96b4;
  font-size: 0.78rem;
  margin-bottom: 0.3rem;
}

.message-body {
  margin: 0;
  color: #0e1e35;
  white-space: pre-wrap;
  font-size: 0.9rem;
  line-height: 1.55;
}

.message-system {
  align-self: center;
  text-align: center;
  font-size: 0.82rem;
  font-style: italic;
  color: #1f6f58;
  background: #ecfbf5;
  border: 1px solid #bde7d9;
  border-radius: 999px;
  padding: 0.25rem 0.8rem;
  max-width: 80%;
}

/* ── Compose area ────────────────────────────────────────────── */

.compose-area {
  flex-shrink: 0;
  background: #ffffff;
  border-top: 1px solid rgba(47, 114, 201, 0.12);
  padding: 0.75rem 1rem;
  box-sizing: border-box;
  width: 100%;
  min-width: 0;
}

.compose-area form {
  display: grid;
  gap: 0.4rem;
}

.compose-input-row {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
}

.compose-input-row textarea {
  flex: 1;
  resize: none;
  border-radius: 10px;
  font-size: 0.9rem;
  padding: 0.5rem 0.7rem;
  line-height: 1.5;
  border: 1px solid rgba(47, 114, 201, 0.25);
  outline: none;
  transition: border-color 140ms ease;
}

.compose-input-row textarea:focus {
  border-color: #2f72c9;
}

.btn-send {
  flex-shrink: 0;
  align-self: flex-end;
  background: linear-gradient(180deg, #5c9df0, #357ad4);
  color: #fff;
  font-family: 'Space Grotesk', 'Segoe UI', sans-serif;
  font-weight: 700;
  font-size: 0.9rem;
  padding: 0.5rem 1.3rem;
  border-radius: 10px;
  border: none;
  cursor: pointer;
  transition: opacity 150ms ease;
}

.btn-send:hover {
  opacity: 0.9;
}

.btn-send:disabled {
  opacity: 0.55;
  cursor: default;
}

/* ── Placeholder / auth gate ─────────────────────────────────── */

.thread-placeholder {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f4f8ff;
}

.placeholder-inner {
  text-align: center;
  color: #7a96b4;
  padding: 2rem;
}

.placeholder-icon {
  font-size: 2.8rem;
  margin-bottom: 0.75rem;
}

.placeholder-inner h2 {
  margin: 0 0 0.35rem;
  font-family: 'Space Grotesk', 'Segoe UI', sans-serif;
  font-size: 1.2rem;
  color: #304e72;
}

.placeholder-inner p {
  margin: 0;
  font-size: 0.9rem;
}

.placeholder-inner .gate-btn {
  display: inline-block;
  margin-top: 1.2rem;
  padding: 0.6rem 1.4rem;
  border-radius: 10px;
  background: linear-gradient(180deg, #5c9df0, #357ad4);
  color: #fff;
  font-family: 'Space Grotesk', 'Segoe UI', sans-serif;
  font-weight: 700;
  font-size: 0.95rem;
  text-decoration: none;
  transition: opacity 150ms ease;
}

.placeholder-inner .gate-btn:hover {
  opacity: 0.9;
}

/* ── Status message ──────────────────────────────────────────── */

.message {
  font-size: 0.82rem;
  margin: 0;
  min-height: 1.2em;
}

.message.error {
  color: #c0392b;
}

.message.success {
  color: #1f6f58;
}

/* ── Back button (only shown on mobile) ──────────────────────── */

.back-btn {
  display: none;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  border: none;
  background: none;
  cursor: pointer;
  color: #2f72c9;
  border-radius: 8px;
  transition: background 140ms ease;
}

.back-btn:hover {
  background: rgba(47, 114, 201, 0.1);
}

/* ── Mobile: master / detail ─────────────────────────────────── */

@media (max-width: 700px) {
  .chat-shell {
    display: block; /* stacked, full height */
  }

  /* Thread list fills the screen by default */
  .chat-sidebar {
    position: absolute;
    inset: 0;
    border-right: none;
    z-index: 1;
  }

  /* Chat view hidden by default on mobile */
  .chat-view {
    position: absolute;
    inset: 0;
    background: #f4f8ff;
    z-index: 2;
    display: none;
  }

  /* When a thread is open: show chat, hide sidebar */
  .thread-active .chat-view {
    display: flex;
    flex-direction: column;
  }

  .thread-active .chat-sidebar {
    display: none;
  }

  /* More breathing room around thread items on mobile */
  .thread-list {
    padding: 0.6rem 0.85rem;
  }

  /* Show back button on mobile */
  .back-btn {
    display: flex;
  }

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

  /* Textarea on mobile */
  .compose-input-row textarea {
    min-height: 52px;
    font-size: 1rem;
  }

  .compose-area {
    padding: 0.6rem 0.75rem;
  }

  /* Compact header on mobile */
  .chat-view-header {
    padding: 0.5rem 0.75rem;
    min-height: 48px;
  }

  .chat-header-info {
    font-size: 0.95rem;
  }

  .header-racket {
    display: none; /* hide racket model in header on mobile to save space */
  }
}

/* Make chat-shell a positioning context */
.chat-shell {
  position: relative;
}
