/*
 * Writing workspace chrome only. Load after apple-interactions.css.
 * Never change screenplay line styles, typography, preview sheets or pagination.
 * The existing immerse-mode reader and double-page sheet geometry stay separate.
 */
.writing-font-control {
  display: inline-flex;
  align-items: center;
  flex: 0 0 auto;
  gap: 5px;
  color: #5c6865;
  font-size: 12px;
}

.writing-font-control select {
  min-height: 44px;
  min-width: 64px;
  padding: 6px;
  color: #283b3b;
  background: #fffefa;
  border: 1px solid #dcded7;
  border-radius: 8px;
  font: inherit;
}

body:not(.script-editor-page) .writing-font-control { display: none; }

/* Screen-only typography. Double-page sheets retain their print geometry. */
body.script-editor-page[data-writing-font-size]:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode):not(.double-page-writing) #editor .cm-editor {
  font-size: var(--writing-font-size);
}

:is(#writingFocusBtn, #wordPreviewToggleBtn) {
  pointer-events: auto;
  min-width: 44px;
  min-height: 44px;
  touch-action: manipulation;
}

body.script-editor-page.writing-editor-mode:not(.immerse-mode) {
  --writing-paper: #fffefa;
  --writing-surround: #f2f1ed;
  --writing-chrome: #faf9f6;
  --writing-ink: #283b3b;
  --writing-muted: #5c6865;
  --writing-line: #dcded7;
  --writing-accent: #245e60;
}

body.script-editor-page.writing-editor-mode:not(.immerse-mode) .editor-pane {
  background: var(--writing-surround);
  grid-template-rows: auto minmax(0, 1fr);
}

body.script-editor-page.writing-editor-mode:not(.immerse-mode) .editor-pane > .pane-head {
  min-height: 48px;
  height: auto;
  padding: 5px 12px;
  gap: 8px;
  border-bottom: 1px solid var(--writing-line);
  background: var(--writing-chrome);
  box-shadow: none;
}

body.script-editor-page.writing-editor-mode:not(.immerse-mode) .pane-status-cluster {
  min-width: 0;
  gap: 8px;
}

/* Script pages keep their persistent writing actions in the pane header;
   only page navigation is relocated to the desktop top bar. */
body.script-editor-page.source-editor-mode:not(.immerse-mode) #editorPane {
  grid-template-rows: auto minmax(0, 1fr);
}

body.script-editor-page.source-editor-mode:not(.immerse-mode) #editorPane > .pane-head {
  height: auto;
  min-height: 48px;
}

body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode) #editorPane > .pane-head .pane-status-cluster {
  display: flex;
  flex: 0 0 auto;
  width: auto;
  max-width: none;
  overflow: visible;
  gap: 8px;
}

body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode) #editorPane > .pane-head .formatbar {
  flex: 1 1 0;
  min-width: 0;
  overflow-x: auto;
  overflow-y: hidden;
}

body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode) .pane-status-cluster #aiServiceStatus {
  display: none !important;
}

@media (max-width: 1599px) {
  body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode) .pane-status-cluster :is(#aiProviderSelect, #aiModelSelect) {
    display: none !important;
  }
}

@media (min-width: 1181px) {
  body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode) .panel-rail-right {
    display: none;
  }
}

/* Desktop controls share a height. Wrap formatting actions inside their own
   grid track so navigation never covers clipped, unclickable buttons. */
@media (min-width: 901px) {
  body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode):not(.writing-focus-mode) #editorPane {
    grid-template-columns: minmax(0, 1fr);
  }

  body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode):not(.writing-focus-mode) #editorPane > .pane-head {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: start;
    min-width: 0;
    min-height: 48px;
    padding: 8px 12px;
    gap: 12px;
  }

  body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode):not(.writing-focus-mode) #editorPane > .pane-head .formatbar {
    grid-column: 1;
    width: 100%;
    height: auto;
    min-height: 32px;
    padding: 0;
    gap: 6px;
    flex-wrap: wrap;
    overflow: visible;
  }

  body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode):not(.writing-focus-mode) #editorPane > .pane-head .pane-status-cluster {
    grid-column: 2;
    align-items: center;
    gap: 6px;
  }

  body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode):not(.writing-focus-mode) #editorPane > .pane-head :is(.format-btn, .writing-font-control select, #wordPreviewToggleBtn, #writingFocusBtn, #doublePageWritingBtn) {
    height: 32px;
    min-height: 32px;
    padding: 4px 8px;
    font-size: 12px;
    line-height: 1.2;
    border-radius: 7px;
  }

  body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode):not(.writing-focus-mode) .writing-font-control {
    height: 32px;
    gap: 6px;
    font-size: 12px;
    white-space: nowrap;
  }

  body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode):not(.writing-focus-mode) .double-page-writing-nav {
    height: 32px;
  }

  body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode):not(.writing-focus-mode) #saveState {
    max-width: 110px;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode):not(.writing-focus-mode) .find-bar {
    top: var(--writing-toolbar-height, 48px);
  }

  body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode):not(.writing-focus-mode) .panel-resizer {
    top: var(--writing-toolbar-height, 48px);
  }

  body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode):not(.writing-focus-mode) .sidebar-section:first-child {
    grid-template-rows: var(--writing-toolbar-height, 48px) minmax(0, 1fr);
  }

  body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode):not(.writing-focus-mode) .sidebar-section:first-child > .sidebar-head {
    height: var(--writing-toolbar-height, 48px);
  }

  body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode):not(.writing-focus-mode) .sidebar-section:first-child.collapsed {
    min-height: var(--writing-toolbar-height, 48px);
    flex-basis: var(--writing-toolbar-height, 48px);
  }

  body:not(.immerse-mode):not(.writing-focus-mode) #desktopPageRail .pane-title {
    flex: 0 1 auto;
    justify-content: center;
  }

  #desktopPageRail .mode-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
}

/* Leave room for one complete file record below a wrapped desktop header. */
@media (min-width: 901px) and (min-height: 600px) {
  body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode):not(.writing-focus-mode) .sidebar-section:first-child:not(.collapsed) {
    min-height: calc(var(--writing-toolbar-height, 48px) + 128px);
  }
}

@media (min-width: 1280px) {
  body.double-page-writing.writing-editor-mode #editorCanvas {
    width: 100%;
    max-width: 100%;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  body.double-page-writing.writing-editor-mode .editor-sheet {
    grid-template-columns: minmax(0, 1fr);
  }
}

body.script-editor-page.writing-editor-mode:not(.immerse-mode) #saveState {
  color: var(--writing-muted);
  font-size: 12px;
  white-space: nowrap;
}

body.script-editor-page.writing-editor-mode:not(.immerse-mode) :is(#writingFocusBtn, #wordPreviewToggleBtn) {
  flex: 0 0 auto;
  min-width: 44px;
  min-height: 44px;
  padding: 8px 12px;
  border: 1px solid var(--writing-line);
  border-radius: 8px;
  background: var(--writing-paper);
  color: var(--writing-ink);
  box-shadow: none;
  font-size: 12px;
  line-height: 1.25;
  white-space: nowrap;
  cursor: pointer;
  pointer-events: auto;
  touch-action: manipulation;
  transition: background-color 160ms ease, border-color 160ms ease;
}

body.script-editor-page.writing-editor-mode:not(.immerse-mode) :is(#writingFocusBtn, #wordPreviewToggleBtn):hover {
  border-color: var(--writing-accent);
  background: #edf3ef;
}

body.script-editor-page.writing-editor-mode:not(.immerse-mode) :is(#writingFocusBtn, #wordPreviewToggleBtn)[aria-pressed="true"] {
  background: #e6efea;
  border-color: #9cb9b1;
  color: var(--writing-accent);
}

body.script-editor-page.writing-editor-mode:not(.immerse-mode):not(.double-page-writing) #editor .cm-editor {
  width: min(100%, 940px);
  background: var(--writing-paper);
  border-color: var(--writing-line);
  border-radius: 2px;
  box-shadow: 0 3px 16px rgb(40 59 59 / 5%);
}

body.script-editor-page.writing-editor-mode:not(.immerse-mode) #editor {
  background: var(--writing-surround);
}

/* Normal writing keeps panel access, but moves edge handles away from text. */
@media (min-width: 901px) {
  body.script-editor-page.writing-editor-mode:not(.immerse-mode):not(.writing-focus-mode) .panel-rail {
    top: auto;
    bottom: 30px;
    transform: none;
    writing-mode: horizontal-tb;
    min-width: 44px;
    min-height: 44px;
    height: auto;
    padding: 8px 12px;
    border: 1px solid var(--writing-line);
    border-radius: 8px;
    background: var(--writing-chrome);
    color: var(--writing-muted);
    box-shadow: 0 2px 8px rgb(40 59 59 / 5%);
    opacity: 1;
  }

  body.script-editor-page.writing-editor-mode:not(.immerse-mode):not(.writing-focus-mode) .panel-rail-left {
    left: 12px;
  }

  body.script-editor-page.writing-editor-mode:not(.immerse-mode):not(.writing-focus-mode) .panel-rail-right {
    right: 12px;
  }

  body.script-editor-page.writing-editor-mode:not(.immerse-mode):not(.writing-focus-mode) .panel-rail-auto {
    left: auto;
    right: 108px;
  }
}

/* Writing focus is a real editable workspace, not the existing reader mode. */
body.writing-focus-mode:not(.immerse-mode) {
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

body.writing-focus-mode:not(.immerse-mode) > .app {
  height: 100%;
  min-height: 0;
  grid-template-rows: minmax(0, 1fr);
}

body.writing-focus-mode:not(.immerse-mode) .topbar {
  position: fixed;
  inset: max(12px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) auto auto;
  z-index: 60;
  display: flex;
  width: auto;
  min-height: 44px;
  gap: 8px;
  padding: 0;
  background: transparent;
  border: 0;
  box-shadow: none;
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
}

body.writing-focus-mode:not(.immerse-mode) .topbar > .brand {
  display: none;
}

body.writing-focus-mode:not(.immerse-mode) .toolbar {
  flex: 0 0 auto;
  width: auto;
  gap: 8px;
}

body.writing-focus-mode:not(.immerse-mode) .toolbar > :not(#saveBtn):not(#writingFocusBtn):not(#resolveConflictBtn),
body.writing-focus-mode:not(.immerse-mode) #desktopPageRail,
body.writing-focus-mode:not(.immerse-mode) :is(.sidebar, .preview-pane, .panel-rail, .panel-resizer, .sidebar-resizer),
body.writing-focus-mode:not(.immerse-mode) #editorPane > .pane-head {
  display: none !important;
}

body.writing-focus-mode:not(.immerse-mode) .workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) !important;
  min-height: 0;
  min-width: 0;
  background: #f2f1ed;
  transition: none;
}

body.writing-focus-mode:not(.immerse-mode) #editorPane {
  grid-column: 1;
  min-width: 0;
  min-height: 0;
  width: 100%;
  border: 0;
  box-shadow: none;
  grid-template-rows: minmax(0, 1fr) !important;
}

body.writing-focus-mode:not(.immerse-mode) :is(.format-btn, #saveBtn, #resolveConflictBtn) {
  min-width: 44px;
  min-height: 44px;
}

body.writing-focus-mode:not(.immerse-mode) .statusbar {
  /* Keep the live status available to assistive tech without a permanent bar. */
  position: fixed;
  width: 1px;
  height: 1px;
  min-height: 0;
  padding: 0;
  border: 0;
  overflow: hidden;
  clip-path: inset(50%);
}

/* A quiet workspace must never conceal a save failure or conflict. */
body.writing-focus-mode:not(.immerse-mode) .statusbar:has(#saveNotice:is(.error, .warning):not(.is-passive):not([hidden])) {
  inset: auto 12px max(12px, env(safe-area-inset-bottom));
  width: auto;
  height: auto;
  max-height: none;
  clip-path: none;
  z-index: 65;
  background: #fffefa;
  border-radius: 8px;
}

body.writing-focus-mode:not(.immerse-mode) .statusbar > :not(#saveNotice) {
  display: none !important;
}

body.writing-focus-mode:not(.immerse-mode) .statusbar > #saveNotice:is(.error, .warning) {
  white-space: normal;
  max-height: none;
  line-height: 1.5;
}

body.writing-focus-mode:has(#saveNotice.is-passive:not([hidden])) #saveBtn::before {
  content: none;
}

body.writing-focus-mode:has(#saveNotice.is-passive:not([hidden])) #saveBtn::after {
  content: "";
  width: 6px;
  height: 6px;
  flex: 0 0 6px;
  border-radius: 50%;
  background: #a97828;
}

/* A quiet five-line focus band. Scrolling stays native; JS centers the caret
   only on user editing/navigation and measures this padding in viewport px. */
body.writing-focus-mode:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode):not(.double-page-writing) #editor .cm-editor {
  width: min(100%, 860px);
  margin-inline: auto;
  border-radius: 0;
  border-width: 0;
  border-color: transparent;
  box-shadow: none;
}

body.writing-focus-mode:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode) #editor .cm-scroller {
  -webkit-mask-image: linear-gradient(to bottom,
    rgb(0 0 0 / .38) 0%,
    rgb(0 0 0 / .38) calc(50% - var(--writing-focus-line-height, 32px) * 3.5),
    #000 calc(50% - var(--writing-focus-line-height, 32px) * 2.5),
    #000 calc(50% + var(--writing-focus-line-height, 32px) * 2.5),
    rgb(0 0 0 / .38) calc(50% + var(--writing-focus-line-height, 32px) * 3.5),
    rgb(0 0 0 / .38) 100%);
  mask-image: linear-gradient(to bottom,
    rgb(0 0 0 / .38) 0%,
    rgb(0 0 0 / .38) calc(50% - var(--writing-focus-line-height, 32px) * 3.5),
    #000 calc(50% - var(--writing-focus-line-height, 32px) * 2.5),
    #000 calc(50% + var(--writing-focus-line-height, 32px) * 2.5),
    rgb(0 0 0 / .38) calc(50% + var(--writing-focus-line-height, 32px) * 3.5),
    rgb(0 0 0 / .38) 100%);
}

body.writing-focus-mode:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode) #editor .cm-content {
  padding-top: var(--writing-focus-padding, 40vh);
  padding-bottom: var(--writing-focus-padding, 40vh);
}

/* In focus mode, formatting colors yield to the words. Keep all indentation,
   markers and line metrics unchanged so editing and export stay consistent. */
body.writing-focus-mode:not(.immerse-mode) #editor .cm-line {
  background: transparent !important;
  border-color: transparent !important;
  box-shadow: none !important;
}

body.writing-focus-mode:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode) #editor .cm-activeLine {
  color: #283b3b;
  background: rgb(36 94 96 / 4%) !important;
}

/* The non-editable line columns use measured CodeMirror line coordinates.
   Do not add the focus padding again to the overlay: that would double-shift. */
body.writing-focus-mode.writing-editor-mode:not(.immerse-mode) #editor .cm-tusun-line-columns {
  padding-block: 0;
}

body.writing-focus-mode:not(.immerse-mode) #immerseReenterBtn {
  display: none !important;
}

/* The phone header already offers pure reading. A second floating entry
   covers the manuscript and competes with the primary writing workflow. */
body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode) #immerseReenterBtn {
  display: none !important;
}

body.writing-focus-mode.source-editor-mode:not(.immerse-mode) #editorPane {
  background: #f2f1ed;
}

body.writing-focus-mode.source-editor-mode:not(.immerse-mode) #editor .cm-editor {
  margin-inline: auto;
  background: #fffefa;
}

body.writing-focus-mode.source-editor-mode:not(.immerse-mode) #writingFocusBtn {
  min-height: 44px;
  min-width: 44px;
  padding: 8px 12px;
  color: #245e60;
  border: 1px solid #9cb9b1;
  background: #e6efea;
  pointer-events: auto;
}

body.script-editor-page.writing-editor-mode:not(.immerse-mode) :is(.pane-head button, .panel-rail):focus-visible,
body.writing-focus-mode.source-editor-mode:not(.immerse-mode) .pane-head button:focus-visible,
body.writing-focus-mode:not(.immerse-mode) .topbar button:focus-visible {
  outline: 3px solid #245e60;
  outline-offset: 2px;
}

@media (max-width: 900px) {
  body.script-editor-page.writing-editor-mode:not(.immerse-mode) #editorPane > .pane-head {
    padding: 6px 10px;
    flex-wrap: wrap;
    overflow: visible;
  }

  body.script-editor-page.writing-editor-mode:not(.immerse-mode) #editorPane > .pane-head .pane-status-cluster {
    flex: 1 1 auto;
    max-width: 100%;
    justify-content: flex-end;
  }

  body.script-editor-page.writing-editor-mode:not(.immerse-mode) #editorPane > .pane-head .formatbar {
    flex: 1 0 100%;
    height: 44px;
    order: 3;
  }

  body.script-editor-page.writing-editor-mode:not(.immerse-mode) .format-btn {
    min-height: 44px;
    min-width: 44px;
  }
}

@media (max-width: 480px) {
  body.writing-focus-mode:not(.immerse-mode) .topbar {
    right: max(10px, env(safe-area-inset-right));
    gap: 8px;
  }

  body.writing-focus-mode:not(.immerse-mode) .toolbar :is(#saveBtn, #resolveConflictBtn) {
    padding-inline: 10px;
    white-space: nowrap;
  }
}

@media (max-width: 620px) {
  body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode) #editorPane > .pane-head .pane-status-cluster {
    display: flex;
    flex: 1 1 100%;
    max-width: 100%;
    min-width: 0;
    flex-wrap: wrap;
    gap: 6px;
    overflow: visible;
    justify-content: flex-end;
  }

  body.script-editor-page:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode) #writingFocusBtn {
    min-width: 44px;
    min-height: 44px;
    pointer-events: auto;
  }

  body.writing-focus-mode:not(.immerse-mode) .toolbar > #saveBtn {
    width: auto;
    flex: 0 0 auto;
    min-width: 64px;
    font-size: 13px;
    gap: 5px;
  }
}

@media (prefers-reduced-motion: reduce) {
  body.script-editor-page.writing-editor-mode:not(.immerse-mode) :is(#writingFocusBtn, #wordPreviewToggleBtn, .workspace, .panel-rail) {
    transition: none;
    animation: none;
  }
}

/* Night reading changes the editor surface and sidebar only.
   Word preview sheets (.script-page) and print colors stay paper white. */
body[data-theme="night"] {
  --night-paper: #1a1e1c;
  --night-surround: #121614;
  --night-ink: #f4f0e6;
  --night-cursor: #f6d48a;
  --night-muted: #d2ccc0;
  --night-scene: #9ed8d4;
  --night-character: #e2d2f8;
  --night-paren: #f3c2b0;
}

body[data-theme="night"] #editorPane,
body[data-theme="night"].script-editor-page.writing-editor-mode:not(.immerse-mode) .editor-pane,
body[data-theme="night"].writing-focus-mode:not(.immerse-mode) .workspace,
body[data-theme="night"].writing-focus-mode.source-editor-mode:not(.immerse-mode) #editorPane {
  background: var(--night-surround);
}

body[data-theme="night"] :is(#editor, #editorRight) .cm-editor,
body[data-theme="night"] :is(#editor, #editorRight) .cm-scroller,
body[data-theme="night"].script-editor-page.writing-editor-mode:not(.immerse-mode):not(.double-page-writing) #editor .cm-editor,
body[data-theme="night"].script-editor-page.writing-editor-mode:not(.immerse-mode) #editor,
body[data-theme="night"].writing-focus-mode.source-editor-mode:not(.immerse-mode) #editor .cm-editor {
  background: var(--night-paper);
  color: var(--night-ink);
}

body[data-theme="night"] :is(#editor, #editorRight) .cm-content,
body[data-theme="night"] :is(#editor, #editorRight) .cm-line,
body[data-theme="night"] :is(#editor, #editorRight) :is(.cm-tusun-dialogue, .cm-tusun-action),
body[data-theme="night"].writing-focus-mode:is(.writing-editor-mode, .source-editor-mode):not(.immerse-mode) #editor .cm-activeLine {
  color: var(--night-ink);
  caret-color: var(--night-cursor);
}

body[data-theme="night"].writing-editor-mode :is(#editor, #editorRight) .cm-line {
  color: var(--night-ink) !important;
  background: transparent !important;
  box-shadow: none !important;
}

body[data-theme="night"].writing-editor-mode :is(#editor, #editorRight) .cm-line:is(.cm-tusun-scene, .cm-tusun-episode) {
  color: var(--night-scene) !important;
}

body[data-theme="night"].writing-editor-mode :is(#editor, #editorRight) .cm-line.cm-tusun-character {
  color: var(--night-character) !important;
}

body[data-theme="night"].writing-editor-mode :is(#editor, #editorRight) .cm-line:is(.cm-tusun-paren, .cm-tusun-transition, .cm-tusun-subtitle) {
  color: var(--night-paren) !important;
}

body[data-theme="night"].writing-editor-mode :is(#editor, #editorRight) .cm-line:is(.cm-tusun-note, .cm-tusun-meta, .cm-tusun-pagebreak) {
  color: var(--night-muted) !important;
}

body[data-theme="night"] :is(#editor, #editorRight) :is(.cm-cursor, .cm-dropCursor) {
  border-left-color: var(--night-cursor);
}

body[data-theme="night"] :is(#editor, #editorRight) .cm-gutters {
  background: var(--night-surround);
  color: var(--night-muted);
  border-right-color: #3c4440;
}

body[data-theme="night"] .sidebar {
  --ink: var(--night-ink);
  --muted: var(--night-muted);
  --accent: var(--night-scene);
  --accent-2: var(--night-paren);
  --panel: #2a302d;
  --line: #3c4440;
  background: #232826;
  color: var(--night-ink);
}

body[data-theme="night"] .sidebar :is(.file-row:hover, .scene-item:hover, .character-item:hover, .sidebar-head:hover) {
  background: rgba(244, 240, 230, 0.08);
}

body[data-theme="night"] .sidebar .file-row.active {
  background: #2e3531;
}
