:root {
  --bg: #05080f; --bg2: #0a101c; --panel: #0d1422; --line: rgba(140,170,255,.12);
  --text: #e6ecf7; --dim: #8492ab; --faint: #56627a;
  --accent: #8b7bff; --accent2: #3fd0ff; --ok: #3ddc97; --warn: #ffb547; --err: #ff5c7a;
  --user: #1b2440; --radius: 14px;
  --font: "Inter", system-ui, -apple-system, sans-serif; --mono: "JetBrains Mono", ui-monospace, monospace;
  --safe-top: env(safe-area-inset-top); --safe-bottom: env(safe-area-inset-bottom);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  background: radial-gradient(900px 600px at 70% -10%, rgba(139,123,255,.14), transparent 60%),
              radial-gradient(700px 500px at 0% 110%, rgba(63,208,255,.08), transparent 60%), var(--bg);
  color: var(--text); font: 15px/1.55 var(--font); overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

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

/* ------------------------------------------------ sidebar */
#sidebar {
  width: 280px; flex-shrink: 0; display: flex; flex-direction: column; gap: 12px;
  background: rgba(10,16,28,.85); border-right: 1px solid var(--line);
  padding: calc(16px + var(--safe-top)) 14px calc(14px + var(--safe-bottom));
  backdrop-filter: blur(12px);
}
.side-head { display: flex; align-items: center; justify-content: space-between; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; letter-spacing: .01em; }
.brand-orb, .welcome-orb {
  width: 22px; height: 22px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff 0 8%, var(--accent2) 30%, var(--accent) 70%, #2a1f6b 100%);
  box-shadow: 0 0 18px rgba(139,123,255,.6);
}
#sessions { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; margin: 0 -6px; padding: 0 6px; }
.sess {
  display: flex; align-items: center; gap: 6px; padding: 9px 10px; border-radius: 10px;
  color: var(--dim); text-align: left; background: none; border: 0; width: 100%;
}
.sess:hover { background: rgba(255,255,255,.04); color: var(--text); }
.sess.active { background: rgba(139,123,255,.14); color: var(--text); }
.sess .t { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
.sess .del { opacity: 0; font-size: 12px; padding: 2px 6px; border-radius: 6px; background: none; border: 0; color: var(--faint); }
.sess:hover .del, .sess.active .del { opacity: 1; }
.sess .del:hover { color: var(--err); }
.side-foot { display: flex; flex-direction: column; gap: 10px; }
.status-line { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--faint); padding: 0 4px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); }
.dot.ok { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.dot.bad { background: var(--err); box-shadow: 0 0 8px var(--err); }
#scrim { display: none; }

/* ------------------------------------------------ buttons */
.btn { border-radius: 10px; padding: 9px 14px; border: 1px solid var(--line); background: rgba(255,255,255,.03); font-weight: 500; font-size: 14px; }
.btn:hover { background: rgba(255,255,255,.07); }
.btn.primary { background: linear-gradient(135deg, var(--accent), #6a5cf0); border-color: transparent; color: #fff; }
.btn.primary:hover { filter: brightness(1.1); }
.btn.ghost { background: transparent; }
.btn.ok { border-color: var(--ok); color: var(--ok); }
.btn.no { border-color: var(--err); color: var(--err); }
.btn.wide { width: 100%; }
.icon-btn { background: none; border: 1px solid transparent; border-radius: 10px; width: 38px; height: 38px; font-size: 18px; color: var(--dim); }
.icon-btn:hover { background: rgba(255,255,255,.06); color: var(--text); }
.chip { border: 1px solid var(--line); background: rgba(255,255,255,.03); border-radius: 999px; padding: 7px 13px; font-size: 13px; color: var(--dim); }
.chip:hover { color: var(--text); border-color: rgba(139,123,255,.5); }
.only-mobile { display: none; }

/* ------------------------------------------------ main */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#topbar {
  display: flex; align-items: center; gap: 10px; padding: calc(10px + var(--safe-top)) 18px 10px;
  border-bottom: 1px solid var(--line); min-height: 58px;
}
#topbar h1 { flex: 1; font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#messages { flex: 1; overflow-y: auto; padding: 24px 18px 12px; display: flex; flex-direction: column; gap: 18px; scroll-behavior: smooth; }
#messages > * { width: 100%; max-width: 780px; margin: 0 auto; }

#welcome { margin: auto; text-align: center; display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 30px 0; }
.welcome-orb { width: 64px; height: 64px; animation: breathe 4s ease-in-out infinite; }
@keyframes breathe { 50% { transform: scale(1.08); box-shadow: 0 0 40px rgba(139,123,255,.8); } }
#welcome h2 { font-size: 22px; font-weight: 600; }
#welcome p { color: var(--dim); }
.suggestions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 8px; }

.msg { display: flex; }
.msg.user { justify-content: flex-end; }
.msg.user .bubble { background: var(--user); border: 1px solid rgba(139,123,255,.18); border-radius: 18px 18px 4px 18px; padding: 10px 14px; max-width: 85%; white-space: pre-wrap; word-break: break-word; }
.msg.assistant .bubble { width: 100%; word-break: break-word; }
.bubble p { margin: 0 0 .7em; } .bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: .3em 0 .7em 1.3em; }
.bubble h1, .bubble h2, .bubble h3 { font-size: 1.05em; margin: 1em 0 .4em; }
.bubble a { color: var(--accent2); }
.bubble code { font-family: var(--mono); font-size: .88em; background: rgba(255,255,255,.06); padding: 1px 5px; border-radius: 5px; }
.bubble pre { background: #070b14; border: 1px solid var(--line); border-radius: 10px; padding: 12px; overflow-x: auto; margin: .6em 0; }
.bubble pre code { background: none; padding: 0; }
.bubble table { border-collapse: collapse; margin: .6em 0; display: block; overflow-x: auto; }
.bubble th, .bubble td { border: 1px solid var(--line); padding: 6px 10px; font-size: 14px; }
.bubble blockquote { border-left: 3px solid var(--accent); padding-left: 10px; color: var(--dim); }
.cursor::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--accent); }
@keyframes blink { 50% { opacity: 0; } }

.tools { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.tool { font: 12px var(--mono); color: var(--dim); border: 1px solid var(--line); border-radius: 999px; padding: 3px 10px; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tool.running { color: var(--accent2); border-color: rgba(63,208,255,.4); }
.tool.running::before { content: "● "; animation: blink 1s steps(2) infinite; }
.tool.done::before { content: "✓ "; color: var(--ok); }
.tool.failed::before { content: "✕ "; color: var(--err); }
.thinking { color: var(--faint); font-size: 14px; }

.approval { border: 1px solid rgba(255,181,71,.45); background: rgba(255,181,71,.06); border-radius: var(--radius); padding: 12px 14px; margin: 8px 0; }
.approval h4 { color: var(--warn); font-size: 14px; margin-bottom: 6px; }
.approval .why { font-size: 13px; color: var(--dim); margin-bottom: 6px; }
.approval pre { font: 12px var(--mono); background: #070b14; border-radius: 8px; padding: 8px 10px; white-space: pre-wrap; word-break: break-all; margin-bottom: 10px; }
.approval .choices { display: flex; flex-wrap: wrap; gap: 8px; }
.approval details.tech { margin-bottom: 10px; }
.approval details.tech summary { cursor: pointer; font-size: 12px; color: var(--faint); list-style: none; }
.approval details.tech summary::before { content: "› "; }
.approval details.tech[open] summary::before { content: "⌄ "; }
.approval details.tech pre { margin-top: 8px; }
.approval.resolved { opacity: .6; }
.note { font-size: 13px; color: var(--dim); }
.note.err { color: var(--err); }

/* ------------------------------------------------ report */
.report { border: 1px solid var(--line); border-radius: var(--radius); background: rgba(13,20,34,.8); padding: 14px; margin: 10px 0; }
.report h3 { font-size: 15px; margin: 0 0 10px; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; margin-bottom: 12px; }
.kpi { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; background: rgba(255,255,255,.02); }
.kpi .l { font-size: 12px; color: var(--dim); }
.kpi .v { font-size: 22px; font-weight: 700; margin-top: 2px; }
.kpi .d { font-size: 12px; font-family: var(--mono); color: var(--ok); }
.kpi .d.neg { color: var(--err); }
.report .gridjs-wrapper, .report .gridjs-table { background: transparent; box-shadow: none; border-color: var(--line); }
.report th.gridjs-th { background: rgba(139,123,255,.08); color: var(--text); border-color: var(--line); font-size: 12px; }
.report td.gridjs-td, .report .gridjs-tr, .report .gridjs-tbody { background: transparent !important; color: var(--text); border-color: var(--line); font-size: 13px; }
.report .gridjs-search input, .report .gridjs-pagination, .report .gridjs-pagination button { background: transparent; color: var(--dim); border-color: var(--line); }

/* ------------------------------------------------ composer */
#composer {
  display: flex; align-items: flex-end; gap: 8px; margin: 0 auto; width: 100%; max-width: 816px;
  padding: 10px 18px calc(14px + var(--safe-bottom));
}
#input {
  flex: 1; resize: none; max-height: 200px; border-radius: 16px; border: 1px solid var(--line);
  background: var(--panel); color: var(--text); font: 15px/1.45 var(--font); padding: 12px 14px; outline: none;
}
#input:focus { border-color: rgba(139,123,255,.55); }
.send-btn { width: 46px; height: 46px; border-radius: 14px; border: 0; background: linear-gradient(135deg, var(--accent), #6a5cf0); color: #fff; font-size: 17px; flex-shrink: 0; }
.send-btn:disabled { opacity: .4; cursor: default; }
.send-btn.stop { background: rgba(255,92,122,.15); color: var(--err); border: 1px solid var(--err); }

/* ------------------------------------------------ voice overlay */
#voice {
  position: fixed; inset: 0; z-index: 40; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  background: radial-gradient(700px 600px at 50% 45%, rgba(40,30,110,.55), transparent 70%), rgba(3,5,10,.97);
  padding: calc(20px + var(--safe-top)) 20px calc(20px + var(--safe-bottom));
}
.voice-close { position: absolute; top: calc(14px + var(--safe-top)); right: 14px; }
#orb { width: min(70vw, 360px); height: min(70vw, 360px); cursor: pointer; }
#voice-status { color: var(--dim); font-size: 14px; letter-spacing: .04em; text-align: center; }
#voice-transcript { min-height: 48px; max-width: 560px; text-align: center; font-size: 17px; color: var(--text); }
#voice-user { min-height: 20px; max-width: 560px; text-align: center; font-size: 13px; color: var(--faint); }
#voice-pending { display: flex; flex-direction: column; gap: 10px; width: 100%; max-width: 480px; }

/* ------------------------------------------------ settings */
.modal { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.6); backdrop-filter: blur(4px); display: flex; align-items: center; justify-content: center; padding: 16px; }
.sheet { width: min(520px, 100%); max-height: 90dvh; overflow-y: auto; background: var(--bg2); border: 1px solid var(--line); border-radius: 18px; padding: 22px; }
.sheet h2 { font-size: 18px; margin-bottom: 6px; }
.sheet label { display: block; font-weight: 600; font-size: 14px; margin: 14px 0 6px; }
.sheet input[type=text], .sheet select { width: 100%; background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; font: 15px var(--font); }
.sheet .row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.sheet .check { display: flex; gap: 8px; align-items: center; font-weight: 500; }
.help { font-size: 12px; color: var(--faint); margin-top: 4px; }
.info { font: 12px/1.7 var(--mono); color: var(--faint); margin-top: 16px; border-top: 1px solid var(--line); padding-top: 12px; }
.actions { display: flex; gap: 10px; align-items: center; justify-content: flex-end; margin-top: 18px; }
.actions .smsg { margin-right: auto; font-size: 13px; }

/* ------------------------------------------------ mobile */
@media (max-width: 820px) {
  .only-mobile { display: inline-flex; align-items: center; justify-content: center; }
  #sidebar { position: fixed; inset: 0 auto 0 0; z-index: 30; width: min(84vw, 320px); transform: translateX(-102%); transition: transform .22s ease; }
  #app.side-open #sidebar { transform: none; }
  #app.side-open #scrim { display: block; position: fixed; inset: 0; z-index: 25; background: rgba(0,0,0,.5); }
  #messages { padding: 16px 12px 8px; }
  #composer { padding-left: 10px; padding-right: 10px; }
  #topbar { padding-left: 8px; padding-right: 8px; }
  .sess .del { opacity: 1; }
  #input { font-size: 16px; } /* avoid iOS zoom on focus */
}

/* ------------------------------------------------ onboarding */
.onboard { margin-top: 18px; max-width: 460px; text-align: left; border: 1px solid rgba(139,123,255,.4);
  background: linear-gradient(135deg, rgba(139,123,255,.12), rgba(63,208,255,.05)); border-radius: 16px; padding: 16px 18px; }
.onboard h3 { font-size: 16px; margin-bottom: 4px; }
.onboard p { font-size: 14px; color: var(--dim); }
.onboard-actions { display: flex; gap: 8px; margin-top: 12px; }

.profile-card { border: 1px solid rgba(61,220,151,.4); background: rgba(61,220,151,.05); border-radius: var(--radius); padding: 12px 14px; margin: 10px 0; }
.profile-card h4 { font-size: 14px; margin-bottom: 8px; color: var(--ok); }
.profile-card details { margin-bottom: 10px; font-size: 14px; }
.profile-card summary { cursor: pointer; color: var(--dim); }
.profile-card .pv { max-height: 320px; overflow: auto; margin-top: 8px; padding: 10px; border-radius: 10px; background: #070b14; }

/* ------------------------------------------------ settings tabs */
.sheet.wide { width: min(640px, 100%); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin: 10px 0 14px; }
.tab { background: none; border: 0; padding: 9px 12px; color: var(--dim); border-bottom: 2px solid transparent; font-weight: 500; }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.editor { width: 100%; background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 10px;
  padding: 12px; font: 13px/1.5 var(--mono); resize: vertical; margin-top: 10px; }
.counter { font: 12px var(--mono); color: var(--faint); text-align: right; margin-top: 4px; }
.mem { margin-top: 16px; }
.mem-head { display: flex; justify-content: space-between; align-items: baseline; }
.mem-head h3 { font-size: 14px; }
.mem-usage { font: 12px var(--mono); color: var(--faint); }
.bar { height: 4px; background: rgba(255,255,255,.06); border-radius: 4px; margin: 6px 0 8px; overflow: hidden; }
.bar span { display: block; height: 100%; background: linear-gradient(90deg, var(--accent2), var(--accent)); }
.bar.full span { background: var(--warn); }
.mem-list { list-style: none; display: flex; flex-direction: column; gap: 6px; }
.mem-list li { display: flex; gap: 6px; align-items: flex-start; border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; font-size: 14px; background: rgba(255,255,255,.02); }
.mem-list li .txt { flex: 1; white-space: pre-wrap; word-break: break-word; }
.mem-list li textarea { flex: 1; background: var(--panel); color: var(--text); border: 1px solid var(--accent); border-radius: 8px; padding: 6px 8px; font: 14px var(--font); resize: vertical; }
.mem-list li button { background: none; border: 0; color: var(--faint); padding: 2px 5px; font-size: 13px; }
.mem-list li button:hover { color: var(--text); }
.mem-list li button.rm:hover { color: var(--err); }
.mem-list .empty { color: var(--faint); font-style: italic; border-style: dashed; }
.mem-add { display: flex; gap: 8px; margin-top: 8px; }
.mem-add input { flex: 1; background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; font: 14px var(--font); }
.smsg { font-size: 13px; }

/* ------------------------------------------------ secrets */
.sec-title { font-size: 14px; margin: 18px 0 8px; }
.sec-list { list-style: none; display: flex; flex-direction: column; gap: 6px; }
.sec-list li { display: flex; align-items: center; gap: 8px; border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; font-size: 14px; }
.sec-list li .n { flex: 1; font-family: var(--mono); font-size: 13px; }
.sec-list li .d { color: var(--faint); font-size: 12px; }
.sec-list li button { background: none; border: 0; color: var(--faint); font-size: 13px; }
.sec-list li button:hover { color: var(--err); }
.sec-list .empty { color: var(--faint); font-style: italic; border-style: dashed; }
#sec-service { width: 100%; background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; font: 15px var(--font); }
#sec-form label { display: block; font-weight: 600; font-size: 14px; margin: 12px 0 6px; }
#sec-form input { width: 100%; background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; font: 15px var(--font); }
.secret-card { border: 1px solid rgba(63,208,255,.45); background: rgba(63,208,255,.05); border-radius: var(--radius); padding: 12px 14px; margin: 10px 0; }
.secret-card h4 { font-size: 14px; margin-bottom: 6px; color: var(--accent2); }
.secret-card .why { font-size: 13px; color: var(--dim); margin-bottom: 8px; }
.secret-card input { width: 100%; background: #070b14; color: var(--text); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; font: 15px var(--mono); margin-bottom: 8px; }
.secret-card .lock { font-size: 12px; color: var(--faint); margin-top: 8px; }

a.btn { text-decoration: none; display: inline-flex; align-items: center; justify-content: center; }
a.btn.primary { color: #fff; }
a.btn.ghost { color: var(--text); }
.linkish { background: none; border: 0; color: var(--faint); font-size: 12px; text-decoration: underline; padding: 4px 0; }
.linkish:hover { color: var(--text); }
.onboard-actions { flex-wrap: wrap; align-items: center; }
