:root {
  --bg: #f6f7f9;
  --surface: #fff;
  --soft: #eef0f3;
  --line: #e2e5ea;
  --text: #16191f;
  --muted: #6b7280;
  --accent: #0f9d6e;
  --accent-ink: #fff;
  --danger: #dc2626;
  color-scheme: light;
}
:root[data-theme=dark] {
  --bg: #0f1115;
  --surface: #171a21;
  --soft: #1e222b;
  --line: #2a2f3a;
  --text: #eceef2;
  --muted: #9aa1ae;
  --accent: #34d399;
  --accent-ink: #06281d;
  --danger: #f87171;
  color-scheme: dark;
}

* { box-sizing: border-box; margin: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body { font: 15px/1.5 Inter, system-ui, sans-serif; background: var(--bg); color: var(--text); display: flex; overflow: hidden; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 6px; border-radius: 10px; }
button:focus-visible, textarea:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.i { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.ib { width: 40px; height: 40px; color: var(--muted); }
.ib:hover { background: var(--soft); color: var(--text); }
.mf { font-family: Verdana, 'Noto Sans Sinhala', sans-serif; line-height: 1.75; }
.hide { display: none !important; }

/* Sidebar */
#sb { width: 280px; background: var(--surface); border-right: 1px solid var(--line); display: flex; flex-direction: column; flex: none; z-index: 40; }
#sb.off { display: none; }
.sbh { padding: 10px 12px; display: flex; gap: 8px; border-bottom: 1px solid var(--line); padding-top: max(10px, env(safe-area-inset-top)); }
.newbtn { flex: 1; height: 40px; background: var(--accent); color: var(--accent-ink); font-weight: 600; font-size: 14px; }
#list { flex: 1; overflow-y: auto; padding: 10px; }
.grp { font-size: 12px; color: var(--muted); padding: 10px 8px 4px; }
.it { display: flex; align-items: center; gap: 6px; padding: 0 4px 0 10px; min-height: 42px; border-radius: 10px; cursor: pointer; }
.it:hover, .it.on { background: var(--soft); }
.it.on { font-weight: 500; }
.it span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.it button { width: 34px; height: 34px; color: var(--muted); }
.it button:hover { color: var(--danger); }
.sbf { padding: 12px 14px; border-top: 1px solid var(--line); font-size: 12px; color: var(--muted); padding-bottom: max(12px, env(safe-area-inset-bottom)); }
#veil { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 30; }

/* Main Layout */
#app { flex: 1; min-width: 0; display: flex; flex-direction: column; height: 100dvh; }
header { height: calc(52px + env(safe-area-inset-top)); padding: env(safe-area-inset-top) 10px 0; display: flex; align-items: center; gap: 6px; border-bottom: 1px solid var(--line); background: var(--surface); flex: none; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 16px; flex: 1; min-width: 0; }
.logo { width: 32px; height: 32px; border-radius: 10px; background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); display: flex; align-items: center; justify-content: center; flex: none; }
.beta { font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, transparent); padding: 2px 7px; border-radius: 99px; }
#chat { flex: 1; overflow-y: auto; padding: 20px 14px; overscroll-behavior: contain; }
#msgs { max-width: 760px; margin: 0 auto; display: flex; flex-direction: column; gap: 22px; }
#hero { max-width: 560px; margin: 8vh auto 0; text-align: center; }
#hero .logo { width: 52px; height: 52px; border-radius: 16px; margin: 0 auto 14px; }
#hero .i { width: 28px; height: 28px; }
#hero h1 { font-size: clamp(22px, 5vw, 30px); font-weight: 600; letter-spacing: -.02em; }
#hero p { color: var(--muted); margin-top: 6px; font-size: 14px; }
#chips { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 22px; }
#chips button { padding: 12px; background: var(--surface); border: 1px solid var(--line); font-size: 13px; text-align: left; justify-content: flex-start; line-height: 1.4; font-family: Verdana, 'Noto Sans Sinhala', sans-serif; }
#chips button:hover { border-color: var(--accent); }

/* Messages & Markdown */
.u { align-self: flex-end; max-width: 85%; background: color-mix(in srgb, var(--accent) 13%, var(--surface)); border: 1px solid color-mix(in srgb, var(--accent) 28%, var(--line)); padding: 10px 14px; border-radius: 16px 16px 4px 16px; white-space: pre-wrap; overflow-wrap: anywhere; }
.a { display: flex; gap: 10px; align-items: flex-start; }
.a .logo { width: 30px; height: 30px; margin-top: 2px; }
.a > div:last-child { flex: 1; min-width: 0; }
.md { overflow-wrap: anywhere; }
.md p { margin-bottom: .7rem; }
.md p:last-child { margin: 0; }
.md ul, .md ol { padding-left: 1.4rem; margin-bottom: .7rem; }
.md li { margin-bottom: .25rem; }
.md h4 { font-size: 1.05em; margin: .9rem 0 .35rem; }
.md strong { font-weight: 700; }
.md a { color: var(--accent); }
.md blockquote { border-left: 3px solid var(--accent); background: var(--soft); padding: .4rem .8rem; margin: .7rem 0; border-radius: 0 8px 8px 0; }
.ic { font: .88em ui-monospace, Menlo, Consolas, monospace; background: var(--soft); padding: .1rem .35rem; border-radius: 6px; }
.cb { margin: .8rem 0; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: var(--soft); }
.cbh { display: flex; justify-content: space-between; align-items: center; padding: 2px 6px 2px 12px; font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--line); }
.cbh button { height: 32px; padding: 0 8px; font-size: 12px; color: var(--muted); }
.cbh button:hover { color: var(--accent); }
.cb pre { padding: 12px; overflow-x: auto; font: 13px/1.55 ui-monospace, Menlo, Consolas, monospace; -webkit-overflow-scrolling: touch; }
.acts { display: flex; flex-wrap: wrap; align-items: center; gap: 2px; margin-top: 6px; font-size: 12px; color: var(--muted); }
.acts button { height: 34px; padding: 0 10px; color: var(--muted); }
.acts button:hover { background: var(--soft); color: var(--text); }
.cur::after { content: ''; display: inline-block; width: 7px; height: 1em; margin-left: 3px; vertical-align: -2px; background: var(--accent); animation: bl .9s steps(1) infinite; }
@keyframes bl { 50% { opacity: 0; } }
.dots { display: inline-flex; gap: 4px; padding: 8px 0; }
.dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); animation: pl 1.2s infinite ease-in-out both; }
.dots i:nth-child(2) { animation-delay: .15s; }
.dots i:nth-child(3) { animation-delay: .3s; }
@keyframes pl { 0%, 80%, 100% { transform: scale(.6); opacity: .4; } 40% { transform: scale(1); opacity: 1; } }

/* TaNIA Error Card */
.note { background: var(--surface); border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--line)); border-radius: 16px; padding: 14px 16px; font-size: 14px; flex: 1; }
.note b { color: var(--accent); display: block; margin-bottom: 4px; font-size: 15px; }
.note p { color: var(--text); line-height: 1.65; }
.note small { display: block; color: var(--muted); margin-top: 6px; font-size: 12px; }
.note button { margin-top: 12px; height: 36px; padding: 0 14px; background: var(--accent); color: var(--accent-ink); font-weight: 600; font-size: 13px; }

/* Composer */
#bar { flex: none; padding: 8px 12px max(10px, env(safe-area-inset-bottom)); background: linear-gradient(transparent, var(--bg) 30%); }
.box { max-width: 760px; margin: 0 auto; background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 8px 8px 6px; }
.box:focus-within { border-color: var(--accent); }
#inp { width: 100%; border: 0; background: none; color: var(--text); resize: none; max-height: 160px; padding: 6px 8px; font-size: 16px; outline: 0; }
#inp::placeholder { color: var(--muted); }
.row { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 2px; }
.seg { display: inline-flex; background: var(--soft); border-radius: 12px; padding: 3px; }
.seg button { height: 34px; padding: 0 12px; font-size: 13px; font-weight: 500; color: var(--muted); }
.seg button[aria-pressed=true] { background: var(--accent); color: var(--accent-ink); }
.send { width: 40px; height: 40px; background: var(--accent); color: var(--accent-ink); border-radius: 12px; }
.stop { height: 40px; padding: 0 14px; background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); font-weight: 600; font-size: 13px; border-radius: 12px; }
.foot { max-width: 760px; margin: 6px auto 0; font-size: 11px; color: var(--muted); display: flex; justify-content: space-between; padding: 0 6px; }

/* Settings Modal */
#set { position: fixed; inset: 0; background: rgba(0,0,0,.6); z-index: 50; display: none; align-items: center; justify-content: center; padding: 12px; }
#set.on { display: flex; }
.dlg { background: var(--surface); border: 1px solid var(--line); border-radius: 18px; width: 100%; max-width: 480px; max-height: 92dvh; overflow-y: auto; padding: 18px; }
.dlg h2 { font-size: 17px; font-weight: 600; flex: 1; }
.dh { display: flex; align-items: center; margin-bottom: 12px; }
.f { margin-bottom: 14px; }
.f label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.f select, .f textarea { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--bg); color: var(--text); font: inherit; font-size: 15px; }
.f small { display: block; color: var(--muted); font-size: 12px; margin-top: 5px; }
.mtags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.mtag { font-size: 11px; padding: 4px 8px; border-radius: 99px; background: var(--soft); color: var(--muted); cursor: pointer; border: 1px solid transparent; }
.mtag:hover, .mtag.on { border-color: var(--accent); color: var(--text); }
.pr { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.pr button { height: 34px; padding: 0 12px; font-size: 12px; background: var(--soft); }
.btn { height: 40px; padding: 0 14px; background: var(--soft); font-size: 13px; font-weight: 500; }
.btn.p { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.d { color: var(--danger); }
.br { display: flex; flex-wrap: wrap; gap: 8px; }
.br.end { justify-content: flex-end; margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--line); }
#toast { position: fixed; left: 50%; top: calc(60px + env(safe-area-inset-top)); transform: translateX(-50%); background: var(--text); color: var(--bg); font-size: 13px; padding: 8px 14px; border-radius: 99px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 60; }

/* Responsive */
@media (max-width: 899px) {
  #sb { position: fixed; inset: 0 auto 0 0; max-width: 86vw; transform: translateX(-100%); transition: transform .2s; box-shadow: 0 0 40px rgba(0,0,0,.3); }
  #sb.off { display: flex; }
  #sb.open { transform: none; }
  #sb.open ~ #veil { display: block; }
  .u { max-width: 92%; }
}
@media (max-width: 420px) {
  .seg button { padding: 0 9px; }
  #chips { grid-template-columns: 1fr; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}