:root {
      --bg: #f4f6f8;
      --surface: #ffffff;
      --surface-soft: #eef2f5;
      --surface-strong: #11151c;
      --text: #161a22;
      --muted: #657080;
      --muted-strong: #3f4856;
      --accent: #188c72;
      --accent-soft: #dff4ee;
      --border: #d9e0e7;
      --border-dark: #2a303b;
      --danger: #b73838;
      --danger-soft: #fde8e8;
      --shadow: 0 20px 60px rgba(24, 31, 42, .18);
      --ok: #148a49;
      --warn: #9a6500;
      --stage-bg: #171b22;
    }

    * { box-sizing: border-box; }
    html, body { height: 100%; }
    html { scroll-behavior: smooth; }

    body {
      margin: 0;
      font-family: Inter, system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
      background: var(--bg);
      color: var(--text);
    }

    button,
    input {
      font: inherit;
    }

    .app {
      display: grid;
      grid-template-columns: minmax(330px, 400px) minmax(0, 1fr);
      height: 100vh;
      overflow: hidden;
    }

    .start-page {
      min-height: 100vh;
      display: grid;
      place-items: center;
      padding: 24px;
      background: var(--bg);
    }

    .start-panel {
      width: min(560px, 100%);
      padding: 24px;
      border: 1px solid var(--border);
      border-radius: 8px;
      background: var(--surface);
      box-shadow: 0 20px 60px rgba(24, 31, 42, .12);
    }

    .preview-form {
      display: grid;
      gap: 14px;
      margin-top: 20px;
    }

    .preview-form label {
      display: grid;
      gap: 7px;
      color: var(--muted-strong);
      font-size: 12px;
      font-weight: 800;
    }

    .preview-form input {
      width: 100%;
      min-height: 42px;
      padding: 9px 11px;
      border: 1px solid var(--border);
      border-radius: 8px;
      background: var(--surface-soft);
      color: var(--text);
    }

    .start-error {
      margin-top: 16px;
      padding: 10px 12px;
      border: 1px solid #f1c4c4;
      border-radius: 8px;
      background: var(--danger-soft);
    }

    .preload-page {
      min-height: 100vh;
      display: grid;
      place-items: center;
      padding: 24px;
      background:
        linear-gradient(135deg, rgba(24, 140, 114, .08), transparent 42%),
        var(--bg);
    }

    .preload-panel {
      width: min(520px, 100%);
      display: grid;
      justify-items: center;
      gap: 16px;
      text-align: center;
    }

    .preload-logo {
      width: min(260px, 80vw);
      height: auto;
      margin-bottom: 8px;
    }

    .preload-panel h1 {
      width: 100%;
      margin: 0;
      color: var(--text);
      overflow-wrap: anywhere;
    }

    .preload-copy {
      min-height: 20px;
      color: var(--muted-strong);
      font-size: 14px;
      font-weight: 800;
      line-height: 1.4;
    }

    .preload-progress {
      width: min(420px, 100%);
      height: 10px;
      overflow: hidden;
      border: 1px solid var(--border);
      border-radius: 999px;
      background: var(--surface);
    }

    .preload-progress-bar {
      width: 0;
      height: 100%;
      border-radius: inherit;
      background: var(--accent);
      transition: width .28s ease;
    }

    .preload-percent {
      color: var(--muted);
      font-size: 12px;
      font-weight: 800;
    }

    .preload-error {
      width: min(420px, 100%);
      min-height: 20px;
      text-align: center;
    }

    .sidebar {
      display: flex;
      flex-direction: column;
      border-right: 1px solid var(--border);
      background: var(--surface);
      padding: 18px;
      overflow: auto;
      scrollbar-gutter: stable;
    }

    .brand {
      padding: 4px 2px 16px;
      border-bottom: 1px solid var(--border);
      margin-bottom: 14px;
    }

    .brand-logo {
      display: block;
      width: min(220px, 100%);
      height: auto;
      margin-bottom: 12px;
    }

    .eyebrow {
      color: var(--accent);
      font-size: 12px;
      font-weight: 800;
      letter-spacing: .08em;
      text-transform: uppercase;
      margin-bottom: 6px;
    }

    h1 {
      font-size: 24px;
      line-height: 1.05;
      margin: 0 0 8px;
    }

    .sub {
      color: var(--muted);
      font-size: 13px;
      line-height: 1.45;
      margin: 0;
    }

    .sr-only {
      position: absolute;
      width: 1px;
      height: 1px;
      padding: 0;
      margin: -1px;
      overflow: hidden;
      clip: rect(0, 0, 0, 0);
      white-space: nowrap;
      border: 0;
    }

    .content {
      display: grid;
      grid-template-rows: auto minmax(0, 1fr);
      min-width: 0;
      min-height: 0;
      background: var(--stage-bg);
    }

    .toolbar {
      display: flex;
      align-items: center;
      gap: 12px;
      padding: 12px 16px;
      border-bottom: 1px solid var(--border-dark);
      background: rgba(17, 21, 28, .94);
      backdrop-filter: blur(12px);
      z-index: 20;
      flex-wrap: wrap;
      color: #edf3f6;
    }

    .toolbar-title {
      display: grid;
      gap: 2px;
      min-width: 180px;
    }

    .toolbar-logo {
      display: none;
      width: 158px;
      height: auto;
    }

    .toolbar-status {
      display: flex;
      align-items: center;
      gap: 12px;
      flex-wrap: wrap;
    }

    .toolbar-title strong {
      font-size: 14px;
      line-height: 1.2;
    }

    .toolbar-title span {
      color: #aab4c2;
      font-size: 12px;
    }

    .toolbar-tool {
      min-height: 34px;
      padding: 7px 10px;
      border-color: rgba(255,255,255,.16);
      background: rgba(255,255,255,.08);
      color: #edf3f6;
    }

    .toolbar-tool:hover {
      background: rgba(255,255,255,.12);
      border-color: rgba(255,255,255,.26);
    }

    .toolbar-tool.active {
      color: #dff4ee;
      background: rgba(24, 140, 114, .32);
      border-color: rgba(223, 244, 238, .46);
    }

    .viewer-wrap {
      position: relative;
      overflow: hidden;
      background:
        linear-gradient(45deg, #1d232c 25%, transparent 25%),
        linear-gradient(-45deg, #1d232c 25%, transparent 25%),
        linear-gradient(45deg, transparent 75%, #1d232c 75%),
        linear-gradient(-45deg, transparent 75%, #1d232c 75%);
      background-size: 24px 24px;
      background-position: 0 0, 0 12px, 12px -12px, -12px 0;
      min-height: 0;
    }

    .stage {
      width: 100%;
      height: 100%;
      position: relative;
      cursor: grab;
      user-select: none;
      touch-action: none;
      overflow: hidden;
    }

    .stage.picker-active {
      cursor: crosshair;
    }

    .stage.dragging {
      cursor: grabbing;
    }

    .canvas {
      position: absolute;
      left: 50%;
      top: 50%;
      box-shadow: var(--shadow);
    }

    .page {
      position: relative;
      background: white;
      overflow: hidden;
    }

    .page.is-loading .layer {
      opacity: 0;
    }

    .layer {
      position: absolute;
      inset: 0;
      width: 100%;
      height: 100%;
      pointer-events: none;
      image-rendering: auto;
      opacity: 1;
      transition: opacity .18s ease;
    }

    .layer.sep {
      mix-blend-mode: multiply;
    }

    .hidden {
      display: none !important;
    }

    .loading-overlay {
      position: absolute;
      inset: 0;
      z-index: 30;
      display: grid;
      place-items: center;
      background: rgba(17, 21, 28, .74);
      backdrop-filter: blur(3px);
      color: #edf3f6;
      pointer-events: none;
    }

    .loader {
      display: grid;
      gap: 12px;
      justify-items: center;
      padding: 16px 18px;
      border-radius: 8px;
      background: rgba(17, 21, 28, .82);
      box-shadow: 0 18px 50px rgba(0,0,0,.28);
    }

    .loader-ring {
      width: 38px;
      height: 38px;
      border-radius: 50%;
      border: 3px solid rgba(255,255,255,.24);
      border-top-color: #dff4ee;
      animation: spin .85s linear infinite;
    }

    .loader-text {
      color: #d9e2ec;
      font-size: 12px;
      font-weight: 800;
      letter-spacing: .02em;
      text-transform: uppercase;
    }

    .loader-progress {
      width: min(240px, 52vw);
      height: 6px;
      overflow: hidden;
      border-radius: 999px;
      background: rgba(255,255,255,.16);
    }

    .loader-progress-bar {
      width: 0%;
      height: 100%;
      border-radius: inherit;
      background: #dff4ee;
      transition: width .18s ease;
    }

    .loader-count {
      color: #aab4c2;
      font-size: 12px;
      font-variant-numeric: tabular-nums;
    }

    @keyframes spin {
      to { transform: rotate(360deg); }
    }

    .group {
      border: 1px solid var(--border);
      background: var(--surface);
      border-radius: 8px;
      padding: 14px;
      margin-bottom: 14px;
      scroll-margin-top: 78px;
    }

    .group-title {
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: 10px;
      font-size: 12px;
      font-weight: 700;
      color: var(--muted-strong);
      text-transform: uppercase;
      letter-spacing: .06em;
      margin-bottom: 12px;
    }

    .row {
      display: flex;
      align-items: center;
      gap: 10px;
      margin-bottom: 10px;
      flex-wrap: wrap;
    }

    .button-row {
      display: grid;
      grid-template-columns: repeat(2, minmax(0, 1fr));
      gap: 8px;
      margin-bottom: 10px;
    }

    .button-row.compact {
      grid-template-columns: repeat(4, minmax(0, 1fr));
    }

    .page-switcher {
      display: grid;
      grid-template-columns: 42px minmax(0, 1fr) 42px 42px;
      gap: 8px;
      align-items: center;
      margin-bottom: 10px;
    }

    .page-counter {
      display: flex;
      align-items: center;
      justify-content: center;
      min-height: 38px;
      border: 1px solid var(--border);
      border-radius: 8px;
      background: var(--surface-soft);
      color: var(--muted-strong);
      font-size: 13px;
      font-weight: 800;
      font-variant-numeric: tabular-nums;
    }

    .row:last-child {
      margin-bottom: 0;
    }

    label {
      display: flex;
      align-items: center;
      gap: 10px;
      font-size: 14px;
      width: 100%;
      color: var(--muted-strong);
    }

    input[type="checkbox"] {
      width: 16px;
      height: 16px;
      accent-color: var(--accent);
      cursor: pointer;
      flex: 0 0 auto;
    }

    .chip {
      font-size: 12px;
      font-weight: 700;
      padding: 6px 9px;
      border-radius: 8px;
      border: 1px solid rgba(255,255,255,.12);
      background: rgba(255,255,255,.08);
      color: #d9e2ec;
      white-space: nowrap;
    }

    .btn {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      gap: 8px;
      min-height: 38px;
      background: var(--surface-soft);
      color: var(--text);
      border: 1px solid var(--border);
      border-radius: 8px;
      padding: 8px 12px;
      cursor: pointer;
      font-weight: 700;
      text-align: center;
      transition: transform .12s ease, background .16s ease, border-color .16s ease, box-shadow .16s ease;
    }

    .btn:hover {
      border-color: rgba(24, 140, 114, .45);
      background: #f8fbfb;
      transform: translateY(-1px);
    }

    .btn.active {
      border-color: var(--accent);
      background: var(--accent-soft);
      color: #0f5f4d;
      box-shadow: inset 0 0 0 1px rgba(24, 140, 114, .16);
    }

    .btn.danger {
      color: var(--danger);
      background: var(--danger-soft);
      border-color: #f1c4c4;
    }

    .btn:disabled {
      opacity: .45;
      cursor: not-allowed;
    }

    .spacer {
      flex: 1;
    }

    .stat {
      font-size: 13px;
      color: #aab4c2;
    }

    .swatch {
      width: 14px;
      height: 14px;
      border-radius: 4px;
      border: 1px solid rgba(22, 26, 34, .18);
      flex: 0 0 auto;
    }

    .channel-row {
      display: grid;
      grid-template-columns: minmax(0, 1fr) auto;
      gap: 10px;
      align-items: center;
      min-height: 38px;
      padding: 7px 0;
      border-bottom: 1px solid var(--border);
    }

    .channel-row:last-child {
      border-bottom: 0;
    }

    .channel-control {
      min-width: 0;
    }

    .channel-name {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .channel-cal {
      min-width: 74px;
      padding: 5px 8px;
      border-radius: 8px;
      background: var(--surface-soft);
      color: var(--muted);
      font-size: 12px;
      font-weight: 800;
      font-variant-numeric: tabular-nums;
      text-align: right;
    }

    .channel-cal.is-active {
      color: #0f5f4d;
      background: var(--accent-soft);
    }

    .ink-summary {
      display: grid;
      grid-template-columns: 1fr auto;
      gap: 8px 12px;
      align-items: center;
      margin-top: 12px;
      padding: 10px 12px;
      border-radius: 8px;
      background: var(--surface-soft);
      color: var(--muted-strong);
      font-size: 12px;
    }

    .ink-summary strong {
      color: var(--text);
      font-size: 16px;
      font-variant-numeric: tabular-nums;
    }

    .page-popover {
      display: none;
      margin-top: 10px;
    }

    .page-popover.open {
      display: block;
    }

    .mobile-page-switcher {
      display: none;
    }

    .app-footer {
      margin-top: auto;
      padding: 14px 2px 0;
      color: var(--muted);
      font-size: 12px;
      font-weight: 700;
      text-align: center;
    }

    .error {
      color: var(--danger);
      font-size: 12px;
      line-height: 1.45;
      word-break: break-word;
      min-height: 18px;
    }

    .page-list {
      display: grid;
      gap: 8px;
    }

    .page-list .btn {
      width: 100%;
      text-align: left;
      justify-content: flex-start;
    }

    .meta {
      font-size: 12px;
      color: var(--muted);
      margin-top: 8px;
      line-height: 1.45;
    }

    .picker-status {
      margin-top: 10px;
      font-size: 12px;
      line-height: 1.45;
      color: var(--muted);
      padding: 10px 12px;
      border-radius: 8px;
      background: var(--surface-soft);
    }

    .picker-status.warn {
      color: var(--warn);
      background: #fff6df;
    }

    .picker-status.ok {
      color: var(--ok);
      background: #e8f7ed;
    }

    .picker-crosshair {
      position: absolute;
      width: 16px;
      height: 16px;
      transform: translate(-50%, -50%);
      pointer-events: none;
      z-index: 15;
      display: none;
    }

    .picker-crosshair::before,
    .picker-crosshair::after {
      content: "";
      position: absolute;
      background: rgba(255,255,255,.95);
      box-shadow: 0 0 0 1px rgba(0,0,0,.7);
    }

    .picker-crosshair::before {
      left: 50%;
      top: 0;
      width: 1px;
      height: 100%;
      transform: translateX(-50%);
    }

    .picker-crosshair::after {
      top: 50%;
      left: 0;
      width: 100%;
      height: 1px;
      transform: translateY(-50%);
    }

    .picker-dot {
      position: absolute;
      left: 50%;
      top: 50%;
      width: 4px;
      height: 4px;
      transform: translate(-50%, -50%);
      background: rgba(255,255,255,.98);
      border: 1px solid rgba(0,0,0,.75);
      border-radius: 50%;
    }

    .inline-note {
      font-size: 12px;
      color: var(--muted);
      line-height: 1.45;
      margin-top: 10px;
    }

    .code-note {
      font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
      font-size: 12px;
      color: #26303d;
      background: var(--surface-soft);
      padding: 8px 10px;
      border-radius: 8px;
      margin-top: 8px;
      white-space: pre-wrap;
      word-break: break-word;
    }

    @media (max-width: 900px) {
      .app {
        grid-template-columns: 1fr;
        grid-template-rows: minmax(0, 1fr) auto;
        height: 100svh;
        min-height: 100svh;
        overflow: hidden;
      }

      .sidebar {
        order: 2;
        border-right: none;
        border-top: 1px solid var(--border);
        border-bottom: none;
        max-height: 44svh;
        padding: 12px 14px;
      }

      .content {
        order: 1;
        min-height: 0;
      }

      .toolbar {
        display: grid;
        grid-template-columns: auto minmax(0, 1fr);
        grid-template-rows: auto auto;
        align-items: center;
        column-gap: 12px;
        row-gap: 8px;
      }

      .toolbar-title {
        display: flex;
        align-items: center;
        justify-content: center;
        grid-column: 1;
        grid-row: 1 / 3;
        align-self: center;
        min-width: 0;
        margin-right: 0;
        padding: 0;
        border-radius: 0;
        background: transparent;
      }

      .toolbar-title-copy,
      .spacer,
      .stat {
        display: none;
      }

      .toolbar-logo {
        display: block;
        width: 136px;
        max-height: 30px;
      }

      .toolbar-status {
        grid-column: 2;
        grid-row: 1;
        justify-content: flex-end;
        justify-self: end;
        gap: 8px;
      }

      .stage {
        height: 100%;
      }

      .brand,
      .page-group,
      .view-group {
        display: none;
      }

      .mobile-page-switcher {
        display: grid;
        grid-template-columns: 36px minmax(74px, 1fr) 36px 36px;
        gap: 6px;
        grid-column: 2;
        grid-row: 2;
        justify-self: end;
        margin-left: auto;
        width: min(420px, 100%);
        max-width: 420px;
      }

      .mobile-page-switcher .btn {
        min-height: 34px;
        padding: 6px 8px;
        border-color: rgba(255,255,255,.16);
        background: rgba(255,255,255,.08);
        color: #edf3f6;
      }

      .mobile-page-switcher .page-counter {
        min-height: 34px;
        border-color: rgba(255,255,255,.16);
        background: rgba(255,255,255,.08);
        color: #edf3f6;
      }

      .mobile-page-switcher .page-popover {
        grid-column: 1 / -1;
        margin-top: 0;
      }

      .mobile-page-switcher .page-list {
        padding: 8px;
        border: 1px solid rgba(255,255,255,.16);
        border-radius: 8px;
        background: rgba(17, 21, 28, .96);
      }

      .mobile-page-switcher .page-list .btn {
        justify-content: flex-start;
      }

      #pageChip {
        display: none;
      }

      .chip {
        order: initial;
      }

      .toolbar-tool {
        order: initial;
      }

      .group {
        margin-bottom: 10px;
      }

      .channels-group {
        margin-bottom: 0;
      }
    }

    @media (max-width: 560px) {
      .sidebar {
        padding: 14px;
      }

      .toolbar {
        gap: 8px;
      }

      .chip {
        padding: 5px 7px;
        font-size: 11px;
      }

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

      .ink-summary {
        margin-top: 10px;
      }
    }
