/* Widget do chat-companion — launcher flutuante + painel. JS puro, theme-aware.
   Escopado em .cmp para não colidir com o tema do livro. */

.cmp {
  --cmp-bg: #ffffff; --cmp-fg: #1a2230; --cmp-muted: #5b6675;
  --cmp-line: #e3e8ef; --cmp-accent: #2563eb; --cmp-accent-fg: #ffffff;
  --cmp-user: #eaf1ff; --cmp-bot: #f4f6f9; --cmp-panel-sh: 0 18px 50px rgba(10,20,40,.28);
  position: fixed; right: max(18px, env(safe-area-inset-right)); bottom: max(18px, env(safe-area-inset-bottom));
  z-index: 2147483000; font-family: inherit;
}
@media (prefers-color-scheme: dark) {
  .cmp { --cmp-bg: #131a24; --cmp-fg: #e7edf5; --cmp-muted: #93a1b3;
    --cmp-line: #263141; --cmp-user: #1d2b45; --cmp-bot: #1a222e; }
}
:root[data-theme="dark"] .cmp { --cmp-bg: #131a24; --cmp-fg: #e7edf5; --cmp-muted: #93a1b3;
  --cmp-line: #263141; --cmp-user: #1d2b45; --cmp-bot: #1a222e; }
:root[data-theme="light"] .cmp { --cmp-bg: #ffffff; --cmp-fg: #1a2230; --cmp-muted: #5b6675;
  --cmp-line: #e3e8ef; --cmp-user: #eaf1ff; --cmp-bot: #f4f6f9; }

/* Launcher */
.cmp-launcher {
  width: 58px; height: 58px; border-radius: 50%; border: none; cursor: pointer;
  background: var(--cmp-accent); color: var(--cmp-accent-fg); font-size: 1.5rem; line-height: 1;
  box-shadow: 0 10px 26px rgba(37,99,235,.42); transition: transform .15s ease, box-shadow .15s ease;
  display: grid; place-items: center;
}
.cmp-launcher:hover { transform: translateY(-2px); box-shadow: 0 14px 32px rgba(37,99,235,.5); }
.cmp[data-open="true"] .cmp-launcher { display: none; }

/* Painel */
.cmp-panel {
  display: none; flex-direction: column; width: min(480px, calc(100vw - 32px));
  height: min(78vh, calc(100vh - 96px)); background: var(--cmp-bg); color: var(--cmp-fg);
  border: 1px solid var(--cmp-line); border-radius: 16px; overflow: hidden; box-shadow: var(--cmp-panel-sh);
}
.cmp[data-open="true"] .cmp-panel { display: flex; }

.cmp-head { display: flex; align-items: center; gap: .5rem; padding: .7rem .8rem;
  background: var(--cmp-accent); color: var(--cmp-accent-fg); }
.cmp-title { font-weight: 700; font-size: .95rem; display: flex; align-items: baseline; gap: .45rem; }
.cmp-cap { font-weight: 500; font-size: .72rem; opacity: .85; }
.cmp-actions { margin-left: auto; display: flex; align-items: center; gap: .4rem; }
.cmp-mode { font-size: .72rem; border-radius: 7px; border: none; padding: .25rem .4rem;
  background: rgba(255,255,255,.18); color: #fff; cursor: pointer; }
.cmp-mode option { color: #1a2230; }
.cmp-min { border: none; background: rgba(255,255,255,.18); color: #fff; width: 26px; height: 26px;
  border-radius: 7px; cursor: pointer; font-size: 1rem; line-height: 1; }

/* Capacidades */
.cmp-caps { padding: .55rem .8rem; border-bottom: 1px solid var(--cmp-line); background: var(--cmp-bot); }
.cmp-caps-t { font-size: .7rem; color: var(--cmp-muted); margin: 0 0 .4rem; text-transform: uppercase; letter-spacing: .04em; }
.cmp-chips { display: flex; flex-wrap: wrap; gap: .3rem; }
.cmp-chip { font-size: .72rem; padding: .18rem .5rem; border-radius: 999px; border: 1px solid var(--cmp-line);
  color: var(--cmp-fg); background: var(--cmp-bg); }
.cmp-chip[data-on="true"] { border-color: #23a06133; background: #23a0611a; color: #1f9d5b; font-weight: 600; }
.cmp-chip[data-on="true"]::before { content: "● "; }
.cmp-chip[data-on="false"] { opacity: .55; }
.cmp-chip[data-on="false"]::before { content: "🔒 "; }

/* Mensagens */
.cmp-msgs { flex: 1; overflow-y: auto; padding: .8rem; display: flex; flex-direction: column; gap: .55rem; }
.cmp-msg { max-width: 88%; padding: .5rem .7rem; border-radius: 12px; font-size: .86rem; line-height: 1.45;
  white-space: pre-wrap; word-wrap: break-word; }
.cmp-msg.user { align-self: flex-end; background: var(--cmp-user); border-bottom-right-radius: 4px; }
.cmp-msg.bot { align-self: flex-start; background: var(--cmp-bot); border-bottom-left-radius: 4px; }
.cmp-msg.sys { align-self: center; font-size: .76rem; color: var(--cmp-muted); background: transparent; text-align: center; }
.cmp-msg code { background: rgba(127,127,127,.16); padding: 0 .25em; border-radius: 4px; }
.cmp-src { display: block; margin-top: .35rem; font-size: .7rem; color: var(--cmp-muted); }
.cmp-typing { align-self: flex-start; color: var(--cmp-muted); font-size: .8rem; }

/* Input */
.cmp-form { display: flex; gap: .4rem; padding: .6rem; border-top: 1px solid var(--cmp-line); background: var(--cmp-bg); }
.cmp-input { flex: 1; border: 1px solid var(--cmp-line); border-radius: 10px; padding: .5rem .6rem;
  font: inherit; font-size: .86rem; background: var(--cmp-bg); color: var(--cmp-fg); resize: none; }
.cmp-input:focus { outline: 2px solid var(--cmp-accent); outline-offset: -1px; }
.cmp-send { border: none; background: var(--cmp-accent); color: #fff; border-radius: 10px; width: 42px;
  cursor: pointer; font-size: 1.05rem; }
.cmp-send:disabled { opacity: .5; cursor: default; }

@media (max-width: 560px) {
  .cmp { right: 10px; bottom: 10px; }
  .cmp-panel { width: calc(100vw - 20px); height: min(78vh, calc(100vh - 80px)); }
}

/* Form de sugestão (E05) */
.cmp-sugform[hidden] { display: none; }
.cmp-sugform { display: flex; gap: .4rem; padding: .6rem; border-bottom: 1px solid var(--cmp-line); background: var(--cmp-bot); }

/* BYOK (spec 048) */
.cmp-byok-selo { background: none; border: 1px solid rgba(255,255,255,.35); border-radius: 6px;
  cursor: pointer; font-size: .8rem; padding: 0 .3rem; margin-left: .4rem; }
.cmp-byokform .cmp-input { font-family: monospace; }


/* ===== Spec 053 — estados de layout (float | dock | max) ===== */
.cmp[data-dock="dock"][data-open="true"] .cmp-panel,
.cmp[data-dock="max"][data-open="true"] .cmp-panel {
  position: fixed; top: 0; right: 0; bottom: 0; height: 100vh; height: 100svh;
  width: var(--cmp-dockw, 430px); max-width: 100vw; border-radius: 0; border-right: none;
}
html.cmp-docked body { padding-right: var(--cmp-dockw, 430px); transition: padding-right .18s ease; }
html.cmp-docked #alt-tema { right: calc(var(--cmp-dockw, 430px) + 1rem); }
@media (max-width: 820px) {
  html.cmp-docked body { padding-right: 0; }
  .cmp[data-dock="dock"][data-open="true"] .cmp-panel,
  .cmp[data-dock="max"][data-open="true"] .cmp-panel { width: 100vw; }
}

/* Entrada maior (textarea + linha de dicas) */
.cmp-entrada { flex-direction: column; gap: .35rem; position: relative; }
.cmp-entrada .cmp-input { width: 100%; min-height: 64px; max-height: 220px; }
.cmp-ent-linha { display: flex; align-items: center; gap: .5rem; }
.cmp-ent-dica { flex: 1; font-size: .68rem; color: var(--cmp-muted); }
.cmp-send-rot { width: auto; padding: .4rem .95rem; font-size: .85rem; font-weight: 700; border-radius: 8px; }

/* Paleta de comandos (/) */
.cmp-pal { position: absolute; bottom: calc(100% + .3rem); left: .6rem; right: .6rem;
  background: var(--cmp-bg); border: 1px solid var(--cmp-line); border-radius: 10px; overflow: hidden;
  box-shadow: 0 -10px 30px rgba(0,0,0,.35); z-index: 5; }
.cmp-pal[hidden] { display: none; }
.cmp-pal-item { display: flex; gap: .6rem; padding: .45rem .7rem; font-size: .82rem; cursor: pointer; }
.cmp-pal-item span { color: var(--cmp-muted); }
.cmp-pal-item.sel, .cmp-pal-item:hover { background: var(--cmp-user); }

/* Tooltip de capacidade */
.cmp-chip { position: relative; cursor: help; }
.cmp-tip { position: absolute; left: 0; top: calc(100% + .35rem); width: 230px; z-index: 8;
  background: var(--cmp-bg); border: 1px solid var(--cmp-line); border-radius: 8px;
  padding: .5rem .6rem; font-size: .74rem; line-height: 1.4; color: var(--cmp-fg);
  box-shadow: 0 8px 24px rgba(0,0,0,.35); text-transform: none; letter-spacing: 0; font-weight: 400; white-space: normal; }
.cmp-tip[hidden] { display: none; }
.cmp-tip-st { margin-top: .25rem; color: var(--cmp-muted); }
.cmp-chip:nth-child(n+4) .cmp-tip { left: auto; right: 0; }

/* Barra de status (mini-inspetor) */
.cmp-status { display: flex; align-items: center; gap: .8rem; padding: .32rem .8rem;
  border-top: 1px solid var(--cmp-line); background: var(--cmp-bot); color: var(--cmp-muted);
  font-size: .7rem; cursor: pointer; user-select: none; }
.cmp-status:hover { color: var(--cmp-fg); }
.cmp-status-abrir { margin-left: auto; color: var(--cmp-accent); font-weight: 600; }

/* Painel Bastidores */
.cmp-aux { position: fixed; z-index: 2147482999; background: var(--cmp-bot); color: var(--cmp-fg);
  border: 1px solid var(--cmp-line); display: flex; flex-direction: column; overflow: hidden; }
.cmp-aux[hidden] { display: none; }
.cmp[data-dock="float"] .cmp-aux { right: calc(min(480px, calc(100vw - 32px)) + 28px);
  bottom: max(18px, env(safe-area-inset-bottom)); width: 320px; height: min(78vh, calc(100vh - 96px));
  border-radius: 14px; box-shadow: var(--cmp-panel-sh); }
.cmp[data-dock="dock"] .cmp-aux, .cmp[data-dock="max"] .cmp-aux {
  top: 0; bottom: 0; right: var(--cmp-dockw, 430px); width: 320px; border-radius: 0; }
.cmp-aux-tabs { display: flex; align-items: center; border-bottom: 1px solid var(--cmp-line); }
.cmp-aux-tabs span { flex: 1; text-align: center; padding: .5rem .3rem; font-size: .76rem;
  color: var(--cmp-muted); cursor: pointer; }
.cmp-aux-tabs span.on { color: var(--cmp-fg); box-shadow: inset 0 -2px 0 var(--cmp-accent); font-weight: 600; }
.cmp-aux-x { border: none; background: none; color: var(--cmp-muted); font-size: 1.1rem; cursor: pointer; padding: 0 .6rem; }
.cmp-aux-corpo { flex: 1; overflow-y: auto; padding: .7rem; display: flex; flex-direction: column; gap: .6rem; font-size: .78rem; }
.cmp-bloco { background: var(--cmp-bg); border: 1px solid var(--cmp-line); border-radius: 9px; padding: .55rem .65rem; }
.cmp-bloco-t { font-size: .64rem; letter-spacing: .08em; text-transform: uppercase; color: var(--cmp-accent); margin-bottom: .3rem; font-weight: 700; }
.cmp-kv { display: flex; justify-content: space-between; gap: .6rem; padding: .12rem 0; color: var(--cmp-muted); }
.cmp-kv b { color: var(--cmp-fg); font-weight: 600; text-align: right; }
.cmp-barra { height: 7px; border-radius: 4px; background: rgba(127,127,127,.18); overflow: hidden; margin: .25rem 0 .35rem; }
.cmp-barra i { display: block; height: 100%; background: linear-gradient(90deg, #6fd08a, #e0a24a); }
.cmp-trecho { color: var(--cmp-muted); margin-top: .3rem; line-height: 1.35; }
.cmp-aux-vazio { color: var(--cmp-muted); font-style: italic; }
.cmp-doc { display: block; color: var(--cmp-accent); text-decoration: none; padding: .2rem 0; }
.cmp-doc:hover { text-decoration: underline; }
@media (max-width: 820px) {
  .cmp .cmp-aux { inset: 0 !important; width: auto !important; height: auto !important; border-radius: 0 !important; }
}


.cmp-form[hidden], .cmp-status[hidden] { display: none !important; }

/* ===== Spec 054 — consentimento, tour e plano ===== */
.cmp-banner { position: fixed; left: 50%; transform: translateX(-50%); bottom: 14px; z-index: 2147483001;
  display: flex; align-items: center; gap: .8rem; max-width: min(680px, calc(100vw - 24px));
  background: var(--cmp-bg, #131a24); color: var(--cmp-fg, #e7edf5); border: 1px solid var(--cmp-line, #263141);
  border-radius: 12px; padding: .6rem .9rem; font-size: .8rem; line-height: 1.4;
  box-shadow: 0 14px 40px rgba(0,0,0,.4); }
.cmp-banner-bt { border: none; background: var(--cmp-accent, #2563eb); color: #fff; border-radius: 8px;
  padding: .45rem .8rem; font-weight: 700; cursor: pointer; white-space: nowrap; }

.cmp-consent { padding: .9rem; border-top: 1px solid var(--cmp-line); background: var(--cmp-bot); font-size: .82rem; }
.cmp-consent[hidden] { display: none; }
.cmp-consent-t { font-weight: 700; margin-bottom: .3rem; }
.cmp-consent p { margin: 0 0 .6rem; color: var(--cmp-muted); }

.cmp-tour-ov { position: fixed; inset: 0; z-index: 2147483002; background: transparent; }
.cmp-tour-foco { position: fixed; border-radius: 10px; box-shadow: 0 0 0 9999px rgba(0,0,0,.55);
  border: 2px solid var(--cmp-accent, #2563eb); pointer-events: none; transition: all .2s ease; }
.cmp-tour-ov:empty { background: rgba(0,0,0,.55); }
.cmp-tour-card { position: fixed; z-index: 2147483003; width: 300px; background: var(--cmp-bg, #131a24);
  color: var(--cmp-fg, #e7edf5); border: 1px solid var(--cmp-line, #263141); border-radius: 12px;
  padding: .8rem .9rem; font-size: .84rem; box-shadow: 0 16px 44px rgba(0,0,0,.5); }
.cmp-tour-card p { margin: .3rem 0 .6rem; color: var(--cmp-muted, #93a1b3); line-height: 1.45; }
.cmp-tour-n { font-size: .68rem; color: var(--cmp-muted, #93a1b3); margin-bottom: .2rem; }
.cmp-tour-bts { display: flex; align-items: center; justify-content: space-between; }
.cmp-tour-skip { border: none; background: none; color: var(--cmp-muted, #93a1b3); cursor: pointer; font-size: .76rem; text-decoration: underline; }

/* ===== Spec 080 — conta de leitura (link mágico) ===== */
/* Convite discreto por contrato: uma linha no rodapé do painel, peso de nota de
   rodapé, dispensável. Nunca modal, nunca interstitial. */
.cmp-conta { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap;
  padding: .4rem .9rem .6rem; border-top: 1px solid var(--cmp-line); background: var(--cmp-bot);
  font-size: .72rem; color: var(--cmp-muted); }
.cmp-conta[hidden] { display: none; }
.cmp-conta-e { font-weight: 600; color: var(--cmp-fg); overflow-wrap: anywhere; }
.cmp-conta-bt { border: none; background: none; color: var(--cmp-muted); cursor: pointer;
  font: inherit; text-decoration: underline; padding: 0; }
.cmp-conta-bt:hover { color: var(--cmp-fg); }
.cmp-conta-conv { text-decoration: none; border: 1px dashed var(--cmp-line); border-radius: 999px;
  padding: .22rem .7rem; }
.cmp-conta-conv:hover { border-color: var(--cmp-accent, #2563eb); }
.cmp-conta-perigo:hover { color: #e06c6c; }
.cmp-assinarform[hidden] { display: none; }

/* Convite no cartão "Retomar" do sumário — onde a perda de progresso dói. */
.cmp-conv-retomar { margin: -.4rem 0 1.2rem; font-size: .82rem; color: var(--muted); }
.cmp-conv-link { border: none; background: none; padding: 0; font: inherit;
  color: inherit; text-decoration: underline; cursor: pointer; }
.cmp-conv-link:hover { color: var(--texto); }
