/* ============================================================
   Life OS — iMessage-styled chat
   ============================================================ */

* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --blue: #0a84ff;
  --grey-bubble: #e9e9eb;
  --grey-text: #8e8e93;
  --grey-line: #d1d1d6;
  --bg: #ffffff;
  --header-bg: rgba(247, 247, 247, 0.85);
  --ink: #000000;

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --grey-bubble: #2c2c2e;
    --grey-text: #98989d;
    --grey-line: #38383a;
    --bg: #000000;
    --header-bg: rgba(28, 28, 30, 0.85);
    --ink: #ffffff;
  }
}

html, body {
  height: 100%;
  width: 100%;
  background: var(--bg);
  color: var(--ink);
  font: 17px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

body {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100dvh;
}

/* ---------------- Header ---------------- */

.topbar {
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(8px + var(--safe-top)) 16px 8px;
  background: var(--header-bg);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 0.5px solid var(--grey-line);
  z-index: 10;
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--blue);
  color: white;
  display: grid;
  place-items: center;
  font-weight: 600;
  font-size: 15px;
}

.title { line-height: 1.1; }
.title .name { font-weight: 600; font-size: 15px; }
.title .status { font-size: 12px; color: var(--grey-text); margin-top: 2px; }

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

.messages {
  overflow-y: auto;
  padding: 12px 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  scroll-behavior: smooth;
}

.bubble {
  max-width: 75%;
  padding: 8px 12px;
  border-radius: 18px;
  font-size: 17px;
  line-height: 1.3;
  word-wrap: break-word;
  white-space: pre-wrap;
  position: relative;
}

.bubble.user {
  align-self: flex-end;
  background: var(--blue);
  color: white;
  border-bottom-right-radius: 4px;
}

.bubble.assistant {
  align-self: flex-start;
  background: var(--grey-bubble);
  color: var(--ink);
  border-bottom-left-radius: 4px;
}

/* tighten consecutive bubbles from same side */
.bubble + .bubble.user { margin-top: 2px; }
.bubble + .bubble.assistant { margin-top: 2px; }
.bubble.user + .bubble.assistant,
.bubble.assistant + .bubble.user { margin-top: 8px; }

/* timestamp dividers */
.timestamp {
  align-self: center;
  font-size: 11px;
  color: var(--grey-text);
  margin: 14px 0 6px;
  font-weight: 500;
}

/* typing indicator */
.bubble.typing {
  display: inline-flex;
  gap: 4px;
  padding: 12px 14px;
}
.bubble.typing span {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--grey-text);
  opacity: 0.4;
  animation: blink 1.2s infinite ease-in-out;
}
.bubble.typing span:nth-child(2) { animation-delay: 0.2s; }
.bubble.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink {
  0%, 80%, 100% { opacity: 0.3; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-2px); }
}

/* error bubble */
.bubble.error {
  background: #ffe5e5;
  color: #c00;
  border: 0.5px solid #ffaaaa;
}
@media (prefers-color-scheme: dark) {
  .bubble.error { background: #3a1414; color: #ff8a8a; border-color: #5a1f1f; }
}

/* ---------------- Composer ---------------- */

.composer {
  padding: 6px 8px 8px;
  background: var(--bg);
  border-top: 0.5px solid var(--grey-line);
  /* leave room for the tab bar below */
  margin-bottom: calc(60px + var(--safe-bottom));
}

#form {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

#input {
  flex: 1;
  resize: none;
  border: 0.5px solid var(--grey-line);
  border-radius: 18px;
  padding: 8px 14px;
  font: inherit;
  background: var(--bg);
  color: var(--ink);
  outline: none;
  max-height: 140px;
  line-height: 1.3;
}

#input::placeholder { color: var(--grey-text); }
#input:focus { border-color: var(--grey-text); }

#send {
  flex: 0 0 auto;
  width: 32px; height: 32px;
  border-radius: 50%;
  border: 0;
  background: var(--blue);
  color: white;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: opacity .15s, transform .1s;
  padding: 0;
}
#send:disabled { opacity: 0.35; cursor: not-allowed; }
#send:active:not(:disabled) { transform: scale(0.92); }
