/*
 * OfficePress shell: sidebar, rail, phone drawer, header, mode control and account menu.
 * Slot 8 in the stylesheet order (after components.css, before r005/app.css). Colour, spacing and size come
 * from tokens only; widths and heights come from --op-aside-w, --op-aside-rail-w, --op-aside-mobile-w,
 * --op-header-h and --op-header-h-mobile. r005 wireframe.css still draws the page bodies and loads before this
 * file; its 900px drawer rules are overridden here so the accepted breakpoint is 768px.
 *
 *   >= 768px   sidebar 260px, or rail 64px when collapsed (html[data-sidebar="collapsed"], set by /scripts/sidebar-state.js before first paint and kept by React); header 64px
 *   <  768px   drawer 300px limited to the viewport; header 56px
 */

/* The mode attribute sits on this element in the server HTML; it adds no box of its own. */
.mode-scope { display: contents; }

html { scroll-padding-top: calc(var(--op-header-h) + var(--op-space-2)); }

.app-shell {
  --shell-header: var(--op-header-h);
  /* r005 reads --wf-sidebar-width; the rail changes --op-aside-w on this element, so follow it here. */
  --wf-sidebar-width: var(--op-aside-w);
  min-height: 100vh;
}
html[data-sidebar="collapsed"] .app-shell { --op-aside-w: var(--op-aside-rail-w); }

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

.sidebar {
  z-index: 40;
  gap: var(--op-space-1);
  padding: var(--op-space-4) var(--op-space-3);
  overflow: hidden auto;
  border-right: 0;
  background: var(--op-nav);
  color: var(--op-nav-text);
}

.sidebar :focus-visible { outline-color: var(--op-focus-on-nav); }

.sidebar-footer { margin-top: auto; }
.app-suite { display: flex; flex-direction: column; gap: var(--op-space-1); padding: var(--op-space-3); color: var(--op-nav-text); font-weight: 700; }
.app-suite span { color: var(--op-nav-text-2); font-size: var(--wf-text-xs); font-weight: 400; }

.sidebar-top { display: flex; align-items: center; justify-content: space-between; gap: var(--op-space-2); padding-bottom: var(--op-space-3); }
.sidebar .brand { min-width: 0; min-height: var(--op-control-h); padding: 0 var(--op-space-1); color: var(--op-nav-text); font: var(--op-text-title); }
.brand-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.sidebar .nav-label {
  margin: var(--op-space-4) var(--op-space-3) var(--op-space-1);
  color: var(--op-nav-text-2);
  font: 700 11px/1.5 var(--op-font);
  letter-spacing: 1px;
}

.sidebar .nav-link {
  min-height: 44px;
  gap: var(--op-space-3);
  padding: 0 var(--op-space-3);
  border-radius: var(--op-radius-4);
  color: var(--op-nav-text-2);
  font-size: var(--wf-text-sm);
  font-weight: 400;
}
.sidebar .nav-link .icon { width: 18px; height: 18px; stroke-width: 1.75; }
.sidebar .nav-link[aria-current="page"] { background: var(--op-nav-active); color: var(--op-nav-text); font-weight: 700; }
.sidebar .nav-link[aria-current="page"] .icon { stroke-width: 2.25; }
@media (hover: hover) {
  .sidebar .nav-link:hover { background: var(--op-nav-field); color: var(--op-nav-text); text-decoration: none; }
  .sidebar .nav-link[aria-current="page"]:hover { background: var(--op-nav-active); }
}
.nav-text { min-width: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar .nav-count { flex: none; min-width: 22px; justify-content: center; margin-left: auto; }

/* ---------- Header ---------- */

.app-header {
  position: sticky;
  z-index: 20;
  top: 0;
  display: flex;
  align-items: center;
  gap: var(--op-space-3);
  min-width: 0;
  height: var(--op-header-h);
  padding: 0 var(--op-space-4);
  border-bottom: 1px solid var(--op-border);
  background: var(--op-surface);
  color: var(--op-text);
}
.app-header > .brand { display: none; min-width: 0; min-height: 0; padding: 0; color: var(--op-text); font: var(--op-text-title); }
.app-header-actions { display: flex; align-items: center; gap: var(--op-space-2); min-width: 0; margin-left: auto; }

.header-button {
  position: relative;
  display: inline-grid;
  flex: none;
  place-items: center;
  width: var(--op-control-h);
  height: var(--op-control-h);
  padding: 0;
  border: 1px solid transparent;
  border-radius: var(--op-radius-4);
  background: transparent;
  color: var(--op-text);
  cursor: pointer;
  transition-property: background-color, border-color, color;
  transition-duration: var(--op-dur-fast);
  transition-timing-function: var(--op-ease-out);
}
.header-button .icon { width: 18px; height: 18px; stroke-width: 1.75; }
@media (hover: hover) { .header-button:hover { background: var(--op-sunken); } }
.sidebar .header-button { color: var(--op-nav-text-2); }
@media (hover: hover) { .sidebar .header-button:hover { background: var(--op-nav-field); color: var(--op-nav-text); } }

/* Round, as the preview draws the globals. */
.mode-control { border-radius: var(--op-radius-full); border-color: var(--op-border-strong); background: var(--op-surface); }
/* Saving: still focusable (aria-disabled, not disabled) but visibly waiting. */
.mode-control[aria-disabled="true"] { cursor: progress; opacity: 0.65; }

.nav-toggle--phone, .drawer-close { display: none; }

/* ---------- Account menu ---------- */

.account-menu { position: relative; min-width: 0; }

.account-trigger {
  display: flex;
  align-items: center;
  gap: var(--op-space-2);
  max-width: 240px;
  height: 40px;
  padding: 0 var(--op-space-2) 0 var(--op-space-1);
  border: 1px solid var(--op-border-strong);
  border-radius: var(--op-radius-full);
  background: var(--op-surface);
  color: var(--op-text);
  font: inherit;
  cursor: pointer;
}
@media (hover: hover) { .account-trigger:hover { background: var(--op-sunken); } }
.account-trigger[aria-expanded="true"] { background: var(--op-tint); border-color: var(--op-accent); }
.account-trigger-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 700; }
.account-trigger-chevron .icon { width: 16px; height: 16px; color: var(--op-text-2); }

.account-popover {
  position: absolute;
  z-index: 50;
  top: calc(100% + var(--op-space-2));
  right: 0;
  display: flex;
  flex-direction: column;
  gap: var(--op-space-1);
  width: 280px;
  max-width: calc(100vw - var(--op-space-4));
  padding: var(--op-space-2);
  border-radius: var(--op-radius-12);
  background: var(--op-surface);
  box-shadow: var(--op-elev-pop);
}
.account-identity { display: flex; align-items: center; gap: var(--op-space-3); min-width: 0; padding: var(--op-space-2) var(--op-space-2) var(--op-space-3); border-bottom: 1px solid var(--op-border); margin-bottom: var(--op-space-1); }
.account-identity strong { min-width: 0; overflow-wrap: anywhere; }

.account-item {
  display: flex;
  align-items: center;
  gap: var(--op-space-3);
  min-height: 44px;
  padding: 0 var(--op-space-2);
  border-radius: var(--op-radius-4);
  color: var(--op-text);
  text-decoration: none;
}
.account-item .icon { width: 18px; height: 18px; color: var(--op-text-2); }
.account-item[aria-current="page"] { background: var(--op-tint); font-weight: 700; }
@media (hover: hover) { .account-item:hover { background: var(--op-sunken); text-decoration: none; } }

/* The popover sets its own display, which would override the hidden attribute of a closed menu. */
.account-popover[hidden] { display: none; }

/* Page bodies keep their sticky side panels clear of the sticky header. */
.category-list, .step-nav, .summary-aside, .detail-aside { top: calc(var(--shell-header) + var(--op-space-5)); }

/* ---------- 768px and wider: sidebar or rail ---------- */

@media (min-width: 768px) {
  .sidebar { width: var(--op-aside-w); transform: none; visibility: visible; box-shadow: none; }
  .main { margin-left: var(--op-aside-w); }
  .drawer-overlay { display: none; }
  .app-shell[data-settled="true"] .sidebar { transition: width var(--op-dur) var(--op-ease-out); }
  .app-shell[data-settled="true"] .main { transition: margin-left var(--op-dur) var(--op-ease-out); }

  /* Rail: icons only. Names stay in the DOM (visually hidden) so each link keeps its accessible name. */
  html[data-sidebar="collapsed"] .app-shell .sidebar { align-items: center; padding-inline: var(--op-space-3); }
  html[data-sidebar="collapsed"] .app-shell .sidebar-top { justify-content: center; }
  html[data-sidebar="collapsed"] .app-shell .sidebar .brand { padding: 0; }
  html[data-sidebar="collapsed"] .app-shell .brand-name,
  html[data-sidebar="collapsed"] .app-shell .nav-text {
    position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap;
  }
  html[data-sidebar="collapsed"] .app-shell .sidebar .nav-label,
  html[data-sidebar="collapsed"] .app-shell .sidebar-footer { display: none; }
  html[data-sidebar="collapsed"] .app-shell .sidebar .nav { justify-items: center; }
  html[data-sidebar="collapsed"] .app-shell .sidebar .nav ~ .nav { width: 100%; margin-top: var(--op-space-3); padding-top: var(--op-space-3); border-top: 1px solid var(--op-nav-border); }
  html[data-sidebar="collapsed"] .app-shell .sidebar .nav-link { position: relative; justify-content: center; width: 40px; min-height: 40px; padding: 0; }
  html[data-sidebar="collapsed"] .app-shell .sidebar .nav-count {
    position: absolute; top: 0; right: -4px; min-width: 18px; height: 18px; padding: 0 4px; font-size: 10px;
  }
}

/* ---------- Below 768px: drawer and 56px header ---------- */

@media (max-width: 767px) {
  .app-shell { --shell-header: var(--op-header-h-mobile); }
  html { scroll-padding-top: calc(var(--op-header-h-mobile) + var(--op-space-2)); }

  .sidebar {
    width: min(var(--op-aside-mobile-w), 100vw);
    max-width: 100vw;
    transform: translateX(-100%);
    /* Closed drawer links must not take focus, with or without scripts. */
    visibility: hidden;
    box-shadow: var(--op-elev-pop);
    transition: transform var(--op-dur) var(--op-ease-out), visibility 0s linear var(--op-dur);
  }
  .sidebar.is-open { transform: translateX(0); visibility: visible; transition-delay: 0s; }
  .drawer-overlay { z-index: 35; background: var(--op-scrim); }
  .main { margin-left: 0; }

  .app-header { gap: var(--op-space-1); height: var(--op-header-h-mobile); padding: 0 var(--op-space-3) 0 var(--op-space-1); }
  .app-header > .brand { display: flex; min-width: 0; }
  .nav-toggle--desktop { display: none; }
  .nav-toggle--phone, .drawer-close { display: inline-grid; }
  /* Thumb-sized targets in the bar. */
  .header-button { width: 44px; height: 44px; }
  .account-trigger { height: 44px; padding: 0 var(--op-space-1); }
  .account-trigger-name, .account-trigger-chevron { display: none; }
  .account-popover { position: fixed; top: calc(var(--op-header-h-mobile) + var(--op-space-1)); right: var(--op-space-2); left: auto; }
}

/* ---------- Print: the record prints without navigation ---------- */

@media print {
  .app-header, .drawer-overlay { display: none !important; }
  .main { margin-left: 0; transition: none !important; }
}
