@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";

/* The admin's design system. docs/admin/DESIGN.md says when to use what; app/helpers/admin_ui_helper.rb builds the
   markup for the pieces that take more than one class. Fonts are the self-hosted copies the public site already
   serves from /fonts. */

@font-face {
  font-family: "Figtree";
  font-style: normal;
  font-weight: 300 900;
  font-display: swap;
  src: url("/fonts/figtree-latin.woff2") format("woff2");
}

@font-face {
  font-family: "IBM Plex Mono";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("/fonts/ibm-plex-mono-400-latin.woff2") format("woff2");
}

@font-face {
  font-family: "IBM Plex Mono";
  font-style: normal;
  font-weight: 500;
  font-display: swap;
  src: url("/fonts/ibm-plex-mono-500-latin.woff2") format("woff2");
}

@layer base {
  html {
    @apply bg-canvas text-ink-900 antialiased;
    font-feature-settings: "cv11", "ss01";
  }

  ::selection {
    @apply bg-lime-300 text-ink-950;
  }

  :focus-visible {
    @apply outline-none ring-2 ring-olive-600 ring-offset-2 ring-offset-canvas;
  }

  /* Numbers line up in every table without each cell having to ask. */
  table {
    font-variant-numeric: tabular-nums;
  }

  [type="text"], [type="email"], [type="password"], [type="number"], [type="search"], [type="url"], [type="date"],
  [type="datetime-local"], [type="tel"], textarea, select {
    @apply rounded-md border-ink-300 text-sm shadow-xs focus:border-olive-600 focus:ring-olive-600;
  }

  [type="checkbox"], [type="radio"] {
    @apply border-ink-300 text-olive-700 focus:ring-olive-600;
  }

  @media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
      transition-duration: 0.01ms !important;
      animation-duration: 0.01ms !important;
    }
  }
}

@layer components {
  /* Page header: title, one line of context, actions on the right. */
  .page-header {
    @apply flex flex-col gap-3 pb-5 pt-6 sm:flex-row sm:items-end sm:justify-between lg:pt-8;
  }
  .page-title {
    @apply text-xl font-bold tracking-tight text-ink-900;
  }
  .page-subtitle {
    @apply mt-1 max-w-3xl text-sm text-ink-600;
  }
  .page-actions {
    @apply flex flex-wrap items-center gap-2;
  }
  .section-title {
    @apply text-base font-semibold text-ink-900;
  }

  /* Buttons. One primary per screen; secondary for everything else; danger only where data goes away. */
  .btn {
    @apply inline-flex cursor-pointer items-center justify-center gap-1.5 whitespace-nowrap rounded-md px-3 py-2 text-sm font-semibold leading-5 transition-colors duration-150 disabled:cursor-not-allowed disabled:opacity-50;
  }
  .btn-primary {
    @apply btn bg-lime-400 text-ink-950 shadow-xs ring-1 ring-inset ring-lime-500/60 hover:bg-lime-300 active:bg-lime-500;
  }
  .btn-secondary {
    @apply btn bg-white text-ink-900 shadow-xs ring-1 ring-inset ring-ink-300 hover:bg-ink-50 active:bg-ink-100;
  }
  .btn-danger {
    @apply btn bg-white text-red-700 shadow-xs ring-1 ring-inset ring-red-300 hover:bg-red-50 active:bg-red-100;
  }
  .btn-danger-solid {
    @apply btn bg-red-700 text-white shadow-xs hover:bg-red-800;
  }
  .btn-ghost {
    @apply btn text-ink-700 hover:bg-ink-100 hover:text-ink-900;
  }
  /* After the variants, which @apply .btn's padding, so the size wins. */
  .btn-sm {
    @apply px-2.5 py-1.5 text-xs;
  }
  /* button_to wraps its button in a form; keep it inline in a row of actions. */
  form.button_to {
    @apply inline-flex;
  }

  .link {
    @apply font-medium text-olive-700 underline decoration-olive-300 underline-offset-2 hover:text-olive-900 hover:decoration-olive-600;
  }
  .link-quiet {
    @apply font-medium text-ink-900 hover:text-olive-700 hover:underline hover:underline-offset-2;
  }

  /* Surfaces */
  .card {
    @apply rounded-lg bg-white ring-1 ring-ink-200;
  }
  .card-header {
    @apply flex items-center justify-between gap-3 border-b border-ink-200 px-4 py-3 sm:px-5;
  }
  .card-body {
    @apply px-4 py-4 sm:px-5;
  }

  /* Tables: wrap in .table-wrap so wide ones scroll inside their card instead of pushing the page sideways. */
  .table-wrap {
    @apply relative overflow-x-auto rounded-lg bg-white ring-1 ring-ink-200;
  }
  .data-table {
    @apply min-w-full text-left text-sm;
  }
  .data-table > thead > tr > th {
    @apply sticky top-0 z-10 whitespace-nowrap border-b border-ink-200 bg-ink-50 px-3 py-2.5 text-xs font-semibold text-ink-600 first:pl-4 last:pr-4;
  }
  .data-table > thead > tr > th a {
    @apply text-ink-700 hover:text-ink-950;
  }
  .data-table > tbody > tr {
    @apply border-b border-ink-100 last:border-0 hover:bg-ink-50/60;
  }
  .data-table > tbody > tr > td {
    @apply px-3 py-2.5 align-middle text-ink-800 first:pl-4 last:pr-4;
  }
  /* Phones: each row becomes a card, one cell per line with its column's name beside it (lib/admin_table_labels.js
     copies the header into data-label), so no column hides off the right edge. Add data-table-scroll to keep a table
     as a grid that scrolls inside its wrap instead. */
  @media (max-width: 639.98px) {
    .data-table:not(.data-table-scroll) {
      @apply block;
      /* The header keeps only its sort links, as a row of chips above the cards. */
      > thead {
        @apply block border-b border-ink-200 bg-ink-50 px-4 py-1;
      }
      > thead > tr {
        @apply flex flex-wrap items-center gap-x-5;
      }
      > thead > tr > th {
        @apply static border-0 bg-transparent p-0 first:pl-0 last:pr-0;
      }
      > thead > tr > th a {
        @apply inline-block py-1.5 underline decoration-ink-300 underline-offset-2;
      }
      > thead > tr > th:not(:has(a)) {
        @apply sr-only;
      }
      > thead:not(:has(a)) {
        @apply sr-only;
      }
      > tbody {
        @apply block;
      }
      > tbody > tr {
        @apply block px-4 py-3;
      }
      > tbody > tr > td {
        @apply block min-w-0 max-w-none whitespace-normal py-0.5 pl-28 pr-0 text-left first:pl-28 last:pr-0;
      }
      > tbody > tr > td:empty, > tbody > tr > td.cell-blank {
        @apply hidden;
      }
      /* A cell spanning the row (a note, an empty state) uses the card's full width. */
      > tbody > tr > td[colspan] {
        @apply pl-0 first:pl-0;
      }
      /* Floated into the cell's left padding, so a long column name wraps instead of being cut off. */
      > tbody > tr > td[data-label]:not([data-label=""])::before {
        @apply float-left -ml-28 w-24 whitespace-normal pt-0.5 font-sans text-xs font-medium normal-case leading-snug tracking-normal text-ink-500;
        content: attr(data-label);
      }
      > tbody > tr > td::after {
        @apply clear-both block;
        content: "";
      }
    }
  }
  .table-row-attention {
    @apply bg-amber-50/70 hover:bg-amber-50;
  }
  .cell-muted {
    @apply text-ink-500;
  }
  .cell-num {
    @apply whitespace-nowrap text-right;
  }
  .cell-id {
    @apply whitespace-nowrap font-mono text-xs;
  }

  /* Status badges: tone carries meaning, the word carries it too. */
  .badge {
    @apply inline-flex items-center gap-1 whitespace-nowrap rounded-full px-2 py-0.5 text-xs font-medium ring-1 ring-inset;
  }
  .badge-neutral { @apply badge bg-ink-100 text-ink-700 ring-ink-200; }
  .badge-brand { @apply badge bg-lime-100 text-olive-800 ring-lime-300; }
  .badge-success { @apply badge bg-green-50 text-green-800 ring-green-200; }
  .badge-warning { @apply badge bg-amber-50 text-amber-800 ring-amber-200; }
  .badge-danger { @apply badge bg-red-50 text-red-800 ring-red-200; }
  .badge-info { @apply badge bg-sky-50 text-sky-800 ring-sky-200; }

  /* Filter chips: each one a link to a filtered view, with its count. */
  .chips {
    @apply -mx-1 flex flex-wrap items-center gap-1.5 px-1;
  }
  .chip {
    @apply inline-flex items-center gap-1.5 whitespace-nowrap rounded-full bg-white px-3 py-1 text-sm font-medium text-ink-700 ring-1 ring-inset ring-ink-200 transition-colors duration-150 hover:bg-ink-50 hover:text-ink-900;
  }
  .chip-active {
    @apply bg-ink-900 text-white ring-ink-900 hover:bg-ink-800 hover:text-white;
  }
  .chip-count {
    @apply rounded-full bg-ink-100 px-1.5 text-xs font-semibold tabular-nums text-ink-700;
  }
  .chip-active .chip-count {
    @apply bg-lime-400 text-ink-950;
  }

  /* Toolbar above a table: chips on the left, search and filters on the right. */
  .toolbar {
    @apply mb-4 flex flex-wrap items-center justify-between gap-3;
  }
  /* Chips keep to one line; the filters drop below them when the row is too narrow for both. */
  .toolbar > .chips {
    @apply max-w-full shrink-0;
  }
  .toolbar > .filters {
    @apply ml-auto;
  }
  .filters {
    @apply flex flex-wrap items-center gap-2;
  }
  .filters select, .filters input[type="text"], .filters input[type="search"], .filters input[type="number"] {
    @apply py-1.5;
  }
  /* A visible label and its field, kept on one line when the filters wrap. */
  .filter-field {
    @apply inline-flex items-center gap-2 whitespace-nowrap text-sm text-ink-600;
  }

  /* Forms */
  /* A date in British order (AdminUiHelper#british_date_field): the text, a calendar button inside its right edge,
     and the browser's date input it opens, invisible, under the field so its picker opens there. */
  .date-field {
    @apply relative items-stretch;
  }
  .date-field > input[type="text"] {
    @apply pr-9 tabular-nums;
  }
  .date-field-button {
    @apply absolute inset-y-0 right-0 flex w-9 items-center justify-center rounded-r-md text-ink-800 hover:text-ink-950 focus-visible:outline-2 focus-visible:outline-olive-600;
  }
  .date-field-picker {
    @apply pointer-events-none absolute bottom-0 left-0 h-px w-px opacity-0;
  }
  .field-label {
    @apply mb-1 block text-sm font-medium text-ink-800;
  }
  .field-hint {
    @apply mt-1 text-xs text-ink-500;
  }
  .field-error {
    @apply mt-1 text-xs font-medium text-red-700;
  }

  /* Definition lists for record detail pages. */
  .kv {
    @apply grid grid-cols-1 gap-x-6 gap-y-3 text-sm sm:grid-cols-[minmax(9rem,max-content)_1fr];
  }
  .kv > dt {
    @apply font-medium text-ink-500;
  }
  .kv > dd {
    @apply text-ink-900;
  }
  /* Narrow column variant (a sidebar card): each label sits right above its value. */
  .kv-stacked {
    @apply grid grid-cols-1 text-sm;
  }
  .kv-stacked > dt {
    @apply mt-3 text-xs font-medium text-ink-500 first:mt-0;
  }
  .kv-stacked > dd {
    @apply mt-0.5 text-ink-900;
  }

  .empty-state {
    @apply flex flex-col items-center justify-center gap-1 px-6 py-12 text-center;
  }
  .empty-state-title {
    @apply text-sm font-semibold text-ink-900;
  }
  .empty-state-body {
    @apply max-w-md text-sm text-ink-500;
  }

  .callout {
    @apply rounded-lg px-4 py-3 text-sm ring-1 ring-inset;
  }
  .callout-info { @apply callout bg-sky-50 text-sky-900 ring-sky-200; }
  .callout-warning { @apply callout bg-amber-50 text-amber-900 ring-amber-200; }
  .callout-danger { @apply callout bg-red-50 text-red-900 ring-red-200; }
  .callout-success { @apply callout bg-green-50 text-green-900 ring-green-200; }

  .kbd {
    @apply rounded border border-ink-300 bg-white px-1 font-mono text-[11px] text-ink-600;
  }
}

/* Pagy's series nav, restyled to match the chips. */
.pagy {
  @apply mt-4 flex flex-wrap items-center gap-1 text-sm font-medium text-ink-700;
  a:not(.gap) {
    @apply block rounded-md px-2.5 py-1 ring-1 ring-inset ring-ink-200 bg-white tabular-nums;
    &:hover {
      @apply bg-ink-50;
    }
    &:not([href]) { /* disabled links */
      @apply cursor-default text-ink-300;
    }
    &.current {
      @apply bg-ink-900 text-white ring-ink-900;
    }
  }
  label {
    @apply inline-block whitespace-nowrap rounded-md bg-ink-100 px-3 py-0.5;
    input {
      @apply rounded-md border-none bg-white;
    }
  }
}
