/* ************************************************** */
/* #region DROPDOWN MENU                              */
/* ************************************************** */

dropdown-menu[popover] {
    isolation: isolate;
    inset: unset;
    min-width: min-content;
    max-width: calc(100% - var(--grid-gap));
    max-height: 50vh;
    width: fit-content;
    margin: 0;
    margin-top: 0.25em;
    margin-bottom: var(--grid-gap);
    margin-right: var(--grid-gap);

    position-area: block-end span-inline-end;
    position-try-fallbacks: flip-block, flip-inline, flip-inline flip-block;
}

dropdown-menu[popover]:popover-open {
    outline: auto;
}

dropdown-menu.submenu-popover[popover] {
    margin-left: var(--shadow-x);

    position-area: span-block-end inline-end;
    position-try-fallbacks: flip-inline, flip-block, flip-inline flip-block;
}

/* #endregion */

/* ************************************************** */
/* #region MENU-ITEM-GROUP                            */
/* ************************************************** */

menu-item-group,
.menu-item-group {
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 0.25em;
    width: 100%;
    border-bottom: var(--border-width) solid var(--border-color);
}

dropdown-menu menu-item-group,
dropdown-menu .menu-item-group {
    padding: 0.25em;
}

menu-item-group:last-child,
.menu-item-group:last-child,
.menu-item-group:last-of-type {
    border-bottom: 0;
}

menu-item-group:empty,
.menu-item-group:empty {
    display: none;
}

/* hide bottom border if menu-item-group precedes an empty group */
.menu-item-group:has(+ :empty) {
    border-bottom: 0;
}

/* #endregion */

/* ************************************************** */
/* #region MENU-ITEM-GROUP SEARCH                     */
/* Generic search filtering for any picker/list.      */
/* JS sets data-mode="search" on the .menu-item-group */
/* while the search box has text, and toggles        */
/* .search-match on matching .menu-item-contents.     */
/* ************************************************** */

/* while searching, hide rows that don't match (but never the message row) */
.menu-item-group[data-mode="search"] > .menu-item-contents:not(.search-match, .empty-search-message) {
    display: none;
}

/* hide the "No search results!" row unless we're searching AND nothing matched */
.menu-item-group:not([data-mode="search"]) > .empty-search-message,
.menu-item-group:has(> .search-match) > .empty-search-message {
    display: none;
}

/* #endregion */

/* ************************************************** */
/* #region MENU-ITEM WRAPPER                          */
/* Only used if menu-item has menu-item-tools         */
/* ************************************************** */

.menu-item-wrapper {
    display: grid;
    grid-template-areas: "menu-item-wrapper";
    align-items: center;
    position: relative;
}

.menu-item-wrapper > .menu-item {
    grid-area: menu-item-wrapper;
    padding-inline: 0.125em 3em;
}

.menu-item-tools {
    grid-area: menu-item-wrapper;
    justify-self: flex-end;
    align-self: flex-start;
    display: flex;
    align-items: center;
    gap: 0.25em;
    width: fit-content;
    height: fit-content;
    padding: 0.125em;
    z-index: 1;
}

.menu-item-tool:hover {
    background-color: var(--bg-color-hover);
    color: var(--text-color-hover);
}

.menu-item-tool.active {
    background-color: var(--bg-color-hover);
    color: var(--text-color-hover);
}

/* #endregion */

/* ************************************************** */
/* #region MENU-ITEM                                  */
/* ************************************************** */

.menu-item-contents {
    display: block;
    max-width: 100%;
}

.menu-item {
    display: flex;
    align-items: flex-start;
    position: relative;
    width: 100%;
    padding-inline: 0.125em;
    padding-block: 0.125em;
    cursor: pointer;
}

p.menu-item {
    cursor: default;
}

dropdown-menu .menu-item {
    padding-inline: 0.125em 1.5em;
}

.menu-item-label-icon {
    flex-shrink: 0;
    width: max(1.5em, 3ch);
    margin-right: 1ch;
}

.menu-item-label {
    flex: 1;
    display: flex;
    align-items: center;
    max-width: 66ch;
    margin-top: calc((1.5em - 1lh) / 2);
    overflow-wrap: anywhere;
}

.menu-item-label[contenteditable="true"]:empty,
.menu-item-label[contenteditable="true"]:empty {
    align-self: center;
}

.menu-item:hover > .menu-item-label {
    text-decoration: underline;
}

.menu-item-value {
    display: flex;
    align-items: center;
    min-height: 1.5em;
    margin-inline: 2ch 1ch;
    font-variant-numeric: tabular-nums;
    text-align: right;
}

/* #endregion */

/* ************************************************** */
/* #region PICKER ITEM                                */
/* ************************************************** */

.picker-container {
    overflow: hidden;
}

.menu-item.picker-item {
    gap: 0.25em;
    padding-inline: 0.125em;
}

.menu-item.picker-item:hover {
    cursor: default;
}

.menu-item.picker-item > .menu-item-label {
    margin-right: 1ch;
}

.menu-item.picker-item:hover > .menu-item-label {
    cursor: default;
    text-decoration: none;
}

/* #endregion */

/* ************************************************** */
/* #region ITEM HINT                                  */
/* ************************************************** */

.item-hint {
    display: none;  /* start by not showing hints */
    padding-block: 0.5em;
    border-bottom: 1px solid var(--border-color);
    font-size: 0.875em;
    opacity: 0.7;
}

dropdown-menu .item-hint {
    padding-inline: 0.5em;
    padding-block: 0.25em;
}

/* Show hints when checked panel preference checkbox is checked */
.panel:has([data-preference="show-widget-hints"]:checked) .item-hint {
    display: block;
}

/* Hide bottom border of hint in the last list item */
li:last-child > .item-hint,
.field-wrapper:last-child > .item-hint,
.menu-item-contents:last-child > .item-hint {
    border-bottom: 0;
}

/* Dim hint when its owner label is disabled */
label:has(input:disabled) + .item-hint {
    color: alpha(from var(--text-color) / 50%);
}

.item-hint [icon] {
    display: inline-flex;
    vertical-align: text-bottom;
    height: 1lh;
    overflow: visible;
}

/* #endregion */

/* ************************************************** */
/* #region OPTIONS-GROUP                              */
/* ************************************************** */

.options-group {
    display: flex;
    flex-direction: column;
    gap: 0.25em;
}

.options-group > li > .menu-item.picker-item {
    padding-inline: 0;
}

/* #endregion */

/* ************************************************** */
/* #region EMPTY MENU-ITEM-GROUP                      */
/* ************************************************** */

/* Hide the "No saved notes/chats" message when there is at least 1 note */
.all-items-list:has(.checkbox-item) .empty-note {
    display: none;
}

/* Generic: keep a permanent <li class="menu-item-contents empty-note"> in any .menu-item-group. */
/* It hides itself as soon as the group has at least 1 real row (any .menu-item-contents that isn't a message row). */
/* JS only adds/removes the real rows — it never creates or deletes the empty note. */
.menu-item-group:has(> .menu-item-contents:not(.empty-note, .empty-search-message, .load-error)) > .empty-note {
    display: none;
}

/* data-state="loading" (first load not done yet) → show nothing, rather than a wrong "nothing here!" */
.menu-item-group[data-state="loading"] > .empty-note {
    display: none;
}

/* Permanent "Couldn't load the list!" row: hidden unless JS flags data-state="error" — and then it's the ONLY thing shown */
.menu-item-group:not([data-state="error"]) > .load-error,
.menu-item-group[data-state="error"] > .menu-item-contents:not(.load-error) {
    display: none;
}

.menu-item-contents.load-error .menu-item {
    padding-left: 0.375em;
}

.menu-item-contents.empty-note .menu-item {
    padding-left: 0.375em;
}

/* #endregion */

/* ************************************************** */
/* #region SUBMENU ACTIVATOR ITEM                     */
/* ************************************************** */

.menu-item.submenu-activator {
    padding-inline: 0.125em;
}

.menu-item.submenu-activator::after {
    content: "⏵";
    display: flex;
    justify-content: center;
    align-items: center;
    height: 1.5em;
    margin-left: 2ch;
}

/* #endregion */

/* ************************************************** */
/* #region NUM ITEM                                   */
/* ************************************************** */

.menu-item.num-item {
    padding-inline: 0.125em;
}

.menu-item.num-item .number-input-container {
    margin-left: 1ch;
}

/* #endregion */

/* ************************************************** */
/* #region RANGE ITEM                                 */
/* ************************************************** */

.menu-item.range-item {
    padding-inline: 0.125em 0.5em;
    color: var(--text-color);
}

.menu-item .range-input-container {
    width: max(15%, 3em);
}

/* #endregion */

/* ************************************************** */
/* #region CHECKBOX/RADIO MENU-ITEMS                  */
/* ************************************************** */

.menu-item.checkbox-item::before,
.menu-item.radio-item::before {
    margin-right: 1ch;
}

/* ========================== */
/* #region Checkbox menu-item */
/* ========================== */

.checkbox-item::before {
    content: "[ ]";
}

.checkbox-item:has(input:indeterminate)::before {
    content: "[–]";
}

.checkbox-item:has(input:checked)::before {
    content: "[×]";
}

.checkbox-item:has(input[disabled]:checked)::before {
    content: "[ ]";
}

/* #endregion */

/* ======================= */
/* #region Radio menu-item */
/* ======================= */

.radio-item::before {
    content: "( )";
}

.radio-item:has(input:checked)::before {
    content: "(×)"
}

.radio-item:has(input[disabled]:checked)::before {
    content: "( )";
}

/* #endregion */

/* #endregion */

/* ************************************************** */
/* #region COLOR MENU-ITEM                            */
/* ************************************************** */

.color-item {
    padding-inline: 0.25em 0.5em;
}

.color-item input {
    margin-left: 2ch;
}

/* #endregion */

/* ************************************************** */
/* #region MASTER SELECT-ALL TOGGLE                   */
/* ************************************************** */

.master-select-all-toggle-container {
    flex-shrink: 0;
    position: sticky;
    top: 0;
    left: 0;
    background-color: var(--bg-color);
    color: var(--text-color);
    z-index: 1;
}

/* #endregion */
