/*
 * HxCore shell (M5 F2), ported from ../hxcore/admin/src/shell/shellLook.ts:
 * icon rail (one icon per area) | area sidebar with counters | topbar with ⌘K,
 * "+ Neu", theme switch, user menu | content. Up to 1024px the sidebar is an
 * overlay, up to 720px the rail too (hx.js toggles `.hx-shell--nav-open`).
 */
@layer components {
  .hx-shell {
    display: grid;
    min-height: 100vh;
    grid-template-columns: var(--rail-width) var(--sidebar-width) minmax(0, 1fr);
    background: var(--bg);
    color: var(--text);
  }

  .hx-skip {
    position: absolute;
    top: -60px;
    left: var(--space-3);
    z-index: var(--z-skip);
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-md);
    background: var(--accent);
    color: var(--on-accent);
    font-weight: 600;
  }

  .hx-skip:focus {
    top: var(--space-3);
    color: var(--on-accent);
  }

  /* ---------- Rail ---------- */

  .hx-rail {
    position: sticky;
    top: 0;
    z-index: var(--z-rail);
    display: flex;
    height: 100vh;
    flex-direction: column;
    align-items: center;
    gap: var(--space-1-5);
    padding: var(--space-4-5) 0;
    border-right: 1px solid var(--border);
    background: var(--bg);
    overflow-y: auto;
  }

  .hx-rail__brand {
    display: flex;
    flex: none;
    align-items: center;
    justify-content: center;
    width: var(--size-brand-box);
    height: var(--size-brand-box);
    margin-bottom: var(--space-4-5);
    border-radius: var(--space-2-5);
    background: var(--accent);
    color: var(--on-accent);
    font-family: var(--font-display);
    font-size: var(--size-brand);
    font-weight: 800;
    text-decoration: none;
  }

  .hx-rail__brand:hover {
    color: var(--on-accent);
  }

  .hx-rail__item {
    position: relative;
    display: flex;
    flex: none;
    align-items: center;
    justify-content: center;
    width: var(--size-rail-item);
    height: var(--size-rail-item);
    min-height: 0;
    padding: 0;
    border: 0;
    border-radius: var(--radius-lg);
    background: transparent;
    color: var(--text-subtle);
    cursor: pointer;
  }

  .hx-rail__item:hover:not(:disabled) {
    border-color: transparent;
    background: var(--surface-hover);
    color: var(--text);
  }

  .hx-rail__item[aria-pressed='true'] {
    background: var(--surface-active);
  }

  .hx-rail__item--current {
    color: var(--accent-text);
  }

  .hx-rail__item--current::before {
    content: '';
    position: absolute;
    top: var(--space-2-5);
    bottom: var(--space-2-5);
    left: calc(var(--space-2-5) * -1);
    width: var(--size-bar);
    border-radius: 0 var(--radius-full) var(--radius-full) 0;
    background: var(--accent-text);
  }

  .hx-rail__item--bottom {
    margin-top: auto;
  }

  .hx-rail__item .hx-icon {
    width: var(--size-icon-md);
    height: var(--size-icon-md);
  }

  /* Tooltip of a rail entry (data-hx-tip): the label next to the item on hover (after --tooltip-delay, only with a
     real pointer - no long-press tooltip on touch) and on keyboard focus. Fixed, so the scrolling rail does not clip
     it; hx.js sets --hx-tip-x/-y from the item's position. The button itself is named by aria-label. */
  .hx-rail__tip {
    position: fixed;
    top: var(--hx-tip-y, 0);
    left: var(--hx-tip-x, 0);
    z-index: var(--z-menu);
    padding: var(--space-1) var(--space-2-5);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--surface-active);
    box-shadow: var(--shadow-md);
    color: var(--text-strong);
    font-family: var(--font-sans);
    font-size: var(--size-small);
    font-weight: 600;
    line-height: 1.3;
    white-space: nowrap;
    pointer-events: none;
    opacity: 0;
    visibility: hidden;
    translate: 0 -50%;
    transition:
      opacity var(--transition-fast),
      visibility 0s linear var(--transition-fast);
  }

  @media (hover: hover) {
    [data-hx-tip]:hover > .hx-rail__tip {
      opacity: 1;
      visibility: visible;
      transition-delay: var(--tooltip-delay);
    }
  }

  [data-hx-tip]:focus-visible > .hx-rail__tip {
    opacity: 1;
    visibility: visible;
    transition-delay: var(--tooltip-delay);
  }

  /* ---------- Sidebar ---------- */

  .hx-sidebar {
    position: sticky;
    top: 0;
    z-index: var(--z-sidebar);
    display: flex;
    height: 100vh;
    flex-direction: column;
    gap: var(--space-1);
    padding: var(--space-5-5) var(--space-3-5);
    overflow-y: auto;
    border-right: 1px solid var(--border);
    background: var(--bg);
  }

  .hx-sidebar__area[hidden] {
    display: none;
  }

  .hx-sidebar__area {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
  }

  .hx-sidebar__title {
    margin: 0;
    padding: 0 var(--space-2-5) var(--space-3-5);
    font-family: var(--font-display);
    font-size: var(--size-h1);
    font-weight: 800;
    letter-spacing: var(--tracking-heading);
  }

  .hx-sidebar__section {
    padding: var(--space-5-5) var(--space-2-5) var(--space-2);
    font-size: var(--size-label);
    letter-spacing: var(--tracking-label);
    text-transform: uppercase;
    color: var(--text-subtle);
  }

  .hx-sidebar__link {
    display: flex;
    align-items: center;
    gap: var(--space-2-5);
    padding: 9px var(--space-2-5);
    border-radius: var(--radius-md);
    font-size: var(--size-body);
    color: var(--text-muted);
    text-decoration: none;
  }

  .hx-sidebar__link:hover {
    background: var(--surface-hover);
    color: var(--text);
  }

  .hx-sidebar__link > .hx-icon {
    color: var(--text-subtle);
  }

  .hx-sidebar__link:hover > .hx-icon,
  .hx-sidebar__link--active > .hx-icon {
    color: var(--accent-text);
  }

  .hx-sidebar__label {
    flex: 1;
    min-width: 0;
  }

  .hx-sidebar__link--active {
    background: var(--surface-active);
    font-weight: 600;
    color: var(--text-strong);
  }

  .hx-sidebar__count {
    font-family: var(--font-mono);
    font-size: var(--size-small);
    font-weight: 400;
    font-variant-numeric: tabular-nums;
    color: var(--text-subtle);
  }

  .hx-sidebar__count:empty {
    display: none;
  }

  .hx-scrim {
    display: none;
  }

  /* ---------- Main column + topbar ---------- */

  .hx-main {
    display: flex;
    min-width: 0;
    flex-direction: column;
  }

  .hx-topbar {
    position: sticky;
    top: 0;
    z-index: var(--z-topbar);
    display: flex;
    height: var(--header-height);
    align-items: center;
    gap: var(--space-3-5);
    padding: 0 var(--space-7);
    border-bottom: 1px solid var(--border);
    background: color-mix(in srgb, var(--bg) 92%, transparent);
    backdrop-filter: blur(4px);
  }

  .hx-topbar__toggle {
    display: none;
    width: var(--size-brand-box);
    height: var(--size-brand-box);
    min-height: 0;
    padding: 0;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-md);
    background: transparent;
    color: var(--text-muted);
  }

  .hx-topbar__tenant {
    display: flex;
    align-items: center;
    gap: var(--space-2-5);
    font-size: var(--size-body);
    font-weight: 600;
    white-space: nowrap;
  }

  .hx-topbar__instance {
    padding: var(--space-0-5) var(--space-1-5);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    font-family: var(--font-mono);
    font-size: var(--size-label);
    font-weight: 400;
    color: var(--text-subtle);
  }

  .hx-topbar__spacer {
    flex: 1;
  }

  .hx-topbar__search {
    display: flex;
    width: var(--width-search);
    height: var(--control-height);
    align-items: center;
    gap: var(--space-2-5);
    padding: 0 var(--space-3);
    border: 1px solid var(--border-strong);
    border-radius: var(--space-2-5);
    background: var(--surface);
    color: var(--text-subtle);
    font-size: var(--size-control);
    font-weight: 400;
    line-height: inherit;
    cursor: text;
  }

  .hx-topbar__search:hover:not(:disabled) {
    border-color: var(--border-stronger);
    background: var(--surface);
    color: var(--text-muted);
  }

  .hx-topbar__search-text {
    flex: 1;
    overflow: hidden;
    text-align: left;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .hx-topbar__new {
    display: inline-flex;
    height: var(--control-height);
    align-items: center;
    gap: var(--space-1-5);
    padding: 0 var(--space-4);
    border: 0;
    border-radius: var(--space-2-5);
    background: var(--accent);
    color: var(--on-accent);
    font-size: var(--size-body);
    font-weight: 600;
    line-height: inherit;
  }

  .hx-topbar__new:hover:not(:disabled) {
    background: color-mix(in srgb, var(--accent) 86%, var(--text-strong));
    color: var(--on-accent);
  }

  .hx-topbar__avatar {
    width: var(--size-avatar);
    height: var(--size-avatar);
    min-height: 0;
    padding: 0;
    border: 0;
    border-radius: var(--radius-full);
    background: var(--surface-active);
    color: var(--text);
    font-size: var(--size-small);
    font-weight: 600;
    line-height: inherit;
  }

  .hx-topbar__avatar:hover:not(:disabled) {
    background: var(--surface-active);
  }

  .hx-topbar__chat[aria-expanded='true'] {
    background: var(--surface-active);
  }

  /* ---------- Menus (disclosure: + Neu, user) ---------- */

  .hx-menu {
    position: relative;
  }

  .hx-menu__panel {
    position: absolute;
    top: calc(100% + var(--space-2));
    right: 0;
    z-index: var(--z-menu);
    display: flex;
    min-width: var(--width-menu);
    flex-direction: column;
    padding: var(--space-1-5);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-lg);
    background: var(--surface);
    box-shadow: var(--shadow-md);
  }

  .hx-menu__panel--left {
    right: auto;
    left: 0;
  }

  .hx-menu__label {
    padding: var(--space-1-5) var(--space-2-5) var(--space-1);
    font-size: var(--size-label);
    letter-spacing: var(--tracking-label);
    text-transform: uppercase;
    color: var(--text-subtle);
  }

  .hx-menu__item {
    display: flex;
    min-height: 0;
    align-items: center;
    justify-content: flex-start;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-2-5);
    border: 0;
    border-radius: var(--radius-md);
    background: transparent;
    color: var(--text);
    font-size: var(--size-body);
    font-weight: 400;
    line-height: inherit;
    text-align: left;
    text-decoration: none;
    width: 100%;
  }

  .hx-menu__item:hover:not(:disabled),
  .hx-menu__item:focus-visible {
    background: var(--surface-active);
    color: var(--text);
  }

  .hx-menu__item:focus-visible {
    outline: 2px solid var(--focus-color);
    outline-offset: -2px;
    box-shadow: inset var(--space-1) 0 0 var(--focus-color);
  }

  .hx-menu__who {
    display: flex;
    flex-direction: column;
    gap: var(--space-0-5);
    margin-bottom: var(--space-1);
    padding: var(--space-2) var(--space-2-5) var(--space-2-5);
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
  }

  .hx-menu__form {
    margin: 0;
  }

  /* ---------- Theme switch ---------- */

  .hx-theme-switch {
    display: inline-flex;
    gap: var(--space-0-5);
    padding: var(--size-bar);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-md);
  }

  .hx-theme-switch__option {
    display: inline-flex;
    width: var(--space-7);
    height: var(--size-tab);
    min-height: 0;
    align-items: center;
    justify-content: center;
    padding: 0;
    border: 0;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-subtle);
  }

  .hx-theme-switch__option:hover:not(:disabled) {
    background: transparent;
    color: var(--text);
  }

  .hx-theme-switch__option[aria-checked='true'] {
    background: var(--surface-active);
    color: var(--accent-text);
  }

  .hx-theme-switch__option:focus-visible {
    outline-offset: 1px;
  }

  /* ---------- Content ---------- */

  .hx-content {
    min-width: 0;
    flex: 1;
    padding: var(--space-7);
    outline: none;
  }

  .hx-messages:empty {
    display: none;
  }

  /* Lizenzhinweis über dem Seiteninhalt (Schonfrist, nur lesen). */
  .hx-license-banner {
    margin-bottom: var(--space-5);
    gap: var(--space-3);
  }

  .hx-messages {
    display: grid;
    gap: var(--space-2);
    margin-bottom: var(--space-5);
  }

  /* ---------- Auth (login without shell) ---------- */

  .hx-auth {
    display: flex;
    min-height: 100vh;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--space-6);
    padding: var(--space-6);
  }

  .hx-auth__card {
    width: 100%;
    max-width: 420px;
    padding: var(--space-8);
    border: 1px solid var(--border);
    border-radius: var(--radius-2xl);
    background: var(--surface);
  }

  .hx-auth-form {
    display: grid;
    gap: var(--space-4);
  }

  .hx-auth-form > .hx-btn {
    width: 100%;
  }

  /* ---------- ⌘K palette ---------- */

  .hx-palette__search {
    display: flex;
    height: var(--size-rail-item);
    align-items: center;
    gap: var(--space-3);
    padding: 0 var(--space-3);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-lg);
    background: var(--surface-sunken);
    color: var(--text-subtle);
  }

  .hx-palette__search:focus-within {
    border-color: var(--focus-color);
  }

  input.hx-palette__input {
    min-width: 0;
    min-height: 0;
    flex: 1;
    padding: 0;
    border: 0;
    background: transparent;
    color: var(--text);
    font-size: var(--size-body);
    box-shadow: none;
    outline: none;
  }

  input.hx-palette__input:focus-visible {
    box-shadow: none;
  }

  .hx-palette__list {
    max-height: var(--height-palette);
    margin-top: var(--space-3);
    overflow-y: auto;
  }

  /* U1: palette with preview pane (Schnellauskunft); narrow screens show the list only. */
  .hx-palette--preview {
    max-width: var(--width-dialog-xl, min(980px, calc(100vw - 32px)));
  }

  .hx-palette--preview .hx-palette__body {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1.15fr);
    gap: var(--space-4);
  }

  .hx-palette__preview {
    max-height: var(--height-palette);
    margin-top: var(--space-3);
    overflow-y: auto;
    padding-left: var(--space-4);
    border-left: 1px solid var(--border);
  }

  @media (max-width: 760px) {
    .hx-palette--preview .hx-palette__body { display: block; }
    .hx-palette__preview { display: none; }
  }

  .hx-palette__group-label {
    padding: var(--space-2) var(--space-3) var(--space-1);
    font-size: var(--size-label);
    letter-spacing: var(--tracking-label);
    text-transform: uppercase;
    color: var(--text-subtle);
  }

  .hx-palette__option {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: 9px var(--space-3);
    border-radius: var(--radius-md);
    color: var(--text-muted);
    cursor: pointer;
  }

  .hx-palette__option[aria-selected='true'] {
    background: var(--surface-active);
    color: var(--text);
    box-shadow:
      inset var(--size-bar) 0 0 var(--focus-color),
      inset 0 0 0 1px var(--focus-color);
  }

  .hx-palette__label {
    min-width: 0;
    flex: 1;
    font-size: var(--size-body);
  }

  .hx-palette__label--mono {
    flex: none;
    font-family: var(--font-mono);
    font-size: var(--size-mono);
    color: var(--text);
  }

  .hx-palette__meta {
    font-size: var(--size-small);
    color: var(--text-subtle);
  }

  .hx-palette__empty,
  .hx-palette__hint {
    margin: 0;
    padding: var(--space-4) var(--space-3);
    font-size: var(--size-control);
    color: var(--text-subtle);
  }

  .hx-palette__hint {
    padding: var(--space-3) 0 0;
    font-size: var(--size-small);
  }

  /* ---------- Narrow: sidebar overlay (≤ 1024px), rail overlay (≤ 720px) ---------- */

  @media (max-width: 64rem) {
    .hx-shell {
      grid-template-columns: var(--rail-width) minmax(0, 1fr);
    }

    .hx-sidebar {
      position: fixed;
      top: 0;
      bottom: 0;
      left: var(--rail-width);
      width: var(--sidebar-width);
      height: auto;
      box-shadow: var(--shadow-md);
      transition:
        translate 160ms ease,
        visibility 160ms ease;
    }

    .hx-shell:not(.hx-shell--nav-open) .hx-sidebar {
      visibility: hidden;
      translate: -110% 0;
    }

    .hx-shell--nav-open .hx-scrim {
      position: fixed;
      inset: 0;
      z-index: var(--z-scrim);
      display: block;
      background: var(--scrim);
    }

    .hx-topbar {
      padding: 0 var(--space-4);
    }

    .hx-topbar__toggle {
      display: inline-flex;
    }

    .hx-content {
      padding: var(--space-5) var(--space-4);
    }
  }

  @media (max-width: 45rem) {
    .hx-shell {
      grid-template-columns: minmax(0, 1fr);
    }

    /* Mobile menu: wider rail with the area label visible under each icon (no hover tooltips on touch). */
    .hx-shell {
      --rail-width: var(--rail-width-touch);
    }

    .hx-rail {
      position: fixed;
      top: 0;
      bottom: 0;
      left: 0;
      width: var(--rail-width);
      height: auto;
      transition:
        translate 160ms ease,
        visibility 160ms ease;
    }

    .hx-shell:not(.hx-shell--nav-open) .hx-rail {
      visibility: hidden;
      translate: -110% 0;
    }

    .hx-rail__item {
      width: calc(var(--rail-width) - var(--space-2));
      height: auto;
      min-height: var(--size-rail-item);
      flex-direction: column;
      gap: var(--space-0-5);
      padding: var(--space-1-5) var(--space-0-5);
    }

    .hx-rail__item > .hx-rail__tip {
      position: static;
      padding: 0;
      border: 0;
      background: none;
      box-shadow: none;
      color: inherit;
      font-size: var(--size-label);
      font-weight: 500;
      line-height: 1.15;
      text-align: center;
      white-space: normal;
      overflow-wrap: anywhere;
      opacity: 1;
      visibility: visible;
      translate: none;
      transition: none;
    }

    .hx-rail__brand > .hx-rail__tip {
      display: none;
    }

    .hx-topbar__tenant,
    .hx-topbar__search-text,
    .hx-topbar__search kbd,
    .hx-topbar__new-label {
      display: none;
    }

    .hx-topbar {
      gap: var(--space-2);
    }

    .hx-topbar__search {
      width: var(--control-height);
      justify-content: center;
      padding: 0;
    }

    .hx-topbar__new {
      padding: 0 var(--space-2-5);
    }
  }

  @media print {
    .hx-rail,
    .hx-sidebar,
    .hx-topbar,
    .hx-scrim {
      display: none;
    }

    .hx-shell {
      display: block;
    }
  }
}

/* ---------- KI-Chat-Leiste (M5 A5, früher chat.css): rechts fest, außerhalb des Seitenrasters ---------- */
@layer components {
  .hx-chat {
    position: fixed;
    right: var(--space-4);
    bottom: var(--space-4);
    z-index: calc(var(--z-menu) - 5);
  }

  .hx-chat__toggle {
    border-radius: var(--radius-full);
    box-shadow: var(--shadow-md);
  }

  .hx-chat--open .hx-chat__toggle {
    display: none;
  }

  .hx-chat__panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    display: flex;
    flex-direction: column;
    width: min(var(--chat-width, var(--width-chat)), 100vw);
    background: var(--surface);
    border-left: 1px solid var(--border);
    box-shadow: var(--shadow-lg);
  }

  .hx-chat__panel[hidden] {
    display: none;
  }

  /* Resizable: drag handle on the left edge; while open the page makes room (no overlap). */
  .hx-chat__resize {
    position: absolute;
    top: 0;
    bottom: 0;
    left: calc(var(--space-1) * -1);
    z-index: 1;
    width: var(--space-2);
    cursor: col-resize;
    touch-action: none;
  }

  .hx-chat__resize:hover,
  .hx-chat__resize:focus-visible {
    background: color-mix(in srgb, var(--accent) 60%, transparent);
    outline: none;
  }

  body.hx-chat-resizing {
    cursor: col-resize;
    user-select: none;
  }

  .hx-chat__head {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    min-height: var(--header-height);
    padding: 0 var(--space-3) 0 var(--space-4);
    border-bottom: 1px solid var(--border);
  }

  .hx-chat__title {
    flex: 1;
    margin: 0;
    font-family: var(--font-display);
    font-size: var(--size-h3);
    font-weight: 700;
  }

  .hx-chat__thread {
    flex: 1;
    overflow-y: auto;
    padding: var(--space-3) var(--space-4);
    background: var(--bg);
  }

  .hx-chat__form {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    padding: var(--space-3) var(--space-4);
    border-top: 1px solid var(--border);
  }

  .hx-chat__form textarea {
    resize: vertical;
  }

  .hx-chat__form textarea:disabled {
    opacity: 0.6;
  }

  .hx-chat__actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-2);
  }

  .hx-chat__busy {
    font-size: var(--size-small);
    color: var(--text-muted);
  }

  [data-chat-voice][aria-pressed='true'] {
    border-color: var(--danger);
    background: var(--danger-bg);
    color: var(--danger);
  }

  @media (min-width: 761px) {
    body.hx-chat-docked {
      padding-right: var(--chat-width, var(--width-chat));
    }
  }

  @media (max-width: 760px) {
    .hx-chat__panel {
      width: 100vw;
    }

    .hx-chat__resize {
      display: none;
    }
  }
}
