/* ============================== Hermes PWA ============================== */
:root {
  --bg: #0b0f17;
  --bg-2: #0f1420;
  --surface: #141b29;
  --surface-2: #1a2233;
  --line: #232e44;
  --text: #e8edf7;
  --text-dim: #9aa7c0;
  --text-faint: #69758f;
  --accent: #3b82f6;
  --accent-2: #22d3ee;
  --teal: #2dd4bf;
  --user-bubble: #1e3a8a;
  --err: #f87171;
  --ok: #34d399;
  --radius: 16px;
  --tap: 48px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --hdr-h: calc(62px + var(--safe-top));
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI",
          Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  touch-action: manipulation;
}

.hidden, [hidden] { display: none !important; }

button { font-family: inherit; }

/* --------------------------- Login --------------------------- */
.login {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(120% 80% at 50% 0%, #12233f 0%, var(--bg) 60%);
}
.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 32px 26px 28px;
  text-align: center;
  box-shadow: 0 24px 60px rgba(0,0,0,.5);
}
.login-card .logo { width: 76px; height: 76px; border-radius: 18px; margin-bottom: 18px; box-shadow: 0 10px 30px rgba(59,130,246,.25); }
.login-card h1 { margin: 0 0 4px; font-size: 24px; letter-spacing: .2px; }
.login-card .sub { margin: 0 0 22px; color: var(--text-dim); font-size: 14px; }
.login-card input {
  width: 100%;
  height: var(--tap);
  padding: 0 16px;
  margin-bottom: 12px;
  border-radius: 13px;
  border: 1px solid var(--line);
  background: var(--bg-2);
  color: var(--text);
  font-size: 17px;
  outline: none;
}
.login-card input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(59,130,246,.22); }

.err { color: var(--err); font-size: 14px; margin: 14px 0 0; min-height: 1em; }

/* --------------------------- Buttons --------------------------- */
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 13px;
  min-height: var(--tap);
  padding: 0 18px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: transform .06s ease, background .15s ease, opacity .15s;
}
.btn:active { transform: scale(.98); }
.btn.primary { width: 100%; background: linear-gradient(180deg, #3b82f6, #2563eb); border-color: transparent; color: #fff; }
.btn:disabled { opacity: .5; }

.iconbtn {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text-dim);
  width: 44px;
  height: 44px;
  border-radius: 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex: none;
}
.iconbtn:active { background: var(--surface-2); color: var(--text); }

/* --------------------------- App frame --------------------------- */
#app { display: flex; flex-direction: column; height: 100dvh; --hdr-h: calc(60px + var(--safe-top)); }

.hdr {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: calc(8px + var(--safe-top)) 12px 8px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, var(--bg-2), var(--bg));
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  position: relative;
  z-index: 60; /* header stays above the sheet so its buttons are always tappable */
  height: var(--hdr-h);
  box-sizing: border-box;
}
.hdr-left { display: flex; align-items: center; gap: 11px; min-width: 0; }
.hdr-logo { width: 34px; height: 34px; border-radius: 9px; flex: none; }
.hdr-title { font-weight: 700; font-size: 16px; line-height: 1.1; }
.hdr-sub { font-size: 11.5px; color: var(--teal); margin-top: 1px; }
.hdr-sub.err { color: var(--err); }
.hdr-right { display: flex; gap: 2px; }

/* --------------------------- Scroller / messages --------------------------- */
.scroller { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 14px 12px 10px; }
.scroller::-webkit-scrollbar { width: 0; }

.messages { display: flex; flex-direction: column; gap: 12px; }

.msg { display: flex; flex-direction: column; max-width: 100%; }
.msg.user { align-items: flex-end; }
.msg.agent { align-items: flex-start; }

.bubble {
  border-radius: 20px;
  padding: 11px 14px;
  font-size: 15.5px;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
  max-width: 88%;
  line-height: 1.5;
}
.msg.user .bubble {
  background: linear-gradient(180deg, #2b54c9, #1e3a8a);
  color: #fff;
  border-bottom-right-radius: 8px;
}
.msg.agent .bubble {
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--text);
  border-bottom-left-radius: 8px;
}
.msg.agent .bubble.thinking { color: var(--text-dim); font-style: italic; }

.meta { font-size: 11px; color: var(--text-faint); margin: 5px 6px 0; }
.meta.right { text-align: right; }

.toolrow {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 12px;
  color: var(--text-dim);
  background: var(--surface);
  border: 1px dashed var(--line);
  border-radius: 11px;
  padding: 6px 11px;
  margin-top: 6px;
  width: fit-content;
}
.toolrow .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent-2); animation: pulse 1.1s infinite ease-in-out; }
.toolrow.done .dot { background: var(--ok); animation: none; }
@keyframes pulse { 0%,100% { opacity: .35; transform: scale(.85);} 50% { opacity: 1; transform: scale(1.15);} }

.cursor {
  display: inline-block;
  width: 8px; height: 17px;
  background: var(--teal);
  border-radius: 2px;
  margin-left: 3px;
  vertical-align: text-bottom;
  animation: blink 1s steps(2, start) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.sysline {
  text-align: center;
  font-size: 12px;
  color: var(--text-faint);
  margin: 4px 0;
}
.sysline .warn { color: var(--err); }

/* code */
.bubble code {
  font-family: var(--mono);
  font-size: .88em;
  background: rgba(120,150,220,.14);
  padding: .08em .35em;
  border-radius: 5px;
  word-break: break-word;
}
.codebox {
  margin: 8px 0;
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  background: #0a0e17;
}
.codebox .code-lang {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-faint);
  padding: 5px 11px 0;
}
.codebox pre { margin: 0; padding: 10px 12px; overflow-x: auto; }
.codebox code {
  font-family: var(--mono);
  font-size: 12.5px;
  white-space: pre;
  background: transparent;
  padding: 0;
  color: #c9d3e6;
}

/* --------------------------- Welcome --------------------------- */
.welcome { padding: 8px 4px 4px; }
.welcome p { margin: 6px 2px 16px; color: var(--text-dim); font-size: 15px; }
.chips { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.chip {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  border-radius: 14px;
  padding: 13px 12px;
  font-size: 13.5px;
  text-align: left;
  line-height: 1.3;
  cursor: pointer;
  min-height: 54px;
}
.chip:active { background: var(--surface-2); transform: scale(.98); }

/* --------------------------- Attach strip --------------------------- */
.attach-strip {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 8px 12px 0;
  -webkit-overflow-scrolling: touch;
}
.file-pill {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: none;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 6px 8px 6px 10px;
  font-size: 12.5px;
  max-width: 220px;
}
.file-pill .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 130px; }
.file-pill .sz { color: var(--text-faint); flex: none; }
.file-pill button { appearance:none;border:none;background:transparent;color:var(--text-faint);font-size:18px;line-height:1;padding:0 2px;cursor:pointer; }
.file-pill button:active { color: var(--err); }

/* --------------------------- Composer --------------------------- */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 8px 8px calc(8px + var(--safe-bottom));
  border-top: 1px solid var(--line);
  background: linear-gradient(180deg, var(--bg), var(--bg-2));
}
.composer-input {
  flex: 1;
  resize: none;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  border-radius: 15px;
  padding: 11px 13px;
  font-size: 16px;
  line-height: 1.35;
  max-height: 120px;
  outline: none;
  min-height: 42px;
}
.composer-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(59,130,246,.18); }

.sendbtn {
  appearance: none;
  flex: none;
  width: 44px;
  height: 44px;
  border-radius: 14px;
  border: none;
  background: linear-gradient(180deg, #3b82f6, #2563eb);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform .06s ease, opacity .15s;
}
.sendbtn:active { transform: scale(.94); }
.sendbtn:disabled { opacity: .4; }
.sendbtn.stop { background: linear-gradient(180deg, #f87171, #dc2626); }
.sendbtn.stop.armed {
  background: linear-gradient(180deg, #facc15, #ca8a04);
  animation: armed-pulse .5s ease-in-out infinite alternate;
}
@keyframes armed-pulse {
  from { box-shadow: 0 1px 6px rgba(0,0,0,.4), 0 0 0 0 rgba(250, 204, 21, .35); }
  to   { box-shadow: 0 1px 6px rgba(0,0,0,.4), 0 0 0 9px rgba(250, 204, 21, .0); }
}
.sendbtn.stop svg { width: 18px; height: 18px; }
.sendbtn .stopbar { width: 14px; height: 14px; border-radius: 3px; background: #fff; }

/* --------------------------- Left panel (sessions) --------------------------- */
:root { --panel-w: 320px; }
.sheet {
  position: fixed;
  inset: 0;
  z-index: 50;
  pointer-events: none;
}
.sheet[aria-hidden="true"] .sheet-card { visibility: hidden; transition: none; }
.sheet-backdrop {
  position: absolute;
  inset: 0;
  top: var(--hdr-h);
  background: rgba(3,6,12,.55);
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
  opacity: 0;
  transition: opacity .26s ease;
}
body.panel-open #sheet { pointer-events: auto; }
body.panel-open .sheet-backdrop { opacity: 1; pointer-events: auto; }

/* legacy side-tab: replaced by the top-left header chevron (#btn-panel) */
.panel-handle { display: none; }

#btn-panel { min-width: 0; width: 44px; height: 44px; transition: transform .3s; flex: none; }
body.panel-open #btn-panel { transform: rotate(180deg); background: var(--surface-2); color: var(--text); }
/* The drawer drops below the header, so it never covers the toggle row */
body.panel-open .sheet-card { top: var(--hdr-h); }
body.panel-open .sheet-bar { padding: 10px 12px; }

/* Thinking blocks (assistant reasoning from the DB) */
.think-block {
  margin: 0 0 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.02);
  font-size: 0.85rem;
}
.think-block summary {
  cursor: pointer;
  padding: 8px 12px;
  color: var(--text-dim);
  list-style: none;
  user-select: none;
  min-height: 44px;
  display: flex;
  align-items: center;
}
.think-block summary::before {
  content: "▸";
  margin-right: 8px;
  display: inline-block;
  transition: transform .2s;
}
.think-block[open] summary::before { transform: rotate(90deg); }
.msg.user .think-block summary { padding-right: 12px; }
.think-body {
  padding: 0 12px 10px;
  color: var(--text-dim);
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.5;
  max-height: 320px;
  overflow-y: auto;
}
/* Live thinking feed (streams while the model thinks) */
.think-block.think-live {
  border-color: var(--accent, #6cc);
  background: rgba(102, 204, 204, 0.05);
}
.think-block.think-live summary {
  color: var(--text);
  animation: think-pulse 1.4s ease-in-out infinite;
}
@keyframes think-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.55; }
}
.think-cursor {
  display: inline-block;
  width: 7px;
  height: 14px;
  margin-left: 3px;
  vertical-align: -2px;
  background: var(--text-dim);
  animation: think-blink 0.85s steps(1) infinite;
}
@keyframes think-blink {
  50% { opacity: 0; }
}
.sheet-card {
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: min(var(--panel-w), 86vw);
  background: var(--bg-2);
  border-right: 1px solid var(--line);
  box-shadow: 10px 0 32px rgba(0,0,0,.45);
  display: flex;
  flex-direction: column;
  padding-left: env(safe-area-inset-left);
  transform: translateX(-104%);
  transition: transform .3s cubic-bezier(.25,.8,.25,1);
  pointer-events: auto;
}
body.panel-open .sheet-card { transform: none; }

/* One-arrow toggle: a chevron tab riding the panel's edge.
   Closed → flush at the left screen edge, pointing right (open it).
   Open   → at the panel's right edge, pointing left (minimise it). */
/* legacy side-tab: retired — replaced by the top-left header chevron (#btn-panel) */
.panel-handle { display: none !important; }
.panel-handle svg { transition: none; }
body.panel-open .panel-handle { left: 0; }
body.panel-open .panel-handle svg { transform: none; }
.panel-handle:active { background: var(--surface-2); color: var(--text); }
.sheet-bar { display: flex; align-items: center; justify-content: space-between; padding: calc(12px + env(safe-area-inset-top)) 12px 10px; }
.sheet-bar h2 { margin: 0; font-size: 17px; }
.sheet-bar h2 .scount { font-size: 12px; font-weight: 500; color: var(--text-faint); margin-left: 6px; }
.sheet-scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 0 12px calc(18px + var(--safe-bottom)); margin-left: 12px; }

#app { transition: margin-left .3s cubic-bezier(.25,.8,.25,1); }
@media (min-width: 900px) {
  /* Wide screens: dock the panel, push the chat over — no backdrop. */
  .sheet-backdrop { display: none; }
  .sheet-card { width: var(--panel-w); box-shadow: none; }
  body.panel-open #app { margin-left: var(--panel-w); }
}

.session-list { display: flex; flex-direction: column; gap: 9px; }
.srow {
  width: 100%;
  text-align: left;
  appearance: none;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 14px;
  padding: 12px 13px;
  color: var(--text);
  cursor: pointer;
}
.srow:active { background: var(--surface-2); }
.srow .t { font-size: 14.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding-right: 44px; }
.srow .t.titling { color: var(--text-faint); font-style: italic; }
.srow .d { font-size: 12px; color: var(--text-dim); margin-top: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.srow .r2 { font-size: 11px; color: var(--text-faint); margin-top: 5px; display: flex; gap: 10px; }
.srow.active { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, var(--surface)); }
.sdel {
  position: absolute;
  top: 8px; right: 8px;
  width: 28px; height: 28px;
  border: none; border-radius: 9px;
  background: transparent;
  color: var(--text-faint);
  font-size: 15px; line-height: 1;
  display: grid; place-items: center;
  cursor: pointer;
  transition: background .12s, color .12s;
}
.sdel:hover, .sdel:active { background: color-mix(in srgb, #d24 22%, transparent); color: #ff9a9a; }
.srow { position: relative; }

.session-detail { padding-top: 6px; }
.back { appearance:none; border:none; background:transparent; color: var(--accent-2); font-size: 14px; padding: 6px 0; cursor:pointer; display:inline-flex; align-items:center; gap:6px; }
.detail-title { font-size: 16px; font-weight: 700; margin: 6px 0 12px; }

.muted { color: var(--text-faint); font-size: 14px; text-align: center; padding: 24px 0; }
.muted.err { color: var(--err); }

/* --------------------------- Small screens --------------------------- */
@media (max-width: 360px) {
  .bubble { font-size: 15px; max-width: 92%; }
  .chips { grid-template-columns: 1fr; }
}

@media (prefers-color-scheme: light) {
  /* Keep the dark, focused theme regardless — reads best on a phone chat app. */
}
