:root {
  --bg: #0f1115; --panel: #171a21; --panel2: #1e222b; --line: #2a2f3a;
  --text: #e6e8ec; --muted: #9aa3b2; --accent: #4f8cff; --accent2: #2f6fd6;
  --user: #243044; --ok: #3ad29f; --err: #ff6b6b;
}
* { box-sizing: border-box; }
body { margin: 0; font: 15px/1.5 system-ui, sans-serif; background: var(--bg); color: var(--text); }
a { color: var(--accent); text-decoration: none; }

/* Auth */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; }
.auth-card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 32px; width: 340px; }
.auth-card h1 { margin: 0 0 4px; }
.tagline { color: var(--muted); margin: 0 0 20px; }
.auth-card label { display: block; margin-bottom: 12px; font-size: 13px; color: var(--muted); }
.auth-card input { width: 100%; margin-top: 4px; padding: 10px; background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; color: var(--text); }
.auth-card button { width: 100%; padding: 11px; margin-top: 6px; background: var(--accent); border: 0; border-radius: 8px; color: #fff; font-weight: 600; cursor: pointer; }
.muted { color: var(--muted); font-size: 13px; }
.error { background: #3a1f24; border: 1px solid #5c2b33; color: #ffb4b4; padding: 10px; border-radius: 8px; margin-bottom: 14px; font-size: 13px; }

/* App layout */
.app { display: grid; grid-template-columns: 260px 1fr; height: 100vh; }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); padding: 14px; display: flex; flex-direction: column; gap: 10px; overflow: hidden; }
.new-chat { display: block; text-align: center; padding: 10px; background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; color: var(--text); }
.chat-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; }
.chat-row { display: flex; align-items: center; border-radius: 8px; }
.chat-row:hover { background: var(--panel2); }
.chat-row.active { background: var(--panel2); }
.chat-link { flex: 1; padding: 9px 10px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-row.active .chat-link { color: var(--text); }
.chat-rename { opacity: 0; background: transparent; border: 0; color: var(--muted); cursor: pointer; padding: 4px 6px; display: flex; align-items: center; }
.chat-row:hover .chat-rename { opacity: .7; }
.chat-rename:hover { opacity: 1; color: var(--accent); }
.chat-del { display: flex; }
.chat-del button { opacity: 0; background: transparent; border: 0; color: var(--muted); cursor: pointer; padding: 4px 8px; font-size: 12px; }
.chat-row:hover .chat-del button { opacity: .7; }
.chat-del button:hover { opacity: 1; color: var(--err); }
.chat-rename-input { flex: 1; min-width: 0; margin: 4px 6px; padding: 5px 8px; background: var(--bg); border: 1px solid var(--accent); border-radius: 6px; color: var(--text); font: inherit; font-size: 13px; }
.credits { display: block; background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 10px; text-decoration: none; }
.credits:hover { border-color: var(--accent); }
.credits-top { display: flex; justify-content: space-between; align-items: center; }
.credits-label { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.credits-tier { font-size: 10px; font-weight: 700; padding: 1px 6px; border-radius: 10px; background: var(--line); color: var(--text); text-transform: uppercase; }
.credits-tier.tier-core { background: #2f6fd6; color: #fff; }
.credits-tier.tier-pro { background: #8b5cf6; color: #fff; }
.credits-value { font-size: 18px; font-weight: 700; color: var(--text); margin-top: 2px; }
.credits-sub { font-size: 10px; color: var(--accent); margin-top: 3px; }
.bubble.lockout { background: #3a1f24 !important; border: 1px solid #5c2b33 !important; color: #ffb4b4; }
.logout button { width: 100%; padding: 9px; background: transparent; border: 1px solid var(--line); border-radius: 8px; color: var(--muted); cursor: pointer; }

.chat { display: flex; flex-direction: column; height: 100vh; }
.disclaimer { background: #1a1d26; color: var(--muted); font-size: 12px; padding: 8px 16px; border-bottom: 1px solid var(--line); text-align: center; }
.messages { flex: 1; overflow-y: auto; padding: 24px; display: flex; flex-direction: column; gap: 16px; }

.msg { display: flex; flex-direction: column; }
.msg.user { align-items: flex-end; }
.msg .bubble { max-width: 760px; padding: 12px 16px; border-radius: 14px; }
.msg.user .bubble { background: var(--user); }
.msg.bot .bubble { background: var(--panel); border: 1px solid var(--line); width: 100%; max-width: 820px; }
.msg.bot.error .bubble { border-color: #5c2b33; color: #ffb4b4; }

/* Per-message hover action bar (beneath the bubble) */
.msg-actions { display: flex; gap: 2px; margin-top: 4px; opacity: 0; transition: opacity .12s; }
.msg.user .msg-actions { justify-content: flex-end; }
.msg:hover .msg-actions { opacity: 1; }
.msg-act { display: inline-flex; align-items: center; gap: 4px; background: transparent; border: 0; color: var(--muted); cursor: pointer; font-size: 11px; padding: 3px 7px; border-radius: 6px; }
.msg-act:hover { background: var(--panel2); color: var(--text); }
.msg-act[data-act="delete"]:hover { color: var(--err); }
.act-ico { width: 13px; height: 13px; flex-shrink: 0; }

/* Inline edit box for user messages */
.msg-edit { width: 100%; max-width: 760px; display: flex; flex-direction: column; gap: 6px; }
.msg-edit-input { width: 100%; min-height: 60px; padding: 10px; background: var(--panel2); border: 1px solid var(--accent); border-radius: 10px; color: var(--text); font: inherit; resize: vertical; }
.msg-edit-row { display: flex; gap: 6px; justify-content: flex-end; }
.msg-edit-save { background: var(--accent); color: #fff; border: 0; border-radius: 8px; padding: 6px 12px; cursor: pointer; font-size: 13px; }
.msg-edit-cancel { background: transparent; color: var(--muted); border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; cursor: pointer; font-size: 13px; }
.answer :first-child { margin-top: 0; } .answer :last-child { margin-bottom: 0; }

/* Collapsible, opt-in agent activity panel */
/* Per-tool inline widgets (the conversation IS the activity timeline) */
.activity-flow { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.activity-flow:empty { margin: 0; }
.widget { background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.widget-head { display: flex; align-items: center; gap: 8px; padding: 8px 12px; font-size: 13px; cursor: pointer; user-select: none; }
.widget-head:hover { background: rgba(255,255,255,.03); }
.widget-caret { color: var(--muted); font-size: 10px; transition: transform .15s; }
.widget:not(.open) .widget-caret { transform: rotate(-90deg); }
.widget-icon { font-size: 14px; }
.widget-title { font-weight: 600; color: var(--text); }
.widget-body { padding: 0 12px 10px 34px; font-size: 13px; color: var(--muted); display: flex; flex-direction: column; gap: 6px; }
.widget-body:empty { display: none; }
/* collapsed widgets hide their body; header (with caret + ✓) stays visible */
.widget:not(.open) .widget-body { display: none; }
/* nested sub-tool widgets (e.g. a specialist's case search) indent inside parent */
.widget .widget { margin-top: 6px; background: var(--panel); }

.widget-spin { margin-left: auto; width: 10px; height: 10px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
.widget-spin.hidden { display: none; }
.widget.done .widget-head .widget-title::after { content: " ✓"; color: var(--ok); font-weight: 400; }
@keyframes spin { to { transform: rotate(360deg); } }

.w-query { color: var(--text); font-style: italic; }
.w-note { color: var(--muted); font-size: 12px; }
.w-hits { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.w-hits li { display: flex; flex-direction: column; }
.w-hit-title { color: var(--accent); }
.w-hit-meta { color: var(--muted); font-size: 12px; }
.w-passage { display: flex; gap: 8px; }
.w-page { color: var(--accent); flex-shrink: 0; }
.w-weblink { color: var(--accent); display: block; }
.w-spec-status { color: var(--text); }
/* Prettified browser steps */
.w-steps { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 5px; }
.w-step { display: flex; align-items: baseline; flex-wrap: wrap; gap: 5px; color: var(--muted); font-size: 12px; }
.w-step-icon { width: 14px; height: 14px; border-radius: 50%; flex-shrink: 0; display: inline-block; position: relative; top: 2px; }
.w-step-icon.nav   { background: #2f6fd6; }
.w-step-icon.type  { background: #4f8cff; }
.w-step-icon.click { background: #8b5cf6; }
.w-step-icon.read  { background: #64748b; }
.w-step-icon.ok    { background: var(--ok); }
.w-step-icon.warn, .w-step-icon.err { background: var(--err); }
.w-step-label { color: var(--text); }
.w-step-val { color: var(--accent); font-weight: 600; }
.w-step-host { color: var(--accent); font-weight: 600; }
.w-step-field { color: var(--text); background: var(--panel); padding: 0 5px; border-radius: 4px; }
.w-step-into { color: var(--muted); font-weight: 400; }

/* Citations */
.citations { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }
.citations-title { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin-bottom: 6px; }
.citations ol { margin: 0; padding-left: 20px; }
.citations li { margin: 6px 0; font-size: 13px; }
.cite-meta { display: block; color: var(--muted); font-size: 12px; }
.verified { color: var(--ok); margin-left: 6px; }

/* Composer */
.composer { display: flex; gap: 8px; padding: 14px 24px; border-top: 1px solid var(--line); align-items: flex-end; }
.upload { cursor: pointer; font-size: 20px; padding: 8px; align-self: center; }
.composer textarea { flex: 1; resize: none; padding: 12px; background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; color: var(--text); font: inherit; max-height: 160px; }
.composer button { display: inline-flex; align-items: center; justify-content: center; gap: 7px; padding: 12px 18px; background: var(--accent); border: 0; border-radius: 10px; color: #fff; font-weight: 600; cursor: pointer; line-height: 1; }
.composer button:hover:not(:disabled) { background: var(--accent2); }
.composer button:disabled { opacity: .5; cursor: default; }
.send-ico { width: 16px; height: 16px; }
.upload-status { padding: 0 24px 10px; color: var(--muted); font-size: 12px; min-height: 16px; }

/* ===================== Mobile / responsive ===================== */
/* Mobile top bar + drawer overlay are hidden on desktop. */
.mobile-bar { display: none; }
.sidebar-overlay { display: none; }

@media (max-width: 768px) {
  /* Single column; sidebar becomes an off-canvas drawer. */
  .app { grid-template-columns: 1fr; }

  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: 80%; max-width: 300px;
    z-index: 40;
    transform: translateX(-100%);
    transition: transform .22s ease;
    border-right: 1px solid var(--line);
  }
  .sidebar.open { transform: translateX(0); }

  .sidebar-overlay { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.5); opacity: 0; pointer-events: none; transition: opacity .22s; z-index: 30; }
  .sidebar-overlay.show { opacity: 1; pointer-events: auto; }

  /* Mobile top bar with hamburger. */
  .mobile-bar { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-bottom: 1px solid var(--line); background: var(--panel); }
  .menu-toggle { background: transparent; border: 0; color: var(--text); cursor: pointer; display: flex; padding: 4px; }
  .mobile-title { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .mobile-new { color: var(--text); font-size: 22px; line-height: 1; padding: 2px 8px; text-decoration: none; }

  /* Touch has no hover: show message actions + chat delete permanently. */
  .msg-actions { opacity: 1 !important; }
  .chat-del button { opacity: .7 !important; }

  /* Roomier tap targets. */
  .msg-act { font-size: 12px; padding: 6px 9px; }

  /* Use more width for bubbles + tighter page padding. */
  .messages { padding: 14px; gap: 14px; }
  .msg .bubble { max-width: 100%; }
  .composer { padding: 10px 12px; }
  .composer button { padding: 12px 14px; }
  .send-label { display: none; }  /* icon-only send on small screens */
  .upload-status { padding: 0 12px 8px; }
}

/* ===================== Billing / profile page ===================== */
.billing-wrap { min-height: 100vh; overflow-y: auto; padding: 32px 16px; }
.billing { max-width: 720px; margin: 0 auto; }
.billing h1 { margin: 8px 0 20px; }
.billing h2 { font-size: 16px; margin: 0 0 12px; }
.back-link { color: var(--muted); font-size: 13px; text-decoration: none; }
.back-link:hover { color: var(--text); }
.banner { padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; font-size: 14px; background: var(--panel2); border: 1px solid var(--line); color: var(--muted); }
.banner.ok { background: #16311f; border-color: #275c3a; color: #9ff0c0; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 20px; margin-bottom: 16px; }
.card-head { display: flex; align-items: center; justify-content: space-between; }
.tier-badge { font-size: 11px; font-weight: 700; padding: 3px 10px; border-radius: 12px; background: var(--line); color: var(--text); text-transform: uppercase; }
.tier-badge.tier-core { background: #2f6fd6; color: #fff; }
.tier-badge.tier-pro { background: #8b5cf6; color: #fff; }

.credit-meter { margin: 14px 0; }
.credit-fig { font-size: 20px; font-weight: 700; margin-bottom: 6px; }
.bar { height: 8px; background: var(--panel2); border-radius: 5px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--accent); }

.tiers { display: flex; gap: 14px; flex-wrap: wrap; }
.tier-col { flex: 1; min-width: 180px; border: 1px solid var(--line); border-radius: 10px; padding: 16px; text-align: center; }
.tier-col.current { border-color: var(--accent); }
.tier-name { font-weight: 700; font-size: 15px; }
.tier-price { font-size: 26px; font-weight: 800; margin: 6px 0; }
.tier-price span { font-size: 13px; font-weight: 500; color: var(--muted); }
.tier-credits { color: var(--muted); font-size: 13px; margin-bottom: 14px; }
.tier-btn { display: block; padding: 9px; border-radius: 8px; background: var(--accent); color: #fff; border: 0; text-decoration: none; cursor: pointer; font-weight: 600; }
.tier-btn:disabled { background: var(--panel2); color: var(--muted); cursor: default; }
.tier-btn.ghost { background: transparent; border: 1px solid var(--line); color: var(--muted); }
.tier-btn.ghost:hover { border-color: var(--accent); color: var(--text); }

.topup { display: flex; align-items: flex-end; gap: 14px; flex-wrap: wrap; }
.topup label { display: flex; flex-direction: column; font-size: 13px; color: var(--muted); gap: 4px; }
.topup input { padding: 10px; width: 120px; background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; color: var(--text); font: inherit; }
.topup-quote { font-size: 15px; padding-bottom: 10px; }
.topup button { padding: 10px 18px; background: var(--accent); color: #fff; border: 0; border-radius: 8px; cursor: pointer; font-weight: 600; }

.inline { display: inline; }
.link-btn { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 0; font: inherit; }
.card.danger { border-color: #5c2b33; }
.danger-btn { padding: 9px 16px; background: transparent; border: 1px solid var(--err); color: var(--err); border-radius: 8px; cursor: pointer; font-weight: 600; }
.danger-btn:hover { background: var(--err); color: #fff; }
