/* KSM's shell: the wordmark and the nav of templates/partials/ksm_nav.html.
 * - The items live in one <details class="ksm-menu">. That is the Menu everywhere by default: a button that drops a
 *   panel of items under it, with no script, so it also works under a strict Content-Security-Policy. From 1024 px
 *   wide, in a browser that knows ::details-content, the button hides and the items show as one row, as before:
 *   beside the wordmark from 1280 px, on a second line under it below that. (Wordmark and row need about 1,120 px:
 *   they fit Pico's 1200 px container, not the 950 px one below 1280 px.)
 *   ksm-shell.js, deferred from base.html, closes the open Menu on Escape, a click outside and focus moving on.
 * - Tessera tokens only (tessera-tokens.css): no colour, font, shadow or radius is written here.
 * - It needs neither Pico nor tessera.css, and where they load too (base.html) its selectors outrank their nav,
 *   details and button rules, so a page that loads only the tokens and this file gets the same bar.
 * - Every target is at least 44 px each way. Keyboard focus is a 2 px accent ring, 3:1 or better on every surface.
 * - Printing leaves the nav out. */

.ksm-nav {
  display: flex; flex-wrap: nowrap; align-items: center; justify-content: space-between; gap: .5rem 1rem;
  padding-top: 1rem; padding-bottom: .75rem;
  font-family: var(--font-body);
}

/* Wordmark. On a very narrow phone it gives way first: the eyebrow wraps, the Menu button keeps its size. */
.ksm-nav .wordmark {
  flex: 0 1 auto; min-width: 0; display: inline-block; min-height: 44px; padding: 2px 0; border-radius: var(--radius-sm);
  font-family: var(--font-display); font-size: 26px; font-weight: 600; letter-spacing: -0.005em; line-height: 1;
  color: var(--ink); text-decoration: none;
}
.ksm-nav .wordmark:hover { color: var(--accent); }
.ksm-nav .eyebrow {
  display: block; margin-top: 4px;
  font-family: var(--font-mono); font-size: 10px; letter-spacing: .18em; line-height: 1.4;
  text-transform: uppercase; color: var(--ink-mute);
}

/* The Menu button. Pico colours a summary through its accordion variables; they point at the same tokens here. */
.ksm-nav .ksm-menu {
  flex: none; position: relative; margin: 0; padding: 0; border: 0;
  --pico-accordion-close-summary-color: var(--ink);
  --pico-accordion-active-summary-color: var(--ink);
  --pico-accordion-open-summary-color: var(--accent-ink);
}
.ksm-nav .ksm-menu > summary {
  display: inline-flex; align-items: center; justify-content: center; gap: .45rem;
  min-width: 44px; min-height: 44px; margin: 0; padding: 0 .85rem;
  list-style: none; cursor: pointer; user-select: none; -webkit-user-select: none;
  font-family: var(--font-body); font-size: 14px; font-weight: 500; line-height: 1; color: var(--ink);
  background: var(--panel-tint); border: 1px solid var(--border); border-radius: var(--radius-md);
}
.ksm-nav .ksm-menu > summary::-webkit-details-marker { display: none; }
.ksm-nav .ksm-menu > summary::after { content: none; display: none; }
.ksm-nav .ksm-menu > summary:hover { background: var(--panel-hi); border-color: var(--ink-subtle); }
.ksm-nav .ksm-menu[open] > summary { color: var(--accent-ink); background: var(--accent-softer); border-color: var(--accent-soft); }
.ksm-nav .ksm-menu > summary svg { flex: none; }
.ksm-nav .ksm-menu .ksm-icon-close, .ksm-nav .ksm-menu[open] .ksm-icon-open { display: none; }
.ksm-nav .ksm-menu[open] .ksm-icon-close { display: inline; }

/* The items: a panel under the button, one row per item */
.ksm-nav .ksm-menu > ul {
  position: absolute; z-index: 10; top: calc(100% + 6px); right: 0;
  width: calc(100vw - 2rem); max-width: 24rem; max-height: calc(100vh - 7rem); max-height: calc(100dvh - 7rem); overflow-y: auto;
  display: flex; flex-direction: column; align-items: stretch; gap: 2px; margin: 0; padding: .4rem; list-style: none;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius-lg); box-shadow: var(--shadow-lg);
}
.ksm-nav .ksm-menu li { display: block; margin: 0; padding: 0; list-style: none; }
.ksm-nav .ksm-menu li::before { content: none; }
.ksm-nav .ksm-menu a {
  display: flex; align-items: center; min-width: 44px; min-height: 44px; margin: 0; padding: 0 .8rem 0 calc(.8rem - 3px);
  border: 0; border-left: 3px solid transparent; border-radius: var(--radius-md); white-space: nowrap; text-decoration: none;
  font-family: var(--font-body); font-size: 15px; font-weight: 500; line-height: 1.2; color: var(--ink-dim);
}
.ksm-nav .ksm-menu a:hover { color: var(--accent-ink); background: var(--panel-tint); }
.ksm-nav .ksm-menu a[aria-current="page"] {
  color: var(--accent-ink); font-weight: 600; background: var(--accent-softer); border-left-color: var(--accent);
}
.ksm-nav .ksm-menu form { margin: 0; }
.ksm-nav .ksm-signout-item { margin-top: .3rem; padding-top: .4rem; border-top: 1px solid var(--border-soft); }
/* The person's account link, above Sign out in the panel and beside it in the row: a Lucide user icon and the name,
 * and in the panel a second line saying what the page holds, since touch screens never show its title. Each line
 * ends in an ellipsis rather than widening the panel or pushing Sign out off the row (the title has the name whole). */
.ksm-nav .ksm-menu .ksm-signout-item { display: flex; flex-direction: column; align-items: stretch; gap: 2px; }
.ksm-nav .ksm-menu a.ksm-account {
  display: grid; grid-template-columns: auto minmax(0, 1fr); align-items: center; column-gap: .6rem; row-gap: 2px;
  padding-top: .35rem; padding-bottom: .35rem;
}
.ksm-nav .ksm-account svg { grid-row: span 2; }
.ksm-nav .ksm-account-name, .ksm-nav .ksm-account-hint { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ksm-nav .ksm-account-hint { font-size: 12.5px; font-weight: 400; color: var(--ink-dim); }
.ksm-nav .ksm-signout {
  display: inline-flex; align-items: center; justify-content: center; width: 100%; min-height: 44px; margin: 0; padding: 0 .9rem;
  font-family: var(--font-body); font-size: 15px; font-weight: 500; line-height: 1.2; white-space: nowrap; cursor: pointer;
  color: var(--ink); background: var(--panel-tint); border: 1px solid var(--border); border-radius: var(--radius-md); box-shadow: none;
}
.ksm-nav .ksm-signout:hover { color: var(--ink); background: var(--panel-hi); border-color: var(--ink-subtle); }

/* Keyboard focus on everything in the bar */
.ksm-nav .wordmark:focus-visible, .ksm-nav .ksm-menu a:focus-visible,
.ksm-nav .ksm-menu > summary:focus-visible, .ksm-nav .ksm-menu .ksm-signout:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px; box-shadow: none;
}

/* Wide screens: the row. From 1280 px it sits beside the wordmark. From 1024 px to 1279 px (an iPad in landscape, a
 * laptop window, KSM in HA's sidebar) Pico's container is 950 px, too narrow for both on one line, so the row takes
 * a second line under the wordmark rather than hiding behind the Menu. Below 1024 px it is the Menu. */
@media (min-width: 1024px) {
  @supports selector(::details-content) {
    .ksm-nav .wordmark { flex: none; }
    .ksm-nav .ksm-menu { flex: 0 1 auto; min-width: 0; }
    .ksm-nav .ksm-menu > summary { display: none; }
    .ksm-nav .ksm-menu::details-content { display: block; content-visibility: visible; }
    .ksm-nav .ksm-menu > ul {
      position: static; z-index: auto; width: auto; max-width: none; max-height: none; overflow: visible;
      flex-direction: row; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 0;
      padding: 0; background: none; border: 0; border-radius: 0; box-shadow: none;
    }
    .ksm-nav .ksm-menu a { justify-content: center; padding: 0 .5rem; border-left: 0; font-size: 13px; background: none; }
    .ksm-nav .ksm-menu a:hover { background: none; }
    .ksm-nav .ksm-menu a.minor { font-size: 12px; }
    .ksm-nav .ksm-menu a[aria-current="page"] {
      background: none;
      text-decoration: underline; text-decoration-thickness: 2px; text-underline-offset: 6px; text-decoration-color: var(--accent);
    }
    .ksm-nav .ksm-signout-item { margin: 0 0 0 .5rem; padding: 0; border: 0; }
    .ksm-nav .ksm-menu .ksm-signout-item { flex-direction: row; align-items: center; gap: .25rem; }
    /* The row has about 213 px for the account link and Sign out (an admin's twelve items, 1200 px container): icon
     * and name within 8rem keep it one line */
    .ksm-nav .ksm-menu a.ksm-account { display: flex; column-gap: .3rem; max-width: 8rem; padding-top: 0; padding-bottom: 0; }
    .ksm-nav .ksm-account svg { flex: none; }
    .ksm-nav .ksm-account-hint { display: none; }
    .ksm-nav .ksm-signout { width: auto; font-size: 13px; }
  }
}
@media (min-width: 1024px) and (max-width: 1279.98px) {
  @supports selector(::details-content) {
    .ksm-nav { flex-wrap: wrap; row-gap: .25rem; }
    .ksm-nav .ksm-menu { flex: 1 1 100%; }
    /* The second line is 950 px: an admin's twelve items, the account link with a long name and Sign out fit on it
     * with a little less room around each item (targets stay 44 px wide at least). */
    .ksm-nav .ksm-menu a { padding: 0 .35rem; }
  }
}

@media print {
  .ksm-nav { display: none; }
}
