/* ==========================================================================
   Smart21brain AI — full-page chat (ChatGPT / Gemini / Claude style)
   ========================================================================== */
:root {
  --bg: #f7faf8; --panel: #ffffff; --side: #eef4f0; --text: #1b2620; --soft: #5f6b65; --line: #e0e8e3;
  --hover: rgba(11,110,79,.08); --user: #e4f3ec; --brand: #0B6E4F; --brand-2: #7c5cff; --on-brand: #fff;
  --code-bg: #0d1512; --code-bar: #16231d; --shadow: 0 10px 40px rgba(16,40,28,.12);
  --font: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif; --display: 'Poppins', var(--font);
  --side-w: 288px; --col: 780px;
}
[data-theme="dark"] {
  --bg: #0e1411; --panel: #161e1a; --side: #0a0f0c; --text: #e9efeb; --soft: #94a39b; --line: #25312b;
  --hover: rgba(255,255,255,.06); --user: #1f2d26; --brand: #2fbf8a; --brand-2: #9a82ff; --on-brand: #04130c;
  --shadow: 0 14px 50px rgba(0,0,0,.55);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font-family: var(--font); background: var(--bg); color: var(--text); -webkit-font-smoothing: antialiased; overflow: hidden; }
button { font-family: inherit; color: inherit; cursor: pointer; }
a { color: inherit; text-decoration: none; }
.skip { position: absolute; left: -999px; top: 0; background: var(--brand); color: var(--on-brand); padding: .5rem 1rem; z-index: 99; }
.skip:focus { left: 8px; top: 8px; }
.only-mobile { display: none; }

.app { display: flex; height: 100dvh; }

/* ---------------- sidebar ---------------- */
.side { width: var(--side-w); background: var(--side); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: .8rem; gap: .7rem; transition: margin-left .25s ease, transform .25s ease; flex-shrink: 0; }
.app.side-hidden .side { margin-left: calc(var(--side-w) * -1); }
.side-top { display: flex; align-items: center; justify-content: space-between; padding: .2rem .3rem; }
.brand { display: inline-flex; align-items: center; gap: .55rem; font-family: var(--display); font-weight: 700; font-size: 1.05rem; }
.brand img { border-radius: 50%; }
.brand em { font-style: normal; color: var(--brand); }
.brand b { font-weight: 800; background: linear-gradient(90deg, var(--brand-2), var(--brand)); -webkit-background-clip: text; background-clip: text; color: transparent; }
.icon-btn { background: transparent; border: 0; width: 38px; height: 38px; border-radius: 10px; display: inline-grid; place-items: center; color: var(--soft); font-size: .95rem; transition: background .15s, color .15s; }
.icon-btn:hover { background: var(--hover); color: var(--text); }
.new-chat { display: flex; align-items: center; gap: .6rem; width: 100%; padding: .7rem .9rem; border-radius: 12px; border: 1px solid var(--line); background: var(--panel); font-weight: 600; font-size: .92rem; transition: border-color .15s, transform .15s; }
.new-chat:hover { border-color: var(--brand); }
.new-chat i { color: var(--brand); }
.search-wrap { position: relative; }
.search-wrap i { position: absolute; left: .85rem; top: 50%; transform: translateY(-50%); color: var(--soft); font-size: .8rem; }
.search-wrap input { width: 100%; padding: .55rem .8rem .55rem 2.1rem; border-radius: 10px; border: 1px solid var(--line); background: transparent; color: var(--text); font-size: .86rem; outline: none; font-family: inherit; }
.search-wrap input:focus { border-color: var(--brand); }
.history { flex: 1; overflow-y: auto; margin: 0 -.3rem; padding: 0 .3rem; scrollbar-width: thin; }
.h-group { font-size: .7rem; text-transform: uppercase; letter-spacing: .06em; color: var(--soft); margin: .9rem .6rem .35rem; font-weight: 700; }
.h-item { display: flex; align-items: center; gap: .3rem; border-radius: 10px; padding: 0 .3rem 0 .7rem; }
.h-item:hover, .h-item.active { background: var(--hover); }
.h-item.active { background: color-mix(in srgb, var(--brand) 14%, transparent); }
.h-title { flex: 1; text-align: left; background: none; border: 0; padding: .55rem 0; font-size: .87rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.h-del { opacity: 0; width: 28px; height: 28px; font-size: .75rem; }
.h-item:hover .h-del, .h-item:focus-within .h-del { opacity: 1; }
.h-empty { color: var(--soft); font-size: .84rem; padding: 1rem .7rem; }
.side-bottom { border-top: 1px solid var(--line); padding-top: .6rem; display: flex; flex-direction: column; gap: .1rem; }
.side-bottom a, .link-btn { display: flex; align-items: center; gap: .65rem; padding: .5rem .6rem; border-radius: 9px; font-size: .86rem; color: var(--soft); background: none; border: 0; text-align: left; }
.side-bottom a:hover, .link-btn:hover { background: var(--hover); color: var(--text); }
.side-bottom i { width: 1.1rem; text-align: center; }
.scrim { display: none; }

/* ---------------- main ---------------- */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; background: var(--bg); }
.top { display: flex; align-items: center; gap: .3rem; padding: .55rem .8rem; }
.model-chip { display: inline-flex; align-items: center; gap: .5rem; padding: .4rem .85rem; border-radius: 999px; font-weight: 600; font-size: .9rem; border: 1px solid var(--line); background: var(--panel); }
.model-chip i { color: var(--brand-2); }
.top-actions { margin-left: auto; display: flex; gap: .1rem; }
.stage { flex: 1; overflow-y: auto; scroll-behavior: smooth; padding: 0 1rem; }

/* welcome */
.welcome { max-width: var(--col); margin: 0 auto; padding: min(9vh, 4.5rem) 0 1rem; text-align: center; animation: rise .5s ease both; }
.w-logo { border-radius: 50%; box-shadow: 0 8px 30px color-mix(in srgb, var(--brand-2) 40%, transparent); }
.welcome h1 { font-family: var(--display); font-weight: 700; font-size: clamp(1.7rem, 4.6vw, 2.5rem); line-height: 1.2; margin: 1rem 0 .5rem; }
.grad { background: linear-gradient(90deg, var(--brand-2), var(--brand), #f5b942); -webkit-background-clip: text; background-clip: text; color: transparent; }
.w-sub { color: var(--soft); margin: 0 0 1.8rem; font-size: 1rem; }
.cards { display: grid; grid-template-columns: repeat(2, 1fr); gap: .7rem; text-align: left; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: .9rem 1rem; display: flex; gap: .8rem; align-items: flex-start; transition: transform .15s, border-color .15s, box-shadow .15s; }
.card:hover { transform: translateY(-2px); border-color: var(--brand); box-shadow: var(--shadow); }
.card i { width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center; flex-shrink: 0; font-size: .95rem; color: #fff; }
.card b { display: block; font-size: .92rem; margin-bottom: .1rem; }
.card span { font-size: .82rem; color: var(--soft); line-height: 1.4; }

/* thread */
.thread { max-width: var(--col); margin: 0 auto; padding: 1rem 0 1.5rem; display: flex; flex-direction: column; gap: 1.6rem; }
.app.has-chat .welcome { display: none; }
.app:not(.has-chat) .thread { display: none; }
.msg { display: flex; gap: .8rem; animation: rise .3s ease both; }
.msg.user { justify-content: flex-end; }
.avatar { width: 32px; height: 32px; border-radius: 50%; flex-shrink: 0; object-fit: cover; margin-top: .1rem; }
.u-wrap { max-width: min(85%, 620px); display: flex; flex-direction: column; align-items: flex-end; gap: .2rem; }
.u-bubble { background: var(--user); padding: .7rem 1rem; border-radius: 20px 20px 6px 20px; white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.55; }
.u-tools { opacity: 0; transition: opacity .15s; }
.msg.user:hover .u-tools, .u-tools:focus-within { opacity: 1; }
.u-edit textarea { width: min(560px, 80vw); min-height: 90px; background: var(--panel); color: var(--text); border: 1px solid var(--brand); border-radius: 14px; padding: .7rem .9rem; font: inherit; resize: vertical; outline: none; }
.u-edit .row { display: flex; gap: .5rem; justify-content: flex-end; margin-top: .5rem; }
.pill-btn { border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: .35rem .9rem; font-size: .84rem; font-weight: 600; }
.pill-btn.primary { background: var(--brand); color: var(--on-brand); border-color: var(--brand); }
.ai-body { flex: 1; min-width: 0; line-height: 1.7; font-size: .97rem; }

/* markdown inside AI replies */
.md > :first-child { margin-top: 0; } .md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 .85rem; }
.md h3, .md h4, .md h5, .md h6 { font-family: var(--display); font-weight: 700; margin: 1.4rem 0 .5rem; line-height: 1.3; }
.md h3 { font-size: 1.25rem; } .md h4 { font-size: 1.1rem; } .md h5, .md h6 { font-size: 1rem; }
.md ul, .md ol { margin: 0 0 .9rem; padding-left: 1.5rem; } .md li { margin: .25rem 0; } .md li > ul, .md li > ol { margin: .25rem 0; }
.md li::marker { color: var(--brand); }
.md a { color: var(--brand); text-decoration: underline; text-underline-offset: 2px; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 1.2rem 0; }
.md blockquote { margin: 0 0 .9rem; padding: .5rem 1rem; border-left: 3px solid var(--brand-2); background: color-mix(in srgb, var(--brand-2) 10%, transparent); border-radius: 0 10px 10px 0; }
.md :not(pre) > code { background: color-mix(in srgb, var(--brand) 14%, transparent); padding: .12rem .4rem; border-radius: 6px; font-size: .88em; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.ai-table { overflow-x: auto; margin: 0 0 1rem; border: 1px solid var(--line); border-radius: 12px; }
.ai-table table { border-collapse: collapse; width: 100%; font-size: .9rem; }
.ai-table th, .ai-table td { padding: .55rem .85rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.ai-table th { background: color-mix(in srgb, var(--brand) 10%, transparent); font-weight: 700; white-space: nowrap; }
.ai-table tr:last-child td { border-bottom: 0; }
.ai-code { margin: .8rem 0 1rem; border-radius: 14px; overflow: hidden; background: var(--code-bg); border: 1px solid rgba(255,255,255,.1); }
.ai-code-bar { display: flex; align-items: center; justify-content: space-between; gap: .5rem; padding: .45rem .9rem; background: var(--code-bar); font-size: .76rem; color: #a9c7b9; }
.ai-code-actions { display: inline-flex; gap: .3rem; }
.ai-code-actions button { background: transparent; color: #cfe8dc; border: 0; border-radius: 7px; padding: .22rem .6rem; font-size: .76rem; }
.ai-code-actions button:hover { background: rgba(255,255,255,.14); color: #fff; }
.ai-code pre { margin: 0; padding: .9rem 1.1rem; overflow-x: auto; background: transparent; }
.ai-code pre code { background: none; padding: 0; color: #e6f3ec; font-size: .86rem; line-height: 1.6; white-space: pre; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.hl-k { color: #ff8fb1; } .hl-s { color: #a5e075; } .hl-c { color: #7f9a8c; font-style: italic; } .hl-n { color: #ffcb6b; }
.hl-f { color: #82cfff; } .hl-t { color: #ff8fb1; } .hl-y { color: #c8a6ff; }

.cursor { display: inline-block; width: .5em; height: 1.1em; margin-left: 2px; vertical-align: text-bottom; background: var(--brand-2); border-radius: 2px; animation: blink 1s steps(2, start) infinite; }
@keyframes blink { to { visibility: hidden; } }
.dots { display: inline-flex; gap: 5px; padding: .5rem 0; }
.dots i { width: 8px; height: 8px; border-radius: 50%; background: linear-gradient(135deg, var(--brand-2), var(--brand)); animation: bounce 1.1s infinite ease-in-out; }
.dots i:nth-child(2) { animation-delay: .15s; } .dots i:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%, 60%, 100% { transform: translateY(0); opacity: .45; } 30% { transform: translateY(-6px); opacity: 1; } }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.actions { display: flex; gap: .1rem; margin-top: .6rem; flex-wrap: wrap; }
.actions .icon-btn { width: 32px; height: 32px; font-size: .82rem; }
.actions .icon-btn.on { color: var(--brand); }
.err { color: #ff6b81; }

.to-bottom { position: absolute; left: 50%; bottom: 8.2rem; transform: translateX(-50%) scale(.9); width: 38px; height: 38px; border-radius: 50%; border: 1px solid var(--line); background: var(--panel); box-shadow: var(--shadow); opacity: 0; pointer-events: none; transition: all .2s; z-index: 5; }
.to-bottom.show { opacity: 1; pointer-events: auto; transform: translateX(-50%) scale(1); }

/* ---------------- composer ---------------- */
.composer-wrap { padding: .3rem 1rem calc(.6rem + env(safe-area-inset-bottom, 0px)); }
.modes, .composer, .fine { max-width: var(--col); margin-left: auto; margin-right: auto; }
.modes { display: flex; gap: .4rem; overflow-x: auto; min-width: 0; scrollbar-width: none; margin: 0 auto .55rem; padding: 2px; }
.modes::-webkit-scrollbar { display: none; }
.modes button { flex: 0 0 auto; display: inline-flex; align-items: center; gap: .4rem; border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: .32rem .7rem; font-size: .8rem; font-weight: 600; color: var(--soft); transition: all .15s; }
.modes button:hover { color: var(--text); background: var(--hover); }
.modes button.on { background: color-mix(in srgb, var(--brand) 16%, var(--panel)); color: var(--text); border-color: color-mix(in srgb, var(--brand) 55%, transparent); }
.composer { background: var(--panel); border: 1px solid var(--line); border-radius: 24px; padding: .7rem .8rem .55rem 1.1rem; box-shadow: var(--shadow); transition: border-color .15s; }
.composer:focus-within { border-color: var(--brand); }
.composer textarea { width: 100%; border: 0; outline: 0; background: transparent; color: var(--text); font: inherit; font-size: 1rem; resize: none; max-height: 200px; line-height: 1.5; padding: .2rem 0; }
.composer textarea::placeholder { color: var(--soft); }
.c-row { display: flex; align-items: center; justify-content: space-between; gap: .5rem; }
.c-btns { display: inline-flex; align-items: center; gap: .3rem; }
.icon-btn.rec { color: #ff4d6d; animation: blink 1s steps(2, start) infinite; }
.send { width: 38px; height: 38px; border-radius: 50%; border: 0; background: var(--text); color: var(--bg); display: grid; place-items: center; transition: transform .15s, opacity .15s; }
.send:hover:not(:disabled) { transform: scale(1.06); }
.send:disabled { opacity: .3; cursor: not-allowed; }
.send.stop { background: var(--brand); color: var(--on-brand); }
.fine { text-align: center; font-size: .74rem; color: var(--soft); margin: .5rem auto 0; }

/* modal + toast */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.6); z-index: 50; display: grid; place-items: center; padding: 1rem; }
.modal[hidden] { display: none; }
.modal-box { background: var(--panel); border-radius: 16px; width: min(900px, 100%); height: min(80dvh, 640px); display: flex; flex-direction: column; overflow: hidden; border: 1px solid var(--line); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: .4rem .6rem .4rem 1rem; border-bottom: 1px solid var(--line); }
.modal-box iframe { flex: 1; border: 0; background: #fff; }
.toast { position: fixed; left: 50%; bottom: 1.5rem; transform: translate(-50%, 20px); background: var(--text); color: var(--bg); padding: .55rem 1rem; border-radius: 999px; font-size: .86rem; opacity: 0; pointer-events: none; transition: all .25s; z-index: 60; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------------- responsive ---------------- */
@media (max-width: 860px) {
  .only-mobile { display: inline-grid; } .only-desktop { display: none; }
  .side { position: fixed; z-index: 40; inset: 0 auto 0 0; width: min(86vw, 320px); transform: translateX(-100%); margin-left: 0 !important; box-shadow: var(--shadow); }
  .app.side-open .side { transform: none; }
  .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 30; opacity: 0; pointer-events: none; transition: opacity .2s; }
  .app.side-open .scrim { opacity: 1; pointer-events: auto; }
  .cards { grid-template-columns: 1fr; }
  .avatar { width: 28px; height: 28px; }
  .msg { gap: .6rem; }
  .u-wrap { max-width: 90%; }
  .to-bottom { bottom: 7.8rem; }
}
@media (min-width: 861px) { #sideOpen { display: none; } }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; scroll-behavior: auto !important; } }

/* code block: Preview | Code | Smart21Editor switch */
.ai-code-lang { text-transform: lowercase; }
.ai-code-actions { align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.ai-seg { display: inline-flex; padding: 2px; gap: 2px; border-radius: 9px; background: rgba(255,255,255,.08); }
.ai-seg button { white-space: nowrap; }
.ai-seg button.on { background: rgba(255,255,255,.2); color: #fff; }
.ai-seg button[data-ai-editor] { color: #82cfff; }
.ai-code-prev { background: #fff; border-top: 1px solid rgba(255,255,255,.1); }
.ai-code-prev iframe { display: block; width: 100%; height: min(420px, 60vh); border: 0; background: #fff; }
.ai-code pre[hidden], .ai-code-prev[hidden] { display: none; }
@media (max-width: 520px) { .ai-code-bar { flex-wrap: wrap; } .ai-code-actions { width: 100%; justify-content: space-between; } }

.modes.modes-in { margin: 0; max-width: none; padding: 0; overflow: visible; }
.modes.modes-in button { border-color: transparent; background: transparent; }
.modes.modes-in button.on { border-color: color-mix(in srgb, var(--brand) 55%, transparent); background: color-mix(in srgb, var(--brand) 16%, var(--panel)); }

.ai-code-note { padding: .9rem 1rem; font-size: .85rem; color: #333; background: #f3f6f4; }

/* ---------------- attachments, pinned + rename (used by ai-attach.js / ai-page.js) ---------------- */
.att-row { display: flex; flex-wrap: wrap; gap: .5rem; padding: 0 0 .5rem; }
.att-row[hidden] { display: none; }
.att-chip { position: relative; display: inline-flex; align-items: center; gap: .45rem; border: 1px solid var(--line); background: var(--bg); border-radius: 12px; padding: .3rem .6rem; font-size: .8rem; max-width: 220px; }
.att-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att-chip i.fa-solid { color: var(--brand); }
.att-img { padding: 0; width: 64px; height: 64px; overflow: hidden; }
.att-img img { width: 100%; height: 100%; object-fit: cover; display: block; }
.att-x { position: absolute; top: -6px; right: -6px; width: 20px; height: 20px; border-radius: 50%; border: 1px solid var(--line); background: var(--panel); color: var(--text); font-size: .6rem; display: grid; place-items: center; padding: 0; }
.att-file .att-x { position: static; width: 20px; height: 20px; flex-shrink: 0; }
.att-spin { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--line); border-top-color: var(--brand); animation: spin .8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
.u-atts { display: flex; flex-wrap: wrap; gap: .4rem; justify-content: flex-end; }
.u-thumb { width: 96px; height: 96px; object-fit: cover; border-radius: 14px; border: 1px solid var(--line); }
.u-file { display: inline-flex; align-items: center; gap: .4rem; border: 1px solid var(--line); background: var(--panel); border-radius: 12px; padding: .35rem .7rem; font-size: .82rem; }
.u-file i { color: var(--brand); }
.c-tools { display: inline-flex; align-items: center; gap: .1rem; }
.app.drag .composer { border-color: var(--brand); box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 25%, transparent); }
.h-item.pinned .h-title i { color: var(--brand); margin-right: .35rem; font-size: .7rem; }
.h-act { opacity: 0; width: 28px; height: 28px; font-size: .72rem; }
.h-item:hover .h-act, .h-item:focus-within .h-act { opacity: 1; }
.h-ren { flex: 1; min-width: 0; margin: .3rem 0; padding: .35rem .5rem; border-radius: 8px; border: 1px solid var(--brand); background: var(--panel); color: var(--text); font: inherit; font-size: .86rem; outline: none; }
@media (hover: none) { .h-act, .h-del { opacity: 1; } }
