html,
body {
  height: 100%;
  margin: 0;
}

body {
  display: grid;
  grid-template-rows: auto 1fr auto;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: var(--fs-md);
  line-height: 1.75;
  -webkit-font-smoothing: antialiased;
}

[hidden] {
  display: none !important;
}

/* UI icons only; chart SVGs keep their own styling */
:where(svg:not(.chart svg)) {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: var(--r-sm);
  font: inherit;
  font-size: var(--fs-sm);
  font-weight: 500;
  line-height: 1;
  padding: 9px 12px;
  cursor: pointer;
  color: var(--ink);
  background: transparent;
}
.btn svg {
  width: 16px;
  height: 16px;
}
.btn-quiet {
  color: var(--ink-2);
  border-color: var(--line);
  background: var(--surface);
}
.btn-quiet:hover {
  color: var(--ink);
  border-color: var(--water);
}
.btn-link {
  padding: 4px 0;
  color: var(--water-ink);
  font-size: var(--fs-sm);
}
.btn-link:hover {
  text-decoration: underline;
  text-underline-offset: 3px;
}

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.brand h1 {
  margin: 0;
  font-size: var(--fs-md);
  font-weight: 700;
  white-space: nowrap;
}
.brand-mark {
  width: 22px;
  height: 22px;
  fill: var(--water);
  stroke: none;
}
.brand-mark-line {
  fill: none;
  stroke: var(--surface);
  stroke-width: 1.6;
}
.conn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: var(--fs-xs);
  white-space: nowrap;
}
.conn-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
}
.conn[data-state='ok'] .conn-dot {
  background: #1f9d6a;
}
.conn[data-state='down'] .conn-dot {
  background: var(--critical);
}
.conn[data-state='down'] {
  color: var(--critical);
}

/* ---------- thread ---------- */
.thread {
  overflow-y: auto;
  scroll-behavior: smooth;
  padding: 28px 20px 40px;
}
.thread > * {
  max-width: 860px;
  margin-inline: auto;
}

/* empty state */
.empty {
  padding-top: 8vh;
}
.empty h2 {
  margin: 0 0 8px;
  font-size: var(--fs-2xl);
  line-height: 1.3;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.empty p {
  margin: 0 0 28px;
  max-width: 34em;
  color: var(--ink-2);
}
.suggestions {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 8px;
}
.suggestion {
  text-align: start;
  font: inherit;
  font-size: var(--fs-md);
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  padding: 12px 14px;
  cursor: pointer;
  line-height: 1.6;
}
.suggestion:hover {
  border-color: var(--water);
  background: var(--water-soft);
}

/* ---------- one exchange: question + answer ---------- */
.exchange {
  padding-bottom: 36px;
}
.exchange + .exchange {
  padding-top: 32px;
  border-top: 1px solid var(--line);
}

.question {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin: 0 0 18px;
  font-size: var(--fs-xl);
  font-weight: 600;
  line-height: 1.5;
  color: var(--ink);
  overflow-wrap: anywhere;
}
.question .src {
  flex: none;
  display: inline-grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--brass-soft);
  color: var(--brass);
  transform: translateY(3px);
}
.question .src svg {
  width: 15px;
  height: 15px;
}

.answer {
  font-size: var(--fs-md);
}

/* status: a pipe that fills stage by stage */
.status {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 4px 0 18px;
  color: var(--ink-2);
  font-size: var(--fs-sm);
}
.pipe {
  flex: none;
  display: grid;
  grid-template-columns: repeat(3, 28px);
  gap: 3px;
}
.pipe span {
  position: relative;
  height: 6px;
  border-radius: 3px;
  background: var(--line);
  overflow: hidden;
}
.pipe span::after {
  content: '';
  position: absolute;
  inset: 0;
  background: var(--water);
  transform: scaleX(0);
  transform-origin: right;
  transition: transform 0.5s ease;
}
.pipe span:dir(ltr)::after {
  transform-origin: left;
}
.pipe span.done::after {
  transform: scaleX(1);
}
.pipe span.active::after {
  transform: scaleX(1);
  background: linear-gradient(90deg, var(--water) 0 40%, var(--water-soft) 50%, var(--water) 60% 100%);
  background-size: 250% 100%;
  animation: flow 1.2s linear infinite;
}
@keyframes flow {
  from {
    background-position: 100% 0;
  }
  to {
    background-position: -150% 0;
  }
}
.status-label {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.status-label b {
  font-weight: 500;
  color: var(--ink);
}

/* markdown body */
.md {
  max-width: 68ch;
  overflow-wrap: anywhere;
}
.md:empty {
  display: none;
}
.md > :first-child {
  margin-top: 0;
}
.md > :last-child {
  margin-bottom: 0;
}
.md p,
.md ul,
.md ol,
.md blockquote,
.md table,
.md pre {
  margin: 0 0 12px;
}
.md ul,
.md ol {
  padding-inline-start: 1.4em;
}
.md li + li {
  margin-top: 4px;
}
.md li::marker {
  color: var(--water);
}
.md strong {
  font-weight: 700;
}
.md h1,
.md h2,
.md h3,
.md h4 {
  font-size: var(--fs-lg);
  line-height: 1.5;
  margin: 18px 0 8px;
}
.md blockquote {
  padding: 8px 14px;
  border-inline-start: 3px solid var(--brass);
  background: var(--brass-soft);
  border-radius: 0 var(--r-sm) var(--r-sm) 0;
  color: var(--ink);
}
.md blockquote:dir(rtl) {
  border-radius: var(--r-sm) 0 0 var(--r-sm);
}
.md blockquote p {
  margin: 0;
}
.md code {
  font-family: var(--mono);
  font-size: 0.86em;
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--surface-2);
  border: 1px solid var(--line-soft);
  direction: ltr;
  unicode-bidi: isolate;
}
.md pre {
  direction: ltr;
  text-align: left;
  overflow-x: auto;
  padding: 12px 14px;
  border-radius: var(--r-md);
  background: var(--surface-2);
}
.md pre code {
  padding: 0;
  border: 0;
  background: none;
}
.md a {
  color: var(--water-ink);
}
.md table {
  border-collapse: collapse;
  font-size: var(--fs-sm);
}
.md th,
.md td {
  padding: 6px 12px;
  border-bottom: 1px solid var(--line);
  text-align: start;
}
.md th {
  font-weight: 600;
  color: var(--ink-2);
}
.streaming .md > :last-child::after {
  content: '';
  display: inline-block;
  width: 7px;
  height: 1em;
  margin-inline-start: 3px;
  vertical-align: -0.15em;
  background: var(--water);
  border-radius: 2px;
  animation: blink 1s steps(2, start) infinite;
}
@keyframes blink {
  to {
    visibility: hidden;
  }
}

/* ---------- evidence: chart + table ---------- */
.evidence {
  margin-top: 20px;
  display: grid;
  gap: 16px;
}
.evidence:empty {
  display: none;
}
.figure {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  overflow: hidden;
}
.figure-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px 0;
}
.figure-title {
  margin: 0;
  font-size: var(--fs-md);
  font-weight: 600;
  line-height: 1.5;
}
.figure-meta {
  flex: none;
  color: var(--muted);
  font-size: var(--fs-xs);
}
.chart {
  height: 300px;
  margin: 4px 8px 8px;
}
.chart.pie {
  height: 280px;
}

.table-scroll {
  overflow: auto;
  max-height: 520px;
  margin-top: 10px;
}
.data {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--fs-sm);
  font-variant-numeric: tabular-nums;
}
.data th,
.data td {
  padding: 8px 16px;
  text-align: start;
  white-space: nowrap;
  border-bottom: 1px solid var(--line-soft);
}
.data th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface-2);
  color: var(--ink-2);
  font-weight: 600;
  border-bottom-color: var(--line);
}
.data td.num,
.data th.num {
  text-align: end;
}
.data tbody tr:hover td {
  background: var(--water-soft);
}
.data tbody tr:last-child td {
  border-bottom: 0;
}
.data tr.max td.num.key {
  font-weight: 700;
}
.table-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 16px 10px;
  border-top: 1px solid var(--line-soft);
  color: var(--muted);
  font-size: var(--fs-xs);
}
.table-foot:empty {
  display: none;
}

/* ---------- answer footer: SQL toggle + timing ---------- */
.answer-foot {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 18px;
  margin-top: 16px;
  color: var(--muted);
  font-size: var(--fs-xs);
}
.answer-foot:empty {
  display: none;
}
.sql-toggle {
  gap: 6px;
}
.sql-toggle svg {
  width: 14px;
  height: 14px;
  transition: transform 0.2s ease;
}
.sql-toggle[aria-expanded='true'] svg {
  transform: rotate(90deg);
}
.sql-toggle:dir(rtl) svg {
  transform: scaleX(-1);
}
.sql-toggle[aria-expanded='true']:dir(rtl) svg {
  transform: scaleX(-1) rotate(90deg);
}
.sql-panel {
  margin-top: 10px;
  display: grid;
  gap: 10px;
}
.sql-panel[hidden] {
  display: none;
}
.sql-item {
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: var(--surface);
  overflow: hidden;
}
.sql-item-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line-soft);
  color: var(--ink-2);
  font-size: var(--fs-xs);
}
.sql-item-head .purpose {
  color: var(--ink);
  font-weight: 500;
}
.sql-item pre {
  margin: 0;
  padding: 12px 14px;
  direction: ltr;
  text-align: left;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.6;
  color: var(--ink);
  background: var(--surface-2);
}
.sql-item pre .kw {
  color: var(--water-ink);
  font-weight: 600;
}
.sql-item pre .str {
  color: var(--brass);
}
.sql-item pre .num {
  color: var(--brass);
}

/* ---------- error ---------- */
.error {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 12px;
  padding: 14px 16px;
  border-radius: var(--r-md);
  background: var(--critical-soft);
  border: 1px solid color-mix(in srgb, var(--critical) 35%, transparent);
  margin-top: 8px;
}
.error > :not(.error-icon) {
  grid-column: 2;
}
.error-icon {
  grid-column: 1;
  grid-row: 1 / span 4;
  width: 22px;
  height: 22px;
  color: var(--critical);
  margin-top: 2px;
}
.error-title {
  margin: 0;
  font-weight: 700;
  color: var(--critical);
}
.error-msg {
  margin: 0;
  color: var(--ink);
}
.error-actions {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-top: 6px;
}
.error-code {
  color: var(--muted);
  font-size: var(--fs-xs);
  font-family: var(--mono);
}

/* ---------- composer ---------- */
.composer-wrap {
  padding: 12px 20px 10px;
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.composer {
  max-width: 860px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow);
}
.composer:focus-within {
  border-color: var(--water);
}
.composer textarea {
  flex: 1;
  min-width: 0;
  resize: none;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: var(--fs-md);
  line-height: 1.6;
  padding: 7px 8px;
  max-height: 180px;
}
.composer textarea::placeholder {
  color: var(--muted);
}
.btn-send {
  padding: 10px 16px;
  border-radius: var(--r-md);
  background: var(--water);
  color: var(--on-water);
  font-size: var(--fs-md);
}
.btn-send:hover {
  background: var(--water-ink);
}
.btn-send:disabled {
  opacity: 0.45;
  cursor: default;
}
.btn-stop {
  padding: 10px 14px;
  border-radius: var(--r-md);
  border-color: var(--line);
  color: var(--ink-2);
}
.btn-stop svg {
  fill: currentColor;
  stroke: none;
}
.hint {
  max-width: 860px;
  margin: 6px auto 0;
  color: var(--muted);
  font-size: var(--fs-xs);
  text-align: center;
}

/* ---------- small windows ---------- */
@media (max-width: 560px) {
  .topbar {
    padding: 8px 16px;
  }
  .conn-label {
    display: none;
  }
  .thread {
    padding: 20px 16px 32px;
  }
  .composer-wrap {
    padding: 10px 16px 8px;
  }
  .empty h2 {
    font-size: var(--fs-xl);
  }
  .question {
    font-size: var(--fs-lg);
  }
  .btn-reset-label {
    display: none;
  }
  .hint {
    display: none;
  }
}

.hint kbd {
  font: inherit;
  font-weight: 600;
  color: var(--ink-2);
  unicode-bidi: isolate;
}

/* ---------- header actions ---------- */
.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}
.btn-icon {
  padding: 9px;
}
button.conn {
  border: 0;
  background: none;
  font: inherit;
  font-size: var(--fs-xs);
  padding: 4px 6px;
  border-radius: var(--r-sm);
  cursor: pointer;
}
button.conn:hover {
  background: var(--surface-2);
}
.conn[data-state='auth'] .conn-dot {
  background: var(--brass);
}
.conn[data-state='auth'] {
  color: var(--brass);
}
body.is-web .electron-only {
  display: none;
}

/* ---------- composer mic ---------- */
.btn-mic {
  --lvl: 0;
  position: relative;
  padding: 10px;
  border-radius: var(--r-md);
  color: var(--ink-2);
  border-color: var(--line);
}
.btn-mic svg {
  width: 18px;
  height: 18px;
}
.btn-mic:hover {
  color: var(--water-ink);
  border-color: var(--water);
}
.btn-mic[data-state='recording'] {
  color: #2a1d05;
  background: #d9a441;
  border-color: #d9a441;
  box-shadow: 0 0 0 calc(2px + var(--lvl) * 8px) color-mix(in srgb, #d9a441 35%, transparent);
  transition: box-shadow 0.08s linear;
}
.btn-mic[data-state='busy'] {
  color: var(--water);
  border-color: var(--water);
}
.btn-mic[data-state='busy'] svg {
  animation: pulse-op 1s ease-in-out infinite;
}
@keyframes pulse-op {
  50% {
    opacity: 0.35;
  }
}
.mic-time {
  font-size: var(--fs-xs);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.mic-time:empty {
  display: none;
}
.composer-note {
  max-width: 860px;
  margin: 6px auto 0;
  font-size: var(--fs-xs);
  color: var(--ink-2);
  text-align: center;
}
.composer-note.err {
  color: var(--critical);
}

/* ---------- settings dialog ---------- */
.settings {
  width: min(520px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 24px 60px -20px rgba(4, 24, 30, 0.45);
}
.settings::backdrop {
  background: rgba(8, 24, 30, 0.45);
}
.settings form {
  display: grid;
  gap: 18px;
  padding: 22px 22px 18px;
}
.settings h2 {
  margin: 0;
  font-size: var(--fs-lg);
  font-weight: 700;
}
.field {
  display: grid;
  gap: 6px;
}
.field-label {
  font-size: var(--fs-sm);
  font-weight: 600;
}
.field-label .muted {
  color: var(--muted);
  font-weight: 400;
}
.field input {
  width: 100%;
  font: inherit;
  font-size: var(--fs-sm);
  font-family: var(--mono);
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  padding: 9px 10px;
}
.field input:focus {
  outline: 2px solid var(--water);
  outline-offset: -1px;
  border-color: var(--water);
}
.field-help {
  font-size: var(--fs-xs);
  color: var(--muted);
  line-height: 1.9;
}
.help-opts {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 16px;
}
.help-opt {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}
.chip {
  font: inherit;
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--water-ink);
  background: var(--water-soft);
  border: 0;
  border-radius: 4px;
  padding: 1px 6px;
  cursor: pointer;
}
.token-row {
  display: flex;
  gap: 8px;
}
.token-row input {
  flex: 1;
  min-width: 0;
}
.test-result {
  display: grid;
  gap: 4px;
  font-size: var(--fs-sm);
}
.test-result:empty {
  display: none;
}
.test-line {
  display: flex;
  gap: 8px;
  align-items: baseline;
}
.test-line .mark {
  flex: none;
  width: 18px;
  font-weight: 700;
  text-align: center;
}
.test-line.ok .mark {
  color: #1f9d6a;
}
.test-line.bad .mark {
  color: var(--critical);
}
.test-line.wait .mark {
  color: var(--muted);
}
.dialog-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}
.dialog-actions .spacer {
  flex: 1;
}
.dialog-actions .btn-send {
  font-size: var(--fs-sm);
  padding: 9px 18px;
}

/* ---------- 401 card extras ---------- */
.error-en {
  margin: 0;
  color: var(--ink-2);
  font-size: var(--fs-sm);
}
.error .btn-link.primary {
  font-weight: 700;
}
