:root {
  --accent: #8B7CFF;
  --on-accent: #fff;
  --font: "Segoe UI Variable Text", "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
}
[data-theme="dark"] {
  --bg: #0D111C; --surface: #141A29; --surface2: #1B2236; --line: #263049;
  --text: #E9ECF5; --muted: #8D96B2; --danger: #FF7A8A; --ok: #4FD6A0;
}
[data-theme="light"] {
  --bg: #F5F6FA; --surface: #FFFFFF; --surface2: #ECEEF5; --line: #DCE0EC;
  --text: #151A2B; --muted: #5D6680; --danger: #C93B4F; --ok: #17936A;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 16px/1.55 var(--font); -webkit-text-size-adjust: 100%; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }

.app { height: 100dvh; display: grid; grid-template-columns: 280px 1fr; }

/* barra lateral */
.sidebar { background: var(--surface); border-right: 1px solid var(--line); display: flex; flex-direction: column; gap: 12px; padding: 16px 12px; padding-top: max(16px, env(safe-area-inset-top)); min-height: 0; }
.brand { display: flex; align-items: center; gap: 10px; font-size: 20px; font-weight: 700; letter-spacing: .04em; padding: 2px 8px 6px; }
.star { display: inline-flex; width: 26px; height: 26px; color: var(--accent); }
.star svg { width: 100%; height: 100%; }
.btn { border: 1px solid var(--line); background: var(--surface2); border-radius: 10px; padding: 9px 14px; font-weight: 600; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; text-align: left; }
.conv-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; min-height: 0; }
.conv { display: flex; align-items: center; gap: 4px; border-radius: 10px; }
.conv.active { background: var(--surface2); box-shadow: inset 3px 0 0 var(--accent); }
.conv:hover { background: var(--surface2); }
.conv-open { flex: 1; min-width: 0; text-align: left; background: none; border: 0; padding: 9px 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-del { background: none; border: 0; color: var(--muted); padding: 6px 9px; border-radius: 8px; }
.conv-del:hover { color: var(--danger); }
.list-empty { color: var(--muted); font-size: 14px; padding: 8px 10px; }

/* área principal */
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; height: 100dvh; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top)); }
.menu-btn { display: none; }
.icon-btn { background: none; border: 0; padding: 8px; border-radius: 10px; color: var(--muted); display: inline-flex; }
.icon-btn:hover { color: var(--text); background: var(--surface2); }
.status { margin-left: auto; display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.status.ok .dot { background: var(--ok); }
.status.bad .dot { background: var(--danger); }

.messages { flex: 1; overflow-y: auto; padding: 8px 16px 24px; min-height: 0; }
.msg { width: min(740px, 100%); margin: 0 auto 22px; display: flex; gap: 12px; }
.msg .body { min-width: 0; overflow-wrap: anywhere; }
.msg.user { justify-content: flex-end; }
.msg.user .body { background: var(--surface2); padding: 10px 15px; border-radius: 18px 18px 4px 18px; max-width: 85%; white-space: pre-wrap; }
.msg.assistant .body { flex: 1; padding-top: 2px; }
.avatar { flex: none; width: 28px; height: 28px; color: var(--accent); margin-top: 1px; }
.avatar svg { width: 100%; height: 100%; }
.msg.thinking .avatar svg { animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 0%,100% { transform: scale(.8) rotate(0); opacity: .6; } 50% { transform: scale(1.1) rotate(45deg); opacity: 1; } }
.body p { margin: 0 0 .7em; }
.body p:last-child { margin-bottom: 0; }
.body ul, .body ol { margin: 0 0 .7em; padding-left: 1.4em; }
.body h4 { margin: .9em 0 .4em; font-size: 1.02em; }
.body code { background: var(--surface2); padding: .1em .35em; border-radius: 6px; font: .9em Consolas, "Cascadia Mono", monospace; }
.body pre { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; overflow-x: auto; margin: 0 0 .8em; }
.body pre code { background: none; padding: 0; }

.empty { margin: auto; text-align: center; max-width: 420px; padding: 8vh 12px 0; color: var(--muted); }
.empty .star { width: 56px; height: 56px; margin-bottom: 14px; }
.empty h1 { margin: 0 0 8px; color: var(--text); font-size: 26px; font-weight: 650; }
.empty p { margin: 0; }
.empty h1, .empty p { text-wrap: balance; }
.empty .nb { white-space: nowrap; }
@media (prefers-reduced-motion: no-preference) {
  .empty .star { animation: greet-in .5s ease both; }
  .empty h1 { animation: greet-in .55s .08s ease both; }
  .empty p { animation: greet-in .6s .2s ease both; }
}
@keyframes greet-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.composer { padding: 8px 16px 16px; padding-bottom: max(16px, env(safe-area-inset-bottom)); }
.composer-box { width: min(740px, 100%); margin: 0 auto; display: flex; align-items: flex-end; gap: 8px; background: var(--surface); border: 1px solid var(--line); border-radius: 22px; padding: 8px 8px 8px 16px; }
.composer-box:focus-within { border-color: var(--accent); }
.attach-bar { width: min(740px, 100%); margin: 0 auto 8px; display: flex; flex-wrap: wrap; gap: 6px; }
.attach-bar[hidden] { display: none; }
.chip { display: flex; align-items: center; gap: 8px; max-width: 100%; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 6px 6px 6px 10px; font-size: 13px; }
.chip-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 200px; color: var(--text); }
.chip small { color: var(--muted); }
.chip.loading small { animation: chip-pulse 1.2s ease-in-out infinite; }
.chip.error { border-color: #d9534f; }
.chip.error small { color: #ff8a85; }
.chip button { background: none; border: 0; color: var(--muted); cursor: pointer; padding: 2px 6px; border-radius: 8px; font-size: 13px; }
.chip button:hover { color: var(--text); }
.send.wait { opacity: .5; }
.thumbs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.thumbs img { max-width: 140px; max-height: 140px; border-radius: 10px; object-fit: cover; }
@keyframes chip-pulse { 50% { opacity: .4; } }
.composer textarea { flex: 1; resize: none; border: 0; background: none; outline: none; max-height: 180px; padding: 6px 0; line-height: 1.45; }
.send { flex: none; width: 38px; height: 38px; border-radius: 50%; border: 0; background: var(--accent); color: var(--on-accent); display: grid; place-items: center; }
.send:disabled { opacity: .45; cursor: default; }

.tool { flex: none; width: 38px; height: 38px; margin-left: -8px; border-radius: 50%; border: 0; background: none; color: var(--muted); display: grid; place-items: center; }
.tool:hover { color: var(--text); background: var(--surface2); }
.tool[aria-pressed="true"] { color: var(--on-accent); background: var(--accent); }
.body a { color: var(--accent); word-break: break-all; }

/* painel de definições */
.panel-scrim, .scrim { position: fixed; inset: 0; background: rgba(5, 8, 16, .55); opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 20; }
.panel-scrim.show, .scrim.show { opacity: 1; pointer-events: auto; }
.panel { position: fixed; top: 0; right: 0; bottom: 0; width: min(430px, 100%); background: var(--surface); border-left: 1px solid var(--line); z-index: 30; display: flex; flex-direction: column; transform: translateX(100%); transition: transform .22s ease; padding-top: env(safe-area-inset-top); }
.panel.open { transform: none; }
.panel-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 6px 20px; }
.panel-head h2 { margin: 0; font-size: 20px; }
.tabs { display: flex; gap: 4px; padding: 6px 14px 0; border-bottom: 1px solid var(--line); overflow-x: auto; }
.tab { background: none; border: 0; padding: 9px 10px; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.tab-body { flex: 1; overflow-y: auto; padding: 18px 20px; display: flex; flex-direction: column; gap: 16px; }
.tab-body[hidden] { display: none; }
.tab-body label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 14px; }
.tab-body small, .hint { font-weight: 400; color: var(--muted); font-size: 13px; margin: 0; }
.tab-body input, .tab-body textarea, .tab-body select { background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; font-weight: 400; font-size: 15px; }
.tab-body input[type=color] { padding: 2px; height: 42px; width: 100%; }
.tab-body textarea { resize: vertical; line-height: 1.45; }
.swatches { display: flex; gap: 8px; flex-wrap: wrap; }
.swatch { width: 30px; height: 30px; border-radius: 50%; border: 2px solid transparent; }
.swatch.sel { border-color: var(--text); }
.mem-add { display: flex; gap: 8px; }
.mem-add input { flex: 1; min-width: 0; }
.mem-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.mem-list li { display: flex; gap: 8px; align-items: flex-start; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 8px 8px 8px 12px; font-size: 15px; }
.mem-list li span { flex: 1; overflow-wrap: anywhere; }
.mem-list button { background: none; border: 0; color: var(--muted); padding: 2px 8px; border-radius: 8px; }
.mem-list button:hover { color: var(--danger); }
.panel-foot { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 20px; padding-bottom: max(12px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
#saveMsg { color: var(--ok); font-size: 14px; }
#saveMsg.err { color: var(--danger); }

@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; top: 0; bottom: 0; left: 0; width: min(300px, 86vw); z-index: 25; transform: translateX(-100%); transition: transform .22s ease; }
  .sidebar.open { transform: none; }
  .menu-btn { display: inline-flex; }
}
@media (prefers-reduced-motion: reduce) {
  .msg.thinking .avatar svg { animation: none; }
  .panel, .sidebar, .scrim, .panel-scrim { transition: none; }
}

a.btn { display: block; text-decoration: none; color: inherit; }

/* conta */
.account { display: flex; flex-direction: column; gap: 8px; padding: 10px 8px 4px; border-top: 1px solid var(--line); }
.acc-row { display: flex; align-items: center; gap: 10px; min-width: 0; }
.acc-pic { width: 34px; height: 34px; border-radius: 50%; flex: none; }
.acc-id { display: flex; flex-direction: column; min-width: 0; }
.acc-id strong, .acc-id small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.acc-id small, .acc-hint { color: var(--muted); font-size: 12.5px; }
.acc-actions { display: flex; gap: 8px; }
.acc-actions .btn { flex: 1; padding: 7px 10px; font-size: 14px; }
.acc-link { background: none; border: 0; color: var(--muted); font-size: 12.5px; text-align: left; padding: 2px 0; text-decoration: underline; }
.acc-link:hover { color: var(--danger); }

/* logo personalizada (imagem em vez da estrela) */
.star img, .avatar img { width: 100%; height: 100%; object-fit: contain; border-radius: 22%; display: block; }
.msg.thinking .avatar img { animation: pulse 1.6s ease-in-out infinite; }

/* editar mensagens enviadas */
.msg.user .edit-btn { order: -1; align-self: center; flex: none; width: 30px; height: 30px; border: 0; border-radius: 50%; background: transparent; color: var(--muted); font-size: 16px; cursor: pointer; opacity: 0; transition: opacity .15s, background .15s; }
.msg.user:hover .edit-btn, .msg.user .edit-btn:focus-visible { opacity: 1; }
.msg.user .edit-btn:hover { background: var(--surface2); color: var(--text); }
@media (hover: none) { .msg.user .edit-btn { opacity: .55; } }
.msg.user.editing .edit-btn { display: none; }
.msg.user.editing .body { width: min(560px, 100%); max-width: 100%; white-space: normal; }
.edit-text { width: 100%; box-sizing: border-box; resize: none; border: 1px solid var(--line); border-radius: 12px; background: var(--bg); color: inherit; font: inherit; padding: 10px 12px; }
.edit-row { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.btn.small { padding: 5px 12px; font-size: 14px; }
