/* Barebones: only the layout the features need. Everything else is browser defaults. */

:root {
  --gap: 4px;
  /* Set by app.js from the visual viewport so the search bar rides above the iOS keyboard */
  --keyboard: 0px;
  --viewport-height: 100dvh;
}

body {
  margin: 0;
  background: #ffffff;
  color: #000000;
  font-family: system-ui, sans-serif;
}

/* ---------- Home ---------- */

.home {
  max-width: 560px;
  padding: calc(8px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom));
}

.home input {
  box-sizing: border-box;
  width: 100%;
  font-size: 16px; /* 16px+ stops iOS zooming in on focus */
}

.errors {
  color: #cc0000;
}

/* ---------- Image feed ---------- */

/* One image per row, scrolled vertically; the bottom padding clears the search bar */
.feed {
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  margin: 0;
  padding: env(safe-area-inset-top) 0 calc(4rem + env(safe-area-inset-bottom));
  list-style: none;
}

.feed img {
  display: block;
  width: 100%;
  height: auto;
}

/* ---------- Search ---------- */

/* Results and search bar stack at the bottom, above the keyboard when it's open */
.dock {
  position: fixed;
  right: 0;
  bottom: var(--keyboard);
  left: 0;
  background: #ffffff;
}

.results {
  display: flex;
  flex-direction: column;
  max-height: calc(var(--viewport-height) - 5rem - env(safe-area-inset-top));
  border-top: 1px solid #000000;
}

.results[hidden] {
  display: none;
}

.results-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 8px;
}

.results-scroll {
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain; /* reaching the top or bottom doesn't scroll the images */
}

/* Freeze the images while the sheet is open, so drags anywhere on the panel stay put */
html:has(.results:not([hidden])) {
  overflow: hidden;
}

.results.is-empty .results-scroll {
  display: none;
}

.searchbar {
  padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
  border-top: 1px solid #000000;
}

.searchbar input {
  box-sizing: border-box;
  width: 100%;
  font-size: 16px;
}

/* ---------- Sheet table ---------- */

/* The sheet's own formatting comes from per-cell classes; these are just the grid */
.sheet {
  border-collapse: separate; /* keeps borders on sticky cells */
  border-spacing: 0;
  table-layout: fixed;
  border-top: 1px solid #e2e2e2;
  border-left: 1px solid #e2e2e2;
  font: 10pt Arial, sans-serif;
}

/* :where() keeps these at zero specificity so per-cell format classes always win */
:where(.sheet td) {
  padding: 2px 4px;
  overflow: hidden;
  border-right: 1px solid #e2e2e2;
  border-bottom: 1px solid #e2e2e2;
  background: #ffffff;
  vertical-align: bottom;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* Header rows and frozen columns stay pinned while scrolling the results */
.sheet td.fr,
.sheet td.fc,
.sheet tr[data-header] td {
  position: sticky;
  z-index: 1;
}

/* A header row that isn't frozen in the sheet (just the first row) pins to the top */
.sheet tr[data-header] td:not(.fr) {
  top: 0;
}

.sheet td.fr.fc {
  z-index: 2;
}

/* ---------- Wider screens ---------- */

@media (min-width: 640px) {
  .feed {
    display: block;
    columns: 3 260px;
    column-gap: var(--gap);
  }

  .feed li {
    margin-bottom: var(--gap);
    break-inside: avoid;
  }
}
