/* Copley workflows (v3) - page layout (shell).
   Desktop (1024px and wider), no top bar:
     [sidebar, resizable 200-480px, collapsible to a 36px rail] |
     [middle page, scrolls, content 760px wide at most] |
     [diagram panel, resizable, collapsible to a 36px rail]
   with the Mark up toolbar floating at the bottom centre of the middle + diagram.
   Phones and tablets (body.cw-mobile): one area at a time and a bottom bar
   (Workflows / Page / Diagram), chosen by body[data-area]. */

#app {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
}

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

#sidebar {
  position: relative;
  z-index: 3;                       /* its edge tab sits over the page edge */
  flex: 0 0 var(--cw-side-w);
  width: var(--cw-side-w);
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--cw-surface-2);
  border-right: 1px solid var(--cw-border);
  font-size: var(--cw-fs);
}

#sidebar-content {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
  overflow-y: auto;
}

/* ---------- Drag handles ---------- */

.cw-resizer {
  z-index: 4;
  cursor: col-resize;
  touch-action: none;
}
.cw-resizer:focus-visible { outline: none; }
.cw-resizer::after {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  background: transparent;
  transition: background 0.1s ease;
}
.cw-resizer:hover::after,
.cw-resizer.is-dragging::after,
.cw-resizer:focus-visible::after { background: var(--cw-accent); }

/* On the sidebar's right edge (8px to grab, a 2px line shows on hover). */
#sidebar-resizer {
  position: absolute;
  top: 0;
  bottom: 0;
  right: -4px;
  width: 8px;
}
#sidebar-resizer::after { left: 3px; width: 2px; }

/* Between the page and the diagram: the 1px divider itself, grabbed a little to its right
   (so the page's scrollbar stays easy to grab). */
#diagram-resizer {
  position: relative;
  flex: 0 0 1px;
  width: 1px;
  background: var(--cw-border);
}
#diagram-resizer::before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: -2px;
  right: -6px;
}
#diagram-resizer::after { left: -1px; width: 3px; }

body.cw-resizing,
body.cw-resizing * {
  cursor: col-resize !important;
  -webkit-user-select: none !important;
  user-select: none !important;
}
body.cw-resizing iframe,
body.cw-resizing #diagram-root { pointer-events: none; }

/* ---------- Hide tab and rails ---------- */

/* Small tab on the sidebar's edge that hides it (the rail then brings it back). */
.cw-edge-tab {
  position: absolute;
  top: 50%;
  right: -14px;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 14px;
  height: 40px;
  margin-top: -20px;
  padding: 0;
  border: 1px solid var(--cw-border);
  border-left: 0;
  background: var(--cw-surface);
  color: var(--cw-muted);
}
.cw-edge-tab:hover {
  border-color: var(--cw-border-strong);
  background: var(--cw-accent-weak);
  color: var(--cw-accent);
}

/* A collapsed panel: a 36px strip with its name, the whole strip is the button. */
.cw-rail {
  display: none;
  flex: 0 0 var(--cw-rail-w);
  width: var(--cw-rail-w);
  flex-direction: column;
  align-items: center;
  gap: var(--cw-pad-3);
  padding: 10px 0;
  border: 0;
  background: var(--cw-surface-2);
  color: var(--cw-text);
  font-size: var(--cw-fs);
}
.cw-rail--left { border-right: 1px solid var(--cw-border); }
.cw-rail--right { border-left: 1px solid var(--cw-border); }
.cw-rail:hover { background: var(--cw-accent-weak); color: var(--cw-accent); }
.cw-rail svg { flex: none; }
/* Upright letters, one under the other (no sideways text to tilt your head for). */
.cw-rail-label {
  writing-mode: vertical-rl;
  text-orientation: upright;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: -0.5px;
  line-height: 1;
  text-transform: uppercase;
  white-space: nowrap;
}

/* The diagram's rail: a full-height accent strip reading "Show diagram" under a diagram
   picture, so a hidden diagram is easy to find. (js/core.js writes "Diagram" or
   "Diagram of P01" into the label; "Show " is put in front here.) */
#diagram-rail {
  flex: 0 0 40px;
  width: 40px;
  align-self: stretch;
  gap: 10px;
  padding: 12px 0;
  overflow: hidden;
  border: 0;
  border-left: 1px solid #1e40af;
  background: var(--cw-accent);
  color: #ffffff;
  cursor: pointer;
}
#diagram-rail:hover { background: #1e40af; color: #ffffff; }
#diagram-rail:focus-visible { outline: 3px solid var(--cw-text); outline-offset: -3px; }
#diagram-rail::before {
  content: '';
  flex: none;
  width: 22px;
  height: 22px;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 22 22' fill='none' stroke='white' stroke-width='1.8'%3E%3Crect x='1.5' y='2.5' width='8' height='6'/%3E%3Crect x='12.5' y='13.5' width='8' height='6'/%3E%3Cpath d='M5.5 8.5v8h7'/%3E%3C/svg%3E") center / 22px 22px no-repeat;
}
#diagram-rail svg { width: 14px; height: 14px; }
#diagram-rail .cw-rail-label { font-size: 12px; letter-spacing: 0; }
#diagram-rail .cw-rail-label::before { content: 'Show '; }

body:not(.cw-mobile).cw-side-collapsed #sidebar { display: none; }
body:not(.cw-mobile).cw-side-collapsed #sidebar-rail { display: flex; }

/* ---------- Workspace: middle page + diagram ---------- */

#workspace {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
}

/* The page column: the page scrolls; the Mark up toolbar sits under it (never on top of it). */
#page-col {
  position: relative;
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--cw-surface);
}

#middle {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  overflow-x: hidden;
  overflow-y: auto;
  background: var(--cw-surface);
  font-size: var(--cw-fs-page);
  -webkit-overflow-scrolling: touch;
  scroll-padding-top: 8px;
  container-type: inline-size;      /* css/page.css adapts the step rows to the page's width */
  container-name: page;
}
#middle:focus { outline: none; }

#middle-inner {
  max-width: 760px;
  margin: 0 auto;
  padding: 6px 20px 48px;
}

/* Slim header row on the left: Back (when there is somewhere to go back to) and where you are.
   #middle-head is the shell's; views draw the same row themselves with CW.ui.viewHead(). */
#middle-head,
.cw-viewhead {
  display: flex;
  align-items: center;
  gap: var(--cw-pad-3);
  min-width: 0;
  min-height: 30px;
  margin: 0 0 var(--cw-pad);
  color: var(--cw-muted);
  font-size: var(--cw-fs);
}
#middle-head.is-redundant { display: none; }   /* the view shows its own */

.cw-back {
  display: inline-flex;
  align-items: center;
  gap: var(--cw-pad);
  flex: none;
  min-height: 28px;
  padding: 0 var(--cw-pad-3) 0 var(--cw-pad-2);
  border: 1px solid var(--cw-border);
  background: var(--cw-surface);
  color: var(--cw-text);
  font-size: var(--cw-fs);
}
.cw-back:hover { background: var(--cw-hover); border-color: var(--cw-border-strong); }

.cw-crumbs {
  display: flex;
  align-items: center;
  gap: var(--cw-pad);
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  font-size: var(--cw-fs);
}
.cw-crumbs-item { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.cw-crumbs-item.is-last { color: var(--cw-text); }
.cw-crumbs-sep { flex: none; color: var(--cw-border-strong); }
.cw-crumbs-link {
  flex: 0 1 auto;
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 0;
  border: 0;
  background: none;
  color: var(--cw-muted);
  font: inherit;
  text-decoration: none;
  cursor: pointer;
}
.cw-crumbs-link:hover { color: var(--cw-accent); text-decoration: underline; }

#middle-body {
  position: relative;               /* page drawings are placed inside it */
  min-height: 40px;
}

/* ---------- Diagram panel ---------- */

#diagram-panel {
  position: relative;
  flex: 0 0 var(--cw-diag-w);
  min-width: 260px;
  max-width: calc(100% - 400px);     /* the page keeps at least 400px */
  display: flex;
  flex-direction: column;
  background: var(--cw-surface);
  isolation: isolate;
}

#diagram-root {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

body:not(.cw-mobile).cw-diag-collapsed #diagram-panel,
body:not(.cw-mobile).cw-diag-collapsed #diagram-resizer { display: none; }
body:not(.cw-mobile).cw-diag-collapsed #diagram-rail { display: flex; }

/* ---------- Mark up toolbar slot (docked under the page; js/markup.js fills it) ---------- */

#markup-root {
  position: relative;
  flex: none;
  z-index: 20;
  min-width: 0;
}
#markup-root:empty,
#markup-root[hidden] { display: none; }

/* ---------- Bottom bar (phones and tablets) ---------- */

#bottom-bar { display: none; }

body.cw-mobile #bottom-bar {
  position: relative;
  z-index: 30;
  display: flex;
  flex: 0 0 auto;
  height: calc(var(--cw-bottom-h) + env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  background: var(--cw-surface);
  border-top: 1px solid var(--cw-border);
}
#bottom-bar button {
  flex: 1 1 0;
  min-width: 0;
  padding: 0 var(--cw-pad);
  border: 0;
  border-top: 3px solid transparent;
  background: transparent;
  color: var(--cw-muted);
  font-size: 15px;
  font-weight: 600;
}
#bottom-bar button + button { border-left: 1px solid var(--cw-border); }
#bottom-bar button[aria-pressed="true"] {
  border-top-color: var(--cw-accent);
  background: var(--cw-accent-weak);
  color: var(--cw-accent);
}

/* ---------- Phones and tablets: one area at a time ---------- */

body.cw-mobile #stage { display: block; }

/* Every area stays laid out (keeps its size and scroll position); only the chosen one shows. */
body.cw-mobile #sidebar,
body.cw-mobile #workspace {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  width: auto;
}
body.cw-mobile #sidebar {
  z-index: 1;
  border-right: 0;
  visibility: hidden;
}
body.cw-mobile[data-area="workflows"] #sidebar { z-index: 3; visibility: visible; }
body.cw-mobile[data-area="workflows"] #workspace { visibility: hidden; }

/* The page column fills the area (the page scrolls, the Mark up strip sits at its foot); the
   diagram fills the same area but stops above the Mark up strip (--cw-mk-h, set by js/markup.js),
   so on the Diagram tab the strip shows under the diagram and covers nothing. */
body.cw-mobile #page-col,
body.cw-mobile #diagram-panel {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  width: auto;
  min-width: 0;
  max-width: none;
  flex: none;
}
body.cw-mobile #diagram-panel { bottom: var(--cw-mk-h, 0px); }
body.cw-mobile #page-col { z-index: 1; }
body.cw-mobile #diagram-panel { z-index: 0; }
body.cw-mobile[data-area="page"] #diagram-panel { visibility: hidden; }
body.cw-mobile[data-area="diagram"] #page-col { visibility: hidden; }
body.cw-mobile[data-area="diagram"] #markup-root { visibility: visible; }
body.cw-mobile[data-area="diagram"] #diagram-panel { z-index: 2; }

body.cw-mobile .cw-resizer,
body.cw-mobile .cw-edge-tab,
body.cw-mobile .cw-rail { display: none !important; }

body.cw-mobile #middle-inner { padding: 6px 12px 32px; }

body.cw-mobile .cw-back,
body.cw-mobile .cw-crumbs-link { min-height: var(--cw-tap); }

/* ---------- Stand-ins when a module did not load ---------- */

.cw-fallback-title { padding: var(--cw-pad-3); font-weight: 600; }
.cw-fallback-list { display: flex; flex-direction: column; }
.cw-fallback-row {
  display: flex;
  gap: var(--cw-pad-2);
  min-width: 0;
  padding: 3px var(--cw-pad-3);
  color: var(--cw-text);
}
.cw-fallback-row:hover { background: var(--cw-hover); text-decoration: none; }
.cw-fallback-code { flex: 0 0 34px; color: var(--cw-muted); font-variant-numeric: tabular-nums; }
.cw-fallback-name { min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.cw-fallback-home h1 { margin: 16px 0 8px; font-size: 22px; }
