/*
 * Menus with submenus: an icon on EVERY item (Bootstrap Icons), main items larger and bolder, submenu items visibly
 * smaller, indented on a guide line, groups separated by dividers. Uses the --app-nav-* tokens from theme.css.
 * Needs Bootstrap Icons' CSS (bootstrap-icons.min.css) loaded in the layout.
 *
 * Full sidebar with collapsible groups and the bottom-left user/theme corner: sidebar.html (+ menu.js).
 * Minimal sidebar markup:
 *   <nav class="app-sidebar">
 *     <a class="app-nav-main" th:href="@{/}"><i class="bi bi-speedometer2" aria-hidden="true"></i>Dashboard</a>
 *     <hr class="app-nav-divider">
 *     <div class="app-nav-group">
 *       <div class="app-nav-main"><i class="bi bi-receipt" aria-hidden="true"></i>Orders</div>   <!-- group title -->
 *       <div class="app-nav-items">
 *         <a class="app-nav-sub" th:href="@{/orders}"><i class="bi bi-list-ul" aria-hidden="true"></i>All orders</a>
 *         <a class="app-nav-sub" th:href="@{/orders/refunds}"><i class="bi bi-arrow-counterclockwise" aria-hidden="true"></i>Refunds</a>
 *       </div>
 *     </div>
 *     <hr class="app-nav-divider">
 *     ...
 *   </nav>
 *
 * Bootstrap dropdown with groups:
 *   <ul class="dropdown-menu app-dropdown">
 *     <li><h6 class="dropdown-header"><i class="bi bi-bar-chart" aria-hidden="true"></i>Reports</h6></li>
 *     <li><a class="dropdown-item" href="#"><i class="bi bi-calendar3" aria-hidden="true"></i>Monthly</a></li>
 *     <li><hr class="dropdown-divider"></li>
 *     <li><h6 class="dropdown-header"><i class="bi bi-gear" aria-hidden="true"></i>Settings</h6></li>
 *     <li><a class="dropdown-item" href="#"><i class="bi bi-people" aria-hidden="true"></i>Users</a></li>
 *   </ul>
 *
 * Top navbar items follow the same rule: <a class="nav-link"><i class="bi bi-…" aria-hidden="true"></i>Label</a>.
 */

/* ---- Icons: fixed-width column so labels line up; size follows the item's level ---- */
.app-nav-main > .bi,
.app-nav-sub > .bi,
.app-dropdown .dropdown-item > .bi,
.app-dropdown .dropdown-header > .bi,
.navbar .nav-link > .bi {
    display: inline-block;
    width: 1.35em;
    margin-right: .5rem;
    text-align: center;
    flex-shrink: 0;
}
.app-nav-main > .bi { font-size: 1.05em; }
.app-nav-sub > .bi,
.app-dropdown .dropdown-item > .bi { font-size: .95em; opacity: .85; }

/* ---- Sidebar ---- */
.app-sidebar {
    background: var(--app-nav-bg);
    color: var(--app-nav-text);
    width: 16rem;
    padding: .75rem .5rem;
    overflow-y: auto;
}

.app-nav-main {
    display: flex;
    align-items: center;
    font-size: .95rem;
    font-weight: 600;
    color: var(--app-nav-text);
    padding: .45rem .75rem;
    text-decoration: none;
    white-space: nowrap;
}

.app-nav-items {
    margin: .1rem 0 .4rem 1.1rem;
    padding-left: .6rem;
    border-left: 1px solid rgba(185, 192, 200, .35);                                   /* fallback: iOS < 16.2 */
    border-left: 1px solid color-mix(in srgb, var(--app-nav-sub) 35%, transparent);   /* guide line */
}

.app-nav-sub {
    display: flex;
    align-items: center;
    font-size: .85rem;          /* always smaller than .app-nav-main */
    font-weight: 400;
    color: var(--app-nav-sub);
    padding: .28rem .6rem;
    border-radius: .3rem;
    text-decoration: none;
}

/* Hover only where there is a real pointer: on phones a tapped item would otherwise stay highlighted */
@media (hover: hover) {
    .app-nav-main:hover, .app-nav-sub:hover { color: #fff; background: rgba(255, 255, 255, .06); }
}
.app-nav-main:active, .app-nav-sub:active { background: rgba(255, 255, 255, .1); }            /* instant tap feedback */
.app-nav-sub.active, .app-nav-main.active { color: #fff; background: rgba(255, 255, 255, .12); }
.app-nav-sub.active > .bi { opacity: 1; }

.app-nav-divider {
    margin: .4rem .5rem;
    border: 0;
    border-top: 1px solid rgba(185, 192, 200, .3);                                     /* fallback: iOS < 16.2 */
    border-top: 1px solid color-mix(in srgb, var(--app-nav-sub) 30%, transparent);
    opacity: 1;
}

/* Collapsed sidebar: icons only, label kept for screen readers and as a tooltip (title="…" on the link) */
/* The "rail": the section minimised to a vertical bar of buttons (rail.js adds .collapsed; SKILL.md §20).
   Only logo, bell, chat, one icon per menu group/item, user, theme and the expand button stay; labels become
   tooltips. Sub-items are hidden: clicking a group icon expands the section and opens the group. */
.app-sidebar.collapsed { width: 3.5rem; }
@media (min-width: 992px) { .app-sidebar.collapsed { flex: 0 0 3.5rem; padding-left: .25rem; padding-right: .25rem; } }
.app-sidebar.collapsed .app-nav-main,
.app-sidebar.collapsed .app-nav-sub { font-size: 0; justify-content: center; padding-left: 0; padding-right: 0; }
.app-sidebar.collapsed .bi { font-size: 1.1rem; margin-right: 0; }
.app-sidebar.collapsed .app-nav-items { display: none; }
.app-sidebar.collapsed .app-nav-chevron { display: none; }
.app-sidebar.collapsed .app-nav-divider { margin: .3rem .5rem; }
.app-sidebar.collapsed .app-sidebar-head { flex-direction: column; padding: 0 0 .4rem; gap: .1rem; align-items: center; }
.app-sidebar.collapsed .app-sidebar-brand { display: none; }
.app-sidebar.collapsed .app-rail-label { display: none; }
.app-sidebar.collapsed .app-rail-toggle { justify-content: center; padding-left: 0; padding-right: 0; }
.app-sidebar.collapsed .app-footer-btn { justify-content: center; padding-left: .25rem; padding-right: .25rem; }
.app-sidebar.collapsed .app-footer-btn > .bi { margin-right: 0; }
.app-sidebar.collapsed .app-user-menu { flex: 0 0 auto; }

/* The minimise / expand button, above the bottom-left corner */
.app-rail-toggle {
    display: flex; align-items: center; gap: .4rem; width: 100%;
    background: none; border: 0; border-radius: .3rem; padding: .35rem .75rem;
    color: var(--app-nav-sub); font-size: .8rem; text-align: left;
}
.app-rail-toggle > .bi { font-size: 1rem; }
@media (hover: hover) { .app-rail-toggle:hover { color: #fff; background: rgba(255, 255, 255, .06); } }
.app-rail-toggle:active, .app-rail-toggle:focus-visible { color: #fff; background: rgba(255, 255, 255, .1); }
@media (pointer: coarse) { .app-rail-toggle { min-height: 44px; } }

/* An optional right-hand section (aside.html) follows the same rule, on the other edge */
.app-aside { flex: 0 0 20rem; width: 20rem; display: flex; flex-direction: column; min-width: 0;
             background: var(--app-surface); border-left: 1px solid var(--app-border); }
.app-aside-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem;
                  padding: .5rem .75rem; border-bottom: 1px solid var(--app-border); }
.app-aside-body { flex: 1 1 auto; overflow-y: auto; padding: .75rem; }
.app-aside-rail { display: none; flex-direction: column; align-items: center; gap: .25rem; padding: .5rem 0; }
.app-aside .app-rail-item { display: flex; align-items: center; justify-content: center; width: 44px; height: 44px;
                            border: 0; border-radius: .4rem; background: none; color: var(--app-text-muted); }
@media (hover: hover) { .app-aside .app-rail-item:hover { color: var(--app-text); background: var(--app-surface-2); } }
.app-aside.collapsed { flex-basis: 3.25rem; width: 3.25rem; }
.app-aside.collapsed .app-aside-body, .app-aside.collapsed .app-aside-title,
.app-aside.collapsed .app-rail-label { display: none; }
.app-aside.collapsed .app-aside-head { justify-content: center; padding: .5rem 0; border-bottom: 0; }
.app-aside.collapsed .app-aside-rail { display: flex; }
@media (max-width: 991.98px) { .app-aside { display: none; } }          /* phones: the aside's content moves into its own page */

/* ---- Dropdowns ---- */
.app-dropdown .dropdown-header {
    display: flex;
    align-items: center;
    font-size: .8rem;
    font-weight: 600;
    text-transform: none;
    color: var(--app-text);
}
.app-dropdown .dropdown-item {
    display: flex;
    align-items: center;
    font-size: .85rem;
    padding-left: 1.5rem;       /* indented under its header */
    color: var(--app-text-muted);
}
.app-dropdown .dropdown-item:hover,
.app-dropdown .dropdown-item.active { color: var(--app-text); }

/* ---- Collapsible groups (menu.js): title is a button, chevron turns when open ---- */
.app-sidebar { display: flex; flex-direction: column; }
.app-nav { flex: 1 1 auto; overflow-y: auto; }
.app-nav-toggle {
    width: 100%;
    background: none;
    border: 0;
    text-align: left;
    cursor: pointer;
    border-radius: .3rem;
}
.app-nav-chevron { margin-left: auto; font-size: .75em; opacity: .6; transition: transform .2s ease; }
.app-nav-group.open > .app-nav-toggle .app-nav-chevron { transform: rotate(180deg); }
.app-nav-items[hidden] { display: none; }
/* A group holding the current page gets a hint even if the user closes it */
.app-nav-group:has(.app-nav-sub.active):not(.open) > .app-nav-toggle { color: #fff; }

/* ---- Bottom-left corner: [user ▾] [theme ▾] ---- */
.app-sidebar-footer {
    display: flex;
    align-items: center;
    gap: .25rem;
    margin-top: .5rem;
    padding-top: .5rem;
    border-top: 1px solid rgba(185, 192, 200, .3);                                     /* fallback: iOS < 16.2 */
    border-top: 1px solid color-mix(in srgb, var(--app-nav-sub) 30%, transparent);
}
.app-user-menu { min-width: 0; flex: 1 1 auto; }      /* user name first, at the very left; takes the free width */
.app-theme-menu { flex: 0 0 auto; }
.app-footer-btn {
    display: flex;
    align-items: center;
    max-width: 100%;
    background: none;
    border: 0;
    color: var(--app-nav-sub);
    font-size: .85rem;
    padding: .35rem .5rem;
    border-radius: .3rem;
}
@media (hover: hover) { .app-footer-btn:hover { color: #fff; background: rgba(255, 255, 255, .08); } }
.app-footer-btn[aria-expanded="true"], .app-footer-btn:active { color: #fff; background: rgba(255, 255, 255, .08); }
.app-footer-btn > .bi { margin-right: .4rem; }
.app-footer-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.app-theme-status { font-size: .75rem; color: var(--app-text-muted); white-space: normal; max-width: 14rem; }
.app-dropdown .dropdown-item.active { background: var(--app-surface-2); color: var(--app-text); font-weight: 600; }
.app-sidebar.collapsed .app-footer-label { display: none; }
.app-sidebar.collapsed .app-sidebar-footer { flex-direction: column; }

/* ---- Settings tabs: one page per tab (SKILL.md §8) ---- */
.app-settings-tabs .nav-link > .bi { margin-right: .4rem; }

/* ---- Responsive shell (layout.html, SKILL.md §13) ----
 * ≥ 992 px: sidebar sticky on the left at full height. < 992 px: top bar + the sidebar as a slide-in panel
 * (Bootstrap offcanvas-lg). */
.app-shell { display: flex; min-height: 100vh; min-height: 100dvh; }            /* vh: fallback for iOS < 15.4 */
.app-main  { flex: 1 1 auto; min-width: 0; padding: 1rem; }        /* min-width:0 stops wide tables pushing the page sideways */
@media (min-width: 768px) { .app-main { padding: 1.5rem 2rem; } }

.app-sidebar.offcanvas-lg {
    background-color: var(--app-nav-bg) !important;                 /* Bootstrap makes offcanvas-lg transparent on lg+ */
    color: var(--app-nav-text);
}
@media (min-width: 992px) {
    .app-sidebar { position: sticky; top: 0; height: 100vh; height: 100dvh; flex: 0 0 16rem; }
}
@media (max-width: 991.98px) {
    .app-sidebar.offcanvas-lg {
        --bs-offcanvas-width: min(85vw, 20rem);
        --bs-offcanvas-bg: var(--app-nav-bg);
        --bs-offcanvas-color: var(--app-nav-text);
        width: var(--bs-offcanvas-width);
        height: 100vh;
        height: 100dvh;
        padding: calc(.75rem + env(safe-area-inset-top)) .5rem calc(.5rem + env(safe-area-inset-bottom))
                 calc(.5rem + env(safe-area-inset-left));
    }
    /* Phone / tablet: one step larger overall, same ratio */
    .app-nav-main { font-size: 1.05rem; padding: .6rem .9rem; }
    .app-nav-sub  { font-size: .95rem;  padding: .5rem .8rem; }
}
.app-offcanvas-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: .25rem; }
.app-offcanvas-head .app-nav-main { padding-left: .75rem; }

.app-topbar {
    position: sticky; top: 0; z-index: 1030;
    display: flex; align-items: center; gap: .5rem;
    background: var(--app-nav-bg); color: var(--app-nav-text);
    padding: calc(.35rem + env(safe-area-inset-top)) .75rem .35rem calc(.5rem + env(safe-area-inset-left));
}
.app-topbar-btn {
    background: none; border: 0; color: inherit; font-size: 1.6rem; line-height: 1;
    min-width: 44px; min-height: 44px; border-radius: .4rem;
}
.app-topbar-btn:hover, .app-topbar-btn:focus-visible { background: rgba(255, 255, 255, .08); }
.app-topbar-brand { color: inherit; text-decoration: none; font-weight: 600; display: block;
                    min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 0 1 auto;
                    margin-right: .25rem; }                              /* right next to the logo; shrinks, never grows */
.app-topbar-menu { margin-left: auto; flex: 0 0 auto; }               /* ☰ at the right end; the logo owns the left */

/* App logo: the very top-left corner of every screen (SKILL.md §17); everything else sits to its right */
.app-logo { display: inline-flex; align-items: center; justify-content: center; flex: 0 0 auto;
            min-width: 44px; min-height: 44px; border-radius: .4rem; }
.app-logo > img { width: 32px; height: 32px; display: block; }
.app-logo:focus-visible { outline: 2px solid var(--app-accent); outline-offset: 2px; }

/* Taps act at once (no double-tap-zoom delay), without the grey flash iOS/Android WebViews add */
a, button, .btn, [role="button"], summary, label { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }

/* Touch screens: every menu target at least 44 px high */
@media (pointer: coarse), (max-width: 991.98px) {      /* also a mouse on a narrow window: the slide-in phone menu */
    .app-nav-main, .app-nav-sub, .app-footer-btn, .app-dropdown .dropdown-item { min-height: 44px; }
}

/* Attention badge on a menu item (the dashboard's needs-attention counts, SKILL.md §21): <span class="app-nav-badge">3</span> */
.app-nav-badge { margin-left: auto; min-width: 1.25rem; padding: 0 .4rem; border-radius: 999px; background: var(--bs-danger); color: #fff; font-size: .7rem; line-height: 1.25rem; text-align: center; }
.app-sidebar.collapsed .app-nav-badge { position: absolute; top: .1rem; right: .1rem; margin: 0; }

/* Tables: resizable columns (assets/tables.js). Fixed layout is switched on by the script once widths are measured. */
table.app-resizable { table-layout: fixed; max-width: none; }
table.app-resizable th, table.app-resizable td { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
table.app-resizable td:has(.btn), table.app-resizable td:has(.badge) { text-overflow: clip; }
.app-col-grip { position: absolute; top: 0; right: 0; bottom: 0; width: 14px; cursor: col-resize; z-index: 2; touch-action: none; }   /* INSIDE the header cell: th has overflow:hidden, a handle sticking out would be clipped and hard to hit */
.app-col-grip::after { content: ""; position: absolute; top: 18%; bottom: 18%; right: 3px; width: 2px; border-radius: 1px; background: var(--bs-secondary-color); opacity: .55; }   /* always visible: this is the handle */
.app-col-grip:hover::after, .app-col-grip:focus-visible::after { background: var(--bs-primary); opacity: 1; width: 3px; }
html.app-col-busy, html.app-col-busy * { cursor: col-resize !important; user-select: none !important; }
@media (pointer: coarse) { .app-col-grip { width: 28px; } .app-col-grip::after { right: 12px; } }   /* a fat target for fingers */

/* Tables on phones (< 768 px): every table.table that tables.js touched becomes cards; <table data-no-cards> keeps a real table (scrolls sideways inside .table-responsive). */
@media (max-width: 767.98px) {
  table.app-cards thead { display: none; }
  table.app-cards, table.app-cards tbody, table.app-cards tr, table.app-cards td { display: block; width: 100%; }
  table.app-cards tr { margin-bottom: .6rem; padding: .35rem .75rem; border: 1px solid var(--bs-border-color); border-radius: .5rem; background: var(--bs-body-bg); }
  table.app-cards td { display: flex; justify-content: space-between; gap: 1rem; border: 0; padding: .3rem 0; text-align: right; white-space: normal; }
  table.app-cards td[data-label]::before { content: attr(data-label); flex: none; color: var(--bs-secondary-color); font-size: .8rem; text-align: left; }
  table.app-cards td:empty { display: none; }
  table.app-cards td:last-child:has(.btn) { justify-content: stretch; }
  table.app-cards td:last-child .btn { flex: 1; }
}

.app-table-tools:empty { display: none !important; }
.app-topbar-brand { min-height: 44px; display: inline-flex; align-items: center; }   /* the app name in the phone top bar is a 44 px target */

/* The docked notification window (full width, bottom) must not cover the fixed sidebar footer (user menu + theme dropdown) nor the phone menu's: leave its height free */
@media (min-width: 992px) { .app-sidebar { bottom: var(--app-notif-h, 0px); } }
.offcanvas.app-sidebar, #appSidebar.offcanvas { padding-bottom: var(--app-notif-h, 0px); }
