/* Pencilwick app shell: desktop rail (248px) → tablet icon rail (72px) → mobile top bar + tab bar.
   Markup lives in public/app.html; behaviour in public/js/app.js. */

:root {
  --rail-w: 248px;
  --topbar-h: 56px;
  --tabbar-h: 66px;
  --view-pad-x: var(--s-10);
  --view-pad-top: var(--s-10);
}
@media (max-width: 1023px) { :root { --rail-w: 72px; --view-pad-x: var(--s-8); --view-pad-top: var(--s-8); } }
@media (max-width: 759px)  { :root { --rail-w: 0px; --view-pad-x: var(--s-4); --view-pad-top: var(--s-5); } }

.shell {
  display: grid;
  grid-template-columns: var(--rail-w) minmax(0, 1fr);
  min-height: 100vh;
  min-height: 100dvh;
}

/* ===================================================================== Rail */
.rail {
  position: sticky; top: 0;
  height: 100vh; height: 100dvh;
  display: flex; flex-direction: column;
  padding: var(--s-6) var(--s-3) var(--s-4);
  border-right: 1px solid var(--line);
  background: var(--paper);
  overflow-y: auto; overflow-x: hidden;
  scrollbar-width: thin;
  z-index: 20;
}
.rail__brand { display: flex; align-items: center; padding: 0 var(--s-3); margin-bottom: var(--s-8); min-height: 44px; }
.rail__nav { display: grid; gap: 2px; list-style: none; padding: 0; }
.rail__foot { margin-top: auto; padding-top: var(--s-6); display: grid; gap: var(--s-3); }

.nav-item {
  position: relative;
  display: flex; align-items: center; gap: var(--s-3);
  min-height: 44px;
  padding: 0 var(--s-3);
  border-radius: var(--r-sm);
  color: var(--ink-2);
  font-weight: 600;
  text-decoration: none;
  transition: background-color var(--t-fast) ease, color var(--t-fast) ease;
}
.nav-item .icon { --icon-size: 21px; }
.nav-item:hover { background: var(--sheet-2); color: var(--ink); text-decoration: none; }
.nav-item:focus-visible { border-radius: var(--r-sm); }
.nav-item[aria-current="page"] { background: var(--gum-wash); color: var(--gum); }

.nav-item[aria-current="page"]::before {
  content: ""; position: absolute;
  left: calc(-1 * var(--s-3)); top: 9px; bottom: 9px; width: 3px;
  border-radius: 0 3px 3px 0;
  background: var(--gum);
}
.nav-item__label { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nav-item .badge { margin-left: auto; }
/* Snap keeps its camera in a small gum tile everywhere — the one tool with its own button */
.nav-item--snap .nav-item__icon {
  display: grid; place-items: center;
  width: 28px; height: 28px; margin: 0 -4px 0 -4px;
  border-radius: 7px;
  background: var(--gum); color: var(--on-gum);
}
.nav-item--snap .icon { --icon-size: 17px; stroke-width: 2; }
.nav-sep { height: 1px; background: var(--line); margin: var(--s-3) var(--s-3); border: 0; }

/* the user's name, like the label on the cover of an exercise book */
.me-btn {
  display: flex; align-items: center; gap: var(--s-3);
  width: 100%; min-height: 56px;
  padding: var(--s-2) var(--s-3);
  border-radius: var(--r-sm);
  text-align: left;
}
.me-btn:hover { background: var(--sheet-2); }
.me-btn:focus-visible { border-radius: var(--r-sm); }
.me-btn__text { flex: 1; min-width: 0; display: grid; line-height: 1.3; }
.me-btn__name { font-weight: 650; color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.me-btn__sub { font-size: var(--fs-xs); color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.me-btn > .icon { color: var(--ink-3); }

.rail .role-switch { margin: 0 var(--s-1); }

/* ---- tablet: icon rail ---- */
@media (max-width: 1023px) {
  .rail { padding: var(--s-5) var(--s-2) var(--s-3); align-items: center; }
  .rail__brand { padding: 0; justify-content: center; margin-bottom: var(--s-6); }
  .rail__brand .wordmark__text { display: none; }
  .rail__nav { justify-items: center; }
  .nav-item { width: 52px; height: 48px; padding: 0; justify-content: center; }
  .nav-item__label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  .nav-item[aria-current="page"]::before { left: -10px; }
  .nav-item .badge { position: absolute; top: 2px; right: 2px; margin: 0; min-width: 18px; height: 18px; font-size: 0.6875rem; padding: 0 4px; }
  .nav-item--snap .nav-item__icon { margin: 0; width: 32px; height: 32px; }
  .nav-sep { width: 32px; margin-inline: auto; }
  .rail__foot { justify-items: center; }
  .rail .role-switch { display: none; }
  .me-btn { width: 52px; min-height: 52px; padding: 0; justify-content: center; border-radius: 50%; }
  .me-btn__text, .me-btn > .icon { display: none; }
}

/* ===================================================================== Main view */
.main { min-width: 0; display: flex; flex-direction: column; }
.view {
  flex: 1;
  width: 100%;
  max-width: calc(1120px + 2 * var(--view-pad-x));
  padding: var(--view-pad-top) var(--view-pad-x) var(--s-16);
  min-width: 0;
}
.view:focus { box-shadow: none; }

/* page header: title, lead, actions — left-aligned on the paper */
.page-header {
  display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: end;
  gap: var(--s-3) var(--s-6);
  margin-bottom: var(--s-8);
}
.page-header__text { display: grid; gap: var(--s-2); min-width: 0; }
.page-header__title { font-size: var(--fs-4xl); letter-spacing: -0.03em; font-weight: 750; line-height: 1.04; }
.page-header__lead { color: var(--ink-2); font-size: var(--fs-lg); max-width: 58ch; line-height: 1.5; }
.page-header__meta { display: flex; flex-wrap: wrap; gap: var(--s-2); align-items: center; }
.page-header__actions { display: flex; flex-wrap: wrap; gap: var(--s-3); justify-content: flex-end; }
.back-link {
  display: inline-flex; align-items: center; gap: 2px;
  min-height: 36px; margin-left: -6px; padding-right: 6px;
  color: var(--ink-2); font-weight: 600; font-size: var(--fs-sm); text-decoration: none;
  justify-self: start;
}
.back-link:hover { color: var(--gum); }
@media (max-width: 1023px) { .page-header__title { font-size: var(--fs-3xl); } }
@media (max-width: 759px) {
  .page-header { grid-template-columns: minmax(0, 1fr); margin-bottom: var(--s-6); }
  .page-header__title { font-size: var(--fs-2xl); }
  .page-header__lead { font-size: var(--fs-md); }
  .page-header__actions { justify-content: flex-start; }
}

/* system views (404, no access, error) */
.system-view { display: grid; gap: var(--s-4); max-width: 52ch; padding-top: var(--s-6); }
.system-view .num { font-size: var(--fs-hero); font-weight: 760; line-height: 0.9; letter-spacing: -0.04em; color: var(--rule-strong); font-stretch: 85%; }

/* route loading skeleton */
.route-skeleton { display: grid; gap: var(--s-6); }
.route-skeleton__head { display: grid; gap: var(--s-3); }

/* ===================================================================== Mobile top bar */
.topbar { display: none; }
@media (max-width: 759px) {
  .shell { display: block; }
  .rail { display: none; }
  .topbar {
    position: sticky; top: 0; z-index: 30;
    display: flex; align-items: center; justify-content: space-between; gap: var(--s-3);
    height: calc(var(--topbar-h) + env(safe-area-inset-top));
    padding: env(safe-area-inset-top) var(--s-2) 0 var(--s-4);
    background: var(--paper);
    border-bottom: 1px solid transparent;
    transition: border-color var(--t-fast) ease;
  }
  .topbar.is-scrolled { border-bottom-color: var(--line); }
  .topbar__title-wrap { position: relative; flex: 1; min-width: 0; height: 100%; display: flex; align-items: center; }
  .topbar .wordmark { font-size: 1.375rem; transition: opacity var(--t-med) ease, transform var(--t-med) var(--ease-out); }
  .topbar__title {
    position: absolute; left: 0; right: 0;
    font-family: var(--font-display); font-weight: 700; font-size: var(--fs-lg); letter-spacing: -0.015em;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    opacity: 0; transform: translateY(6px);
    transition: opacity var(--t-med) ease, transform var(--t-med) var(--ease-out);
    pointer-events: none;
  }
  .topbar.show-title .wordmark { opacity: 0; transform: translateY(-6px); pointer-events: none; }
  .topbar.show-title .topbar__title { opacity: 1; transform: none; }
  .topbar .me-btn { width: 48px; min-height: 48px; padding: 0; justify-content: center; border-radius: 50%; }
  .topbar .me-btn__text, .topbar .me-btn > .icon { display: none; }
  .view { padding-bottom: calc(var(--tabbar-h) + var(--s-12) + env(safe-area-inset-bottom)); }
  :root { --toast-offset: calc(var(--tabbar-h) + 16px); }
}

/* ===================================================================== Mobile tab bar */
.tabbar { display: none; }
@media (max-width: 759px) {
  .tabbar {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr);
    height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
    padding: 0 var(--s-1) env(safe-area-inset-bottom);
    background: var(--sheet);
    border-top: 1px solid var(--line);
  }
}
.tab-item {
  position: relative;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  min-width: 0; padding-top: 2px;
  color: var(--ink-2);
  font-size: 0.75rem; font-weight: 600; line-height: 1.1;
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
}
.tab-item__icon {
  display: grid; place-items: center;
  width: 56px; height: 30px; border-radius: 15px;
  transition: background-color var(--t-fast) ease;
}
.tab-item .icon { --icon-size: 22px; }
.tab-item__label { max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tab-item[aria-current="page"] { color: var(--gum); }
.tab-item[aria-current="page"] .tab-item__icon { background: var(--gum-wash); }
.tab-item:focus-visible { box-shadow: inset 0 0 0 2px var(--wattle-deep); border-radius: var(--r-sm); }
.tab-item .badge { position: absolute; top: 4px; left: calc(50% + 6px); min-width: 18px; height: 18px; font-size: 0.6875rem; padding: 0 4px; }

/* the raised Snap button */
.tab-item--snap { justify-content: flex-end; padding-bottom: 9px; color: var(--ink); }
.tab-item--snap .tab-item__icon {
  position: absolute; left: 50%; top: -20px;
  width: 60px; height: 60px; border-radius: 50%;
  transform: translateX(-50%);
  background: var(--gum); color: var(--on-gum);
  box-shadow: 0 0 0 5px var(--sheet), 0 0 0 6px var(--line), inset 0 -3px 0 rgb(0 0 0 / 0.18);
  transition: transform var(--t-fast) ease, background-color var(--t-fast) ease;
}
.tab-item--snap .icon { --icon-size: 27px; stroke-width: 1.9; }
.tab-item--snap:active .tab-item__icon { transform: translateX(-50%) translateY(1px) scale(0.97); }
.tab-item--snap[aria-current="page"] { color: var(--gum); }
.tab-item--snap[aria-current="page"] .tab-item__icon { background: var(--gum-hover); }
.tab-item--snap:focus-visible { box-shadow: none; }
.tab-item--snap:focus-visible .tab-item__icon { box-shadow: 0 0 0 4px var(--sheet), 0 0 0 7px var(--wattle-deep); }
/* hide the line under the button where it meets the bar edge */
.tab-item--snap::before {
  content: ""; position: absolute; left: 50%; top: -1px; width: 58px; height: 2px; transform: translateX(-50%);
  background: var(--sheet);
}

/* "More" sheet on mobile */
.more-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--s-2); }
.more-grid .nav-item {
  width: auto; height: auto; min-height: 56px; padding: 0 var(--s-4); justify-content: flex-start;
  border: 1px solid var(--line); background: var(--sheet); color: var(--ink);
}
.more-grid .nav-item__label { position: static; width: auto; height: auto; overflow: hidden; clip: auto; }
.more-grid .nav-item[aria-current="page"] { background: var(--gum-wash); border-color: var(--gum); color: var(--gum); }
.more-grid .nav-item[aria-current="page"]::before { display: none; }
.more-grid .nav-item .badge { position: static; }

@media (min-width: 1024px) { .menu--rail .menu__section:has(> .menu__roles) { display: none; } }

/* ===================================================================== Bare layout (welcome, log in, sign up) */
body[data-layout="bare"] .shell { display: block; }
body[data-layout="bare"] .rail,
body[data-layout="bare"] .tabbar { display: none; }
body[data-layout="bare"] .topbar { display: none; }
.bare-head { display: none; }
body[data-layout="bare"] .bare-head {
  display: flex; align-items: center; justify-content: space-between;
  width: 100%; max-width: calc(1120px + 2 * var(--view-pad-x));
  padding: var(--s-6) var(--view-pad-x) 0;
}
body[data-layout="bare"] .view { padding-bottom: var(--s-16); }

/* ===================================================================== Boot state (before JS) */
.boot { display: grid; gap: var(--s-6); }
body:not([data-ready]) .rail__nav:empty { min-height: 300px; }

/* ===================================================================== Print */
@media print {
  .rail, .topbar, .tabbar, .toasts, .skip-link { display: none !important; }
  .shell { display: block; }
  .view { padding: 0; max-width: none; }
  .page { box-shadow: none; border: 1px solid #ccc; }
}
