/* ==========================================================================
   MKH - Application shell
   Visual language from the Claude Design project.
   Requires tokens.css, base.css and components.css.

   Sidebar states, all driven from <body>:
     (default)                  expanded rail, 252px
     data-sidebar="collapsed"   icon-only rail, 74px  (desktop choice, persisted)
     data-drawer="open"         off-canvas drawer     (below 900px only)
   ========================================================================== */

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

body[data-sidebar="collapsed"] .app { --rail-w: 74px; }

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

   Dark in BOTH themes. It is the one constantly-branded surface in the app,
   so it keeps its own --rail-* tokens rather than following the theme.
   -------------------------------------------------------------------------- */

.sidebar {
  position: sticky;
  top: 0;
  z-index: var(--z-rail);
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100svh;
  overflow: hidden;
  background: var(--rail-bg);
  border-right: 1px solid var(--rail-line);
  color: var(--rail-txt-2);
  transition: transform var(--dur-slow) var(--ease);
}

/* ---- Head ---------------------------------------------------------------- */

.sidebar__head {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 64px;
  padding: 0 14px;
  flex: none;
  border-bottom: 1px solid var(--rail-line-soft);
}

.sidebar__brand { display: flex; align-items: center; gap: 11px; min-width: 0; flex: 1; }
.sidebar__brand:hover { text-decoration: none; }

.sidebar__mark {
  width: 34px;
  height: 34px;
  flex: none;
  border-radius: 9px;
  box-shadow: 0 4px 14px -6px rgba(0, 0, 0, 0.8);
}

.sidebar__wordmark { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.sidebar__name {
  font-family: var(--font-display);
  font-size: 16px;
  font-weight: 800;
  letter-spacing: 0.07em;
  line-height: 1;
  color: var(--rail-txt);
}
.sidebar__name em { font-style: normal; color: var(--sky-300); }
.sidebar__sub {
  font-size: 8.5px;
  font-weight: var(--fw-bold);
  letter-spacing: 0.17em;
  text-transform: uppercase;
  color: var(--rail-txt-3);
  white-space: nowrap;
}

.sidebar__collapse {
  margin-left: auto;
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  flex: none;
  border-radius: var(--r-xs);
  color: var(--rail-txt-3);
  transition: background-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.sidebar__collapse:hover { background: var(--rail-hover); color: var(--rail-txt); }
.sidebar__collapse svg { width: 16px; height: 16px; transition: transform var(--dur-base) var(--ease); }
body[data-sidebar="collapsed"] .sidebar__collapse svg { transform: rotate(180deg); }

/* ---- Nav ----------------------------------------------------------------- */

.sidebar__nav {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 12px 10px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.nav__section { display: flex; flex-direction: column; gap: 2px; }

.nav__label {
  padding: 6px 10px 4px;
  font-size: 9.5px;
  font-weight: var(--fw-bold);
  letter-spacing: 0.17em;
  text-transform: uppercase;
  color: var(--rail-txt-3);
  white-space: nowrap;
}

.nav__item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 11px;
  height: 38px;
  padding: 0 10px;
  border-radius: var(--r-sm);
  font-size: var(--fs-base);
  font-weight: var(--fw-medium);
  color: var(--rail-txt-2);
  white-space: nowrap;
  transition: background-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.nav__item:hover { background: var(--rail-hover); color: var(--rail-txt); text-decoration: none; }
.nav__item svg { width: 18px; height: 18px; }

/* The active item is a soft green wash plus a green edge marker, rather than
   a solid indigo pill - it sits better against the rail's own gradient. */
.nav__item[aria-current="page"] {
  background: var(--rail-active);
  color: var(--rail-txt);
  font-weight: var(--fw-semibold);
}
.nav__item[aria-current="page"]::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  width: 2px;
  height: 20px;
  margin-top: -10px;
  border-radius: 0 2px 2px 0;
  background: var(--green-400);
}

.nav__text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }

.nav__count {
  flex: none;
  padding: 1px 7px;
  border-radius: var(--r-full);
  background: rgba(147, 152, 214, 0.16);
  font-family: var(--font-mono);
  font-size: var(--fs-2xs);
  font-weight: var(--fw-semibold);
}

/* ---- Foot ---------------------------------------------------------------- */

.sidebar__foot { flex: none; padding: 12px 14px 16px; border-top: 1px solid var(--rail-line-soft); }
.sidebar__org { display: flex; align-items: center; gap: 10px; }
.sidebar__org-text { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.sidebar__org-name {
  font-size: var(--fs-sm);
  font-weight: var(--fw-semibold);
  color: var(--rail-txt);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sidebar__org-meta { font-size: 10.5px; color: var(--rail-txt-3); white-space: nowrap; }

/* ---- Collapsed rail ------------------------------------------------------ */

body[data-sidebar="collapsed"] .sidebar__wordmark,
body[data-sidebar="collapsed"] .nav__label,
body[data-sidebar="collapsed"] .nav__text,
body[data-sidebar="collapsed"] .nav__count,
body[data-sidebar="collapsed"] .sidebar__org-text { display: none; }

body[data-sidebar="collapsed"] .sidebar__head { justify-content: center; padding: 0 var(--sp-2); }
body[data-sidebar="collapsed"] .sidebar__brand { flex: none; }
body[data-sidebar="collapsed"] .nav__item { justify-content: center; padding-left: 0; padding-right: 0; }
body[data-sidebar="collapsed"] .nav__section + .nav__section {
  margin-top: var(--sp-2);
  padding-top: var(--sp-2);
  border-top: 1px solid var(--rail-line-soft);
}
body[data-sidebar="collapsed"] .sidebar__org { justify-content: center; }

/* With no room left in the rail, the control becomes a handle straddling the
   edge. Circular and raised so it reads as a grip, not a clipped button. */
body[data-sidebar="collapsed"] .sidebar__collapse {
  position: absolute;
  right: -13px;
  top: 17px;
  z-index: 1;
  border-radius: var(--r-full);
  background: var(--indigo-800);
  border: 1px solid var(--rail-line);
  box-shadow: var(--sh-md);
}
body[data-sidebar="collapsed"] .sidebar__collapse:hover { background: var(--indigo-700); }

/* --------------------------------------------------------------------------
   Main column
   -------------------------------------------------------------------------- */

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

/* ---- Topbar -------------------------------------------------------------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: var(--z-topbar);
  display: flex;
  align-items: center;
  gap: 10px;
  height: 64px;
  flex: none;
  padding: 0 clamp(14px, 2.5vw, 26px);
  /* Frosted rather than opaque, so content scrolling under it stays hinted. */
  background: var(--surface-glass);
  backdrop-filter: blur(14px) saturate(1.4);
  -webkit-backdrop-filter: blur(14px) saturate(1.4);
  border-bottom: 1px solid var(--line);
}

.topbar__menu { display: none; }

.topbar__search { position: relative; display: flex; align-items: center; flex: 1; max-width: 420px; }
.topbar__search .field__icon { left: 11px; }
.topbar__search .input {
  height: 38px;
  padding-left: 36px;
  padding-right: 38px;
  font-size: var(--fs-md);
  border-radius: var(--r-full);
}

.topbar__kbd {
  position: absolute;
  right: 10px;
  display: grid;
  place-items: center;
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: var(--surface-3);
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--txt-3);
  pointer-events: none;
}

.topbar__actions { margin-left: auto; display: flex; align-items: center; gap: var(--sp-2); }
.topbar__sep { width: 1px; height: 22px; background: var(--line); }

.usermenu { position: relative; }
.usermenu__trigger {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  height: 34px;
  padding: 0 8px 0 4px;
  border: 1px solid var(--line);
  border-radius: var(--r-full);
  background: var(--surface-2);
  color: var(--txt-2);
  transition: border-color var(--dur-fast) var(--ease);
}
.usermenu__trigger:hover { border-color: var(--line-strong); }
.usermenu__trigger .avatar { width: 26px; height: 26px; border-radius: var(--r-full); font-size: 10.5px; }
.usermenu__name { font-size: var(--fs-md); font-weight: var(--fw-semibold); white-space: nowrap; }
.usermenu__trigger svg { width: 14px; height: 14px; color: var(--txt-3); }

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

.content {
  flex: 1;
  min-width: 0;
  padding: clamp(18px, 2.6vw, 30px) clamp(14px, 2.5vw, 30px) 64px;

  /* Page grids size themselves against this box, not the viewport - the rail
     is 178px wide when expanded, more than enough to change how many columns
     actually fit. */
  container-type: inline-size;
  container-name: content;
}

/* A measure cap: on a very wide monitor a full-bleed table is unreadable. */
.content__inner {
  max-width: 1400px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: clamp(16px, 2vw, 22px);
}

.page-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 18px;
  flex-wrap: wrap;
}
.page-head__text { min-width: 0; display: flex; flex-direction: column; gap: 7px; }
.page-head__title {
  font-family: var(--font-display);
  font-size: clamp(24px, 2.7vw, 32px);
  font-weight: var(--fw-bold);
  letter-spacing: -0.025em;
  line-height: 1.1;
}
.page-head__sub { font-size: var(--fs-base); color: var(--txt-2); max-width: 74ch; }
.page-head__actions { display: flex; align-items: center; gap: 9px; flex-wrap: wrap; }

.breadcrumb {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: var(--fs-sm);
  color: var(--txt-3);
}
.breadcrumb a { color: var(--txt-3); font-weight: var(--fw-medium); }
.breadcrumb a:hover { color: var(--primary); text-decoration: none; }
.breadcrumb svg { width: 12px; height: 12px; color: var(--line-strong); }

/* --------------------------------------------------------------------------
   Drawer scrim - only rendered below 900px
   -------------------------------------------------------------------------- */

.scrim {
  display: none;
  position: fixed;
  inset: 0;
  z-index: var(--z-scrim);
  background: rgba(9, 10, 26, 0.55);
  backdrop-filter: blur(2px);
  animation: mkh-fade 0.2s var(--ease);
}

/* --------------------------------------------------------------------------
   Responsive
   -------------------------------------------------------------------------- */

/* Narrow desktops start from the collapsed rail unless the user chose otherwise. */
@media (max-width: 1200px) {
  body:not([data-sidebar="expanded"]) .app { --rail-w: 74px; }
  body:not([data-sidebar="expanded"]) .sidebar__wordmark,
  body:not([data-sidebar="expanded"]) .nav__label,
  body:not([data-sidebar="expanded"]) .nav__text,
  body:not([data-sidebar="expanded"]) .nav__count,
  body:not([data-sidebar="expanded"]) .sidebar__org-text { display: none; }
  body:not([data-sidebar="expanded"]) .sidebar__head { justify-content: center; padding: 0 var(--sp-2); }
  body:not([data-sidebar="expanded"]) .nav__item { justify-content: center; padding-left: 0; padding-right: 0; }
  body:not([data-sidebar="expanded"]) .sidebar__org { justify-content: center; }

  body:not([data-sidebar="expanded"]) .sidebar__collapse {
    position: absolute;
    right: -13px;
    top: 17px;
    z-index: 1;
    border-radius: var(--r-full);
    background: var(--indigo-800);
    border: 1px solid var(--rail-line);
    box-shadow: var(--sh-md);
  }
  body:not([data-sidebar="expanded"]) .sidebar__collapse:hover { background: var(--indigo-700); }
}

/* Tablet and phone - the rail leaves the flow and becomes a drawer. */
@media (max-width: 900px) {
  .app { grid-template-columns: minmax(0, 1fr); }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: 272px;
    transform: translateX(-100%);
    box-shadow: var(--sh-lg);
  }
  body[data-drawer="open"] .sidebar { transform: translateX(0); }
  body[data-drawer="open"] .scrim { display: block; }
  body[data-drawer="open"] { overflow: hidden; }

  /* The drawer is always the full rail, whatever the desktop collapse state. */
  .sidebar__wordmark, .nav__label, .nav__text, .nav__count, .sidebar__org-text { display: flex !important; }
  .nav__label { display: block !important; }
  .sidebar__head { justify-content: flex-start !important; padding: 0 14px !important; }
  .nav__item { justify-content: flex-start !important; padding-left: 10px !important; padding-right: 10px !important; }
  .sidebar__org { justify-content: flex-start !important; }
  .sidebar__collapse { display: none; }

  .topbar__menu { display: grid; }
  .topbar__search { max-width: none; }
  .usermenu__name { display: none; }
}

@media (max-width: 620px) {
  .topbar { gap: var(--sp-2); padding: 0 14px; }
  .topbar__kbd { display: none; }
  .topbar__sep { display: none; }
}

/* Below this the search field crowds everything else out - move it to its own
   row under the topbar rather than shrinking it into uselessness. */
@media (max-width: 480px) {
  .topbar { flex-wrap: wrap; height: auto; padding-top: 10px; padding-bottom: 10px; }
  .topbar__search { order: 3; flex: 1 0 100%; }
  .topbar__actions { margin-left: auto; }
}
