/* Krishn chat page. Colours are tokens; dark mode follows the system unless the user picks one (data-theme). */
:root {
  --bg: #f7f5f0; --panel: #ffffff; --sunk: #f0ede6; --ink: #1d1c1a; --muted: #6c6a64; --line: #e4e0d7;
  --accent: #1f5f8b; --accent-ink: #ffffff; --accent-soft: #e3eef6; --user: #1f5f8b; --user-ink: #ffffff;
  --ok: #2e7d32; --warn: #a15c00; --bad: #c0392b; --run: #1f5f8b; --shadow: 0 1px 2px rgba(0,0,0,.06);
  --radius: 14px; --side: 290px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121315; --panel: #1b1c1f; --sunk: #232428; --ink: #ecebe8; --muted: #9c9a95; --line: #2e2f33;
    --accent: #6fb3e0; --accent-ink: #0d1b26; --accent-soft: #1d2a35; --user: #2a6a96; --user-ink: #ffffff;
    --ok: #6cc070; --warn: #f0a94b; --bad: #ef6b5b; --run: #6fb3e0; --shadow: none;
  }
}
:root[data-theme="dark"] {
  --bg: #121315; --panel: #1b1c1f; --sunk: #232428; --ink: #ecebe8; --muted: #9c9a95; --line: #2e2f33;
  --accent: #6fb3e0; --accent-ink: #0d1b26; --accent-soft: #1d2a35; --user: #2a6a96; --user-ink: #ffffff;
  --ok: #6cc070; --warn: #f0a94b; --bad: #ef6b5b; --run: #6fb3e0; --shadow: none;
}

* { box-sizing: border-box }
html, body { height: 100% }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", "Noto Sans", "Noto Sans Telugu", "Noto Sans Devanagari", sans-serif;
  -webkit-text-size-adjust: 100%;
}
button, input, textarea, select { font: inherit; color: inherit }
button { cursor: pointer }
a { color: var(--accent) }
.hidden { display: none !important }
.muted { color: var(--muted); font-size: 13px }
.err { color: var(--bad); font-size: 14px; min-height: 1.2em }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px }

/* buttons */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border-radius: 10px;
       border: 1px solid var(--accent); background: var(--accent); color: var(--accent-ink); font-weight: 600 }
.btn:disabled { opacity: .5; cursor: default }
.btn.ghost { background: transparent; color: var(--ink); border-color: var(--line); font-weight: 500 }
.btn.small { padding: 3px 10px; font-size: 13px; border-radius: 8px }
.btn.danger { color: var(--bad); border-color: var(--line); background: transparent }
.icon-btn { width: 36px; height: 36px; display: inline-grid; place-items: center; border-radius: 10px;
            border: 1px solid transparent; background: transparent; color: var(--ink) }
.icon-btn:hover { background: var(--sunk) }
.icon-btn svg { width: 20px; height: 20px }

/* login */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: 16px }
.login { width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line);
         border-radius: var(--radius); padding: 24px; box-shadow: var(--shadow) }
.login h1 { margin: 0 0 4px; font-size: 22px }
.login label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 4px }
.field { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg) }
.login .btn { width: 100%; justify-content: center; margin-top: 18px; padding: 10px }

/* app shell */
.shell { display: grid; grid-template-columns: var(--side) 1fr; height: 100% }
.side { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0 }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 8px }
.brand { font-weight: 700; font-size: 19px; letter-spacing: .2px; display: flex; align-items: center; gap: 8px }
.brand img { width: 26px; height: 26px }
.nav { display: flex; flex-direction: column; gap: 2px; padding: 4px 8px }
.nav button { text-align: left; padding: 8px 10px; border-radius: 10px; border: 0; background: transparent;
              display: flex; align-items: center; gap: 10px }
.nav button svg { width: 18px; height: 18px; color: var(--muted) }
.nav button[aria-current="page"] { background: var(--accent-soft); font-weight: 600 }
.nav button:hover { background: var(--sunk) }
.side .search { margin: 8px 12px 4px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px;
                background: var(--bg); width: calc(100% - 24px) }
.history { flex: 1; overflow-y: auto; padding: 4px 8px 12px; min-height: 0 }
.history h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted);
              margin: 14px 8px 4px; font-weight: 600 }
.history button { display: block; width: 100%; text-align: left; border: 0; background: transparent;
                  padding: 7px 10px; border-radius: 9px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
                  font-size: 14px }
.history button:hover { background: var(--sunk) }
.history button.active { background: var(--accent-soft) }
.history .dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 7px; background: var(--muted);
                vertical-align: middle }
.history .dot.running, .history .dot.planning { background: var(--run) }
.history .dot.done { background: var(--ok) } .history .dot.failed, .history .dot.refused { background: var(--bad) }
.side-foot { border-top: 1px solid var(--line); padding: 10px 12px; display: flex; align-items: center;
             justify-content: space-between; gap: 8px }
.who { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative }

/* 3D avatar (D-138): large above the empty chat, docked bottom-right once there are messages */
#avatar { position: absolute; z-index: 5; cursor: pointer; transition: width .35s ease, height .35s ease, top .35s ease, right .35s ease }
#avatar canvas { width: 100%; height: 100%; display: block }
#avatar[hidden] { display: none }
.shell:not(.has-thread) #avatar { top: 5vh; left: 50%; transform: translateX(-50%); width: min(300px, 72vw); height: min(330px, 40vh) }
.shell.has-thread #avatar { right: 18px; bottom: calc(var(--composer-h, 92px) + 10px); width: 150px; height: 175px;
  background: radial-gradient(circle at 50% 35%, var(--accent-soft), var(--panel) 70%); border: 1px solid var(--line);
  border-radius: 22px; box-shadow: 0 6px 24px rgba(0,0,0,.12) }
.avatar-on:not(.has-thread) .empty { padding-top: calc(min(330px, 40vh) + 1vh) }
.avatar-on #emptyIcon { display: none }
.avatar-on.has-thread .thread { padding-bottom: 150px }
.shell.chat-hidden #avatar { display: none }
@media (prefers-reduced-motion: reduce) { #avatar { transition: none } }
.topbar { display: none; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--line);
          background: var(--panel) }
.view { flex: 1; overflow-y: auto; min-height: 0 }
.wrap { max-width: 820px; margin: 0 auto; padding: 20px 16px }

/* chat */
.thread { display: flex; flex-direction: column; gap: 18px; padding-bottom: 8px }
.empty { text-align: center; padding: 8vh 8px 0 }
.empty h2 { margin: 8px 0 4px; font-size: 24px }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 18px }
.chip { border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: 7px 14px; font-size: 14px }
.chip:hover { border-color: var(--accent) }
.msg-user { align-self: flex-end; max-width: 85%; background: var(--user); color: var(--user-ink);
            padding: 10px 14px; border-radius: 16px 16px 4px 16px; white-space: pre-wrap; overflow-wrap: anywhere }
.msg-bot { align-self: stretch; background: var(--panel); border: 1px solid var(--line); border-radius: 4px 16px 16px 16px;
           padding: 12px 16px; box-shadow: var(--shadow); overflow-wrap: anywhere }
.msg-bot.flash { outline: 2px solid var(--accent) }
.meta { display: flex; flex-wrap: wrap; gap: 6px 10px; align-items: center; font-size: 12px; color: var(--muted) }
.badge { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; padding: 1px 8px; border-radius: 999px;
         border: 1px solid currentColor }
.badge.done, .badge.reassigned { color: var(--ok) }
.badge.running, .badge.retrying, .badge.planning, .badge.pending, .badge.queued { color: var(--run) }
.badge.failed, .badge.skipped, .badge.refused { color: var(--bad) }
.spin { width: 10px; height: 10px; border-radius: 50%; border: 2px solid currentColor; border-right-color: transparent;
        animation: spin 1s linear infinite }
@keyframes spin { to { transform: rotate(360deg) } }
@media (prefers-reduced-motion: reduce) { .spin { animation: none } }
details.plan { margin: 8px 0 2px; border: 1px solid var(--line); border-radius: 10px; background: var(--sunk) }
details.plan summary { cursor: pointer; padding: 6px 10px; font-size: 13px; color: var(--muted) }
details.plan ol { margin: 0; padding: 4px 12px 10px 32px; font-size: 14px }
details.plan li { margin: 4px 0 }
details.plan .part { color: var(--muted); font-size: 12px }
.answer { margin-top: 8px }
.answer p { margin: 0 0 10px } .answer p:last-child { margin-bottom: 0 }
.answer ul, .answer ol { margin: 0 0 10px; padding-left: 22px }
.answer pre { background: var(--sunk); border: 1px solid var(--line); border-radius: 8px; padding: 10px; overflow-x: auto;
              font-size: 13px; white-space: pre }
.answer code { background: var(--sunk); padding: 1px 5px; border-radius: 5px; font-size: 13.5px }
.answer pre code { background: none; padding: 0 }
.answer h4 { margin: 12px 0 6px; font-size: 15px }
.notes { margin-top: 8px; font-size: 13px; color: var(--warn) }
.files { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px }
.file { display: inline-flex; align-items: center; gap: 8px; padding: 7px 12px; border: 1px solid var(--line);
        border-radius: 10px; text-decoration: none; color: var(--ink); background: var(--bg); font-size: 14px }
.file:hover { border-color: var(--accent) }
.actions { display: flex; gap: 6px; margin-top: 10px; align-items: center }
.actions .btn.on { border-color: var(--accent); background: var(--accent-soft) }

.composer { border-top: 1px solid var(--line); background: var(--bg); padding: 10px 16px calc(10px + env(safe-area-inset-bottom)) }
.composer form { max-width: 820px; margin: 0 auto }
.box { display: flex; gap: 8px; align-items: flex-end; background: var(--panel); border: 1px solid var(--line);
       border-radius: 16px; padding: 8px 8px 8px 14px; box-shadow: var(--shadow) }
.box:focus-within { border-color: var(--accent) }
.box textarea { flex: 1; border: 0; background: transparent; resize: none; max-height: 40vh; min-height: 24px;
                padding: 6px 0; outline: none }
.box .btn { border-radius: 12px; padding: 8px 12px }
.hint { display: flex; justify-content: space-between; gap: 8px; margin-top: 6px; font-size: 12px; color: var(--muted) }

/* panels (memory, trading, admin) */
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px;
         margin-bottom: 16px; box-shadow: var(--shadow) }
.panel h2 { margin: 0 0 4px; font-size: 18px }
.panel h3 { margin: 16px 0 6px; font-size: 15px }
.list { list-style: none; margin: 8px 0 0; padding: 0 }
.list li { display: flex; gap: 10px; align-items: flex-start; justify-content: space-between; padding: 9px 0;
           border-top: 1px solid var(--line) }
.list li:first-child { border-top: 0 }
.tag { font-size: 11px; color: var(--muted); border: 1px solid var(--line); border-radius: 6px; padding: 0 6px; margin-left: 6px }
.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px }
.tabs button[aria-selected="true"] { border-color: var(--accent); background: var(--accent-soft) }
.md table { border-collapse: collapse; font-size: 13px; margin: 8px 0 14px; display: block; overflow-x: auto; max-width: 100% }
.md th, .md td { border: 1px solid var(--line); padding: 4px 8px; text-align: left; white-space: nowrap }
.md th { background: var(--sunk) }
.md h1 { font-size: 19px; margin: 0 0 8px } .md h2 { font-size: 16px; margin: 16px 0 6px } .md h3 { font-size: 15px }
.md ul { padding-left: 20px }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px }
.secret { font-family: ui-monospace, monospace; background: var(--sunk); padding: 8px; border-radius: 8px;
          overflow-wrap: anywhere; font-size: 13px }
table.plain { width: 100%; border-collapse: collapse; font-size: 14px }
table.plain td, table.plain th { padding: 6px 4px; border-top: 1px solid var(--line); text-align: left }

.scrim { display: none }
@media (max-width: 820px) {
  .shell { grid-template-columns: 1fr }
  .side { position: fixed; inset: 0 auto 0 0; width: min(86vw, var(--side)); z-index: 20; transform: translateX(-100%);
          transition: transform .2s ease }
  .shell.open .side { transform: none; box-shadow: 0 0 40px rgba(0,0,0,.25) }
  .shell.open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 10 }
  .topbar { display: flex }
  .wrap { padding: 14px 16px }
  .msg-user { max-width: 92% }
  .grid2 { grid-template-columns: 1fr }
  .shell.has-thread #avatar { right: 10px; width: 96px; height: 112px; border-radius: 18px }
  .avatar-on.has-thread .thread { padding-bottom: 100px }
}
@media (prefers-reduced-motion: reduce) { .side { transition: none } }
