/*
  ----------------------------------------
  UI Components
  ----------------------------------------
*/

@layer styles {
    /* Buttons
  ---------------------------------- */
    .ui-button {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        padding: 0 var(--space-sm);
        line-height: 2.2;
        border-radius: var(--rounded);
        border: var(--border) solid var(--white);
        box-shadow: var(--shadow-sm);
        background: var(--white);
        font-weight: 600;
        color: var(--darker);
        transition: all 70ms ease;
        cursor: pointer;
        background: var(--lighter);
        text-decoration: none;
        transition: all 200ms ease;
        margin: 0;

        &:hover {
            filter: brightness(1.1);
            box-shadow: var(--shadow);
        }

        &:active {
            transform: translateY(0.5px);
            box-shadow: none;
        }

        &:disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }

        &.ui-brand-1 {
            background: var(--brand-1);
            color: var(--white);
            border-color: var(--white);
        }

        &.ui-brand-1-content {
            background: var(--brand-1-content);
            color: var(--white);
        }

        &.ui-brand-2 {
            background: var(--brand-2);
            color: var(--white);
            border-color: var(--white);
        }

        &.ui-brand-3 {
            background: var(--brand-3);
            color: var(--white);
            border-color: var(--white);
        }

        &.ui-brand-4 {
            background: var(--brand-4);
            color: var(--white);
            border-color: var(--white);
        }

        &.ui-neutral {
            background: var(--midtone);
            color: var(--white);
            border-color: var(--white);
        }

        &.ui-info {
            background: var(--info);
            color: var(--white);
            border-color: var(--white);
        }

        &.ui-success {
            background: var(--success);
            color: var(--white);
            border-color: var(--white);
        }

        &.ui-warning {
            background: var(--warning);
            color: var(--white);
            border-color: var(--white);
        }

        &.ui-error {
            background: var(--error);
            color: var(--white);
            border-color: var(--white);
        }
    }

    /* Buttons mini-round */
    .ui-button-mini-round {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 1.5rem;
        height: 1.5rem;
        border-radius: 9999px;
        background: var(--lighter);
        color: var(--brand-1);
        box-shadow: var(--shadow-sm);
        transition: all 250ms ease;

        &:hover {
            background: var(--brand-1);
            color: var(--lighter);
        }
    }

    /* Data display primitives
  ---------------------------------- */
    .ui-panel {
        min-width: 0;
        border: var(--border) solid var(--light);
        border-radius: var(--rounded-sm);
        background: var(--white);
        padding: var(--space-sm);
    }

    .ui-panel-header {
        display: flex;
        flex-wrap: wrap;
        align-items: flex-start;
        justify-content: space-between;
        gap: var(--space-sm);
    }

    .ui-eyebrow {
        margin: 0 0 var(--space-3xs);
        color: var(--midtone);
        font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
        font-size: var(--text-sm);
        letter-spacing: 0.12em;
        text-transform: uppercase;
    }

    .ui-filter-form {
        display: flex;
        flex-wrap: wrap;
        align-items: flex-end;
        gap: var(--space-xs);
    }

    .ui-filter-field {
        display: flex;
        flex: 1 1 12rem;
        flex-direction: column;
        gap: var(--space-3xs);
        min-width: min(100%, 12rem);
    }

    .ui-filter-input {
        width: 100%;
        height: var(--ui-field-height);
        border: var(--ui-field-border);
        border-radius: var(--ui-field-border-radius);
        background: var(--ui-field-bg);
        color: var(--ui-field-value-color);
        font: inherit;
        padding: 0 var(--ui-field-padding);
    }

    .ui-filter-input:focus {
        border-color: var(--ui-field-focus-color);
        outline: var(--ui-field-focus-outline) var(--ui-field-focus-color);
        outline-offset: 0;
    }

    .ui-filter-submit {
        min-height: var(--ui-field-height);
        padding-right: var(--space-md);
        padding-left: var(--space-md);
    }

    .ui-card-grid {
        display: grid;
        grid-template-columns: minmax(0, 1fr);
        gap: var(--space-sm);
    }

    .ui-stat-card {
        min-width: 0;
        border: var(--border) solid var(--light);
        border-radius: var(--rounded-sm);
        background: var(--lighter);
        overflow: hidden;
    }

    .ui-stat-card-title {
        display: block;
        margin: 0;
        border-bottom: var(--border) solid var(--light);
        background: var(--white);
        color: var(--midtone);
        font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
        font-size: var(--text-sm);
        letter-spacing: 0.08em;
        padding: var(--space-2xs) var(--space-sm);
        text-transform: uppercase;
    }

    .ui-stat-card-body {
        padding: var(--space-sm);
    }

    .ui-stat-card.is-brand .ui-stat-card-title {
        border-color: var(--brand-1);
        background: var(--brand-1);
        color: var(--white);
    }

    .ui-stat-card.is-warning .ui-stat-card-title {
        border-color: var(--warning);
        background: var(--warning);
        color: var(--white);
    }

    .ui-stat-card.is-muted .ui-stat-card-title {
        border-color: var(--midtone);
        background: var(--midtone);
        color: var(--white);
    }

    .ui-legend {
        display: flex;
        flex-wrap: wrap;
        gap: var(--space-xs);
        color: var(--midtone);
        font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
        font-size: var(--text-sm);
        letter-spacing: 0.08em;
        text-transform: uppercase;
    }

    .ui-legend-item {
        display: inline-flex;
        align-items: center;
        gap: var(--space-3xs);
        white-space: nowrap;
    }

    .ui-color-mark {
        display: inline-block;
        width: 0.6rem;
        height: 0.6rem;
        border-radius: var(--rounded-sm);
        background: var(--midtone);
    }

    .ui-color-mark.is-brand,
    .ui-split-fill.is-brand,
    .ui-timeline-segment.is-brand {
        background: var(--brand-1);
    }

    .ui-color-mark.is-warning,
    .ui-split-fill.is-warning,
    .ui-timeline-segment.is-warning {
        background: var(--warning);
    }

    .ui-color-mark.is-muted,
    .ui-split-fill.is-muted,
    .ui-timeline-segment.is-muted {
        background: var(--midtone);
    }

    .ui-split-bar {
        display: flex;
        flex: 1 1 18rem;
        align-self: stretch;
        min-width: min(100%, 14rem);
        min-height: 1.25rem;
        overflow: hidden;
        border: var(--border) solid var(--light);
        border-radius: 999px;
        background: var(--lighter);
    }

    .ui-split-fill {
        min-width: 0;
    }

    .ui-timeline {
        display: flex;
        gap: var(--border-lg);
        min-height: 2.25rem;
        overflow: hidden;
        border: var(--border) solid var(--light);
        border-radius: var(--rounded-sm);
        background: var(--lighter);
    }

    .ui-timeline-segment {
        display: flex;
        flex: var(--ui-timeline-segment-flex, 1) 1 0;
        align-items: center;
        justify-content: center;
        min-width: 0.4rem;
        padding: 0 var(--space-2xs);
        overflow: hidden;
        color: var(--white);
        font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
        font-size: var(--text-sm);
        letter-spacing: 0.08em;
        text-transform: uppercase;
        white-space: nowrap;
    }

    .ui-data-table {
        width: 100%;
        min-width: 42rem;
    }

    .ui-data-table th {
        font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
        font-size: var(--text-sm);
        letter-spacing: 0.1em;
        text-transform: uppercase;
    }

    .ui-mqtt-table-wrapper {
        overflow-x: auto;
    }

    .ui-mqtt-table {
        width: 100%;
        min-width: 64rem;
        table-layout: fixed;
    }

    .ui-mqtt-table th,
    .ui-mqtt-table td {
        vertical-align: top;
    }

    .ui-mqtt-table__meter {
        width: 7rem;
    }

    .ui-mqtt-table__type {
        width: 8rem;
    }

    .ui-mqtt-table__topic {
        width: 31%;
    }

    .ui-mqtt-table__received {
        width: 9rem;
        white-space: nowrap;
    }

    .ui-mqtt-table__payload {
        width: 36%;
    }

    .ui-mqtt-type {
        display: block;
        font-weight: var(--font-semibold);
        line-height: 1.3;
    }

    .ui-mqtt-load {
        display: inline-block;
        margin-top: var(--space-3xs);
        border-radius: var(--rounded-sm);
        background: var(--lighter);
        color: var(--midtone);
        font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
        font-size: var(--text-sm);
        padding: 0 var(--space-3xs);
    }

    .ui-topic-path {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: var(--space-3xs);
        line-height: 1.65;
    }

    .ui-topic-path__segment {
        display: inline-block;
        max-width: 100%;
        overflow: hidden;
        border: var(--border) solid var(--light);
        border-radius: var(--rounded-sm);
        background: var(--lighter);
        padding: 0 var(--space-3xs);
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .ui-topic-path__separator {
        color: var(--midtone);
    }

    .ui-payload-details {
        min-width: 0;
    }

    .ui-payload-details summary {
        display: inline-flex;
        align-items: center;
        gap: var(--space-2xs);
        color: var(--brand-1);
        cursor: pointer;
        font-weight: var(--font-semibold);
    }

    .ui-payload-details[open] summary {
        margin-bottom: var(--space-xs);
    }

    .ui-payload-size {
        color: var(--midtone);
        font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
        font-size: var(--text-sm);
        font-weight: var(--font-normal);
    }

    .ui-payload-pre {
        max-width: 100%;
        max-height: 24rem;
        overflow: auto;
        margin: 0;
        border: var(--border) solid var(--light);
        border-radius: var(--rounded-sm);
        background: var(--lighter);
        color: var(--darker);
        font-size: var(--text-sm);
        line-height: 1.5;
        padding: var(--space-xs);
        white-space: pre;
    }

    .ui-state-pill {
        display: inline-flex;
        align-items: center;
        gap: var(--space-3xs);
        border: var(--border) solid var(--light);
        border-radius: 999px;
        padding: var(--space-3xs) var(--space-xs);
        white-space: nowrap;
    }

    .ui-state-pill::before {
        content: "";
        width: 0.45rem;
        height: 0.45rem;
        border-radius: var(--rounded-sm);
        background: var(--midtone);
    }

    .ui-state-pill.is-brand::before {
        background: var(--brand-1);
    }

    .ui-state-pill.is-warning::before {
        background: var(--warning);
    }

    @media (min-width: 48rem) {
        .ui-card-grid-two {
            grid-template-columns: repeat(2, minmax(0, 1fr));
        }

        .ui-card-grid-three {
            grid-template-columns: repeat(3, minmax(0, 1fr));
        }

        .ui-card-grid-four {
            grid-template-columns: repeat(4, minmax(0, 1fr));
        }
    }

    /* Hamburger Button
  ---------------------------------- */
    .ui-hamburger-button {
        --button-width: 30px;
        --button-height: 24px;
        --line-thickness: 3px;
        --line-color: var(--darker);
        --line-radius: 2px;
        --number-of-spins: 5;
        --duration: 0.15s;
        --focus-color: rgba(0, 0, 0, 0.2);

        position: relative;
        display: block;
        width: var(--button-width);
        height: var(--button-height);
        cursor: pointer;
        outline: none;

        .line-1,
        .line-2,
        .line-3 {
            position: absolute;
            display: block;
            width: 100%;
            height: var(--line-thickness);
            background-color: var(--line-color);
            border-radius: var(--line-radius);
            transition: all var(--duration) ease-in-out;
        }

        .line-2 {
            top: calc(50% - var(--line-thickness) / 2);
        }

        .line-3 {
            bottom: 0;
        }

        /* Hamburger animate */
        [data-ui-hamburger-open-value="true"] & {
            .line-1 {
                transform: translateY(
                        calc(
                            var(--button-height) / 2 - var(--line-thickness) / 2
                        )
                    )
                    rotate(calc(-1 * 45deg * var(--number-of-spins)));
            }
            .line-2 {
                opacity: 0;
            }
            .line-3 {
                transform: translateY(
                        calc(
                            -1 *
                                (
                                    var(--button-height) / 2 -
                                        var(--line-thickness) / 2
                                )
                        )
                    )
                    rotate(calc(45deg * var(--number-of-spins)));
            }
        }
    }

    /* Hamburger Menu
    ---------------------------------- */
    .ui-hamburger-menu {
        position: fixed;
        right: 0;
        top: 4rem;
        bottom: 0;
        z-index: 90;
        width: 100%;
        background-color: rgba(0, 0, 0, 0.9);
        backdrop-filter: blur(10px);
        box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1);
        transition:
            transform 120ms ease-in-out,
            visibility 0s 120ms;
        transform: translateX(100%);
        overflow-y: auto;
        visibility: hidden;

        /* Show menu when hamburger is open */
        [data-ui-hamburger-open-value="true"] & {
            visibility: visible;
            transform: translateX(0);
            transition:
                transform 120ms ease-in-out,
                visibility 0s 0s;
        }
    }

    /* Avatar
  ---------------------------------- */
    .ui-avatar {
        &[data-size="sm"] {
            --avatar-size: 1.5rem;
            --initials-font-size: 0.7rem;
        }

        /* Medium is the default */
        &[data-size="md"] {
            --avatar-size: 2.5rem;
            --initials-font-size: 1rem;
        }

        &[data-size="lg"] {
            --avatar-size: 7.5rem;
            --initials-font-size: 3rem;
        }

        width: var(--avatar-size);
        height: var(--avatar-size);
        border-radius: 9999px;
        outline: var(--border-lg) solid var(--white);
        background: var(--brand-1);
        color: var(--white);
        overflow: hidden;
        box-shadow: var(--shadow);

        .initials {
            font-size: var(--initials-font-size);
            line-height: 1;
        }
    }

    /* Toast
  ---------------------------------- */
    .ui-toast {
        position: fixed;
        display: flex;
        top: 1rem;
        left: 50%;
        transform: translateX(-50%);
        z-index: 9999;
        align-items: center;
        gap: var(--space-xs);
        min-width: 24rem;
        padding: var(--space-sm) var(--space-sm);
        border-radius: var(--rounded);
        box-shadow: var(--shadow);
        background: var(--darker);
        color: var(--light);
        border-left: 6px solid;
        animation: ephemeral 8s both;

        svg {
            flex-shrink: 0;
            border-radius: 50%;
        }

        span {
            flex: 1;
        }

        .ui-toast-close {
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            border: none;
            border-radius: 100%;
            width: 1.6rem;
            height: 1.6rem;

            &:hover {
                color: var(--white);
                background-color: var(--black);
            }
        }

        &.ui-toast-success {
            border-left-color: var(--success);

            svg.icon {
                color: var(--success);
            }
        }

        &.ui-toast-error {
            border-left-color: var(--error);

            svg.icon {
                color: var(--error);
            }
        }

        &.ui-toast-warning {
            border-left-color: var(--warning);

            svg.icon {
                color: var(--warning);
            }
        }

        &.ui-toast-alarm {
            border-left-color: var(--error);

            svg.icon {
                color: var(--error);
            }
        }

        &.ui-toast-emergency {
            border-left-color: var(--error);
            background: var(--black);
            outline: var(--border-lg) solid var(--error-light);

            svg.icon {
                color: var(--error);
            }
        }
    }

    .ui-toast-stack {
        position: fixed;
        top: var(--space-sm);
        left: 50%;
        z-index: 9999;
        display: flex;
        flex-direction: column;
        gap: var(--space-xs);
        width: min(42rem, calc(100vw - var(--space-md)));
        pointer-events: none;
        transform: translateX(-50%);

        .ui-toast {
            position: static;
            width: 100%;
            min-width: 0;
            transform: none;
            pointer-events: auto;
        }
    }

    /* Dropdown
  ---------------------------------- */
    .ui-dropdown {
        position: relative;
        display: inline-block;

        menu,
        ul {
            position: absolute;
            top: 100%;
            right: 0;
            z-index: 100;
            min-width: 12rem;
            margin-top: var(--space-2xs);
            padding: var(--space-2xs);
            background: var(--white);
            border-radius: var(--rounded);
            box-shadow: var(--shadow);
            list-style: none;
            opacity: 0;
            visibility: hidden;
            transform: translateY(-8px);
            transition:
                opacity 150ms ease-in-out,
                transform 150ms ease-in-out,
                visibility 0s 150ms;

            li {
                margin: 0;
            }

            a,
            button {
                display: flex;
                align-items: center;
                gap: var(--space-2xs);
                width: 100%;
                padding: var(--space-2xs) var(--space-xs);
                border-radius: var(--rounded-sm);
                text-decoration: none;
                color: var(--darker);
                background: transparent;
                border: none;
                cursor: pointer;
                transition: background-color 100ms ease;

                &:hover {
                    background-color: var(--lighter);
                }

                svg {
                    flex-shrink: 0;
                }
            }
        }

        /* Show menu when dropdown is open */
        &.is-open menu,
        &.is-open ul {
            opacity: 1;
            visibility: visible;
            transform: translateY(0);
            transition:
                opacity 150ms ease-in-out,
                transform 150ms ease-in-out,
                visibility 0s 0s;
        }
    }

    /* Dropdown (Inline)
  ---------------------------------- */
    .ui-dropdown.ui-dropdown-inline {
        display: block;
        width: 100%;

        menu,
        ul {
            position: static;
            min-width: 0;
            margin-top: var(--space-2xs);
            padding: 0 0 0 var(--space-sm);
            background: transparent;
            box-shadow: none;
            display: none;
            opacity: 1;
            visibility: visible;
            transform: none;
            transition: none;
        }

        &.is-open menu,
        &.is-open ul {
            display: flex;
            flex-direction: column;
            gap: var(--space-2xs);
        }
    }

    /* Auth Verify OTP Input
  ---------------------------------- */
    .ui-auth-verify {
        display: flex;
        justify-content: center;

        .ui-auth-verify-digits {
            display: flex;
            gap: var(--space-xs);

            input {
                width: 3rem;
                height: 3.5rem;
                padding: 0;
                font-size: 1.5rem;
                font-weight: 700;
                text-align: center;
                border: var(--border-lg) solid var(--light);
                border-radius: var(--rounded);
                background: var(--white);
                color: var(--darker);
                transition:
                    border-color 150ms ease,
                    box-shadow 150ms ease;

                &:focus {
                    outline: none;
                    border-color: var(--brand-1);
                    box-shadow: 0 0 0 3px
                        color-mix(in srgb, var(--brand-1) 20%, transparent);
                }

                &::placeholder {
                    color: var(--midtone);
                }
            }
        }
    }
}
