//
// Copyright © 2025 Hermana AS
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

@use '../../foundations';
@use '../../foundations/layout';
@use '../../styles/elevation';
@use '../../styles/motion';
@use '../../styles/shapes';
@use '../../styles/typography';

:root {
    --md-sys-sidesheet-width: 256px;
    --md-sys-sidesheet-maxwidth: 400px;
    --md-sys-sidesheet-padding-standard: var(--md-sys-layout-window-margin, 24px);
}
body {
    --md-sys-sidesheet-motion-spatial: #{motion.$md-sys-motion-expressive-slow-spatial};
    --md-sys-sidesheet-motion-duration: #{motion.$md-sys-motion-expressive-slow-spatial-duration};
    --md-sys-sidesheet-motion-duration-reverse: #{motion.$md-sys-motion-expressive-default-spatial-duration};
}

dialog.micl-sidesheet {
    --md-sys-sidesheet-spring-buffer: 40px;

    box-sizing: border-box;
    display: none;
    flex-direction: column;
    justify-content: space-between;
    min-inline-size: calc(var(--md-sys-sidesheet-width) + var(--md-sys-sidesheet-spring-buffer));
    max-inline-size: calc(var(--md-sys-sidesheet-maxwidth) + var(--md-sys-sidesheet-spring-buffer));
    block-size: 100vh;
    max-block-size: 100%;
    min-block-size: 100%;
    margin-block: 0;
    margin-inline-start: auto;
    margin-inline-end: calc(-1 * (var(--md-sys-sidesheet-maxwidth) + var(--md-sys-sidesheet-spring-buffer)));
    padding-block: var(--md-sys-sidesheet-padding-standard) 0;
    padding-inline: 0 var(--md-sys-sidesheet-spring-buffer);
    background-color: var(--md-sys-color-surface);
    border: none;
    border-radius: var(--md-sys-shape-corner-none);
    box-shadow: var(--md-sys-elevation-level0);
    opacity: 0;
    overflow: hidden;
    transition:
        opacity var(--md-sys-sidesheet-motion-duration-reverse) linear,
        margin-inline-end var(--md-sys-sidesheet-motion-duration-reverse) var(--md-sys-sidesheet-motion-spatial),
        overlay var(--md-sys-sidesheet-motion-duration-reverse) linear allow-discrete,
        display var(--md-sys-sidesheet-motion-duration-reverse) linear allow-discrete;

    .micl-sidesheet__headline {
        display: flex;
        align-items: center;
        block-size: var(--md-sys-typescale-title-large-line-height);
        padding-inline: var(--md-sys-sidesheet-padding-standard);
        margin-block-end: 16px;
        column-gap: 12px;
        color: var(--md-sys-color-on-surface-variant);

        h1, h2, h3, h4, h5, h6, .micl-heading {
            @include typography.title-large;

            flex: 1 2 fit-content;
            margin: 0;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            user-select: none;
        }
        button {
            margin-inline: -12px;
        }
        button:first-child {
            margin-inline-start: -16px;
        }
    }
    .micl-sidesheet__content {
        box-sizing: border-box;
        flex: 1 1 auto;
        padding-inline: var(--md-sys-sidesheet-padding-standard);
        overflow: hidden auto;
    }
    .micl-sidesheet__actions {
        box-sizing: border-box;
        display: flex;
        flex-shrink: 0;
        align-items: center;
        justify-content: start;
        padding-block: 16px var(--md-sys-sidesheet-padding-standard);
        padding-inline: var(--md-sys-sidesheet-padding-standard);
        column-gap: 8px;
        overflow: hidden;
    }

    &[open] {
        background-color: var(--md-sys-color-surface-container-low);
        border-start-start-radius: var(--md-sys-shape-corner-large);
        border-end-start-radius: var(--md-sys-shape-corner-large);
        box-shadow: var(--md-sys-elevation-level1);
    }
    &:popover-open {
        --md-sys-divider-color: var(--md-sys-color-outline, gray);

        border-inline-start: var(--md-sys-divider-thickness, 1px) solid var(--md-sys-divider-color);
    }
    &:popover-open,
    &[open] {
        display: flex;
        margin-inline-end: calc(-1 * var(--md-sys-sidesheet-spring-buffer));
        opacity: 100%;
        transition:
            opacity var(--md-sys-sidesheet-motion-duration) linear,
            margin-inline-end var(--md-sys-sidesheet-motion-duration) var(--md-sys-sidesheet-motion-spatial),
            overlay var(--md-sys-sidesheet-motion-duration) linear allow-discrete,
            display var(--md-sys-sidesheet-motion-duration) linear allow-discrete;

        @starting-style {
            margin-inline-end: calc(-1 * (var(--md-sys-sidesheet-maxwidth) + var(--md-sys-sidesheet-spring-buffer)));
            opacity: 0;
        }
    }
    &::backdrop {
        background-color: rgba(0, 0, 0, 0);
        transition: background-color var(--md-sys-sidesheet-motion-duration-reverse) linear;
    }
    &[open]::backdrop {
        background-color: rgba(0, 0, 0, var(--md-sys-state-backdrop-opacity, 32%));
        transition: background-color var(--md-sys-sidesheet-motion-duration) linear;

        @starting-style {
            background-color: rgba(0, 0, 0, 0);
        }
    }
}

body:has(dialog.micl-sidesheet[popover]) {
    box-sizing: border-box;
    max-inline-size: 100%;
    margin: 0;
    transition: max-inline-size var(--md-sys-sidesheet-motion-duration-reverse);
}
body:has(dialog.micl-sidesheet:popover-open) {
    max-inline-size: calc(100vw - var(--md-sys-sidesheet-maxwidth));
    transition: max-inline-size var(--md-sys-sidesheet-motion-duration) var(--md-sys-sidesheet-motion-spatial);
}
