@layer base {
  /* Layout Shell */
  .app-layout {
    display: flex;
    min-height: 100dvh;
    width: 100%;
  }

  /* 
     * Global Layout Skeleton
     * Uses ch units for optimal reading metrics and min() to avoid media queries.
     * Incorporates env() to safely wrap content away from hardware notches.
     */

  .main {
    width: 100%;
    --layout-max-width: 100ch;

    max-inline-size: min(var(--layout-max-width), 100%);
    margin-inline: auto;

    /* Responsive padding that respects iPhone/iOS safe areas. */
    padding-inline: max(var(--space-4), env(safe-area-inset-left))
      max(var(--space-4), env(safe-area-inset-right));
    padding-block: max(var(--space-4), env(safe-area-inset-top))
      max(var(--space-4), env(safe-area-inset-bottom));
  }

  /* Modifier for more width (~1200px+) */
  .main.layout-wide {
    --layout-max-width: 150ch;
  }

  /*  Modifier for narrow width (~400-500px) */
  .main.layout-narrow {
    --layout-max-width: 40ch;
  }

  .main.layout-full {
    max-inline-size: 100%;
    margin-inline: 0;
    padding: 0;
    height: 100dvh;
    overflow: hidden;
  }

  /* Styling for workspace layout */
  .workspace-layout {
    display: flex;
    width: 100%;
    height: 100%;
    overflow: hidden;
  }

  /* The width dragged at the line next to the column (--column-width, see ColumnsHelper), else on the left a share of
     the window (the outline, the sources). A column never takes so much that the main column becomes unusable in a
     narrow window. */
  .workspace-column {
    width: var(--column-width, 16%);
    min-width: 200px;
    max-width: 45%;
    flex-shrink: 0;
    height: 100%;
    background-color: var(--color-canvas); /* both sides alike, expanded or collapsed */
  }

  /* On the right the citations, on the citations page with their form, wider than the columns on the left (Niklas,
     1.12.: the citations were too narrow, the sidebars on the left can do with less); the rest to the PDF or the text,
     which needs no more than its own width on Write. In a narrower window at most 40 %, so the text or the PDF keeps
     its room. */
  .workspace-column--right {
    width: var(--column-width, min(30rem, 40%));
  }

  .column-header {
    background-color: var(--color-surface-raised);
    border-bottom: var(--border);
  }

  .column-title {
    font-size: var(--text-large);
    font-weight: var(--font-weight-bold);
  }

  /* Collapsed: a strip with the toggle on top and the title upright in its middle. A click anywhere expands the
     column (column_controller#expand); on hover the strip turns darker gray, as a .btn-action. */
  .workspace-column.is-column-collapsed {
    min-width: 0;
    width: max-content;
    cursor: pointer;

    /* As high as the strip, so the title can sit in its middle */
    .column-header {
      flex-direction: column;
      height: 100%;
      background-color: transparent;
      border-bottom: 0;
    }

    .column-toggle {
      order: -1;
      rotate: 180deg;
    }

    /* Read from bottom to top, in the middle of the strip below the toggle */
    .column-title {
      margin: auto 0; /* physical: margin-block would be left and right in vertical writing */
      writing-mode: vertical-rl;
      rotate: 180deg;
      font-size: var(--text-small);
      font-weight: var(--font-weight-semibold);
      letter-spacing: var(--letter-spacing-uppercase);
      text-transform: uppercase;
      color: var(--color-muted);
    }

    &:hover {
      background-color: var(--color-surface-soft);

      .column-title {
        color: var(--color-ink);
      }
    }
  }

  /* The line between a column and the main column (ColumnsHelper#column_resize_handle). The column's border stays
     the visible line; the handle takes no space, gives it a wider area to grab and highlights it. */
  .column-resize-handle {
    position: relative;
    z-index: var(--z-1);
    flex-shrink: 0;
    width: 0;
    cursor: col-resize;
    touch-action: none;
  }

  .column-resize-handle::before {
    content: "";
    position: absolute;
    inset-block: 0;
    inset-inline: -5px; /* 10px wide to grab */
  }

  .column-resize-handle::after {
    content: "";
    position: absolute;
    inset-block: 0;
    inset-inline: -2px; /* 4px wide when highlighted, over the 1px line */
    transition: background-color 0.15s ease;
  }

  .column-resize-handle:hover::after,
  .column-resize-handle:focus-visible::after,
  .is-resizing-column .column-resize-handle::after {
    background-color: var(--color-blue-accent);
  }

  .column-resize-handle:focus-visible {
    outline: none;
  }

  /* A collapsed column isn't resized */
  .workspace-column.is-column-collapsed + .column-resize-handle,
  .column-resize-handle:has(+ .workspace-column.is-column-collapsed) {
    display: none;
  }

  /* While dragging, the PDF viewer (iframe) and text selection don't take over the pointer */
  .is-resizing-column,
  .is-resizing-column * {
    cursor: col-resize !important;
    user-select: none;
  }

  .is-resizing-column iframe {
    pointer-events: none;
  }

  /* A layer of its own: what is positioned inside it (Write's toolbar at --z-write-toolbar, whose white reaches past
     its edges) stays under the line of a column-resize-handle, which reaches 2px into it */
  .workspace-main {
    flex: 1;
    height: 100%;
    min-width: 0;
    isolation: isolate;
  }

  /* The part of a column or the main column below its header that scrolls, so the scrollbar starts below the
     header */
  .workspace-scroll {
    flex: 1;
    min-height: 0;
  }

  .workspace-main > .workspace-scroll {
    scroll-behavior: smooth; /* Smooth scrolling for outline items */
  }

  /* A collapsed column shows only its header strip */
  .workspace-column.is-column-collapsed > .workspace-scroll {
    display: none;
  }

  .layout-grid {
    display: grid;
    gap: var(--space-3);
  }

  .layout-grid-2 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .layout-grid-3 {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .layout-grid-cards > .card-panel {
    margin-block-end: 0;
  }

  .layout-grid-full-span {
    grid-column: 1 / -1;
  }

  @media (max-width: 48rem) {
    .layout-grid-stack-sm {
      grid-template-columns: 1fr;
    }
  }

  /* 
     * Reusable optimal reading width for text-heavy panels or articles 
     * Applies perfectly to 60-80 characters per line.
     */
  .readable-measure {
    max-inline-size: min(65ch, 100%);
    margin-inline: auto;
  }
}

/* The toggle of a hovered collapsed column darkens with its title, without a box of its own; in this layer, as
   .btn-action sets its colors */
@layer components {
  .workspace-column.is-column-collapsed:hover .column-toggle {
    background-color: transparent;
    color: var(--color-ink);
  }
}
