/**
 * Kili preview shells — navigation drawer + small-viewport chrome
 * design-spec/10 §10.5 (collapse < lg), §10.3 touch targets
 * Applies to: app-shell--drawer (shells 3, 4, 5, 5b), auth, topbar-only
 */

/* Skip link — design-spec/09 §9.3 */
.skip-to-main {
  position: fixed;
  top: var(--space-3);
  left: var(--space-4);
  z-index: 400;
  padding: var(--space-2) var(--space-4);
  background: var(--bg-surface);
  border: var(--border-width-default) solid var(--border);
  border-radius: var(--radius-md);
  font-size: 13px;
  font-weight: 500;
  color: var(--text-primary);
  text-decoration: none;
  box-shadow: var(--shadow-md);
  transform: translateY(calc(-100% - var(--space-8)));
  transition: transform var(--duration-base) var(--easing-enter);
}

.skip-to-main:focus {
  outline: none;
  box-shadow: var(--shadow-focus);
  transform: translateY(0);
}

@media (prefers-reduced-motion: reduce) {
  .skip-to-main {
    transition: none;
  }
}

/* ── Hamburger (44px tap area — design-spec/10 §10.3) ── */
.shell-nav-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  margin: calc(var(--space-2) * -1);
  margin-right: var(--space-2);
  padding: 0;
  border: none;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--text-primary);
  cursor: pointer;
  flex-shrink: 0;
}

.shell-nav-toggle:focus-visible {
  outline: none;
  box-shadow: var(--shadow-focus);
}

.shell-nav-toggle__bars {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 5px;
  width: 18px;
}

.shell-nav-toggle__bar {
  display: block;
  height: 2px;
  width: 100%;
  border-radius: var(--radius-pill);
  background: currentColor;
}

/* Overlay — tap closes drawer (§10.5) */
.shell-nav-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 100;
  border: none;
  padding: 0;
  margin: 0;
  width: 100%;
  height: 100%;
  background: var(--overlay-scrim);
  cursor: pointer;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition:
    opacity 150ms var(--easing-exit),
    visibility 0ms linear 150ms;
}

.shell-nav-overlay.is-visible {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transition:
    opacity 200ms var(--easing-enter),
    visibility 0ms linear 0ms;
}

@media (prefers-reduced-motion: reduce) {
  .shell-nav-overlay {
    transition: none;
  }
  .shell-nav-overlay.is-visible {
    transition: none;
  }
}

/* ═══ App shells with primary sidebar drawer ═══ */
@media (max-width: calc(var(--breakpoint-lg-min) - 1px)) {
  .app-shell--drawer .shell-nav-toggle {
    display: inline-flex;
  }

  .app-shell--drawer .shell-nav-overlay {
    display: block;
  }

  /* Topbar stays above scrim so menu control stays usable (drawer above both) */
  .app-shell--drawer .app-shell__topbar {
    z-index: 110;
  }

  .app-shell--drawer.app-shell--secondary .app-shell__secondary-nav {
    z-index: 108;
  }

  .app-shell--drawer.app-shell--dual .topbar {
    z-index: 110;
  }

  /* Shell 3 & 4: two-row chrome */
  .app-shell--drawer:not(.app-shell--secondary):not(.app-shell--dual) {
    grid-template-columns: 1fr;
    grid-template-rows: var(--topbar-height) 1fr;
    grid-template-areas:
      'topbar'
      'content';
  }

  .app-shell--drawer:not(.app-shell--secondary):not(.app-shell--dual) .app-shell__sidebar {
    grid-area: auto;
    position: fixed;
    left: 0;
    top: 0;
    width: min(88vw, var(--sidebar-width-open));
    max-width: 280px;
    height: 100vh;
    height: 100dvh;
    z-index: 200;
    transform: translate3d(-100%, 0, 0);
    transition: transform 150ms var(--easing-exit);
    box-shadow: var(--shadow-lg);
  }

  .app-shell--drawer:not(.app-shell--secondary):not(.app-shell--dual).app-shell--nav-open .app-shell__sidebar {
    transform: translate3d(0, 0, 0);
    transition: transform 200ms var(--easing-enter);
  }

  /* Shell 5: topbar + secondary + content */
  .app-shell--drawer.app-shell--secondary {
    grid-template-columns: 1fr;
    grid-template-rows: var(--topbar-height) var(--secondary-nav-height) 1fr;
    grid-template-areas:
      'topbar'
      'secondary-nav'
      'content';
  }

  .app-shell--drawer.app-shell--secondary .app-shell__sidebar {
    grid-area: auto;
    position: fixed;
    left: 0;
    top: 0;
    width: min(88vw, var(--sidebar-width-open));
    max-width: 280px;
    height: 100vh;
    height: 100dvh;
    z-index: 200;
    transform: translate3d(-100%, 0, 0);
    transition: transform 150ms var(--easing-exit);
    box-shadow: var(--shadow-lg);
  }

  .app-shell--drawer.app-shell--secondary.app-shell--nav-open .app-shell__sidebar {
    transform: translate3d(0, 0, 0);
    transition: transform 200ms var(--easing-enter);
  }

  .app-shell--drawer.app-shell--secondary .app-shell__secondary-nav {
    top: var(--topbar-height);
  }

  /* Shell 5b: flex column so fixed primary sidebar does not reserve a grid cell */
  .app-shell--drawer.app-shell--dual {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
  }

  .app-shell--drawer.app-shell--dual .primary-sidebar {
    position: fixed;
    left: 0;
    top: 0;
    width: min(88vw, var(--sidebar-width-open));
    max-width: 280px;
    height: 100vh;
    height: 100dvh;
    z-index: 200;
    transform: translate3d(-100%, 0, 0);
    transition: transform 150ms var(--easing-exit);
    box-shadow: var(--shadow-lg);
  }

  .app-shell--drawer.app-shell--dual.app-shell--nav-open .primary-sidebar {
    transform: translate3d(0, 0, 0);
    transition: transform 200ms var(--easing-enter);
  }

  .app-shell--drawer.app-shell--dual .topbar {
    flex: 0 0 auto;
  }

  .app-shell--drawer.app-shell--dual .secondary-sidebar {
    flex: 0 1 auto;
    position: relative;
    top: auto;
    height: auto;
    max-height: 40vh;
    min-height: var(--space-10);
    border-right: none;
    border-bottom: var(--border-width-default) solid var(--border);
  }

  .app-shell--drawer.app-shell--dual .content {
    flex: 1 1 auto;
    min-height: 0;
  }
}

@media (max-width: calc(var(--breakpoint-lg-min) - 1px)) and (prefers-reduced-motion: reduce) {
  .app-shell--drawer .app-shell__sidebar,
  .app-shell--drawer.app-shell--secondary .app-shell__sidebar,
  .app-shell--drawer.app-shell--dual .primary-sidebar {
    transition: none;
  }
}

/* Topbar icon actions: min gap 8px (§10.3), larger tap on narrow viewports */
@media (max-width: calc(var(--breakpoint-lg-min) - 1px)) {
  .topbar__right {
    gap: var(--space-2);
  }

  .topbar__action {
    width: 44px;
    height: 44px;
    flex-shrink: 0;
  }

  /* Shell 4: tooltips are hover/focus-only; hide pseudo tooltips on touch-narrow (avoid clipped flyouts) */
  .shell-preview--sidebar-collapsed .sidebar__nav-item::after {
    content: none;
  }
}

/* ── Shell 1: Auth — full-width card, tighter padding ── */
@media (max-width: calc(var(--breakpoint-lg-min) - 1px)) {
  .auth-shell {
    justify-content: flex-start;
    padding: var(--space-5) var(--space-4);
    padding-top: var(--space-8);
  }

  .auth-shell__card {
    max-width: none;
  }
}

/* ── Shell 6: Topbar-only — stepper wraps / scrolls ── */
@media (max-width: calc(var(--breakpoint-lg-min) - 1px)) {
  .focused-shell__topbar {
    flex-wrap: wrap;
    height: auto;
    min-height: var(--topbar-height);
    padding: var(--space-3) var(--space-4);
    gap: var(--space-2);
    align-items: flex-start;
  }

  .focused-shell__topbar .topbar__left {
    order: 1;
  }

  .focused-shell__topbar .topbar__right {
    order: 2;
    margin-left: auto;
  }

  .focused-shell__topbar .topbar__stepper {
    order: 3;
    position: static;
    transform: none;
    width: 100%;
    justify-content: flex-start;
    overflow-x: auto;
    padding: var(--space-2) 0 var(--space-1);
    gap: var(--space-2);
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
  }

  .focused-shell__content {
    padding: var(--space-5) var(--space-4);
  }

  .focused-shell__content-inner {
    max-width: none;
  }

  .focused-shell__action-bar {
    max-width: none;
    flex-direction: column-reverse;
    align-items: stretch;
    gap: var(--space-3);
  }

  .focused-shell__action-bar .btn-ghost,
  .focused-shell__action-bar .btn-primary {
    width: 100%;
    min-height: 44px;
  }

  .topbar__exit {
    min-height: 44px;
    padding: 0 var(--space-5);
  }
}
