/* =========================================================
   OVERLAYS / WINDOWS – gemensam grund
========================================================= */

:root {
    --overlay-safe-top: env(safe-area-inset-top, 0px);
    --overlay-safe-right: env(safe-area-inset-right, 0px);
    --overlay-safe-bottom: env(safe-area-inset-bottom, 0px);
    --overlay-safe-left: env(safe-area-inset-left, 0px);
}

.overlay-portal {
    position: relative;
    isolation: isolate;
}

.overlay {
    position: fixed;
    inset: 0;
    z-index: 60;
    display: none; /* styrs av [hidden] */
}

/* hidden-attribut styr synlighet */
.overlay[hidden] {
    display: none;
}

/* När overlay är synlig: centrera fönstret */
.overlay:not([hidden]) {
    display: flex;
    justify-content: center;
    align-items: flex-start; /* fönstret lite ned från toppen */
    padding:
        calc(40px + var(--overlay-safe-top))
        calc(24px + var(--overlay-safe-right))
        calc(24px + var(--overlay-safe-bottom))
        calc(24px + var(--overlay-safe-left)); /* luft runt fönstret + safe area */
    pointer-events: none; /* endast kortet tar emot klick */
}

/* Bakgrund (just nu transparent, kan bli dim senare) */
.overlay-backdrop {
    position: absolute;
    inset: 0;
    background: transparent;
    pointer-events: none;
}

/* Wrapper runt själva kortet (window-card) */
.overlay-content {
    position: relative;
    max-width: none;
    max-height: calc(100vh - 40px - var(--overlay-safe-top) - var(--overlay-safe-bottom));
    pointer-events: none; /* klick går vidare till window-card */
}

/* =========================================================
   WINDOW CARD – FIXERAD STORLEK
========================================================= */

.window-card {
    position: relative;
    background: var(--color-bg-panel);
    border-radius: var(--radius-lg);
    border: 1px solid var(--color-border);
    box-shadow: var(--shadow-window);
    padding-top: 32px;
    padding-right: calc(32px + var(--overlay-safe-right));
    padding-bottom: calc(24px + var(--overlay-safe-bottom));
    padding-left: calc(32px + var(--overlay-safe-left));
    pointer-events: auto; /* kortet + innehåll är klickbart */

    /* Storlek (generellt för alla fönster) */
    width: 620px;
    height: 660px;

    max-width: 95vw; /* spräng inte små skärmar */
    max-height: 90vh;

    display: flex;
    flex-direction: column;
}

.window-drag-handle {
    display: none;
}

/* Grundtypografi för fönsterinnehåll */
.window-title {
    margin: 0 0 12px;
    font-size: 1.5rem;
    letter-spacing: -0.01em;
}

.window-body {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 16px;
    overflow-y: auto;
    padding-right: 4px;
    color: var(--color-text-main);
    overscroll-behavior: contain;
    scrollbar-gutter: stable;
    scroll-padding-bottom: calc(80px + var(--overlay-safe-bottom));
}

.window-body p {
    margin: 0;
    line-height: 1.55;
}

.window-card .field-subtitle {
    margin: 0;
    color: var(--color-text-muted);
}

/* Gemensamma listor och sektioner */
.window-sections,
.consent-sections {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.window-section,
.consent-section {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 6px 0 14px;
    border-bottom: 1px solid var(--color-border);
}

.window-section:last-child,
.consent-section:last-child {
    border-bottom: none;
    padding-bottom: 0;
}

.window-card .bullet-list {
    margin: 0;
    padding-left: 18px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

/* Dropdowns: gemensam pil och spacing i fönster */
.window-card select {
    appearance: none;
    background-color: var(--color-bg-panel);
    background-image: url("data:image/svg+xml;utf8,<svg fill='%2364748b' height='20' viewBox='0 0 20 20' width='20' xmlns='http://www.w3.org/2000/svg'><path d='M5.516 7.548L10 12.032l4.484-4.484L16 8.064l-6 6-6-6z'/></svg>");
    background-repeat: no-repeat;
    background-position: right 10px center;
    background-size: 16px;
    padding-right: 36px;
}

/* Fönsterstorlekar: generellt + specifika överlägg */
.overlay-context .window-card {
    max-width: 520px;
}

.overlay-login .window-card,
.overlay-signup .window-card {
    width: 520px;
    height: auto;
    max-height: 80vh;
    padding: 28px 28px 24px;
}

.login-footer {
    justify-content: space-between;
}

.signup-footer {
    justify-content: flex-end;
}

.login-switch {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    color: var(--color-text-muted);
}

.login-switch-link {
    color: var(--color-brand-hover);
    font-weight: 600;
    text-decoration: underline;
}

.overlay-settings .window-card {
    width: 720px;
    max-width: 95vw;
}

.overlay-download .window-card {
    width: 560px;
    max-width: 92vw;
    height: auto;
    max-height: 82vh;
}

.overlay-history .window-card {
    width: 820px;
    max-width: 98vw;
}

/* Header (ej flexibel) */
.window-header {
    flex-shrink: 0;
    margin-bottom: 20px;
    padding-left: calc(var(--overlay-safe-left) + 0px);
    padding-right: calc(var(--overlay-safe-right) + 0px);
}

/* Footer (ej flexibel) */
.window-footer {
    flex-shrink: 0;
    margin-top: 24px;
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    align-items: center;
    padding-bottom: var(--overlay-safe-bottom);
    padding-left: calc(var(--overlay-safe-left) + 0px);
    padding-right: calc(var(--overlay-safe-right) + 0px);
}

.window-footer .link-button {
    padding: 12px 18px;
    border-radius: 999px; /* gör pillform */
    font-weight: 700;
    font-size: 0.9rem;
    color: var(--color-brand-hover);
    box-shadow: 0 0 0 1px var(--color-brand-hover);
    background: transparent;
    transition:
        background 0.15s ease,
        color 0.15s ease,
        box-shadow 0.15s ease;
    min-height: 48px;
    touch-action: manipulation;
}

.window-footer .link-button:hover:not(:disabled) {
    background: rgba(30, 64, 175, 0.06);
    box-shadow: 0 0 0 1px var(--color-brand-hover);
    text-decoration: none;
}

/* =========================================================
   HEADER-RAD + CLOSE-BUTTON
========================================================= */

.window-header-row {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
}

.window-header-row--right {
    justify-content: flex-end;
}

.window-close-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    border: none;
    background: transparent;
    padding: 14px;
    cursor: pointer;
    border-radius: 999px;
    min-height: 52px;
    min-width: 52px;
    transition: background-color 0.15s ease, color 0.15s ease;
    touch-action: manipulation;
}

.window-close-icon {
    width: 24px;
    height: 24px;
    display: block;
    color: var(--color-text-muted);
    font-size: 24px;
    line-height: 1;
}

.window-close-icon--mobile {
    display: none;
}

.window-close-btn:hover .window-close-icon {
    color: var(--color-text-main);
}

.window-close-btn:focus-visible .window-close-icon,
.window-close-btn:active .window-close-icon {
    color: var(--color-text-main);
}

.window-close-btn:active {
    background-color: rgba(0, 0, 0, 0.06);
}

.window-close-btn:focus-visible {
    outline: none;
    box-shadow: 0 0 0 2px var(--color-border-strong);
}

/* =========================================================
   MOBILE VARIANT – BOTTOM SHEET / FULL-HEIGHT (<=900px)
========================================================= */

@media (max-width: 900px) {
    #liveSummaryContainer {
        position: relative;
    }

    #liveSummaryContainer[data-window-active] {
        overflow: hidden;
        background: #f7f9fc;
    }

    #liveSummaryContainer[data-window-active] #liveSummaryContent,
    #liveSummaryContainer[data-window-active] #liveSummaryStatus {
        display: none;
    }

    .overlay-portal {
        position: absolute;
        inset: 0;
        pointer-events: none;
        z-index: 70;
    }

    .overlay-portal[data-active-window] {
        pointer-events: auto;
    }

    .overlay-portal .overlay {
        position: absolute;
        inset: 0;
    }

    .overlay:not([hidden])[data-window-mode="mobile"] {
        align-items: stretch;
        justify-content: stretch;
        padding: 0;
        background: transparent;
    }

    .overlay[data-window-mode="mobile"] .overlay-backdrop {
        display: none;
    }

    .overlay[data-window-mode="mobile"] .overlay-content {
        width: 100%;
        max-width: none;
        max-height: none;
        display: flex;
        justify-content: center;
        align-items: stretch;
    }

    .overlay[data-window-mode="mobile"] .window-card {
        width: 100%;
        max-width: 760px;
        min-height: 100%;
        max-height: none;
        height: 100%;
        border-radius: 18px 18px 0 0;
        padding: calc(18px + var(--overlay-safe-top) * 0.2)
            calc(16px + var(--overlay-safe-right))
            calc(18px + var(--overlay-safe-bottom))
            calc(16px + var(--overlay-safe-left));
        box-shadow: none;
        border: 1px solid var(--color-border);
        overflow: hidden;
    }

    .window-drag-handle {
        display: flex;
        justify-content: center;
        padding: 10px 0 6px;
        pointer-events: none;
    }

    .window-drag-handle::before {
        content: "";
        width: 56px;
        height: 6px;
        border-radius: 999px;
        background: rgba(0, 0, 0, 0.2);
        box-shadow: 0 1px 0 rgba(255, 255, 255, 0.4);
    }

    .window-header {
        margin-bottom: 12px;
    }

    .window-header-row {
        flex-direction: column;
        align-items: stretch;
        gap: 12px;
    }

    .window-title {
        margin: 0;
        font-size: 1.25rem;
    }

    .window-close-btn {
        background: rgba(0, 0, 0, 0.04);
        border: 1px solid var(--color-border);
        border-radius: 14px;
        padding: 12px 14px;
        color: var(--color-text-main);
        align-self: flex-end;
    }

    .window-close-icon--desktop {
        display: none;
    }

    .window-close-icon--mobile {
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    .window-body {
        gap: 12px;
        padding-right: 0;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        background: #ffffff;
        border-radius: 12px;
        padding: 10px 4px 12px 4px;
    }

    .window-footer {
        margin-top: 16px;
        flex-wrap: wrap;
        gap: 10px;
        justify-content: space-between;
    }

    .window-footer .link-button {
        min-height: 48px;
        padding: 12px 16px;
    }

    .window-step {
        min-height: 48px;
        padding: 12px 14px;
        width: 100%;
        justify-content: center;
    }

    .window-steps {
        width: 100%;
        flex-direction: column;
        align-items: stretch;
    }
}

/* =========================================================
   STEG-PILLER (1–2–3) – GENERELL STIL
========================================================= */

.window-steps {
    display: flex;
    gap: 16px;
    flex-wrap: wrap;
    margin-top: 4px;
    margin-bottom: 16px;
}

/* <button type="button" class="window-step" data-step="1"> */
.window-step {
    position: relative;
    display: inline-flex;
    align-items: center;
    gap: 6px;

    padding: 12px 16px;
    border-radius: 999px;

    font-size: 0.95rem;
    font-weight: 500;
    color: #6b7280; /* neutral grå */

    background: transparent;
    border: none;
    cursor: pointer;
    user-select: none;

    pointer-events: auto;
    -webkit-tap-highlight-color: transparent; /* iOS: snyggare touch */
    min-height: 48px;
    min-width: 48px;
    touch-action: manipulation;
}

/* Nummerdelen kan ges lite tyngd */
.window-step-number {
    font-weight: 600;
}

/* Aktivt steg – neutral badge */
.window-step.is-active {
    background: #edf2f7;
    color: #111827;
}

/* Hover/active (desktop) */
.window-step:hover,
.window-step:focus-visible {
    background: #e5e7eb;
}

.window-step:active {
    background: #e2e4e8;
}

.window-step:active {
    transform: translateY(0.5px);
}

/* Tillgänglig fokusmarkering */
.window-step:focus-visible {
    outline: 2px solid #9ca3af;
    outline-offset: 2px;
}

.window-jump-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 14px;
    min-height: 48px;
    border-radius: 12px;
    border: 1px solid var(--color-border);
    background: var(--color-bg-subtle);
    color: var(--color-text-main);
    cursor: pointer;
    font-weight: 600;
    touch-action: manipulation;
    transition: background-color 0.15s ease, border-color 0.15s ease;
}

.window-jump-link:hover {
    background: rgba(0, 0, 0, 0.04);
}

.window-jump-link:focus-visible {
    outline: 2px solid var(--color-border-strong);
    outline-offset: 2px;
    background: rgba(0, 0, 0, 0.04);
}

.window-scroll-anchor {
    width: 100%;
    height: 1px;
}

.window-inline-actions {
    display: flex;
    justify-content: flex-start;
}

@media (max-width: 720px) {
    .window-inline-actions {
        width: 100%;
    }

    .window-jump-link {
        width: 100%;
        justify-content: center;
    }
}

/* =========================================================
   HISTORY OVERLAY
========================================================= */

.history-body {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.history-filter {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    border-radius: var(--radius-md);
    border: 1px solid var(--color-border);
    background: var(--color-bg-subtle);
    flex-wrap: wrap;
}

.history-filter-label {
    font-weight: 600;
    color: var(--color-text-main);
}

.history-select {
    min-width: 200px;
    min-height: 48px;
}

.history-list {
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    overflow: hidden;
    background: var(--color-bg-panel);
}

.history-list-header,
.history-list-row {
    display: grid;
    grid-template-columns: 1.1fr 1.6fr 1.4fr auto;
    gap: 12px;
    align-items: center;
    padding: 14px 16px;
}

.history-list-header {
    background: var(--color-bg-subtle);
    font-weight: 600;
    color: var(--color-text-muted);
    border-bottom: 1px solid var(--color-border);
}

.history-list-row + .history-list-row {
    border-top: 1px solid var(--color-border);
}

.history-field {
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-width: 0;
}

.history-datetime,
.history-title {
    font-weight: 600;
    color: var(--color-text-main);
}

.history-documents {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.history-doc-list {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
}

.history-doc-link {
    font-weight: 600;
}

.history-actions-cell {
    display: flex;
    justify-content: flex-end;
}

.history-actions-col {
    text-align: right;
}

.history-delete {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 12px;
    border-radius: var(--radius-sm);
}

.history-empty {
    padding: 18px;
    text-align: center;
    color: var(--color-text-muted);
}

@media (max-width: 720px) {
    .history-filter {
        flex-direction: column;
        align-items: stretch;
    }

    .history-select {
        width: 100%;
    }

    .history-list {
        border: none;
        background: none;
    }

    .history-list-header {
        display: none;
    }

    .history-list-row {
        grid-template-columns: 1fr;
        gap: 10px;
        padding: 16px;
        border: 1px solid var(--color-border);
        border-radius: var(--radius-md);
        background: var(--color-bg-panel);
    }

    .history-list-row + .history-list-row {
        border-top: none;
    }

    .history-list-row .history-field {
        display: grid;
        grid-template-columns: auto 1fr;
        column-gap: 12px;
        row-gap: 4px;
    }

    .history-list-row .history-field::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--color-text-muted);
    }

    .history-documents {
        gap: 6px;
    }

    .history-doc-list {
        justify-content: flex-start;
    }

    .history-actions-cell {
        justify-content: flex-start;
    }
}

/* Reservyta om steg behöver döljas */
.window-steps--placeholder {
    visibility: hidden;
    height: auto;
    margin-top: 4px;
    margin-bottom: 16px;
}

/* =========================================================
   DOWNLOAD WINDOW – små, scoped justeringar
========================================================= */

.overlay-download .download-header-left {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.overlay-download .download-body {
    gap: 14px;
}

.overlay-download .download-order,
.overlay-download .download-files,
.overlay-download .download-cloud {
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    background: var(--color-bg-subtle);
    padding: 14px 16px;
}

.overlay-download .download-section-header {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.overlay-download .download-section-title {
    font-weight: 700;
    color: var(--color-text-main);
}

.overlay-download .download-section-text {
    margin: 0;
    color: var(--color-text-muted);
    line-height: 1.45;
}

.overlay-download .download-files {
    background: linear-gradient(180deg, #f8fafc 0%, #f4f6f9 100%);
    border-color: #e2e8f0;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.7);
}

.download-file-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 10px;
    margin-top: 10px;
}

.download-file-card {
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: 10px;
    border: 1px solid #d7dce4;
    border-radius: var(--radius-md);
    background: #ffffff;
    padding: 10px 12px;
    text-decoration: none;
    color: inherit;
    transition: border-color 0.15s ease, box-shadow 0.15s ease, transform 0.1s ease;
    box-shadow:
        0 1px 1px rgba(0, 0, 0, 0.03),
        inset 0 1px 0 rgba(255, 255, 255, 0.7);
}

.download-file-card:hover,
.download-file-card:focus-visible {
    border-color: #c4cee0;
    box-shadow:
        0 2px 6px rgba(0, 0, 0, 0.06),
        inset 0 1px 0 rgba(255, 255, 255, 0.8);
    transform: translateY(-1px);
}

.download-file-card:active {
    border-color: #b5c2d8;
    background: #f7f9fc;
    transform: translateY(0);
}

.download-file-card:focus-visible {
    outline: 2px solid var(--color-brand-hover);
    outline-offset: 2px;
}

.download-file-card.is-disabled {
    opacity: 0.7;
    cursor: not-allowed;
    border-style: dashed;
    box-shadow: none;
}

.download-file-card.is-disabled:hover {
    transform: none;
}

.download-file-icon {
    width: 42px;
    height: 52px;
    border-radius: 6px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-weight: 800;
    color: #0b1f4f;
    background: linear-gradient(180deg, #f2f6ff 0%, #dbe7ff 100%);
    border: 1px solid #c7d6ff;
    text-transform: uppercase;
    letter-spacing: 0.02em;
}

.download-file-icon--pdf {
    color: #5c0a0a;
    background: linear-gradient(180deg, #fff4f2 0%, #ffe0dc 100%);
    border-color: #ffd4cc;
}

.download-file-body {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}

.download-file-title {
    font-weight: 700;
    color: var(--color-text-main);
}

.download-file-description {
    color: var(--color-text-muted);
    font-size: 0.95rem;
}

.download-file-status {
    color: #2563eb;
    font-size: 0.9rem;
    min-height: 1em;
}

.download-file-card.is-disabled .download-file-status {
    color: var(--color-text-muted);
}

.download-file-extension {
    font-family: var(--font-mono);
    font-size: 0.95rem;
    color: var(--color-text-muted);
    padding: 4px 8px;
    border-radius: 8px;
    border: 1px solid var(--color-border);
    background: #f8fafc;
}

.download-cloud {
    background: #eef6ff;
    border-color: #cfe3ff;
}

.download-cloud .download-section-title {
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.download-cloud .download-section-title::before {
    content: "";
    width: 10px;
    height: 10px;
    border-radius: 999px;
    background: #2563eb;
    box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.16);
}

.download-cloud.is-error {
    background: #fff5f5;
    border-color: #f6c7c7;
}

.download-cloud.is-error .download-section-title::before {
    background: #c81e1e;
    box-shadow: 0 0 0 4px rgba(200, 30, 30, 0.16);
}

/* =========================================================
   CONSENT WINDOW – små justeringar för copy + checkbox
========================================================= */

.overlay-consent .consent-header-left {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.overlay-consent .consent-footer {
    margin: 0;
    color: var(--color-text-muted);
    font-size: 0.95rem;
}

.overlay-consent .consent-confirm-box {
    margin-top: 4px;
    padding: 14px 16px;
    background: #f5f5f7;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.overlay-consent .consent-checkbox-row {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    line-height: 1.45;
}

.overlay-consent .consent-checkbox-row input[type="checkbox"] {
    width: 18px;
    height: 18px;
    margin-top: 2px;
    flex-shrink: 0;
    cursor: pointer;
}

.overlay-consent .consent-note {
    margin: 0;
    color: var(--color-text-muted);
    font-size: 0.95rem;
    line-height: 1.4;
}

@media (max-width: 720px) {
    .overlay-download .download-section-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 6px;
    }

    .download-file-grid {
        grid-template-columns: 1fr;
    }

    .download-file-card {
        grid-template-columns: auto 1fr;
        grid-template-areas:
            "icon body"
            ". ext";
        row-gap: 8px;
    }

    .download-file-icon {
        grid-area: icon;
    }

    .download-file-body {
        grid-area: body;
    }

    .download-file-extension {
        grid-area: ext;
        justify-self: flex-start;
    }
}
