* { box-sizing: border-box; }
body { margin: 0; font-family: var(--sans); background: var(--cream); color: var(--text); line-height: 1.5; min-height: 100vh; }
a { color: inherit; }

.page {
  width: 100%; margin: 0 auto; min-height: 100vh;
  display: grid; grid-template-columns: 240px 1fr;
}

/* === Main === */
main.content { padding: 36px 48px 60px; display: flex; flex-direction: column; min-height: 100vh; }
.page-head { margin-bottom: 18px; }
.page-head h1 {
  font-family: var(--display); font-size: 32px; font-weight: 700;
  color: var(--ink); letter-spacing: -0.02em; margin: 0 0 6px;
}
.page-head .sub { color: var(--muted); font-size: 14px; }

/* Inbox 2-col */
.inbox-layout {
  display: grid; grid-template-columns: 320px 1fr; gap: 18px;
  flex: 1; min-height: 0;
}

/* Thread list */
.thread-list {
  background: var(--paper); border: 1px solid var(--rule);
  border-radius: var(--r-md); overflow: hidden;
  display: flex; flex-direction: column; min-height: 0;
}
.thread-list-head {
  padding: 14px 16px; border-bottom: 1px solid var(--rule);
  display: flex; justify-content: space-between; align-items: center;
}
.thread-list-head h3 {
  margin: 0; font-family: var(--display); font-size: var(--t-body);
  font-weight: 600; color: var(--ink);
}
.thread-list-head .count {
  font-family: var(--mono); font-size: var(--t-micro); color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.1em;
}
.thread-items { overflow-y: auto; flex: 1; }
.thread-row {
  display: flex; gap: 10px; padding: 14px 16px;
  border-bottom: 1px solid var(--rule); cursor: pointer;
  transition: background var(--t-fast);
}
.thread-row:hover { background: var(--cream-deep); }
.thread-row.active {
  background: var(--g10); border-inline-start: 3px solid var(--forest);
  padding-inline-start: 13px;
}
.thread-row .avatar {
  width: 38px; height: 38px; border-radius: 50%;
  background: var(--forest); color: var(--cream);
  font-size: var(--t-small); font-weight: 600;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  background-size: cover; background-position: center;
}
.thread-row .tr-main { flex: 1; min-width: 0; }
.thread-row .tr-head {
  display: flex; justify-content: space-between;
  align-items: baseline; gap: 6px;
}
.thread-row .tr-name {
  font-family: var(--display); font-weight: 600;
  font-size: 14px; color: var(--ink);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.thread-row .tr-time {
  font-family: var(--mono); font-size: var(--t-micro); color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.06em; white-space: nowrap;
}
.thread-row .tr-preview {
  font-size: var(--t-small); color: var(--muted); margin-top: 2px;
  display: -webkit-box; -webkit-line-clamp: 2;
  -webkit-box-orient: vertical; overflow: hidden;
}
.thread-row .tr-unread {
  width: 8px; height: 8px; background: var(--forest);
  border-radius: 50%; flex-shrink: 0; margin-top: 8px;
}
.thread-row.unread .tr-name { color: var(--forest-deep); }
.thread-row.unread .tr-preview { color: var(--text); font-weight: 500; }

/* Open thread (right side) */
.thread-view {
  background: var(--paper); border: 1px solid var(--rule);
  border-radius: var(--r-md); display: flex; flex-direction: column;
  min-height: 0; overflow: hidden;
}
.thread-head {
  padding: 14px 22px; border-bottom: 1px solid var(--rule);
  display: flex; align-items: center; gap: var(--s-3);
}
.thread-head .avatar-lg {
  width: 44px; height: 44px; border-radius: 50%;
  background: var(--forest); color: var(--cream);
  font-family: var(--display); font-weight: 600; font-size: 16px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  background-size: cover; background-position: center;
}
.thread-head .th-info { flex: 1; min-width: 0; }
.thread-head h2 {
  font-family: var(--display); font-size: var(--t-lead); font-weight: 600;
  color: var(--ink); margin: 0 0 2px; letter-spacing: -0.01em;
}
.thread-head .th-meta {
  font-family: var(--mono); font-size: var(--t-micro); color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.08em;
}
.thread-head .view-therapist {
  font-family: inherit; font-size: var(--t-small);
  padding: 7px 14px; border-radius: var(--r-pill);
  border: 1px solid var(--rule); background: none;
  color: var(--text); text-decoration: none; cursor: pointer;
  transition: 140ms;
  white-space: nowrap;
}
.thread-head .view-therapist:hover { border-color: var(--ink); }

.thread-messages {
  flex: 1; overflow-y: auto; padding: 18px 22px;
  display: flex; flex-direction: column; gap: var(--s-3);
}
.msg { display: flex; gap: 10px; max-width: 78%; }
/* On the patient page, the *patient* (the current user) is on the
   right; the therapist is on the left. Opposite of therapist-inbox. */
.msg.from-patient { align-self: flex-end; flex-direction: row-reverse; }
.msg.from-therapist { align-self: flex-start; }
.msg .avatar {
  width: 30px; height: 30px; border-radius: 50%;
  background: var(--forest); color: var(--cream);
  font-size: var(--t-micro); font-weight: 600;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  background-size: cover; background-position: center;
}
.msg.from-patient .avatar { background: var(--ink); }
.msg .bubble {
  background: var(--cream-deep); padding: 9px 14px;
  border-radius: 16px 16px 16px 4px;
  font-size: var(--t-small); line-height: 1.5; color: var(--text);
  word-wrap: break-word; overflow-wrap: anywhere;
}
.msg.from-patient .bubble {
  background: var(--ink); color: var(--cream);
  border-radius: 16px 16px 4px 16px;
}
.msg .ts {
  font-family: var(--mono); font-size: 10px; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.06em; margin-top: 4px;
}
.msg.edited .ts::after { content: " · edited"; }

/* Edit/delete on the patient's own bubbles (QA doc item 21). Revealed
   on hover/focus so the thread stays calm; logical margin for RTL. */
.msg .entry-actions {
  display: inline-flex; gap: 2px; margin-inline-start: 6px;
  opacity: 0; transition: opacity 120ms;
}
.msg:hover .entry-actions,
.msg:focus-within .entry-actions { opacity: 1; }
.msg-action {
  border: 0; background: none; color: var(--muted);
  cursor: pointer; font-size: 12px; line-height: 1;
  padding: 1px 4px; border-radius: 4px;
}
.msg-action:hover { color: var(--ink); background: var(--cream-deep); }

.day-divider {
  align-self: center; font-family: var(--mono); font-size: var(--t-micro);
  text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted);
  padding: var(--s-1) var(--s-3); background: var(--cream-deep); border-radius: var(--r-pill);
}

/* Composer */
.composer { border-top: 1px solid var(--rule); padding: 14px 18px; background: var(--cream-deep); }
.composer textarea {
  width: 100%; min-height: 56px; max-height: 200px;
  border: 1px solid var(--rule); border-radius: var(--r-md);
  padding: 10px 14px; font-family: inherit; font-size: var(--t-small);
  line-height: 1.5; background: var(--paper); color: var(--ink);
  resize: vertical; outline: none; transition: border-color var(--t-fast);
  box-sizing: border-box;
}
.composer textarea:focus { border-color: var(--forest); }
.composer-foot {
  display: flex; justify-content: space-between;
  align-items: center; margin-top: 8px;
}
.composer .char { font-family: var(--mono); font-size: var(--t-micro); color: var(--muted); }
.composer .char.over { color: var(--forest-deep); }
.composer .send {
  background: var(--forest); color: var(--cream); border: none;
  padding: 8px 22px; border-radius: var(--r-pill);
  font-family: inherit; font-size: var(--t-small); font-weight: 500;
  cursor: pointer; transition: background var(--t-fast);
}
.composer .send:hover { background: var(--forest-deep); }
.composer .send:disabled { opacity: 0.4; cursor: not-allowed; }


/* Responsive */
@media (max-width: 1200px) { main.content { padding: 28px 32px; } }
@media (max-width: 900px) {
  .page { grid-template-columns: 1fr; }
  main.content { padding: 22px 22px; }
  .inbox-layout { grid-template-columns: 1fr; gap: 14px; }
  .thread-list { max-height: 320px; }
}
@media (max-width: 640px) {
  main.content { padding: 18px 16px; }
  .page-head h1 { font-size: 26px; }
  .msg { max-width: 92%; }
}
