/* ===========================================================================
   MassEnergize Data Search
   Plain CSS, no framework, no build step.

   Structure:
     1. Tokens (light + dark)
     2. Reset & base
     3. Primitives  (buttons, inputs, alerts, spinner)
     4. Layout      (topbar, main)
     5. Sign-in
     6. Search
     7. Results table
     8. SQL disclosure
     9. Motion & responsive

   To rebrand, change --accent-* in the token block. Nothing else hardcodes
   a brand colour.
   =========================================================================== */

/* ---------- 1. Tokens ---------------------------------------------------- */

:root {
  /* Brand — a deep, calm green. Contrast-checked against --surface for AA. */
  --accent: #0f7048;
  --accent-hover: #0c5c3b;
  --accent-soft: #e7f4ee;
  --accent-ring: rgba(15, 112, 72, 0.32);

  /* Neutrals */
  --bg: #f6f7f6;
  --surface: #ffffff;
  --surface-sunken: #f2f4f3;
  --surface-hover: #f7f9f8;
  --border: #e2e6e4;
  --border-strong: #cdd4d1;

  /* Text — all AA or better on --surface */
  --text: #121a17;
  --text-muted: #5c6b65;
  --text-faint: #64706b;

  /* Status */
  --danger: #a52220;
  --danger-bg: #fdf0ef;
  --danger-border: #f2cbc8;
  --info: #10527f;
  --info-bg: #eef5fb;
  --info-border: #cbdff0;

  /* Shape & depth */
  --r-sm: 7px;
  --r-md: 10px;
  --r-lg: 14px;
  --shadow-sm: 0 1px 2px rgba(16, 26, 22, 0.05);
  --shadow-md: 0 2px 4px rgba(16, 26, 22, 0.05), 0 6px 16px rgba(16, 26, 22, 0.06);
  --shadow-lg: 0 8px 28px rgba(16, 26, 22, 0.1);

  /* Type */
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --accent: #3fbd86;
    --accent-hover: #55cd97;
    --accent-soft: #12241c;
    --accent-ring: rgba(63, 189, 134, 0.36);

    --bg: #0e1211;
    --surface: #161b19;
    --surface-sunken: #1b2220;
    --surface-hover: #1e2523;
    --border: #262e2b;
    --border-strong: #35403c;

    --text: #e8edeb;
    --text-muted: #9aa8a2;
    --text-faint: #7d8b85;

    --danger: #ff8a84;
    --danger-bg: #2a1615;
    --danger-border: #4a2422;
    --info: #86bff0;
    --info-bg: #111d28;
    --info-border: #22374a;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-md: 0 2px 4px rgba(0, 0, 0, 0.3), 0 6px 16px rgba(0, 0, 0, 0.35);
    --shadow-lg: 0 8px 28px rgba(0, 0, 0, 0.5);
  }
}

/* ---------- 2. Reset & base --------------------------------------------- */

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

/*
 * The `hidden` attribute is how app.js switches views, but ANY `display` rule
 * (.signin uses grid, .boot uses flex) silently overrides it. Without this the
 * sign-in card stays on screen behind the search UI. Do not remove.
 */
[hidden] {
  display: none !important;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.55;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

h1,
h2,
p {
  margin: 0;
}

.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;
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: 100;
  padding: 0.6rem 1rem;
  background: var(--accent);
  color: #fff;
  border-radius: 0 0 var(--r-sm) 0;
  text-decoration: none;
}
.skip-link:focus {
  left: 0;
}

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

:where(a) {
  color: var(--accent);
}

/* One consistent focus ring everywhere. */
:where(button, input, summary, a, [tabindex]):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--r-sm);
}

/* ---------- 3. Primitives ------------------------------------------------ */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  padding: 0.58rem 0.95rem;
  font: inherit;
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-sm);
  box-shadow: var(--shadow-sm);
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.14s ease, border-color 0.14s ease, transform 0.06s ease;
}
.btn:hover:not(:disabled) {
  background: var(--surface-hover);
  border-color: var(--text-faint);
}
.btn:active:not(:disabled) {
  transform: translateY(1px);
}
.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  box-shadow: none;
}

.btn--primary {
  color: #fff;
  background: var(--accent);
  border-color: transparent;
  box-shadow: 0 1px 2px var(--accent-ring);
}
.btn--primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: transparent;
}
@media (prefers-color-scheme: dark) {
  .btn--primary {
    color: #06120d;
  }
}

.btn--ghost {
  background: transparent;
  border-color: transparent;
  box-shadow: none;
  color: var(--text-muted);
}
.btn--ghost:hover:not(:disabled) {
  background: var(--surface-sunken);
  border-color: transparent;
  color: var(--text);
}

.btn--sm {
  padding: 0.4rem 0.7rem;
  font-size: 0.82rem;
}

.btn--block {
  width: 100%;
}

.btn__icon {
  width: 15px;
  height: 15px;
  flex: none;
}

.input {
  width: 100%;
  padding: 0.65rem 0.8rem;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-sm);
  transition: border-color 0.14s ease, box-shadow 0.14s ease;
}
.input::placeholder {
  color: var(--text-faint);
}
.input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-ring);
}

.field {
  margin-bottom: 1rem;
}
.field__label {
  display: block;
  margin-bottom: 0.35rem;
  font-size: 0.85rem;
  font-weight: 600;
}
.field__hint {
  margin-top: 0.4rem;
  font-size: 0.8rem;
  color: var(--text-muted);
}

/* Alerts double as the live region for progress, empty states and errors. */
.alert {
  padding: 0.7rem 0.95rem;
  margin: 0 0 1rem;
  font-size: 0.9rem;
  background: var(--info-bg);
  border: 1px solid var(--info-border);
  border-left: 3px solid var(--info);
  border-radius: var(--r-sm);
  color: var(--info);
}
.alert--error {
  background: var(--danger-bg);
  border-color: var(--danger-border);
  border-left-color: var(--danger);
  color: var(--danger);
}
.alert--busy {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.spinner {
  width: 15px;
  height: 15px;
  flex: none;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  opacity: 0.8;
  animation: spin 0.7s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-md);
}

/* ---------- 4. Layout ---------------------------------------------------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(1.6) blur(10px);
  border-bottom: 1px solid var(--border);
}

.topbar__inner {
  max-width: 1180px;
  margin: 0 auto;
  padding: 0.75rem 1.25rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  min-width: 0;
}
.brand__mark {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  flex: none;
  color: #fff;
  background: var(--accent);
  border-radius: 8px;
}
@media (prefers-color-scheme: dark) {
  .brand__mark {
    color: #06120d;
  }
}
.brand__mark svg {
  width: 17px;
  height: 17px;
}
.brand__text {
  display: flex;
  align-items: baseline;
  gap: 0.4rem;
  min-width: 0;
}
.brand__name {
  font-weight: 650;
  letter-spacing: -0.01em;
}
.brand__sub {
  font-size: 0.9rem;
  color: var(--text-muted);
}

.userbar {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  min-width: 0;
}
.userbar__email {
  font-size: 0.85rem;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 40vw;
}

main {
  max-width: 1180px;
  margin: 0 auto;
  padding: 1.75rem 1.25rem 5rem;
}

.boot {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
  padding: 5rem 0;
  color: var(--text-muted);
}

/* ---------- 5. Sign-in --------------------------------------------------- */

.signin {
  display: grid;
  place-items: center;
  min-height: 64vh;
}
.card--signin {
  width: 100%;
  max-width: 25rem;
  padding: 1.85rem;
}
.signin__title {
  font-size: 1.3rem;
  font-weight: 650;
  letter-spacing: -0.015em;
}
.signin__lede {
  margin: 0.35rem 0 1.4rem;
  color: var(--text-muted);
  font-size: 0.92rem;
}

/* ---------- 6. Search ---------------------------------------------------- */

.search {
  margin-bottom: 1.5rem;
}

.search__form {
  display: flex;
  gap: 0.6rem;
  align-items: stretch;
}

.search__control {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
}
.search__icon {
  position: absolute;
  left: 0.95rem;
  /* Aligned to the first line, not the middle — the box is multi-line now. */
  top: 0.95rem;
  display: grid;
  place-items: center;
  color: var(--text-faint);
  pointer-events: none;
}
.search__icon svg {
  width: 17px;
  height: 17px;
}
.search__input {
  display: block;
  width: 100%;
  padding: 0.85rem 1rem 0.85rem 2.8rem;
  font: inherit;
  font-size: 1rem;
  line-height: 1.55;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-sm);
  resize: vertical;
  min-height: 5.5rem;
  transition: border-color 0.14s ease, box-shadow 0.14s ease;
}
.search__input::placeholder {
  color: var(--text-faint);
}
.search__input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-ring);
}
.btn--search {
  padding-inline: 1.6rem;
  font-size: 0.95rem;
  border-radius: var(--r-md);
}

/* Hint + button row under the question box. */
.search__foot {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-top: 0.6rem;
}
.search__hint {
  flex: 1 1 18rem;
  font-size: 0.8rem;
  color: var(--text-muted);
}
.search__hint kbd {
  display: inline-block;
  padding: 0.05rem 0.3rem;
  font-family: var(--mono);
  font-size: 0.75rem;
  color: var(--text);
  background: var(--surface-sunken);
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
}

/* Mode switch: plain English vs raw SQL */
.modeswitch {
  display: inline-flex;
  gap: 0.15rem;
  padding: 0.2rem;
  margin-bottom: 0.75rem;
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: 999px;
}
.modeswitch__btn {
  padding: 0.35rem 0.85rem;
  font: inherit;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-muted);
  background: transparent;
  border: none;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.14s ease, color 0.14s ease;
}
.modeswitch__btn:hover {
  color: var(--text);
}
.modeswitch__btn.is-active {
  color: var(--text);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

/* The ask panel stacks: textarea, then a hint + button row — same shape as the
   SQL panel, so switching modes doesn't rearrange the page under you. */
.search__panel {
  display: flex;
  flex-direction: column;
  width: 100%;
}

/* SQL editor */
.sqlpanel {
  width: 100%;
}
.sqlpanel__editor {
  width: 100%;
  padding: 0.85rem 1rem;
  font-family: var(--mono);
  font-size: 0.85rem;
  line-height: 1.6;
  tab-size: 2;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-sm);
  resize: vertical;
  min-height: 8rem;
  transition: border-color 0.14s ease, box-shadow 0.14s ease;
}
.sqlpanel__editor::placeholder {
  color: var(--text-faint);
}
.sqlpanel__editor:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-ring);
}
.sqlpanel__foot {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-top: 0.6rem;
}
.sqlpanel__hint {
  font-size: 0.8rem;
  color: var(--text-muted);
}
.sqlpanel__hint code {
  font-family: var(--mono);
  font-size: 0.95em;
}
.sqlpanel__hint kbd {
  display: inline-block;
  padding: 0.05rem 0.3rem;
  font-family: var(--mono);
  font-size: 0.75rem;
  color: var(--text);
  background: var(--surface-sunken);
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
}

.sqlbox__tools {
  position: absolute;
  top: 0.55rem;
  right: 0.55rem;
  display: flex;
  gap: 0.35rem;
}

/* Example questions — a blank input is intimidating for non-technical users. */
.examples {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.45rem;
  margin-top: 0.85rem;
}
.examples__label {
  font-size: 0.82rem;
  color: var(--text-faint);
}
.chip {
  padding: 0.32rem 0.7rem;
  font: inherit;
  font-size: 0.82rem;
  color: var(--text-muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  cursor: pointer;
  transition: color 0.14s ease, border-color 0.14s ease, background 0.14s ease;
}
.chip:hover {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--accent-soft);
}

/* ---------- Skeleton ----------------------------------------------------- */

.skeleton {
  padding: 1rem 1.1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-sm);
}
.skeleton__bar {
  height: 11px;
  margin: 0.7rem 0;
  border-radius: 5px;
  background: linear-gradient(
    90deg,
    var(--surface-sunken) 25%,
    var(--surface-hover) 37%,
    var(--surface-sunken) 63%
  );
  background-size: 400% 100%;
  animation: shimmer 1.3s ease-in-out infinite;
}
.skeleton__bar--head {
  height: 13px;
  width: 34%;
  background: var(--surface-sunken);
  animation: none;
}
.skeleton__bar:nth-child(3) {
  width: 92%;
}
.skeleton__bar:nth-child(4) {
  width: 78%;
}
.skeleton__bar:nth-child(5) {
  width: 86%;
}
@keyframes shimmer {
  0% {
    background-position: 100% 0;
  }
  100% {
    background-position: 0 0;
  }
}

/* ---------- 7. Results --------------------------------------------------- */

.results__bar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.7rem;
}
.results__count {
  font-size: 1rem;
  font-weight: 650;
  letter-spacing: -0.01em;
}
.results__detail {
  margin-top: 0.1rem;
  font-size: 0.82rem;
  color: var(--text-faint);
}
.results__actions {
  display: flex;
  gap: 0.45rem;
}

.tablewrap {
  overflow: auto;
  max-height: 66vh;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-sm);

  /*
   * Scroll shadows. Wide results are common (a join easily exceeds the
   * viewport), and a hard clip at the edge reads as "that's all there is".
   * The two `local` layers mask the shadow when you're already at that end,
   * so the cue only appears when there's genuinely more to see.
   */
  background:
    linear-gradient(to right, var(--surface) 30%, transparent) left center / 3rem 100% no-repeat local,
    linear-gradient(to left, var(--surface) 30%, transparent) right center / 3rem 100% no-repeat local,
    radial-gradient(farthest-side at 0 50%, rgba(0, 0, 0, 0.14), transparent) left center / 1rem 100% no-repeat scroll,
    radial-gradient(farthest-side at 100% 50%, rgba(0, 0, 0, 0.14), transparent) right center / 1rem 100% no-repeat scroll;
  background-color: var(--surface);
}

.table {
  border-collapse: separate;
  border-spacing: 0;
  width: 100%;
  font-size: 0.875rem;
  font-variant-numeric: tabular-nums;
}

.table th,
.table td {
  padding: 0.55rem 0.85rem;
  text-align: left;
  vertical-align: top;
  border-bottom: 1px solid var(--border);
  max-width: 30rem;
}

.table thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  font-size: 0.74rem;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  /* Opaque, so the wrapper's scroll-shadow gradients don't show through. */
  background-color: var(--surface-sunken);
  border-bottom: 1px solid var(--border-strong);
  white-space: nowrap;
}

.table tbody tr:last-child td {
  border-bottom: none;
}
.table tbody tr:hover td {
  background: var(--surface-hover);
}

/* Cell variants, chosen from the value's shape — see app.js */
.cell-num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.cell-null {
  color: var(--text-faint);
}
.cell-mono {
  font-family: var(--mono);
  font-size: 0.8rem;
  color: var(--text-muted);
}
.cell-id {
  font-family: var(--mono);
  font-size: 0.78rem;
  color: var(--text-faint);
  white-space: nowrap;
  max-width: 9rem;
  overflow: hidden;
  text-overflow: ellipsis;
}
/*
 * Long free text is clamped on an inner <span>, never on the <td> itself:
 * `display: -webkit-box` on a table cell drops it out of the table layout and
 * neighbouring columns overlap.
 */
.clamp {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.cell-date {
  white-space: nowrap;
  color: var(--text-muted);
}

.badge {
  display: inline-block;
  padding: 0.1rem 0.45rem;
  font-size: 0.75rem;
  font-weight: 600;
  border-radius: 999px;
  border: 1px solid transparent;
}
.badge--yes {
  color: var(--accent);
  background: var(--accent-soft);
  border-color: color-mix(in srgb, var(--accent) 28%, transparent);
}
.badge--no {
  color: var(--text-muted);
  background: var(--surface-sunken);
  border-color: var(--border-strong);
}

/* ---------- 8. SQL disclosure -------------------------------------------- */

.sqlbox {
  margin-top: 0.9rem;
}
.sqlbox__summary {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.3rem 0.1rem;
  font-size: 0.83rem;
  color: var(--text-muted);
  cursor: pointer;
  list-style: none;
  user-select: none;
}
.sqlbox__summary::-webkit-details-marker {
  display: none;
}
.sqlbox__summary:hover {
  color: var(--text);
}
.sqlbox__chevron {
  width: 14px;
  height: 14px;
  transition: transform 0.16s ease;
}
.sqlbox[open] .sqlbox__chevron {
  transform: rotate(90deg);
}

.sqlbox__body {
  position: relative;
  margin-top: 0.4rem;
}
.sqlbox__code {
  margin: 0;
  padding: 0.9rem 1rem;
  padding-right: 5rem;
  font-family: var(--mono);
  font-size: 0.8rem;
  line-height: 1.65;
  color: var(--text-muted);
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  white-space: pre-wrap;
  word-break: break-word;
  overflow-x: auto;
}

/* ---------- 9. Motion & responsive --------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  .skeleton__bar {
    background: var(--surface-sunken);
  }
}

@media (max-width: 640px) {
  main {
    padding: 1.25rem 0.9rem 4rem;
  }
  .topbar__inner {
    padding: 0.7rem 0.9rem;
  }
  .brand__sub {
    display: none;
  }
  .search__form,
  .search__panel {
    flex-direction: column;
  }
  .sqlpanel__foot,
  .search__foot {
    flex-direction: column;
    align-items: stretch;
  }
  .sqlpanel__foot .btn,
  .search__foot .btn {
    width: 100%;
  }
  .btn--search {
    width: 100%;
    padding-block: 0.75rem;
  }
  .results__bar {
    align-items: stretch;
  }
  .results__actions {
    justify-content: flex-start;
  }
  .tablewrap {
    max-height: none;
  }
  .userbar__email {
    display: none;
  }
}
