/*
 * The CSS bridge a public app stands on — `@conjunto/ui/island.css`.
 *
 * A public app is mounted into very different pages: one wearing Tabler, and
 * one rendered by a site theme that brings no CSS framework at all. It must
 * look at home in both without knowing which one it is in — so it styles
 * itself against a small set of its own custom properties (the `--i-*`
 * contract below) and the `i-*` classes, never against a framework's class
 * names.
 *
 * Each property is filled by a fallback chain: the names site themes commonly
 * use (`--c-*`), then Tabler's `--tblr-*` (which only resolve on a Tabler
 * page), then a neutral literal. A theme that wants exact control sets the
 * `--i-*` value itself — its own declaration wins.
 *
 * Imported by the app (`import "@conjunto/ui/island.css"`), so it is bundled
 * into the app's own stylesheet, which `{% public_app %}` links with its
 * integrity hash. Reference: `docs/usage/public_apps.md`.
 */

/* Both anchors carry the contract: an app that renders its own markup sits
   inside `[data-island]`, while a server-rendered page that uses the `i-*`
   classes around the mount element wraps them in `.i-page`. Custom properties
   only inherit downwards — declared on the mount alone, every class outside
   it would silently fall back to `initial` (no border, no fill). */
[data-island], .i-page {
  --i-accent: var(--c-accent, var(--tblr-primary, #206bc4));
  --i-muted: var(--c-muted, var(--tblr-secondary, #667382));

  /* Text *on* the accent fill (a primary button's label). Derived below when
     the browser can; a plain white only remains for browsers too old for
     that. */
  --i-accent-fg: var(--c-on-accent, #fff);

  --i-fg: var(
    --c-text,
    var(--c-ink, var(--c-fg, var(--c-body-fg, var(--tblr-body-color, #182433))))
  );
  --i-surface: var(
    --c-surface,
    var(--c-page, var(--c-bg, var(--c-body-bg, var(--tblr-bg-surface, #fff))))
  );
  --i-line: var(--c-border, var(--c-line, var(--tblr-border-color, #dfe3e7)));

  /* Status tones. Literals, so they stay readable against any palette; fills
     are tinted with color-mix below. */
  --i-danger: #b42318;
  --i-warning: #b54708;
  --i-success: #2f7d32;

  --i-radius: 6px;
  --i-gap: 1rem;

  color: var(--i-fg);
}

/* Black or white on the accent, whichever WCAG says is readable — so a theme
   (or a tenant's own accent colour) never has to know about the apps at all.
   `--c-on-accent` still wins where a theme names its own tone.

   The choice is made on the accent's *relative luminance*: every sRGB colour
   clears 4.5 : 1 against black or against white, but only if the flip happens
   at luminance 0.1775. A flip on a perceptual lightness (OKLCH `l`) bottoms
   out around 4.1 : 1 for mid-tone greens and blues.

   `pow((c + 0.055) / 1.055, 2.4)` is the sRGB transfer function without its
   linear toe, which shifts the result only for near-black channels, far from
   the flip. */
@supports (color: rgb(from red r g b)) and (width: calc(pow(2, 2) * 1px)) {
  [data-island], .i-page {
    --i-accent-luminance: calc(
      0.2126 * pow((r / 255 + 0.055) / 1.055, 2.4) +
      0.7152 * pow((g / 255 + 0.055) / 1.055, 2.4) +
      0.0722 * pow((b / 255 + 0.055) / 1.055, 2.4)
    );
    /* 0 or 255 — the multiplier only has to be steep enough to saturate. */
    --i-accent-fg-channel: calc(
      255 * clamp(0, (0.1775 - var(--i-accent-luminance)) * 1000, 1)
    );
    --i-accent-fg: var(
      --c-on-accent,
      rgb(
        from var(--i-accent) var(--i-accent-fg-channel)
          var(--i-accent-fg-channel) var(--i-accent-fg-channel)
      )
    );
  }
}

/* An app sits inside a page that already set a font; inherit, don't impose. */
[data-island] :is(h1, h2, h3, p, label, button, input, select, textarea, dt, dd) {
  font-family: inherit;
}

/* ─── page scaffolding ──────────────────────────────────────────────────
 *
 * A themed page carries no CSS framework, so the few layout pieces a
 * standalone form needs live here. Deliberately not a grid: these are single
 * column forms, lists and notices.
 */

.i-page {
  max-width: 40rem;
  margin: 0 auto;
  padding: 2.5rem 1rem;
}

.i-page--wide {
  max-width: 52rem;
}

.i-lead {
  color: var(--i-muted);
  margin: 0 0 1.5rem;
}

.i-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: var(--i-gap);
}

/* ─── surfaces ──────────────────────────────────────────────────────── */

.i-card {
  background: var(--i-surface);
  border: 1px solid var(--i-line);
  border-radius: var(--i-radius);
  padding: var(--i-gap);
}

.i-card + .i-card,
.i-card + .i-stack,
.i-stack + .i-card {
  margin-top: var(--i-gap);
}

.i-title {
  font-size: 1.125rem;
  font-weight: 600;
  margin: 0 0 0.5rem;
}

.i-heading {
  font-size: 1.375rem;
  font-weight: 600;
  margin: 0;
}

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

.i-small {
  font-size: 0.875rem;
}

.i-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--i-gap);
  margin-bottom: var(--i-gap);
  flex-wrap: wrap;
}

.i-stack > * + * {
  margin-top: var(--i-gap);
}

/* ─── buttons ───────────────────────────────────────────────────────── */

/* 2.75 rem = 44 px: the touch-target minimum — public forms are filled in on
   phones. Small and inline buttons opt out. */
.i-btn {
  font: inherit;
  line-height: 1.4;
  min-height: 2.75rem;
  padding: 0.4rem 0.9rem;
  border-radius: var(--i-radius);
  border: 1px solid var(--i-line);
  background: transparent;
  color: var(--i-fg);
  cursor: pointer;
}

.i-btn:hover:not(:disabled) {
  border-color: var(--i-accent);
}

.i-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.i-btn:focus-visible {
  outline: 2px solid var(--i-accent);
  outline-offset: 2px;
}

.i-btn--primary {
  background: var(--i-accent);
  border-color: var(--i-accent);
  color: var(--i-accent-fg);
}

.i-btn--quiet {
  border-color: transparent;
  color: var(--i-muted);
}

.i-btn--danger {
  border-color: transparent;
  color: var(--i-danger);
  min-height: 0;
  padding: 0 0.35rem;
}

.i-btn--sm {
  min-height: 0;
  padding: 0.2rem 0.6rem;
  font-size: 0.875rem;
}

.i-btn + .i-btn {
  margin-left: 0.5rem;
}

/* ─── form fields ───────────────────────────────────────────────────── */

.i-field + .i-field,
.i-field + .i-alert,
.i-field + .i-btn {
  margin-top: var(--i-gap);
}

.i-label {
  display: block;
  margin-bottom: 0.25rem;
  font-weight: 500;
}

/* Covers <input>, <select> and <textarea> — they have to line up. */
.i-input {
  font: inherit;
  width: 100%;
  max-width: 28rem;
  min-height: 2.75rem;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--i-line);
  border-radius: var(--i-radius);
  background: var(--i-surface);
  color: var(--i-fg);
}

textarea.i-input {
  max-width: none;
  resize: vertical;
}

/* `:user-invalid`, not `:invalid`: the latter marks every empty required
   field before the visitor typed anything, and the form comes up looking as
   if it had already failed. */
.i-input:user-invalid {
  border-color: var(--i-danger);
}

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

.i-errors {
  color: var(--i-danger);
  font-size: 0.875rem;
  margin-top: 0.25rem;
}

/* Django renders field errors as <ul class="errorlist">. */
.i-errors ul,
ul.errorlist {
  color: var(--i-danger);
  font-size: 0.875rem;
  margin: 0.25rem 0 0;
  padding-left: 1.1rem;
}

.i-hint {
  display: block;
  margin-top: 0.25rem;
  font-size: 0.875rem;
  color: var(--i-muted);
}

/* A themed page has no link colour of its own for this markup. */
.i-page a:not(.i-btn) {
  color: var(--i-accent);
}

/* `start` rather than `baseline`: a box this size hangs off a baseline
   instead of sitting on the first line of its label. */
.i-check {
  display: flex;
  align-items: start;
  gap: 0.5rem;
}

.i-check input[type="checkbox"],
.i-check input[type="radio"] {
  flex: none;
  width: 1.5rem;
  height: 1.5rem;
  min-height: 1.5rem;
  margin: 0.15rem 0 0;
  accent-color: var(--i-accent);
}

/* ─── notices ───────────────────────────────────────────────────────── */

.i-alert {
  border: 1px solid currentColor;
  border-radius: var(--i-radius);
  padding: 0.6rem 0.9rem;
  margin: var(--i-gap) 0;
}

/* color-mix keeps the fill in step with whatever tone the modifier sets. */
.i-alert--danger {
  color: var(--i-danger);
  background: color-mix(in srgb, var(--i-danger) 8%, transparent);
}

.i-alert--warning {
  color: var(--i-warning);
  background: color-mix(in srgb, var(--i-warning) 8%, transparent);
}

.i-alert--success {
  color: var(--i-success);
  background: color-mix(in srgb, var(--i-success) 8%, transparent);
}

.i-alert--info {
  color: var(--i-accent);
  background: color-mix(in srgb, var(--i-accent) 8%, transparent);
}

.i-alert--bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--i-gap);
}

.i-badge {
  display: inline-block;
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  font-size: 0.8125rem;
  background: color-mix(in srgb, var(--i-accent) 12%, transparent);
  color: var(--i-accent);
}

.i-badge--ok {
  background: color-mix(in srgb, var(--i-success) 12%, transparent);
  color: var(--i-success);
}

/* ─── data display ──────────────────────────────────────────────────── */

.i-empty {
  text-align: center;
  padding: 2rem 1rem;
}

.i-empty-title {
  font-weight: 600;
  margin: 0 0 0.25rem;
}

.i-fields {
  display: grid;
  grid-template-columns: minmax(6rem, 10rem) 1fr;
  gap: 0.35rem var(--i-gap);
  margin: var(--i-gap) 0 0;
}

.i-fields dt {
  color: var(--i-muted);
  font-weight: 400;
}

.i-fields dd {
  margin: 0;
}
/**
 * The slot picker of the booking and the rebooking app.
 *
 * Only these two apps use it, so it is theirs rather than part of
 * `@conjunto/ui/island.css`; it styles against the same `--i-*` contract.
 * Imported after `island.css` by both `main.js`.
 */

/* Radio group rendered as pickable chips (appointment slots). The radio stays
   in the DOM and focusable — it is what the form submits and what a screen
   reader announces; only its default appearance is replaced. */
.i-slots {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(9rem, 1fr));
  gap: 0.5rem;
}

.i-slot input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.i-slot label {
  display: block;
  text-align: center;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--i-line);
  border-radius: var(--i-radius);
  cursor: pointer;
}

/* The fixed length the practice set for this purpose, under the day. */
.i-slot-time {
  display: block;
  font-size: 0.8125rem;
  color: var(--i-muted);
}

.i-slot input:checked + label .i-slot-time {
  color: inherit;
}

.i-slot input:checked + label {
  background: var(--i-accent);
  border-color: var(--i-accent);
  color: var(--i-accent-fg);
}

.i-slot input:focus-visible + label {
  outline: 2px solid var(--i-accent);
  outline-offset: 2px;
}
