/* Defines global resets, typography, controls, forms, and table presentation. */
/* Applies predictable sizing and disables tap-highlight overlays across the page. */
* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

/* Sets the selected color scheme and shared typography defaults for the document. */
:root {

    /*
        Change color scheme based on selected value in header
        Default is System Settings 
    */
    /* Uses the operating-system preference when the system theme option is selected. */
    &:has([value="⭐"]:checked) {
        color-scheme: light dark;
    }

    /* Forces a light palette when the light theme option is selected. */
    &:has([value="☀️"]:checked) {
        color-scheme: light;
    }

    /* Forces a dark palette when the dark theme option is selected. */
    &:has([value="🌑"]:checked) {
        color-scheme: dark;
    }

    font-family: var(--font-family-content);
}


/* Removes extra font metric space from common text content. */
:is(h1,
    h2,
    h3,
    h4,
    h5,
    h6,
    p,
    span,
    label) {
    text-box: trim-both cap alphabetic;

}

/* Applies heading typography, balanced wrapping, and heading text color. */
:is(h1,
    h2,
    h3,
    h4,
    h5,
    h6,
    label) {
    text-wrap: balance;
    font-family: var(--font-family-heading);
    line-height: 1.2;
    color: var(--text-color-heading);
}

/* Applies readable body typography and content text color to inline and list text. */
:is(p,
    span,
    a,
    li,
) {
    font-family: var(--font-family-content);
    font-weight: 200;
    text-wrap: pretty;
    color: var(--text-color-content);
}

/* Sets the largest fluid heading size and its vertical spacing. */
h1 {
    font-size: var(--font-size-4);
    margin-block: 0.5em;
}

/* Sets the second-level heading size and vertical spacing. */
h2 {
    font-size: var(--font-size-3);
    margin-block: 0.5em;
}

/* Sets the third-level heading size from the shared type scale. */
h3 {
    font-size: var(--font-size-2);
}

/* Sets the fourth-level heading size from the shared type scale. */
h4 {
    font-size: var(--font-size-1);
}

/* Sets the fifth-level heading size from the shared type scale. */
h5 {
    font-size: var(--font-size-1);
}

/* Sets the sixth-level heading size from the shared type scale. */
h6 {
    font-size: var(--font-size-1);
}

/* Styles paragraphs for comfortable reading. */
p {
    font-size: var(--font-size-0);
    line-height: 1.5;
    font-weight: 300;
}

/* Applies the base content font size to inline spans. */
span {
    font-size: var(--font-size-0);
}

/* Makes form labels compact and visually prominent. */
label {
    font-size: 0.8rem;
    font-weight: bold;
}

/* Enables smooth anchor scrolling and consistent, themed scrollbar presentation. */
html {
    scroll-behavior: smooth;
    scrollbar-gutter: stable;

    scrollbar-color: var(--primary-color) transparent;
    scrollbar-width: thin;
}

/* Emphasizes strong text with the theme's foreground color. */
strong {
    font-weight: bold;
    color: light-dark(var(--text-color-primary), var(--text-color-accent));
}

/* Adds space above anchored elements to account for the page header. */
[id] {
    scroll-margin-top: 150px;
}


/* Makes images responsive and crops them to fill their displayed boxes. */
img {
    display: block;
    max-width: 100%;
    height: auto;
    object-fit: cover;
}

/* Removes default page spacing and applies the theme surface behind all content. */
body {
    margin: 0;
    padding: 0;
    background-color: var(--surface-light-dark);
}

/* Removes underlines from links, whose interaction styling is handled elsewhere. */
a {
    text-decoration: none;
}

/* Removes default list markers and spacing from navigation and content lists. */
:is(ul, ol, menu, li) {
    list-style: none;
    padding: 0;
    margin: 0;
}

/* Applies the shared primary button appearance and interaction states. */
button {
    /* border-radius: 1px; */
    padding: 1rem 1.25rem;
    color: var(--text-white);
    background-color: var(--surface-color-primary);
    transition: var(--transition-duration-small);
    border: none;
    font-weight: bold;
    font-family: var(--font-family-content);
    cursor: pointer;

    /* Lightens a button while the pointer hovers over it. */
    &:hover {
        background-color: var(--surface-color-primary-hover);
    }

    /* Darkens a button while it is pressed. */
    &:active {
        background-color: var(--surface-color-primary-active);
    }
}

/* Applies shared colors, borders, spacing, and focus/validation states to form controls. */
:is(input, textarea, select) {
    color: var(--text-color-content);
    background-color: var(--surface-light-dark);
    border: none;
    outline: var(--border-size-small) var(--border-light-dark);
    padding: 1rem;
    transition: var(--transition-duration-small);
    flex: 1;

    /* Highlights form controls on pointer hover. */
    &:is(:hover) {
        outline: var(--border-size-small) var(--border-color-primary-hover);
    }

    /* Highlights controls while focused or actively interacted with. */
    &:is(:active, :focus-within) {
        outline: var(--border-size-small) var(--border-color-primary-active);
    }

    /* Marks browser-detected invalid controls with an error outline. */
    &:user-invalid {

        outline: var(--border-size-small) red;
    }
}

/* Gives textareas a readable minimum height and lets their content size them. */
textarea {
    min-block-size: 8lh;
    field-sizing: content;
}

/* Uses the browser's customizable native select appearance for controls and popovers. */
select,
::picker(select) {
    appearance: base-select;
    cursor: pointer;
    border-radius: 0;
}

/* Hides the native select arrow to use the custom picker presentation. */
select {
    /* Hides the browser-provided select icon in favor of the custom picker. */
    &::picker-icon {
        display: none;
    }
}

/* Styles the select popover and its closed-state transition. */
::picker(select) {
    border: var(--border-size-small) var(--border-light-dark);
    margin-block: 1rem;

    /* Animates picker changes only when the user has not requested reduced motion. */
    @media (prefers-reduced-motion: no-preference) {
        transition: var(--transition-duration-medium);
    }

    opacity: 0;
    scale: 0.99;

}

/* Reveals and scales the select popover when its select control is open. */
select:open::picker(select) {
    opacity: 1;
    transform: scaleY(1);

    /* Defines the initial popover state used when the open transition begins. */
    @starting-style {
        opacity: 0;
        scale: 0.99;
    }
}


/* Styles select options and their hover, focus, selected, and active states. */
option {
    padding: 1rem 1rem;
    background-color: var(--surface-white);
    transition: var(--transition-duration-small);

    /* Highlights an option under the pointer or keyboard focus. */
    &:is(:hover, :focus-visible) {
        background-color: var(--surface-white-hover);
    }

    /* Marks the currently selected or pressed option. */
    &:is(:checked, :active) {
        background-color: var(--surface-white-active);
    }


    /* Hides the platform checkmark in favor of the custom option styling. */
    &::checkmark {
        display: none;
    }
}

/* Defines the shared form shell, field layout, messages, and responsive controls. */
form {
    display: flex;
    flex-direction: column;
    background-color: white;
    border: var(--border-size-small) var(--border-light-dark);
    box-shadow: var(--box-shadow-spread-medium) var(--box-shadow-light-dark);

    min-width: min(80ch, 100%);
    margin-inline: auto;

    /* Styles the form title banner. */
    header {
        padding: calc((var(--fluid) * 0.5rem) + 1rem);
        background-color: var(--surface-color-primary);

        /* Keeps form header text visible on the primary background. */
        &>* {
            color: var(--text-white);
        }
    }

    /* Normalizes headings inside forms. */
    :is(h1, h2, h3, h4, h5, h6) {
        font-size: var(--font-size-1);
        margin: 0;
    }

    /* Makes form controls fill their available field width. */
    :is(input, textarea, select) {
        width: 100%;
    }

    /* Allows form textareas to grow with content without a manual resize handle. */
    textarea {
        min-block-size: 8lh;
        resize: none;
        field-sizing: content;
    }

    /* Stacks label text over its associated control. */
    label {
        display: flex;
        flex-direction: column;
        height: fit-content;
    }

    /* Adds separation between each label and its control. */
    label :is(input, textarea, select) {
        margin-block-start: 0.5rem;
    }

    /* Aligns form buttons to the start of the field column. */
    button {
        align-self: flex-start;
    }

    /* Arranges fieldset contents as a padded vertical group. */
    fieldset {
        border: none;
        display: flex;
        flex-direction: column;
        gap: 1rem;
        padding: calc((var(--fluid) * 0.5rem) + 1rem);
        margin: 0;
    }

    /* Stacks each custom form item label, control, and supporting content. */
    ims-form-item {
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
    }

    /* Styles inline form feedback and fades it into view. */
    ims-form-message {
        transition: var(--transition-duration-small);
        padding: 1rem;
        color: var(--text-color-primary);
        background-color: var(--surface-color-accent);
        opacity: 1;

        /* Starts newly displayed feedback as transparent for its entrance transition. */
        @starting-style {
            opacity: 0;
        }
    }
    /* Aligns related compact controls in a single row. */
    ims-form-row {
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }
}

/* Presents address information as a stacked block without browser italics. */
address {
    font-style: normal;
    line-height: 1.5;
    display: flex;
    flex-direction: column;

    /* Keeps links in address blocks at normal font weight. */
    a {
        font-weight: normal;
    }
}

/* Defines shared table structure, row interaction, cell spacing, and sorting controls. */
table {
    display: table;
    background-color: var(--surface-white);
    border-collapse: collapse;
    width: 100%;
    /* Separates table rows and aligns header and row content to the start. */
    th,
    tr {
        border-block: var(--border-size-small) var(--border-light-dark);
        text-align: start;
    }

    /* Styles table headers with the primary color and generous cell padding. */
    th {
        color: var(--text-white);
        background-color: var(--surface-color-primary);
        padding: 1.2rem 1.5rem;
    }

    /* Makes rows interactive and defines hover, pressed, and empty states. */
    tr {
        transition: var(--transition-duration-medium);
        cursor: pointer;

        /* Highlights a row while hovered. */
        &:hover {
            background-color: var(--surface-white-hover);
        }

        /* Shows a pressed row state. */
        &:active {
            background-color: var(--surface-white-active);
        }

        /* Hides empty rows generated by the table component. */
        &:empty {
            display: none;
        }
    }

    /* Adds consistent spacing to table data cells. */
    td {
        padding: 0.75rem 1.5rem;

        /* Hides empty cells to avoid unused table gaps. */
        &:empty {
            display: none;
        }

        /* Prevents the first data column from wrapping its identifier or label. */
        &:first-child {
            text-wrap: nowrap;
        }
    }

    /* Styles the QuickGrid column-options trigger without its normal button surface. */
    .col-options-button {
        margin-inline-end: 0.5rem;
        background: none !important;

        /* Supplies a compact menu glyph for column options. */
        &::before {
            content: '☰';
        }
    }

    /* Removes button chrome from sorting indicators and uses the primary text color. */
    .sort-indicator {
        background: none !important;
        color: var(--text-color-primary);
    }

    /* Adds a rotated direction glyph to sorted table headings. */
    th:not([aria-sort="none"]) .sort-indicator::after {
        transition: var(--transition-duration-small);
        display: inline-block;
        color: var(--text-white);
        content: '>';
        rotate: 90deg;
    }

    /* Keeps the table column-options menu above adjacent table content. */
    .col-options {
        z-index: 10;
    }

}