/* Owner: Maxemilian Gimmler */
/* ==========================================================================
   Mobile-Pass Android/iOS
   Wird als LETZTES Stylesheet geladen und enthält nur Overrides für
   schmale Viewports und Touch-Geräte. Die bestehenden Dateien bleiben
   unverändert; Reihenfolge statt !important.
   ========================================================================== */

/* --- M1: Grundlagen ------------------------------------------------------ */
:root {
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --touch: 44px;
}

html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

/* clip statt hidden: verhindert seitliches Wackeln, ohne Sticky zu brechen */
html,
body {
    overflow-x: clip;
}

a,
button,
summary,
label,
input,
select,
textarea {
    -webkit-tap-highlight-color: rgba(45, 212, 191, 0.18);
}

/* Doppeltippen soll nicht zoomen, sondern wie ein Klick wirken. */
a,
button,
summary,
input[type="checkbox"],
input[type="radio"],
input[type="range"] {
    touch-action: manipulation;
}

/* iOS zoomt beim Fokus automatisch, wenn ein Feld kleiner als 16 px ist. */
@media (max-width: 900px) {
    input,
    select,
    textarea {
        font-size: 16px;
    }

    .topbar-inner {
        padding-top: calc(12px + var(--safe-top));
        padding-left: calc(var(--space-4) + var(--safe-left));
        padding-right: calc(var(--space-4) + var(--safe-right));
    }

    .container {
        padding-left: calc(var(--space-4) + var(--safe-left));
        padding-right: calc(var(--space-4) + var(--safe-right));
    }

    .page {
        padding-left: calc(var(--space-4) + var(--safe-left));
        padding-right: calc(var(--space-4) + var(--safe-right));
        padding-bottom: calc(80px + var(--safe-bottom));
    }

    .editor-head {
        padding-left: calc(var(--space-4) + var(--safe-left));
        padding-right: calc(var(--space-4) + var(--safe-right));
    }

    .site-footer {
        padding-bottom: calc(var(--space-5) + var(--safe-bottom));
    }

    .to-top {
        bottom: calc(18px + var(--safe-bottom));
    }
}

/* --- M2: Vorschau und Dokumente ------------------------------------------ */
@media (max-width: 1080px) {
    /* Vorschau steht unter dem Formular; die Karte selbst scrollt nicht. */
    .preview-pane {
        position: static;
        top: auto;
    }

    /* Ohne min-width:0 zwingt die 794-px-PDF das Grid auf Dokumentbreite;
       die ganze Seite wird dann breiter als der Viewport ("reingezoomt"). */
    .editor-layout > * {
        min-width: 0;
    }

    #ergebnis,
    #ablage,
    .generated-file,
    .generated-file .doc-frame {
        min-width: 0;
        max-width: 100%;
    }

    .preview-scroll {
        max-width: 100%;
    }

    .doc-thumb {
        max-width: 100%;
    }
}

/* Ab hier skaliert frames.js nur noch nach Breite; die Höhe scrollt.
   Feste --s-Werte würden die Seite rechts abschneiden. */
@media (max-width: 640px) {
    .preview-pane {
        padding: var(--space-3);
    }

    .preview-scroll {
        margin: 0 auto;
    }

    .preview-head {
        gap: 6px;
    }

    .preview-tabs {
        width: 100%;
        margin-left: 0;
        overflow-x: auto;
        scrollbar-width: none;
        -webkit-overflow-scrolling: touch;
    }

    .preview-tabs::-webkit-scrollbar {
        display: none;
    }

    .preview-tabs .mini {
        flex: 0 0 auto;
        min-height: 34px;
    }

    .preview-state {
        font-size: 11px;
    }

    .doc-thumb {
        height: 260px;
        max-width: 100%;
    }

    .doc-thumb iframe {
        width: 794px;
        height: 1123px;
        transform-origin: top left;
    }

    .generated-file {
        padding: 16px 0;
    }
}

/* --- M1: Touch-Ziele ----------------------------------------------------- */
@media (max-width: 980px) {
    /* Ausklapp-Menue: bei kleinen Geraeten scrollbar und mit Rand frei
       von Notch und Home-Indikator. */
    .mainnav {
        max-height: calc(100dvh - 68px - var(--safe-top) - var(--safe-bottom));
        overflow-y: auto;
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
        padding-left: calc(var(--space-4) + var(--safe-left));
        padding-right: calc(var(--space-4) + var(--safe-right));
        padding-bottom: calc(var(--space-4) + var(--safe-bottom));
    }

    /* Der Chat-Knopf wuerde sonst ueber dem offenen Menue liegen. */
    body:has(.mainnav.open) .ai-fab {
        display: none;
    }
}

@media (max-width: 900px) {
    .mini,
    .button,
    button.primary,
    button.secondary,
    .nav-cta,
    .card-cta,
    .page-back {
        min-height: var(--touch);
    }

    .mini {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        padding: 10px 14px;
    }

    .nav-list > li > a,
    .nav-toggle,
    .submenu a {
        min-height: var(--touch);
        display: flex;
        align-items: center;
    }

    .submenu a {
        flex-direction: column;
        align-items: flex-start;
        justify-content: center;
        gap: 2px;
        padding: 10px 12px;
    }

    .burger {
        width: var(--touch);
        height: var(--touch);
    }

    .checkbox {
        min-height: var(--touch);
        align-items: center;
    }

    .checkbox input {
        width: 20px;
        height: 20px;
    }
}

/* --- M3: KI-Chat als Vollbild-Sheet -------------------------------------- */
@media (max-width: 640px) {
    .ai-fab {
        right: calc(12px + var(--safe-right));
        bottom: calc(12px + var(--safe-bottom));
    }

    /* Waehrend der Chat offen ist, liegt er ueber der ganzen Seite. */
    .ai-panel {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        top: auto;
        width: auto;
        max-width: none;
        max-height: none;
        height: 100vh;
        height: 100dvh;
        padding: calc(10px + var(--safe-top)) calc(14px + var(--safe-right))
            calc(10px + var(--safe-bottom)) calc(14px + var(--safe-left));
        border-radius: 0;
        border-left: 0;
        border-right: 0;
        border-bottom: 0;
        gap: 8px;
        overflow: hidden;
        /* Deckend, damit die Seite hinter dem Vollbild nicht durchscheint. */
        background:
            linear-gradient(180deg, rgba(45, 212, 191, 0.06), transparent 22%),
            #080d16;
    }

    .ai-panel-head {
        flex: 0 0 auto;
        padding-bottom: 8px;
        border-bottom: 1px solid rgba(45, 212, 191, 0.16);
    }

    .ai-close {
        width: var(--touch);
        height: var(--touch);
        margin-right: -8px;
        font-size: 28px;
    }

    /* Der Verlauf nimmt den freien Platz und scrollt allein. */
    .ai-log {
        flex: 1 1 auto;
        min-height: 0;
        max-height: none;
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
        padding-right: 2px;
    }

    .ai-bubble,
    .ai-proposal {
        max-width: 100%;
        overflow-wrap: anywhere;
    }

    .ai-proposal-text {
        max-height: none;
    }

    .ai-chips {
        flex: 0 0 auto;
        flex-wrap: nowrap;
        overflow-x: auto;
        padding-bottom: 4px;
        scrollbar-width: none;
        -webkit-overflow-scrolling: touch;
    }

    .ai-chips::-webkit-scrollbar {
        display: none;
    }

    .ai-chip {
        flex: 0 0 auto;
        min-height: 34px;
        padding: 8px 12px;
    }

    .ai-form {
        flex: 0 0 auto;
        align-items: stretch;
        gap: 8px;
    }

    .ai-form textarea {
        min-height: 48px;
        max-height: 96px;
    }

    .ai-form .mini {
        min-width: 84px;
        min-height: 48px;
    }

    .ai-proposal-actions {
        flex-wrap: wrap;
    }

    .ai-proposal-actions .mini {
        flex: 1 1 140px;
    }

    .ai-target-select {
        font-size: 16px;
    }

    .ai-credit {
        flex: 0 0 auto;
        font-size: 10px;
    }

    /* Hintergrund steht still, solange der Chat offen ist. */
    html.ai-open,
    html.ai-open body {
        overflow: hidden;
        touch-action: none;
    }

    html.ai-open .ai-fab {
        display: none;
    }
}

@media (max-height: 430px) and (max-width: 900px) {
    /* Querformat: Kopfzeile und Verlauf kompakt halten. */
    .ai-panel {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        top: 0;
        width: auto;
        max-width: none;
        max-height: none;
        border-radius: 0;
        padding: calc(8px + var(--safe-top)) calc(12px + var(--safe-right))
            calc(8px + var(--safe-bottom)) calc(12px + var(--safe-left));
        gap: 6px;
        overflow: hidden;
    }

    .ai-panel-head {
        padding-bottom: 4px;
    }

    .ai-log {
        flex: 1 1 auto;
        min-height: 0;
        max-height: none;
    }

    .ai-chips,
    .ai-credit {
        display: none;
    }

    .ai-form textarea {
        max-height: 64px;
    }
}

/* --- M4: Editor ---------------------------------------------------------- */
@media (max-width: 900px) {
    .editor-head {
        padding-top: calc(28px + var(--safe-top));
    }

    .editor-head h1 {
        font-size: clamp(24px, 7vw, 34px);
        letter-spacing: 0.5px;
        line-height: 1.2;
    }

    .editor-head .hero-lead {
        font-size: 15px;
    }

    .editor-layout {
        gap: var(--space-4);
    }

    /* Die Schrittleiste bleibt eine Zeile und wischt seitlich. */
    .stepper {
        flex-wrap: nowrap;
        overflow-x: auto;
        overflow-y: hidden;
        margin: 0 calc(-1 * var(--space-4)) var(--space-3);
        padding: 0 var(--space-4) var(--space-2);
        scroll-snap-type: x proximity;
        scrollbar-width: none;
        -webkit-overflow-scrolling: touch;
    }

    .stepper::-webkit-scrollbar {
        display: none;
    }

    .step-btn {
        flex: 0 0 auto;
        min-height: var(--touch);
        padding: 10px 14px;
        font-size: 13px;
        white-space: nowrap;
        scroll-snap-align: start;
    }

    .editor-row {
        grid-template-columns: 1fr;
        padding: var(--space-2);
        gap: var(--space-2);
    }

    /* Fähigkeiten: Name, Stufe und Löschen untereinander statt gequetscht. */
    .editor-row.skill-row {
        grid-template-columns: minmax(0, 1fr) auto;
        grid-template-areas:
            "name remove"
            "range range";
        align-items: center;
    }

    .skill-row > .editor-field:first-child {
        grid-area: name;
    }

    .skill-row > .editor-field:nth-child(2) {
        grid-area: range;
    }

    .skill-row > button {
        grid-area: remove;
        align-self: start;
    }

    .editor-actions,
    .ai-head,
    .ai-head-actions {
        flex-wrap: wrap;
        gap: var(--space-2);
    }

    .editor-actions > button,
    .ai-head-actions > button {
        flex: 1 1 160px;
        min-height: var(--touch);
    }

    /* Foto: Vorschau über den Bedienelementen. */
    .photo-grid {
        grid-template-columns: 1fr;
    }

    .photo-controls input[type="file"] {
        width: 100%;
    }

    .photo-controls .editor-actions {
        margin-top: 0;
    }

    .nav-buttons {
        position: static;
        gap: var(--space-2);
    }

    .nav-buttons button {
        width: 100%;
        min-height: var(--touch);
    }

    .ai-box {
        padding: var(--space-3);
    }

    .ai-head-actions .mini {
        flex: 1 1 180px;
    }
}

/* --- M5: Startseite ------------------------------------------------------ */
@media (max-width: 640px) {
    .hero {
        padding-top: calc(24px + var(--safe-top));
    }

    .hero-title {
        font-size: clamp(28px, 9vw, 40px);
        letter-spacing: 1px;
        line-height: 1.15;
    }

    .hero-lead {
        font-size: 15px;
        line-height: 1.6;
    }

    .hero-actions {
        flex-direction: column;
        align-items: stretch;
        gap: var(--space-2);
    }

    .hero-actions .button,
    .hero-actions .secondary {
        width: 100%;
        min-height: var(--touch);
        justify-content: center;
        text-align: center;
    }

    /* Kennzahlen untereinander statt in einer gequetschten Reihe. */
    .hero-stats {
        display: grid;
        grid-template-columns: 1fr;
        gap: 0;
        margin-top: var(--space-4);
    }

    .hero-stats li {
        padding: var(--space-2) 0;
        border-left: 0;
        border-top: 1px solid var(--border);
        text-align: left;
    }

    .hero-stats li:first-child {
        border-top: 0;
        padding-top: 0;
    }

    .hero-stats strong {
        font-size: 26px;
    }

    .trust-band {
        display: grid;
        grid-template-columns: 1fr;
        gap: var(--space-2);
        margin-top: var(--space-4);
    }

    .cards,
    .cards-4,
    .steps-grid {
        grid-template-columns: 1fr;
        gap: var(--space-3);
    }

    .cta-band {
        padding: var(--space-4);
        border-radius: var(--radius);
        text-align: left;
    }

    .cta-band h2 {
        font-size: 24px;
    }

    .cta-band .button {
        width: 100%;
        min-height: var(--touch);
        justify-content: center;
    }

    .site-footer .footer-inner {
        grid-template-columns: 1fr;
        gap: var(--space-4);
    }

    .site-footer {
        padding-left: calc(var(--space-4) + var(--safe-left));
        padding-right: calc(var(--space-4) + var(--safe-right));
    }

    .footer-bottom {
        flex-direction: column;
        align-items: flex-start;
        gap: var(--space-1);
    }
}

/* --- M6: Unterseiten, Formulare, Konto ----------------------------------- */
@media (max-width: 900px) {
    .page-head {
        padding-top: calc(28px + var(--safe-top));
    }

    .page-head h1 {
        font-size: clamp(24px, 7vw, 34px);
        letter-spacing: 0.5px;
        line-height: 1.2;
    }

    .breadcrumb {
        font-size: 12px;
        overflow-wrap: anywhere;
    }

    .info-card {
        padding: var(--space-4);
        border-radius: var(--radius);
    }

    .info-card h2 {
        font-size: 20px;
    }
}

@media (max-width: 640px) {
    .auth-form {
        padding: var(--space-4);
    }

    .auth-form button,
    .auth-form .button {
        width: 100%;
        min-height: var(--touch);
        justify-content: center;
    }

    /* Upload: Beschriftung und Feld untereinander, Knopf in voller Breite. */
    .upload-form,
    .name-form,
    .account-actions {
        flex-direction: column;
        align-items: stretch;
        gap: var(--space-2);
    }

    .upload-label {
        flex-direction: column;
        align-items: stretch;
        gap: var(--space-1);
    }

    .upload-label input[type="file"] {
        width: 100%;
    }

    .upload-form button,
    .name-form button,
    .account-actions .secondary,
    .account-actions button {
        width: 100%;
        min-height: var(--touch);
        justify-content: center;
    }

    .name-form input[type="text"] {
        min-width: 0;
        width: 100%;
    }

    /* Ablage: Name über Links, Reihenfolge-Auswahl darunter. */
    .ablage-item,
    .ablage-head,
    .ablage-doc {
        flex-direction: column;
        align-items: flex-start;
        gap: var(--space-1);
    }

    .ablage-doc-name {
        min-width: 0;
    }

    .ablage-links,
    .ablage-actions {
        flex-wrap: wrap;
        gap: var(--space-2);
        width: 100%;
    }

    .ablage-links a,
    .ablage-actions a,
    .ablage-actions button {
        min-height: var(--touch);
        display: inline-flex;
        align-items: center;
    }

    .merge-select {
        margin-left: 0;
        width: 100%;
        justify-content: space-between;
    }

    .merge-order {
        min-height: var(--touch);
    }

    .ablage-file {
        width: 100%;
    }

    .page-back {
        display: inline-flex;
        align-items: center;
        min-height: var(--touch);
    }
}

/* --- M7: Touch und Leistung ---------------------------------------------- */
@media (max-width: 900px) {
    /* Der Zurück-nach-oben-Knopf sitzt über dem Chat, damit beide frei
       erreichbar bleiben und sich nicht überdecken. */
    .to-top {
        right: calc(12px + var(--safe-right));
        bottom: calc(76px + var(--safe-bottom));
        width: var(--touch);
        height: var(--touch);
    }
}

/* Auf Geräten ohne echten Zeiger sind Hover-Effekte nur Ballast: sie bleiben
   nach dem Tippen "kleben". Deshalb hier zurücksetzen. */
@media (hover: none) {
    .card:hover,
    .step-card:hover,
    .info-card:hover,
    .ablage-item:hover,
    .ai-fab:hover,
    .to-top:hover {
        transform: none;
        box-shadow: none;
    }

    .card:hover,
    .step-card:hover,
    .info-card:hover {
        border-color: var(--border);
    }

    /* Weniger Weichzeichner pro Bild = weniger Arbeit für die GPU. */
    .panel,
    .card,
    .step-card,
    .info-card,
    .preview-pane,
    .ai-panel,
    .site-footer {
        backdrop-filter: blur(4px);
        -webkit-backdrop-filter: blur(4px);
    }
}

@media (prefers-reduced-motion: reduce) {
    .step-panel {
        animation: none;
    }

    .to-top,
    .ai-panel,
    .mini {
        transition: none;
    }
}

/* Sehr kleine Geräte (z. B. 320 px breit): Innenabstände enger. */
@media (max-width: 380px) {
    .container,
    .page,
    .editor-head,
    .page-head {
        padding-left: calc(12px + var(--safe-left));
        padding-right: calc(12px + var(--safe-right));
    }

    .panel,
    .info-card,
    .step-card {
        padding: var(--space-3);
    }

    /* Schrittleiste bleibt randlos, obwohl der Container hier 12 px nutzt. */
    .stepper {
        margin-left: -12px;
        margin-right: -12px;
        padding-left: 12px;
        padding-right: 12px;
    }
}
