/*
 * appyMenu design system: colour tokens and shared components.
 *
 * The one place colours are defined. Tailwind's `primary` and `am-*` colours
 * (assets/js/ui.js), style.css and swal-theme.css all read these variables,
 * so a colour is changed here and nowhere else.
 *
 * Values come from the Figma palette (Frame 1984078656) and its campaign
 * table. The few marked "provisional" are not in Figma; they are listed in
 * docs/UI-STANDARDISATION-PLAN.md (section 5.2) until they are confirmed.
 * Figma's colours are Tailwind v4's 600 shades (red, blue, green, orange),
 * so the provisional ones are the neighbouring shades of the same palette.
 *
 * Specificity: the Tailwind Play CDN appends its <style> after every
 * stylesheet, so its preflight and forms-plugin rules win any tie with this
 * file. Rules that have to beat them ([type='button'] backgrounds,
 * [type='text'] borders) are qualified with an element name. Nothing here
 * uses !important.
 */

/* ── Tokens ────────────────────────────────────────────────────────────── */

:root {
  /* Figma palette, the same in both themes. Kept as "r g b" so a colour can
     be used at any opacity: rgb(var(--am-red-rgb) / 0.15). */
  --am-red-rgb: 231 0 11;          /* Primary Color        #E7000B */
  --am-grey-rgb: 108 117 125;      /* Secondary Color      #6C757D */
  --am-black-rgb: 33 30 30;        /* Black Color          #211E1E */
  --am-stroke-rgb: 224 224 224;    /* Stroke Outline Color #E0E0E0 */
  --am-field-rgb: 243 243 245;     /* Field Color          #F3F3F5 */
  --am-green-rgb: 0 166 62;        /* Green Color          #00A63E */
  --am-green-light-rgb: 0 201 80;  /* Light Green is this at 15%: #D9F7E5 */
  --am-blue-rgb: 21 93 252;        /* Blue Color           #155DFC */
  --am-orange-rgb: 245 73 0;       /* Duplicate action     #F54900 */

  /* Brand and status */
  --am-primary-rgb: var(--am-red-rgb);
  --am-primary: rgb(var(--am-primary-rgb));
  --am-primary-hover-rgb: 193 0 7; /* provisional: #C10007 (red-700) */
  --am-primary-hover: rgb(var(--am-primary-hover-rgb));
  --am-danger: var(--am-primary);  /* Figma has a single red */
  --am-success-rgb: var(--am-green-rgb);
  --am-success: rgb(var(--am-success-rgb));
  /* A solid green with white text on it (popup confirm buttons): white on the
     Figma green is 3.2:1, on green-700 5.0:1. Not a Figma value (decision 17). */
  --am-success-strong: #008236;
  --am-info-rgb: var(--am-blue-rgb);
  --am-info: rgb(var(--am-info-rgb));

  /* Coloured icons, badges and soft buttons. -soft is the fill in the Figma
     table (a tint of the colour), -light the palette's "Light" swatch at 15%,
     used for hover; both keep the Figma colours.
     -fg is the icon colour (3:1 on the fill, hovered too) and text on a plain
     surface. -text is text on the fill, in badges and soft buttons (4.5:1,
     also in a hovered table row). Where the Figma colour falls short, these
     are a darker shade of the same palette, not Figma values (decision 10). */
  --am-red-fg: rgb(var(--am-red-rgb));
  --am-red-text: #C10007;                                   /* red-700 */
  --am-red-soft: rgb(var(--am-red-rgb) / 0.07);
  --am-red-light: rgb(var(--am-red-rgb) / 0.15);            /* Light Red   #FBD9DA */
  --am-blue-fg: rgb(var(--am-blue-rgb));
  --am-blue-text: #1447E6;                                  /* blue-700 */
  --am-blue-soft: rgb(var(--am-blue-rgb) / 0.1);
  --am-blue-light: rgb(var(--am-blue-rgb) / 0.15);          /* Light Blue  #DCE7FF */
  --am-green-fg: #008236;                                   /* green-700 */
  --am-green-text: #016630;                                 /* green-800 */
  --am-green-soft: rgb(var(--am-green-rgb) / 0.1);
  --am-green-light: rgb(var(--am-green-light-rgb) / 0.15);  /* Light Green #D9F7E5 */
  --am-orange-fg: #CA3500;                                  /* orange-700 */
  --am-orange-soft: rgb(var(--am-orange-rgb) / 0.1);
  --am-orange-light: rgb(var(--am-orange-rgb) / 0.15);
  --am-grey-fg: rgb(var(--am-grey-rgb));
  --am-grey-text: rgb(var(--am-black-rgb));                 /* Figma Black */
  --am-grey-soft: rgb(var(--am-grey-rgb) / 0.1);
  --am-grey-light: rgb(var(--am-grey-rgb) / 0.15);

  /* Warning / Pending: not in the Figma palette, so these are the Tailwind
     ambers the pages already use, kept so the meaning does not change. */
  --am-warning: #F59E0B;           /* amber-500: popup warning icon */
  --am-warning-ring: #FCD34D;      /* amber-300: its ring */
  --am-warning-fg: #92400E;        /* amber-800: Pending badge text, warning text */
  --am-warning-soft: #FEF3C7;      /* amber-100: Pending badge fill */
  --am-warning-strong: #D97706;    /* amber-600: a solid amber under a white glyph (verify email "expired") */
  --am-warning-tint: #FFFBEB;      /* amber-50: a row or panel that is new / needs a look */
  --am-warning-border: #FDE68A;    /* amber-200: the border of a warning panel */

  /* Purple: the Manager role badge (decision 14). Not in the Figma palette; the
     Tailwind purples the badge already used, so the meaning does not change. */
  --am-purple-soft: #F3E8FF;       /* purple-100 */
  --am-purple-text: #6B21A8;       /* purple-800 */

  /* Text */
  --am-text-rgb: var(--am-black-rgb);
  --am-text: rgb(var(--am-text-rgb));
  --am-text-muted-rgb: var(--am-grey-rgb);
  --am-text-muted: rgb(var(--am-text-muted-rgb));
  --am-text-on-color: #FFFFFF;

  /* Surfaces and lines */
  --am-surface-rgb: 255 255 255;
  --am-surface: rgb(var(--am-surface-rgb));
  --am-surface-muted-rgb: 249 250 251; /* Figma table header #F9FAFB */
  --am-surface-muted: rgb(var(--am-surface-muted-rgb));
  --am-border-rgb: var(--am-stroke-rgb);
  --am-border: rgb(var(--am-border-rgb));
  --am-border-hover: #BDBDBD;          /* provisional */

  /* App chrome: the sidebar and the header's logo box (the header bar is the
     brand red). Not in the Figma palette: the existing rose-50, and in dark
     mode the existing #252D37 (provisional). */
  --am-chrome-rgb: 255 241 242;        /* rose-50 #FFF1F2 */
  --am-chrome: rgb(var(--am-chrome-rgb));
  /* Sidebar and the header's logo box above it: the original rose-50 (as
     --am-chrome); dark mode keeps the existing #252D37. Items tint with the
     brand red at 10% on hover and when active; the label of the open page
     uses --am-red-text. */
  --am-sidebar-bg-rgb: var(--am-chrome-rgb);
  --am-sidebar-bg: rgb(var(--am-sidebar-bg-rgb));
  --am-sidebar-item-hover: rgb(var(--am-red-rgb) / 0.1);
  --am-sidebar-item-active: rgb(var(--am-red-rgb) / 0.1);
  /* The page behind the cards (<main>): white, in dark mode slate-900. */
  --am-page-rgb: 255 255 255;
  --am-page: rgb(var(--am-page-rgb));
  /* The page behind everything on the sign-in family and the dashboard layout
     (the <body>): the warm off-white of the original design, and in dark mode
     the near-black it used. Tailwind: bg-background-light / bg-background-dark. */
  --am-canvas-light-rgb: 255 247 237;  /* orange-50 #FFF7ED */
  --am-canvas-dark-rgb: 26 26 26;      /* #1A1A1A */
  /* The dim layer behind a dialog, and the faint hover wash over a thumbnail:
     black at a low opacity (bg-am-scrim/50), the same in both themes. */
  --am-scrim-rgb: 0 0 0;
  --am-shadow-card: 0 1px 4px 0 rgb(var(--am-scrim-rgb) / 0.25);   /* Figma card shadow */
  --am-shadow-float: 0 25px 50px -12px rgb(var(--am-scrim-rgb) / 0.25);  /* bars and menus that float over the page */
  --am-shadow-modal: 0 20px 60px rgb(var(--am-scrim-rgb) / 0.3);   /* a dialog */
  /* Sign-in page (split layout): the red panel and the warm off-white. Not
     Figma values: the panel is the darker brand red (as primary hover), on
     which white and off-white text keep 4.5:1; the off-white is the existing
     #F6F5F3 (provisional). */
  --am-auth-red: var(--am-primary-hover);
  --am-auth-paper: #F6F5F3;
  /* The sign-in form's fields, which always sit light on the red panel in both
     themes (they mirror appyFeed's sign-in): fill, text, placeholder, icon, and
     the invalid state. The existing values, moved here; provisional. */
  --am-auth-field-bg: #F0F0F0;
  --am-auth-field-bg-focus: #FFFFFF;
  --am-auth-field-text: #333333;
  --am-auth-field-placeholder: #AAAAAA;
  --am-auth-field-icon: #888888;
  --am-auth-field-invalid-bg: #FDF3F2;
  --am-auth-field-invalid-border: #F1948C;

  /* Fields: forms use the palette's Field Color, search and filter bars are
     white in the Figma table. */
  --am-field-bg-rgb: var(--am-field-rgb);
  --am-field-bg: rgb(var(--am-field-bg-rgb));
  --am-search-bg: var(--am-surface);

  /* Placeholders (decision 11). The Figma grey is 4.2:1 on the grey form
     field, so form fields use it 6% darker; on the white search field it
     is 4.7:1 and stays. Not a Figma value. */
  --am-placeholder: #666E76;
  --am-search-placeholder: var(--am-text-muted);

  /* Switch when off (decision 15): the muted grey, like an empty checkbox's
     border, so the track keeps 3:1 against the row. Light: Figma Secondary
     with the white knob (4.7:1). */
  --am-switch-off: var(--am-text-muted);
  --am-switch-off-knob: var(--am-text-on-color);

  /* Loading skeleton: its base fill and the highlight that sweeps across it. */
  --am-skeleton-base: rgb(var(--am-field-rgb));
  --am-skeleton-shine: rgb(var(--am-stroke-rgb));

  /* Focus. The border and the button ring are what shows focus, so they are
     solid (at least 3:1 against what they sit on); the halo only softens it. */
  --am-focus: var(--am-primary);
  --am-focus-ring: rgb(var(--am-primary-rgb) / 0.18); /* halo round a focused field */
  --am-focus-outline: var(--am-primary);              /* keyboard focus on a button */

  /* Shape and size (Figma: 8px controls, 16px cards, 36px icon buttons) */
  --am-radius-control: 8px;
  --am-radius-card: 16px;
  --am-control-height: 44px;
  --am-control-height-sm: 36px;
  --am-icon-size: 18px;
  --am-disabled-opacity: 0.5;
}

/* Dark mode has no Figma design: these follow the slate palette the dark
   pages already use (provisional). The brand and status colours do not
   change; focus and icons use the lighter 400 shades, which keep 3:1 on slate. */
.dark {
  --am-text-rgb: 241 245 249;          /* slate-100 */
  --am-text-muted-rgb: 156 163 175;    /* gray-400 */
  --am-surface-rgb: 30 41 59;          /* slate-800 */
  --am-surface-muted-rgb: 51 65 85;    /* slate-700 */
  --am-border-rgb: 71 85 105;          /* slate-600 */
  --am-border-hover: #64748B;          /* slate-500 */
  --am-chrome-rgb: 37 45 55;           /* #252D37, the existing dark header and sidebar */
  --am-sidebar-bg-rgb: 37 45 55;       /* #252D37, as --am-chrome */
  --am-sidebar-item-hover: var(--am-surface-muted); /* slate-700, the dark hover the sidebar already had */
  --am-sidebar-item-active: var(--am-primary);       /* white label on the brand red */
  --am-page-rgb: 15 23 42;             /* slate-900 */
  --am-field-bg-rgb: 51 65 85;         /* slate-700 */
  --am-search-bg: var(--am-field-bg);
  --am-skeleton-base: rgb(var(--am-surface-muted-rgb));  /* slate-700 */
  --am-skeleton-shine: rgb(var(--am-border-rgb));        /* slate-600 */
  --am-placeholder: #A8AEB9;          /* gray-400 lightened: 4.6:1 on slate-700 */
  --am-search-placeholder: var(--am-placeholder);
  --am-switch-off-knob: var(--am-surface); /* a white knob is 2.5:1 on the gray-400 track */
  --am-focus: var(--am-red-fg);
  --am-focus-ring: rgb(var(--am-primary-rgb) / 0.35);
  --am-focus-outline: var(--am-red-fg);

  /* Lighter icon colours and stronger tints, so they read on slate. Red and
     blue text go a shade lighter again, for badges in a hovered row. */
  --am-red-fg: #FF6467;
  --am-red-text: #FFA2A2;                   /* red-300 */
  --am-red-soft: rgb(var(--am-red-rgb) / 0.2);
  --am-red-light: rgb(var(--am-red-rgb) / 0.3);
  --am-blue-fg: #51A2FF;
  --am-blue-text: #8EC5FF;                  /* blue-300 */
  --am-blue-soft: rgb(var(--am-blue-rgb) / 0.2);
  --am-blue-light: rgb(var(--am-blue-rgb) / 0.3);
  --am-green-fg: #05DF72;
  --am-green-text: #05DF72;
  --am-green-soft: rgb(var(--am-green-rgb) / 0.2);
  --am-green-light: rgb(var(--am-green-rgb) / 0.3);
  --am-orange-fg: #FF8904;
  --am-orange-soft: rgb(var(--am-orange-rgb) / 0.2);
  --am-orange-light: rgb(var(--am-orange-rgb) / 0.3);
  --am-grey-fg: #CBD5E1;
  --am-grey-text: #CBD5E1;
  --am-grey-soft: rgb(148 163 184 / 0.16);
  --am-grey-light: rgb(148 163 184 / 0.26);
  --am-warning-fg: #FCD34D;                 /* amber-300, as the dark Pending badge */
  --am-warning-soft: rgb(120 53 15 / 0.3);  /* amber-900 at 30% */
  --am-warning-tint: rgb(120 53 15 / 0.1);  /* amber-900 at 10% */
  --am-warning-border: #92400E;             /* amber-800 */
  --am-purple-soft: rgb(88 28 135 / 0.3);   /* purple-900 at 30% */
  --am-purple-text: #D8B4FE;                /* purple-300 */
}

/* ── Base: Tailwind's forms plugin, in the brand colour ──────────────────
 * The forms plugin draws every focused field, and every checked or focused
 * checkbox, in blue (#2563eb). These rules swap in the tokens:
 *   - only on pages whose <body> has .am-ui (the dashboard and sign-in
 *     layouts; the Design Board opts in during its own phase);
 *   - only where the element says nothing itself: a field with its own
 *     focus:border-* or focus:ring-* classes, or a checkbox with a text-*
 *     colour, keeps it.
 * Specificity is (0,2,1): one step above the plugin's (0,2,0), and below any
 * page rule written with two classes, like the 2FA code boxes. */

:where(.am-ui) :is(input:where(:not([type="checkbox"], [type="radio"], [type="range"], [type="file"], [type="color"], [type="button"], [type="submit"], [type="reset"], [type="image"], [type="hidden"])), textarea, select):not([class*="focus:border"]):focus {
  border-color: var(--am-focus);
}

:where(.am-ui) :is(input:where(:not([type="checkbox"], [type="radio"], [type="range"], [type="file"], [type="color"], [type="button"], [type="submit"], [type="reset"], [type="image"], [type="hidden"])), textarea, select):not([class*="focus:ring"]):focus {
  --tw-ring-shadow: 0 0 0 3px var(--am-focus-ring);
}

/* The plugin fills a checked box with currentColor. */
:where(.am-ui) input:where([type="checkbox"], [type="radio"]):not([class*="text-"]) {
  color: var(--am-primary);
}

:where(.am-ui) input:where([type="checkbox"], [type="radio"]):not([class*="focus:ring"]):focus {
  --tw-ring-color: var(--am-focus-outline);
}

/* ── Spacing ───────────────────────────────────────────────────────────────
 * The components start with no margin, at zero specificity, so a margin
 * utility on one (mt-6, ml-4) still applies. */
:where(.am-btn, .am-icon-btn, .am-field, .am-check) {
  margin: 0;
}

/* ── Icons ─────────────────────────────────────────────────────────────────
 * <x-icon name="edit" /> in Blade, AppUI.icon('edit') in scripts; the glyphs
 * are in resources/views/partials/icon-sprite.blade.php. */

svg.am-icon {
  display: inline-block;
  flex-shrink: 0;
  width: var(--am-icon-size);
  height: var(--am-icon-size);
  vertical-align: middle;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Tailwind's preflight draws a bare <svg> as a block, so an icon that moved
 * to the sprite from inline markup keeps that with the .block utility (the
 * .am-icon selector above is more specific than a utility alone). */
svg.am-icon.block {
  display: block;
}

svg.am-icon.align-baseline {
  vertical-align: baseline;
}

/* ── Buttons ───────────────────────────────────────────────────────────────
 * <button class="am-btn am-btn-primary">. A variant only sets the
 * --am-btn-* variables; the element-qualified rule below draws them. */

.am-btn {
  --am-btn-bg: var(--am-surface);
  --am-btn-bg-hover: var(--am-surface-muted);
  --am-btn-fg: var(--am-text);
  --am-btn-fg-hover: var(--am-btn-fg);
  --am-btn-border: var(--am-border);
  --am-btn-border-hover: var(--am-border-hover);
}

/* OK / Save / Create / Add / Update / Search */
.am-btn-primary {
  --am-btn-bg: var(--am-primary);
  --am-btn-bg-hover: var(--am-primary-hover);
  --am-btn-fg: var(--am-text-on-color);
  --am-btn-border: transparent;
  --am-btn-border-hover: transparent;
}

/* Cancel / Close / Reset / Back: the defaults above */
.am-btn-secondary {
  --am-btn-bg: var(--am-surface);
}

/* Delete / Remove buttons with text: solid brand red, white text and icon.
   Small icon-only deletes in tables use .am-icon-btn-delete instead. */
.am-btn-danger {
  --am-btn-bg: var(--am-danger);
  --am-btn-bg-hover: var(--am-primary-hover);
  --am-btn-fg: var(--am-text-on-color);
  --am-btn-border: transparent;
  --am-btn-border-hover: transparent;
}

/* Disable and other cautionary actions with no confirmation popup: a red
   outline (decision 13), like the popup's deny button. On the hover tint the
   text goes a shade darker, to keep 4.5:1 (decision 10).
   .am-btn-primary-outline is the same look for an ordinary action that keeps
   the brand colour (the log viewer's Export). Added in module 9. */
.am-btn-danger-outline,
.am-btn-primary-outline {
  --am-btn-bg: var(--am-surface);
  --am-btn-bg-hover: var(--am-red-soft);
  --am-btn-fg: var(--am-red-fg);
  --am-btn-fg-hover: var(--am-red-text);
  --am-btn-border: var(--am-red-fg);
  --am-btn-border-hover: var(--am-red-fg);
}

/* Restore / Activate */
.am-btn-success {
  --am-btn-bg: var(--am-green-soft);
  --am-btn-bg-hover: var(--am-green-light);
  --am-btn-fg: var(--am-green-text);
  --am-btn-border: transparent;
  --am-btn-border-hover: transparent;
}

/* Low-emphasis text actions */
.am-btn-ghost {
  --am-btn-bg: transparent;
  --am-btn-fg: var(--am-text-muted);
  --am-btn-fg-hover: var(--am-text);
  --am-btn-border: transparent;
  --am-btn-border-hover: transparent;
}

:is(a, button, input, label, span).am-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  min-height: var(--am-control-height);
  padding: 0 1.25rem;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  line-height: 1.25rem;
  text-align: center;
  text-decoration: none;
  color: var(--am-btn-fg);
  background-color: var(--am-btn-bg);
  background-image: none;
  border: 1px solid var(--am-btn-border);
  border-radius: var(--am-radius-control);
  cursor: pointer;
  user-select: none;
  transition: background-color 150ms, border-color 150ms, color 150ms, box-shadow 150ms, filter 150ms;
}

:is(a, button, input, label, span).am-btn:hover:not(:disabled, [aria-disabled="true"]) {
  color: var(--am-btn-fg-hover);
  background-color: var(--am-btn-bg-hover);
  border-color: var(--am-btn-border-hover);
}

:is(a, button, input, label, span).am-btn:active:not(:disabled, [aria-disabled="true"]) {
  filter: brightness(0.95);
}

:is(a, button, input, label, span).am-btn.am-btn-sm {
  min-height: var(--am-control-height-sm);
  padding: 0 0.875rem;
  font-size: 0.8125rem;
}

/* ── Icon buttons ──────────────────────────────────────────────────────────
 * Table and row actions, one class per action, so every Edit button in the
 * app changes together:
 *   <button class="am-icon-btn am-icon-btn-edit" title="…" aria-label="…">
 * Colours follow the Figma table. */

.am-icon-btn {
  --am-tone-fg: var(--am-grey-fg);
  --am-tone-fg-hover: var(--am-tone-fg);
  --am-tone-bg: var(--am-grey-soft);
  --am-tone-bg-hover: var(--am-grey-light);
}

.am-icon-btn-view,
.am-icon-btn-edit {
  --am-tone-fg: var(--am-blue-fg);
  --am-tone-bg: var(--am-blue-soft);
  --am-tone-bg-hover: var(--am-blue-light);
}

.am-icon-btn-duplicate {
  --am-tone-fg: var(--am-orange-fg);
  --am-tone-bg: var(--am-orange-soft);
  --am-tone-bg-hover: var(--am-orange-light);
}

.am-icon-btn-delete {
  --am-tone-fg: var(--am-red-fg);
  --am-tone-bg: var(--am-red-soft);
  --am-tone-bg-hover: var(--am-red-light);
}

.am-icon-btn-restore,
.am-icon-btn-activate {
  --am-tone-fg: var(--am-green-fg);
  --am-tone-bg: var(--am-green-soft);
  --am-tone-bg-hover: var(--am-green-light);
}

/* .am-icon-btn-archive, -pause, -impersonate and -neutral keep the grey
   defaults above. */

/* The × that closes a dialog: no tile until hovered, like the popup's ×
   (decision 3). */
.am-icon-btn-close {
  --am-tone-fg: var(--am-text-muted);
  --am-tone-fg-hover: var(--am-text);
  --am-tone-bg: transparent;
  --am-tone-bg-hover: var(--am-surface-muted);
}

:is(a, button, span).am-icon-btn {
  display: inline-flex;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
  width: var(--am-control-height-sm);
  height: var(--am-control-height-sm);
  padding: 0;
  color: var(--am-tone-fg);
  background-color: var(--am-tone-bg);
  background-image: none;
  border: 0;
  border-radius: var(--am-radius-control);
  cursor: pointer;
  transition: background-color 150ms, color 150ms, box-shadow 150ms, filter 150ms;
}

:is(a, button, span).am-icon-btn:hover:not(:disabled, [aria-disabled="true"]) {
  color: var(--am-tone-fg-hover);
  background-color: var(--am-tone-bg-hover);
}

:is(a, button, span).am-icon-btn:active:not(:disabled, [aria-disabled="true"]) {
  filter: brightness(0.95);
}

/* Row of actions in a table cell (Figma: 10px apart) */
.am-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.625rem;
}

/* Keyboard focus, disabled: the same for both kinds of button */
:is(a, button, input, label, span):is(.am-btn, .am-icon-btn):focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--am-surface), 0 0 0 4px var(--am-focus-outline);
}

:is(a, button, input, label, span):is(.am-btn, .am-icon-btn):is(:disabled, [aria-disabled="true"]) {
  opacity: var(--am-disabled-opacity);
  cursor: not-allowed;
}

/* ── Fields ────────────────────────────────────────────────────────────────
 * <input class="am-field">, <select class="am-field">, <textarea class="am-field">.
 * Errors: add .is-invalid or aria-invalid="true", and put the message in
 * .am-field-error. */

.am-label {
  display: block;
  margin-bottom: 0.375rem;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--am-text);
}

/* The "*" after a required field's label */
.am-required {
  color: var(--am-red-fg);
}

:is(input, select, textarea).am-field {
  display: block;
  width: 100%;
  height: var(--am-control-height);
  padding: 0 0.875rem;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  color: var(--am-text);
  background-color: var(--am-field-bg);
  border: 1px solid var(--am-border);
  border-radius: var(--am-radius-control);
  box-shadow: none;
  transition: border-color 150ms, box-shadow 150ms, background-color 150ms;
}

/* Room for the forms plugin's chevron */
select.am-field {
  padding-right: 2.5rem;
}

textarea.am-field {
  height: auto;
  min-height: 6rem;
  padding: 0.625rem 0.875rem;
  resize: vertical;
}

:is(input, select, textarea).am-field::placeholder {
  color: var(--am-placeholder);
  opacity: 1;
}

:is(input, select, textarea).am-field:hover:not(:focus, :disabled, [readonly]) {
  border-color: var(--am-border-hover);
}

:is(input, select, textarea).am-field:focus {
  outline: none;
  border-color: var(--am-focus);
  box-shadow: 0 0 0 3px var(--am-focus-ring);
}

/* -fg, not --am-danger: in dark mode it is the lighter red, which reads on slate */
:is(input, select, textarea).am-field:is(.is-invalid, [aria-invalid="true"]) {
  border-color: var(--am-red-fg);
}

:is(input, select, textarea).am-field:disabled {
  opacity: var(--am-disabled-opacity);
  cursor: not-allowed;
}

/* 20px in all, as the error lines were before: a message that appears on
   blur moves the form's buttons no further than it used to, so a click
   that starts the blur still lands on the button it was aimed at. */
.am-field-error {
  margin-top: 0.25rem;
  font-size: 0.8125rem;
  line-height: 1rem;
  color: var(--am-red-fg);
}

/* Help text under a field; -warning is a caution, in the existing amber (decision 8). */
.am-field-hint {
  margin-top: 0.25rem;
  font-size: 0.8125rem;
  line-height: 1rem;
  color: var(--am-text-muted);
}

.am-field-hint-warning {
  color: var(--am-warning-fg);
}

/* Search box: <label class="am-search"><x-icon name="search" /><input class="am-field" type="search"></label>
   Field with an icon in front: <div class="am-icon-field"><x-icon name="mail" /><input class="am-field"></div>,
   optionally followed by a button at its end (show password):
   <button type="button" class="am-field-action" aria-label="…"><x-icon name="show-password" /></button> */
.am-search,
.am-icon-field {
  position: relative;
  display: block;
}

/* Centred on the input, not the label: a flex or grid row can stretch the label.
   In a textarea that puts it beside the first line. */
:is(.am-search, .am-icon-field) > svg.am-icon {
  position: absolute;
  top: calc(var(--am-control-height) / 2);
  left: 0.875rem;
  color: var(--am-text-muted);
  transform: translateY(-50%);
  pointer-events: none;
}

:is(.am-search, .am-icon-field) > :is(input, select, textarea).am-field {
  padding-left: 2.625rem;
}

.am-icon-field:has(> .am-field-action) > .am-field {
  padding-right: var(--am-control-height);
}

.am-icon-field > button.am-field-action {
  position: absolute;
  top: 0;
  right: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--am-control-height);
  height: var(--am-control-height);
  margin: 0;
  padding: 0;
  color: var(--am-text-muted);
  background: transparent;
  border: 0;
  border-radius: var(--am-radius-control);
  cursor: pointer;
  transition: color 150ms, box-shadow 150ms;
}

.am-icon-field > button.am-field-action:hover {
  color: var(--am-text);
}

.am-icon-field > button.am-field-action:focus-visible {
  outline: none;
  box-shadow: inset 0 0 0 2px var(--am-focus-outline);
}

.am-search > input.am-field {
  background-color: var(--am-search-bg);
}

/* A field in a filter bar (date, level, …) is white like the search box
   (decision 6): <select class="am-field am-field-filter">. Added in module 9. */
:is(input, select).am-field.am-field-filter {
  background-color: var(--am-search-bg);
}

.am-search > input.am-field::placeholder {
  color: var(--am-search-placeholder);
}

/* The browser's clear (×) button, blue by default, in the muted grey with the
   close glyph. It is still the browser's own button and clears the field as before. */
.am-search > input.am-field::-webkit-search-cancel-button {
  -webkit-appearance: none;
  appearance: none;
  width: 1rem;
  height: 1rem;
  margin: 0;
  background-color: var(--am-text-muted);
  -webkit-mask: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round'%3e%3cpath d='M6 18L18 6M6 6l12 12'/%3e%3c/svg%3e") center / contain no-repeat;
  mask: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round'%3e%3cpath d='M6 18L18 6M6 6l12 12'/%3e%3c/svg%3e") center / contain no-repeat;
  cursor: pointer;
}

.am-search > input.am-field::-webkit-search-cancel-button:hover {
  background-color: var(--am-text);
}

/* ── Checkbox ──────────────────────────────────────────────────────────────
 * <input type="checkbox" class="am-check">: square, 18px. The round
 * checkboxes over media thumbnails keep their own style in style.css. */

input[type="checkbox"].am-check {
  appearance: none;
  -webkit-appearance: none;
  display: inline-block;
  flex-shrink: 0;
  width: 18px;
  height: 18px;
  padding: 0;
  vertical-align: middle;
  color: var(--am-primary);
  background-color: var(--am-surface);
  background-position: center;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  border: 1.5px solid var(--am-text-muted); /* 3:1 or more, so an empty box is visible */
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 150ms, border-color 150ms, box-shadow 150ms;
}

input[type="checkbox"].am-check:hover:not(:disabled) {
  border-color: var(--am-primary);
}

input[type="checkbox"].am-check:is(:checked, :indeterminate) {
  background-color: var(--am-primary);
  border-color: var(--am-primary);
}

input[type="checkbox"].am-check:checked {
  background-image: url("data:image/svg+xml,%3csvg viewBox='0 0 16 16' fill='white' xmlns='http://www.w3.org/2000/svg'%3e%3cpath d='M12.207 4.793a1 1 0 010 1.414l-5 5a1 1 0 01-1.414 0l-2-2a1 1 0 011.414-1.414L6.5 9.086l4.293-4.293a1 1 0 011.414 0z'/%3e%3c/svg%3e");
}

input[type="checkbox"].am-check:indeterminate {
  background-image: url("data:image/svg+xml,%3csvg viewBox='0 0 16 16' fill='none' xmlns='http://www.w3.org/2000/svg'%3e%3cpath stroke='white' stroke-linecap='round' stroke-width='2' d='M4 8h8'/%3e%3c/svg%3e");
}

input[type="checkbox"].am-check:focus {
  outline: none;
  box-shadow: none;
}

input[type="checkbox"].am-check:focus-visible {
  box-shadow: 0 0 0 3px var(--am-focus-ring);
}

input[type="checkbox"].am-check:disabled {
  opacity: var(--am-disabled-opacity);
  cursor: not-allowed;
}

/* ── Switch ────────────────────────────────────────────────────────────────
 * An on/off checkbox drawn as a switch. The checkbox stays the control
 * (visually hidden, still focusable and clickable through its label):
 *   <label><input type="checkbox" class="sr-only"><span class="am-switch"></span> Label</label>
 * On is the Figma green with a white knob; off is --am-switch-off with
 * --am-switch-off-knob (decision 15). */

.am-switch {
  position: relative;
  display: inline-block;
  flex-shrink: 0;
  width: 2rem;
  height: 1rem;
  background-color: var(--am-switch-off);
  border-radius: 9999px;
  transition: background-color 150ms, box-shadow 150ms;
}

.am-switch::after {
  content: "";
  position: absolute;
  top: 0.125rem;
  left: 0.125rem;
  width: 0.75rem;
  height: 0.75rem;
  background-color: var(--am-switch-off-knob);
  border-radius: 9999px;
  transition: transform 150ms, background-color 150ms;
}

input:checked + .am-switch {
  background-color: var(--am-success);
}

input:checked + .am-switch::after {
  background-color: var(--am-text-on-color);
  transform: translateX(1rem);
}

input:focus-visible + .am-switch {
  box-shadow: 0 0 0 2px var(--am-surface), 0 0 0 4px var(--am-focus-outline);
}

input:disabled + .am-switch {
  opacity: var(--am-disabled-opacity);
}

/* ── Badges ────────────────────────────────────────────────────────────────
 * Status pills: <span class="am-badge am-badge-success">Active</span> */

.am-badge {
  --am-badge-fg: var(--am-grey-text);
  --am-badge-bg: var(--am-grey-soft);
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  min-height: 1.75rem;
  padding: 0 0.75rem;
  font-size: 0.8125rem;
  font-weight: 600;
  line-height: 1.25rem;
  white-space: nowrap;
  color: var(--am-badge-fg);
  background-color: var(--am-badge-bg);
  border-radius: 9999px;
}

.am-badge-success { --am-badge-fg: var(--am-green-text); --am-badge-bg: var(--am-green-soft); }
.am-badge-info    { --am-badge-fg: var(--am-blue-text);  --am-badge-bg: var(--am-blue-soft); }
.am-badge-danger  { --am-badge-fg: var(--am-red-text);   --am-badge-bg: var(--am-red-soft); }
.am-badge-warning { --am-badge-fg: var(--am-warning-fg); --am-badge-bg: var(--am-warning-soft); }
/* .am-badge-neutral keeps the grey defaults. */

/* ── Tables ────────────────────────────────────────────────────────────────
 * <div class="am-table-wrap"><table class="am-table">…</table></div> */

.am-table-wrap {
  overflow-x: auto;
  background-color: var(--am-surface);
  border: 1px solid var(--am-border);
  border-radius: var(--am-radius-card);
}

.am-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9375rem;
  color: var(--am-text);
}

.am-table thead th {
  padding: 0.875rem 1.5rem;
  font-size: 0.8125rem;
  font-weight: 500;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  white-space: nowrap;
  color: var(--am-text);
  background-color: var(--am-surface-muted);
  border-bottom: 1px solid var(--am-border);
}

/* :where() lets a header cell's own alignment win, e.g. text-center (Roles). */
.am-table :where(thead th) {
  text-align: start;
}

/* :where() lets a cell's own padding win, e.g. py-12 on a loading or empty row. */
.am-table :where(tbody td) {
  padding: 1rem 1.5rem;
  vertical-align: middle;
  border-bottom: 1px solid var(--am-border);
}

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

/* Rows of data only: a loading, empty or error row (a single cell) stays still. */
.am-table tbody tr:hover:not(:has(> td:only-child)) {
  background-color: rgb(var(--am-surface-muted-rgb) / 0.6);
}

/* ── Menus and chips ───────────────────────────────────────────────────────
 * A dropdown item: <a|button class="am-menu-item">icon Label</a>, red for a
 * destructive one (-danger). A chip: a small neutral pill for counts and
 * facts on a card (<span class="am-chip">3 playlists</span>), smaller than
 * .am-badge, which is for statuses. Added in module 7. */

:is(a, button).am-menu-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.5rem 0.75rem;
  font-size: 0.875rem;
  line-height: 1.25rem;
  text-align: start;
  text-decoration: none;
  color: var(--am-text);
  background-color: transparent;
  border: 0;
  border-radius: var(--am-radius-control);
  cursor: pointer;
  transition: background-color 150ms, color 150ms;
}

:is(a, button).am-menu-item:hover {
  background-color: var(--am-surface-muted);
}

:is(a, button).am-menu-item:focus-visible {
  outline: none;
  box-shadow: inset 0 0 0 2px var(--am-focus-outline);
}

:is(a, button).am-menu-item.am-menu-item-danger {
  color: var(--am-red-fg);
}

:is(a, button).am-menu-item.am-menu-item-danger:hover {
  color: var(--am-red-text);
  background-color: var(--am-red-soft);
}

.am-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.125rem 0.5rem;
  font-size: 0.75rem;
  line-height: 1rem;
  color: var(--am-grey-text);
  background-color: var(--am-grey-soft);
  border-radius: 9999px;
}

/* ── Hiding ────────────────────────────────────────────────────────────────
 * Scripts hide controls with Tailwind's .hidden (or the hidden attribute).
 * The element-qualified display rules above are one step more specific than
 * .hidden, so without this a hidden .am-btn would stay on screen. */

:is(.am-btn, .am-icon-btn, .am-field, .am-search, .am-icon-field, .am-switch, .am-badge, .am-actions, .am-menu-item, .am-chip):is(.hidden, [hidden]),
input[type="checkbox"].am-check:is(.hidden, [hidden]) {
  display: none;
}
