:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-soft: #f0f2f5;
  --text: #0f172a;
  --muted: #5b6475;
  --border: #d8dde6;
  --accent: #2b6cb0;
  --accent-strong: #1f4e82;
  --danger: #c53030;
  --warning: #b7791f;
  --code-bg: #f8fafc;
  --code-border: #e2e8f0;
  --shadow: 0 12px 32px rgba(15, 23, 42, 0.08);
  --pw-bg: var(--surface);
  --pw-surface: var(--surface);
  --pw-border: var(--border);
  --pw-text: var(--text);
  --pw-muted: var(--muted);
  --pw-shadow: var(--shadow);
  --pw-radius: 12px;
  --pw-handle: 10px;
  --pw-header-h: 38px;
  --pw-min-w: 260px;
  --pw-min-h: 160px;
  --sidebar-width: 320px;
  --line-height: 1.5em;
  --stack-offset: 120px;
  --control-offset: 60px;
}

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

body {
  margin: 0;
  font-family: "Inter", "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  min-height: 100vh;
}

button {
  border: 1px solid transparent;
  background: var(--surface);
  color: var(--text);
  padding: 9px 14px;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 600;
  transition: transform 0.08s ease, box-shadow 0.12s ease, border-color 0.12s, background 0.12s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

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

button:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 8px 16px rgba(15, 23, 42, 0.08);
}

button.active {
  border-color: var(--accent);
  color: var(--accent-strong);
  background: rgba(43, 108, 176, 0.12);
}

button.primary {
  background: linear-gradient(120deg, var(--accent) 0%, #3b82f6 100%);
  color: #fff;
  border-color: var(--accent);
}

button.primary.large {
  padding: 14px 20px;
  font-size: 16px;
  border-radius: 12px;
}

button.secondary {
  border-color: var(--accent);
  color: var(--accent-strong);
  background: transparent;
}

button.ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--muted);
}

button.tiny {
  padding: 6px 10px;
  font-size: 12px;
}

button.link-button {
  background: transparent;
  color: var(--accent-strong);
  border-color: transparent;
  padding: 6px 8px;
  text-decoration: underline;
}

.btn-emoji {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

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

input,
textarea {
  font-family: inherit;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  background: #fff;
  color: var(--text);
}

input:focus,
textarea:focus,
button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

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

.brand-mark {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  background: var(--surface-soft);
  display: grid;
  place-items: center;
  font-weight: 800;
  letter-spacing: 0.5px;
  color: var(--accent-strong);
  border: 1px solid var(--border);
}

.brand-title {
  font-size: 16px;
  font-weight: 700;
}

.brand-subtitle {
  font-size: 12px;
  color: var(--muted);
}

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

.control-bar {
  position: sticky;
  top: var(--control-offset);
  z-index: 15;
  background: var(--surface-soft);
  border-bottom: 1px solid var(--border);
  padding: 10px 16px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
}

.control-actions {
  display: flex;
  gap: 8px;
  justify-content: center;
}

.status-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  border-radius: 10px;
  background: #e2e8f0;
  color: var(--muted);
  font-weight: 600;
}

.active-indicator {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  font-size: 13px;
  color: var(--muted);
  min-width: 0;
}

.active-indicator .value {
  color: var(--text);
  font-weight: 600;
  max-width: 70%;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}

.app-shell {
  display: flex;
  position: relative;
  min-height: calc(100vh - var(--stack-offset));
}

.empty-panel {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  z-index: 2;
  pointer-events: auto;
}

.empty-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 24px;
  width: min(600px, 90vw);
  box-shadow: var(--shadow);
}

.empty-title {
  font-size: 22px;
  font-weight: 800;
  margin-bottom: 8px;
}

.empty-body p {
  margin: 6px 0;
}

.empty-actions {
  display: flex;
  gap: 10px;
  margin-top: 14px;
  flex-wrap: wrap;
}

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

.inline-warning {
  margin-top: 8px;
  padding: 10px 12px;
  background: #fffaf0;
  border: 1px solid #f6e05e;
  border-radius: 10px;
  color: #744210;
}

.status-banner {
  padding: 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  margin-bottom: 12px;
  color: var(--text);
  font-weight: 600;
}

.edge-handle {
  width: 10px;
  background: var(--surface-soft);
  border-right: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.2s ease, opacity 0.2s ease;
  opacity: 0.7;
  flex-shrink: 0;
  height: calc(100vh - var(--stack-offset));
  position: sticky;
  top: var(--stack-offset);
}

.edge-handle:hover {
  opacity: 1;
}

.sidebar {
  position: sticky;
  top: var(--stack-offset);
  height: calc(100vh - var(--stack-offset));
  background: var(--surface);
  border-right: 1px solid var(--border);
  width: var(--sidebar-width);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 8px 0 24px rgba(15, 23, 42, 0.05);
  transition: transform 0.2s ease, opacity 0.2s ease;
  flex-shrink: 0;
}

.sidebar.unpinned {
  position: fixed;
  left: 0;
  top: var(--stack-offset);
  height: calc(100vh - var(--stack-offset));
  bottom: auto;
  transform: translateX(-100%);
  opacity: 0;
  pointer-events: none;
}

.sidebar.unpinned.overlay-open {
  transform: translateX(0);
  opacity: 1;
  pointer-events: auto;
  z-index: 15;
}

.sidebar-header {
  padding: 12px 12px 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
}

.sidebar-title {
  font-weight: 700;
}

.sidebar-subtitle {
  font-size: 12px;
  color: var(--muted);
}

.sidebar-body {
  overflow: hidden;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 12px;
  min-height: 0;
}

.resize-handle {
  height: 10px;
  cursor: ew-resize;
  width: 100%;
  padding: 4px 0;
  text-align: center;
  border-top: 1px dashed var(--border);
}

.section-title {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 8px;
}

.tree-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
  min-height: 0;
}

.tree {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px;
  background: var(--surface-soft);
  overflow: auto;
  flex: 1;
  min-height: 0;
}

.placeholder {
  padding: 10px 12px;
  background: var(--surface-soft);
  border: 1px dashed var(--border);
  border-radius: 10px;
  color: var(--muted);
  font-size: 13px;
}

.tree-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 6px;
  border-radius: 6px;
  cursor: pointer;
  color: var(--text);
}

.tree-item[aria-current="location"],
.tree-item:hover {
  background: rgba(43, 108, 176, 0.12);
}

.tree-item.file.active {
  background: rgba(43, 108, 176, 0.15);
  border: 1px solid rgba(43, 108, 176, 0.25);
}

.tree-item.file.is-hidden {
  opacity: 0.5;
  cursor: default;
}

.tree-item.file.is-hidden:hover {
  background: transparent;
}

.caret {
  width: 14px;
  text-align: center;
  color: var(--muted);
}

.node-label {
  flex: 1;
  font-size: 13px;
}

.main-pane {
  padding: 16px;
  position: relative;
  flex: 1;
  min-width: 0;
  z-index: 1;
}

.main-pane.inactive {
  pointer-events: none;
}

.file-container {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.file-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
  scroll-margin-top: calc(var(--stack-offset) + 24px);
}

.file-section.is-hidden {
  display: none;
}

.file-summary {
  list-style: none;
  outline: none;
  display: block;
  cursor: pointer;
}

.file-summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 4px;
  border-radius: 10px;
}

.file-summary::-webkit-details-marker {
  display: none;
}

.file-header {
  padding: 10px 12px;
  background: #f4f6f9;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  position: sticky;
  top: calc(var(--stack-offset) + 12px);
  z-index: 5;
}

.file-toggle {
  font-size: 14px;
  color: var(--muted);
  width: 16px;
  text-align: center;
  transition: transform 0.15s ease;
  margin-right: 2px;
}

.file-section:not([open]) .file-toggle {
  transform: rotate(-90deg);
}

.file-path {
  font-weight: 700;
  color: var(--text);
}

.badge {
  padding: 4px 8px;
  border-radius: 8px;
  background: rgba(43, 108, 176, 0.12);
  color: var(--accent-strong);
  font-size: 12px;
  font-weight: 700;
}

.stat {
  font-size: 12px;
  color: var(--muted);
}

.file-actions {
  margin-left: auto;
  display: flex;
  gap: 6px;
}

.toc-panel {
  margin: 12px 0;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: 0 10px 24px rgba(15, 23, 42, 0.05);
  overflow: hidden;
}

.code-search-panel {
  margin-top: 0;
}

.toc-summary {
  padding: 12px 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  user-select: none;
}

.toc-summary::before {
  content: "▸";
  font-size: 12px;
  color: var(--muted);
}

details[open] .toc-summary::before {
  content: "▾";
}

.toc-summary::-webkit-details-marker {
  display: none;
}

.toc-summary::marker {
  content: "";
}

.toc-count {
  margin-left: auto;
  font-size: 12px;
  color: var(--muted);
}

.toc-body {
  padding: 12px 16px 16px;
  border-top: 1px solid var(--border);
  background: var(--surface-soft);
  display: grid;
  gap: 12px;
}

.code-search-body {
  gap: 14px;
}

.code-search-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
}

.code-search-select,
.code-search-input {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  background: #fff;
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
}

.code-search-select:focus,
.code-search-input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.code-search-input {
  min-width: 180px;
  flex: 1 1 220px;
}

.code-search-toggle {
  margin-right: 0;
  font-size: 13px;
}

.code-search-error {
  border-radius: 10px;
  border: 1px solid #f6e05e;
  background: #fffaf0;
  color: #744210;
  padding: 10px 12px;
  font-size: 13px;
  display: grid;
  gap: 4px;
}

.code-search-error-detail {
  color: #8a5b16;
  font-size: 12px;
}

.code-search-results {
  display: grid;
  gap: 8px;
}

.code-search-results-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 700;
  font-size: 13px;
}

.code-search-results-meta {
  color: var(--muted);
  font-weight: 600;
  font-size: 12px;
}

.code-search-unavailable,
.code-search-cap {
  font-size: 12px;
  color: var(--muted);
}

.code-search-results-list {
  display: grid;
  gap: 10px;
  max-height: min(45vh, 360px);
  overflow: auto;
  padding-right: 4px;
}

.code-search-result {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fff;
  padding: 10px 12px;
  display: grid;
  gap: 8px;
  box-shadow: 0 6px 16px rgba(15, 23, 42, 0.04);
}

.code-search-result-header {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  font-size: 12px;
}

.code-search-result-link {
  color: var(--accent-strong);
  font-weight: 700;
  text-decoration: none;
  cursor: pointer;
}

.code-search-result-link:hover {
  text-decoration: underline;
}

.code-search-preview {
  background: var(--code-bg);
  border: 1px solid var(--code-border);
  border-radius: 10px;
  padding: 6px 0;
  font-family: "JetBrains Mono", "SFMono-Regular", "Consolas", monospace;
  font-size: 12px;
  line-height: 1.5;
}

.code-search-line {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 10px;
  padding: 2px 10px;
  cursor: pointer;
}

.code-search-line:hover {
  background: rgba(43, 108, 176, 0.08);
}

.code-search-line.is-match {
  background: rgba(43, 108, 176, 0.12);
}

.code-search-line-number {
  color: var(--muted);
  font-size: 11px;
}

.code-search-line-text {
  white-space: pre-wrap;
  word-break: break-word;
}

.code-search-match {
  background: rgba(255, 193, 7, 0.35);
  border-radius: 4px;
  padding: 0 1px;
}

.toc-toolbar {
  display: grid;
  gap: 8px;
}

.toc-selection,
.toc-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.toc-separator {
  height: 1px;
  background: var(--border);
}

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

.toc-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 6px;
}

.toc-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 6px 8px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.7);
  border: 1px solid transparent;
}

.toc-item.is-hidden {
  opacity: 0.5;
}

.toc-item:hover {
  border-color: var(--border);
  background: #fff;
}

.toc-checkbox {
  margin-top: 3px;
}

.toc-link {
  color: var(--accent-strong);
  text-decoration: none;
  font-weight: 600;
  word-break: break-word;
}

.toc-text {
  color: var(--muted);
  font-weight: 600;
  word-break: break-word;
}

.toc-link:hover {
  text-decoration: underline;
}

pre {
  margin: 0;
  padding: 14px;
  background: var(--code-bg);
  color: #0f172a;
  font-family: "JetBrains Mono", "SFMono-Regular", "Consolas", monospace;
  overflow: auto;
  white-space: pre-wrap;
  line-height: var(--line-height);
  border-top: 1px solid var(--code-border);
  position: relative;
}

pre code {
  display: block;
  font-family: inherit;
  line-height: inherit;
}

pre code.hljs {
  padding: 0;
  background: transparent;
}

pre.nowrap {
  white-space: pre;
}

/* Preview window */
.pw-root {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 2147483647;
  background: var(--pw-surface);
  color: var(--pw-text);
  border: 1px solid var(--pw-border);
  border-radius: var(--pw-radius);
  box-shadow: var(--pw-shadow);
  overflow: hidden;
  user-select: none;
  opacity: 0.96;
}

.pw-header {
  height: var(--pw-header-h);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 12px;
  background: linear-gradient(120deg, rgba(43, 108, 176, 0.12), rgba(59, 130, 246, 0.12));
  border-bottom: 1px solid var(--pw-border);
  cursor: grab;
}

.pw-header:active {
  cursor: grabbing;
}

.pw-title {
  font-size: 13px;
  color: var(--pw-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pw-content {
  height: calc(100% - var(--pw-header-h));
  padding: 12px;
  overflow: auto;
  user-select: text;
}

.pw-resize-cursor-n { cursor: ns-resize; }
.pw-resize-cursor-s { cursor: ns-resize; }
.pw-resize-cursor-e { cursor: ew-resize; }
.pw-resize-cursor-w { cursor: ew-resize; }
.pw-resize-cursor-ne { cursor: nesw-resize; }
.pw-resize-cursor-sw { cursor: nesw-resize; }
.pw-resize-cursor-nw { cursor: nwse-resize; }
.pw-resize-cursor-se { cursor: nwse-resize; }

.panel {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
}

.panel.hidden {
  display: none;
}

.panel-content {
  background: #fff;
  border-radius: 14px;
  width: min(900px, 90vw);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow);
}

.panel-header,
.panel-footer {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.panel-footer {
  border-top: 1px solid var(--border);
  border-bottom: none;
}

.panel-title {
  font-size: 18px;
  font-weight: 700;
}

.panel-subtitle {
  font-size: 13px;
  color: var(--muted);
}

.panel-body {
  padding: 16px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.field label {
  display: block;
  margin-bottom: 6px;
  font-weight: 600;
  color: var(--text);
}

.field textarea {
  width: 100%;
  resize: vertical;
}

.field.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
}

.inline-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-right: 12px;
  color: var(--muted);
}

.field.hint {
  color: var(--muted);
  font-size: 12px;
}

.log-body {
  font-family: "JetBrains Mono", "SFMono-Regular", "Consolas", monospace;
  white-space: pre-wrap;
  background: #0f172a;
  color: #d6e7ff;
  border-radius: 10px;
  padding: 12px;
}

.ts-window {
  position: fixed;
  z-index: 25;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.ts-titlebar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  background: linear-gradient(120deg, rgba(43, 108, 176, 0.12), rgba(59, 130, 246, 0.12));
  border-bottom: 1px solid var(--border);
  cursor: move;
}

.ts-title {
  font-weight: 800;
}

.ts-subtitle {
  font-size: 12px;
  color: var(--muted);
  max-width: 320px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ts-controls {
  display: flex;
  gap: 6px;
}

.ts-body {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow: auto;
  flex: 1;
  min-height: 0;
}

.ts-status {
  font-size: 13px;
  color: var(--text);
}

.ts-status.muted {
  color: var(--muted);
}

.ts-section {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-soft);
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.ts-section-title {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.ts-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.ts-placeholder {
  padding: 8px 10px;
  background: #fff;
  border: 1px dashed var(--border);
  border-radius: 8px;
  color: var(--muted);
  font-size: 13px;
}

.ts-row {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid transparent;
  background: #fff;
  border-radius: 8px;
  padding: 8px 10px;
  text-align: left;
  cursor: pointer;
}

.ts-row.include {
  justify-content: space-between;
  cursor: default;
}

.ts-row:hover {
  border-color: var(--border);
  box-shadow: 0 4px 12px rgba(15, 23, 42, 0.06);
}

.ts-kind {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 4px 6px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: rgba(43, 108, 176, 0.1);
  color: var(--accent-strong);
}

.ts-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 700;
}

.ts-line {
  color: var(--muted);
  font-size: 12px;
}

.ts-resize-handle {
  align-self: flex-end;
  width: 16px;
  height: 16px;
  margin: 4px;
  cursor: se-resize;
  border-radius: 4px;
  background: linear-gradient(135deg, transparent 50%, var(--border) 50%), linear-gradient(45deg, transparent 50%, var(--border) 50%);
  background-size: 100% 100%;
  background-repeat: no-repeat;
}

.ts-chip {
  position: fixed;
  right: 12px;
  top: 40%;
  transform: translateY(-50%);
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  cursor: pointer;
  writing-mode: vertical-rl;
  text-orientation: mixed;
  color: var(--text);
  user-select: none;
  z-index: 24;
}

.ts-marker {
  position: absolute;
  left: 0;
  width: 2px;
  height: 0;
  border-left: 2px solid var(--accent);
  pointer-events: none;
}

.hidden {
  display: none !important;
}

@media (max-width: 960px) {
  .control-bar {
    grid-template-columns: 1fr;
    justify-items: flex-start;
  }

  .control-actions {
    justify-content: flex-start;
    flex-wrap: wrap;
  }

  .active-indicator {
    justify-content: flex-start;
  }

  .app-shell {
    display: block;
  }

  .sidebar {
    position: absolute;
    inset: var(--stack-offset) auto 0 0;
    height: calc(100vh - var(--stack-offset));
    min-height: auto;
  }

  .edge-handle {
    width: 14px;
  }

  .file-header {
    top: calc(var(--stack-offset) + 24px);
  }
}
