/* sbtools: top bar, login page and responsive fixes on top of Datasette's app.css */

:root {
  --sbt-bar-bg: #172033;
  --sbt-bar-fg: #f1f5f9;
  --sbt-bar-muted: #cbd5e1;
  --sbt-crumbs-bg: #0f172a;
  --sbt-prod: #dc2626;
  --sbt-prod-dark: #991b1b;
  --sbt-qc: #f59e0b;
  --sbt-qc-dark: #92400e;
  --sbt-none: #64748b;
  --sbt-accent: #2563eb;
  --sbt-env: var(--sbt-none);
}
body.sbt-env-prod { --sbt-env: var(--sbt-prod); }
body.sbt-env-qc { --sbt-env: var(--sbt-qc); }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 1rem;
  line-height: 1.5;
  color: #111a35;
  background-color: #f8fafb;
}

/* ---- top bar ----------------------------------------------------------- */
header.sbt-bar {
  padding: 0;
  min-height: 0;
  overflow: visible;
  background: var(--sbt-bar-bg);
  color: var(--sbt-bar-fg);
  border-top: 6px solid var(--sbt-env);
}
.sbt-bar-inner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 1rem;
  padding: 0.5rem 1rem;
}
header.sbt-bar a.sbt-brand,
header.sbt-bar a.sbt-brand:visited {
  color: #fff;
  font-weight: 700;
  font-size: 1.05rem;
  text-decoration: none;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  min-height: 32px;
}
.sbt-brand-sep { color: var(--sbt-bar-muted); padding: 0 0.15em; }
.sbt-brand:hover, .sbt-brand:focus-visible { text-decoration: underline; }

.sbt-env { display: flex; align-items: center; gap: 0.4rem; }
.sbt-env-label {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--sbt-bar-muted);
}
.sbt-env-select {
  font: inherit;
  font-size: 1rem;
  font-weight: 700;
  min-height: 36px;
  padding: 0.25rem 2rem 0.25rem 0.6rem;
  border-radius: 6px;
  border: 2px solid #94a3b8;
  background-color: #334155;
  color: #fff;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='white' d='M6 8.5 1.5 4h9z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.6rem center;
}
.sbt-env-select-prod { background-color: var(--sbt-prod); border-color: #fecaca; }
.sbt-env-select-qc {
  background-color: var(--sbt-qc);
  border-color: #fde68a;
  color: #1c1300;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%231c1300' d='M6 8.5 1.5 4h9z'/%3E%3C/svg%3E");
}
.sbt-env-select option { background: #fff; color: #111; font-weight: 400; }
.sbt-env-select:focus-visible { outline: 3px solid #93c5fd; outline-offset: 2px; }

.sbt-snap {
  flex: 0 1 auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.35rem 0.75rem;
  font-size: 0.95rem;
  color: var(--sbt-bar-fg);
  min-width: 0;
}
.sbt-snap time { font-weight: 700; color: #fff; }
.sbt-rel { color: var(--sbt-bar-muted); white-space: nowrap; }
.sbt-snap-all { gap: 0.25rem 1rem; }
.sbt-snap-item { white-space: normal; }
.sbt-pill {
  display: inline-block;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 0.05rem 0.45rem;
  border-radius: 999px;
  margin-right: 0.25rem;
}
.sbt-pill-prod { background: var(--sbt-prod); color: #fff; }
.sbt-pill-qc { background: var(--sbt-qc); color: #1c1300; }

.sbt-sync { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 0.35rem 0.6rem; }
.sbt-sync-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  min-height: 32px;
  padding: 0.3rem 0.8rem;
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  color: #fff;
  background: var(--sbt-accent);
  border: 1px solid #93c5fd;
  border-radius: 6px;
  cursor: pointer;
}
.sbt-sync-btn:hover:not(:disabled) { background: #1d4ed8; }
.sbt-sync-btn:focus-visible { outline: 3px solid #93c5fd; outline-offset: 2px; }
.sbt-sync-btn:disabled { background: #334155; color: #94a3b8; border-color: #475569; cursor: not-allowed; }
.sbt-sync-btn.is-busy svg { animation: sbt-spin 1s linear infinite; }
@keyframes sbt-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .sbt-sync-btn.is-busy svg { animation: none; } }
.sbt-sync-note { font-size: 0.85rem; color: var(--sbt-bar-muted); }
.sbt-sync-note.is-error { color: #fecaca; font-weight: 600; }
.sbt-sync-note.is-ok { color: #bbf7d0; font-weight: 600; }

.sbt-user { display: flex; align-items: center; gap: 0.6rem; margin-left: auto; }
.sbt-user-name { display: inline-flex; align-items: center; gap: 0.3rem; font-weight: 600; white-space: nowrap; }
.sbt-logout { margin: 0; display: inline; }
.sbt-logout-btn {
  min-height: 32px;
  padding: 0.25rem 0.75rem;
  font: inherit;
  font-size: 0.9rem;
  color: #fff;
  background: transparent;
  border: 1px solid #64748b;
  border-radius: 6px;
  cursor: pointer;
}
.sbt-logout-btn:hover, .sbt-logout-btn:focus-visible { background: #334155; border-color: #cbd5e1; }

nav.sbt-crumbs {
  background: var(--sbt-crumbs-bg);
  padding: 0.15rem 1rem;
  font-size: 0.95rem;
  min-height: 0;
}
nav.sbt-crumbs:empty { display: none; }
header.sbt-bar .crumbs { float: none; margin: 0; }
header.sbt-bar .crumbs a,
header.sbt-bar .crumbs a:visited {
  color: #bfdbfe;
  text-decoration: underline;
  display: inline-block;
  padding: 0.2rem 0.1rem;
  min-height: 24px;
}
header.sbt-bar .crumbs a:hover { color: #fff; }

/* PROD vs QC also on the page heading */
body.sbt-env-prod .page-header,
body.sbt-env-prod.query h1,
body.sbt-env-prod.row h1 { border-left-color: var(--sbt-prod) !important; }
body.sbt-env-qc .page-header,
body.sbt-env-qc.query h1,
body.sbt-env-qc.row h1 { border-left-color: var(--sbt-qc) !important; }

.sbt-message { margin: 0.75rem 1rem 0; border-radius: 6px; }

/* ---- content ------------------------------------------------------------ */
section.content { margin: 0; padding: 0 1rem; max-width: 100%; }
h1, .page-header h1 { font-size: 1.75rem; overflow-wrap: anywhere; }
pre, .wrapped-sql, pre#sql-query { white-space: pre-wrap; overflow-wrap: anywhere; max-width: 100%; }
.db-table p em { overflow-wrap: anywhere; }

/* result tables scroll inside their own box, never the page */
.table-wrapper {
  overflow-x: auto;
  max-width: 100%;
  -webkit-overflow-scrolling: touch;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  background: #fff;
}
/* one line per row where possible; sbtools.js marks long values (.sbt-long)
   so they wrap in a bounded column instead of stretching the table */
table.rows-and-columns { width: max-content; min-width: 100%; }
table.rows-and-columns td.sbt-long { width: min(28em, 80vw); min-width: min(16em, 70vw); overflow-wrap: anywhere; }
table.rows-and-columns th { background: #f1f5f9; padding: 6px 1em 6px 6px; text-align: left; }
table.rows-and-columns td { padding: 4px 6px; }
table.rows-and-columns th a { display: inline-block; min-height: 24px; }

/* SQL editor full width */
form.sql .CodeMirror, form.sql textarea { width: 100%; box-sizing: border-box; }
form.sql textarea { font-size: 1rem; }
.CodeMirror { font-size: 0.95rem; }
form.sql input[type=submit], form.sql button { min-height: 36px; font-size: 1rem; }

/* every link and control gets a tap target of at least 24 x 24 px */
section.content a, .ft a, .sbt-message a {
  display: inline-block;
  min-height: 24px;
  min-width: 24px;
  line-height: 24px;
}
section.content h1 a, section.content h2 a, section.content h3 a { line-height: inherit; }
.ft { line-height: 2; }
form input[type=submit], form button[type=button] { min-height: 32px; }
.select-wrapper select { min-height: 36px; }
input[type=checkbox], input[type=radio] { width: 24px; height: 24px; margin: 0 0.3em 0 0; vertical-align: middle; }

/* index page: the same PROD red / QC amber as the top bar */
body.index h2:has(> a[href$="/prod"]) { border-left-color: var(--sbt-prod) !important; }
body.index h2:has(> a[href$="/qc"]) { border-left-color: var(--sbt-qc) !important; }

/* ---- login page ---------------------------------------------------------- */
.sbt-login-wrap { display: flex; justify-content: center; padding: 2.5rem 0 1rem; }
.sbt-login-card {
  width: 100%;
  max-width: 26rem;
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  box-shadow: 0 6px 24px rgba(15, 23, 42, 0.08);
  padding: 1.5rem 1.5rem 1.25rem;
  box-sizing: border-box;
}
.sbt-login-card h1 { margin: 0 0 0.5rem; font-size: 1.6rem; }
.sbt-login-intro { color: #475569; font-size: 0.95rem; }
.sbt-login-error {
  background: #fef2f2;
  color: #991b1b;
  border: 1px solid #fecaca;
  border-radius: 6px;
  padding: 0.6rem 0.8rem;
  font-weight: 600;
}
form.sbt-login-form label {
  display: block;
  width: auto;
  font-weight: 600;
  font-size: 0.95rem;
  margin: 0.9rem 0 0.3rem;
}
form.sbt-login-form input[type=text],
form.sbt-login-form input[type=password] {
  display: block;
  width: 100%;
  box-sizing: border-box;
  min-height: 44px;
  padding: 0.5rem 0.75rem;
  font: inherit;
  font-size: 1rem;
  border: 1px solid #94a3b8;
  border-radius: 8px;
  background: #fff;
}
form.sbt-login-form input:focus-visible { outline: 3px solid #93c5fd; outline-offset: 1px; border-color: var(--sbt-accent); }
.sbt-login-submit {
  display: block;
  width: 100%;
  min-height: 44px;
  margin-top: 1.25rem;
  font: inherit;
  font-size: 1rem;
  font-weight: 700;
  color: #fff;
  background: var(--sbt-accent);
  border: 0;
  border-radius: 8px;
  cursor: pointer;
}
.sbt-login-submit:hover, .sbt-login-submit:focus-visible { background: #1d4ed8; }
.sbt-login-foot { margin: 1rem 0 0; font-size: 0.85rem; color: #64748b; }
body.sbt-login-page .sbt-message { max-width: 26rem; margin: 1rem auto 0; }

/* ---- narrower screens ---------------------------------------------------- */
@media (max-width: 1400px) {
  .sbt-env-label { /* visually hidden, still names the select */
    position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
    overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
  }
}
@media (max-width: 1100px) {
  /* row 1: brand, environment, user; row 2: snapshot time + sync */
  .sbt-brand { order: 1; }
  .sbt-env { order: 2; }
  .sbt-user { order: 3; }
  .sbt-snap { order: 4; flex-basis: 100%; }
}
@media (max-width: 760px) {
  .sbt-bar-inner { gap: 0.45rem 0.75rem; padding: 0.5rem 0.75rem; }
  .sbt-brand { flex-basis: 100%; }
  section.content { padding: 0 0.75rem; }
  nav.sbt-crumbs { padding: 0.15rem 0.75rem; }
  h1, .page-header h1 { font-size: 1.45rem; }
}
@media (max-width: 420px) {
  header.sbt-bar a.sbt-brand { font-size: 1rem; }
  .sbt-user-name svg { display: none; }
  .sbt-login-card { padding: 1.25rem 1rem 1rem; }
  .sbt-login-wrap { padding-top: 1.25rem; }
}

@media only screen and (max-width: 576px) {
  /* keep real tables (Datasette turns them into cards) and scroll them
     sideways; the single-record row page keeps Datasette's card layout */
  body:not(.row) table.rows-and-columns { display: table; }
  body:not(.row) .rows-and-columns thead { display: table-header-group; }
  body:not(.row) .rows-and-columns tbody { display: table-row-group; }
  body:not(.row) .rows-and-columns tr { display: table-row; border: 0; margin: 0; border-radius: 0; background: none; padding: 0; }
  body:not(.row) .rows-and-columns th, body:not(.row) .rows-and-columns td { display: table-cell; }
  body:not(.row) .rows-and-columns thead tr { position: static; }
  body:not(.row) .rows-and-columns td {
    padding: 4px 6px;
    border-top: 1px solid #aaa;
    border-right: 1px solid #eee;
    border-bottom: 0;
  }
  body:not(.row) .rows-and-columns td:before { content: none !important; display: none !important; }
  body.row table.rows-and-columns { width: auto; min-width: 0; }
  body.row table.rows-and-columns td.sbt-long { width: auto; min-width: 0; }
  body.row .table-wrapper { border: 0; background: none; }
  body.row .rows-and-columns td { padding: 4px 6px 6px 6px; }
  body.row .rows-and-columns td:before { margin-left: 0; font-weight: 700; color: #475569; }
  form label { width: auto; display: block; }
  form input[type=text], form input[type=search] { width: 100%; box-sizing: border-box; font-size: 1rem; }
  form.sql textarea { width: 100%; }
  .filters input.filter-value { width: 100%; max-width: 14rem; box-sizing: border-box; }
  .search-row label { width: auto; }
}

/* ---- "Useful queries" panel (templates/_sbtools_queries.html) ------------ */
.sbt-sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}
.sbt-q-layout { margin: 1rem 0 0; }
.sbt-q {
  margin: 0 0 1rem;
  background: #fff;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  min-width: 0;
}
h2.sbt-q-heading { margin: 0; padding: 0; font-size: 1rem; border: 0; }
.sbt-q-toggle {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  min-height: 44px;
  padding: 0.5rem 0.75rem;
  font: inherit;
  font-size: 1.05rem;
  font-weight: 700;
  color: #0f172a;
  text-align: left;
  background: #f1f5f9;
  border: 0;
  border-radius: 8px;
  cursor: pointer;
}
.sbt-q.is-open .sbt-q-toggle { border-radius: 8px 8px 0 0; border-bottom: 1px solid #e2e8f0; }
.sbt-q-toggle:focus-visible { outline: 3px solid #93c5fd; outline-offset: 1px; }
.sbt-q-count {
  font-size: 0.8rem;
  font-weight: 700;
  line-height: 1.5;
  padding: 0 0.5rem;
  color: #1e3a8a;
  background: #dbeafe;
  border-radius: 999px;
  white-space: nowrap;
}
.sbt-q-chevron { margin-left: auto; flex: none; transition: transform 0.15s; }
.sbt-q.is-open .sbt-q-chevron { transform: rotate(180deg); }
@media (prefers-reduced-motion: reduce) { .sbt-q-chevron { transition: none; } }

.sbt-q-body { display: none; padding: 0.6rem 0.75rem 0.75rem; }
.sbt-q.is-open .sbt-q-body { display: block; }

.sbt-q-warning {
  margin: 0 0 0.6rem;
  padding: 0.45rem 0.6rem;
  font-size: 0.85rem;
  line-height: 1.4;
  color: #78350f;
  background: #fffbeb;
  border: 1px solid #fcd34d;
  border-radius: 6px;
  overflow-wrap: anywhere;
}
.sbt-q-search { display: flex; align-items: center; gap: 0.5rem; margin: 0 0 0.5rem; }
.sbt-q-search-label { font-weight: 600; font-size: 0.9rem; color: #334155; }
input.sbt-q-filter {
  flex: 1;
  min-width: 0;
  min-height: 40px;
  padding: 0.35rem 0.6rem;
  font: inherit;
  font-size: 1rem;
  border: 1px solid #94a3b8;
  border-radius: 6px;
  background: #fff;
  box-sizing: border-box;
}
input.sbt-q-filter:focus-visible { outline: 3px solid #93c5fd; outline-offset: 1px; border-color: var(--sbt-accent); }

.sbt-q-list {
  max-height: 65vh;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  margin: 0 -0.25rem;
  padding: 0 0.25rem;
}
.sbt-q-group + .sbt-q-group { margin-top: 0.6rem; }
.sbt-q-group[hidden], .sbt-q-item[hidden] { display: none; }
h3.sbt-q-cat {
  margin: 0;
  padding: 0.35rem 0 0.2rem;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: #475569;
  background: #fff;
  position: sticky;
  top: 0;
  z-index: 1;
}
ul.sbt-q-items { list-style: none; margin: 0; padding: 0; }
.sbt-q-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: start;
  gap: 0 0.4rem;
  padding: 0.15rem 0;
  border-top: 1px solid #f1f5f9;
}
.sbt-q-load {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.1rem;
  width: 100%;
  min-height: 44px;
  padding: 0.35rem 0.45rem;
  font: inherit;
  text-align: left;
  color: inherit;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  cursor: pointer;
}
.sbt-q-load:hover { background: #eff6ff; border-color: #bfdbfe; }
.sbt-q-load:focus-visible { outline: 3px solid #93c5fd; outline-offset: 0; }
.sbt-q-item.is-current .sbt-q-load { background: #eff6ff; border-color: #60a5fa; }
.sbt-q-title { font-weight: 700; font-size: 0.95rem; line-height: 1.3; color: #1d4ed8; overflow-wrap: anywhere; }
.sbt-q-desc { font-size: 0.85rem; line-height: 1.35; color: #475569; overflow-wrap: anywhere; }
.sbt-q-pnames { display: flex; flex-wrap: wrap; gap: 0.25rem; margin-top: 0.15rem; }
.sbt-q-pnames code {
  font-size: 0.78rem;
  padding: 0 0.3rem;
  color: #334155;
  background: #f1f5f9;
  border: 1px solid #e2e8f0;
  border-radius: 4px;
}
.sbt-q-copy {
  min-height: 32px;
  min-width: 24px;
  margin-top: 0.35rem;
  padding: 0.25rem 0.55rem;
  font: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  white-space: nowrap;
  color: #1e293b;
  background: #fff;
  border: 1px solid #94a3b8;
  border-radius: 6px;
  cursor: pointer;
}
.sbt-q-copy:hover { background: #f1f5f9; }
.sbt-q-copy:focus-visible { outline: 3px solid #93c5fd; outline-offset: 1px; }
.sbt-q-copy.is-ok { color: #14532d; background: #dcfce7; border-color: #86efac; }
.sbt-q-copy.is-error { color: #7f1d1d; background: #fee2e2; border-color: #fca5a5; }
.sbt-q-empty { margin: 0.5rem 0; font-size: 0.9rem; color: #475569; }

.sbt-q-help { margin: 0.75rem 0 0.25rem; padding-top: 0.4rem; border-top: 1px solid #e2e8f0; font-size: 0.85rem; color: #334155; }
.sbt-q-help summary {
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  font-weight: 600;
  cursor: pointer;
  color: #1d4ed8;
}
.sbt-q-help summary:focus-visible { outline: 3px solid #93c5fd; outline-offset: 1px; }
.sbt-q-help ul { margin: 0.25rem 0 0; padding-left: 1.1rem; list-style: disc; }
.sbt-q-help li { margin: 0 0 0.3rem; line-height: 1.4; }
.sbt-q-source {
  margin: 0.5rem 0 0;
  font-size: 0.8rem;
  line-height: 1.4;
  color: #475569;
  overflow-wrap: anywhere;
}
.sbt-q-problem { color: #92400e; }
section.content .sbt-q-source a { line-height: 1.4; min-height: 24px; color: #1d4ed8; }

/* parameter boxes made by sbtools-queries.js, and the "loaded" note */
.sbt-params[hidden] { display: none; }
.sbt-params h3 { margin-top: 0.25rem; }
.sbt-param label { overflow-wrap: anywhere; }
.sbt-param input::placeholder { color: #64748b; }
.sbt-q-status { margin: 0.25rem 0 0.4rem; font-size: 0.9rem; line-height: 1.4; color: #334155; }
.sbt-q-status:empty { display: none; }

.sbt-q-layout form.sql > h3:first-child { margin-top: 0; }

/* wide screens: the panel is a column left of the SQL form, as tall as the
   form (at least 30rem); its list scrolls inside it */
@media (min-width: 1024px) {
  .sbt-q-layout {
    display: grid;
    grid-template-columns: minmax(17rem, 22rem) minmax(0, 1fr);
    grid-template-rows: minmax(min(36rem, calc(100vh - 6rem)), auto);
    gap: 0 1.5rem;
    align-items: stretch;
  }
  .sbt-q {
    contain: size; /* the form decides the row height, not the list */
    display: flex;
    flex-direction: column;
    margin: 0 0 1rem;
  }
  .sbt-q-toggle { cursor: default; border-radius: 8px 8px 0 0; border-bottom: 1px solid #e2e8f0; }
  .sbt-q-chevron { display: none; }
  .sbt-q-body { display: flex; flex-direction: column; flex: 1; min-height: 0; }
  .sbt-q-list { flex: 1; min-height: 0; max-height: none; }
  .sbt-q-layout form.sql { min-width: 0; }
}
@media (min-width: 1440px) {
  .sbt-q-layout { grid-template-columns: minmax(17rem, 25rem) minmax(0, 1fr); }
}
@media (max-width: 420px) {
  .sbt-q-body { padding: 0.5rem 0.5rem 0.6rem; }
  .sbt-q-search-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0, 0, 0, 0); }
}
