:root {
  --bg: #f7f7f8;
  --panel: #ffffff;
  --panel-soft: #fafafa;
  --text: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --line-strong: #d1d5db;
  --accent: #111827;
  --accent-strong: #0b1220;
  --success: #10a37f;
  --danger-bg: #fff5f5;
  --danger-border: #f5c2c2;
  --danger-text: #b42318;
  --radius-lg: 16px;
  --radius-md: 12px;
  --radius-sm: 10px;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.05);
  --shadow-md: 0 10px 30px rgba(16, 24, 40, 0.08);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  color: var(--text);
  font: 14px/1.5 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
  background: #f7f7f8;
}

.app {
  height: 100dvh;
  min-height: 100dvh;
  display: grid;
  grid-template-columns: 264px minmax(0, 1fr);
  gap: 0;
  padding: 0;
  max-width: none;
  margin: 0;
}

.thread-rail {
  min-height: 0;
  border: 0;
  border-right: 1px solid var(--line);
  border-radius: 0;
  background: #fbfbfc;
  box-shadow: none;
  display: flex;
  flex-direction: column;
  max-width: 100%;
  overflow: hidden;
  overflow-x: hidden;
  overscroll-behavior-x: none;
}

.rail-top {
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 100%;
  overflow-x: hidden;
  overscroll-behavior-x: none;
}

.rail-actions {
  display: flex;
  gap: 8px;
}

.rail-actions .new-thread-btn {
  flex: 1 1 0;
  min-width: 0;
}

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

.brand-home-link {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  color: inherit;
  text-decoration: none;
  border-radius: 10px;
}

.brand-home-link:hover .brand {
  text-decoration: underline;
  text-underline-offset: 2px;
}

.rail-collapse-btn {
  appearance: none;
  width: 28px;
  height: 28px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: #4b5563;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex: 0 0 auto;
}

.rail-collapse-btn:hover {
  background: #f2f4f7;
  border-color: #dde3ec;
}

.rail-collapse-btn svg {
  width: 14px;
  height: 14px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}

.rail-collapse-btn-rail {
  display: none;
}

.brand-mark {
  width: 32px;
  height: 32px;
  border-radius: 9px;
  display: block;
  overflow: hidden;
  border: 1px solid rgba(17, 24, 39, 0.08);
  background: #fff;
}

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

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

.brand {
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.1px;
  line-height: 1.05;
}

.brand-sub {
  margin-top: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.05;
}

.new-thread-btn {
  appearance: none;
  width: 100%;
  border: 1px solid var(--line-strong);
  background: #fff;
  color: var(--text);
  border-radius: 999px;
  padding: 0.48rem 0.78rem;
  font: 600 13px/1 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
  cursor: pointer;
  transition: background 0.16s ease, border-color 0.16s ease;
}

.new-thread-btn:hover {
  background: var(--panel-soft);
  border-color: #c7ccd4;
}

.new-thread-btn.secondary {
  color: #374151;
  background: #f9fafb;
}

.new-thread-btn.secondary:hover {
  background: #f3f4f6;
}

.thread-list {
  flex: 1 1 auto;
  min-height: 0;
  max-width: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior-x: none;
  touch-action: pan-y;
  padding: 8px 8px 10px;
}

.thread-empty {
  border: 1px dashed #d8dee7;
  border-radius: 10px;
  padding: 10px;
  color: var(--muted);
  font-size: 12px;
}

.thread-section {
  margin-bottom: 10px;
  max-width: 100%;
}

.thread-section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 100%;
  margin-bottom: 4px;
  padding: 0 4px;
  min-width: 0;
}

.thread-section-left {
  display: flex;
  align-items: center;
  flex: 1 1 auto;
  min-width: 0;
  gap: 4px;
}

.thread-section-toggle {
  appearance: none;
  width: 18px;
  height: 18px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: #6b7280;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.thread-section-toggle:hover {
  border-color: #d7dee8;
  background: #f6f8fb;
}

.thread-section-toggle svg {
  width: 11px;
  height: 11px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  transform: rotate(90deg);
  transition: transform 0.16s ease;
}

.thread-section-toggle.collapsed svg {
  transform: rotate(0deg);
}

.thread-section-title {
  flex: 1 1 auto;
  max-width: 100%;
  font-size: 11px;
  color: #6b7280;
  font-weight: 700;
  letter-spacing: 0.2px;
  text-transform: uppercase;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thread-section-folder .thread-section-head {
  margin-bottom: 4px;
  padding: 0 2px 3px;
}

.thread-section-folder:not(.collapsed) .thread-section-head {
  border-bottom: 1px solid #eceff4;
}

.thread-section-folder .thread-section-left {
  gap: 5px;
}

.thread-section-folder .thread-section-title {
  font-size: 12.6px;
  font-weight: 600;
  letter-spacing: 0;
  text-transform: none;
  color: #384152;
}

.thread-section-folder-icon {
  width: 14px;
  height: 14px;
  color: #6b7280;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
}

.thread-section-folder-icon svg {
  width: 14px;
  height: 14px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.thread-section-folder .thread-section-count {
  font-size: 10.5px;
  color: #a1a8b3;
}

.thread-section-count {
  font-size: 11px;
  color: #9ca3af;
}

.thread-section-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 0 0 auto;
  flex-shrink: 0;
  max-width: 100%;
}

.thread-section-actions .thread-item-btn.icon.section {
  opacity: 0;
  transform: translateX(6px);
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
}

.thread-section:hover .thread-section-actions .thread-item-btn.icon.section,
.thread-section:focus-within .thread-section-actions .thread-item-btn.icon.section,
.thread-section.menu-open .thread-section-actions .thread-item-btn.icon.section {
  opacity: 1;
  transform: translateX(0);
  pointer-events: auto;
}

.thread-row {
  position: relative;
  border: 1px solid transparent;
  border-radius: 9px;
  margin-bottom: 3px;
  overflow: hidden;
}

.thread-row:hover {
  background: transparent;
  border-color: transparent;
}

.thread-item-wrap {
  position: relative;
  display: flex;
  align-items: center;
  max-width: 100%;
  min-width: 0;
  gap: 0;
  padding: 2px 0;
}

.thread-item {
  width: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  column-gap: 8px;
  border: 1px solid transparent;
  background: transparent;
  border-radius: 9px;
  padding: 6px 10px;
  text-align: left;
  color: var(--text);
  cursor: pointer;
  margin-bottom: 0;
  transition: background 0.16s ease, border-color 0.16s ease, padding-right 0.18s ease;
  touch-action: pan-y;
}

.thread-row:hover .thread-item,
.thread-row:focus-within .thread-item,
.thread-row.menu-open .thread-item {
  padding-right: 112px;
}

.thread-item:hover {
  background: #f3f4f6;
}

.thread-item.active {
  background: #eff1f4;
  border-color: #e2e6ec;
}

.thread-item-controls {
  position: absolute;
  right: 3px;
  top: 50%;
  display: flex;
  align-items: center;
  gap: 3px;
  padding: 2px;
  border: 1px solid #e3e7ee;
  border-radius: 999px;
  background: rgba(250, 251, 253, 0.96);
  box-shadow: none;
  z-index: 2;
  opacity: 0;
  transform: translate(14px, -50%);
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
}

.thread-row:hover .thread-item-controls,
.thread-row:focus-within .thread-item-controls,
.thread-row.menu-open .thread-item-controls {
  opacity: 1;
  transform: translate(0, -50%);
  pointer-events: auto;
}

.thread-item-btn {
  appearance: none;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: #4b5563;
  padding: 0.2rem 0.45rem;
  font: 600 11px/1 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
  cursor: pointer;
}

.thread-item-btn.icon {
  width: 22px;
  height: 22px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.thread-item-btn.icon.section {
  width: 22px;
  height: 22px;
}

.thread-item-btn.icon svg {
  width: 12px;
  height: 12px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.thread-item-btn:hover {
  background: #f4f6f8;
}

.thread-item-btn.delete {
  color: #9a2b23;
  border-color: #f1c6c3;
  background: #fff8f8;
}

.thread-item-btn.delete:hover {
  background: #fff2f2;
}

.thread-item.placeholder {
  opacity: 0.85;
}

.thread-item.placeholder .thread-title {
  font-weight: 500;
  color: #6b7280;
}

.thread-empty-folder {
  margin: 0 2px;
  padding: 8px 10px;
  border: 1px dashed #e3e7ee;
  border-radius: 10px;
  color: #8b95a5;
  font-size: 11.5px;
}

.thread-folder-menu {
  position: fixed;
  z-index: 150;
  min-width: 168px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 18px 40px rgba(15, 23, 42, 0.18);
  overflow: hidden;
}

.thread-folder-menu-item {
  width: 100%;
  border: 0;
  background: transparent;
  text-align: left;
  color: var(--text);
  padding: 0.55rem 0.66rem;
  font: 600 12.2px/1.2 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
  cursor: pointer;
}

.thread-folder-menu-item:hover {
  background: #f5f7fa;
}

.thread-folder-menu-item.checked::before {
  content: "✓ ";
  color: var(--success);
}

.thread-folder-menu-item.danger {
  color: #9a2b23;
}

.thread-folder-menu-sep {
  height: 1px;
  margin: 4px 0;
  background: #eceff3;
}

.thread-folder-menu-label {
  padding: 0.4rem 0.66rem 0.26rem;
  color: #8a94a3;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.35px;
  text-transform: uppercase;
}

.thread-title {
  font-size: 12.8px;
  font-weight: 600;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.thread-meta {
  margin-top: 0;
  font-size: 10.8px;
  color: var(--muted);
  white-space: nowrap;
  text-align: right;
  min-width: fit-content;
}

.thread-section-folder > .thread-row {
  margin-left: 18px;
}

.chat-workspace {
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 0;
  background: var(--panel);
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 0;
  border-bottom: 1px solid rgba(17, 24, 39, 0.06);
  border-radius: 0;
  background: rgba(255, 255, 255, 0.84);
  backdrop-filter: saturate(140%) blur(10px);
  padding: 11px 14px;
  box-shadow: none;
}

.workspace-title-wrap {
  min-width: 0;
}

.workspace-title {
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.1px;
  line-height: 1.05;
}

.workspace-subtitle {
  margin-top: 0;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.05;
}

.spacer {
  flex: 1;
}

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

.auth-controls {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.auth-status-pill {
  display: inline-flex;
  align-items: center;
  max-width: 180px;
  padding: 0.34rem 0.56rem;
  border: 1px solid #e3e7ef;
  border-radius: 999px;
  background: #f8fafc;
  color: #556070;
  font: 600 11.5px/1 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.top-actions button,
.toolbar button,
.toolbar .dropdown-btn,
.toolbar a.btn {
  appearance: none;
  border: 1px solid var(--line-strong);
  background: #fff;
  color: var(--text);
  border-radius: 999px;
  padding: 0.4rem 0.72rem;
  font: 600 12.4px/1 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  white-space: nowrap;
  transition: background 0.16s ease, border-color 0.16s ease;
}

.top-actions button:hover,
.toolbar button:hover,
.toolbar .dropdown-btn:hover,
.toolbar a.btn:hover {
  background: #fff;
  border-color: #d5dbe7;
}

#btnMode {
  min-width: 138px;
}

#btnMode[hidden] {
  display: none !important;
}

body.mode-developer #btnMode {
  background: #111827;
  border-color: #111827;
  color: #fff;
}

.toast {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 140;
  max-width: min(72vw, 380px);
  padding: 0.42rem 0.7rem;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 999px;
  background: rgba(17, 24, 39, 0.94);
  color: #ffffff;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  box-shadow: var(--shadow-md);
  pointer-events: none;
}

.toast:empty {
  display: none;
}

.chat-toggle {
  display: none;
}

.chat {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  border: 0;
  border-radius: 0;
  background: var(--panel);
  box-shadow: none;
  overflow: hidden;
}

.chat-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 12px 14px;
  scrollbar-gutter: stable;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.msg {
  margin-bottom: 12px;
}

.msg .role {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  row-gap: 4px;
  margin-bottom: 0.28rem;
  color: var(--muted);
  font-size: 11.6px;
  font-weight: 650;
}

.msg .role .role-kind {
  display: inline-flex;
  align-items: center;
  height: 18px;
  padding: 0 8px;
  border-radius: 999px;
  font: 700 10px/1 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
  letter-spacing: 0.01em;
}

.msg .role .role-kind.role-kind-diagram {
  border: 1px solid #c7d7ff;
  background: #eef4ff;
  color: #1d4ed8;
}

.msg .role .role-kind.role-kind-support {
  border: 1px solid #cddbe8;
  background: #f2f7fc;
  color: #334155;
}

.msg .role .role-kind.role-kind-advisor {
  border: 1px solid #f2d27a;
  background: #fff7db;
  color: #9a6700;
}

.msg .role .role-debug-seq {
  display: inline-flex;
  align-items: center;
  min-height: 18px;
  padding: 0 8px;
  border-radius: 999px;
  border: 1px dashed #d5dde8;
  background: #f8fafc;
  color: #475569;
  font: 600 10px/1 "IBM Plex Mono", "SFMono-Regular", Menlo, monospace;
  letter-spacing: 0;
  max-width: min(56vw, 520px);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 0 1 auto;
  max-width: min(52vw, 460px);
}

.msg .role .role-debug-path {
  display: inline-flex;
  align-items: center;
  min-height: 18px;
  padding: 0 8px;
  border-radius: 999px;
  border: 1px dashed #cfd8c5;
  background: #f7fbf2;
  color: #365314;
  font: 600 10px/1 "IBM Plex Mono", "SFMono-Regular", Menlo, monospace;
  letter-spacing: 0;
  flex: 1 1 100%;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.msg .bubble {
  border: 1px solid var(--line);
  border-radius: 13px;
  background: #fafafa;
  padding: 0.58rem 0.66rem;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.42;
}

.msg.msg-user .bubble {
  background: #f3f6fb;
  border-color: #dde4ef;
}

.msg.msg-assistant .bubble {
  background: #fff;
  max-height: min(46vh, 390px);
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-gutter: stable;
  line-height: 1.5;
}

.msg.msg-assistant .bubble.bubble-has-diagram-picker {
  max-height: none;
  overflow-y: visible;
}

.msg.msg-assistant .bubble .bubble-paragraph {
  margin: 0;
}

.msg.msg-assistant .bubble .bubble-paragraph + .bubble-paragraph {
  margin-top: 0.5rem;
}

.msg.msg-assistant .bubble .bubble-list {
  margin: 0.58rem 0;
  padding-left: 1.25rem;
}

.msg.msg-assistant .bubble .bubble-paragraph + .bubble-list,
.msg.msg-assistant .bubble .bubble-list + .bubble-paragraph {
  margin-top: 0.58rem;
}

.msg.msg-assistant .bubble .bubble-list + .bubble-list {
  margin-top: 0.48rem;
}

.msg.msg-assistant .bubble .bubble-list-item {
  margin: 0.16rem 0;
}

.msg.msg-assistant .bubble .bubble-list-lead {
  font-weight: 700;
}

.msg .bubble .bubble-mail-link {
  color: #2563eb;
  text-decoration: underline;
  text-decoration-color: rgba(37, 99, 235, 0.4);
  text-underline-offset: 2px;
  font-weight: 600;
}

.msg .bubble .bubble-mail-link:hover {
  color: #1d4ed8;
  text-decoration-color: rgba(29, 78, 216, 0.7);
}

.msg .bubble .bubble-mail-link:focus-visible {
  outline: 2px solid rgba(37, 99, 235, 0.25);
  outline-offset: 2px;
  border-radius: 4px;
}

.msg.msg-assistant .bubble .bubble-diagram-picker {
  margin: 0 0 0.62rem;
  border: 1px solid #dbe4ef;
  border-radius: 12px;
  padding: 0.52rem 0.56rem 0.56rem;
  background:
    radial-gradient(120% 110% at 100% 0%, rgba(14, 165, 233, 0.08), rgba(14, 165, 233, 0) 56%),
    radial-gradient(110% 110% at 0% 100%, rgba(59, 130, 246, 0.07), rgba(59, 130, 246, 0) 60%),
    #f8fbff;
}

.msg.msg-assistant .bubble .bubble-diagram-picker-title {
  margin: 0;
  color: #0f172a;
  font: 700 12px/1.15 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
}

.msg.msg-assistant .bubble .bubble-diagram-picker-subtitle {
  margin: 0.22rem 0 0;
  color: #475569;
  font: 500 11px/1.35 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
}

.msg.msg-assistant .bubble .bubble-diagram-picker-helper {
  margin-top: 0.42rem;
  border: 1px solid #dbe7f5;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.88);
  padding: 0.38rem 0.46rem;
}

.msg.msg-assistant .bubble .bubble-diagram-picker-helper-title {
  margin: 0;
  color: #0f172a;
  font: 700 11px/1.15 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
}

.msg.msg-assistant .bubble .bubble-diagram-picker-helper-line {
  margin: 0.2rem 0 0;
  color: #334155;
  font: 500 10.8px/1.35 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
}

.msg.msg-assistant .bubble .bubble-diagram-picker-section {
  margin-top: 0.5rem;
}

.msg.msg-assistant .bubble .bubble-diagram-picker-section-label {
  margin: 0 0 0.22rem;
  color: #64748b;
  font: 700 10px/1.1 "IBM Plex Mono", "SFMono-Regular", Menlo, monospace;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

.msg.msg-assistant .bubble .bubble-diagram-picker-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(108px, 1fr));
  gap: 0.34rem;
}

.msg.msg-assistant .bubble .bubble-diagram-picker-btn {
  --accent-h: 210;
  border: 1px solid hsl(var(--accent-h) 65% 78%);
  border-radius: 10px;
  padding: 0.38rem 0.48rem;
  background: linear-gradient(
    180deg,
    hsl(var(--accent-h) 86% 97%),
    hsl(var(--accent-h) 78% 95%)
  );
  color: hsl(var(--accent-h) 42% 24%);
  font: 650 11.5px/1.15 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
  text-align: left;
  cursor: pointer;
  transition: transform 120ms ease, box-shadow 120ms ease, border-color 120ms ease;
  box-shadow: 0 1px 0 rgba(15, 23, 42, 0.04);
}

.msg.msg-assistant .bubble .bubble-diagram-picker-btn:hover {
  transform: translateY(-1px);
  border-color: hsl(var(--accent-h) 70% 68%);
  box-shadow: 0 5px 14px rgba(15, 23, 42, 0.08);
}

.msg.msg-assistant .bubble .bubble-diagram-picker-btn[data-popular="true"] {
  border-width: 1.5px;
}

.msg.msg-assistant .bubble .bubble-diagram-picker-btn[data-unavailable="true"] {
  opacity: 0.66;
  border-style: dashed;
}

.msg.msg-assistant .bubble .bubble-diagram-picker-btn[data-unavailable="true"]:hover {
  transform: none;
  box-shadow: 0 1px 0 rgba(15, 23, 42, 0.04);
}

.msg.msg-assistant .bubble .bubble-diagram-picker-btn:focus-visible {
  outline: 2px solid rgba(37, 99, 235, 0.2);
  outline-offset: 1px;
}

.msg.msg-assistant .bubble .bubble-diagram-picker-btn.is-loading,
.msg.msg-assistant .bubble .bubble-diagram-picker-btn:disabled {
  cursor: wait;
  opacity: 0.72;
  transform: none;
  box-shadow: none;
}

.msg .actions {
  display: flex;
  gap: 0.4rem;
  margin-top: 0.38rem;
}

.msg .actions button {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  padding: 0.26rem 0.5rem;
  font: 600 12px/1 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
  cursor: pointer;
}

.msg .actions button:hover {
  background: #f7f8fa;
  border-color: #ccd2da;
}

.chat-suggestions {
  border-top: 1px solid var(--line);
  background: #fcfcfd;
  padding: 0.46rem 0.7rem 0.28rem;
  display: flex;
  flex-direction: column;
  gap: 0.36rem;
}

.dev-usage-summary {
  align-self: flex-end;
  max-width: min(100%, 860px);
  border: 1px solid #d8e0ea;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.92);
  padding: 0.34rem 0.42rem;
  box-shadow: 0 6px 20px rgba(15, 23, 42, 0.05);
}

.dev-usage-summary[hidden] {
  display: none !important;
}

.dev-usage-title {
  margin: 0 0 0.18rem;
  color: #64748b;
  font: 700 10px/1.1 "IBM Plex Mono", "SFMono-Regular", Menlo, monospace;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

.dev-usage-row {
  display: flex;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 0.24rem;
}

.dev-usage-row + .dev-usage-row {
  margin-top: 0.22rem;
}

.dev-usage-metric {
  display: inline-flex;
  align-items: center;
  min-height: 18px;
  padding: 0 7px;
  border-radius: 999px;
  border: 1px solid #dbe3ee;
  background: #f8fafc;
  color: #334155;
  font: 600 10px/1 "IBM Plex Mono", "SFMono-Regular", Menlo, monospace;
  white-space: nowrap;
}

.dev-usage-metric.scope {
  border-color: #c9d7ff;
  background: #eff4ff;
  color: #1d4ed8;
}

.dev-usage-metric.agent {
  border-style: dashed;
}

.dev-usage-metric.last {
  border-color: #d7e7cf;
  background: #f4fbef;
  color: #3f6212;
}

.suggestion-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.44rem;
}

.suggestion {
  border: 1px dashed #cbd5e1;
  background: #fff;
  color: #111827;
  border-radius: 999px;
  padding: 0.34rem 0.68rem;
  font: 600 12px/1 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
  cursor: pointer;
}

.suggestion:hover {
  background: #f7f9fb;
  border-color: #b9c3d2;
}

.chat-input {
  border-top: 1px solid var(--line);
  background: #fff;
  padding: 0.56rem 0.7rem 0.7rem;
  display: block;
  position: relative;
}

.chat-input textarea {
  width: 100%;
  display: block;
  resize: none;
  min-height: 46px;
  max-height: 25vh;
  overflow-y: hidden;
  border-radius: 18px;
  border: 1px solid var(--line-strong);
  background: #fff;
  color: var(--text);
  padding: 0.65rem 2.35rem 0.62rem 0.8rem;
  font: 500 13.2px/1.4 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
}

.chat-input #btnSend {
  width: 28px;
  height: 28px;
  position: absolute;
  right: 16px;
  bottom: 14px;
  z-index: 1;
  border: 1px solid #111827;
  border-radius: 999px;
  background: #111827;
  color: #fff;
  font: 700 12px/1 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
  cursor: pointer;
  transition: background-color 120ms ease, border-color 120ms ease, opacity 120ms ease;
}

.chat-input #btnSend:not(:disabled):hover {
  background: #0b1220;
  border-color: #0b1220;
}

.chat-input #btnSend:disabled {
  cursor: default;
}

.chat-input #btnSend.is-loading {
  color: transparent;
}

.chat-input #btnSend.is-loading::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 10px;
  height: 10px;
  margin-left: -5px;
  margin-top: -5px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.28);
  border-top-color: #fff;
  animation: chat-send-spin 0.72s linear infinite;
}

@keyframes chat-send-spin {
  to { transform: rotate(360deg); }
}

.chat-note {
  color: var(--muted);
  font-size: 12px;
  padding: 0 0.8rem 0.7rem;
}

.auth-nudge {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 0 0.7rem 0.55rem;
  padding: 0.55rem 0.7rem;
  border: 1px solid #e3e7ef;
  border-radius: 12px;
  background: #fbfcfe;
  color: #5c6676;
  font-size: 12px;
}

.auth-nudge > span {
  min-width: 0;
}

.auth-nudge button {
  appearance: none;
  border: 1px solid #d5dbe7;
  background: #fff;
  color: #1f2937;
  border-radius: 999px;
  padding: 0.34rem 0.62rem;
  font: 600 12px/1 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
  cursor: pointer;
  white-space: nowrap;
  flex: 0 0 auto;
}

.auth-nudge button:hover {
  border-color: #c7cfdd;
  background: #f9fafb;
}

.debug-panel {
  margin: 0 0.7rem 0.7rem;
  border: 1px solid #2f3c53;
  border-radius: 10px;
  background: #0c1220;
  color: #dce6f8;
}

.debug-panel summary {
  cursor: pointer;
  user-select: none;
  padding: 0.45rem 0.58rem;
  color: #9ec5ff;
  font-weight: 620;
}

.debug-panel pre {
  margin: 0;
  padding: 0.58rem;
  max-height: 240px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  border-top: 1px solid rgba(158, 197, 255, 0.28);
  font: 12px/1.36 "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

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

body.app-modal-open {
  overflow: hidden;
}

.app-modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 220;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(2, 6, 23, 0.42);
  opacity: 0;
  transition: opacity 90ms linear;
  will-change: opacity;
}

.app-modal-backdrop.open {
  opacity: 1;
}

.app-modal {
  width: min(460px, calc(100vw - 32px));
  border: 1px solid rgba(148, 163, 184, 0.24);
  border-radius: 18px;
  background: linear-gradient(180deg, #ffffff 0%, #f8fafc 100%);
  box-shadow: 0 28px 80px rgba(15, 23, 42, 0.28);
  color: var(--text);
  overflow: hidden;
  opacity: 0;
  transform: translateY(6px) scale(0.99);
  transition: opacity 120ms ease-out, transform 140ms cubic-bezier(0.2, 0.8, 0.2, 1);
  will-change: opacity, transform;
}

.app-modal-backdrop.open .app-modal {
  opacity: 1;
  transform: translateY(0) scale(1);
}

.app-modal-header {
  padding: 15px 16px 8px;
}

.app-modal-title {
  margin: 0;
  font: 700 17px/1.2 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
  color: #0f172a;
}

.app-modal-body {
  padding: 0 16px 12px;
}

.app-modal-message {
  margin: 0 0 10px;
  color: #475569;
  font: 500 13.5px/1.4 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
}

.app-modal-input {
  width: 100%;
  display: block;
  border: 1px solid #cbd5e1;
  border-radius: 12px;
  background: #fff;
  color: #0f172a;
  padding: 0.72rem 0.82rem;
  font: 500 14px/1.3 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
  outline: none;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.6);
}

.app-modal-input:focus {
  border-color: #10a37f;
  box-shadow: 0 0 0 3px rgba(16, 163, 127, 0.14);
}

.app-modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 12px 16px 16px;
  border-top: 1px solid rgba(148, 163, 184, 0.16);
  background: rgba(248, 250, 252, 0.7);
}

.app-modal-btn {
  appearance: none;
  border-radius: 999px;
  border: 1px solid #cbd5e1;
  background: #fff;
  color: #0f172a;
  padding: 0.5rem 0.85rem;
  font: 600 12.8px/1 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
  cursor: pointer;
}

.app-modal-btn:hover {
  background: #f8fafc;
  border-color: #b8c3d3;
}

.app-modal-btn.app-modal-btn-primary {
  background: #0f172a;
  border-color: #0f172a;
  color: #fff;
}

.app-modal-btn.app-modal-btn-primary:hover {
  background: #111827;
  border-color: #111827;
}

.app-modal-btn.app-modal-btn-primary.danger {
  background: #b42318;
  border-color: #b42318;
}

.app-modal-btn.app-modal-btn-primary.danger:hover {
  background: #991b1b;
  border-color: #991b1b;
}

@media (prefers-reduced-motion: reduce) {
  .app-modal-backdrop,
  .app-modal {
    transition: none;
  }
}

.diagram-drawer {
  position: fixed;
  inset: 0;
  z-index: 90;
}

.diagram-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  backdrop-filter: blur(2px);
}

.preview {
  position: absolute;
  inset: 28px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: #fff;
  box-shadow: 0 28px 80px rgba(15, 23, 42, 0.26);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

body.shared-view .app {
  display: none;
}

body.shared-view .diagram-backdrop {
  display: none;
}

body.shared-view .preview {
  inset: 0;
  border: 0;
  border-radius: 0;
  box-shadow: none;
}

.workspace-topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  border-bottom: 1px solid var(--line);
  padding: 10px 12px;
  background: #f9fafb;
}

.workspace-topbar #btnCloseCanvas {
  margin-left: auto;
  border: 1px solid var(--line-strong);
  background: #fff;
  color: var(--text);
  border-radius: 999px;
  padding: 0.4rem 0.72rem;
  font: 600 12.4px/1 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
  cursor: pointer;
}

.workspace-topbar #btnCloseCanvas:hover {
  background: #f3f4f6;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 0.42rem;
  flex-wrap: wrap;
  margin-left: 0;
}

.dropdown {
  position: relative;
}

.menu {
  position: absolute;
  right: 0;
  top: calc(100% + 7px);
  min-width: 168px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 20px 40px rgba(15, 23, 42, 0.12);
  display: none;
  overflow: hidden;
  z-index: 110;
}

.menu.open {
  display: block;
}

.menu .menu-item {
  width: 100%;
  text-align: left;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--text);
  padding: 0.36rem 0.82rem;
  font: 600 12.6px/1.2 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
  cursor: pointer;
}

.menu .menu-item:hover {
  background: #f5f7fa;
}

.menu .menu-item.checked {
  background: #e9edf4;
  color: #0f172a;
  border-radius: 0;
}

.menu .menu-item.checked:hover {
  background: #e2e8f2;
}

#themeMenuWrap,
#dirMenuWrap {
  width: auto;
  flex: 0 0 auto;
}

#themeMenu,
#dirMenu {
  left: 0;
  right: auto;
  top: calc(100% - 1px);
  width: 100%;
  min-width: 100%;
  max-width: 100%;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
  box-shadow: 0 16px 28px rgba(15, 23, 42, 0.12);
}

#themeMenuBtn,
#dirMenuBtn {
  width: auto;
  max-width: 190px;
  padding-left: 0.82rem;
  padding-right: 0.82rem;
  justify-content: space-between;
  gap: 0.35rem;
  overflow: hidden;
  text-overflow: ellipsis;
}

#themeMenuWrap:has(> #themeMenu.open) > #themeMenuBtn,
#dirMenuWrap:has(> #dirMenu.open) > #dirMenuBtn {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  border-color: #c8ced6;
  background: #fff;
}

.workspace-panel {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.canvas {
  position: relative;
  flex: 1;
  min-height: 420px;
  overflow: hidden;
  background: linear-gradient(180deg, #fff 0%, #fbfcfd 100%);
}

.viewport {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: 0 0;
  will-change: transform;
}

#svgImg {
  display: block;
  max-width: none;
  user-select: none;
  pointer-events: none;
}

#svgInlineHost {
  display: none;
  max-width: none;
  user-select: none;
  pointer-events: none;
}

#svgInlineHost > svg {
  display: block;
  max-width: none;
  user-select: none;
  pointer-events: none;
}

.error {
  position: absolute;
  left: 10px;
  bottom: 10px;
  display: none;
  max-width: min(760px, 92%);
  border: 1px solid var(--danger-border);
  border-radius: 10px;
  background: var(--danger-bg);
  color: var(--danger-text);
  padding: 0.45rem 0.58rem;
  font-size: 12px;
}

.error.show {
  display: block;
}

.top-actions button:focus-visible,
.toolbar button:focus-visible,
.toolbar .dropdown-btn:focus-visible,
.toolbar a.btn:focus-visible,
.chat-input textarea:focus-visible,
.chat-input #btnSend:focus-visible,
.rail-collapse-btn:focus-visible,
.new-thread-btn:focus-visible,
.thread-item:focus-visible,
.thread-section-toggle:focus-visible,
.thread-item-btn:focus-visible,
.thread-folder-menu-item:focus-visible,
.suggestion:focus-visible,
.workspace-topbar #btnCloseCanvas:focus-visible {
  outline: 2px solid rgba(16, 163, 127, 0.3);
  outline-offset: 2px;
}

@media (max-width: 1140px) {
  .app {
    grid-template-columns: 240px minmax(0, 1fr);
  }

  .preview {
    inset: 16px;
  }
}

@media (max-width: 900px) {
  .app {
    grid-template-columns: minmax(0, 1fr);
    padding: 0;
  }

  .thread-rail {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: min(88vw, 320px);
    z-index: 80;
    transform: translateX(-110%);
    transition: transform 0.22s ease;
    border: 0;
    border-right: 1px solid var(--line);
    border-radius: 0;
    box-shadow: none;
  }

  body.rail-open .thread-rail {
    transform: translateX(0);
  }

  .chat-toggle {
    display: none;
    appearance: none;
    border: 1px solid var(--line-strong);
    background: #fff;
    color: var(--text);
    border-radius: 999px;
    padding: 0.38rem 0.7rem;
    font: 600 12.4px/1 "Instrument Sans", "Avenir Next", "Segoe UI", sans-serif;
    cursor: pointer;
  }

  .rail-collapse-btn-topbar {
    display: inline-flex;
  }

  .rail-collapse-btn-rail {
    display: none;
  }

  body.rail-open .rail-collapse-btn-topbar {
    display: none;
  }

  body.rail-open .thread-rail .rail-collapse-btn-rail {
    display: inline-flex;
  }

  .preview {
    inset: 10px;
  }

  .toolbar {
    margin-left: 0;
  }

  .chat-workspace {
    border-radius: 0;
    border: 0;
    box-shadow: none;
    background: var(--panel);
  }

  .topbar,
  .chat {
    border-radius: 0;
  }

  .auth-controls {
    order: 3;
  }

  .top-actions {
    flex-wrap: wrap;
    justify-content: flex-end;
  }

  .auth-status-pill {
    max-width: 120px;
  }

  .auth-nudge {
    flex-wrap: wrap;
    align-items: flex-start;
  }

  .app-modal-backdrop {
    align-items: flex-end;
    padding: 10px;
  }

  .app-modal {
    width: min(100%, 560px);
    border-radius: 16px;
  }

  .app-modal-footer {
    gap: 8px;
  }
}

@media (min-width: 901px) {
  .rail-collapse-btn-topbar {
    display: none;
  }

  .thread-rail .rail-collapse-btn-rail {
    display: inline-flex;
  }

  body.rail-collapsed .rail-collapse-btn-topbar {
    display: inline-flex;
  }

  body.rail-collapsed .thread-rail .rail-collapse-btn-rail {
    display: none;
  }

  .chat-workspace {
    border: 0;
    border-radius: 0;
    box-shadow: none;
  }

  .app {
    transition: grid-template-columns 0.18s ease;
  }

  body.rail-collapsed .app {
    grid-template-columns: 0 minmax(0, 1fr);
  }

  body.rail-collapsed .thread-rail {
    min-width: 0;
    width: 0;
    border-right: 0;
    opacity: 0;
    pointer-events: none;
  }
}

@media (hover: none) and (pointer: coarse) {
  /* Prevent iOS Safari from zooming the page when the chat input is focused. */
  .chat-input textarea {
    font-size: 16px;
  }

  .thread-row .thread-item {
    padding-right: 12px;
  }

  .thread-item-controls {
    display: none;
  }

  .thread-section-actions .thread-item-btn.icon.section {
    display: none;
  }
}
