.chat {

  --chat-chrome: 132px;
  display: flex;
  flex-direction: column;
  min-height: 0;

  height: calc(100dvh - var(--chat-chrome));
}

@supports not (height: 100dvh) {
  .chat { height: calc(100vh - var(--chat-chrome)); }
}

@media (min-width: 781px) and (pointer: fine) {
  .chat { height: min(calc(100dvh - var(--chat-chrome)), 720px); }
}

/* ── feed ─────────────────────────────────────────────────────── */

.chat-feed {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.chat-items {
  display: flex;
  flex-direction: column;
  gap: 10px;

  padding: 12px 20px 4px 0;
  min-height: 100%;

  justify-content: flex-end;
}

.chat-day {
  align-self: center;
  font-size: 12px;
  font-weight: 500;
  color: var(--ink-3);
  background: var(--surface-sunk);
  border-radius: 999px;
  padding: 3px 12px;
  margin: 6px 0 2px;
}

/* ── bubbles ──────────────────────────────────────────────────── */

.chat-msg {
  max-width: min(78%, 520px);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.chat-msg.them { align-self: flex-start; align-items: flex-start; }
.chat-msg.me { align-self: flex-end; align-items: flex-end; }

.chat-bubble {
  position: relative;
  padding: 9px 13px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.42;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);

  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.chat-msg.me .chat-bubble {
  background: var(--accent);
  border-color: transparent;
  color: #fff;
}

.chat-msg.them .chat-bubble { border-bottom-left-radius: 5px; }
.chat-msg.me .chat-bubble { border-bottom-right-radius: 5px; }

.chat-who {
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-3);
  padding: 0 4px;
}

/* ── time and status ──────────────────────────────────────────── */

.chat-meta {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  color: var(--ink-3);
  padding: 0 4px;
}

.chat-msg.me .chat-meta { justify-content: flex-end; }

.chat-ticks { display: inline-flex; align-items: center; color: var(--ink-3); }
.chat-ticks svg {
  height: 11px;
  width: auto;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.chat-ticks.read { color: var(--accent); }

.chat-state { display: inline-flex; align-items: center; gap: 3px; }
.chat-state.sending { opacity: .65; }
.chat-state.failed { color: var(--stop); font-weight: 600; }

.chat-retry {
  border: 0;
  background: none;
  color: var(--stop);
  font: inherit;
  font-weight: 600;
  text-decoration: underline;
  cursor: pointer;
  padding: 0;
}

/* ── attachments ──────────────────────────────────────────────── */

.chat-shot {
  margin: 0;
  border-radius: 14px;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--line);
  max-width: 260px;
}

.chat-msg.me .chat-shot { background: var(--accent); border-color: transparent; }

.chat-shot-cap {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 7px 10px 8px;
  font-size: 12px;
}

.chat-shot-name {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--ink-2);
}

.chat-msg.me .chat-shot-name { color: #fff; }

.chat-shot-size { flex: 0 0 auto; color: var(--ink-3); }
.chat-msg.me .chat-shot-size { color: rgba(255, 255, 255, .75); }

.chat-shot-frame { position: relative; display: block; }

.chat-shot-eye {
  position: absolute;
  inset-block-start: 50%;
  inset-inline-start: 50%;
  transform: translate(-50%, -50%);
  width: 52px;
  height: 52px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: rgba(10, 15, 25, .55);
  backdrop-filter: blur(4px);
  color: #fff;
  pointer-events: none;
}

.chat-shot-eye svg { width: 26px; height: 26px; }

.chat-photo {
  display: block;
  width: 100%;
  max-height: 220px;
  object-fit: cover;

  object-position: top;
  cursor: zoom-in;
  background: var(--surface-sunk);
}

.chat-file {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 8px 8px 14px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--ink);
  text-decoration: none;
}

.chat-file-text { flex: 1 1 auto; min-width: 0; }

.chat-file-get {
  flex: 0 0 auto;
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;

  border: 0;
  border-radius: 50%;
  background: var(--surface-sunk);
  color: var(--ink-2);
  text-decoration: none;
  cursor: pointer;
}

.chat-file-get svg { width: 19px; height: 19px; }

.chat-msg.me .chat-file-get {
  background: rgba(255, 255, 255, .18);
  color: #fff;
}

.chat-file.gone .chat-file-get { display: none; }

.chat-msg.me .chat-file {
  background: var(--accent);
  border-color: transparent;
  color: #fff;
}

.chat-file-icon {
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  background: var(--surface-sunk);
  color: var(--ink-2);
}

.chat-msg.me .chat-file-icon {
  background: rgba(255, 255, 255, .18);
  color: #fff;
}

.chat-file-name {
  font-size: 14px;
  font-weight: 500;
  word-break: break-word;
}

.chat-file-size { font-size: 12px; opacity: .75; }

.chat-file.gone {
  background: var(--surface-sunk);
  border-style: dashed;
  color: var(--ink-3);
}

/* ── fullscreen photo viewer ──────────────────────────────────── */

.chat-lightbox {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: none;
  place-items: center;
  background: rgba(0, 0, 0, .92);
  padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom);
}

.chat-lightbox.open { display: grid; }

.chat-lightbox img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;

  touch-action: none;
  transform-origin: center center;
  will-change: transform;
}

.chat-lightbox-close,
.chat-lightbox-save {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 12px);
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, .16);
  color: #fff;
  cursor: pointer;
}

.chat-lightbox-close { inset-inline-end: 12px; }
.chat-lightbox-save { inset-inline-end: 64px; }
.chat-lightbox-save svg { width: 20px; height: 20px; }

body.chat-viewing { overflow: hidden; }

.chat-file-get.chat-busy { opacity: .5; pointer-events: none; }

/* ── composer ─────────────────────────────────────────────────── */

.chat-compose {
  flex: 0 0 auto;
  display: flex;

  align-items: flex-end;
  gap: 8px;

  margin-inline: calc(-1 * max(16px, env(safe-area-inset-left, 0px)))
                 calc(-1 * max(16px, env(safe-area-inset-right, 0px)));

  padding: 10px max(16px, env(safe-area-inset-right, 0px)) 10px
                max(16px, env(safe-area-inset-left, 0px));
  background: var(--bg);
  border-top: 1px solid var(--line);
}

.chat-input {
  flex: 1 1 auto;

  box-sizing: border-box;

  -webkit-appearance: none;
  appearance: none;
  display: block;
  height: 44px;
  min-height: 44px;
  max-height: 140px;

  padding: 11px 14px;
  border-radius: 22px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: 15px;
  line-height: 20px;
  resize: none;
}

.chat-input:focus {
  outline: none;
  border-color: var(--accent);
}

.chat-btn {
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: var(--surface-sunk);
  color: var(--ink-2);
  cursor: pointer;
  box-sizing: border-box;
  -webkit-appearance: none;
  appearance: none;

  padding: 0;
  line-height: 0;
}

.chat-btn svg { width: 21px; height: 21px; }

.chat-btn.send {
  background: var(--accent);
  color: #fff;
}
.chat-btn.send svg { transform: translate(-1px, 1px); }

.chat-btn:disabled { opacity: .5; cursor: default; }

.chat-progress {
  flex: 1 1 auto;
  height: 6px;
  border-radius: 3px;
  background: var(--surface-sunk);
  overflow: hidden;
}

.chat-progress span {
  display: block;
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width .15s linear;
}

/* ── empty and error states ───────────────────────────────────── */

.chat-empty {
  margin: auto;
  text-align: center;
  color: var(--ink-2);
  font-size: 15px;
  line-height: 1.5;
  padding: 24px 12px;
}

.chat-empty b {
  display: block;
  color: var(--ink);
  font-size: 17px;
  margin-bottom: 6px;
}

.chat-note {
  margin: 8px 0;
  padding: 10px 14px;
  border-radius: 12px;
  background: var(--surface-sunk);
  color: var(--ink-2);
  font-size: 13px;
  line-height: 1.45;
}

.chat-note.warn {
  background: color-mix(in srgb, var(--warn) 14%, transparent);
  color: var(--warn);
}

.chat-note.stop {
  background: color-mix(in srgb, var(--stop) 12%, transparent);
  color: var(--stop);
}

.tab:has(.tab-dot) { position: relative; }

.tab-dot {
  position: absolute;
  inset-block-start: 6px;
  inset-inline-start: calc(50% + 6px);
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
}

.chat-compose.sending .chat-input { opacity: .6; }

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

.chat-compose.sending .chat-btn.send svg { display: none; }

.chat-compose.sending .chat-btn.send::after {
  content: "";
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, .35);
  border-top-color: #fff;
  animation: chat-spin .7s linear infinite;
}

@keyframes chat-spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .chat-compose.sending .chat-btn.send::after { animation-duration: 2.4s; }
}

.chat-jump {
  position: absolute;
  inset-inline-end: 4px;
  inset-block-end: 74px;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px 8px 10px;
  border: 0;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 10px 26px -10px rgba(0, 0, 0, .55);
  cursor: pointer;
}

.chat-jump svg { width: 17px; height: 17px; }
.chat-jump[hidden] { display: none; }
.chat-jump-count[hidden] { display: none; }
.chat-jump:has(.chat-jump-count[hidden]) { padding: 9px; }

.chat { position: relative; }

.chat-debug {

  position: absolute;
  inset: 8px 8px auto 8px;
  z-index: 6;
  white-space: pre-line;
  padding: 6px 10px;
  border-radius: 8px;
  background: var(--warn);
  color: #1b1200;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}

.chat-compose.chat-debug-on .chat-input,
.chat-compose.chat-debug-on .chat-btn { outline: 1px dashed var(--warn); }

@media (max-width: 899px) {
  body.chat-open { padding-bottom: 0; }
}
