//
// 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 '../../styles/elevation';
@use '../../styles/motion';
@use '../../styles/shapes';
@use '../../styles/typography';

dialog.micl-sidesheet {
    --md-sys-sidesheet-width: 256px;
    --md-sys-sidesheet-maxwidth: 400px;
    --md-sys-sidesheet-spring-buffer: 200px;
    --md-sys-sidesheet-padding-standard: 24px;
    --md-sys-sidesheet-padding-modal: 16px;
    --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};

    box-sizing: border-box;
    display: none;
    flex-direction: column;
    justify-content: space-between;
    min-width: calc(var(--md-sys-sidesheet-width) + var(--md-sys-sidesheet-spring-buffer));
    max-width: calc(var(--md-sys-sidesheet-maxwidth) + var(--md-sys-sidesheet-spring-buffer));
    height: 100%;
    min-height: 100%;
    margin-top: 0;
    margin-bottom: 0;
    margin-inline-start: auto;
    margin-inline-end: calc(-1 * (var(--md-sys-sidesheet-maxwidth) + var(--md-sys-sidesheet-spring-buffer)));
    padding-top: 10px;
    padding-bottom: var(--md-sys-sidesheet-padding-standard);
    padding-inline-start: 0;
    padding-inline-end: var(--md-sys-sidesheet-spring-buffer);
    background-color: var(--md-sys-color-surface);
    border: 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;
        padding-inline-start: 24px;
        padding-inline-end: 12px;
        column-gap: 8px;
        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;
        }
    }
    .micl-sidesheet__content {
        box-sizing: border-box;
        flex: 1 1 auto;
        width: 100%;
        max-width: 100%;
        padding-inline-start: var(--md-sys-sidesheet-padding-standard);
        padding-inline-end: var(--md-sys-sidesheet-padding-standard);
        overflow: hidden auto;
    }
    .micl-sidesheet__actions {
        box-sizing: border-box;
        display: flex;
        align-items: center;
        justify-content: start;
        width: 100%;
        max-width: 100%;
        height: calc(72px - var(--md-sys-sidesheet-padding-standard));
        margin: auto 0 0 0;
        padding-top: 16px;
        overflow: hidden;
        border-top: var(--md-sys-divider-thickness) solid var(--md-sys-divider-color);

        button:active,
        button:focus-visible,
        button:hover {
            color: var(--md-sys-color-primary);
        }
    }

    &::backdrop {
        background-color: rgba(0, 0, 0, 0);
        transition:
            background-color var(--md-sys-sidesheet-motion-duration) linear,
            overlay var(--md-sys-sidesheet-motion-duration) linear allow-discrete,
            display var(--md-sys-sidesheet-motion-duration) linear allow-discrete;
    }
    &[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);

        .micl-sidesheet__headline {
            padding-inline-start: 4px;
        }
    }
    &:popover-open {
        border-inline-start: var(--md-sys-divider-thickness) solid var(--md-sys-divider-color);
    }
    &:popover-open,
    &[open] {
        display: flex;
        margin-inline-end: calc(-1 * var(--md-sys-sidesheet-spring-buffer));
        opacity: 1;
        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;
        }
    }
    &[open]::backdrop {
        background-color: rgba(0, 0, 0, 0.2);

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

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