/* ==========================================================================
   TAW Form — standalone stylesheet
   Stack-agnostic: no Tailwind, no framework dependency.
   All selectors are scoped under .taw-form or .taw-* namespaced classes.
   ========================================================================== */

/* ── Utility ────────────────────────────────────────────────────────────── */

.taw-hidden {
    display: none !important;
}

/* ── Form shell ─────────────────────────────────────────────────────────── */

.taw-form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

/* ── Grid ───────────────────────────────────────────────────────────────── */

.taw-form-grid {
    display: grid;
    grid-template-columns: repeat(12, minmax(0, 1fr));
    gap: 1rem;
}

/* Every cell's width arrives as the --taw-span custom property (1–12), set
   inline by Form::renderField() — never as an inline grid-column, so a
   theme can override `grid-column` with a plain selector, no !important. */
.taw-form-grid > * {
    grid-column: span var(--taw-span, 12) / span var(--taw-span, 12);
}

@media (max-width: 639px) {
    .taw-form-grid > * {
        grid-column: 1 / -1;
    }
}

/* ── Field wrapper ──────────────────────────────────────────────────────── */

.taw-form-field {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

/* ── Labels ─────────────────────────────────────────────────────────────── */

.taw-field-label {
    font-weight: 600;
    color: #44403c;
    font-size: 0.9375rem;
}

.taw-required {
    color: #ef4444;
}

/* ── Inputs ─────────────────────────────────────────────────────────────── */

.taw-input {
    width: 100%;
    padding: 0.5rem 0.625rem;
    border: 1px solid #a8a29e;
    border-radius: 0.375rem;
    background: #fff;
    color: #1c1917;
    font-size: 0.9375rem;
    line-height: 1.5;
    transition: border-color .15s, box-shadow .15s;
    box-sizing: border-box;
    appearance: auto;
}

.taw-input:focus {
    outline: none;
    border-color: #78716c;
    box-shadow: 0 0 0 2px rgba(120, 113, 108, 0.25);
}

.taw-input::placeholder {
    color: #a8a29e;
}

/* ── Radio & checkbox groups ────────────────────────────────────────────── */

.taw-option-group {
    display: flex;
    flex-wrap: wrap;
    gap: 0.375rem 1rem;
}

.taw-option-group--vertical {
    flex-direction: column;
    gap: 0.5rem;
}

.taw-option-label {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    user-select: none;
    font-size: 0.9375rem;
    color: #1c1917;
}

.taw-check {
    cursor: pointer;
    border-radius: 0.25rem;
    border: 1px solid #a8a29e;
    accent-color: #1c1917;
    width: 1rem;
    height: 1rem;
    flex-shrink: 0;
}

/* ── Help popover ───────────────────────────────────────────────────────── */

.taw-help {
    position: relative;
    display: inline-flex;
    margin-left: 0.375rem;
    vertical-align: middle;
}

.taw-help-trigger {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.125rem;
    height: 1.125rem;
    padding: 0;
    border: 1px solid #a8a29e;
    border-radius: 50%;
    background: #f5f5f4;
    color: #57534e;
    font-size: 0.6875rem;
    font-weight: 700;
    line-height: 1;
    cursor: pointer;
    transition: background .15s, border-color .15s;
}

.taw-help-trigger:hover,
.taw-help-trigger:focus-visible {
    background: #e7e5e4;
    border-color: #78716c;
}

/*
 * Anchored (non-modal) popup, always rendered as a <span> — tag-qualified
 * everywhere in this section specifically so none of it can ever apply to
 * the modal variant's <dialog> below (which needs none of this: no
 * absolute positioning, no opacity/visibility/pointer-events toggling —
 * the browser's own [open]/top-layer state is authoritative for that).
 */
span.taw-help-popup {
    position: absolute;
    z-index: 20;
    bottom: calc(100% + 0.5rem);
    left: 50%;
    width: max-content;
    min-width: 220px;
    max-width: 300px;
    max-height: 220px;
    overflow-x: hidden;
    overflow-y: auto;
    word-wrap: break-word;
    overflow-wrap: break-word;
    padding: 0.625rem 0.75rem;
    border-radius: 0.375rem;
    background: #1c1917;
    color: #fafaf9;
    font-size: 0.8125rem;
    font-weight: 400;
    line-height: 1.45;
    text-align: left;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transform: translateX(-50%) translateY(4px);
    transition: opacity .15s, transform .15s, visibility .15s;
}

span.taw-help-popup::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    border: 5px solid transparent;
    border-top-color: #1c1917;
    transform: translateX(-50%);
}

/*
 * Invisible hit-area bridging the gap between the trigger and the popup
 * (the `bottom: calc(100% + 0.5rem)` offset above). Without this, moving
 * the pointer from the trigger toward the popup crosses dead space that
 * belongs to neither element, dropping `.taw-help:hover` and hiding the
 * popup before the pointer ever reaches it — impossible to hover into,
 * let alone scroll. As a ::before of the popup it's still a DOM
 * descendant of .taw-help, so hovering it keeps the parent :hover (and
 * thus the popup) alive across the gap.
 */
span.taw-help-popup::before {
    content: "";
    position: absolute;
    top: 100%;
    left: -1rem;
    right: -1rem;
    height: 0.75rem;
}

/*
 * Default trigger: hover or focus on the trigger reveals the popup.
 * Excluded from .taw-help--click, whose popup opens only on click (see
 * renderScript() in Form.php, which toggles the trigger's aria-expanded)
 * — hover proved hard to use for longer popups, since the pointer has to
 * cross from the trigger into the popup to scroll it, and the popup
 * disappears the moment hover/focus is lost from either element.
 */
.taw-help:not(.taw-help--click):hover span.taw-help-popup,
.taw-help:not(.taw-help--click) .taw-help-trigger:focus-visible + span.taw-help-popup,
.taw-help:not(.taw-help--click) .taw-help-trigger:focus + span.taw-help-popup {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    transform: translateX(-50%) translateY(0);
}

/* Click-triggered anchored popover: shown purely off aria-expanded, set by JS. */
.taw-help--click .taw-help-trigger[aria-expanded="true"] + span.taw-help-popup {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    transform: translateX(-50%) translateY(0);
}

@media (max-width: 639px) {
    span.taw-help-popup {
        left: auto;
        right: 0;
        max-width: 240px;
        transform: translateX(0) translateY(4px);
    }

    span.taw-help-popup::after {
        left: auto;
        right: 0.5rem;
        transform: none;
    }

    .taw-help:not(.taw-help--click):hover span.taw-help-popup,
    .taw-help:not(.taw-help--click) .taw-help-trigger:focus-visible + span.taw-help-popup,
    .taw-help:not(.taw-help--click) .taw-help-trigger:focus + span.taw-help-popup,
    .taw-help--click .taw-help-trigger[aria-expanded="true"] + span.taw-help-popup {
        transform: translateX(0) translateY(0);
    }
}

/*
 * Modal variant ('help_modal' => true): a native <dialog>, not a
 * positioned <span> — see renderHelp()'s doc comment in Form.php for why.
 * In short: a fixed-position "backdrop" element, however high its own
 * z-index, only ever competes for stacking order within whichever
 * ancestor's stacking context it happens to be nested in (this markup can
 * land inside an arbitrary page's arbitrary wrappers — confirmed breaking
 * in practice, trapped behind a fixed header by an unrelated ancestor
 * section wrapper's own position:relative + z-index several levels up).
 * A <dialog>'s top layer renders above the entire document by
 * construction, independent of ancestor stacking contexts entirely — a
 * real fix, not a bigger number.
 *
 * Opened via .showModal() in renderScript(), which is also what applies
 * dialog:modal's own UA-stylesheet default (position: fixed; inset-block:
 * 0; margin: auto — real centering, not something this stylesheet has to
 * do) — deliberately not overridden here. ::backdrop is native too: the
 * browser renders it behind the dialog, covering the full viewport, with
 * no DOM node of its own to size or position.
 */
dialog.taw-help-popup {
    /* dialog:modal's UA stylesheet centers via margin: auto by default,
       but that's an author-vs-UA cascade loser against any CSS reset a
       site happens to load (e.g. Tailwind's preflight zeroes margin on
       every element) — restated explicitly here so centering doesn't
       silently depend on no such reset existing. */
    margin: auto;
    width: min(90vw, 640px); /* wide over tall, deliberately — cuts down how much a given amount of text needs the internal scroll below */
    max-height: min(85vh, 720px);
    overflow-x: hidden;
    overflow-y: auto;
    word-wrap: break-word;
    overflow-wrap: break-word;
    padding: 1.75rem 1.5rem 1.5rem;
    border: none;
    border-radius: 0.5rem;
    background: #1c1917;
    color: #fafaf9;
    font-size: 0.9375rem;
    font-weight: 400;
    line-height: 1.6;
    text-align: left;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}

dialog.taw-help-popup::backdrop {
    background: rgba(20, 18, 15, 0.6);
}

.taw-help-close {
    position: absolute;
    top: 0.625rem;
    right: 0.625rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.75rem;
    height: 1.75rem;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.08);
    color: inherit;
    font-size: 1.25rem;
    line-height: 1;
    cursor: pointer;
    transition: background .15s;
}

.taw-help-close:hover,
.taw-help-close:focus-visible {
    background: rgba(255, 255, 255, 0.18);
}

/* ── Field error ────────────────────────────────────────────────────────── */

.taw-field-error {
    font-size: 0.8125rem;
    color: #dc2626;
    line-height: 1.4;
}

/* ── Feedback banners ───────────────────────────────────────────────────── */

.taw-general-error {
    padding: 0.75rem 1rem;
    color: #991b1b;
    background: #fee2e2;
    border: 1px solid #fecaca;
    border-radius: 0.5rem;
    font-size: 0.9375rem;
}

.taw-success {
    padding: 0.75rem 1rem;
    color: #166534;
    background: #dcfce7;
    border: 1px solid #bbf7d0;
    border-radius: 0.5rem;
    font-size: 0.9375rem;
}

/* ── Structural field types ─────────────────────────────────────────────── */

.taw-field-heading {
    padding: 0.625rem 0.875rem;
    background: #1c1917;
    color: #fff;
    border-radius: 0.375rem;
}

.taw-heading-label {
    font-size: 0.9375rem;
    font-weight: 700;
    margin: 0;
}

.taw-heading-subtitle {
    font-size: 0.8rem;
    opacity: 0.65;
    font-style: italic;
    margin: 0.2rem 0 0;
}

.taw-divider {
    border: none;
    border-top: 1px solid #e7e5e4;
    margin: 0;
}

/* ── Form actions ───────────────────────────────────────────────────────── */

.taw-form-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.75rem;
    margin-top: 1.5rem;
}

/* ── Buttons ────────────────────────────────────────────────────────────── */

.taw-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 1.25rem;
    border-radius: 0.375rem;
    border: 1px solid transparent;
    font-size: 0.9375rem;
    font-weight: 500;
    cursor: pointer;
    transition: background .15s, color .15s, border-color .15s;
    line-height: 1.5;
}

.taw-btn-primary {
    background: #1c1917;
    color: #d6d3d1;
    border-color: #1c1917;
}

.taw-btn-primary:hover:not(:disabled) {
    background: #44403c;
    border-color: #44403c;
}

.taw-btn-primary:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.taw-btn-secondary {
    background: transparent;
    color: #44403c;
    border-color: #a8a29e;
}

.taw-btn-secondary:hover {
    background: #f5f5f4;
}

/* ── Spinner ────────────────────────────────────────────────────────────── */

@keyframes taw-spin {
    to { transform: rotate(360deg); }
}

.taw-btn-spinner {
    width: 1rem;
    height: 1rem;
    animation: taw-spin 1s linear infinite;
    flex-shrink: 0;
}

.taw-spinner-track { opacity: 0.25; }
.taw-spinner-fill  { opacity: 0.75; }

/* ── Step progress bar (mobile < 640px) ─────────────────────────────────── */

.taw-step-progress {
    display: none;
    margin-bottom: 1.5rem;
}

.taw-step-progress-meta {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 0.5rem;
    margin-bottom: 0.5rem;
}

.taw-step-progress-count {
    font-size: 0.75rem;
    color: #78716c;
    font-weight: 500;
    white-space: nowrap;
    flex-shrink: 0;
}

.taw-step-progress-title {
    font-size: 0.875rem;
    font-weight: 600;
    color: #1c1917;
    text-align: right;
}

.taw-step-progress-track {
    height: 4px;
    background: #e7e5e4;
    border-radius: 2px;
    overflow: hidden;
}

.taw-step-progress-fill {
    height: 100%;
    background: #1c1917;
    border-radius: 2px;
    width: 0%;
    transition: width .3s ease;
}

@media (max-width: 639px) {
    .taw-step-indicator { display: none !important; }
    .taw-step-progress  { display: block; }
}

/* ── Step indicator (desktop ≥ 640px) ───────────────────────────────────── */

.taw-step-indicator {
    display: flex;
    align-items: flex-start;
    margin-bottom: 2rem;
}

.taw-step-dot {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.4rem;
    flex-shrink: 0;
}

.taw-step-dot-number {
    width: 2rem;
    height: 2rem;
    border-radius: 50%;
    background: #e7e5e4;
    color: #78716c;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.875rem;
    font-weight: 600;
    transition: background .2s, color .2s;
}

.taw-step-dot.is-active .taw-step-dot-number {
    background: #1c1917;
    color: #fff;
}

.taw-step-dot.is-done .taw-step-dot-number {
    background: #57534e;
    color: #fff;
}

.taw-step-dot-label {
    font-size: 0.7rem;
    color: #78716c;
    text-align: center;
    max-width: 80px;
    line-height: 1.3;
}

.taw-step-dot.is-active .taw-step-dot-label {
    color: #1c1917;
    font-weight: 600;
}

.taw-step-connector {
    flex: 1;
    height: 2px;
    background: #e7e5e4;
    margin-top: 1rem;
    min-width: 1rem;
    transition: background .2s;
}

.taw-step-connector.is-done {
    background: #57534e;
}
