@layer aesthetic-v4 {
  :root {
    --v4-accent: #cba969;
    --v4-accent-soft: rgba(203,169,105,.16);
    --v4-ink: #33252d;
    --v4-muted: #786b72;
    --v4-surface: #fffaf2;
    --v4-surface-2: #f4eadf;
    --v4-border: rgba(77,51,64,.18);
    --v4-danger: #9a3340;
    --v4-radius: 15px;
    --v4-control-min: 40px;
  }

  html[data-app-theme-resolved="dark"] {
    --v4-ink: #f4e9ee;
    --v4-muted: #c9bbc2;
    --v4-surface: #261a22;
    --v4-surface-2: #34242e;
    --v4-border: rgba(240,220,229,.16);
    --v4-danger: #e78d99;
  }

  /* Accessibility baseline for new controls. Visible glyphs may remain compact. */
  .v4-workspace-tile,
  .v4-library-controls button,
  .v4-view-toggle button,
  .v4-search-results button,
  .v4-template-list button,
  .v4-trash-list button,
  .v4-history-list button,
  .v4-block-grid button,
  .v4-export-grid button,
  .v4-bulk-grid button,
  .command-palette-list button,
  .shelf-page-indicator-button,
  .page-index-select,
  .code-block-download,
  .code-block-close,
  .code-block-copy,
  .diary-rich-block button {
    min-width: var(--v4-control-min);
    min-height: var(--v4-control-min);
  }

  /* Workspace */
  .v4-workspace-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }
  .v4-workspace-tile {
    display: grid;
    grid-template-columns: 38px minmax(0,1fr);
    grid-template-rows: auto auto;
    column-gap: 11px;
    align-items: center;
    padding: 12px;
    border: 1px solid var(--v4-border);
    border-radius: var(--v4-radius);
    background: var(--v4-surface);
    color: var(--v4-ink);
    text-align: left;
    cursor: pointer;
  }
  .v4-workspace-tile:hover,
  .v4-workspace-tile:focus-visible { border-color: rgba(203,169,105,.72); box-shadow: 0 8px 26px rgba(27,15,22,.10); outline: none; }
  .v4-workspace-tile > span { grid-row: 1 / 3; font-size: 24px; color: var(--v4-accent); text-align: center; }
  .v4-workspace-tile strong { font-size: 14px; }
  .v4-workspace-tile small { color: var(--v4-muted); line-height: 1.35; }

  /* Library */
  .v4-library-toolbar,
  .v4-backup-actions,
  .v4-view-toggle {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
  }
  .v4-library-toolbar span { padding: 6px 9px; border: 1px solid var(--v4-border); border-radius: 999px; color: var(--v4-muted); font-size: 12px; }
  .v4-library-list { display: grid; gap: 8px; max-height: min(58vh, 560px); overflow: auto; padding-right: 2px; }
  .v4-library-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 11px;
    border: 1px solid var(--v4-border);
    background: var(--v4-surface);
    border-radius: 13px;
  }
  .v4-library-row.is-archived { opacity: .72; }
  .v4-library-main { min-width: 0; display: grid; gap: 3px; }
  .v4-library-main strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--v4-ink); }
  .v4-library-main small { color: var(--v4-muted); }
  .v4-library-controls { display: flex; gap: 5px; flex-wrap: wrap; justify-content: flex-end; }
  .v4-library-controls button,
  .v4-view-toggle button { border: 1px solid var(--v4-border); border-radius: 9px; background: var(--v4-surface-2); color: var(--v4-ink); padding: 6px 9px; cursor: pointer; }

  /* Search */
  .v4-search-form { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 9px; margin-bottom: 12px; }
  .v4-search-form .field:first-child { grid-column: 1 / -1; }
  .v4-search-results { display: grid; gap: 7px; max-height: min(48vh, 480px); overflow: auto; }
  .v4-search-results button {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 4px 10px;
    padding: 9px 10px;
    border: 1px solid var(--v4-border);
    border-radius: 11px;
    background: var(--v4-surface);
    color: var(--v4-ink);
    text-align: left;
    cursor: pointer;
  }
  .v4-search-results button small { grid-column: 1 / -1; color: var(--v4-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .v4-search-empty { color: var(--v4-muted); text-align: center; padding: 18px; }

  /* Calendar / timeline */
  .v4-calendar-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
  .v4-calendar-head input { max-width: 180px; }
  .v4-calendar-grid { display: grid; grid-template-columns: repeat(7,minmax(0,1fr)); gap: 5px; }
  .v4-calendar-weekday { text-align: center; font-size: 11px; color: var(--v4-muted); padding: 5px 0; }
  .v4-calendar-day {
    min-height: 70px;
    border: 1px solid var(--v4-border);
    border-radius: 10px;
    padding: 5px;
    background: var(--v4-surface);
    color: var(--v4-ink);
    overflow: hidden;
  }
  .v4-calendar-day.is-outside { opacity: .38; }
  .v4-calendar-day.is-today { outline: 2px solid var(--v4-accent); outline-offset: -2px; }
  .v4-calendar-day > strong { font-size: 11px; }
  .v4-calendar-items { display: grid; gap: 3px; margin-top: 4px; }
  .v4-calendar-items button { border: 0; border-radius: 6px; padding: 3px 5px; background: var(--v4-accent-soft); color: var(--v4-ink); font-size: 10px; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
  .v4-timeline { display: grid; gap: 8px; max-height: 58vh; overflow: auto; }
  .v4-timeline-day { border-left: 2px solid var(--v4-accent); padding-left: 11px; }
  .v4-timeline-day h4 { margin: 0 0 6px; }
  .v4-timeline-day button { width: 100%; text-align: left; border: 1px solid var(--v4-border); background: var(--v4-surface); color: var(--v4-ink); border-radius: 9px; padding: 8px; margin-bottom: 5px; cursor: pointer; }

  /* Stats / insights */
  .v4-stats-grid,
  .v4-backup-health { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 9px; }
  .v4-stats-grid article,
  .v4-backup-health article { padding: 12px; border: 1px solid var(--v4-border); border-radius: 13px; background: var(--v4-surface); display: grid; gap: 3px; }
  .v4-stats-grid article span,
  .v4-backup-health article span { color: var(--v4-muted); font-size: 11px; text-transform: uppercase; letter-spacing: .05em; }
  .v4-stats-grid article strong,
  .v4-backup-health article strong { font-size: 18px; color: var(--v4-ink); }
  .v4-stats-grid article small,
  .v4-backup-health article small { color: var(--v4-muted); }
  .v4-insight-list { margin-top: 10px; display: grid; gap: 7px; }
  .v4-insight-list > div { display: flex; justify-content: space-between; gap: 8px; padding: 8px 10px; border-bottom: 1px solid var(--v4-border); }

  /* Trash, history, templates */
  .v4-trash-list,
  .v4-template-list,
  .v4-history-list { display: grid; gap: 8px; max-height: 55vh; overflow: auto; }
  .v4-trash-row,
  .v4-template-row,
  .v4-history-row { display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 8px; align-items: center; padding: 10px; border: 1px solid var(--v4-border); border-radius: 12px; background: var(--v4-surface); }
  .v4-trash-row > div,
  .v4-template-row > div,
  .v4-history-row > div { min-width: 0; display: grid; gap: 2px; }
  .v4-trash-row small,
  .v4-template-row small,
  .v4-history-row small { color: var(--v4-muted); }
  .v4-row-actions { display: flex; gap: 5px; }
  .v4-row-actions button { border: 1px solid var(--v4-border); background: var(--v4-surface-2); color: var(--v4-ink); border-radius: 8px; padding: 6px 8px; cursor: pointer; }
  .v4-row-actions button.danger { color: var(--v4-danger); }

  /* Metadata / page chips */
  .page-meta-strip { display: flex; align-items: center; gap: 5px; flex-wrap: wrap; margin: 2px 0 6px; min-height: 22px; }
  .page-meta-chip,
  .page-prompt-chip { border: 1px solid rgba(104,77,87,.18); background: rgba(255,255,255,.48); color: inherit; border-radius: 999px; padding: 3px 7px; font-size: 10px; line-height: 1.25; }
  .page-prompt-chip { cursor: pointer; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .page-menu-panel { max-height: min(68vh, 560px); overflow-y: auto; overscroll-behavior: contain; }

  /* Rich blocks */
  .v4-block-grid,
  .v4-export-grid,
  .v4-bulk-grid { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 8px; }
  .v4-block-grid button,
  .v4-export-grid button,
  .v4-bulk-grid button { border: 1px solid var(--v4-border); border-radius: 12px; background: var(--v4-surface); color: var(--v4-ink); padding: 11px; display: flex; gap: 9px; align-items: center; justify-content: center; cursor: pointer; }
  .v4-bulk-grid button.danger { color: var(--v4-danger); }
  .diary-rich-block { margin: 10px 0; border: 1px solid rgba(97,70,80,.20); border-radius: 12px; background: rgba(255,255,255,.45); padding: 8px; max-width: 100%; overflow: hidden; }
  .diary-image-block img { display: block; max-width: 100%; max-height: 60vh; width: auto; height: auto; border-radius: 8px; margin: auto; }
  .diary-image-block span,
  .diary-audio-block span { display: block; text-align: center; font-size: 11px; opacity: .72; margin-top: 5px; }
  .diary-audio-block audio { display: block; width: 100%; }
  .diary-attachment-block { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
  .diary-attachment-block a { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .diary-table { width: 100%; border-collapse: collapse; margin: 10px 0; }
  .diary-table td,
  .diary-table th { border: 1px solid currentColor; padding: 5px 7px; opacity: .92; }
  .diary-callout { border-left: 4px solid #c2964e; padding: 9px 10px; margin: 10px 0; background: rgba(194,150,78,.10); border-radius: 0 9px 9px 0; }
  .diary-callout p { margin: 4px 0 0; }
  .diary-divider { border: 0; border-top: 1px solid currentColor; opacity: .28; margin: 18px 0; }
  .diary-internal-link,
  .diary-external-link { text-decoration: underline; text-underline-offset: 2px; cursor: pointer; }
  .v4-recording-indicator { min-height: 120px; display: grid; place-items: center; align-content: center; gap: 8px; text-align: center; }
  .v4-recording-indicator > span { width: 18px; height: 18px; border-radius: 50%; background: #b93348; box-shadow: 0 0 0 7px rgba(185,51,72,.13); animation: v4-record-pulse 1.2s infinite; }
  @keyframes v4-record-pulse { 50% { transform: scale(.8); opacity: .65; } }

  /* Backup / preferences */
  .v4-backup-health { margin-bottom: 12px; }
  .v4-pref-section { border: 1px solid var(--v4-border); background: var(--v4-surface); border-radius: 13px; padding: 11px; margin-bottom: 9px; }
  .v4-pref-section h3 { margin: 0 0 9px; font-size: 14px; color: var(--v4-ink); }
  .v4-check { display: flex; gap: 8px; align-items: flex-start; margin: 8px 0; color: var(--v4-ink); }
  .v4-check input { margin-top: 3px; }
  .v4-favourite-grid { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 6px; }
  .v4-favourite-grid label { display: flex; gap: 7px; align-items: center; border: 1px solid var(--v4-border); border-radius: 9px; padding: 7px; }

  /* Page Tools favourites */
  .page-tool-menu-panel .page-tool-nonfavourite { display: none !important; }
  .page-tool-menu-panel.show-all-page-tools .page-tool-nonfavourite { display: inline-flex !important; }
  .page-tools-docked .page-tool-menu-panel .page-tool-nonfavourite { display: none !important; }
  .page-tools-docked .page-tool-menu-panel.show-all-page-tools .page-tool-nonfavourite { display: inline-flex !important; }

  /* Command palette */
  .command-palette-search { margin-bottom: 9px; }
  .command-palette-search input { width: 100%; }
  .command-palette-list { display: grid; gap: 5px; max-height: 55vh; overflow: auto; }
  .command-palette-list button { border: 1px solid var(--v4-border); background: var(--v4-surface); color: var(--v4-ink); border-radius: 10px; padding: 9px 11px; text-align: left; cursor: pointer; }
  .command-palette-list button:hover,
  .command-palette-list button:focus-visible { border-color: var(--v4-accent); outline: none; }

  /* Mobile shelf page indicator */
  .shelf-page-indicator { min-height: 18px; margin-top: 2px; font-size: 11px; letter-spacing: .08em; color: rgba(255,255,255,.72); text-align: center; pointer-events: none; }
  html[data-app-theme-resolved="light"] .shelf-page-indicator { color: rgba(57,45,51,.65); }

  /* Book Index multi-select */
  .book-index-row { position: relative; }
  .page-index-select { display: inline-grid; place-items: center; flex: 0 0 auto; }
  .page-index-select input { width: 18px; height: 18px; accent-color: #9c6f34; }
  .index-bulk-actions-button { white-space: nowrap; }

  /* Code block v4 */
  .diary-code-header { gap: 6px !important; }
  .code-block-filename { width: clamp(72px, 18vw, 160px); min-width: 64px; border: 0; border-bottom: 1px solid rgba(255,255,255,.18); background: transparent; color: inherit; font: inherit; font-size: 11px; padding: 4px 5px; }
  .code-block-highlight-lines { width: 76px; min-width: 64px; }
  .code-block-download { display: inline-grid; place-items: center; border: 0; background: transparent; color: inherit; border-radius: 7px; cursor: pointer; }
  .code-block-download svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 1.8; }
  .code-render-line { display: block; min-height: 1.35em; padding: 0 5px; margin: 0 -5px; }
  .code-render-line.is-highlighted { background: rgba(237,192,77,.15); box-shadow: inset 2px 0 0 #e4b94c; }

  /* Empty / archive state */
  .v4-empty-shelf { width: min(92vw, 420px); min-height: 180px; display: grid; place-items: center; align-content: center; gap: 8px; margin: auto; border: 1px dashed rgba(255,255,255,.26); border-radius: 20px; color: rgba(255,255,255,.78); text-align: center; padding: 20px; }
  .v4-empty-shelf button { border: 1px solid rgba(255,255,255,.3); border-radius: 10px; background: rgba(255,255,255,.08); color: inherit; padding: 9px 12px; cursor: pointer; }

  @media (max-width: 720px) {
    :root { --v4-control-min: 42px; }
    .v4-workspace-grid { grid-template-columns: 1fr; }
    .v4-library-row { align-items: flex-start; flex-direction: column; }
    .v4-library-controls { width: 100%; justify-content: flex-start; }
    .v4-search-form { grid-template-columns: 1fr; }
    .v4-search-form .field:first-child { grid-column: auto; }
    .v4-calendar-day { min-height: 58px; padding: 4px; }
    .v4-calendar-items button { font-size: 9px; padding: 2px 3px; }
    .v4-stats-grid,
    .v4-backup-health { grid-template-columns: 1fr 1fr; }
    .v4-favourite-grid { grid-template-columns: 1fr; }
    .code-block-filename { width: 100px; }
  }

  @media (max-width: 430px) {
    .v4-calendar-grid { gap: 3px; }
    .v4-calendar-day { min-height: 52px; }
    .v4-calendar-items button:nth-child(n+3) { display: none; }
    .v4-block-grid,
    .v4-export-grid,
    .v4-bulk-grid { grid-template-columns: 1fr 1fr; }
  }

  @media (prefers-reduced-motion: reduce) {
    .v4-recording-indicator > span { animation: none; }
  }
}

@layer aesthetic-v4 {
  .v4-search-filters { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); gap:9px; margin-bottom:12px; }
  .v4-search-filters > label { display:grid; gap:5px; min-width:0; color:var(--v4-muted); font-size:12px; }
  .v4-search-filters input,
  .v4-search-filters select { width:100%; }
  .v4-search-result { grid-template-columns:minmax(0,1fr) auto !important; align-items:center; }
  .v4-search-result > span:first-child { display:grid; gap:3px; min-width:0; }

  .v4-calendar-toolbar { display:flex; justify-content:space-between; align-items:center; gap:8px; margin-bottom:10px; }
  .v4-calendar-toolbar > div { display:flex; gap:5px; }
  .v4-calendar-toolbar button { min-height:36px; border:1px solid var(--v4-border); border-radius:8px; background:var(--v4-surface); color:var(--v4-ink); padding:5px 9px; }
  .v4-calendar-toolbar button.active { border-color:var(--v4-accent); background:var(--v4-accent-soft); }
  .v4-calendar-weekdays { display:grid; grid-template-columns:repeat(7,minmax(0,1fr)); gap:4px; margin-bottom:4px; }
  .v4-calendar-weekdays span { text-align:center; color:var(--v4-muted); font-size:10px; text-transform:uppercase; }
  .v4-calendar-cell { min-height:58px; border:1px solid var(--v4-border); border-radius:9px; background:var(--v4-surface); color:var(--v4-ink); display:flex; flex-direction:column; align-items:flex-start; justify-content:space-between; padding:6px; cursor:pointer; }
  .v4-calendar-cell.empty { visibility:hidden; }
  .v4-calendar-cell.has-items { border-color:rgba(203,169,105,.56); background:linear-gradient(145deg,var(--v4-surface),var(--v4-accent-soft)); }
  .v4-calendar-cell span { align-self:flex-end; display:grid; place-items:center; min-width:20px; height:20px; border-radius:999px; background:var(--v4-accent-soft); font-size:10px; }
  .v4-calendar-day-items { display:grid; gap:5px; margin-top:10px; max-height:160px; overflow:auto; }
  .v4-calendar-day-items button,
  .v4-timeline button { border:1px solid var(--v4-border); border-radius:9px; background:var(--v4-surface); color:var(--v4-ink); padding:8px; text-align:left; display:flex; justify-content:space-between; gap:8px; }
  .v4-calendar-day-items button span,
  .v4-timeline button span { display:grid; gap:2px; }
  .v4-calendar-day-items small,
  .v4-timeline small { color:var(--v4-muted); }

  .v4-stat-grid { display:grid; grid-template-columns:repeat(4,minmax(0,1fr)); gap:8px; }
  .v4-stat-grid article { padding:11px; border:1px solid var(--v4-border); border-radius:12px; background:var(--v4-surface); display:grid; gap:2px; text-align:center; }
  .v4-stat-grid strong { color:var(--v4-ink); font-size:20px; }
  .v4-stat-grid span { color:var(--v4-muted); font-size:11px; }
  .v4-insight-columns { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); gap:9px; margin-top:10px; }
  .v4-insight-columns section { border:1px solid var(--v4-border); border-radius:12px; padding:10px; background:var(--v4-surface); }
  .v4-insight-columns h3 { margin:0 0 7px; font-size:13px; }
  .v4-insight-columns p { display:flex; justify-content:space-between; gap:8px; margin:5px 0; color:var(--v4-ink); }
  .v4-goal-progress { margin-top:10px; border:1px solid var(--v4-border); border-radius:12px; padding:10px; background:var(--v4-surface); }
  .v4-goal-progress progress { width:100%; }

  .v4-trash-section { margin-bottom:12px; }
  .v4-trash-section h3 { margin:0 0 7px; font-size:13px; }
  .v4-trash-section article,
  .v4-template-list article,
  .v4-history-list article { display:flex; justify-content:space-between; align-items:center; gap:9px; padding:9px; border:1px solid var(--v4-border); border-radius:10px; background:var(--v4-surface); margin-bottom:6px; }
  .v4-trash-section article > span,
  .v4-template-list article > span,
  .v4-history-list article > span { display:grid; gap:2px; min-width:0; }
  .v4-trash-section article small,
  .v4-template-list article small,
  .v4-history-list article small { color:var(--v4-muted); }
  .v4-trash-section article > div,
  .v4-template-list article > div { display:flex; gap:5px; }
  .v4-trash-section button,
  .v4-template-list button,
  .v4-history-list button,
  .v4-template-actions button,
  .v4-link-actions button,
  .v4-backlink { border:1px solid var(--v4-border); border-radius:8px; background:var(--v4-surface-2); color:var(--v4-ink); min-height:36px; padding:6px 8px; cursor:pointer; }
  .v4-trash-section button.danger,
  .v4-template-list button.danger { color:var(--v4-danger); }
  .v4-template-actions,
  .v4-link-actions { display:flex; gap:7px; flex-wrap:wrap; margin-bottom:9px; }
  .v4-backlink { width:100%; display:flex; justify-content:space-between; text-align:left; margin-bottom:5px; }
  .v4-backlink small { color:var(--v4-muted); }
  .v4-inline-colour { display:flex; align-items:center; gap:9px; }
  .v4-inline-colour input[type="color"] { width:48px; min-width:48px; height:40px; padding:2px; }

  @media (max-width:720px) {
    .v4-search-filters { grid-template-columns:1fr; }
    .v4-stat-grid { grid-template-columns:repeat(2,minmax(0,1fr)); }
    .v4-insight-columns { grid-template-columns:1fr; }
    .v4-calendar-cell { min-height:50px; padding:4px; }
  }
}
