:root {
  color-scheme: dark;
  --bg: #07111a;
  --bg-soft: #0c1824;
  --bg-panel: rgba(13, 25, 38, 0.94);
  --bg-card: rgba(18, 32, 48, 0.92);
  --bg-card-strong: #132435;
  --text: #ecf2f8;
  --text-muted: #9db0c4;
  --text-dim: #76879b;
  --line: rgba(168, 191, 214, 0.16);
  --line-strong: rgba(168, 191, 214, 0.28);
  --accent: #7fe4c7;
  --accent-strong: #32c29d;
  --accent-soft: rgba(127, 228, 199, 0.14);
  --warning: #f2c96d;
  --danger: #ff8b84;
  --shadow: 0 20px 60px rgba(2, 7, 13, 0.45);
  --radius-xl: 28px;
  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 10px;
  --top-space: max(16px, env(safe-area-inset-top));
  --bottom-space: max(18px, env(safe-area-inset-bottom));
  --side-space: max(16px, env(safe-area-inset-left), env(safe-area-inset-right));
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  min-height: 100svh;
  background:
    radial-gradient(circle at top left, rgba(127, 228, 199, 0.16), transparent 32%),
    radial-gradient(circle at top right, rgba(89, 157, 255, 0.15), transparent 28%),
    linear-gradient(180deg, #0a1520 0%, #07111a 100%);
  color: var(--text);
  font-family: "Avenir Next", "Segoe UI", "Helvetica Neue", sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
}

button,
input,
textarea {
  font: inherit;
}

button {
  color: inherit;
}

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  transform: none;
}

.app-shell {
  min-height: 100svh;
  display: flex;
  background: transparent;
}

.app-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: var(--top-space) var(--side-space) var(--bottom-space);
  gap: 16px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 20px;
  border: 1px solid var(--line);
  border-radius: var(--radius-xl);
  background: rgba(8, 18, 28, 0.72);
  backdrop-filter: blur(24px);
  box-shadow: var(--shadow);
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 16px;
  min-width: 0;
}

.brand-block {
  min-width: 0;
}

.brand-row {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.brand-row h1,
.drawer-title,
.context-header h2,
.section-header h3,
.empty-state h3,
.preview-header h2 {
  margin: 0;
  font-family: "Iowan Old Style", "Palatino Linotype", "Book Antiqua", Georgia, serif;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.brand-row h1 {
  font-size: clamp(1.4rem, 5vw, 1.95rem);
}

.repo-label,
.drawer-subtitle,
.preview-path,
.section-copy,
.status-text,
.preview-meta,
.msg-meta,
.empty-state p,
.collection-meta,
.file-item-subtitle,
.context-empty,
.status-pill {
  color: var(--text-muted);
}

.eyebrow {
  margin: 0 0 6px;
  font-size: 0.74rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 34px;
  padding: 0 12px;
  border-radius: 999px;
  background: var(--accent-soft);
  border: 1px solid rgba(127, 228, 199, 0.28);
  color: var(--accent);
  font-size: 0.9rem;
  white-space: nowrap;
}

.mode-row {
  display: flex;
  gap: 10px;
  overflow-x: auto;
  padding-bottom: 2px;
}

.mode-row::-webkit-scrollbar,
.context-bar::-webkit-scrollbar,
.tree-list::-webkit-scrollbar,
.chat-area::-webkit-scrollbar,
.preview-body::-webkit-scrollbar,
.drawer-panel::-webkit-scrollbar,
.collection-list::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

.mode-row::-webkit-scrollbar-thumb,
.context-bar::-webkit-scrollbar-thumb,
.tree-list::-webkit-scrollbar-thumb,
.chat-area::-webkit-scrollbar-thumb,
.preview-body::-webkit-scrollbar-thumb,
.drawer-panel::-webkit-scrollbar-thumb,
.collection-list::-webkit-scrollbar-thumb {
  background: rgba(157, 176, 196, 0.24);
  border-radius: 999px;
}

.mode-chip,
.ghost-button,
.primary-button,
.collection-delete,
.context-chip button,
.file-item,
.drawer-tab {
  min-height: 44px;
}

.mode-chip,
.ghost-button,
.primary-button,
.drawer-tab,
.collection-delete {
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(14, 27, 40, 0.7);
  cursor: pointer;
  transition: transform 0.16s ease, border-color 0.16s ease, background 0.16s ease, color 0.16s ease;
}

.mode-chip:hover,
.ghost-button:hover,
.primary-button:hover,
.drawer-tab:hover,
.collection-delete:hover,
.file-item:hover,
.icon-button:hover {
  transform: translateY(-1px);
  border-color: var(--line-strong);
}

.mode-chip {
  padding: 0 18px;
  color: var(--text-muted);
  white-space: nowrap;
}

.mode-chip.active {
  color: var(--bg);
  background: linear-gradient(135deg, #95f3db 0%, #67d8b9 100%);
  border-color: rgba(149, 243, 219, 0.55);
}

.bp-chip.active {
  background: linear-gradient(135deg, #ffe49b 0%, #f6c451 100%);
  border-color: rgba(246, 196, 81, 0.45);
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  flex: 1;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
}

.chat-panel {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--line);
  border-radius: var(--radius-xl);
  background: rgba(8, 18, 28, 0.76);
  backdrop-filter: blur(24px);
  box-shadow: var(--shadow);
  overflow: hidden;
  height: 100%;
}

.chat-area {
  flex: 1;
  min-height: 0;
  min-width: 0;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 22px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  touch-action: pan-y;
}

.msg {
  min-width: 0;
  max-width: min(100%, 860px);
}

.msg.user {
  align-self: flex-end;
  background: linear-gradient(135deg, #6ce1bf 0%, #35b592 100%);
  color: #052017;
  padding: 14px 16px;
  border-radius: 22px 22px 8px 22px;
  box-shadow: 0 12px 30px rgba(53, 181, 146, 0.22);
  white-space: pre-wrap;
  max-width: min(100%, 720px);
}

.msg.assistant {
  align-self: stretch;
  min-width: 0;
  max-width: 100%;
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 18px;
}

.msg-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 14px;
  padding-bottom: 14px;
  border-bottom: 1px solid rgba(168, 191, 214, 0.12);
  font-size: 0.84rem;
}

.msg-meta[hidden] {
  display: none;
}

.meta-badge {
  display: inline-flex;
  align-items: center;
  padding: 6px 10px;
  border-radius: 999px;
  background: rgba(13, 28, 43, 0.85);
  border: 1px solid rgba(168, 191, 214, 0.14);
  color: var(--text-muted);
}

.msg-body {
  min-width: 0;
  color: var(--text);
  line-height: 1.72;
  font-size: 1rem;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.msg-body > :first-child {
  margin-top: 0;
}

.msg-body > :last-child {
  margin-bottom: 0;
}

.msg-body p,
.msg-body ul,
.msg-body ol,
.msg-body pre,
.msg-body blockquote,
.msg-body h1,
.msg-body h2,
.msg-body h3,
.msg-body h4 {
  margin: 0 0 1em;
}

.msg-body h1,
.msg-body h2,
.msg-body h3,
.msg-body h4 {
  font-family: "Iowan Old Style", "Palatino Linotype", "Book Antiqua", Georgia, serif;
  line-height: 1.2;
}

.msg-body ul,
.msg-body ol {
  padding-left: 1.35rem;
}

.msg-body a {
  color: #8ec8ff;
}

.msg-body code {
  font-family: "SFMono-Regular", "Menlo", "Consolas", monospace;
  background: rgba(142, 200, 255, 0.12);
  padding: 0.16em 0.42em;
  border-radius: 8px;
  color: #cae6ff;
}

.msg-body pre {
  max-width: 100%;
  overflow-x: auto;
  padding: 16px;
  border-radius: 16px;
  background: rgba(4, 12, 19, 0.95);
  border: 1px solid rgba(168, 191, 214, 0.12);
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-x: contain;
  touch-action: pan-x;
}

.msg-body pre code {
  padding: 0;
  background: transparent;
  color: inherit;
}

.msg-body blockquote {
  padding-left: 16px;
  border-left: 3px solid rgba(127, 228, 199, 0.5);
  color: var(--text-muted);
}

.typing {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--text-muted);
}

.typing::after {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 14px 0 0 rgba(127, 228, 199, 0.55), 28px 0 0 rgba(127, 228, 199, 0.28);
}

.composer-wrap {
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex-shrink: 0;
  padding: 18px 18px calc(18px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: rgba(8, 18, 28, 0.92);
}

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

.context-panel[hidden] {
  display: none;
}

.context-header {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 12px;
}

.context-header h2 {
  font-size: 1.08rem;
}

.context-bar {
  display: flex;
  gap: 10px;
  overflow-x: auto;
  padding-bottom: 2px;
}

.context-empty {
  min-height: 44px;
  display: flex;
  align-items: center;
  padding: 0 2px;
}

.context-chip {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 0 12px;
  border-radius: 999px;
  background: rgba(20, 37, 55, 0.88);
  border: 1px solid rgba(168, 191, 214, 0.15);
  white-space: nowrap;
}

.context-chip span {
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.context-chip button {
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  min-width: 32px;
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 12px;
}

#msg-input {
  flex: 1;
  resize: none;
  border: 1px solid rgba(168, 191, 214, 0.18);
  border-radius: 20px;
  background: rgba(18, 32, 48, 0.9);
  color: var(--text);
  padding: 15px 16px;
  min-height: 56px;
  max-height: 180px;
  outline: none;
  line-height: 1.5;
}

#msg-input:focus,
.field input:focus {
  border-color: rgba(127, 228, 199, 0.55);
  box-shadow: 0 0 0 3px rgba(127, 228, 199, 0.12);
}

.primary-button {
  padding: 0 18px;
  background: linear-gradient(135deg, #95f3db 0%, #67d8b9 100%);
  border-color: rgba(149, 243, 219, 0.45);
  color: #032218;
  font-weight: 700;
}

.primary-button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  transform: none;
}

.send-button {
  min-width: 104px;
}

.ghost-button {
  padding: 0 16px;
  color: var(--text-muted);
}

.drawer {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 40;
  width: min(92vw, 380px);
  height: 100svh;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: calc(var(--top-space) + 6px) 16px calc(var(--bottom-space) + 16px);
  background: rgba(6, 14, 22, 0.96);
  border-right: 1px solid var(--line);
  box-shadow: var(--shadow);
  transform: translateX(-105%);
  transition: transform 0.22s ease;
}

.drawer.open {
  transform: translateX(0);
}

.drawer-header,
.preview-header,
.section-header,
.panel-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.drawer-tabs {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  padding: 6px;
  border-radius: 999px;
  background: rgba(12, 25, 38, 0.7);
  border: 1px solid var(--line);
}

.drawer-tab {
  padding: 0 12px;
  color: var(--text-muted);
}

.drawer-tab.active {
  background: rgba(127, 228, 199, 0.15);
  border-color: rgba(127, 228, 199, 0.32);
  color: var(--accent);
}

.drawer-panel {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

.drawer-panel[hidden] {
  display: none;
}

.breadcrumb {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text-muted);
}

.tree-list,
.collection-list,
.knowledge-stack {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.tree-list {
  padding-top: 12px;
}

.file-item,
.collection-card,
.empty-card {
  width: 100%;
  border: 1px solid rgba(168, 191, 214, 0.14);
  border-radius: 18px;
  background: rgba(15, 29, 43, 0.82);
}

.file-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 14px 16px;
  cursor: pointer;
  text-align: left;
}

.file-item.active {
  border-color: rgba(127, 228, 199, 0.38);
  background: rgba(127, 228, 199, 0.12);
}

.file-item-main {
  min-width: 0;
}

.file-item-title {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
}

.file-item-badge,
.path-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  padding: 0 10px;
  height: 28px;
  border-radius: 999px;
  border: 1px solid rgba(168, 191, 214, 0.16);
  background: rgba(8, 18, 28, 0.68);
  font-size: 0.78rem;
  color: var(--text-muted);
  text-transform: uppercase;
}

.file-item-subtitle {
  margin-top: 4px;
  font-size: 0.9rem;
}

.file-item-trailing {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--text-dim);
}

.path-chip {
  min-width: 0;
}

.collection-card,
.section-card {
  padding: 16px;
}

.section-card {
  display: flex;
  flex-direction: column;
  gap: 14px;
  border: 1px solid rgba(168, 191, 214, 0.14);
  border-radius: 22px;
  background: rgba(13, 25, 38, 0.82);
}

.section-copy {
  margin: 0;
}

.collection-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.collection-name {
  font-weight: 600;
}

.collection-meta {
  margin-top: 6px;
  font-size: 0.9rem;
}

.collection-delete {
  padding: 0 14px;
  color: var(--danger);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 14px;
}

.field span,
.check-row span {
  color: var(--text-muted);
  font-size: 0.95rem;
}

.field input {
  min-height: 52px;
  width: 100%;
  border-radius: 16px;
  border: 1px solid rgba(168, 191, 214, 0.16);
  background: rgba(8, 18, 28, 0.82);
  color: var(--text);
  padding: 0 14px;
  outline: none;
}

.field input[type="file"] {
  padding: 12px 14px;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 14px;
}

.check-row input {
  width: 20px;
  height: 20px;
}

.status-text {
  min-height: 24px;
  margin-top: 12px;
  font-size: 0.95rem;
}

.status-text.success {
  color: var(--accent);
}

.status-text.error {
  color: var(--danger);
}

.status-text.warning {
  color: var(--warning);
}

.preview-pane {
  position: fixed;
  inset: 10vh 12px 12px;
  z-index: 45;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 18px;
  background: rgba(8, 18, 28, 0.98);
  border: 1px solid var(--line);
  border-radius: 26px;
  box-shadow: var(--shadow);
  transform: translateY(110%);
  transition: transform 0.22s ease;
}

.preview-pane.open {
  transform: translateY(0);
}

.preview-pane.desktop {
  position: relative;
  inset: auto;
  z-index: auto;
  transform: none;
  box-shadow: var(--shadow);
}

.preview-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.preview-meta {
  font-size: 0.92rem;
  padding-bottom: 12px;
  border-bottom: 1px solid rgba(168, 191, 214, 0.12);
}

.preview-body {
  flex: 1;
  min-height: 0;
  overflow: auto;
  border-radius: 20px;
  border: 1px solid rgba(168, 191, 214, 0.12);
  background: rgba(4, 12, 19, 0.95);
  padding: 18px;
}

.preview-body pre {
  margin: 0;
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-word;
  font-family: "SFMono-Regular", "Menlo", "Consolas", monospace;
  line-height: 1.55;
}

.empty-state {
  display: grid;
  gap: 10px;
  align-content: start;
}

.surface-backdrop {
  position: fixed;
  inset: 0;
  z-index: 35;
  background: rgba(2, 7, 13, 0.5);
  backdrop-filter: blur(8px);
}

.preview-backdrop {
  z-index: 44;
}

.icon-button {
  width: 48px;
  height: 48px;
  border-radius: 16px;
  border: 1px solid var(--line);
  background: rgba(13, 25, 38, 0.82);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  position: relative;
}

.icon-button span {
  position: absolute;
  width: 18px;
  height: 2px;
  border-radius: 999px;
  background: var(--text);
}

.menu-button span:nth-child(1) {
  transform: translateY(-6px);
}

.menu-button span:nth-child(3) {
  transform: translateY(6px);
}

.close-only span:first-child,
.close-preview span:first-child {
  transform: rotate(45deg);
}

.close-only span:last-child,
.close-preview span:last-child {
  transform: rotate(-45deg);
}

.notice {
  position: fixed;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 60;
  min-width: min(90vw, 320px);
  padding: 14px 18px;
  border-radius: 999px;
  background: rgba(12, 25, 38, 0.96);
  border: 1px solid rgba(168, 191, 214, 0.18);
  text-align: center;
  box-shadow: var(--shadow);
}

.notice.success {
  color: var(--accent);
}

.notice.error {
  color: var(--danger);
}

.empty-card {
  padding: 16px;
  color: var(--text-muted);
}

@media (max-width: 767px) {
  body {
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior-y: auto;
  }

  .app-shell {
    display: block;
    min-height: 100%;
  }

  .app-main {
    min-height: 100svh;
    overflow: visible;
  }

  .workspace {
    display: block;
    flex: none;
    height: auto;
    overflow: visible;
  }

  .chat-panel {
    height: auto;
    min-height: auto;
    overflow: visible;
  }

  .chat-area {
    flex: none;
    min-height: auto;
    max-height: none;
    overflow: visible;
    overscroll-behavior-y: auto;
    touch-action: auto;
    padding: 18px;
  }

  .composer-wrap {
    position: sticky;
    bottom: 0;
    z-index: 5;
    padding-bottom: calc(18px + env(safe-area-inset-bottom));
  }

  .composer {
    flex-direction: column;
    align-items: stretch;
  }

  .send-button {
    width: 100%;
  }

  .context-header {
    align-items: start;
    flex-direction: column;
  }
}

@media (min-width: 1024px) {
  .app-shell {
    padding: 18px;
    gap: 18px;
  }

  .app-main {
    padding: 0;
  }

  .menu-button,
  .close-only,
  .close-preview,
  .surface-backdrop {
    display: none;
  }

  .drawer {
    position: relative;
    transform: none;
    width: 360px;
    height: calc(100svh - 36px);
    border: 1px solid var(--line);
    border-radius: 28px;
    background: rgba(6, 14, 22, 0.78);
    backdrop-filter: blur(24px);
    padding-top: 22px;
  }

  .workspace {
    grid-template-columns: minmax(0, 1fr) minmax(340px, 420px);
    gap: 18px;
  }

  .preview-pane {
    position: relative;
    inset: auto;
    z-index: auto;
    transform: none;
    display: flex;
    min-height: 0;
    border-radius: var(--radius-xl);
    background: rgba(8, 18, 28, 0.76);
    backdrop-filter: blur(24px);
  }
}
