:root {
  --bg: #111417;
  --bg-alt: #151a1f;
  --panel: #1a2026;
  --panel-soft: #202830;
  --line: #2b333c;
  --line-strong: #3a4550;
  --text: #e7e2d7;
  --muted: #aca591;
  --accent: #8f7b56;
  --accent-soft: #3d3426;
  --ok: #5f8b73;
  --danger: #b46760;
  --shadow: 0 14px 40px rgba(0, 0, 0, 0.35);
  --shadow-soft: 0 8px 24px rgba(0, 0, 0, 0.24);
  --topbar-h: 84px;
  --composer-h: 112px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: "Inter", "Segoe UI", Roboto, Arial, sans-serif;
  background: linear-gradient(180deg, var(--bg) 0%, var(--bg-alt) 100%);
  color: var(--text);
}

body.sources-open {
  overflow: hidden;
}

.page {
  position: relative;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-top: calc(var(--topbar-h) + env(safe-area-inset-top));
  padding-bottom: calc(var(--composer-h) + env(safe-area-inset-bottom));
}

.topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 30;
  min-height: var(--topbar-h);
  padding: calc(10px + env(safe-area-inset-top)) 14px 10px;
  border-bottom: 1px solid var(--line);
  background: rgba(18, 22, 27, 0.95);
  backdrop-filter: blur(10px);
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.logo {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  overflow: hidden;
  flex-shrink: 0;
  border: 1px solid var(--line-strong);
  box-shadow: var(--shadow-soft);
  background: #161d24;
}

.logo-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.brandtext {
  min-width: 0;
}

.name {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 0.2px;
}

.tag {
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.top-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
}

.tab-btn,
.lang-toggle,
.send,
.sources-close,
.doc-btn,
.source a {
  min-height: 44px;
}

.tab-btn {
  border: 1px solid var(--line-strong);
  border-radius: 12px;
  background: var(--panel);
  color: var(--muted);
  padding: 0 14px;
  font-size: 13px;
  font-weight: 650;
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease, color 0.15s ease;
}

.tab-btn:hover {
  border-color: var(--accent);
  color: var(--text);
}

.tab-btn.active {
  background: #2a313a;
  border-color: var(--accent);
  color: var(--text);
}

.sources-mobile-btn {
  display: none;
}

.lang-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
}

.lang-label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.lang-toggle {
  border: 1px solid var(--line-strong);
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  padding: 0 10px;
  font-weight: 600;
}

.status {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-text,
.version-badge {
  font-size: 11px;
  color: var(--muted);
  font-weight: 600;
}

.version-badge {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 8px;
  background: #1f262d;
  white-space: nowrap;
}

.main {
  flex: 1;
  min-height: 0;
  width: min(1340px, calc(100% - 24px));
  margin: 12px auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 12px;
}

.main.library-mode {
  grid-template-columns: 1fr;
}

.hidden {
  display: none !important;
}

.chat-pane,
.chat,
.sources,
.library {
  min-height: 0;
}

.chat {
  border: 1px solid var(--line);
  border-radius: 18px;
  background: rgba(25, 31, 37, 0.93);
  overflow: auto;
  padding: 14px;
  box-shadow: var(--shadow);
}

.sources,
.library {
  border: 1px solid var(--line);
  border-radius: 18px;
  background: rgba(25, 31, 37, 0.93);
  overflow: auto;
  box-shadow: var(--shadow);
}

.library {
  padding: 14px;
}

.msg {
  max-width: 980px;
  margin: 10px auto;
  display: flex;
  gap: 12px;
  align-items: flex-start;
  opacity: 0;
  transform: translateY(6px);
  transition: opacity 0.18s ease, transform 0.18s ease;
}

.msg.show {
  opacity: 1;
  transform: translateY(0);
}

.role {
  width: 72px;
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.7px;
  padding-top: 10px;
  text-align: right;
  font-weight: 650;
}

.bubble {
  flex: 1;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px 13px;
  line-height: 1.56;
  box-shadow: var(--shadow-soft);
}

.user .bubble {
  background: #262d35;
  border-color: #4b5663;
}

.assistant .bubble {
  background: var(--panel);
}

.assistant .bubble.streaming {
  border-color: var(--accent);
}

.assistant .bubble.streaming::after {
  content: "";
  display: inline-block;
  width: 7px;
  height: 1.05em;
  margin-left: 4px;
  vertical-align: -0.2em;
  background: var(--accent);
  border-radius: 2px;
  animation: blinkCaret 0.9s steps(1) infinite;
}

.assistant .bubble p { margin: 0 0 9px; }
.assistant .bubble p:last-child { margin-bottom: 0; }
.assistant .bubble h1,.assistant .bubble h2,.assistant .bubble h3 { margin: 10px 0 6px; line-height: 1.3; }
.assistant .bubble h1 { font-size: 19px; }
.assistant .bubble h2 { font-size: 17px; }
.assistant .bubble h3 { font-size: 15px; }
.assistant .bubble ul,.assistant .bubble ol { margin: 6px 0 9px 20px; padding: 0; }
.assistant .bubble li { margin: 3px 0; }
.assistant .bubble hr { border: 0; border-top: 1px solid var(--line); margin: 10px 0; }

.sources-title-row {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  background: rgba(24, 30, 36, 0.96);
}

.sources-title {
  font-size: 14px;
  font-weight: 700;
}

.sources-close {
  margin-left: auto;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  padding: 0 12px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  display: none;
}

.sources-list {
  padding: 11px;
}

.sources-empty,
.library-empty {
  color: var(--muted);
  font-size: 13px;
}

.source {
  padding: 11px;
  border: 1px solid var(--line);
  border-radius: 13px;
  margin-bottom: 10px;
  background: var(--panel-soft);
}

.source .t {
  font-weight: 650;
  margin-bottom: 8px;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.source .meta {
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 9px;
}

.source .a {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.source a,
.doc-btn {
  color: var(--text);
  text-decoration: none;
  font-size: 12px;
  padding: 0 11px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--panel);
  display: inline-flex;
  align-items: center;
  cursor: pointer;
}

.source a:hover,
.doc-btn:hover:not(:disabled) {
  border-color: var(--accent);
  background: #2d3540;
}

.doc-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.library-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 12px;
}

.library-head h2 {
  margin: 0;
  font-size: 20px;
}

.library-head p {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.library-count {
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: #242c34;
  padding: 6px 10px;
  font-size: 12px;
  white-space: nowrap;
}

.library-list {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.library-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.library-group-header {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #242c34;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  cursor: pointer;
  min-height: 44px;
}

.library-group-title {
  font-size: 14px;
  font-weight: 700;
}

.library-group-count {
  margin-left: auto;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 11px;
  color: var(--muted);
}

.library-group-chevron {
  color: var(--muted);
  font-size: 14px;
  transition: transform 0.14s ease;
}

.library-group.collapsed .library-group-body { display: none; }
.library-group.collapsed .library-group-chevron { transform: rotate(-90deg); }

.library-group-body {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.library-subgroup {
  border-left: 2px solid var(--line);
  margin-left: 6px;
  padding-left: 10px;
}

.library-subgroup-header {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #20272e;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 10px;
  cursor: pointer;
  min-height: 42px;
}

.library-subgroup-title {
  font-size: 13px;
  font-weight: 680;
  color: var(--text);
}

.library-subgroup-count {
  margin-left: auto;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 11px;
  color: var(--muted);
}

.library-subgroup-chevron {
  color: var(--muted);
  font-size: 14px;
  transition: transform 0.14s ease;
}

.library-subgroup.collapsed .library-subgroup-grid { display: none; }
.library-subgroup.collapsed .library-subgroup-chevron { transform: rotate(-90deg); }

.library-subgroup-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 10px;
}

.doc-card {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel-soft);
  padding: 11px;
}

.doc-title {
  margin: 0 0 8px;
  font-size: 15px;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.doc-meta {
  margin: 0 0 9px;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.doc-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
}

.doc-summary {
  margin: 0;
  color: var(--text);
  font-size: 13px;
  line-height: 1.45;
  min-height: 2.6em;
}

.doc-summary.muted {
  color: var(--muted);
}

.composer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 40;
  border-top: 1px solid var(--line);
  background: rgba(18, 22, 27, 0.95);
  backdrop-filter: blur(10px);
  padding: 10px 12px calc(8px + env(safe-area-inset-bottom));
}

.composer-inner {
  width: min(1340px, 100%);
  margin: 0 auto;
  display: flex;
  gap: 10px;
  align-items: flex-end;
}

.input {
  flex: 1;
  resize: none;
  min-height: 48px;
  max-height: 180px;
  padding: 11px 13px;
  border-radius: 12px;
  border: 1px solid var(--line-strong);
  background: #13181d;
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
}

.input::placeholder {
  color: #8a8577;
}

.input:focus,
.send:focus,
.tab-btn:focus,
.lang-toggle:focus,
.sources-close:focus,
.doc-btn:focus,
.source a:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(143, 123, 79, 0.16);
}

.send {
  border-radius: 12px;
  border: 1px solid #7c6b4a;
  background: #8f7b56;
  color: #f3eee2;
  font-weight: 700;
  letter-spacing: 0.2px;
  padding: 0 16px;
  min-width: 96px;
  cursor: pointer;
}

.send:hover:not(:disabled) {
  filter: brightness(1.04);
}

.send:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.legal-disclaimer {
  width: min(1340px, 100%);
  margin: 8px auto 0;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.4;
  padding-left: 2px;
}

.sources-backdrop {
  position: fixed;
  inset: 0;
  z-index: 45;
  background: rgba(10, 12, 15, 0.56);
}

.status-orb {
  width: 16px;
  height: 16px;
  border-radius: 999px;
  background: radial-gradient(circle at 30% 30%, #fefcf8, #d3c8b0 40%, #89744b 78%, #5c5037);
  box-shadow: 0 4px 14px rgba(95, 79, 49, 0.32);
}

.status-orb[data-state="idle"] { animation: orbIdle 1.8s ease-in-out infinite; }
.status-orb[data-state="thinking"] { animation: orbSpin 1.1s linear infinite, orbWobble 1.2s ease-in-out infinite; }
.status-orb[data-state="retrieving"] { animation: orbStretch 0.9s ease-in-out infinite; }
.status-orb[data-state="citing"] { animation: orbPill 1.0s ease-in-out infinite; }
.status-orb[data-state="done"] { animation: orbPop 0.35s ease-out 1; }
.status-orb[data-state="error"] { animation: orbShake 0.45s ease-in-out infinite; background: var(--danger); }

@keyframes orbIdle { 0%,100%{transform:scale(1);} 50%{transform:scale(1.1);} }
@keyframes orbSpin { to{ transform:rotate(360deg);} }
@keyframes orbWobble { 0%,100%{border-radius:999px;} 50%{border-radius:10px;} }
@keyframes orbStretch { 0%,100%{transform:scaleX(1) scaleY(1);} 50%{transform:scaleX(1.7) scaleY(0.82);} }
@keyframes orbPill { 0%,100%{transform:scaleX(1) scaleY(1);} 50%{transform:scaleX(2.0) scaleY(0.78);} }
@keyframes orbPop { 0%{transform:scale(0.9);} 60%{transform:scale(1.3);} 100%{transform:scale(1);} }
@keyframes orbShake { 0%,100%{transform:translateX(0);} 25%{transform:translateX(-2px);} 75%{transform:translateX(2px);} }
@keyframes blinkCaret { 0%,49%{opacity:1;} 50%,100%{opacity:0;} }

@media (max-width: 1200px) {
  .status {
    gap: 6px;
  }
}

@media (max-width: 980px) {
  :root {
    --topbar-h: 92px;
    --composer-h: 132px;
  }

  .topbar {
    flex-wrap: wrap;
    align-items: flex-start;
    row-gap: 8px;
  }

  .brand {
    width: 100%;
  }

  .tag {
    font-size: 11px;
  }

  .top-actions {
    margin-left: 0;
    width: auto;
    flex-wrap: wrap;
    gap: 6px;
  }

  .sources-mobile-btn {
    display: inline-flex;
    align-items: center;
  }

  .lang-label {
    display: none;
  }

  .status {
    margin-left: auto;
    min-width: 0;
  }

  .status-text,
  .version-badge {
    font-size: 10px;
    white-space: nowrap;
  }

  .version-badge {
    max-width: 130px;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .main {
    width: calc(100% - 16px);
    grid-template-columns: 1fr;
    gap: 10px;
    margin: 8px auto;
  }

  .chat {
    border-radius: 14px;
    padding: 12px;
  }

  .role {
    width: 48px;
    font-size: 10px;
  }

  .library {
    padding: 10px;
    border-radius: 14px;
  }

  .library-head {
    flex-direction: column;
    gap: 8px;
  }

  .library-subgroup-grid {
    grid-template-columns: 1fr;
  }

  .sources {
    position: fixed;
    left: 8px;
    right: 8px;
    top: calc(var(--topbar-h) + env(safe-area-inset-top) + 8px);
    bottom: calc(8px + env(safe-area-inset-bottom));
    max-height: none;
    z-index: 50;
    border-radius: 16px;
    transform: translateY(115%);
    transition: transform 0.24s ease;
    box-shadow: 0 20px 42px rgba(29, 26, 20, 0.26);
  }

  .sources.mobile-open {
    transform: translateY(0);
  }

  .sources-close {
    display: inline-flex;
    align-items: center;
  }

  body.sources-open .composer {
    display: none;
  }

  .page.library-open .composer {
    display: none;
  }

  .page.library-open {
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
  }

  .composer {
    padding: 8px 10px calc(10px + env(safe-area-inset-bottom));
  }

  .composer-inner {
    gap: 8px;
  }

  .input {
    font-size: 16px;
    min-height: 46px;
  }

  .send {
    min-width: 88px;
    padding: 0 12px;
  }

  .legal-disclaimer {
    margin-top: 6px;
    font-size: 10px;
  }
}

@media (max-width: 580px) {
  .tab-btn,
  .lang-toggle {
    padding: 0 10px;
    font-size: 12px;
  }

  .name {
    font-size: 17px;
  }

  .tag {
    max-width: 220px;
  }
}
