:root {
  --bg: #f4f5f4; --panel: #ffffff; --panel-2: #f8faf9; --line: #e3e6e4;
  --fg: #16201d; --muted: #5d6b66; --accent: #0f766e; --accent-fg: #ffffff;
  --accent-soft: #ccfbf1; --in: #ffffff; --out: #d7f5ec; --danger: #b42318; --warn-bg: #fff4e5; --warn-fg: #8a4b00;
  --read: #0284c7; --radius: 12px; --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.04);
}
:root { color-scheme: light; }
/* Oscuro automático (si el teléfono/PC está en modo oscuro), salvo que el usuario elija "Claro" */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1412; --panel: #151d1a; --panel-2: #1a2420; --line: #26322d;
    --fg: #e6efec; --muted: #94a59f; --accent: #2dd4bf; --accent-fg: #062b26;
    --accent-soft: #123b35; --in: #1d2925; --out: #10443c; --danger: #f97066; --warn-bg: #3a2a10; --warn-fg: #fbc56a;
    --read: #38bdf8; --shadow: none;
    color-scheme: dark;
  }
}
/* Oscuro elegido a mano */
:root[data-theme="dark"] {
    --bg: #0e1412; --panel: #151d1a; --panel-2: #1a2420; --line: #26322d;
    --fg: #e6efec; --muted: #94a59f; --accent: #2dd4bf; --accent-fg: #062b26;
    --accent-soft: #123b35; --in: #1d2925; --out: #10443c; --danger: #f97066; --warn-bg: #3a2a10; --warn-fg: #fbc56a;
    --read: #38bdf8; --shadow: none;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--fg); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 9px 11px; width: 100%; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.muted { color: var(--muted); } .small { font-size: 13px; }
.error { color: var(--danger); margin: 0; font-size: 14px; }

.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 9px 14px; cursor: pointer; white-space: nowrap; }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: transparent; font-weight: 600; }
.btn.primary:disabled { opacity: .55; cursor: not-allowed; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--panel-2); }
.btn.small { padding: 5px 9px; font-size: 13px; }
.btn.icon { padding: 7px 10px; }
.btn.danger { color: var(--danger); }
.link { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 4px; }

/* AUTH */
.auth { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.auth-card { width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 28px 24px; display: grid; gap: 14px; box-shadow: var(--shadow); }
.auth-card h1 { margin: 0; font-size: 22px; }
.auth-card p { margin: 0; }
.auth-card label { display: grid; gap: 6px; font-size: 14px; font-weight: 500; }
.auth-logo { border-radius: 14px; }

/* LAYOUT */
.app { display: grid; grid-template-columns: 220px 1fr; height: 100dvh; }
.side { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 14px 12px; gap: 14px; min-width: 0; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; }
.brand img { border-radius: 7px; }
.nav { display: grid; gap: 4px; }
.nav-btn { text-align: left; background: none; border: 0; padding: 9px 10px; border-radius: 10px; cursor: pointer; }
.nav-btn:hover { background: var(--panel-2); }
.nav-btn.active { background: var(--accent-soft); font-weight: 600; }
.side-foot { margin-top: auto; display: grid; gap: 6px; }
.side-foot .muted { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.main { min-width: 0; min-height: 0; overflow: hidden; }

/* INBOX */
.inbox { display: grid; grid-template-columns: 340px 1fr; height: 100%; min-height: 0; }
.list-col { border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; background: var(--panel); }
.list-head { padding: 12px; display: grid; gap: 10px; border-bottom: 1px solid var(--line); }
.tabs { display: flex; gap: 4px; overflow-x: auto; }
.tab { border: 1px solid var(--line); background: none; border-radius: 99px; padding: 4px 11px; font-size: 13px; cursor: pointer; white-space: nowrap; }
.tab.active { background: var(--accent); color: var(--accent-fg); border-color: transparent; }
.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conv { display: grid; grid-template-columns: 40px 1fr auto; gap: 10px; padding: 11px 12px; border-bottom: 1px solid var(--line); cursor: pointer; align-items: center; }
.conv:hover { background: var(--panel-2); }
.conv.active { background: var(--accent-soft); }
.avatar { width: 40px; height: 40px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-weight: 700; }
.conv-main { min-width: 0; }
.conv-top { display: flex; justify-content: space-between; gap: 8px; }
.conv-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-prev { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-side { display: grid; justify-items: end; gap: 4px; font-size: 12px; color: var(--muted); }
.badge { background: var(--accent); color: var(--accent-fg); border-radius: 99px; min-width: 20px; padding: 1px 6px; text-align: center; font-weight: 700; font-size: 12px; }
.dots { display: flex; gap: 3px; }
.dot { width: 8px; height: 8px; border-radius: 50%; }
.empty { color: var(--muted); padding: 24px; text-align: center; }
.empty.big { display: grid; place-items: center; height: 100%; font-size: 16px; }

.chat-col { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.chat { display: flex; flex-direction: column; height: 100%; min-height: 0; position: relative; }
.chat-head { display: flex; align-items: center; gap: 10px; padding: 10px 14px; background: var(--panel); border-bottom: 1px solid var(--line); }
.who { display: grid; min-width: 0; flex: 1; }
.who strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-actions { display: flex; gap: 6px; align-items: center; }
.chat-actions select { width: auto; padding: 6px 8px; }
.tag-row { display: flex; flex-wrap: wrap; gap: 6px; padding: 8px 14px; background: var(--panel); border-bottom: 1px solid var(--line); }
.chip { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); border-radius: 99px; padding: 3px 10px; font-size: 13px; cursor: pointer; background: none; }
.chip .dot { width: 9px; height: 9px; }
.chip.on { background: var(--accent-soft); border-color: var(--accent); font-weight: 600; }
.banner { padding: 9px 14px; font-size: 14px; background: var(--warn-bg); color: var(--warn-fg); border-bottom: 1px solid var(--line); }
.banner.ok { background: var(--accent-soft); color: var(--fg); }
.messages { flex: 1; overflow-y: auto; padding: 16px 14px; display: flex; flex-direction: column; gap: 6px; }
.day { align-self: center; font-size: 12px; color: var(--muted); background: var(--panel); border: 1px solid var(--line); padding: 2px 10px; border-radius: 99px; margin: 8px 0; }
.msg { max-width: min(78%, 560px); padding: 8px 11px 6px; border-radius: 14px; box-shadow: var(--shadow); white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.in { align-self: flex-start; background: var(--in); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.msg.out { align-self: flex-end; background: var(--out); border-bottom-right-radius: 4px; }
.msg .meta { display: flex; justify-content: flex-end; gap: 5px; font-size: 11px; color: var(--muted); margin-top: 3px; }
.msg .kind { font-size: 12px; color: var(--muted); font-style: italic; }
.tick.read { color: var(--read); } .tick.failed { color: var(--danger); }
.msg .err { color: var(--danger); font-size: 12px; }
.composer { display: flex; gap: 8px; padding: 10px 12px; border-top: 1px solid var(--line); background: var(--panel); align-items: flex-end; }
.composer textarea { resize: none; max-height: 160px; }
.composer.template input { flex: 1; }
.qr-pop { position: absolute; left: 12px; right: 12px; bottom: 64px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 8px 30px rgba(0,0,0,.15); max-height: 260px; overflow-y: auto; z-index: 5; }
.qr-item { display: block; width: 100%; text-align: left; background: none; border: 0; border-bottom: 1px solid var(--line); padding: 9px 12px; cursor: pointer; }
.qr-item:hover, .qr-item.sel { background: var(--accent-soft); }
.qr-item b { color: var(--accent); }
.qr-item span { display: block; color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* PANES */
.pane { padding: 20px; overflow-y: auto; height: 100%; }
.pane h2 { margin: 0 0 16px; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; align-items: start; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 16px; }
.card h3 { margin: 0 0 6px; font-size: 16px; }
.row-form { display: grid; gap: 8px; margin: 12px 0; }
.row-form.inline { grid-template-columns: 1fr 52px auto; }
.row-form.inline input[type=color] { padding: 2px; height: 40px; }
.grid-form { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
.item-list { list-style: none; margin: 0; padding: 0; }
.item-list li { display: flex; gap: 10px; align-items: flex-start; padding: 9px 0; border-top: 1px solid var(--line); }
.item-list .grow { flex: 1; min-width: 0; }
.item-list .body { color: var(--muted); font-size: 13px; white-space: pre-wrap; overflow-wrap: anywhere; }
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.table th { color: var(--muted); font-weight: 500; font-size: 13px; }
.table select { width: auto; padding: 4px 8px; }
.pill { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: 12px; background: var(--panel-2); border: 1px solid var(--line); }

.toast { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); background: var(--fg); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 50; max-width: calc(100% - 32px); box-shadow: 0 8px 30px rgba(0,0,0,.2); }
.toast.bad { background: var(--danger); color: #fff; }

.only-mobile { display: none; }

/* MÓVIL / TABLET */
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .side { flex-direction: row; align-items: center; padding: 8px 10px; gap: 8px; border-right: 0; border-bottom: 1px solid var(--line); overflow-x: auto; }
  .brand span { display: none; }
  .biz-picker { flex: 1; min-width: 120px; }
  .nav { display: flex; }
  .nav-btn span { display: none; }
  .side-foot { margin: 0; display: flex; }
  .side-foot .muted { display: none; }
  .inbox { grid-template-columns: 1fr; }
  .inbox.show-chat .list-col { display: none; }
  .inbox:not(.show-chat) .chat-col { display: none; }
  .only-mobile { display: inline-block; }
  .pane { padding: 16px; }
  .msg { max-width: 88%; }
}

/* ARCHIVOS */
.media { margin: 2px 0 4px; }
.media img { display: block; max-width: min(320px, 100%); max-height: 360px; border-radius: 10px; cursor: zoom-in; object-fit: cover; background: var(--panel-2); }
.media-sticker img { max-width: 160px; background: transparent; cursor: default; }
.media audio { width: min(300px, 70vw); height: 40px; display: block; }
.media video { display: block; max-width: min(340px, 100%); max-height: 380px; border-radius: 10px; background: #000; }
.doc { display: flex; flex-wrap: wrap; gap: 4px 6px; align-items: baseline; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel-2); color: var(--fg); text-decoration: none; }
.doc span { font-weight: 600; overflow-wrap: anywhere; }
.caption { margin-top: 2px; }

/* ===== v2: equipo, roles, guía, avisos ===== */
.btn.big { padding: 12px 16px; font-size: 16px; }
.btn.on { color: var(--accent); }
.lead { margin: -8px 0 16px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; margin-top: 12px; }
.nav-btn { position: relative; display: flex; align-items: center; gap: 6px; }
.nav-badge { margin-left: auto; background: var(--accent); color: var(--accent-fg); border-radius: 99px; font-size: 11px; padding: 1px 7px; }
.side-foot .btn { justify-content: flex-start; text-align: left; }
.conv.waiting .conv-name::after { content: " •"; color: var(--accent); }
.composer.readonly { justify-content: center; padding: 14px; }
.composer.template { justify-content: center; }
.msg .by { font-weight: 600; margin-right: auto; padding-right: 8px; }
.pill.warn { background: var(--warn-bg); color: var(--warn-fg); border-color: transparent; margin-left: 6px; }
.avatar.small { width: 32px; height: 32px; font-size: 14px; flex: none; }
.item-list li > select { width: auto; max-width: 140px; padding: 5px 8px; }
details.more { position: relative; }
details.more > summary { list-style: none; }
details.more > summary::-webkit-details-marker { display: none; }
details.more .menu { position: absolute; right: 0; top: 100%; z-index: 10; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 8px 30px rgba(0,0,0,.15); display: grid; padding: 4px; min-width: 170px; }
.add-box { margin-top: 12px; }
.add-box > summary { list-style: none; display: inline-block; }
.add-box > summary::-webkit-details-marker { display: none; }
.add-box[open] > summary { display: none; }
.row-form label, .field .label { display: grid; gap: 5px; font-size: 14px; font-weight: 500; }
.row-form.inline2 { grid-template-columns: 1fr auto; margin-top: 14px; }
.field { margin: 14px 0; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; margin: 6px 0; }
.seg button { border: 0; background: none; padding: 9px 14px; cursor: pointer; }
.seg button.on { background: var(--accent); color: var(--accent-fg); font-weight: 600; }
.check { display: flex !important; align-items: center; gap: 8px; font-size: 14px; }
.check input { width: auto; }
.role { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-top: 10px; }
.role.owner { background: var(--panel-2); }
.role-head { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.role-head .muted { margin-right: auto; }
.perm-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 4px 12px; margin-top: 8px; }
.perm { display: flex; align-items: center; gap: 8px; font-size: 14px; padding: 4px 0; cursor: pointer; }
.perm input { width: 18px; height: 18px; accent-color: var(--accent); }
.perm.soon { color: var(--muted); }
.card.center { text-align: center; max-width: 520px; margin: 40px auto; }

.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; padding: 16px; z-index: 40; }
.modal-card { position: relative; background: var(--panel); color: var(--fg); border-radius: 16px; padding: 22px 20px 18px; width: 100%; max-width: 480px; max-height: 90dvh; overflow-y: auto; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal-card h2 { margin: 0 32px 12px 0; font-size: 19px; }
.modal-x { position: absolute; top: 10px; right: 10px; }
.cred { background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 12px; display: grid; gap: 6px; margin: 10px 0; font-size: 16px; }
.mono { font-family: ui-monospace, "SFMono-Regular", Consolas, monospace; letter-spacing: .5px; }
pre.share { white-space: pre-wrap; background: var(--panel-2); border-radius: 10px; padding: 10px; font: 13px/1.4 system-ui, sans-serif; color: var(--muted); margin: 0; }
.tpl-list { display: grid; gap: 8px; }
.tpl-item { text-align: left; background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; cursor: pointer; }
.tpl-item:hover { border-color: var(--accent); }
.tpl-item div { margin-top: 4px; white-space: pre-wrap; }
.tpl-preview { background: var(--out); border-radius: 12px; padding: 10px 12px; white-space: pre-wrap; margin-bottom: 12px; }
.guide { text-align: center; }
.guide-icon { font-size: 44px; margin: 4px 0 6px; }
.guide h3 { margin: 0 0 8px; font-size: 18px; }
.guide p { color: var(--muted); margin: 0 auto; max-width: 360px; }
.dots-row { display: flex; gap: 6px; justify-content: center; margin: 16px 0 4px; }
.gdot { width: 8px; height: 8px; border-radius: 50%; background: var(--line); }
.gdot.on { background: var(--accent); }

@media (max-width: 900px) {
  .nav-badge { position: absolute; top: 2px; right: 2px; }
  .side-foot .btn span { display: none; }
  #user-name { display: none; }
  .item-list li { flex-wrap: wrap; }
}

/* Selector de tema */
.theme-seg { display: flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.theme-seg button { flex: 1; border: 0; background: none; padding: 6px 0; cursor: pointer; font-size: 15px; }
.theme-seg button.on { background: var(--accent-soft); }
@media (max-width: 900px) { .theme-seg { display: none; } #theme-mini { display: inline-block !important; } }
#theme-mini { display: none !important; }

/* ===== v3: ficha, asignar, horario, actividad ===== */
.note { color: var(--warn-fg); background: var(--warn-bg); border-radius: 6px; padding: 1px 6px; margin-top: 3px; justify-self: start; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#chat-assign { width: auto; max-width: 150px; padding: 6px 8px; }
.auto-box { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin: 10px 0; }
.auto-box p { margin: 4px 0 8px; }
.switch { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.switch input { width: 20px; height: 20px; accent-color: var(--accent); }
.hours-grid { display: grid; gap: 6px; margin-top: 6px; }
.hour-row { display: grid; grid-template-columns: 110px 1fr; align-items: center; gap: 8px; }
.hour-row .times { display: flex; align-items: center; gap: 6px; }
.hour-row input[type=time] { width: auto; padding: 6px 8px; }
.hour-row.closed .times { opacity: .4; pointer-events: none; }
.activity li { font-size: 14px; }
.activity time { color: var(--muted); font-size: 12px; white-space: nowrap; }
.msg .auto { font-size: 11px; color: var(--muted); }
.ficha textarea { min-height: 90px; }
@media (max-width: 900px) { .hide-mobile { display: none; } .chat-head { flex-wrap: wrap; } #chat-assign { max-width: 120px; } }
#hours-card { grid-column: 1 / -1; }
#hours-card .auto-wrap { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 0 16px; }
.hour-row .times { flex-wrap: wrap; }
.hour-row input[type=time] { width: 130px; }
@media (max-width: 520px) { .hour-row { grid-template-columns: 1fr; gap: 2px; } .hour-row input[type=time] { width: 120px; } }
/* Pestañas: que bajen de línea en vez de mostrar barra de desplazamiento */
.tabs { flex-wrap: wrap; overflow: visible; row-gap: 6px; }

/* ===== auditoría ===== */
.net-banner { background: var(--warn-bg); color: var(--warn-fg); padding: 8px 14px; font-size: 14px; text-align: center; border-bottom: 1px solid var(--line); }
.main { display: flex; flex-direction: column; }
.main > section { flex: 1; min-height: 0; }
.messages .older { align-self: center; margin-bottom: 6px; }
.msg .retry { margin-top: 6px; padding: 3px 10px; font-size: 12px; }
