/* =========================================================================
   Chat: welcome screen, thread, messages, composer, voice recorder, image cards, errors
   ========================================================================= */
.chat { flex: 1; min-height: 0; display: flex; flex-direction: column; position: relative; }
.thread { flex: 1; min-height: 0; overflow-y: auto; overflow-x: hidden; overscroll-behavior: contain; scroll-behavior: smooth; padding: var(--s4) var(--s4) var(--s5); }
.thread-inner { width: min(var(--thread-w), 100%); margin-inline: auto; display: flex; flex-direction: column; gap: var(--s5); }
/* empty chat: the greeting and the message box are centered together */
.chat.empty { justify-content: center; }
.chat.empty .thread { flex: 0 0 auto; overflow: visible; padding-bottom: 0; }
.chat.empty .composer-wrap { padding-top: var(--s5); }
.chat.empty .composer-hint { display: none; }
.to-bottom { position: absolute; inset-inline: 0; bottom: 132px; margin-inline: auto; width: 38px; height: 38px; display: grid; place-items: center; border: 1px solid var(--line-strong);
  border-radius: 50%; background: var(--surface); color: var(--muted); box-shadow: var(--shadow); z-index: 6; animation: pop var(--fast) var(--ease); }
.to-bottom:hover { color: var(--ink); background: var(--surface-2); }
.icon-btn.playing { color: var(--accent); background: var(--accent-soft-2); }
.icon-btn:disabled { opacity: .45; cursor: not-allowed; }

/* ---------- welcome ---------- */
.welcome { display: grid; justify-items: center; gap: var(--s4); text-align: center; animation: rise .5s var(--ease) both; padding-top: var(--s5); }
.welcome .mark { position: relative; width: 84px; height: 84px; margin-bottom: var(--s2); display: grid; place-items: center; }
.welcome .mark::before { content: ""; position: absolute; inset: -22px; border-radius: 50%;
  background: radial-gradient(closest-side, color-mix(in srgb, var(--accent) 22%, transparent), transparent 72%); filter: blur(8px); animation: pulse 6s ease-in-out infinite; }
.welcome .mark img { position: relative; width: 84px; height: 84px; border-radius: 24px; box-shadow: 0 0 0 1px var(--line), 0 16px 40px rgba(0, 0, 0, .28); }
.welcome h2 { font-size: clamp(24px, 4vw, 32px); font-weight: 600; letter-spacing: -.02em; line-height: 1.35; }
.welcome h2 .name { background: var(--grad); -webkit-background-clip: text; background-clip: text; color: transparent; }

/* ---------- messages ---------- */
.msg { display: flex; gap: var(--s3); animation: rise .28s var(--ease) both; min-width: 0; }
.msg.user { justify-content: flex-end; flex-direction: column; align-items: flex-end; gap: 6px; }
.msg.user .bubble { max-width: min(88%, 620px); padding: 10px 16px; background: var(--surface-2); border: 1px solid var(--line);
  border-radius: var(--r-xl); border-end-end-radius: 8px; white-space: pre-wrap; word-break: break-word; overflow-wrap: anywhere; }
.msg.user .thumbs { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; max-width: min(88%, 620px); }
.msg.user .thumbs img { max-height: 190px; max-width: 100%; border-radius: var(--r-lg); border: 1px solid var(--line); object-fit: cover; cursor: zoom-in; }
.msg.user .file-card { max-width: min(88%, 420px); }
.msg.assistant { align-items: flex-start; }
.msg.assistant > .mark { flex: none; width: 32px; height: 32px; border-radius: 10px; overflow: hidden; margin-top: 2px; box-shadow: 0 0 0 1px var(--line); }
.msg.assistant > .mark img { width: 100%; height: 100%; display: block; }
.msg .body { flex: 1; min-width: 0; }
.msg-actions { display: flex; align-items: center; gap: 2px; margin-top: var(--s2); margin-inline-start: -8px; flex-wrap: wrap; opacity: 0; transition: opacity var(--fast); }
.msg:hover .msg-actions, .msg:focus-within .msg-actions, .msg:last-child .msg-actions, .msg.streaming .msg-actions { opacity: 1; }
@media (hover: none) { .msg-actions { opacity: 1; } }
.msg-actions .icon-btn { width: 34px; height: 34px; }
.msg-actions .icon-btn.ok { color: var(--ok); }
.msg-actions .sep { width: 1px; height: 16px; background: var(--line); margin-inline: 6px; }
.chip-btn { display: inline-flex; align-items: center; gap: 6px; min-height: 32px; padding: 0 10px; border: 1px solid var(--line); border-radius: var(--r-full); background: var(--surface);
  color: var(--muted); font-size: 13px; font-weight: 600; text-decoration: none; transition: background var(--fast), color var(--fast), border-color var(--fast); }
.chip-btn:hover { background: var(--hover); color: var(--ink); border-color: var(--line-strong); }
.msg.user .msg-actions { margin-inline-start: 0; margin-inline-end: -4px; justify-content: flex-end; }

/* status line while the model works */
.status { display: inline-flex; align-items: center; gap: 10px; color: var(--muted); font-size: 14.5px; }
.dots { display: inline-flex; gap: 4px; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); animation: pulse 1.2s ease-in-out infinite; }
.dots i:nth-child(2) { animation-delay: .15s; } .dots i:nth-child(3) { animation-delay: .3s; }
.note-line { color: var(--muted); font-size: 13.5px; margin-top: var(--s2); display: flex; align-items: center; gap: 6px; }

/* rendered Markdown */
.md { overflow-wrap: anywhere; word-break: break-word; }
.md > :first-child { margin-top: 0; } .md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 .85em; }
.md h2, .md h3, .md h4, .md h5, .md h6 { margin: 1.3em 0 .5em; font-weight: 700; }
.md h2 { font-size: 1.28em; } .md h3 { font-size: 1.14em; } .md h4, .md h5, .md h6 { font-size: 1.04em; }
.md ul, .md ol { margin: 0 0 .85em; padding-inline-start: 1.5em; }
.md li { margin: .25em 0; } .md li::marker { color: var(--accent); }
.md blockquote { margin: 0 0 .85em; padding: 4px var(--s4); border-inline-start: 3px solid var(--line-strong); background: var(--hover); border-radius: 0 var(--r-sm) var(--r-sm) 0; color: var(--muted); }
[dir="rtl"] .md blockquote { border-radius: var(--r-sm) 0 0 var(--r-sm); }
.md hr { border: 0; height: 1px; background: var(--line); margin: 1.4em 0; }
.md code { font: .9em/1.5 var(--mono); padding: .12em .42em; border-radius: 6px; background: var(--code-bg); direction: ltr; unicode-bidi: embed; }
.md a { word-break: break-all; }
.md .table-wrap { overflow-x: auto; margin: 0 0 1em; border: 1px solid var(--line); border-radius: var(--r-md); }
.md table { border-collapse: collapse; width: 100%; font-size: .94em; }
.md th, .md td { padding: 8px 12px; text-align: start; border-bottom: 1px solid var(--line); }
.md th { background: var(--surface-2); font-weight: 700; white-space: nowrap; }
.md tr:last-child td { border-bottom: 0; }
.code { margin: 0 0 1em; border-radius: var(--r-lg); overflow: hidden; background: var(--code-block); color: var(--code-ink); border: 1px solid var(--line); direction: ltr; text-align: left; }
.code-head { display: flex; align-items: center; justify-content: space-between; gap: var(--s2); padding: 6px 8px 6px 14px; background: rgba(255, 255, 255, .06); font: 600 12.5px var(--mono); color: #B4B4C0; }
.code-head .copy { display: inline-flex; align-items: center; gap: 6px; min-height: 28px; padding: 0 10px; border: 0; border-radius: 8px; background: transparent; color: inherit; font: 600 12.5px var(--font); }
.code-head .copy:hover { background: rgba(255, 255, 255, .1); color: #fff; }
.code pre { margin: 0; padding: 14px 16px; overflow-x: auto; font: 13.5px/1.65 var(--mono); }
.code code { padding: 0; background: none; font: inherit; }
.tk-k { color: #8AB4FF; } .tk-s { color: #8EE2B0; } .tk-c { color: #80808F; font-style: italic; } .tk-n { color: #FFB86B; } .tk-f { color: #C8A6FF; } .tk-l { color: #FF9AC1; }

/* sources / places / link preview */
.sources { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: var(--s2); margin-top: var(--s3); }
.source { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; padding: 10px 12px; border: 1px solid var(--line); border-radius: var(--r-md); background: var(--surface);
  color: var(--ink); text-decoration: none; transition: border-color var(--fast), background var(--fast); }
.source:hover { border-color: var(--line-strong); background: var(--surface-2); }
.source .fav { grid-row: span 2; display: grid; place-items: center; width: 30px; height: 30px; border-radius: 8px; background: var(--accent-soft-2); color: var(--accent-ink); font-weight: 700; text-transform: uppercase; }
.source .stitle { font-weight: 600; font-size: 13.5px; line-height: 1.4; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.source .num { color: var(--accent); margin-inline-end: 4px; }
.source .smeta { color: var(--faint); font-size: 12px; }
.locations { display: grid; gap: var(--s3); margin-top: var(--s3); }
.location { display: grid; grid-template-columns: minmax(0, 1fr); border: 1px solid var(--line); border-radius: var(--r-lg); overflow: hidden; background: var(--surface); }
.location iframe { width: 100%; height: 170px; border: 0; }
.linfo { display: grid; gap: 4px; padding: var(--s3) var(--s4); }
.lname { font-weight: 700; } .laddr, .lcoord { color: var(--muted); font-size: 13.5px; }
.link-preview { display: flex; align-items: center; gap: 10px; padding: 8px 12px; margin: 0 0 8px; border: 1px solid var(--line); border-radius: var(--r-md); background: var(--surface-2); }
.lp-body { display: grid; min-width: 0; } .lp-title { font-weight: 600; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lp-desc { color: var(--muted); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- images & files in answers ---------- */
.media { display: grid; gap: var(--s3); margin-top: var(--s3); }
.media figure { margin: 0; display: grid; gap: var(--s2); justify-items: start; }
.img-card { position: relative; border-radius: var(--r-xl); overflow: hidden; border: 1px solid var(--line); background: var(--surface-2); max-width: min(100%, 520px); box-shadow: var(--shadow); }
.img-card img { display: block; width: 100%; height: auto; cursor: zoom-in; }
.media figcaption { color: var(--muted); font-size: 13.5px; }
.img-actions { display: flex; flex-wrap: wrap; gap: var(--s2); }
.file-card { display: inline-flex; align-items: center; gap: var(--s3); padding: 10px 14px; border: 1px solid var(--line); border-radius: var(--r-md); background: var(--surface);
  color: var(--ink); text-decoration: none; max-width: 100%; transition: border-color var(--fast), background var(--fast); }
a.file-card:hover { border-color: var(--line-strong); background: var(--surface-2); }
.file-card .ext { display: grid; place-items: center; min-width: 38px; height: 38px; padding: 0 6px; border-radius: 10px; background: var(--accent-soft-2); color: var(--accent-ink); font-size: 11.5px; font-weight: 800; text-transform: uppercase; }
.file-card .fname { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; font-size: 14px; }

/* image generation in progress */
.img-loading { position: relative; width: min(100%, 360px); aspect-ratio: 1; border-radius: var(--r-xl); overflow: hidden; border: 1px solid var(--line);
  background: linear-gradient(110deg, var(--surface-2) 25%, var(--accent-soft-2) 45%, var(--surface-2) 65%); background-size: 220% 100%; animation: shimmer 1.8s linear infinite;
  display: grid; place-items: center; align-content: center; gap: var(--s3); color: var(--accent-ink); }
.img-loading .spark { width: 44px; height: 44px; animation: pulse 1.6s ease-in-out infinite; }
.img-loading small { color: var(--muted); font-size: 13px; text-align: center; padding-inline: var(--s4); }

/* error card: honest cause + Retry */
.err-card { display: grid; gap: var(--s2); padding: var(--s3) var(--s4); border: 1px solid color-mix(in srgb, var(--danger) 32%, var(--line)); border-radius: var(--r-lg);
  background: var(--danger-soft); max-width: 560px; }
.err-card .err-text { white-space: pre-wrap; line-height: 1.65; }
.err-card .err-row { display: flex; flex-wrap: wrap; gap: var(--s2); align-items: center; }
.err-card .tag { font-size: 12px; color: var(--muted); }

/* ---------- composer ---------- */
.composer-wrap { padding: 0 var(--s4) calc(var(--s3) + var(--safe-bottom)); background: linear-gradient(to top, var(--main) 70%, transparent); position: relative; z-index: 5; }
.chat.empty .composer-wrap { background: none; }
.composer { width: min(var(--thread-w), 100%); margin-inline: auto; background: var(--surface); border: 1px solid var(--line-strong); border-radius: var(--r-2xl);
  box-shadow: var(--shadow-sm); padding: var(--s2) var(--s2) var(--s2); transition: border-color var(--fast), box-shadow var(--fast); position: relative; }
.composer:focus-within { border-color: color-mix(in srgb, var(--accent) 55%, var(--line-strong)); box-shadow: var(--shadow-sm), var(--ring); }
.composer.drop-hint { border-color: var(--accent); background: var(--accent-soft); }
.composer textarea { display: block; width: 100%; min-height: 28px; max-height: 220px; resize: none; border: 0; outline: 0; background: transparent; padding: 10px 12px 4px;
  font-size: 16px; line-height: 1.6; }
.composer textarea::placeholder { color: var(--faint); }
.compose-bar { display: flex; align-items: center; gap: 4px; padding: 2px 2px 0; }
.compose-bar .grow { flex: 1; }
.tool-chip { display: inline-flex; align-items: center; gap: 6px; min-height: 36px; padding: 0 12px; border: 1px solid var(--line); border-radius: var(--r-full); background: transparent;
  color: var(--muted); font-size: 13.5px; font-weight: 600; transition: all var(--fast); white-space: nowrap; }
.tool-chip:hover { background: var(--hover); color: var(--ink); }
.tool-chip[aria-pressed="true"] { background: var(--accent-soft-2); border-color: color-mix(in srgb, var(--accent) 45%, transparent); color: var(--accent-ink); }
.tool-chip .x { margin-inline-start: 2px; opacity: .7; }
.mode-btn { display: inline-flex; align-items: center; gap: 6px; min-height: 36px; padding: 0 10px; border: 0; border-radius: var(--r-full); background: transparent; color: var(--muted);
  font-size: 13.5px; font-weight: 600; }
.mode-btn:hover { background: var(--hover); color: var(--ink); }
.send { display: grid; place-items: center; width: 42px; height: 42px; border: 0; border-radius: 50%; flex: none; background: var(--surface-3); color: var(--faint);
  transition: background var(--fast), transform var(--fast) var(--ease), color var(--fast), box-shadow var(--fast); }
.send.ready { background: var(--grad); color: #fff; box-shadow: 0 4px 14px rgba(37, 99, 235, .30); }
.send.ready:hover { transform: scale(1.06); }
.send.stop { background: var(--ink); color: var(--bg); box-shadow: none; }
.send:disabled { cursor: not-allowed; }
.mic.rec { color: var(--danger); background: var(--danger-soft); }
.edit-note { display: flex; align-items: center; gap: var(--s2); margin: 4px 8px 0; padding: 6px 12px; border-radius: var(--r-md); background: var(--accent-soft); color: var(--accent-ink); font-size: 13.5px; font-weight: 600; }
.edit-note .grow { flex: 1; }
.composer-hint { width: min(var(--thread-w), 100%); margin: 6px auto 0; text-align: center; color: var(--faint); font-size: 12px; }

/* attachments waiting to be sent */
.pending { display: flex; flex-wrap: wrap; gap: var(--s2); padding: 8px 8px 0; }
.chip { position: relative; display: inline-flex; align-items: center; gap: 8px; height: 56px; padding: 0 10px; border: 1px solid var(--line); border-radius: var(--r-md); background: var(--surface-2); max-width: 220px; }
.chip img { height: 44px; width: 44px; object-fit: cover; border-radius: 8px; }
.chip .label { font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip.uploading { opacity: .6; }
.chip.uploading::after { content: ""; position: absolute; inset: 0; border-radius: inherit; background: linear-gradient(100deg, transparent 30%, var(--accent-soft-2) 50%, transparent 70%); background-size: 200% 100%; animation: shimmer 1.2s linear infinite; }
.chip .rm { position: absolute; top: -7px; inset-inline-end: -7px; width: 22px; height: 22px; display: grid; place-items: center; border: 1px solid var(--line-strong); border-radius: 50%;
  background: var(--surface); color: var(--muted); font-size: 11px; line-height: 1; }
.chip .rm:hover { color: var(--danger); }

/* voice recorder (replaces the toolbar while recording) */
.recorder { display: flex; align-items: center; gap: var(--s3); padding: 6px 8px; min-height: 56px; }
.recorder .rec-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--danger); animation: pulse 1s ease-in-out infinite; flex: none; }
.recorder .time { font: 600 14px var(--mono); color: var(--ink); min-width: 46px; }
.recorder canvas { flex: 1; min-width: 0; height: 36px; }
.recorder .rec-note { color: var(--muted); font-size: 13.5px; flex: 1; }

/* lightbox */
.lightbox { position: fixed; inset: 0; z-index: 150; display: grid; grid-template-rows: auto 1fr; background: rgba(0, 0, 0, .92); animation: fade var(--fast) var(--ease); }
.lightbox .lb-bar { display: flex; gap: var(--s2); justify-content: flex-end; padding: calc(var(--s3) + var(--safe-top)) var(--s3) var(--s3); }
.lightbox .lb-bar .btn { background: rgba(255, 255, 255, .1); border-color: rgba(255, 255, 255, .18); color: #fff; }
.lightbox .lb-bar .btn:hover { background: rgba(255, 255, 255, .2); }
.lightbox .lb-stage { display: grid; place-items: center; padding: 0 var(--s4) var(--s5); min-height: 0; }
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: var(--r-lg); }

@media (max-width: 900px) {
  .thread { padding: var(--s3) var(--s3) var(--s4); }
    .composer-wrap { padding-inline: var(--s2); }
  .composer { border-radius: 24px; }
  .msg.assistant > .mark { width: 28px; height: 28px; border-radius: 9px; }
  .msg { gap: var(--s2); }
  .tool-chip .t-label { display: none; }
  .tool-chip { padding: 0 10px; }
  .mode-btn .mode-label { display: none; }
  .composer-hint { display: none; }
  .thread.empty { padding-bottom: 2vh; align-content: start; padding-top: 6vh; }
}
@media (max-width: 420px) { .composer textarea { font-size: 16px; } .msg-actions .icon-btn { width: 38px; height: 38px; } }

/* ---------- tool starters (sidebar → More), "/" template picker, notices ---------- */
.starter { width: min(var(--thread-w), 100%); margin: 0 auto var(--s2); display: flex; align-items: center; gap: var(--s3); padding: 10px 14px; border: 1px solid var(--line);
  border-radius: var(--r-lg); background: var(--surface-2); color: var(--muted); font-size: 14px; }
.starter .m-icon { background: var(--surface-3); }
.starter strong { color: var(--ink); font-weight: 600; }
.starter .grow { display: grid; line-height: 1.45; }
.template-picker { position: absolute; inset-inline: var(--s2); bottom: calc(100% + 8px); max-height: 280px; overflow-y: auto; padding: 6px; display: grid; gap: 2px;
  background: var(--glass); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); border: 1px solid var(--line-strong); border-radius: var(--r-lg);
  box-shadow: var(--shadow-lg); z-index: 30; animation: pop var(--fast) var(--ease); }
.template-picker button { display: grid; gap: 1px; text-align: start; padding: 8px 12px; border: 0; border-radius: var(--r-sm); background: none; color: var(--ink); }
.template-picker button:hover, .template-picker button[aria-selected="true"] { background: var(--active); }
.template-picker strong { font-size: 14px; }
.template-picker small { color: var(--muted); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.template-picker .empty { padding: 10px 12px; color: var(--muted); font-size: 13.5px; }
.limit-note { display: flex; align-items: center; gap: var(--s2); width: min(var(--thread-w), 100%); margin: 0 auto var(--s2); padding: 8px 14px; border-radius: var(--r-md);
  background: color-mix(in srgb, var(--warn) 12%, transparent); color: var(--warn); font-size: 13.5px; font-weight: 600; }
.limit-note.full { background: var(--danger-soft); color: var(--danger); }
