/**
 * Suunta.ai Console - Forms Component
 * ====================================
 * Input, select, toggle, radio, checkbox styles
 * Theme: Northern Lights v273
 */

/* ==========================================
   FORM GROUP
   ========================================== */

.form-group {
    margin-bottom: 20px;
}

.form-group:last-child {
    margin-bottom: 0;
}

.form-row {
    display: flex;
    gap: 16px;
}

.form-row .form-group {
    flex: 1;
}

/* ==========================================
   LABELS & HINTS
   ========================================== */

.form-label {
    display: block;
    font-size: var(--font-size-code);
    font-weight: 500;
    color: var(--color-text-secondary);
    margin-bottom: 8px;
}

.form-label--required::after {
    content: '*';
    color: var(--color-error);
    margin-left: 4px;
}

.form-hint {
    font-size: var(--font-size-xs);
    color: var(--color-text-muted);
    margin-top: 6px;
}

.form-error {
    font-size: var(--font-size-xs);
    color: var(--color-error);
    margin-top: 6px;
    display: flex;
    align-items: center;
    gap: 4px;
}

/* hidden attribute must override display: flex */
.form-error[hidden] {
    display: none;
}

.form-error i {
    font-size: var(--font-size-2xs);
}

/* ==========================================
   TEXT INPUT
   ========================================== */

.form-input {
    width: 100%;
    max-width: 400px;
    padding: 10px 14px;
    /* Changed: rgba(255,255,255,0.05) -> var(--overlay-white-5) for light theme support */
    background: var(--overlay-white-5);
    border: 1px solid var(--glass-border);
    border-radius: 8px;
    color: var(--color-text-primary);
    font-size: var(--font-size-sm);
    font-family: var(--font-family);
    transition: all 0.2s ease;
}

.form-input:focus {
    outline: none;
    border-color: var(--glass-border);
    background: var(--overlay-white-8);
    box-shadow: none;
}

.form-input::placeholder {
    color: var(--color-text-muted);
}

.form-input:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.form-input--error {
    border-color: var(--color-error);
}

.form-input--success {
    border-color: var(--color-success);
}

/* Full width variant */
.form-input--full {
    max-width: none;
}

/* Small variant */
.form-input--sm {
    padding: 8px 12px;
    font-size: var(--font-size-code);
}

/* ==========================================
   TEXTAREA
   ========================================== */

.form-textarea {
    width: 100%;
    max-width: 400px;
    min-height: 100px;
    padding: 12px 14px;
    /* Changed: rgba(255,255,255,0.05) -> var(--overlay-white-5) for light theme support */
    background: var(--overlay-white-5);
    border: 1px solid var(--glass-border);
    border-radius: 8px;
    color: var(--color-text-primary);
    font-size: var(--font-size-sm);
    font-family: var(--font-family);
    resize: none;
    transition: all 0.2s ease;
}

.form-textarea:focus {
    outline: none;
    border-color: var(--glass-border);
    background: var(--overlay-white-8);
    box-shadow: none;
}

.form-textarea::placeholder {
    color: var(--color-text-muted);
}

/* Text entry fields should not draw focus borders/rings. The caret and local
   field fill communicate editing state; buttons and other controls keep their
   focus-visible treatment.

   OPT-OUT: `data-focus-ring`.

   The reasoning above assumes a FILLED field: the fill and border change on
   focus, so the ring would be a third signal. A bare field (an inline value
   cell drawn as a single underline, no background) has no fill to change, so
   the same rule leaves it with a measured focus contrast of 1.05:1 against
   the page in the light theme, i.e. no visible focus at all. Those fields
   carry `data-focus-ring` and get the standard green ring below.

   The exception lives in the rule it excepts, not in the page CSS: this
   selector is specificity 0,8,1 with !important, which no reasonable
   page-level selector can outrank. A page that tries loses silently, and the
   only symptom is an invisible focus ring. Found 24.8.2026 on the execution
   surface's inline reading field. */
input:not([type="button"]):not([type="submit"]):not([type="reset"]):not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"]):not([type="color"]):not([data-focus-ring]):focus,
input:not([type="button"]):not([type="submit"]):not([type="reset"]):not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"]):not([type="color"]):not([data-focus-ring]):focus-visible,
textarea:not([data-focus-ring]):focus,
textarea:not([data-focus-ring]):focus-visible,
[contenteditable="true"]:not([data-focus-ring]):focus,
[contenteditable="true"]:not([data-focus-ring]):focus-visible {
    outline: none !important;
    box-shadow: none !important;
}

input:not([type="button"]):not([type="submit"]):not([type="reset"]):not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"]):not([type="color"]):not([data-focus-ring]):focus:not([aria-invalid="true"]):not(.form-input--error):not(.field-error):not(.is-invalid),
input:not([type="button"]):not([type="submit"]):not([type="reset"]):not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"]):not([type="color"]):not([data-focus-ring]):focus-visible:not([aria-invalid="true"]):not(.form-input--error):not(.field-error):not(.is-invalid),
textarea:focus:not([aria-invalid="true"]):not(.form-input--error):not(.field-error):not(.is-invalid),
textarea:focus-visible:not([aria-invalid="true"]):not(.form-input--error):not(.field-error):not(.is-invalid),
[contenteditable="true"]:focus,
[contenteditable="true"]:focus-visible {
    border-color: var(--glass-border) !important;
}


/* Kohdistusrengas täyttämättömille kentille (ks. opt-out yllä).
   Sama muoto kuin muuallakin: 2 px vihreä, 2 px etäisyydellä (STYLEGUIDE §14). */
input[data-focus-ring]:focus-visible,
textarea[data-focus-ring]:focus-visible,
[contenteditable="true"][data-focus-ring]:focus-visible {
    outline: 2px solid var(--green);
    outline-offset: 2px;
}

/* ==========================================
   SELECT
   ========================================== */

.form-select {
    width: 100%;
    max-width: 400px;
    padding: 10px 14px;
    padding-right: 40px;
    /* Changed: rgba(255,255,255,0.05) -> var(--overlay-white-5) for light theme support */
    background: var(--overlay-white-5);
    border: 1px solid var(--glass-border);
    border-radius: 8px;
    color: var(--color-text-primary);
    font-size: var(--font-size-sm);
    font-family: var(--font-family);
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%239ca3af' viewBox='0 0 16 16'%3E%3Cpath d='M8 11L3 6h10l-5 5z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 14px center;
    transition: all 0.2s ease;
}

.form-select:focus {
    outline: none;
    border-color: var(--color-accent);
    background-color: var(--overlay-white-8);
    box-shadow: 0 0 0 3px var(--emerald-alpha-10);
}

.form-select option {
    background: var(--color-bg-secondary);
    color: var(--color-text-primary);
}

.form-select:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* ==========================================
   TOGGLE SWITCH
   ========================================== */

.toggle-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 0;
    border-bottom: 1px solid var(--glass-border);
}

.toggle-row:last-child {
    border-bottom: none;
}

.toggle-info {
    flex: 1;
    padding-right: 16px;
}

.toggle-label {
    font-size: var(--font-size-sm);
    font-weight: 500;
    color: var(--color-text-primary);
    margin-bottom: 2px;
}

.toggle-description {
    font-size: var(--font-size-xs);
    color: var(--color-text-muted);
}

.toggle-switch {
    position: relative;
    width: 52px;
    height: 28px;
    min-width: 52px;
    min-height: 28px;
    flex-shrink: 0;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    /* Changed: rgba(255,255,255,0.1) -> var(--overlay-white-10) for light theme support */
    background: var(--overlay-white-10);
    border-radius: 24px;
    transition: 0.3s;
}

.toggle-slider::before {
    position: absolute;
    content: "";
    height: 22px;
    width: 22px;
    left: 3px;
    bottom: 3px;
    background: var(--white);
    border-radius: 50%;
    transition: 0.3s;
}

.toggle-switch input:checked + .toggle-slider {
    background: var(--color-accent);
}

.toggle-switch input:checked + .toggle-slider::before {
    transform: translateX(24px);
}

.toggle-switch input:disabled + .toggle-slider {
    opacity: 0.5;
    cursor: not-allowed;
}

/* ==========================================
   RADIO BUTTONS
   ========================================== */

.radio-group {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
}

.radio-group--vertical {
    flex-direction: column;
}

.radio-option {
    position: relative;
}

.radio-option input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}

.radio-option label {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 16px;
    /* Changed: rgba(255,255,255,0.05) -> var(--overlay-white-5) for light theme support */
    background: var(--overlay-white-5);
    border: 1px solid var(--glass-border);
    border-radius: 8px;
    color: var(--color-text-secondary);
    font-size: var(--font-size-sm);
    cursor: pointer;
    transition: all 0.2s ease;
}

.radio-option label i {
    font-size: 1rem;
}

.radio-option input:checked + label {
    background: var(--emerald-alpha-10);
    border-color: var(--color-accent);
    color: var(--color-accent);
}

.radio-option input:disabled + label {
    opacity: 0.5;
    cursor: not-allowed;
}

/* ==========================================
   CHECKBOX
   ========================================== */

.checkbox-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.checkbox-option {
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
    position: relative;
}

/* Visually hidden but keyboard-focusable (display:none would remove
   the input from the tab order — WCAG keyboard access) */
.checkbox-option input {
    position: absolute;
    opacity: 0;
    width: 1px;
    height: 1px;
    margin: 0;
    pointer-events: none;
}

.checkbox-box {
    width: 18px;
    height: 18px;
    border: 1px solid var(--glass-border);
    border-radius: 4px;
    /* Changed: rgba(255,255,255,0.05) -> var(--overlay-white-5) for light theme support */
    background: var(--overlay-white-5);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    flex-shrink: 0;
}

.checkbox-box i {
    font-size: var(--font-size-2xs);
    color: transparent;
    transition: color 0.2s ease;
}

.checkbox-option input:checked + .checkbox-box {
    background: var(--color-accent);
    border-color: var(--color-accent);
}

.checkbox-option input:checked + .checkbox-box i {
    color: var(--brand-blue-700);
}

.checkbox-label {
    font-size: var(--font-size-sm);
    color: var(--color-text-secondary);
}

.checkbox-option:hover .checkbox-label {
    color: var(--color-text-primary);
}

/* ==========================================
   CONTROL FOCUS — keyboard ring on the visible
   proxy element (inputs are visually hidden)
   ========================================== */

.checkbox-option input:focus-visible + .checkbox-box,
.radio-option input:focus-visible + label,
.toggle-switch input:focus-visible + .toggle-slider {
    outline: 2px solid var(--emerald-400);
    outline-offset: 2px;
}

/* Light mode: -400 fails contrast on white (law #8) */
[data-theme="light"] .checkbox-option input:focus-visible + .checkbox-box,
[data-theme="light"] .radio-option input:focus-visible + label,
[data-theme="light"] .toggle-switch input:focus-visible + .toggle-slider {
    outline-color: var(--emerald-600);
}

/* ==========================================
   AVATAR UPLOAD
   ========================================== */

.avatar-upload {
    display: flex;
    align-items: center;
    gap: 20px;
    margin-bottom: 24px;
}

.avatar-preview {
    width: 80px;
    height: 80px;
    border-radius: 16px;
    /* Changed: #3b82f6, #1d4ed8 -> var(--blue-500), var(--blue-600) for consistent theming */
    background: linear-gradient(135deg, var(--blue-500), var(--blue-600));
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    font-weight: 600;
    /* Changed: white -> var(--white) for consistency */
    color: var(--white);
    flex-shrink: 0;
}

.avatar-actions {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.avatar-actions .btn {
    padding: 8px 14px;
    font-size: var(--font-size-code);
}

/* ==========================================
   USAGE BAR
   ========================================== */

.usage-bar {
    margin-bottom: 20px;
}

.usage-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
}

.usage-label {
    font-size: var(--font-size-code);
    font-weight: 500;
    color: var(--color-text-secondary);
}

.usage-value {
    font-size: var(--font-size-code);
    color: var(--color-text-muted);
}

.usage-track {
    height: 8px;
    /* Changed: rgba(255,255,255,0.1) -> var(--overlay-white-10) for light theme support */
    background: var(--overlay-white-10);
    border-radius: 4px;
    overflow: hidden;
}

.usage-fill {
    height: 100%;
    background: var(--color-accent);
    border-radius: 4px;
    transition: width 0.3s ease;
}

.usage-fill--warning {
    background: var(--color-warning);
}

.usage-fill--danger {
    background: var(--color-error);
}

/* ==========================================
   SEARCH INPUT (inline)
   ========================================== */

.search-input-wrapper {
    position: relative;
    max-width: 300px;
}

.search-input-wrapper .form-input {
    padding-left: 36px;
}

.search-input-icon {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--color-text-muted);
    font-size: var(--font-size-sm);
    pointer-events: none;
}

/* ==========================================
   INPUT WITH PREFIX (e.g., https://)
   ========================================== */

.input-with-prefix {
    display: flex;
    align-items: stretch;
    max-width: 400px;
}

.input-prefix {
    display: flex;
    align-items: center;
    padding: 0 12px;
    background: var(--overlay-white-8);
    border: 1px solid var(--glass-border);
    border-right: none;
    border-radius: 8px 0 0 8px;
    color: var(--color-text-muted);
    font-size: var(--font-size-sm);
    font-family: var(--font-family);
    white-space: nowrap;
    user-select: none;
}

.input-with-prefix .form-input {
    border-radius: 0 8px 8px 0;
    max-width: none;
    flex: 1;
}

.input-with-prefix .form-input:focus {
    /* Also highlight prefix on focus */
    position: relative;
    z-index: 1;
}

.input-with-prefix:focus-within .input-prefix {
    border-color: var(--color-accent);
    background: var(--overlay-white-10);
}

/* Full width variant */
.input-with-prefix--full {
    max-width: none;
}

/* ==========================================
   INPUT WITH BUTTON
   ========================================== */

.input-group {
    display: flex;
    gap: 0;
}

.input-group .form-input {
    border-radius: 8px 0 0 8px;
    max-width: none;
}

.input-group .btn {
    border-radius: 0 var(--radius-md) var(--radius-md) 0;
}

/* ==========================================
   MOBILE RESPONSIVE
   ========================================== */

@media (max-width: 768px) {
    /* Full width inputs on mobile */
    .form-input,
    .form-select,
    .form-textarea {
        max-width: none;
        width: 100%;
        padding: var(--space-3) var(--space-4);
        font-size: 16px; /* Prevents iOS zoom on focus */
    }

    /* Form rows stack on mobile */
    .form-row {
        flex-direction: column;
        gap: var(--space-4);
    }

    /* Toggle rows with proper touch targets */
    .toggle-row {
        padding: var(--space-4) 0;
        min-height: var(--touch-target-min);
    }

    .toggle-label {
        font-size: var(--font-size-base);
    }

    .toggle-description {
        font-size: var(--font-size-sm);
    }

    /* Toggle switch remains same size but has padding for touch */
    .toggle-switch {
        padding: var(--space-2);
        margin: calc(var(--space-2) * -1);
    }

    /* Radio options full width */
    .radio-group {
        flex-direction: column;
        gap: var(--space-2);
    }

    .radio-option label {
        width: 100%;
        padding: var(--space-4);
        min-height: var(--touch-target-min);
    }

    /* Checkbox touch friendly */
    .checkbox-option {
        min-height: var(--touch-target-min);
        padding: var(--space-2) 0;
    }

    .checkbox-box {
        width: 22px;
        height: 22px;
    }

    /* Avatar upload stacks on mobile */
    .avatar-upload {
        flex-direction: column;
        align-items: flex-start;
        gap: var(--space-4);
    }

    .avatar-actions {
        flex-direction: row;
        width: 100%;
        gap: var(--space-3);
    }

    .avatar-actions .btn {
        flex: 1;
        justify-content: center;
    }

    /* Input group stacks on very small screens */
    .input-group {
        flex-direction: column;
        gap: var(--space-2);
    }

    .input-group .form-input {
        border-radius: var(--radius-md);
    }

    .input-group .btn {
        border-radius: var(--radius-md);
        width: 100%;
    }

    /* Search input full width */
    .search-input-wrapper {
        max-width: none;
        width: 100%;
    }
}

/* Small mobile (320px support) */
@media (max-width: 380px) {
    .form-label {
        font-size: var(--font-size-sm);
    }

    .form-hint,
    .form-error {
        font-size: var(--font-size-xs);
    }
}

/* ── Light mode — input/select/textarea visible on white surfaces ── */
[data-theme="light"] .form-input,
[data-theme="light"] .form-textarea,
[data-theme="light"] .form-select {
    background: var(--white);
    border-color: var(--color-border);
}

[data-theme="light"] .form-input:focus,
[data-theme="light"] .form-textarea:focus,
[data-theme="light"] .form-select:focus {
    background: var(--white);
    border-color: var(--emerald-600);
    box-shadow: 0 0 0 3px var(--emerald-alpha-10);
}

/* =============================================================================
   iOS:n zoom fokusoidessa — yksi sääntö, ei kenttä kerrallaan
   =============================================================================
   Safari zoomaa sivun kun kosketuslaitteella fokusoidaan syötekenttä jonka
   kirjasin on alle 16 px. Zoom ei palaudu itsestään: käyttäjä jää
   suurennettuun näkymään ja joutuu nipistämään takaisin. Se on yksi
   häiritsevimmistä mobiiliverkon kokemuksista, ja se syntyy yhdestä
   pikselistä.

   Mitattu 29.8.2026 (390 × 844, kosketus): yhdeksän kenttää neljällä
   pinnalla oli 14,5 px — Asetusten seitsemän, Työtilan kirjoitin ja Kodin
   kysymyskenttä. `mobile.css`:n oma 16 px -sääntö oli olemassa mutta se
   koski vain `.form-*`-luokkia eikä pärjännyt sivukohtaiselle
   spesifisyydelle.

   Siksi sääntö on ELEMENTTITASOLLA eikä luokissa: jokainen tekstinsyöttö
   on katettu riippumatta siitä mikä luokka sillä on ja kuka sen tyylittää.
   `max(16px, 1em)` ei kutista mitään — se vain estää alarajan alituksen ja
   säilyttää isommat koot sellaisenaan.

   Osoitinakselilla eikä leveydellä: sama zoom tapahtuu iPadilla, jossa
   leveysraja ei olisi lauennut lainkaan.

   Valintaruudut ja radiot rajattu ulos: ne eivät ota tekstiä vastaan eikä
   Safari zoomaa niihin, mutta niiden koko on visuaalinen ja kasvaisi. */
@media (pointer: coarse) {
    input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]),
    textarea,
    select,
    [contenteditable="true"] {
        /* `!important` on tässä oikea työkalu ja se on perusteltava.
           Elementtitason sääntö (0,0,1) häviää jokaiselle luokalle (0,1,0),
           ja kentillä ON luokat: `.form-select` ja `.compositor__input`
           jäivät 14,5 px:ään vielä sen jälkeen kun sääntö oli olemassa.
           Vaihtoehto olisi luetella kaikki luokat, ja se on juuri se muoto
           joka epäonnistui jo kerran (`mobile.css`:n oma 16 px koski vain
           `.form-*`-luokkia).

           Tämä ei ole tyylittelyä vaan ALUSTAKÄYTTÄYTYMISEN suoja, samaa
           luokkaa kuin `prefers-reduced-motion`: se estää selainta tekemästä
           jotain jota käyttäjä ei pyytänyt eikä voi perua.

           Todennettu ettei mikään kenttä KUTISTU: kaikki kentät mitattiin
           ennen ja jälkeen kahdella leveydellä, ja jokainen arvo joko
           kasvoi tai pysyi samana. */
        font-size: max(16px, 1em) !important;
    }
}
