:root {
  --app-header-height: 64px;
  --app-sidebar-width: 280px;
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body { margin: 0; background: var(--bs-tertiary-bg); }
button, input, textarea, select { font: inherit; }
.hidden { display: none !important; }

.app-icon,
.empty-icon {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  color: var(--bs-primary);
  background: var(--bs-primary-bg-subtle);
  border-radius: var(--bs-border-radius-lg);
}

.app-icon svg,
.empty-icon svg {
  width: 26px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.app-icon-sm { width: 36px; height: 36px; border-radius: var(--bs-border-radius); }
.app-icon-sm svg { width: 21px; }

.login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--bs-tertiary-bg);
}

.login-card { width: min(440px, 100%); }
.form-error { min-height: 1.25rem; }

.app-shell {
  height: 100vh;
  display: grid;
  grid-template: var(--app-header-height) minmax(0, 1fr) / var(--app-sidebar-width) minmax(0, 1fr);
  overflow: hidden;
}

.topbar {
  grid-column: 1 / -1;
  min-height: var(--app-header-height);
  display: grid;
  grid-template-columns: var(--app-sidebar-width) minmax(280px, 640px) 1fr;
  gap: 20px;
  align-items: center;
  z-index: 10;
  min-width: 0;
}

.search-box { width: 100%; min-width: 0; max-width: 640px; }
.search-box .form-control { min-width: 0; }
.search-box svg { width: 17px; fill: none; stroke: currentColor; stroke-width: 1.8; }
.search-box button:hover { color: var(--bs-primary); }
.account-area { justify-self: end; white-space: nowrap; }

.thread-rail {
  grid-row: 2;
  display: flex;
  min-height: 0;
  flex-direction: column;
  padding: 16px;
  min-width: 0;
}

.rail-heading { padding-bottom: 12px; }
.rail-heading .btn { padding-inline: .5rem; font-size: .72rem; }
.thread-list { flex: 1; min-height: 0; overflow-y: auto; gap: 2px; }
.thread-item {
  width: 100%;
  padding: 8px 9px;
  border: 1px solid transparent;
  border-radius: var(--bs-border-radius);
  background: transparent;
  text-align: left;
  line-height: 1.3;
}
.thread-item:hover { background: var(--bs-white); border-color: var(--bs-border-color); }
.thread-item.active { color: var(--bs-primary-text-emphasis); background: var(--bs-primary-bg-subtle); border-color: var(--bs-primary-border-subtle); }
.thread-title-row { display: flex; align-items: center; gap: 5px; min-width: 0; }
.thread-title-row strong { min-width: 0; flex: 1; }
.thread-index { color: var(--bs-primary); font-size: .65rem; font-weight: 700; white-space: nowrap; }
.thread-item strong { display: block; font-size: .8125rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-owner { display: block; margin-top: 2px; color: var(--bs-secondary-color); font-size: .65rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-item footer { display: flex; justify-content: space-between; margin-top: 3px; color: var(--bs-secondary-color); font-size: .6875rem; }
.thread-item .closed { color: var(--bs-danger); }
.empty-list { padding: 12px; color: var(--bs-secondary-color); font-size: .875rem; }

.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--bs-success); }
.repo-revision { display: grid; gap: 5px; }
.repo-revision div { display: grid; grid-template-columns: 48px minmax(0, 1fr); align-items: baseline; gap: 8px; min-width: 0; }
.repo-revision dt { color: var(--bs-secondary-color); font-family: var(--bs-font-monospace); font-size: .625rem; font-weight: 600; letter-spacing: .04em; text-transform: uppercase; }
.repo-revision dd { min-width: 0; margin: 0; overflow: hidden; color: var(--bs-secondary-color); font-size: .6875rem; text-overflow: ellipsis; white-space: nowrap; }
.repo-revision code { color: inherit; font-size: inherit; }

.workspace { grid-row: 2; min-width: 0; min-height: 0; overflow: hidden; }
.empty-state {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px;
  text-align: center;
}
.empty-state p { max-width: 620px; }

.chat-view { height: 100%; display: grid; grid-template-rows: auto minmax(0, 1fr) auto; }
.chat-header { display: flex; align-items: flex-end; justify-content: space-between; padding: 18px 24px; }
.thread-number-badge { padding: 2px 7px; border-radius: var(--bs-border-radius-pill); background: var(--bs-primary-bg-subtle); color: var(--bs-primary-text-emphasis); font-size: .72rem; font-weight: 700; white-space: nowrap; }
.turn-meter { display: grid; justify-items: end; gap: 7px; }
.turn-dots { display: flex; gap: 4px; }
.turn-dots i { display: block; width: 20px; height: 4px; border-radius: 2px; background: var(--bs-secondary-bg); }
.turn-dots i.used { background: var(--bs-primary); }

.message-list { overflow-y: auto; padding: 24px clamp(16px, 5vw, 72px); scroll-behavior: smooth; }
.message { max-width: 920px; margin: 0 auto 24px; }
.message.user { max-width: 760px; margin-right: 0; }
.message-meta { display: flex; align-items: center; gap: 8px; margin-bottom: 7px; color: var(--bs-secondary-color); font-size: .75rem; }
.chat-number { padding: 1px 6px; border: 1px solid var(--bs-border-color); border-radius: var(--bs-border-radius-pill); background: var(--bs-white); color: var(--bs-secondary-color); font-size: .68rem; }
.seal { display: grid; place-items: center; min-width: 28px; height: 28px; padding: 0 5px; border-radius: 50%; background: var(--bs-secondary-bg); color: var(--bs-secondary-color); font-weight: 600; }
.message.assistant .seal { color: var(--bs-primary); background: var(--bs-primary-bg-subtle); }
.message-body {
  padding: 16px 18px;
  border: 1px solid var(--bs-border-color);
  border-radius: var(--bs-border-radius-lg);
  background: var(--bs-white);
  line-height: 1.75;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.markdown-body { white-space: normal; }
.markdown-body > :first-child { margin-top: 0; }
.markdown-body > :last-child { margin-bottom: 0; }
.markdown-body p { margin: 0 0 .85rem; }
.markdown-body h3, .markdown-body h4, .markdown-body h5, .markdown-body h6 { margin: 1.1rem 0 .55rem; font-weight: 600; }
.markdown-body h3 { font-size: 1.1rem; }
.markdown-body h4, .markdown-body h5, .markdown-body h6 { font-size: 1rem; }
.markdown-body ul, .markdown-body ol { margin: 0 0 .85rem; padding-left: 1.4rem; }
.markdown-body li + li { margin-top: .3rem; }
.markdown-body blockquote { margin: 0 0 .85rem; padding: .5rem .75rem; border-left: 3px solid var(--bs-primary-border-subtle); background: var(--bs-tertiary-bg); color: var(--bs-secondary-color); }
.markdown-body code { padding: .12rem .35rem; border-radius: var(--bs-border-radius-sm); background: var(--bs-secondary-bg); color: var(--bs-emphasis-color); }
.markdown-body a { overflow-wrap: anywhere; }
.message.user .message-body { color: var(--bs-primary-text-emphasis); background: var(--bs-primary-bg-subtle); border-color: var(--bs-primary-border-subtle); }
.message.error .message-body { color: var(--bs-danger-text-emphasis); background: var(--bs-danger-bg-subtle); border-color: var(--bs-danger-border-subtle); }
.thinking { display: inline-flex; align-items: center; gap: 5px; }
.thinking i { width: 6px; height: 6px; border-radius: 50%; background: var(--bs-primary); animation: pulse 1.2s infinite; }
.thinking i:nth-child(2) { animation-delay: .15s; }
.thinking i:nth-child(3) { animation-delay: .3s; }

.composer { margin: 0 clamp(12px, 4vw, 56px) 20px; border-color: var(--bs-border-color); }
.composer textarea { min-height: 74px; resize: none; }
.composer textarea:focus { box-shadow: 0 0 0 .2rem rgba(var(--bs-primary-rgb), .16); }
.composer-bottom { min-height: 50px; }

.search-results-view { height: 100%; overflow-y: auto; padding: 32px clamp(20px, 6vw, 80px); }
.search-results-header, .search-results { max-width: 960px; margin-inline: auto; }
.search-filter-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
.admin-search-filters .form-label { margin-bottom: 4px; color: var(--bs-secondary-color); }
.search-results { display: grid; gap: 16px; }
.search-result-card { overflow: hidden; border: 1px solid var(--bs-border-color); border-radius: var(--bs-border-radius-lg); background: var(--bs-white); }
.search-result-card > button { width: 100%; display: flex; justify-content: space-between; gap: 16px; padding: 16px 18px; border: 0; border-bottom: 1px solid var(--bs-border-color); background: var(--bs-white); text-align: left; }
.search-result-card > button:hover { background: var(--bs-tertiary-bg); }
.search-result-card h3 { margin: 0; font-size: 1rem; }
.search-result-title { display: flex; align-items: center; gap: 7px; }
.search-thread-number { color: var(--bs-primary); font-size: .72rem; font-weight: 700; white-space: nowrap; }
.search-status { display: inline-block; margin-top: 5px; padding: 2px 6px; border-radius: var(--bs-border-radius-pill); background: var(--bs-secondary-bg); color: var(--bs-secondary-color); font-size: .68rem; }
.search-owner { display: block; margin-top: 4px; color: var(--bs-secondary-color); font-size: .75rem; }
.match-count { color: var(--bs-primary); font-size: .8rem; white-space: nowrap; }
.match-context { padding: 10px 18px 14px; }
.context-line { display: grid; grid-template-columns: 64px 1fr; gap: 12px; padding: 8px 0; color: var(--bs-secondary-color); font-size: .875rem; line-height: 1.6; }
.context-line.match { margin-inline: -8px; padding-inline: 8px; border-radius: var(--bs-border-radius); background: var(--bs-warning-bg-subtle); color: var(--bs-body-color); }
.context-line b { color: var(--bs-secondary-color); font-size: .7rem; }
mark { padding: 0 2px; background: var(--bs-warning-bg-subtle); }

dialog { padding: 0; border: 0; background: transparent; color: var(--bs-body-color); }
dialog::backdrop { background: rgba(0, 0, 0, .45); }
.admin-dialog, .credential-dialog { max-height: calc(100dvh - 32px); overflow-y: auto; }
.admin-dialog { width: min(1040px, calc(100vw - 32px)); }
.credential-dialog { width: min(480px, calc(100vw - 32px)); }
.admin-grid { display: grid; grid-template-columns: 320px minmax(0, 1fr); gap: 20px; }
.user-ledger { overflow-x: auto; }
.ledger-header, .user-row { min-width: 680px; display: grid; grid-template-columns: minmax(140px, 1.5fr) .6fr .7fr minmax(160px, .9fr); gap: 12px; align-items: center; }
.user-row { padding: 13px 16px; font-size: .875rem; }
.user-row strong, .user-row small { display: block; }
.user-row small { color: var(--bs-secondary-color); }
.role-stamp { width: max-content; padding: 4px 8px; border-radius: var(--bs-border-radius-pill); background: var(--bs-secondary-bg); color: var(--bs-secondary-color); font-size: .75rem; }
.toggle { width: max-content; padding: 3px 8px; border: 1px solid var(--bs-success-border-subtle); border-radius: var(--bs-border-radius); background: var(--bs-success-bg-subtle); color: var(--bs-success-text-emphasis); font-size: .75rem; }
.toggle.inactive { border-color: var(--bs-danger-border-subtle); background: var(--bs-danger-bg-subtle); color: var(--bs-danger-text-emphasis); }
.toggle:disabled { opacity: .5; }
.user-reset-button { white-space: nowrap; }
.credential-card dl { margin: 18px 0; }
.credential-card dl div { padding: 12px 14px; border-bottom: 1px solid var(--bs-border-color); }
.credential-card dl div:last-child { border-bottom: 0; }
.credential-card dt { color: var(--bs-secondary-color); font-size: .75rem; }
.credential-card dd { margin: 4px 0 0; overflow-wrap: anywhere; font-family: var(--bs-font-monospace); }

.app-toast { position: fixed; right: 20px; bottom: 20px; z-index: 2000; max-width: min(420px, calc(100vw - 40px)); margin: 0; opacity: 0; transform: translateY(10px); pointer-events: none; transition: .2s ease; }
.app-toast.show { opacity: 1; transform: translateY(0); }

@keyframes pulse {
  0%, 70%, 100% { opacity: .25; transform: translateY(0); }
  35% { opacity: 1; transform: translateY(-2px); }
}

@media (max-width: 900px) {
  :root { --app-sidebar-width: 240px; }
  .topbar { gap: 12px; }
  .account-area #user-name { display: none; }
  .admin-grid { grid-template-columns: 1fr; }
  .search-filter-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 700px) {
  .app-shell { height: 100dvh; grid-template: auto 154px minmax(0, 1fr) / 1fr; }
  .topbar { grid-column: 1; display: grid; grid-template-columns: 1fr auto; gap: 10px; padding-block: 10px; }
  .navbar-brand { min-width: 0; }
  .navbar-brand > .fw-semibold { display: none; }
  .search-box { grid-column: 1 / -1; grid-row: 2; max-width: none; }
  .account-area { grid-column: 2; grid-row: 1; }
  .thread-rail { grid-column: 1; grid-row: 2; padding: 10px 12px; border-right: 0 !important; border-bottom: 1px solid var(--bs-border-color); }
  .thread-list { display: flex; flex-direction: row; overflow-x: auto; overflow-y: hidden; }
  .thread-item { min-width: 180px; max-width: 220px; background: var(--bs-white); border-color: var(--bs-border-color); }
  .repo-card { display: none; }
  .workspace { grid-column: 1; grid-row: 3; }
  .chat-header { padding: 14px 16px; }
  .message-list { padding: 18px 12px; }
  .composer { margin: 0 10px 10px; }
  .composer-bottom p { display: none; }
  .composer-bottom { justify-content: flex-end !important; }
  .search-results-view { padding: 20px 14px; }
  .context-line { grid-template-columns: 52px 1fr; gap: 8px; }
  .admin-dialog { width: calc(100vw - 20px); }
  .search-filter-grid { grid-template-columns: 1fr; }
}
