:root {
  --bg: #0f1117;
  --bg2: #161923;
  --bg3: #1e2230;
  --border: #2a2f40;
  --text: #e6e9f2;
  --muted: #8b93a8;
  --accent: #c9a227;
  --accent2: #e8c14d;
  --ok: #3fbf7f;
  --warn: #e0a13f;
  --err: #e05f5f;
  --info: #5b8def;
}

* { box-sizing: border-box; }
html, body { height: 100%; overflow-x: hidden; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 "Segoe UI", system-ui, sans-serif;
}
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 13px; }
a { color: var(--accent2); }
code { background: var(--bg3); padding: 1px 5px; border-radius: 4px; font-size: 13px; }
pre {
  background: var(--bg3); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px; font-size: 13px;
  /* Long tool output (PowerShell tables etc.) wraps inside the visible area
     instead of forcing a horizontal scrollbar — scroll stays vertical only. */
  white-space: pre-wrap; word-break: break-word; overflow-wrap: anywhere;
  overflow-x: hidden;
}

/* login */
.login-body { display: flex; align-items: center; justify-content: center; }
.login-card {
  width: 340px; background: var(--bg2); border: 1px solid var(--border);
  border-radius: 14px; padding: 32px;
}
.brand-login { font-size: 26px; font-weight: 700; color: var(--accent2); text-align: center; }
.subtitle { text-align: center; color: var(--muted); margin-top: 4px; }
.login-card form { display: flex; flex-direction: column; gap: 12px; margin-top: 20px; }
input, select, textarea {
  background: var(--bg); border: 1px solid var(--border); color: var(--text);
  border-radius: 8px; padding: 9px 11px; font: inherit;
}
input:focus, select:focus, textarea:focus { outline: 1px solid var(--accent); }
.error { color: var(--err); font-size: 13px; margin-top: 8px; }

/* topbar */
.topbar {
  display: flex; align-items: center; gap: 18px;
  padding: 0 16px; height: 52px;
  background: var(--bg2); border-bottom: 1px solid var(--border);
  transition: transform 0.25s ease;
}
.brand { font-weight: 700; color: var(--accent2); white-space: nowrap; }
.nav-tabs { display: flex; gap: 6px; flex: 1; }
.tab {
  background: transparent; border: none; color: var(--muted);
  padding: 7px 13px; border-radius: 8px; cursor: pointer; font: inherit;
}
.tab.active { background: var(--bg3); color: var(--text); }
.badge { background: var(--err); color: #fff; border-radius: 10px; padding: 0 7px; font-size: 12px; margin-left: 4px; }
.top-right { display: flex; align-items: center; gap: 10px; }
.pill { border-radius: 12px; padding: 4px 12px; font-size: 13px; border: 1px solid var(--border); }
.pill.on { background: rgba(63,191,127,.15); color: var(--ok); border-color: var(--ok); }
.pill.off { background: rgba(224,161,63,.12); color: var(--warn); border-color: var(--warn); }

/* layout */
.layout { display: flex; height: calc(100vh - 52px); }
.sidebar {
  width: 270px; min-width: 270px; border-right: 1px solid var(--border);
  background: var(--bg2); padding: 12px; overflow-y: auto; display: flex; flex-direction: column; gap: 10px;
}
.conv-list { display: flex; flex-direction: column; gap: 4px; }
.conv-item {
  padding: 9px 11px; border-radius: 9px; cursor: pointer; border: 1px solid transparent;
}
.conv-item:hover { background: var(--bg3); }
.conv-item.active { background: var(--bg3); border-color: var(--accent); }
.conv-title { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-meta { color: var(--muted); font-size: 12px; }

.content { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.view { flex: 1; display: flex; flex-direction: column; padding: 16px; overflow-y: auto; }
.view > h2 { margin-top: 0; }

/* Chat view: only the message area scrolls; the composer stays pinned. */
#view-chat { padding: 0; overflow: hidden; }
#view-chat .composer { flex-shrink: 0; }

/* chat */
.chat-scroll { flex: 1; overflow-y: auto; padding: 0 4px 8px; display: flex; flex-direction: column; gap: 10px; }
.chat-empty { margin: auto; text-align: center; color: var(--muted); font-size: 17px; line-height: 2; }
.msg { display: flex; }
.msg.user { justify-content: flex-end; }
.msg.assistant { justify-content: flex-start; flex-direction: column; align-items: stretch; }
.bubble {
  max-width: 78%; padding: 10px 14px; border-radius: 14px; white-space: normal;
  word-wrap: break-word; overflow-wrap: anywhere;
}
.user-bubble { background: var(--accent); color: #17130a; border-bottom-right-radius: 4px; }
.ai-bubble { background: var(--bg3); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.assistant .bubble { max-width: 100%; }

/* thinking indicator */
#thinking-indicator { display: flex; justify-content: flex-start; }
.thinking { display: inline-flex; align-items: center; gap: 7px; }
.thinking .dot {
  width: 7px; height: 7px; border-radius: 50%; background: var(--accent);
  animation: think-bounce 1.2s infinite ease-in-out;
}
.thinking .dot:nth-child(2) { animation-delay: .15s; }
.thinking .dot:nth-child(3) { animation-delay: .3s; }
.thinking-text { color: var(--muted); font-size: 13px; }
.thinking-usage { color: var(--muted); font-size: 12px; opacity: .8; }
@keyframes think-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: .45; }
  30% { transform: translateY(-5px); opacity: 1; }
}

/* shimmer progress bar - pinned at the top of the chat view, outside the
   scroll container, so it stays visible on every screen (incl. phones) */
#thinking-bar {
  position: relative; /* containing block for the absolute ::after shimmer */
  flex-shrink: 0;
  height: 3px; overflow: hidden;
  background: var(--bg);
}
#thinking-bar.active::after {
  content: ''; position: absolute; top: 0; bottom: 0; width: 40%;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  animation: think-slide 1.2s ease-in-out infinite;
}
@keyframes think-slide { 0% { left: -40%; } 100% { left: 100%; } }

.composer {
  display: flex; flex-wrap: wrap; gap: 10px; padding: 12px 16px;
  border-top: 1px solid var(--border); background: var(--bg2);
}
.composer textarea {
  flex: 1; resize: none;
  background-color: var(--bg);
  background-image: linear-gradient(90deg, rgba(232,193,77,0.10), rgba(232,193,77,0.10));
  background-repeat: no-repeat;
  background-position: left center;
  background-size: 0% 100%;
  transition: background-size .5s ease;
}
.composer textarea.ctx-warn {
  background-image: linear-gradient(90deg, rgba(232,193,77,0.17), rgba(232,193,77,0.17));
}
.composer textarea.ctx-full {
  background-image: linear-gradient(90deg, rgba(240,150,40,0.24), rgba(240,150,40,0.24));
}
.ctx-label {
  flex: 1 0 100%;
  font-size: 11px;
  color: var(--muted);
  text-align: right;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  line-height: 1.2;
  margin-top: -2px;
}
.ctx-label b { color: var(--accent2); font-weight: 600; }
.send-btn { padding: 8px 14px; font-size: 13px; line-height: 1; }

/* tool cards */
.tool-card {
  background: var(--bg2); border: 1px solid var(--border); border-radius: 10px;
  padding: 12px; margin: 4px 0; max-width: 100%;
}
.tool-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.tool-status { font-size: 12px; border-radius: 8px; padding: 1px 8px; }
.st-done { background: rgba(63,191,127,.15); color: var(--ok); }
.st-running { background: rgba(91,141,239,.15); color: var(--info); }
.st-queued, .st-pending_approval { background: rgba(224,161,63,.12); color: var(--warn); }
.st-failed, .st-denied, .st-expired, .st-cancelled { background: rgba(224,95,95,.12); color: var(--err); }
.tool-args { max-height: 160px; overflow-y: auto; margin: 0 0 8px; }
.task-result { max-height: 260px; overflow-y: auto; margin: 0 0 8px; white-space: pre-wrap; }
.tool-actions { display: flex; gap: 8px; }

/* buttons */
button { cursor: pointer; font: inherit; }
.primary-btn {
  background: var(--accent); color: #17130a; border: none;
  border-radius: 8px; padding: 9px 16px; font-weight: 600;
}
.primary-btn:hover { background: var(--accent2); }
.primary-btn.working { opacity: .65; }
.danger-btn { background: transparent; border: 1px solid var(--err); color: var(--err); border-radius: 8px; padding: 8px 14px; }
.danger-btn:hover { background: rgba(224,95,95,.12); }
.ghost-btn { background: transparent; border: 1px solid var(--border); color: var(--muted); border-radius: 8px; padding: 7px 13px; }
.ghost-btn:hover { color: var(--text); }
.small { padding: 4px 10px; font-size: 13px; }
.full { width: 100%; }

/* tables + forms */
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; word-break: break-word; }
th { color: var(--muted); font-weight: 600; }
.settings-form { display: flex; flex-direction: column; gap: 10px; max-width: 640px; margin-bottom: 28px; }
.setting-row { display: flex; align-items: center; gap: 14px; justify-content: space-between; }
.setting-label { display: flex; flex-direction: column; }
.setting-label small { color: var(--muted); font-size: 12px; }
.settings-form input[type="number"] { width: 90px; }
.settings-form input[type="checkbox"] { width: 18px; height: 18px; }
.settings-form textarea { width: 380px; }
.token-form { display: flex; gap: 8px; margin: 12px 0; max-width: 640px; }
.token-form input { flex: 1; }
.token-reveal {
  background: rgba(63,191,127,.1); border: 1px solid var(--ok); border-radius: 10px;
  padding: 12px; margin: 8px 0; max-width: 640px; word-break: break-all;
}

.rule-list { display: flex; flex-direction: column; gap: 6px; }
.rule-chip {
  display: inline-flex; align-items: center; gap: 8px;
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: 8px; padding: 4px 10px; font-size: 13px;
}

/* ---- responsive: phones ---- */
.sidebar-toggle { display: none; }
@media (max-width: 768px) {
  .sidebar-toggle { display: inline-flex; align-items: center; }
  .brand { font-size: 15px; }
  .nav-tabs { gap: 2px; }
  .tab { padding: 6px 9px; font-size: 13px; }
  .topbar { gap: 8px; padding: 0 8px; flex-wrap: nowrap; }
  .topbar.hide { transform: translateY(-100%); }
  .topbar.hide + .layout { margin-top: -52px; } /* reclaim the hidden bar's space */

  /* PC status: compact colored dot instead of the full pill text */
  .pill {
    width: 12px; height: 12px; min-width: 12px;
    padding: 0; font-size: 0; overflow: hidden;
    border-radius: 50%; border-width: 2px;
  }

  .nav-tabs { overflow-x: auto; flex-shrink: 1; min-width: 0; scrollbar-width: none; }
  .nav-tabs::-webkit-scrollbar { display: none; }
  .brand { flex-shrink: 0; }
  .top-right { gap: 6px; flex-shrink: 0; }
  .top-right .pill { font-size: 11px; padding: 3px 8px; white-space: nowrap; }
  .ghost-btn { padding: 6px 10px; font-size: 13px; }

  .layout { position: relative; height: calc(100vh - 52px); transition: margin-top 0.25s ease; }
  .sidebar {
    position: absolute; z-index: 30; top: 0; bottom: 0; left: 0;
    width: 78%; max-width: 300px; min-width: 0;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 2px 0 14px rgba(0, 0, 0, 0.55);
  }
  .sidebar.open { transform: translateX(0); }
  .content { width: 100%; }

  .chat-scroll { padding: 0 0 4px; }
  .bubble { max-width: 92%; }
  .composer { padding: 8px; gap: 6px; }
  .composer textarea { font-size: 16px; } /* prevents iOS zoom on focus */

  .settings-form textarea { width: 100%; }
  .setting-row { flex-wrap: wrap; }
  .token-form { flex-wrap: wrap; }
  table { font-size: 12px; }
  th, td { padding: 6px 4px; }
}

/* toast */
.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: var(--bg3); border: 1px solid var(--accent); color: var(--text);
  padding: 10px 18px; border-radius: 10px; z-index: 99; max-width: 80%;
}
.toast-error { border-color: var(--err); }

/* ---- voice ---- */
.voice-btn {
  background: var(--bg3); border: 1px solid var(--border); color: var(--text);
  border-radius: 8px; padding: 7px 11px; font-size: 12px; line-height: 1;
}
.voice-btn:hover { border-color: var(--accent); }
.voice-btn.recording {
  background: rgba(224,95,95,.18); border-color: var(--err); color: var(--err);
  animation: voice-pulse 1.1s infinite ease-in-out;
}
@keyframes voice-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(224, 95, 95, .45); }
  50% { box-shadow: 0 0 0 6px rgba(224, 95, 95, 0); }
}
/* Compact language switcher: the closed control shows only the native
   dropdown arrow (the label lives in the title tooltip + options list). */
.voice-lang {
  width: 34px; padding: 8px 0; font-size: 13px; line-height: 1;
  color: transparent; overflow: hidden;
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
}
.voice-lang option { color: var(--text); }

/* spoken-message analysis chip + user message body alignment */
.msg-body { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; max-width: 78%; }
.user-body { align-items: flex-end; }
.voice-chip {
  font-size: 12px; color: var(--muted); background: var(--bg3);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 2px 10px; white-space: normal;
}

/* voice reply play/pause button (one engine — autoplay and manual share it) */
.voice-play-btn {
  width: 36px; height: 30px; margin-top: 6px;
  background: var(--bg3); border: 1px solid var(--border); color: var(--text);
  border-radius: 10px; cursor: pointer; font-size: 12px; line-height: 1;
}
.voice-play-btn:hover { border-color: var(--accent); }
.voice-play-btn.playing { background: rgba(201, 162, 39, .16); border-color: var(--accent); color: var(--accent2); }

/* "producing voice…" indicator while the reply audio is synthesizing */
.audio-pending {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 12px; color: var(--muted);
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: 10px; padding: 4px 12px; margin-top: 6px;
}
.audio-pending .bar {
  width: 3px; height: 5px; background: var(--accent); border-radius: 2px;
  animation: audio-bar 0.9s infinite ease-in-out;
}
.audio-pending .bar:nth-child(2) { animation-delay: .15s; }
.audio-pending .bar:nth-child(3) { animation-delay: .3s; }
@keyframes audio-bar { 0%, 100% { height: 4px; opacity: .5; } 50% { height: 13px; opacity: 1; } }

@media (max-width: 768px) {
  .voice-lang { width: 30px; padding: 7px 0; }
  .msg-body { max-width: 92%; }
}


/* ---- images (upload + generation) ---- */
.mic-upload-stack { display: flex; flex-direction: column; gap: 3px; }
.voice-btn-top { border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; }
.upload-btn { border-top-left-radius: 4px; border-top-right-radius: 4px; font-size: 11px; padding: 6px 11px; }

.chat-image { position: relative; display: inline-block; margin: 6px 0; max-width: 320px; }
.chat-image img {
  display: block; max-width: 100%; max-height: 260px; width: auto; height: auto;
  border-radius: 12px; border: 1px solid var(--border); cursor: zoom-in;
}
.chat-image figcaption { font-size: 12px; color: var(--muted); margin-top: 4px; word-break: break-word; }
.img-actions {
  position: absolute; right: 6px; bottom: 6px;
  display: flex; gap: 4px; opacity: 0; transition: opacity .15s ease;
}
.chat-image:hover .img-actions, .chat-image:focus-within .img-actions { opacity: 1; }
.img-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 30px; height: 30px; border-radius: 8px;
  background: rgba(15,17,23,.72); border: 1px solid var(--border);
  color: var(--text); text-decoration: none; font-size: 13px; cursor: pointer;
}
.img-btn:hover { border-color: var(--accent); color: var(--accent2); }

/* lightbox */
.lightbox {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(8,9,14,.92);
  display: flex; align-items: center; justify-content: center;
}
.lightbox-inner { position: relative; max-width: 94vw; max-height: 94vh; }
.lightbox-inner img {
  max-width: 94vw; max-height: 88vh; border-radius: 10px;
  box-shadow: 0 6px 40px rgba(0,0,0,.6);
}
.lightbox-actions { position: absolute; right: 8px; bottom: 8px; display: flex; gap: 6px; }
.lightbox-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; border-radius: 10px;
  background: rgba(15,17,23,.8); border: 1px solid var(--border);
  color: var(--text); font-size: 17px; cursor: pointer; text-decoration: none;
}
.lightbox-btn:hover { border-color: var(--accent); color: var(--accent2); }