/* Admin shell + admin pages — login, survey list, qualifiers, report.
   Loaded after /styles.css by src/views/layout.tsx (and login.tsx), so the
   design tokens (:root in styles.css) and the shared .btn/.input rules are
   already in scope; this file only adds the shell, the toolbar, the admin
   table, status badges and the page-specific blocks. No build step. */

/* App shell ---------------------------------------------------------------- */

.topnav {
  position: sticky;
  top: 0;
  z-index: 10;
  min-height: 56px; /* matches .builder's `100vh - 56px` offset in builder.css */
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 0 20px;
  background: rgba(255, 255, 255, 0.88);
  backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.8);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  font-weight: 700;
  font-size: 1.05rem;
  letter-spacing: -0.015em;
  color: var(--ink);
  text-decoration: none;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 27px;
  height: 27px;
  border-radius: 8px;
  color: #fff;
  background: linear-gradient(140deg, #3c68f5 0%, #1c3fb8 100%);
  box-shadow: 0 2px 6px rgba(31, 73, 207, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.25);
}

.brand-mark .icon {
  width: 16px;
  height: 16px;
}

.brand:hover .brand-mark {
  box-shadow: 0 4px 10px rgba(31, 73, 207, 0.4), inset 0 1px 0 rgba(255, 255, 255, 0.25);
}

.brand:hover {
  color: var(--accent-strong);
}

.topnav nav {
  display: flex;
  gap: 16px;
}

.topnav nav a {
  position: relative;
  padding: 5px 0;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.95rem;
  transition: color 0.16s ease;
}

.topnav nav a::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 2px;
  border-radius: 2px;
  background: var(--accent);
  transform: scaleX(0);
  transform-origin: left;
  transition: transform 0.16s ease;
}

.topnav nav a:hover {
  color: var(--ink);
}

.topnav nav a:hover::after {
  transform: scaleX(1);
}

/* The logout form is a flex item in the 56px bar; keep it pinned right and
   vertically centred by the header's align-items. */
.topnav form {
  display: flex;
  margin-left: auto;
}

.topnav .btn {
  padding: 6px 13px;
  min-height: 0;
  font-size: 0.88rem;
}

.topnav .btn .icon {
  width: 15px;
  height: 15px;
}

.admin-main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 28px 20px 72px;
}

/* Link colour — `:not(.btn)` so action links keep the button's own ink. */
.admin-main a:not(.btn) {
  color: var(--accent);
}

.admin-main h1 {
  margin: 0;
  font-size: 1.6rem;
  line-height: 1.25;
  letter-spacing: -0.01em;
}

.admin-main h2 {
  margin: 0;
  font-size: 1.05rem;
}

/* Links rendered as buttons (all the toolbar actions are <a class="btn">). */
a.btn {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
}

/* Page + toolbar ----------------------------------------------------------- */

.survey-list,
.qualifiers,
.report {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.list-head {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 14px;
}

/* Last child is the action (the "+ New survey" form / "Download CSV" link). */
.list-head > :last-child {
  margin-left: auto;
}

/* Page subtitle: the survey title on the qualifiers and report pages.
   (The builder header has its own .builder-counts rule in builder.css.) */
.list-head .builder-counts {
  color: var(--muted);
  font-size: 0.95rem;
}

.new-survey {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

/* Admin table -------------------------------------------------------------- */

.admin-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  font-size: 0.95rem;
  font-variant-numeric: tabular-nums;
  box-shadow: var(--shadow-sm, 0 1px 3px rgba(20, 29, 57, 0.08));
}

.admin-table th,
.admin-table td {
  padding: 12px 14px;
  text-align: left;
  vertical-align: middle;
  border-bottom: 1px solid var(--line);
}

.admin-table th {
  background: #f8fafc;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.admin-table tbody tr:last-child td {
  border-bottom: 0;
}

.admin-table tbody tr:hover {
  background: #f8faff;
}

.admin-table a:not(.btn) {
  color: var(--ink);
  font-weight: 600;
  text-decoration: none;
}

.admin-table a:not(.btn):hover {
  color: var(--accent);
  text-decoration: underline;
}

.cell-title {
  font-weight: 600;
  max-width: 420px; /* long survey names wrap instead of stretching the row */
  overflow-wrap: anywhere;
}

.cell-actions {
  white-space: nowrap;
}

.cell-actions .btn {
  padding: 5px 10px;
  min-height: 0;
  font-size: 0.8rem;
  gap: 5px;
}

.cell-actions .btn .icon {
  width: 14px;
  height: 14px;
}

/* Delete is a POSTed form, but must sit on the same line as the other actions. */
.cell-actions form {
  display: inline;
}

/* Qualifiers · Responses are counts → right-align them on the survey list. */
.survey-list .admin-table th:nth-child(3),
.survey-list .admin-table td:nth-child(3),
.survey-list .admin-table th:nth-child(4),
.survey-list .admin-table td:nth-child(4) {
  text-align: right;
}

/* Keep the updated timestamp on one line; the table scrolls on narrow screens. */
.survey-list .admin-table th:nth-child(5),
.survey-list .admin-table td:nth-child(5) {
  white-space: nowrap;
}

/* Search icon and clear x sit INSIDE the field: the form is the positioning
   context, the icon is pinned to the left edge and the x to the right one,
   and the input gets asymmetric padding so text never runs under them.
   Typing filters live via public/survey-search.js. */
.survey-list .list-head .new-survey {
  margin-left: 0;
}

.survey-search {
  position: relative;
  margin-left: auto;
  display: flex;
  align-items: center;
  color: var(--muted);
}

/* The magnifier: a direct child of the form (the x button's inner icon also
   has .icon, so this must not match descendants). */
.survey-search > .icon {
  position: absolute;
  left: 11px;
  top: 50%;
  transform: translateY(-50%);
  width: 15px;
  height: 15px;
  flex: none;
  pointer-events: none; /* clicks land in the input, not on the overlay */
}

.survey-search .input {
  width: 220px;
  padding: 7px 30px; /* clears the icon on the left and the x on the right */
}

/* type="search" gives WebKit a built-in clear x inside the field; ours below
   replaces it, so hide the native one. */
.survey-search .input::-webkit-search-cancel-button {
  -webkit-appearance: none;
  appearance: none;
  display: none;
}

/* The "clear search" x: bare ghost icon pinned inside the field's right edge.
   Always in the DOM so live search can reveal it; [hidden] needs re-asserting
   because .btn's display beats the UA rule. */
.survey-search .btn[data-clear-search] {
  position: absolute;
  right: 7px;
  top: 50%;
  transform: translateY(-50%);
  padding: 3px;
  min-height: 0;
  gap: 0;
  background: none;
  border: none;
}

.survey-search .btn[data-clear-search]:hover {
  color: var(--ink);
}

.survey-search .btn[hidden] {
  display: none;
}

.survey-search .btn .icon {
  width: 14px;
  height: 14px;
  display: block;
}

/* While a live-search fetch is in flight the results dim and ignore clicks. */
.survey-results.is-loading {
  opacity: 0.55;
  pointer-events: none;
  transition: opacity 120ms ease;
}

/* Pager under the survey table (only rendered with more than one page).
   Needs its own top margin: it lives inside #survey-results, so the page
   container's 20px gap doesn't reach it. */
.pager {
  margin-top: 16px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 12px;
}

.pager-info {
  color: var(--muted);
  font-size: 0.9rem;
  font-variant-numeric: tabular-nums;
}

.pager .btn {
  padding: 6px 10px;
  min-height: 0;
  font-size: 0.85rem;
  gap: 5px;
}

.pager .btn .icon {
  width: 14px;
  height: 14px;
}

/* Prev/Next on the first/last page render as inert labels instead of links. */
.pager .btn.pager-off {
  opacity: 0.45;
}

/* The qualifiers table squeezes Name against a long personal link: keep names
   on one line and let the URL break instead of blowing the column out. */
.qualifiers .admin-table .cell-title {
  white-space: nowrap;
}

.qualifiers .admin-table td:last-child {
  min-width: 300px; /* long personal links get room instead of crushing Name */
}

.qualifiers .admin-table td:last-child a:not(.btn) {
  word-break: break-all;
  font-size: 0.85rem;
  font-weight: 500;
}

.empty {
  margin: 0;
  padding: 24px;
  text-align: center;
  color: var(--muted);
  background: var(--card);
  border: 1px dashed var(--line);
  border-radius: var(--radius);
}

/* Status badges (shared by the builder header, the survey list and the
   qualifier rows — moved here from builder.css when the admin shell got a
   stylesheet). Class names can't contain spaces: `not sent` → badge-not-sent. */

.badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  background: var(--line, #e4e8f2);
  color: var(--ink, #161e30);
}

.badge-draft {
  background: #eef2f8;
  color: #4b5565;
}

.badge-published {
  background: #e3f5ec;
  color: #178a52;
}

.badge-closed {
  background: #fdeaea;
  color: #c62828;
}

.badge-invited {
  background: #e8f0fe;
  color: #1d6fe8;
}

.badge-opened {
  background: #fff4e5;
  color: #b26a00;
}

.badge-submitted {
  background: #e3f5ec;
  color: #178a52;
}

.badge-not-sent {
  background: #eef2f8;
  color: #4b5565;
}

.copy {
  padding: 5px 10px;
  min-height: 0;
  font-size: 0.8rem;
}

/* Command output (invite / remind / CSV-import results) --------------------- */

.admin-main pre {
  margin: 0;
  padding: 10px 12px;
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--muted);
  font-size: 0.85rem;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-x: auto;
}

.admin-main pre:empty {
  display: none; /* no empty boxes before a run */
}

.admin-main .error {
  margin: 0;
}

.admin-main .error:empty {
  display: none;
}

/* Qualifiers page ----------------------------------------------------------- */

.qualifier-invite {
  display: flex;
  flex-direction: row;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  padding: 16px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.qualifier-invite pre,
.qualifier-invite .error,
.qualifier-hint {
  flex-basis: 100%;
}

.qualifier-hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.5;
}

.qualifier-hint strong {
  color: var(--ink);
}

.qualifier-forms {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 20px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.qualifier-forms h2 {
  margin-top: 14px;
}

.qualifier-forms h2:first-child {
  margin-top: 0;
}

.qualifier-forms form {
  display: grid;
  gap: 10px;
}

.qualifier-forms form .btn {
  justify-self: start; /* the Import CSV button shouldn't stretch full width */
}

/* The 4-column "Add qualifier" form + its submit button. */
.qualifier-forms .new-survey {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
}

.qualifier-forms .new-survey .btn {
  grid-column: 1 / -1;
  justify-self: start;
}

.qualifier-forms .textarea {
  width: 100%;
}

/* Report page (T29) --------------------------------------------------------- */

.report {
  gap: 24px;
}

.report-questions,
.report-respondents {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
}

.kpi {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 14px 16px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.kpi-label {
  color: var(--muted);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.kpi-value {
  font-size: 1.6rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}

.opt-list {
  list-style: none;
  margin: 12px 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.opt {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 12px;
  align-items: center;
}

.opt-label {
  font-size: 0.95rem;
}

.opt-count {
  color: var(--muted);
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
}

.opt .bar-track {
  grid-column: 1 / -1;
}

.summary-line {
  margin: 12px 0 0;
  color: var(--muted);
  font-size: 0.92rem;
}

.value-list {
  margin: 12px 0 0;
  padding-left: 18px;
  font-size: 0.92rem;
  columns: 2;
  column-gap: 28px;
}

.value-list li {
  break-inside: avoid;
  margin-bottom: 4px;
}

.report-respondents details summary {
  cursor: pointer;
  color: var(--accent);
  font-size: 0.9rem;
}

/* `.report-question` needs no extra rule — it rides .q-card + the flex gap. */
.report-matrix {
  margin-top: 12px;
}

.raw-answers {
  max-width: 100%;
  font-size: 0.78rem;
}

/* Login page ---------------------------------------------------------------- */

.login-page {
  min-height: 100vh;
  display: grid;
  place-content: center;
  gap: 16px;
  padding: 24px;
}

.login-page h1 {
  margin: 0;
  font-size: 1.4rem;
  text-align: center;
}

.login-page form {
  width: min(360px, 92vw);
  display: grid;
  gap: 14px;
  padding: 24px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(16, 24, 40, 0.08);
}

.login-page label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 0.9rem;
}

.login-page input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font: inherit;
  color: var(--ink);
  background: #fff;
}

.login-page input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.login-page .btn {
  width: 100%;
  padding: 10px 20px;
}

/* Below ~960px the columns get too tight (the title column collapses to one
   word per line), so let the table scroll horizontally instead. */
@media (max-width: 960px) {
  .admin-table {
    display: block;
    overflow-x: auto;
    white-space: nowrap;
  }

  .admin-table .cell-title {
    max-width: none;
  }
}

/* Narrow screens ------------------------------------------------------------- */

@media (max-width: 720px) {
  .topnav {
    gap: 12px;
    padding: 0 14px;
    flex-wrap: wrap;
    min-height: 0;
    padding-top: 10px;
    padding-bottom: 10px;
  }

  .admin-main {
    padding: 20px 14px 56px;
  }

  .list-head {
    gap: 10px;
  }

  .list-head > :last-child {
    margin-left: 0;
  }

  .new-survey {
    width: 100%;
  }

  .value-list {
    columns: 1;
  }
}

/* In-app confirmation dialog (survey delete) — created by admin-actions.js.
   Backdrop click / Escape cancel; the card mirrors the builder modals. */
.confirm-backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(28, 35, 51, 0.45);
}

.confirm-backdrop[hidden] {
  display: none;
}

.confirm-card {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: min(440px, 100%);
  padding: 18px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
}

.confirm-title {
  margin: 0;
  font-size: 1.05rem;
  line-height: 1.3;
}

.confirm-message {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.confirm-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 4px;
}

/* Keep the page behind the dialog from scrolling while it is open. */
body.confirm-open {
  overflow: hidden;
}
