:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg-secondary: var(--tg-theme-secondary-bg-color, #f1f1f1);
  --text: var(--tg-theme-text-color, #222222);
  --hint: var(--tg-theme-hint-color, #999999);
  --accent: var(--tg-theme-button-color, #2aabee);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --destructive: #e53935;
  --bubble-user: var(--tg-theme-button-color, #2aabee);
  --bubble-user-text: var(--tg-theme-button-text-color, #ffffff);
  --bubble-bot: var(--tg-theme-secondary-bg-color, #f1f1f1);
  --code-bg: #0d1117;
  --border: rgba(127,127,127,.18);
  --radius: 12px;
  --maxw: 760px;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg); color: var(--text);
  -webkit-font-smoothing: antialiased;
}
#app { max-width: var(--maxw); margin: 0 auto; min-height: 100dvh; display: flex; flex-direction: column; }
.view { display: flex; flex-direction: column; min-height: 100dvh; }
.hidden { display: none !important; }

/* top bar */
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 14px 10px;
  position: sticky; top: 0; z-index: 10;
  background: var(--bg); border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 19px; font-weight: 700; flex: 1; }
.icon-btn {
  background: var(--bg-secondary); color: var(--text);
  border: none; border-radius: 10px; width: 38px; height: 38px;
  font-size: 18px; cursor: pointer; display: flex; align-items: center; justify-content: center;
}
.icon-btn:active { opacity: .6; }

/* search */
.searchbox { padding: 10px 14px 4px; position: sticky; top: 58px; z-index: 9; background: var(--bg); }
.searchbox input {
  width: 100%; padding: 11px 14px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--bg-secondary); color: var(--text); font-size: 15px; outline: none;
}
.searchbox input:focus { border-color: var(--accent); }

/* session list */
.list { padding: 6px 10px 20px; flex: 1; }
.day-group h3 {
  font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .6px;
  color: var(--hint); padding: 14px 6px 6px;
}
.session-item {
  display: flex; gap: 12px; align-items: center;
  background: var(--bg-secondary); border-radius: var(--radius);
  padding: 12px 14px; margin-bottom: 8px; cursor: pointer;
}
.session-item:active { opacity: .75; }
.s-emoji {
  width: 42px; height: 42px; border-radius: 12px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center; font-size: 21px;
  background: var(--bg); border: 1px solid var(--border);
}
.s-body { flex: 1; min-width: 0; }
.s-title { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.s-sub { font-size: 12.5px; color: var(--hint); margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.s-right { text-align: right; flex-shrink: 0; }
.s-time { font-size: 12px; color: var(--hint); }
.s-count { font-size: 12px; color: var(--hint); margin-top: 3px; }

/* search results */
.result-item { display: block; width: 100%; text-align: left; background: var(--bg-secondary);
  border-radius: var(--radius); padding: 11px 14px; margin-bottom: 8px; border: none; color: var(--text); cursor: pointer; }
.result-item:active { opacity: .75; }
.r-title { font-size: 14.5px; font-weight: 600; margin-bottom: 4px; }
.r-snippet { font-size: 13px; color: var(--hint); line-height: 1.45; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.r-meta { font-size: 11.5px; color: var(--hint); margin-top: 5px; }

/* conversation */
.conv-title-wrap { flex: 1; min-width: 0; }
.conv-title { font-size: 16px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-meta { font-size: 11.5px; color: var(--hint); margin-top: 1px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.messages { flex: 1; padding: 12px 10px 20px; overflow-y: auto; }

.msg { display: flex; margin-bottom: 12px; }
.msg.user { justify-content: flex-end; }
.msg .bubble {
  max-width: 88%; padding: 9px 13px; border-radius: 16px; font-size: 15px; line-height: 1.5;
  word-wrap: break-word; overflow-wrap: anywhere;
}
.msg.user .bubble { background: var(--bubble-user); color: var(--bubble-user-text); border-bottom-right-radius: 5px; }
.msg.assistant .bubble { background: var(--bubble-bot); color: var(--text); border-bottom-left-radius: 5px; }
.msg.system .bubble { background: transparent; color: var(--hint); font-size: 13px; text-align: center; max-width: 100%; }
.msg.tool { justify-content: flex-start; }
.msg .time { font-size: 10.5px; color: var(--hint); padding: 0 6px; margin-top: 2px; align-self: flex-end; }
.msg.user .time { color: rgba(255,255,255,.75); }

/* markdown inside bubbles */
.bubble p { margin: 4px 0; }
.bubble p:first-child { margin-top: 0; }
.bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: 6px 0 6px 20px; }
.bubble blockquote { border-left: 3px solid var(--hint); margin: 6px 0; padding: 2px 10px; color: var(--hint); }
.bubble a { color: var(--accent); }
.bubble table { border-collapse: collapse; margin: 8px 0; width: 100%; font-size: 13.5px; display: block; overflow-x: auto; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 5px 9px; text-align: left; }
.bubble th { background: var(--bg-secondary); font-weight: 600; }
.bubble pre {
  background: var(--code-bg); color: #e6edf3; border-radius: 10px;
  padding: 11px 13px; overflow-x: auto; margin: 8px 0; position: relative; font-size: 13px;
}
.bubble pre code { background: transparent; padding: 0; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; line-height: 1.5; }
.bubble code { background: rgba(127,127,127,.15); border-radius: 5px; padding: 1px 5px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; }
.bubble pre code { background: transparent; padding: 0; }
.code-copy {
  position: absolute; top: 6px; right: 6px; z-index: 2;
  background: rgba(255,255,255,.12); color: #e6edf3; border: none; border-radius: 7px;
  font-size: 11px; padding: 4px 9px; cursor: pointer;
}
.code-copy:active { opacity: .6; }

/* tool call cards */
.tool-card { margin: 8px 0; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; background: var(--bg); }
.tool-card summary {
  list-style: none; display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; font-size: 13.5px; font-weight: 600; cursor: pointer; color: var(--text);
}
.tool-card summary::-webkit-details-marker { display: none; }
.tool-card summary .chev { margin-left: auto; color: var(--hint); transition: transform .15s; }
.tool-card[open] summary .chev { transform: rotate(90deg); }
.tool-args { padding: 8px 12px; border-top: 1px solid var(--border); font-family: ui-monospace, Menlo, monospace; font-size: 12px; color: var(--hint); white-space: pre-wrap; word-break: break-all; max-height: 200px; overflow: auto; }
.tool-result { padding: 10px 12px; border-top: 1px solid var(--border); font-family: ui-monospace, Menlo, monospace; font-size: 12px; white-space: pre-wrap; word-break: break-word; max-height: 320px; overflow: auto; background: var(--bg-secondary); color: var(--text); }
.tool-result.err { color: var(--destructive); }

/* composer */
.composer {
  display: flex; gap: 8px; align-items: flex-end;
  padding: 8px 10px calc(env(safe-area-inset-bottom, 0px) + 10px);
  border-top: 1px solid var(--border); background: var(--bg);
  position: sticky; bottom: 0;
}
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--border); border-radius: 14px;
  background: var(--bg-secondary); color: var(--text); font-size: 15px;
  padding: 10px 14px; outline: none; max-height: 120px; font-family: inherit;
}
.composer textarea:focus { border-color: var(--accent); }
.composer button {
  width: 46px; height: 46px; border-radius: 50%; border: none; flex-shrink: 0;
  background: var(--accent); color: var(--accent-text); font-size: 17px; cursor: pointer;
}
.composer button:disabled { opacity: .45; }

/* status / empty */
.status { text-align: center; color: var(--hint); padding: 30px 20px; font-size: 14px; }
.spinner {
  display: inline-block; width: 18px; height: 18px; border: 2.5px solid var(--hint);
  border-top-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; vertical-align: -3px; margin-right: 8px;
}
@keyframes spin { to { transform: rotate(360deg); } }
.err-banner { background: rgba(229,57,53,.12); color: var(--destructive); border-radius: 10px; padding: 10px 14px; margin: 10px 14px; font-size: 13.5px; }

/* ==================== HERMES CONTROL CENTER ==================== */
.page { display: flex; flex-direction: column; min-height: 100dvh; padding-bottom: calc(64px + env(safe-area-inset-bottom, 0px)); }
.page.hidden { display: none !important; }

/* status pill */
.status-pill { text-align: center; color: var(--hint); padding: 10px 14px; font-size: 13px; min-height: 20px; }

/* cards */
.card { background: var(--bg-secondary); border-radius: var(--radius); margin: 8px 12px; padding: 12px 14px; }
.card-title { font-size: 12px; font-weight: 700; letter-spacing: .7px; color: var(--hint); text-transform: uppercase; margin-bottom: 8px; }
.card-body { font-size: 14px; }
.card-body .row { margin-bottom: 7px; }
.card-body .row:last-child { margin-bottom: 0; }

/* rows */
.row { display: flex; align-items: center; gap: 8px; font-size: 14px; padding: 7px 0; }
.row-title { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 500; }
.row-sub { flex-basis: 100%; font-size: 12.5px; color: var(--hint); margin-left: 0; padding-left: 0; }
.row-time { font-size: 12px; color: var(--hint); flex-shrink: 0; }
.row-right { margin-left: auto; font-size: 12.5px; color: var(--hint); flex-shrink: 0; }
.muted { color: var(--hint); font-size: 13px; }

/* badges */
.badge { font-size: 10.5px; font-weight: 700; padding: 2.5px 8px; border-radius: 20px; flex-shrink: 0; text-transform: uppercase; letter-spacing: .3px; }
.badge.running, .badge.active { background: rgba(76,175,80,.16); color: #43a047; }
.badge.done { background: rgba(127,127,127,.14); color: var(--hint); }
.badge.off { background: rgba(127,127,127,.14); color: var(--hint); }
.badge.waiting { background: rgba(255,152,0,.16); color: #f57c00; }

/* status dots */
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--hint); flex-shrink: 0; }
.dot.ok { background: #43a047; box-shadow: 0 0 6px rgba(76,175,80,.6); }
.dot.bad { background: var(--destructive); }

/* mini buttons */
.mini-btn { background: var(--bg); border: 1px solid var(--border); color: var(--text); border-radius: 9px; padding: 5px 9px; font-size: 12.5px; cursor: pointer; flex-shrink: 0; }
.mini-btn:active { opacity: .6; }

/* memory */
.mem-file { font-size: 12px; font-weight: 700; color: var(--accent); padding: 12px 6px 5px; text-transform: uppercase; letter-spacing: .5px; }
.mem-line { font-size: 13.5px; padding: 3px 6px; line-height: 1.45; border-bottom: 1px solid var(--border); word-break: break-word; }
.mem-line:last-child { border-bottom: none; }

/* bottom nav */
#bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 100;
  display: flex; justify-content: space-around; align-items: stretch;
  background: var(--bg); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  max-width: var(--maxw); margin: 0 auto;
}
.nav-btn {
  flex: 1; background: none; border: none; color: var(--hint); cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 9px 0 7px; font-size: 20px; font-family: inherit;
}
.nav-btn span { font-size: 10px; font-weight: 600; }
.nav-btn.active { color: var(--accent); }

/* more sheet */
.sheet-overlay {
  position: fixed; inset: 0; z-index: 200; background: rgba(0,0,0,.45);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet {
  background: var(--bg); width: 100%; max-width: var(--maxw);
  border-radius: 16px 16px 0 0; padding: 10px 12px calc(env(safe-area-inset-bottom,0px) + 10px);
  display: flex; flex-direction: column; gap: 4px;
}
.sheet-btn {
  background: var(--bg-secondary); border: none; color: var(--text); border-radius: 12px;
  padding: 14px; font-size: 15px; text-align: left; cursor: pointer; font-family: inherit;
}
.sheet-btn:active { opacity: .6; }

/* conversation view fix (no bottom nav overlap) */
#view-conversation { padding-bottom: 0; }
