/* Components (SPEC D13): Card, ListRow, Button, IconButton, Chip/MemberChip, SegmentedControl,
   Field, Switch, Select, Sheet/Dialog, Toast, EmptyState, Skeleton, Banner, Avatar, Stepper,
   SettingsList, Menu. Hover styles only inside the fine-pointer media query at the end. */
@layer components {
  /* ---------------------------------------------------------------- icons */
  .icon {
    width: 20px;
    height: 20px;
    flex: none;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
  }

  /* ---------------------------------------------------------------- card */
  .card {
    background: var(--color-surface);
    border: 1px solid var(--color-line);
    border-radius: var(--radius-lg);
    padding: var(--space-4);
    box-shadow: 0 4px 22px var(--color-shadow);
    min-width: 0;
  }
  .card-header {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    margin-bottom: var(--space-3);
    min-width: 0;
  }
  .card-header h2 { font-size: var(--text-md); margin-right: auto; }
  /* Links and small buttons in a card header keep their 48 px target but take less room in the row
     (the target reaches into the card's padding). */
  .card-header > .more-link, .card-header > .button.small { margin-block: calc((var(--tap-min) - 1.5em) / -2); }
  .eyebrow {
    font-size: max(12px, var(--text-xs));
    font-weight: var(--weight-heavy);
    letter-spacing: var(--tracking-eyebrow);
    text-transform: uppercase;
    color: var(--color-text-muted);
  }
  .muted { color: var(--color-text-muted); }
  .small { font-size: var(--text-sm); }

  /* ---------------------------------------------------------------- buttons */
  .button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    min-height: var(--tap-min);
    padding: 0 var(--space-5);
    border-radius: var(--radius-pill);
    border: 1px solid var(--color-field-border);
    background: var(--color-surface);
    color: var(--color-text);
    font-size: var(--text-md);
    font-weight: var(--weight-bold);
    text-decoration: none;
    white-space: nowrap;
    transition: background var(--duration-fast) var(--ease), transform var(--duration-fast) var(--ease);
  }
  .button.primary { background: var(--color-accent); border-color: var(--color-accent); color: var(--color-on-accent); }
  .button.ghost { background: none; border-color: transparent; color: var(--color-accent); }
  .button.danger { border-color: var(--color-danger); color: var(--color-danger); }
  /* Visually smaller through padding and font size only: every target stays >= 48 x 48 px (SPEC 4.8 rule 2). */
  .button.small { min-height: var(--tap-min); padding: 0 var(--space-4); font-size: var(--text-sm); }
  .button.block { width: 100%; }
  .button:active { transform: scale(0.98); }
  .button:disabled, .button[aria-disabled='true'] { opacity: 0.55; cursor: not-allowed; }
  .button[aria-busy='true'] { cursor: progress; }
  .button[aria-busy='true']::after {
    content: '';
    width: 16px;
    height: 16px;
    border-radius: 50%;
    border: 2px solid currentColor;
    border-right-color: transparent;
    animation: luma-spin 0.8s linear infinite;
  }
  @keyframes luma-spin { to { transform: rotate(360deg); } }

  .icon-button {
    display: inline-grid;
    place-items: center;
    width: var(--tap-min);
    height: var(--tap-min);
    flex: none;
    border-radius: 50%;
    border: 1px solid transparent;
    background: var(--color-surface-2);
    color: var(--color-text);
  }
  .icon-button[aria-pressed='true'] { background: var(--color-accent); color: var(--color-on-accent); }

  .actions { display: flex; flex-wrap: wrap; gap: var(--space-2); }
  .actions.end { justify-content: flex-end; }

  /* ---------------------------------------------------------------- chips */
  .chip {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    min-height: 32px;
    padding: 0 var(--space-3);
    border-radius: var(--radius-pill);
    border: 1px solid var(--color-line);
    background: var(--color-surface-2);
    color: var(--color-text);
    font-size: var(--text-sm);
    font-weight: var(--weight-bold);
    white-space: nowrap;
  }
  button.chip { min-height: var(--tap-min); padding: 0 var(--space-4); }
  button.chip[aria-pressed='true'] { background: var(--color-accent); border-color: var(--color-accent); color: var(--color-on-accent); }
  .chip-row { display: flex; flex-wrap: wrap; gap: var(--space-2); }

  /* Member chips always show initials or a name (colour is never the only signal). */
  .member {
    --member-bg: var(--color-surface-2);
    --member-ink: var(--color-text);
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    min-height: 28px;
    padding: 0 var(--space-2);
    border-radius: var(--radius-pill);
    background: var(--member-bg);
    color: var(--member-ink);
    font-size: var(--text-xs);
    font-weight: var(--weight-heavy);
    white-space: nowrap;
  }
  .member-dot {
    display: inline-grid;
    place-items: center;
    min-width: 28px;
    height: 28px;
    padding: 0 4px;
    border-radius: 50%;
    background: var(--member-bg);
    color: var(--member-ink);
    font-size: var(--text-xs);
    font-weight: var(--weight-heavy);
  }
  [data-color='1'] { --member-bg: var(--member-1-bg); --member-ink: var(--member-1-ink); }
  [data-color='2'] { --member-bg: var(--member-2-bg); --member-ink: var(--member-2-ink); }
  [data-color='3'] { --member-bg: var(--member-3-bg); --member-ink: var(--member-3-ink); }
  [data-color='4'] { --member-bg: var(--member-4-bg); --member-ink: var(--member-4-ink); }
  [data-color='5'] { --member-bg: var(--member-5-bg); --member-ink: var(--member-5-ink); }
  [data-color='6'] { --member-bg: var(--member-6-bg); --member-ink: var(--member-6-ink); }
  [data-color='7'] { --member-bg: var(--member-7-bg); --member-ink: var(--member-7-ink); }
  [data-color='8'] { --member-bg: var(--member-8-bg); --member-ink: var(--member-8-ink); }

  /* ---------------------------------------------------------------- segmented control (radiogroup) */
  .segmented {
    display: inline-flex;
    flex-wrap: wrap;
    gap: 2px;
    padding: 3px;
    border-radius: var(--radius-pill);
    background: var(--color-surface-2);
    border: 1px solid var(--color-line);
  }
  .segmented button {
    min-height: var(--tap-min);
    padding: 0 var(--space-4);
    border: 0;
    border-radius: var(--radius-pill);
    background: none;
    color: var(--color-text);
    font-weight: var(--weight-bold);
    font-size: var(--text-sm);
  }
  /* The selected option: accent fill (>= 3:1 against the track, on-accent text >= 4.5:1). */
  .segmented button[aria-checked='true'] { background: var(--color-accent); color: var(--color-on-accent); box-shadow: 0 1px 4px var(--color-shadow); }

  /* ---------------------------------------------------------------- fields */
  .form { display: grid; gap: var(--space-4); }
  .field { display: grid; gap: var(--space-1); min-width: 0; align-content: start; }
  .field > label, .field-label { font-weight: var(--weight-bold); font-size: var(--text-sm); }
  .field-help { font-size: var(--text-sm); color: var(--color-text-muted); }
  .field-error { font-size: var(--text-sm); color: var(--color-danger); font-weight: var(--weight-bold); }
  .form-status:focus { outline: none; }
  .input, .select, .textarea {
    width: 100%;
    min-height: var(--tap-min);
    padding: 0 var(--space-3);
    border: 1px solid var(--color-field-border);
    border-radius: var(--radius-sm);
    background: var(--color-surface);
    color: var(--color-text);
    font-size: var(--text-md);
  }
  .textarea { padding: var(--space-3); min-height: 96px; resize: vertical; }
  .input[aria-invalid='true'], .textarea[aria-invalid='true'] { border-color: var(--color-danger); border-width: 2px; }
  .field-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: var(--space-3); }
  fieldset.field { border: 0; padding: 0; margin: 0; }
  fieldset.field > legend { font-weight: var(--weight-bold); font-size: var(--text-sm); padding: 0; margin-bottom: var(--space-1); }

  .check {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-height: var(--tap-min);
    font-weight: var(--weight-regular);
  }
  .check input { width: 24px; height: 24px; flex: none; margin: 0; accent-color: var(--color-accent); }

  /* Switch: a checkbox with role="switch". */
  .switch {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-4);
    min-height: var(--tap-min);
    cursor: pointer;
  }
  /* The whole row (label, help line and switch) is the target, not only the 52 x 32 px switch
     (SPEC 4.8 rule 2): the label's box is stretched over the row. */
  .switch-text label { cursor: pointer; }
  .switch-text label::after { content: ''; position: absolute; inset: 0; }
  .switch input {
    appearance: none;
    flex: none;
    width: 52px;
    height: 32px;
    margin: 0;
    border-radius: var(--radius-pill);
    background: var(--color-surface-2);
    border: 2px solid var(--color-field-border);
    position: relative;
    z-index: 1;
    transition: background var(--duration-fast) var(--ease);
  }
  .switch input::before {
    content: '';
    position: absolute;
    top: 3px;
    left: 3px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--color-field-border);
    transition: transform var(--duration-fast) var(--ease);
  }
  .switch input:checked { background: var(--color-accent); border-color: var(--color-accent); }
  .switch input:checked::before { transform: translateX(20px); background: var(--color-on-accent); }
  .switch-text { display: grid; gap: 2px; }

  /* Colour swatches (radio inputs). */
  .swatches { display: flex; flex-wrap: wrap; gap: var(--space-2); }
  .swatch { position: relative; }
  .swatch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
  .swatch span {
    display: grid;
    place-items: center;
    width: var(--tap-min);
    height: var(--tap-min);
    border-radius: 50%;
    background: var(--member-bg);
    color: var(--member-ink);
    font-weight: var(--weight-heavy);
    border: 2px solid transparent;
  }
  .swatch input:checked + span { border-color: var(--color-text); }
  .swatch input:focus-visible + span { outline: var(--focus-ring) solid var(--color-focus); outline-offset: 2px; }

  /* ---------------------------------------------------------------- lists */
  .list { display: grid; }
  .list-row {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-height: var(--tap-min);
    padding: var(--space-1) 0;
    border-bottom: 1px solid var(--color-line);
    min-width: 0;
  }
  .list-row:last-child { border-bottom: 0; }
  .list-row .row-main { flex: 1 1 auto; min-width: 0; display: grid; }
  .list-row .row-title { overflow-wrap: anywhere; }
  .list-row .row-meta { font-size: var(--text-sm); color: var(--color-text-muted); }
  .list-row.done .row-title { text-decoration: line-through; color: var(--color-text-muted); }
  .more-link {
    display: inline-flex;
    align-items: center;
    min-height: var(--tap-min);
    min-width: var(--tap-min);
    padding-inline: var(--space-2);
    font-size: var(--text-sm);
    font-weight: var(--weight-bold);
    white-space: nowrap;
  }

  /* ---------------------------------------------------------------- dialog / sheet */
  .sheet {
    width: min(560px, calc(100vw - 24px));
    max-height: calc(100dvh - 24px);
    padding: 0;
    border: 1px solid var(--color-line);
    border-radius: var(--radius-lg);
    background: var(--color-surface);
    color: var(--color-text);
    box-shadow: 0 24px 80px var(--color-shadow);
    overflow: auto;
    overscroll-behavior: contain;
  }
  .sheet::backdrop { background: var(--color-scrim); }
  .sheet-body { display: grid; gap: var(--space-4); padding: var(--space-5); }
  .sheet-header { display: flex; align-items: flex-start; gap: var(--space-3); }
  .sheet-header h2 { margin-right: auto; font-size: var(--text-xl); }
  @media (max-width: 599px) {
    .sheet { margin: auto 0 0; width: 100vw; max-width: 100vw; border-radius: var(--radius-lg) var(--radius-lg) 0 0; }
  }

  /* ---------------------------------------------------------------- menu (avatar) */
  .menu {
    position: fixed;
    z-index: 40;
    top: calc(var(--header-height) + 4px);
    right: var(--space-2);
    margin: 0;
    padding: var(--space-2);
    min-width: 240px;
    max-width: calc(100vw - 16px);
    /* Large text on a short landscape phone: the menu scrolls instead of running off the screen. */
    max-height: calc(100dvh - var(--header-height) - var(--space-4));
    overflow-y: auto;
    overscroll-behavior: contain;
    border: 1px solid var(--color-line);
    border-radius: var(--radius-md);
    background: var(--color-surface);
    color: var(--color-text);
    box-shadow: 0 12px 40px var(--color-shadow);
  }
  .menu-head { padding: var(--space-2) var(--space-3) var(--space-3); border-bottom: 1px solid var(--color-line); margin-bottom: var(--space-1); }
  .menu-head strong { display: block; }
  .menu-item {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    width: 100%;
    min-height: var(--tap-min);
    padding: 0 var(--space-3);
    border: 0;
    border-radius: var(--radius-sm);
    background: none;
    color: var(--color-text);
    font-weight: var(--weight-bold);
    text-align: left;
    text-decoration: none;
  }

  /* ---------------------------------------------------------------- avatar */
  .avatar {
    display: inline-grid;
    place-items: center;
    width: var(--tap-min);
    height: var(--tap-min);
    flex: none;
    border-radius: 50%;
    border: 2px solid var(--color-line);
    background: var(--member-bg, var(--color-accent-soft));
    color: var(--member-ink, var(--color-text));
    font-weight: var(--weight-heavy);
    font-size: var(--text-sm);
  }
  .avatar-button {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    min-height: var(--tap-min);
    padding: 0 var(--space-1);
    border: 0;
    border-radius: var(--radius-pill);
    background: none;
    color: var(--color-text);
    font-weight: var(--weight-bold);
    max-width: 260px;
  }
  .avatar-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: var(--text-sm); }

  /* ---------------------------------------------------------------- toast, banner */
  .toast-region {
    position: fixed;
    z-index: 60;
    left: 50%;
    bottom: calc(var(--bar-height) + var(--space-4));
    transform: translateX(-50%);
    display: grid;
    gap: var(--space-2);
    width: min(520px, calc(100vw - 24px));
    pointer-events: none;
  }
  .toast {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-md);
    background: var(--color-text);
    color: var(--color-surface);
    box-shadow: 0 10px 30px var(--color-shadow);
    pointer-events: auto;
    font-weight: var(--weight-bold);
  }
  .toast .toast-text { flex: 1; }
  .toast .button { background: var(--color-surface); color: var(--color-text); border-color: var(--color-surface); }
  .toast .toast-close { background: none; color: inherit; }
  .toast.error { background: var(--color-danger); color: var(--color-surface); }
  /* The toast is dark on light (and light on dark): its focus ring uses the surface colour, which has
     the text contrast against the toast background (tests/js/contrast.test.cjs, "toast focus"). */
  .toast :focus-visible { outline-color: var(--color-surface); }
  /* Wall and desktop: top right below the header, where a toast never covers a page's main actions
     (the onboarding's "Weiter", a sheet's buttons). */
  @media (min-width: 1024px) {
    .toast-region {
      top: calc(var(--header-height) + var(--space-3));
      right: var(--space-6);
      bottom: auto;
      left: auto;
      transform: none;
      width: min(420px, calc(100vw - 48px));
    }
    /* While a sheet is open the region lives in it (top layer): top left, clear of the sheet's close
       button in its top right corner. */
    .sheet .toast-region { right: auto; left: var(--space-6); }
  }
  /* A sheet's failed quick-add row: the text and its own "Erneut versuchen". */
  .quick-added li.failed { display: inline-flex; align-items: center; gap: var(--space-2); color: var(--color-danger); font-weight: var(--weight-bold); }

  /* Printing a sheet (a one-time reset code): only the sheet's content. */
  @media print {
    .app, .skip-link, .toast-region, .sheet-header button, .sheet .actions { display: none; }
    .sheet::backdrop { background: none; }
    .sheet { position: static; box-shadow: none; border: 0; max-height: none; overflow: visible; }
  }

  .banner {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-md);
    border: 1px solid var(--color-field-border);
    background: var(--color-surface);
  }
  .banner.warning { background: var(--color-warning-soft); border-color: var(--color-warning); }
  .banner p { flex: 1 1 240px; }
  .offline-banner {
    position: sticky;
    top: var(--header-height);
    z-index: 15;
    margin: 0 0 var(--space-3);
  }

  /* ---------------------------------------------------------------- empty state, skeleton */
  .empty {
    display: grid;
    justify-items: start;
    align-content: start;
    gap: var(--space-2);
    padding: var(--space-4);
    border: 1px dashed var(--color-field-border);
    border-radius: var(--radius-md);
    color: var(--color-text-muted);
  }
  .empty strong { color: var(--color-text); }
  .skeleton {
    border-radius: var(--radius-sm);
    min-height: 20px;
    background: linear-gradient(90deg, var(--color-surface-2), var(--color-line), var(--color-surface-2));
    background-size: 200% 100%;
    animation: luma-shimmer 1.4s ease-in-out infinite;
  }
  @keyframes luma-shimmer { from { background-position: 100% 0; } to { background-position: -100% 0; } }

  /* Reduced motion (SPEC 4.8 rule 7): the base layer's reset cannot reach these (a later layer wins). */
  @media (prefers-reduced-motion: reduce) {
    .skeleton { animation: none; background-position: 0 0; }
    .button[aria-busy='true']::after { animation: none; border-right-color: currentColor; border-style: dotted; }
  }

  /* ---------------------------------------------------------------- stepper (progress dots) */
  .stepper { display: flex; flex-wrap: wrap; gap: 6px; }
  .stepper span { width: 10px; height: 10px; border-radius: 50%; background: var(--color-line); }
  .stepper span.on { background: var(--color-accent); }
  .stepper span.current { outline: 2px solid var(--color-accent); outline-offset: 2px; }

  /* ---------------------------------------------------------------- settings list */
  .settings-list { display: grid; border: 1px solid var(--color-line); border-radius: var(--radius-md); background: var(--color-surface); }
  .settings-list > * { padding: var(--space-3) var(--space-4); border-bottom: 1px solid var(--color-line); }
  .settings-list > *:last-child { border-bottom: 0; }

  /* ---------------------------------------------------------------- skip link */
  .skip-link {
    position: absolute;
    display: inline-flex;
    align-items: center;
    min-height: var(--tap-min);
    z-index: 100;
    left: var(--space-2);
    top: var(--space-2);
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-sm);
    background: var(--color-accent);
    color: var(--color-on-accent);
    font-weight: var(--weight-bold);
    transform: translateY(-200%);
  }
  .skip-link:focus { transform: none; }

  /* A file chooser that looks like a button and names the chosen file. */
  .file-field { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-3); }
  .file-button { cursor: pointer; }
  .file-input:focus-visible + .file-button { outline: var(--focus-ring) solid var(--color-focus); outline-offset: 2px; }
  .file-name { overflow-wrap: anywhere; }

  .bullets { list-style: disc; padding-left: 1.4em; display: grid; gap: var(--space-1); }

  /* ---------------------------------------------------------------- idle slideshow (wall, ui/idle.js) */
  .idle-show {
    position: fixed;
    inset: 0;
    z-index: 200;
    display: block;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: black;
    color: white;
    cursor: pointer;
  }
  .idle-photo { width: 100%; height: 100%; object-fit: cover; }
  .idle-show.fit-contain .idle-photo { object-fit: contain; }
  .idle-info {
    position: absolute;
    left: var(--space-6);
    bottom: var(--space-6);
    display: flex;
    align-items: baseline;
    gap: var(--space-6);
    padding: var(--space-3) var(--space-6);
    border-radius: var(--radius-md);
    background: var(--color-scrim);
    font-variant-numeric: tabular-nums;
  }
  .idle-time { font-size: var(--text-display); font-weight: var(--weight-heavy); line-height: 1; }
  .idle-weather { display: inline-flex; align-items: center; gap: var(--space-2); font-size: var(--text-xl); }

  @media (hover: hover) and (pointer: fine) {
    .button:hover:not(:disabled) { filter: brightness(1.06); }
    .button.ghost:hover, .menu-item:hover, .nav-item:hover:not([aria-current='page']) { background: var(--color-surface-2); }
    .icon-button:hover { background: var(--color-line); }
    .chip:hover { border-color: var(--color-field-border); }
  }
}
