@layer reset, variables, base, vendor, layout, page;

@import "./fonts/inter.css";
@import "./vendor/tom-select.css" layer(vendor);
@import "./core/reset.css" layer(reset);
@import "./core/variables.css" layer(variables);
@import "./core/base.css" layer(base);

@layer layout {
    .app-layout {
        --sidebar-width: 16rem;

        display: grid;
        grid-template-columns: var(--sidebar-width) minmax(auto, 1fr);
        grid-template-rows: auto 1fr;
    }

    .app-header {
        padding-block: 0.625rem;
        padding-inline: 1rem;
        grid-row-start: 1;
        grid-column-start: 2;

        display: flex;
        align-items: center;
        justify-content: space-between;

        background-color: #ffffff;
        box-shadow: inset 0 -1px 0 0 #e5e5e5;

        position: sticky;
        top: 0;
    }

    .app-header-profile {
        display: flex;
        align-items: center;
        gap: 0.75rem;
    }

    .app-header-profile-info {
        display: flex;
        flex-direction: column;
        align-items: flex-end;

        font-size: var(--font-size-sm);
        line-height: var(--font-size-sm-lh);
    }

    .app-header-profile-name {
        font-weight: var(--font-weight-semibold);
        color: #0a0a0a;
    }

    .app-header-profile-role {
        font-size: var(--font-size-xs);
        line-height: var(--font-size-xs-lh);
        color: #737373;
    }

    .app-header-profile-avatar {
        width: 2.25rem;
        height: 2.25rem;

        display: inline-flex;
        align-items: center;
        justify-content: center;

        background-color: #76e1fe;
        border-radius: 9999px;

        color: #ffffff;
        font-weight: var(--font-weight-semibold);
    }

    .app-sidebar {
        height: 100dvh;
        grid-row: 1 / -1;
        grid-column-start: 1;

        display: flex;
        flex-direction: column;

        background-color: #f5f5f5;
        box-shadow: inset -1px 0 0 0 #e5e5e5;

        position: sticky;
        top: 0;

        overflow: hidden;
        z-index: 5;
    }

    .app-sidebar-header {
        width: var(--sidebar-base-width);
        padding-block: 1rem;
        padding-inline: 1rem;

        box-shadow: inset 0 -1px 0 0 #e5e5e5;
    }

    .app-sidebar-logo {
        display: flex;
        align-items: center;
    }

    .app-sidebar-logo-text {
        display: block;
    }

    .app-sidebar-nav {
        flex: 1 1 auto;
        width: var(--sidebar-base-width);
        padding-block: 1rem;
        padding-inline: 0.5rem;

        display: flex;
        flex-direction: column;
        gap: 0.25rem;
    }

    .app-sidebar-group {
        display: flex;
        flex-direction: column;
    }

    .app-sidebar-toggle {
        padding-block: 0.125rem;
        padding-inline: 0.5rem;
        border: none;

        display: inline-flex;
        align-items: center;
        gap: 0.5rem;

        background-color: transparent;

        text-align: left;

        user-select: none;

        @media (hover: hover) {
            &:hover {
                background-color: #e5e5e5;
            }
        }

        svg {
            height: 1rem;
            width: 1rem;

            pointer-events: none;
        }

        span {
            pointer-events: none;
        }
    }

    .app-sidebar-list {
        margin-block: 0 0.25rem;
        margin-inline: 1rem;
        padding-block: 0.125rem;
        padding-inline: 0.5rem;

        display: flex;
        flex-direction: column;
        gap: 0.125rem;

        box-shadow: inset 1px 0 0 0 #d4d4d4;

        list-style: none;
    }

    .app-sidebar-item {
        display: flex;
        flex-direction: column;
    }

    .app-sidebar-link {
        padding-block: 0.125rem;
        padding-inline: 0.5rem;

        display: flex;
        align-items: center;
        gap: 0.5rem;

        color: #000000;
        text-decoration: none;

        @media (hover: hover) {
            &:hover {
                background-color: #e5e5e5;
            }
        }

        svg {
            height: 1rem;
            width: 1rem;

            pointer-events: none;
        }

        span {
            pointer-events: none;
        }
    }

    .app-sidebar-footer {
        padding-block: 1rem;
        padding-inline: 1rem;

        display: flex;
        flex-direction: column;
    }

    .app-page {
        padding-block: 1rem;
        padding-inline: 1rem;
        grid-row: 2;
        grid-column: 2;

        display: flex;
        flex-direction: column;
        gap: 1.5rem;
    }

    .page-header {
        flex: 0 1 auto;

        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 1rem;
    }

    .page-title {
        font-size: var(--font-size-3xl);
        line-height: var(--font-size-3xl-lh);
        font-weight: var(--font-weight-semibold);
        letter-spacing: var(--letter-spacing-tight);
    }

    .page-header-actions {
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    .page-content {
        flex: 1 1 auto;
    }
}

@layer layout {
    .center-layout {
        padding: 1rem;

        display: grid;
        place-items: center;

        background-color: #f5f5f5;
    }

    .login-page {
        width: 100%;
        max-width: 22rem;
        padding: 1.5rem;
        border: 1px solid #e5e5e5;
        border-radius: var(--radius-md);

        display: flex;
        flex-direction: column;
        gap: 1.5rem;

        background-color: #ffffff;
    }

    .error-page {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 0.5rem;

        text-align: center;
    }

    .error-page-status {
        font-size: var(--font-size-3xl);
        line-height: var(--font-size-3xl-lh);
        font-weight: var(--font-weight-bold);
    }
}

@layer components {
    .btn {
        padding: 0.5rem 0.75rem;
        border: 1px solid #0a0a0a;
        border-radius: var(--radius-md);

        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 0.5rem;

        background-color: #ffffff;

        font-family: inherit;
        font-size: var(--font-size-sm);
        line-height: var(--font-size-sm-lh);
        font-weight: var(--font-weight-normal);
        color: #0a0a0a;
        white-space: nowrap;
        text-decoration: none;

        user-select: none;

        @media (hover: hover) {
            &:hover:not([data-disabled]) {
                background-color: #f5f5f5;
            }
        }

        &.btn-primary {
            background-color: #3b82f6;
            border-color: #3b82f6;

            color: #ffffff;

            @media (hover: hover) {
                &:hover:not([data-disabled]) {
                    background-color: #2563eb;
                    border-color: #2563eb;
                }
            }
        }

        &.btn-danger {
            background-color: #ef4444;
            border-color: #ef4444;

            color: #ffffff;

            @media (hover: hover) {
                &:hover:not([data-disabled]) {
                    background-color: #dc2626;
                    border-color: #dc2626;
                }
            }
        }

        &.btn-full {
            width: 100%;
        }
    }

    .search-form {
        display: flex;
        gap: 0.5rem;
        margin-block-end: 1rem;
    }

    :where(.search-form) .form-fields {
        flex: 1 1 auto;
    }

    .data-table {
        display: flex;
        flex-direction: column;
        gap: 1rem;
    }

    .table {
        width: 100%;
        border-collapse: collapse;

        table-layout: auto;
    }

    :where(.table) {
        thead th {
            padding-block: 0.5rem;
            padding-inline: 0.75rem;
            border-bottom: 1px solid #e5e5e5;

            font-size: var(--font-size-sm);
            line-height: var(--font-size-sm-lh);
            font-weight: var(--font-weight-medium);
            text-align: left;
            color: #737373;

            &:last-child {
                text-align: right;
            }
        }

        tbody td {
            padding-block: 0.5rem;
            padding-inline: 0.75rem;
            border-bottom: 1px solid #e5e5e5;

            text-align: left;
        }
    }

    .table-row-actions {
        display: flex;
        align-items: center;
        justify-content: flex-end;
        gap: 0.5rem;
    }

    .table-row-action {
        display: inline-flex;
        align-items: center;
        justify-content: center;

        text-decoration: none;
        color: inherit;

        svg {
            width: 1.5rem;
            height: 1.5rem;

            pointer-events: none;
        }
    }

    .pagination {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 0.5rem;

        color: #0a0a0a;
    }

    .pagination-link {
        display: inline-flex;
        align-items: center;
        justify-content: center;

        text-decoration: none;
        color: inherit;

        svg {
            width: 1.5rem;
            height: 1.5rem;

            pointer-events: none;
        }

        &[aria-disabled] {
            opacity: 0.5;
        }
    }

    .details {
        display: flex;
        flex-direction: column;
    }

    :where(.details) {
        dt {
            font-weight: var(--font-weight-medium);
            color: #737373;
        }

        dd + dt {
            margin-block-start: 0.75rem;
        }
    }

    .form {
        display: flex;
        flex-direction: column;
        gap: 1.5rem;
    }

    .form-fields {
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
    }

    .form-field {
        display: flex;
        flex-direction: column;
        gap: 0.25rem;

        label {
            font-size: var(--font-size-sm);
            line-height: var(--font-size-sm-lh);
            font-weight: var(--font-weight-semibold);
            color: #0a0a0a;
        }

        input,
        select,
        textarea {
            appearance: none;
            -webkit-appearance: none;

            padding-block: 0.5rem;
            padding-inline: 0.75rem;
            border: 1px solid #0a0a0a;

            border-radius: var(--radius-md);

            font-size: var(--font-size-sm);
            line-height: var(--font-size-sm-lh);
            font-weight: var(--font-weight-medium);

            &::placeholder {
                color: #737373;
            }

            &:disabled {
                background-color: #f5f5f5;
                color: #525252;
            }
        }

        select {
            background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='%230a0a0a' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath stroke='none' d='M0 0h24v24H0z' fill='none' /%3E%3Cpath d='M6 9l6 6l6 -6' /%3E%3C/svg%3E");
            background-repeat: no-repeat;
            background-position: right 0.75rem center;
            background-size: 1.5rem;
        }

        p {
            font-size: var(--font-size-sm);
            line-height: var(--font-size-sm-lh);
            font-weight: var(--font-weight-normal);
            color: #525252;
        }
    }

    /* Tom Select, see the remote selects in app.js: the vendor theme,
       restyled so that the control looks like the other inputs of a form
       field. */
    .ts-wrapper {
        min-block-size: auto;
    }

    .ts-control,
    .ts-wrapper.single .ts-control,
    .ts-wrapper.single.input-active .ts-control {
        gap: 0.25rem;

        padding-block: 0.5rem;
        padding-inline-start: 0.75rem;
        border: 1px solid #0a0a0a;
        border-radius: var(--radius-md);
        background-color: #ffffff;
        box-shadow: none;

        font-size: var(--font-size-sm);
        line-height: var(--font-size-sm-lh);
        font-weight: var(--font-weight-medium);
        color: inherit;

        input {
            flex: 1 1 auto;
            min-inline-size: 4rem;
            font: inherit;
        }
    }

    /* The chevron of the other selects instead of the theme's triangle. */
    .ts-wrapper.single .ts-control {
        background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='%230a0a0a' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath stroke='none' d='M0 0h24v24H0z' fill='none' /%3E%3Cpath d='M6 9l6 6l6 -6' /%3E%3C/svg%3E");
        background-repeat: no-repeat;
        background-position: right 0.75rem center;
        background-size: 1.5rem;

        &::after {
            display: none;
        }
    }

    .ts-wrapper.focus .ts-control {
        outline: 2px solid #0a0a0a;
        outline-offset: 1px;
    }

    .dropdown-active .ts-control {
        border-radius: var(--radius-md);
    }

    /* In a single select the query replaces the chosen item while typing. */
    .ts-wrapper.single.has-items.input-active .ts-control .item {
        display: none;
    }

    .ts-wrapper.plugin-clear_button .clear-button {
        color: #737373;
        font-size: var(--font-size-lg);

        &:hover {
            color: #0a0a0a;
        }
    }

    .ts-dropdown,
    .ts-dropdown.single {
        border: 1px solid #0a0a0a;
        border-radius: var(--radius-md);
        box-shadow: none;

        font-size: var(--font-size-sm);
        line-height: var(--font-size-sm-lh);
        color: inherit;
    }

    .ts-dropdown-content {
        max-block-size: 16rem;
    }

    .ts-dropdown .option,
    .ts-dropdown .no-results,
    .ts-dropdown .loading {
        padding-block: 0.5rem;
        padding-inline: 0.75rem;
    }

    .ts-dropdown .option > div {
        display: flex;
        justify-content: space-between;
        gap: 1rem;
    }

    .ts-dropdown .active {
        background-color: #f5f5f5;
        color: inherit;
    }

    .ts-dropdown .no-results,
    .ts-dropdown .loading {
        color: #737373;
    }

    .alert {
        padding-block: 0.5rem;
        padding-inline: 0.75rem;

        font-size: var(--font-size-sm);
        line-height: var(--font-size-sm-lh);
    }

    .alert-error {
        border: 1px solid #fecaca;

        background-color: #fef2f2;
        color: #991b1b;
    }

    .alert-info {
        border: 1px solid #bfdbfe;

        background-color: #eff6ff;
        color: #1e40af;
    }

    .page-section {
        margin-block-start: 2rem;

        display: flex;
        flex-direction: column;
        gap: 1rem;
    }

    .page-section-title {
        font-size: var(--font-size-xl);
        line-height: var(--font-size-xl-lh);
        font-weight: var(--font-weight-semibold);
    }

    .page-section-subtitle {
        font-size: var(--font-size-base);
        line-height: var(--font-size-base-lh);
        font-weight: var(--font-weight-semibold);
    }

    .page-section-total {
        font-weight: var(--font-weight-semibold);
        text-align: right;
    }

    .page-alert:not(:empty) {
        margin-block-end: 1rem;
    }

    .text-muted {
        color: #737373;
    }

    /* Analytics: stat tiles, chart cards and the report the filters reload,
       which keeps its previous render dimmed while loading. */
    .analytics-filters :where(.form-fields) {
        flex: 1 1 auto;

        flex-direction: row;
    }

    .analytics-filters :where(.form-field) {
        flex: 1 1 auto;
    }

    .analytics-filters {
        align-items: flex-end;
        flex-wrap: wrap;
    }

    .analytics-report {
        display: flex;
        flex-direction: column;
        gap: 1rem;

        transition: opacity 150ms ease;

        &.htmx-request {
            opacity: 0.5;
        }
    }

    .stat-tiles {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
        gap: 1rem;
    }

    .stat-tile {
        padding: 1rem;
        border: 1px solid #e5e5e5;
        border-radius: var(--radius-lg);

        background-color: #ffffff;
    }

    .stat-tile-label {
        font-size: var(--font-size-sm);
        line-height: var(--font-size-sm-lh);
        color: #737373;
    }

    .stat-tile-value {
        margin-block-start: 0.25rem;

        font-size: var(--font-size-2xl);
        line-height: var(--font-size-2xl-lh);
        font-weight: var(--font-weight-semibold);
        color: #0a0a0a;
    }

    .chart-card {
        padding: 1rem;
        border: 1px solid #e5e5e5;
        border-radius: var(--radius-lg);

        background-color: #ffffff;
    }

    .chart-card-caption {
        margin-block-end: 0.75rem;

        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 1rem;

        font-size: var(--font-size-sm);
        line-height: var(--font-size-sm-lh);
        font-weight: var(--font-weight-medium);
        color: #0a0a0a;
    }

    .chart-card-units {
        display: inline-flex;
        gap: 0.25rem;
    }

    .chart-unit {
        padding: 0.125rem 0.5rem;
        border: 1px solid #e5e5e5;
        border-radius: var(--radius-md);

        background-color: #ffffff;
        font-size: var(--font-size-xs);
        line-height: var(--font-size-xs-lh);
        color: #737373;
        cursor: pointer;

        &:hover {
            background-color: #f5f5f5;
        }

        &.is-active {
            border-color: #0a0a0a;
            color: #0a0a0a;
        }
    }

    .chart-card-plot {
        position: relative;
    }

    .analytics-table td {
        font-variant-numeric: tabular-nums;
    }
}

@layer page {
    .budget-details {
        display: flex;
        flex-direction: column;
    }

    :where(.budget-details) h2:not(:first-child) {
        margin-block-start: 1.5rem;
    }

    :where(.budget-details) h2 + dl {
        margin-block-start: 0.5rem;
    }
}
