:root {
  --bg: #f5f5f7;
  --panel: #ffffff;
  --panel-2: #fafafa;
  --line: #e5e5ea;
  --text: #1c1c1e;
  --muted: #8e8e93;
  --hover: #f2f2f7;
  --active: #e8f0fe;
  --bubble-in: #e9e9eb;
  --bubble-in-text: #1c1c1e;
  --imessage: #0a84ff;
  --sms: #30c158;
  --danger: #ff3b30;
  --radius: 18px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .04), 0 8px 24px rgba(0, 0, 0, .06);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --panel: #121214;
    --panel-2: #18181b;
    --line: #26262b;
    --text: #f2f2f7;
    --muted: #8e8e93;
    --hover: #1c1c20;
    --active: #1d2a3d;
    --bubble-in: #26262a;
    --bubble-in-text: #f2f2f7;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Inter, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.app {
  display: grid;
  grid-template-columns: 360px 1fr;
  height: 100vh;
  height: 100dvh;
  max-width: 1500px;
  margin: 0 auto;
  background: var(--panel);
  box-shadow: var(--shadow);
}

/* ---------- barra lateral ---------- */
.sidebar {
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--line);
  min-height: 0;
  background: var(--panel);
}
.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 16px 10px;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 24px; margin: 0; letter-spacing: -.02em; }
.pill {
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 999px;
  white-space: nowrap;
}
.pill-ok { background: color-mix(in srgb, var(--sms) 16%, transparent); color: var(--sms); }
.pill-wait { background: color-mix(in srgb, #f59e0b 18%, transparent); color: #d97706; }
.pill-bad { background: color-mix(in srgb, var(--danger) 15%, transparent); color: var(--danger); }

.icon-btn {
  width: 36px; height: 36px;
  display: inline-grid; place-items: center;
  border: 0; border-radius: 10px;
  background: transparent;
  color: var(--imessage);
}
.icon-btn:hover { background: var(--hover); }

.search { padding: 0 16px 10px; }
.search input {
  width: 100%;
  border: 0;
  border-radius: 10px;
  padding: 9px 12px;
  background: var(--hover);
  outline: none;
}
.search input:focus { box-shadow: 0 0 0 3px color-mix(in srgb, var(--imessage) 30%, transparent); }

.lines-panel { margin: 0 16px 10px; border: 1px solid var(--line); border-radius: 12px; padding: 8px 12px; }
.lines-panel summary { cursor: pointer; font-size: 12.5px; font-weight: 600; color: var(--muted); list-style: none; }
.lines-panel summary::-webkit-details-marker { display: none; }
.lines-panel summary::after { content: "▾"; float: right; }
.lines-panel:not([open]) summary::after { content: "▸"; }
.lines-list { list-style: none; margin: 8px 0 4px; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.lines-list li { display: grid; grid-template-columns: 10px 1fr auto; gap: 8px; align-items: center; font-size: 13px; }
.lines-list .dot { width: 9px; height: 9px; border-radius: 50%; }
.lines-list li.off { color: var(--muted); }
.lines-list li.off .dot { background: transparent !important; border: 1.5px solid var(--muted); width: 8px; height: 8px; }
.lines-list .num { color: var(--muted); font-size: 12px; margin-left: 6px; font-variant-numeric: tabular-nums; }
.lines-list .tag { font-size: 11px; font-weight: 600; color: var(--muted); white-space: nowrap; }
.lines-list .tag.on { color: var(--sms); }
.lines-help { margin: 6px 0 2px; font-size: 11.5px; color: var(--muted); line-height: 1.35; }

.line-btn { flex-direction: column; align-items: flex-start; gap: 0; padding: 5px 11px 6px; text-align: left; }
.line-btn .line-top { display: inline-flex; align-items: center; gap: 6px; }
.line-btn .line-num { font-size: 11px; font-weight: 500; color: var(--muted); font-variant-numeric: tabular-nums; }
.line-btn .line-slot { font-size: 10px; font-weight: 700; color: var(--muted); border: 1px solid var(--line); border-radius: 4px; padding: 0 3px; }

.banner {
  margin: 0 16px 10px;
  padding: 8px 12px;
  border-radius: 10px;
  font-size: 13px;
  background: color-mix(in srgb, var(--imessage) 12%, transparent);
  color: var(--imessage);
}

.conv-list { list-style: none; margin: 0; padding: 0 8px 16px; overflow-y: auto; flex: 1; }
.conv {
  display: grid;
  grid-template-columns: 46px 1fr auto;
  gap: 12px;
  align-items: center;
  padding: 10px 10px;
  border-radius: 12px;
  cursor: pointer;
  position: relative;
}
.conv:hover { background: var(--hover); }
.conv.active { background: var(--active); }
.conv-main { min-width: 0; }
.conv-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-preview {
  color: var(--muted);
  font-size: 13.5px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.conv-side { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; align-self: start; padding-top: 2px; }
.conv-time { color: var(--muted); font-size: 12px; white-space: nowrap; }
.conv.unread .conv-name { font-weight: 700; }
.conv.unread .conv-preview { color: var(--text); }
.badge {
  min-width: 20px; height: 20px; padding: 0 6px;
  border-radius: 999px;
  background: var(--imessage); color: #fff;
  font-size: 11.5px; font-weight: 700;
  display: inline-grid; place-items: center;
}
.svc-dot { width: 7px; height: 7px; border-radius: 50%; display: inline-block; margin-right: 6px; vertical-align: 1px; }
.svc-dot.sms { background: var(--sms); }
.svc-dot.imessage { background: var(--imessage); }

.avatar {
  width: 46px; height: 46px;
  border-radius: 50%;
  display: grid; place-items: center;
  color: #fff; font-weight: 600; font-size: 16px;
  background: linear-gradient(180deg, #a1a1aa, #71717a);
  flex: none;
  user-select: none;
}

/* ---------- conversa ---------- */
.thread { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--panel); }
.empty { margin: auto; text-align: center; color: var(--muted); }
.empty-icon { font-size: 48px; margin-bottom: 8px; }

.thread-view { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.thread-head {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 85%, transparent);
  backdrop-filter: blur(12px);
}
.thread-head .avatar { width: 40px; height: 40px; font-size: 15px; }
.thread-title .name { font-weight: 650; font-size: 16px; }
.thread-title .sub { color: var(--muted); font-size: 12.5px; }
.back { display: none; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 18px 18px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.day {
  align-self: center;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  margin: 16px 0 8px;
}
.load-more {
  align-self: center;
  border: 0; background: var(--hover); color: var(--muted);
  border-radius: 999px; padding: 6px 14px; font-size: 12.5px; margin-bottom: 8px;
}

.msg { display: flex; flex-direction: column; max-width: min(72%, 560px); }
.msg.me { align-self: flex-end; align-items: flex-end; }
.msg.them { align-self: flex-start; align-items: flex-start; }
.msg + .msg.gap { margin-top: 10px; }
.sender { font-size: 11.5px; color: var(--muted); margin: 0 12px 2px; }
.bubble {
  padding: 8px 13px;
  border-radius: var(--radius);
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.them .bubble { background: var(--bubble-in); color: var(--bubble-in-text); border-bottom-left-radius: 6px; }
.me .bubble { color: #fff; border-bottom-right-radius: 6px; }
.me.sms .bubble { background: var(--sms); }
.me.imessage .bubble { background: var(--imessage); }
.me.pending .bubble { opacity: .6; }
.me.failed .bubble { background: var(--danger); }
.bubble a { color: inherit; }
.att-img { display: block; max-width: 280px; max-height: 320px; border-radius: 14px; margin: 2px 0; cursor: zoom-in; background: var(--hover); }
.att-file {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 8px 12px; border-radius: 14px;
  background: var(--hover); color: var(--text); text-decoration: none; font-size: 13.5px; margin: 2px 0;
}
.meta { font-size: 11px; color: var(--muted); margin: 3px 8px 0; display: flex; gap: 6px; align-items: center; }
.meta .line-tag { font-weight: 600; }
.meta .st-failed { color: var(--danger); font-weight: 600; }

/* ---------- composer ---------- */
.composer {
  border-top: 1px solid var(--line);
  padding: 10px 14px calc(12px + env(safe-area-inset-bottom));
  background: var(--panel);
}
.composer-opts { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 8px; }
.seg { display: inline-flex; background: var(--hover); border-radius: 10px; padding: 2px; }
.seg-btn {
  border: 0; background: transparent;
  padding: 5px 11px; border-radius: 8px;
  font-size: 12.5px; font-weight: 600; color: var(--muted);
  display: inline-flex; align-items: center; gap: 6px;
}
.seg-btn[aria-checked="true"] { background: var(--panel); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }
.seg-btn:disabled { opacity: .35; cursor: not-allowed; }
.seg-btn .dot { width: 8px; height: 8px; border-radius: 50%; }
.counter { margin-left: auto; color: var(--muted); font-size: 12px; }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 9px 14px;
  max-height: 180px;
  background: var(--panel);
  outline: none;
}
.composer textarea:focus { border-color: color-mix(in srgb, var(--imessage) 55%, var(--line)); }
.send-btn {
  width: 36px; height: 36px; border-radius: 50%; border: 0;
  display: grid; place-items: center;
  color: #fff; background: var(--imessage);
  flex: none; margin-bottom: 1px;
}
.send-btn.sms { background: var(--sms); }
.send-btn:disabled { opacity: .35; cursor: default; }
.composer-note { margin-top: 6px; font-size: 12.5px; color: var(--muted); }

/* ---------- diálogos ---------- */
.dialog {
  border: 0; border-radius: 16px; padding: 0;
  width: min(420px, calc(100vw - 32px));
  background: var(--panel); color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .3);
}
.dialog::backdrop { background: rgba(0, 0, 0, .35); backdrop-filter: blur(2px); }
.dialog form { padding: 20px; display: flex; flex-direction: column; gap: 10px; }
.dialog h2 { margin: 0 0 4px; font-size: 18px; }
.dialog label { font-size: 13px; color: var(--muted); }
.dialog input {
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; background: var(--panel); outline: none;
}
.dialog input:focus { border-color: var(--imessage); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.btn-primary, .btn-ghost { border: 0; border-radius: 10px; padding: 9px 16px; font-weight: 600; }
.btn-primary { background: var(--imessage); color: #fff; }
.btn-ghost { background: var(--hover); }
.muted { color: var(--muted); margin: 0; font-size: 13.5px; }
.suggest { list-style: none; margin: 0; padding: 0; max-height: 240px; overflow-y: auto; }
.suggest li { padding: 8px 10px; border-radius: 10px; cursor: pointer; display: flex; justify-content: space-between; gap: 10px; }
.suggest li:hover, .suggest li.sel { background: var(--hover); }
.suggest .addr { color: var(--muted); font-size: 13px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: #1c1c1e; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px;
  box-shadow: var(--shadow); z-index: 10; max-width: calc(100vw - 32px);
}

.lightbox {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .85);
  display: grid; place-items: center; z-index: 20; cursor: zoom-out;
}
.lightbox img { max-width: 94vw; max-height: 94vh; border-radius: 8px; }

/* ---------- móvel ---------- */
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .app.show-thread .sidebar { display: none; }
  .app:not(.show-thread) .thread { display: none; }
  .back { display: inline-grid; }
  .msg { max-width: 82%; }
}
