:root {
  --brand: #0f766e;
  --brand-strong: #115e59;
  --brand-soft: #ccfbf1;
  --bg: #eef2f1;
  --chat-bg: #e9efec;
  --surface: #ffffff;
  --surface-2: #f5f7f6;
  --text: #111b21;
  --muted: #667781;
  --border: #e2e8e6;
  --in: #ffffff;
  --out: #d7f5ea;
  --tick: #8696a0;
  --tick-read: #0ea5e9;
  --danger: #dc2626;
  --shadow: 0 1px 1px rgba(11, 20, 26, 0.08);
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --brand: #14b8a6;
    --brand-strong: #0d9488;
    --brand-soft: #134e4a;
    --bg: #0b1416;
    --chat-bg: #0d1719;
    --surface: #152024;
    --surface-2: #1b282c;
    --text: #e6edeb;
    --muted: #8fa3a8;
    --border: #22323a;
    --in: #1b2a2f;
    --out: #0f4a42;
    --tick: #8fa3a8;
    --tick-read: #38bdf8;
    --shadow: 0 1px 1px rgba(0, 0, 0, 0.3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input, textarea { font: inherit; color: inherit; }
a { color: var(--brand); }
[hidden] { display: none !important; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 0; border-radius: 999px; padding: 10px 18px; cursor: pointer;
  background: var(--brand); color: #fff; font-weight: 600; text-decoration: none;
}
.btn:hover { background: var(--brand-strong); }
.btn.ghost { background: transparent; color: var(--brand); border: 1px solid var(--border); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.small { padding: 6px 12px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }

.icon-btn {
  display: inline-grid; place-items: center; flex: none;
  width: 42px; height: 42px; border: 0; border-radius: 50%;
  background: transparent; color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn.primary { background: var(--brand); color: #fff; }
.icon-btn.primary:hover { background: var(--brand-strong); }
.icon-btn:disabled { opacity: .5; cursor: default; }

.avatar {
  display: grid; place-items: center; flex: none;
  width: 40px; height: 40px; border-radius: 50%;
  background: var(--brand-soft); color: var(--brand-strong); font-weight: 700; font-size: 15px;
  position: relative;
}
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .avatar { color: #99f6e4; } }
.avatar.online::after {
  content: ""; position: absolute; right: 0; bottom: 0; width: 11px; height: 11px;
  border-radius: 50%; background: #22c55e; border: 2px solid var(--surface);
}

/* ---------- Mensagens ---------- */
.scroller {
  flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
  background-color: var(--chat-bg);
  background-image: radial-gradient(color-mix(in srgb, var(--brand) 9%, transparent) 1px, transparent 1px);
  background-size: 18px 18px;
  padding: 12px 0 8px;
}
.scroller.dragging { outline: 3px dashed var(--brand); outline-offset: -10px; }
.msgs { max-width: 920px; margin: 0 auto; padding: 0 4%; display: flex; flex-direction: column; gap: 2px; }

.day, .sys { display: flex; justify-content: center; margin: 10px 0 6px; }
.day span, .sys span {
  background: var(--surface); color: var(--muted); font-size: 12.5px;
  padding: 5px 12px; border-radius: 8px; box-shadow: var(--shadow);
}
.sys span { background: color-mix(in srgb, var(--brand-soft) 70%, var(--surface)); color: var(--text); }

.row { display: flex; margin: 1px 0; }
.row.out { justify-content: flex-end; }
.bubble {
  position: relative; max-width: min(78%, 560px); min-width: 72px;
  padding: 6px 8px 4px; border-radius: var(--radius);
  background: var(--in); box-shadow: var(--shadow);
  overflow-wrap: anywhere;
}
.row.out .bubble { background: var(--out); }
.row.in .bubble { border-top-left-radius: 2px; }
.row.out .bubble { border-top-right-radius: 2px; }
.bubble.failed { outline: 1px solid var(--danger); }
.bubble .author { font-size: 12.5px; font-weight: 600; color: var(--brand); margin-bottom: 2px; }
.bubble .via { font-size: 11px; color: var(--muted); margin-bottom: 2px; }
.bubble .text { white-space: pre-wrap; padding: 0 2px; }
.bubble .meta {
  display: flex; justify-content: flex-end; align-items: center; gap: 4px;
  font-size: 11px; color: var(--muted); margin-top: 2px; user-select: none;
}
.tick { color: var(--tick); font-size: 12px; letter-spacing: -3px; padding-right: 3px; }
.tick.read { color: var(--tick-read); }
.tick.failed { color: var(--danger); letter-spacing: 0; font-weight: 700; }
.tick.pending { letter-spacing: 0; }

.media-img, .media-video {
  display: block; max-width: 100%; max-height: 360px; border-radius: 7px;
  margin-bottom: 4px; cursor: zoom-in; background: var(--surface-2);
}
.media-video { cursor: auto; width: 100%; }
.media-audio { display: block; width: min(300px, 64vw); height: 40px; margin: 2px 0; }
.file {
  display: flex; align-items: center; gap: 10px; text-decoration: none; color: inherit;
  background: color-mix(in srgb, var(--text) 6%, transparent); border-radius: 8px; padding: 8px 10px; margin-bottom: 4px;
  min-width: 220px;
}
.file-ico {
  display: grid; place-items: center; width: 38px; height: 44px; border-radius: 5px;
  background: var(--brand); color: #fff; font-size: 10.5px; font-weight: 700; flex: none;
}
.file-info { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.file-name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-size { font-size: 12px; color: var(--muted); }
.file-dl { color: var(--muted); }
.progress { height: 4px; background: color-mix(in srgb, var(--text) 10%, transparent); border-radius: 2px; overflow: hidden; margin: 4px 0 2px; }
.progress > div { height: 100%; background: var(--brand); transition: width .2s; }

/* ---------- Composer ---------- */
.composer { background: var(--surface-2); border-top: 1px solid var(--border); padding: 6px 8px calc(6px + env(safe-area-inset-bottom)); }
.input-bar, .rec-bar { display: flex; align-items: flex-end; gap: 6px; max-width: 920px; margin: 0 auto; }
.input-wrap { flex: 1; background: var(--surface); border-radius: 22px; padding: 9px 14px; min-height: 42px; display: flex; }
.input-wrap textarea {
  flex: 1; border: 0; outline: 0; resize: none; background: transparent;
  max-height: 140px; line-height: 1.4; padding: 0;
}
.rec-bar { align-items: center; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: pulse 1s infinite; }
.rec-time { font-variant-numeric: tabular-nums; font-weight: 600; }
.rec-hint { flex: 1; color: var(--muted); }
@keyframes pulse { 50% { opacity: .3; } }

/* ---------- Diversos ---------- */
.lightbox {
  position: fixed; inset: 0; z-index: 100; background: rgba(0, 0, 0, .88);
  display: grid; place-items: center; padding: 16px; cursor: zoom-out;
}
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 6px; }
.lightbox-close { position: absolute; top: 12px; right: 16px; background: none; border: 0; color: #fff; font-size: 26px; cursor: pointer; }

#toast {
  position: fixed; left: 50%; bottom: 90px; transform: translate(-50%, 20px); z-index: 200;
  background: #111b21; color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: .2s; max-width: calc(100% - 32px); text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

.typing { color: var(--brand) !important; font-style: italic; }
