:root {
  --green: #00a884;
  --green-dark: #008069;
  --bg: #efeae2;
  --panel: #ffffff;
  --panel-alt: #f0f2f5;
  --border: #e9edef;
  --text: #111b21;
  --muted: #667781;
  --bubble-out: #d9fdd3;
  --bubble-in: #ffffff;
  --danger: #d93025;
  --blue: #53bdeb;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif; color: var(--text); background: var(--panel-alt); font-size: 14px; }
a { color: var(--green-dark); }
code, pre { font-family: Consolas, "Cascadia Mono", monospace; }
code { background: var(--panel-alt); padding: 1px 5px; border-radius: 4px; }
pre { background: #1f2c34; color: #e9edef; padding: 12px 14px; border-radius: 8px; overflow-x: auto; font-size: 13px; }
.muted { color: var(--muted); }

/* barra superior */
.topbar { display: flex; align-items: center; gap: 24px; padding: 0 20px; height: 52px; background: var(--green-dark); color: #fff; }
.topbar a { color: #fff; text-decoration: none; }
.brand { font-weight: 600; font-size: 16px; }
.nav-links { display: flex; gap: 18px; flex: 1; }
.nav-links a { opacity: .85; }
.nav-links a:hover { opacity: 1; }
.nav-right { display: flex; align-items: center; gap: 12px; }
.nav-right select { padding: 4px 6px; border-radius: 6px; border: 0; }
.nav-right form { margin: 0; }
.link-btn { background: none; border: 0; color: inherit; cursor: pointer; text-decoration: underline; font: inherit; padding: 0; }
.topbar .link-btn { color: #fff; }
.card .link-btn { color: var(--green-dark); }

/* mensajes flash */
.flashes { position: fixed; top: 60px; right: 20px; z-index: 10; display: flex; flex-direction: column; gap: 8px; max-width: 420px; }
.flash { padding: 10px 14px; border-radius: 8px; background: #fff; box-shadow: 0 2px 8px rgba(0,0,0,.12); border-left: 4px solid var(--green); }
.flash-error { border-left-color: var(--danger); }

/* botones y formularios */
.btn { display: inline-block; padding: 8px 14px; border-radius: 8px; border: 1px solid var(--border); background: #fff; color: var(--text); cursor: pointer; text-decoration: none; font: inherit; }
.btn-primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn-primary:hover { background: var(--green-dark); }
.btn-primary:disabled { opacity: .6; cursor: default; }
.btn-danger { background: #fff; border-color: var(--danger); color: var(--danger); }
input, select, textarea { font: inherit; padding: 8px 10px; border: 1px solid #d1d7db; border-radius: 8px; background: #fff; }
label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
fieldset { border: 1px solid var(--border); border-radius: 8px; padding: 12px; display: grid; gap: 12px; }
legend { color: var(--muted); padding: 0 6px; }

/* páginas de contenido */
.page { max-width: 1000px; margin: 24px auto; padding: 0 16px; display: grid; gap: 16px; }
.card { background: #fff; border-radius: 10px; padding: 20px 24px; box-shadow: 0 1px 3px rgba(0,0,0,.06); }
.card h2, .card h3 { margin-top: 0; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 12px; }
.card-head h2, .card-head h3 { margin: 0; }
.form-grid { display: grid; gap: 14px; max-width: 560px; }
.inline-form { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table th { color: var(--muted); font-weight: 600; }
.table form { margin: 0; }
.actions a { margin-right: 10px; }
.tag { display: inline-block; font-size: 11px; padding: 2px 8px; border-radius: 10px; background: #fff3cd; color: #8a6d00; vertical-align: middle; }
.tag-ok { background: #d9fdd3; color: #0a6b3d; }
.tag-off { background: #fde2e1; color: #a52714; }
.kv { display: grid; grid-template-columns: 160px 1fr; gap: 10px 16px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.copy { user-select: all; word-break: break-all; }
.token-new { background: #fff3cd; font-size: 15px; padding: 4px 8px; }
.warn { color: #8a6d00; margin: 6px 0 0; }

/* login */
.login-page { display: grid; place-items: center; background: linear-gradient(var(--green-dark) 0 220px, var(--panel-alt) 220px); }
.login-card { background: #fff; padding: 32px; border-radius: 12px; width: 360px; box-shadow: 0 2px 12px rgba(0,0,0,.12); }
.login-card h1 { font-size: 20px; margin: 0 0 4px; }
.login-card form { display: grid; gap: 14px; margin-top: 18px; }

/* chat */
.chat-page { overflow: hidden; }
.chat-app { display: grid; grid-template-columns: 360px 1fr; height: calc(100vh - 52px); }
.sidebar { display: flex; flex-direction: column; background: #fff; border-right: 1px solid var(--border); min-height: 0; }
.sidebar-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; background: var(--panel-alt); }
.client-name { font-weight: 600; }
.search { padding: 8px 12px; border-bottom: 1px solid var(--border); }
.search input { width: 100%; background: var(--panel-alt); border: 0; }
.icon-btn { background: none; border: 0; font-size: 20px; cursor: pointer; color: var(--muted); padding: 4px 8px; border-radius: 50%; display: inline-flex; align-items: center; }
.icon-btn:hover { background: rgba(0,0,0,.06); }
.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conv-item { display: flex; gap: 12px; padding: 10px 16px; cursor: pointer; border-bottom: 1px solid var(--border); align-items: center; }
.conv-item:hover { background: var(--panel-alt); }
.conv-item.active { background: #e9edef; }
.avatar { width: 44px; height: 44px; border-radius: 50%; background: #dfe5e7; color: #fff; display: grid; place-items: center; font-weight: 600; flex-shrink: 0; background: #8696a0; }
.conv-body { flex: 1; min-width: 0; }
.conv-top, .conv-bottom { display: flex; justify-content: space-between; gap: 8px; }
.conv-name { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-time { color: var(--muted); font-size: 12px; flex-shrink: 0; }
.conv-preview { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge { background: var(--green); color: #fff; border-radius: 10px; font-size: 11px; padding: 1px 7px; flex-shrink: 0; }
.empty-list { padding: 24px 16px; color: var(--muted); text-align: center; }

.thread { display: flex; flex-direction: column; min-height: 0; background: var(--bg); }
.thread-head { display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--panel-alt); border-bottom: 1px solid var(--border); min-height: 60px; }
.thread-head .title { font-weight: 600; }
.thread-head .sub { color: var(--muted); font-size: 12px; }
.messages { flex: 1; overflow-y: auto; padding: 16px 7%; display: flex; flex-direction: column; gap: 3px; }
.day-sep { align-self: center; background: #fff; color: var(--muted); font-size: 12px; padding: 4px 12px; border-radius: 8px; margin: 10px 0; box-shadow: 0 1px .5px rgba(0,0,0,.1); }
.bubble { max-width: 65%; padding: 6px 9px 8px; border-radius: 8px; box-shadow: 0 1px .5px rgba(0,0,0,.13); position: relative; word-wrap: break-word; }
.bubble.out { align-self: flex-end; background: var(--bubble-out); }
.bubble.in { align-self: flex-start; background: var(--bubble-in); }
.bubble.failed { background: #fde2e1; }
.bubble .text { white-space: pre-wrap; }
.bubble .meta { display: flex; justify-content: flex-end; align-items: center; gap: 4px; font-size: 11px; color: var(--muted); margin-top: 2px; }
.bubble .tpl { font-size: 12px; color: var(--green-dark); font-weight: 600; margin-bottom: 2px; }
.bubble .ref { font-size: 11px; color: var(--muted); margin-right: auto; }
.bubble img.media-img { display: block; max-width: 100%; max-height: 320px; border-radius: 6px; margin-bottom: 4px; }
.bubble video, .bubble audio { display: block; max-width: 100%; margin-bottom: 4px; }
.file-card { display: flex; align-items: center; gap: 10px; background: rgba(0,0,0,.05); padding: 8px 10px; border-radius: 6px; text-decoration: none; color: var(--text); margin-bottom: 4px; }
.file-card .icon { font-size: 26px; }
.file-card .fname { font-weight: 500; word-break: break-all; }
.file-card .fsize { font-size: 12px; color: var(--muted); }
.status.read { color: var(--blue); }
.status.failed { color: var(--danger); }
.error-line { font-size: 12px; color: var(--danger); margin-top: 4px; }
.retry-btn { background: none; border: 0; color: var(--green-dark); cursor: pointer; text-decoration: underline; font-size: 12px; padding: 0 0 0 6px; }

.composer { background: var(--panel-alt); padding: 8px 16px; }
.composer-row { display: flex; align-items: flex-end; gap: 8px; }
.composer textarea { flex: 1; resize: none; max-height: 140px; border: 0; border-radius: 8px; padding: 10px 12px; }
.attach { cursor: pointer; }
.file-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.file-chips:not(:empty) { margin-bottom: 8px; }
.chip { background: #fff; border-radius: 14px; padding: 4px 10px; font-size: 12px; display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); }
.chip button { background: none; border: 0; cursor: pointer; color: var(--muted); font-size: 14px; padding: 0; }
.composer-error { color: var(--danger); font-size: 12px; }
.composer-error:not(:empty) { margin-top: 6px; }

@media (max-width: 800px) {
  .chat-app { grid-template-columns: 1fr; }
  .chat-app.has-thread .sidebar { display: none; }
  .chat-app:not(.has-thread) .thread { display: none; }
  .nav-links { display: none; }
  .bubble { max-width: 85%; }
}
