/* Kernkomponenten Teil 1 (D5): Aktionen und Eingabe. Nur Alias-Tokens (--tp-color-*) und Skalen aus tokens.css. */
@layer components {

    /* Symbole */
    .tp-icon { flex: none; vertical-align: middle; }
    /* Symbol eines Termins (DV15b): Farbe folgt dem Text, nie allein als Träger der Information */
    .tp-entryicon { color: inherit; }
    .tp-entryicon-line { display: flex; align-items: center; gap: var(--tp-space-2); }
    .tp-entry-namecell__icon, .tp-tasklist__icon, .tp-today__icon, .tp-day__icon, .tp-grid__icon, .tp-preview__icon { flex: none; }
    /* Auswahl des Symbols im Terminformular: Raster, gewählt = Primärfarbe; Fokus am Radio sichtbar über :has */
    .tp-iconpicker { display: grid; grid-template-columns: repeat(auto-fill, minmax(5.6rem, 1fr)); gap: var(--tp-space-2); }
    .tp-iconpicker__item {
        position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: var(--tp-space-1);
        min-height: var(--tp-touch-min); padding: var(--tp-space-2); text-align: center; cursor: pointer;
        font-size: var(--tp-font-size-sm); line-height: 1.2;
        color: var(--tp-color-text); background: var(--tp-color-surface);
        border: 1px solid var(--tp-color-border-strong); border-radius: var(--tp-radius);
    }
    :where(:root[data-input="mouse"]) .tp-iconpicker__item:hover { background: var(--tp-color-primary-tint); border-color: var(--tp-color-primary); }
    .tp-iconpicker__item.is-on { background: var(--tp-color-primary-tint); border-color: var(--tp-color-primary); box-shadow: inset 0 0 0 1px var(--tp-color-primary); font-weight: 600; }
    .tp-iconpicker__item:has(.tp-iconpicker__input:focus-visible) { box-shadow: var(--tp-focus-ring); }
    .tp-iconpicker__input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; }
    .tp-iconpicker__mark { display: inline-flex; align-items: center; justify-content: center; height: 24px; font-size: 1.4rem; line-height: 1; }
    .tp-preview__icon { margin-right: var(--tp-space-1); vertical-align: -3px; }
    .tp-day__icon { margin-right: var(--tp-space-1); }
    .tp-grid__icon { margin-right: 2px; vertical-align: -2px; }

    /* Knöpfe */
    .tp-btn,
    .tp-iconbtn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: var(--tp-space-2);
        min-height: var(--tp-touch-min);
        padding: 0 var(--tp-space-4);
        border: 1px solid transparent;
        border-radius: var(--tp-radius);
        font: inherit;
        font-weight: 600;
        line-height: 1.2;
        cursor: pointer;
        user-select: none;
        background: var(--tp-color-surface);
        color: var(--tp-color-text);
        transition: transform var(--tp-duration-fast) var(--tp-easing),
                    box-shadow var(--tp-duration-fast) var(--tp-easing),
                    background-color var(--tp-duration-fast) var(--tp-easing),
                    border-color var(--tp-duration-fast) var(--tp-easing);
    }
    .tp-btn--sm { min-height: var(--tp-touch-min); min-width: var(--tp-touch-min); padding: 0 var(--tp-space-3); font-size: var(--tp-font-size-sm); }
    .tp-btn--lg { min-height: calc(var(--tp-touch-min) * 1.25); padding: 0 var(--tp-space-5); font-size: var(--tp-font-size-lg); }

    :where(:root[data-input="mouse"]) .tp-btn:hover:not(:disabled),
    :where(:root[data-input="mouse"]) .tp-iconbtn:hover:not(:disabled) { transform: translateY(-1px); box-shadow: var(--tp-shadow); }
    .tp-btn:active:not(:disabled),
    .tp-iconbtn:active:not(:disabled) { transform: scale(0.97); box-shadow: none; }
    .tp-btn:focus-visible,
    .tp-iconbtn:focus-visible { outline: none; box-shadow: var(--tp-focus-ring); }
    .tp-btn:disabled,
    .tp-iconbtn:disabled { opacity: 0.5; cursor: not-allowed; }

    /* Primär-Knopf: Füllung, Schrift, Rand und Rundung kommen aus den Button-Tokens des Themes (themes.css), Rückfall auf die Primärfarbe. */
    .tp-btn--primary,
    .tp-iconbtn--primary {
        background: var(--tp-button-fill, var(--tp-color-primary));
        color: var(--tp-button-text, var(--tp-color-surface));
        border-color: var(--tp-button-border, transparent);
        border-radius: var(--tp-button-radius, var(--tp-radius));
    }
    :where(:root[data-input="mouse"]) .tp-btn--primary:hover:not(:disabled),
    :where(:root[data-input="mouse"]) .tp-iconbtn--primary:hover:not(:disabled) { background: var(--tp-button-hover-fill, var(--tp-color-primary-hover)); }

    .tp-btn--secondary,
    .tp-iconbtn--secondary { background: var(--tp-color-surface); color: var(--tp-color-text); border-color: var(--tp-color-border-strong); }
    :where(:root[data-input="mouse"]) .tp-btn--secondary:hover:not(:disabled),
    :where(:root[data-input="mouse"]) .tp-iconbtn--secondary:hover:not(:disabled) { background: var(--tp-color-primary-tint); border-color: var(--tp-color-primary); }

    .tp-btn--ghost,
    .tp-iconbtn--ghost { background: transparent; color: var(--tp-color-primary-hover); }
    :where(:root[data-input="mouse"]) .tp-btn--ghost:hover:not(:disabled),
    :where(:root[data-input="mouse"]) .tp-iconbtn--ghost:hover:not(:disabled) { background: var(--tp-color-primary-tint); box-shadow: none; }

    .tp-btn--danger,
    .tp-iconbtn--danger { background: var(--tp-color-error); color: var(--tp-color-surface); }
    :where(:root[data-input="mouse"]) .tp-btn--danger:hover:not(:disabled),
    :where(:root[data-input="mouse"]) .tp-iconbtn--danger:hover:not(:disabled) { filter: brightness(0.9); }

    .tp-iconbtn { padding: 0; width: var(--tp-touch-min); border-radius: var(--tp-radius-pill); }
    .tp-iconbtn.tp-btn--sm { width: var(--tp-touch-min); }
    .tp-iconbtn.tp-btn--lg { width: calc(var(--tp-touch-min) * 1.25); }

    .tp-btn--busy { cursor: progress; }
    /* Kurz nach dem Speichern: Haken und "Gespeichert" in der Erfolgsfarbe (Rückmeldung am Knopf, DV4). */
    .tp-btn--done:disabled, .tp-btn--done { background: var(--tp-color-success); color: var(--tp-color-surface); opacity: 1; }
    .tp-spinner {
        width: 1em; height: 1em;
        border: 2px solid currentColor;
        border-right-color: transparent;
        border-radius: 50%;
        animation: tp-spin 700ms linear infinite;
    }
    @keyframes tp-spin { to { transform: rotate(360deg); } }

    /* Feldrahmen */
    .tp-field { display: flex; flex-direction: column; gap: var(--tp-space-1); margin-bottom: var(--tp-space-3); }
    .tp-field__label { font-size: var(--tp-font-size-sm); font-weight: 600; color: var(--tp-color-text); }
    .tp-field__help { font-size: var(--tp-font-size-sm); color: var(--tp-color-text-muted); }
    .tp-field__error { font-size: var(--tp-font-size-sm); color: var(--tp-color-error); font-weight: 600; }

    /* Eingaben */
    .tp-input {
        width: 100%;
        min-height: var(--tp-touch-min);
        padding: var(--tp-space-2) var(--tp-space-3);
        border: 1px solid var(--tp-color-border-strong);
        border-radius: var(--tp-radius);
        background: var(--tp-color-surface);
        color: var(--tp-color-text);
        font: inherit;
        transition: border-color var(--tp-duration-fast) var(--tp-easing), box-shadow var(--tp-duration-fast) var(--tp-easing);
    }
    /* Touch: mindestens 16 px, sonst zoomt der Browser beim Fokussieren hinein. */
    :where(:root[data-input="touch"]) .tp-input { font-size: max(16px, 1em); }
    .tp-input::placeholder { color: var(--tp-color-text-muted); }
    :where(:root[data-input="mouse"]) .tp-input:hover:not(:disabled) { border-color: var(--tp-color-primary); }
    .tp-input:focus-visible { outline: none; border-color: var(--tp-color-primary); box-shadow: var(--tp-focus-ring); }
    .tp-input:disabled { background: var(--tp-color-background); color: var(--tp-color-text-muted); cursor: not-allowed; }
    .tp-field--error .tp-input,
    .tp-input[aria-invalid="true"] { border-color: var(--tp-color-error); background: var(--tp-color-error-tint); }
    .tp-textarea { resize: vertical; min-height: calc(var(--tp-touch-min) * 2); }
    .tp-counter { align-self: flex-end; font-size: var(--tp-font-size-sm); color: var(--tp-color-text-muted); }
    .tp-counter--full { color: var(--tp-color-attention); font-weight: 600; }

    /* Auswahlliste (TpListSelect): Radiogruppe, höchstens --tp-list-rows Zeilen sichtbar, darüber scrollt sie */
    .tp-list {
        max-height: calc(var(--tp-touch-min) * var(--tp-list-rows, 5) + 2px);
        overflow-y: auto;
        overscroll-behavior: contain;
        border: 1px solid var(--tp-color-border-strong);
        border-radius: var(--tp-radius);
        background: var(--tp-color-surface);
    }
    .tp-list--error { border-color: var(--tp-color-error); background: var(--tp-color-error-tint); }
    .tp-list:focus-within { border-color: var(--tp-color-primary); }
    .tp-list__row {
        position: relative;
        display: flex;
        align-items: center;
        gap: var(--tp-space-3);
        min-height: var(--tp-touch-min);
        padding: var(--tp-space-2) var(--tp-space-3);
        cursor: pointer;
        color: var(--tp-color-text);
    }
    .tp-list__row + .tp-list__row { border-top: 1px solid var(--tp-color-border); }
    .tp-list__row--selected { background: var(--tp-color-primary-tint); font-weight: 600; }
    .tp-list__row--disabled { opacity: 0.5; cursor: not-allowed; }
    .tp-list__input { position: absolute; opacity: 0; width: 100%; height: 100%; inset: 0; margin: 0; cursor: inherit; }
    .tp-list__mark {
        flex: none;
        width: 20px; height: 20px;
        border: 2px solid var(--tp-color-border-strong);
        border-radius: var(--tp-radius-pill);
        background: var(--tp-color-surface);
        transition: border-color var(--tp-duration-fast) var(--tp-easing), box-shadow var(--tp-duration-fast) var(--tp-easing);
    }
    .tp-list__input:checked ~ .tp-list__mark { border-color: var(--tp-color-primary); box-shadow: inset 0 0 0 4px var(--tp-color-primary); }
    .tp-list__input:focus-visible ~ .tp-list__mark { box-shadow: var(--tp-focus-ring); }
    .tp-list__input:checked:focus-visible ~ .tp-list__mark { box-shadow: inset 0 0 0 4px var(--tp-color-primary), var(--tp-focus-ring); }
    :where(:root[data-input="mouse"]) .tp-list__row:hover:not(.tp-list__row--disabled) { background: var(--tp-color-primary-tint); }
    .tp-list__text { min-width: 0; overflow-wrap: anywhere; }

    /* Checkbox und Schalter */
    .tp-check { margin-bottom: var(--tp-space-3); }
    .tp-check__row {
        display: inline-flex;
        align-items: center;
        gap: var(--tp-space-3);
        min-height: var(--tp-touch-min);
        cursor: pointer;
        position: relative;
    }
    .tp-check__input {
        position: absolute;
        opacity: 0;
        width: 100%;
        height: 100%;
        margin: 0;
        cursor: inherit;
    }
    .tp-check__label { color: var(--tp-color-text); }
    .tp-check__input:disabled ~ * { opacity: 0.5; cursor: not-allowed; }
    .tp-check__input:disabled { cursor: not-allowed; }

    .tp-check__box {
        flex: none;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 28px; height: 28px;
        border: 2px solid var(--tp-color-border-strong);
        border-radius: var(--tp-radius-sm);
        background: var(--tp-color-surface);
        color: transparent;
        transition: background-color var(--tp-duration-fast) var(--tp-easing), border-color var(--tp-duration-fast) var(--tp-easing);
    }
    :where(:root[data-input="mouse"]) .tp-check__row:hover .tp-check__input:not(:disabled) ~ .tp-check__box { border-color: var(--tp-color-primary); }
    .tp-check__input:checked ~ .tp-check__box { background: var(--tp-color-primary); border-color: var(--tp-color-primary); color: var(--tp-color-surface); }
    .tp-check__input:focus-visible ~ .tp-check__box,
    .tp-check__input:focus-visible ~ .tp-switch__track { box-shadow: var(--tp-focus-ring); }
    .tp-check.tp-field--error .tp-check__box { border-color: var(--tp-color-error); }

    .tp-switch__track {
        flex: none;
        position: relative;
        width: 52px; height: 30px;
        border-radius: var(--tp-radius-pill);
        background: var(--tp-color-border-strong);
        transition: background-color var(--tp-duration-fast) var(--tp-easing);
    }
    .tp-switch__thumb {
        position: absolute;
        top: 3px; left: 3px;
        width: 24px; height: 24px;
        border-radius: 50%;
        background: var(--tp-color-surface);
        box-shadow: var(--tp-shadow-sm);
        transition: transform var(--tp-duration) var(--tp-easing);
    }
    .tp-check__input:checked ~ .tp-switch__track { background: var(--tp-color-primary); }
    .tp-check__input:checked ~ .tp-switch__track .tp-switch__thumb { transform: translateX(22px); }

    @media (prefers-reduced-motion: reduce) {
        :where(:root[data-input="mouse"]) .tp-btn:hover:not(:disabled),
        :where(:root[data-input="mouse"]) .tp-iconbtn:hover:not(:disabled),
        .tp-btn:active:not(:disabled),
        .tp-iconbtn:active:not(:disabled) { transform: none; }
        .tp-spinner { animation-duration: 2s; }
    }

    /* Flächenebenen (DV1): Hintergrund, Karte, schwebend. Rand, Schatten und (dunkel) hellere Fläche kommen aus den Tokens. */
    .tp-layer-1 { background: var(--tp-layer-1-bg); border: 1px solid var(--tp-layer-1-border); box-shadow: var(--tp-layer-1-shadow); }
    .tp-layer-2 { background: var(--tp-layer-2-bg); border: 1px solid var(--tp-layer-2-border); box-shadow: var(--tp-layer-2-shadow); }
    .tp-layer-3 { background: var(--tp-layer-3-bg); border: 1px solid var(--tp-layer-3-border); box-shadow: var(--tp-layer-3-shadow); }

    /* Kalenderfarb-Rollen (DV1, DV5): Quelle ist --tp-cal (Farbe des Kalenders, per style-Attribut gesetzt) oder die ältere --tp-cal-color;
       daraus entstehen fünf Töne. Bausteine verwenden nur diese Rollen: soft = zarte Fläche, strong = kräftig (Punkt, Streifen, Kreisrand),
       border = Rand, lit = Farbe, im dunklen Schema aufgehellt (--tp-cal-lift), ink = Textfarbe auf der zarten Fläche (Kontrast mindestens 4,5:1,
       vom Test für alle Palettenfarben in allen Schemata geprüft). Die Liste nennt die Bausteine, die eine Kalenderfarbe tragen. */
    :is(.tp-cal, .tp-entry-chip, .tp-caldot, .tp-avatar, .tp-calchip, .tp-day__item, .tp-day__step, .tp-today__entry, .tp-grid__block, .tp-grid__laneitem, .tp-tasklist__item, .tp-calnav__checks .tp-check) {
        --tp-cal-src: var(--tp-cal, var(--tp-cal-color, var(--tp-color-primary)));
        --tp-cal-lit: color-mix(in srgb, var(--tp-cal-src) calc(100% - var(--tp-cal-lift)), var(--tp-color-text));
        --tp-cal-soft: color-mix(in srgb, var(--tp-cal-src) var(--tp-cal-soft-mix), var(--tp-color-surface));
        --tp-cal-strong: color-mix(in srgb, var(--tp-cal-lit) var(--tp-cal-strong-mix), var(--tp-color-surface));
        --tp-cal-border: color-mix(in srgb, var(--tp-cal-lit) var(--tp-cal-border-mix), var(--tp-color-surface));
        --tp-cal-ink: color-mix(in srgb, var(--tp-cal-src) var(--tp-cal-ink-mix), var(--tp-color-text));
    }

    /* Touch-Gesten (DV10, TpSwipeArea, gestures.js): Wischen braucht waagerechte Bewegung ohne Browser-Scrollen; Herunterziehen zeigt eine Anzeige,
       die mit dem Finger (--tp-pull, von gestures.js gesetzt) herunterkommt. Nur bei data-input="touch" gibt der Baustein data-tp-gestures aus. */
    .tp-swipe { position: relative; }
    .tp-swipe[data-tp-gestures~="swipe"], .tp-swipe[data-tp-gestures~="swipe"] .tp-grid__scroller { touch-action: pan-y pinch-zoom; }
    .tp-swipe[data-tp-gestures~="longpress"] { -webkit-touch-callout: none; }
    .tp-pull {
        position: absolute; top: 0; left: 50%; z-index: 5; pointer-events: none;
        display: flex; align-items: center; justify-content: center;
        width: var(--tp-touch-min); height: var(--tp-touch-min); border-radius: var(--tp-radius-pill);
        background: var(--tp-color-surface); color: var(--tp-color-primary); box-shadow: var(--tp-layer-3-shadow);
        opacity: 0; transform: translate(-50%, calc(var(--tp-pull, 0px) - 100%));
    }
    .tp-swipe[data-tp-pull] > .tp-pull { opacity: 1; }
    .tp-swipe[data-tp-pull="pulling"] > .tp-pull .tp-spinner { animation: none; }
    .tp-swipe[data-tp-pull="ready"] > .tp-pull .tp-spinner { animation: none; border-right-color: currentColor; }
    .tp-swipe[data-tp-pull="refreshing"] > .tp-pull { transition: transform var(--tp-duration-fast) var(--tp-easing); }

    /* Wisch-Aktionen an Listenzeilen (DV15d, TpSwipeRow, gestures.js): nur bei data-input="touch" (der Baustein gibt sonst weder die Klasse noch data-tp-row aus).
       Die ganze Zeile (li) folgt dem Finger (--tp-row-x); die Aktionen sind Kinder der Zeile, liegen neben ihr (Start links, End rechts) und werden beim
       Verschieben sichtbar. Der Listenumschlag beschneidet nur waagerecht, damit das Verschieben keine Seitenleiste erzeugt. */
    .tp-swiperow { position: relative; touch-action: pan-y; transform: translateX(var(--tp-row-x, 0px)); }
    .tp-swiperow:not([data-tp-dragging]) { transition: transform var(--tp-motion-change) var(--tp-easing); }
    :where(ul, ol):has(> .tp-swiperow) { overflow-x: clip; }
    .tp-swiperow__action {
        position: absolute; top: 0; bottom: 0; width: 100%; display: flex; align-items: stretch;
        visibility: hidden; opacity: 0; color: var(--tp-color-surface); background: var(--tp-color-primary);
        border-radius: var(--tp-radius);
        transition: visibility 0s linear var(--tp-motion-change), opacity var(--tp-motion-feedback) var(--tp-easing);
    }
    .tp-swiperow__action--start { right: 100%; justify-content: flex-end; }
    .tp-swiperow__action--end { left: 100%; justify-content: flex-start; }
    .tp-swiperow__action--success { background: var(--tp-success); }
    .tp-swiperow__action--danger { background: var(--tp-danger); }
    .tp-swiperow[data-tp-side="start"] > .tp-swiperow__action--start, .tp-swiperow[data-tp-side="end"] > .tp-swiperow__action--end {
        visibility: visible; opacity: 1; transition-delay: 0s;
    }
    .tp-swiperow__btn {
        flex: none; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: var(--tp-space-1);
        width: 88px; min-height: var(--tp-touch-min); padding: var(--tp-space-2); border: 0; background: transparent; color: inherit; font: inherit;
        font-size: var(--tp-font-size-sm); font-weight: 600; cursor: pointer;
    }
    .tp-swiperow[data-tp-armed="start"] > .tp-swiperow__action--start .tp-swiperow__btn,
    .tp-swiperow[data-tp-armed="end"] > .tp-swiperow__action--end .tp-swiperow__btn { font-weight: 800; text-decoration: underline; }
    .tp-swiperow__more { flex: none; margin-left: auto; }
    .tp-swiperow.tp-day__item > .tp-swiperow__more { position: absolute; top: var(--tp-space-1); right: var(--tp-space-1); }
    .tp-swiperow.tp-day__item .tp-day__line { padding-inline-end: var(--tp-touch-min); }
    @media (prefers-reduced-motion: reduce) {
        /* Ohne Federn: die Zeile springt zurück, die Aktion blendet nur ein und aus. */
        .tp-swiperow:not([data-tp-dragging]) { transition: none; }
        .tp-swiperow__action { transition: visibility 0s linear var(--tp-motion-feedback), opacity var(--tp-motion-feedback) linear; }
    }
}
