/* Inkwell phone — "Deep Charcoal" page theme. Dark only. */

:root {
  --bg: #1E1E1C;
  --surface: #262624;
  --surface-2: #2C2C2A;
  --surface-3: #343432;
  --hairline: rgba(255, 255, 255, 0.08);
  --hairline-strong: rgba(255, 255, 255, 0.14);

  --text: #F5F2EC;
  --text-2: #C9C5BD;
  --text-3: #8A8D94;

  --accent: #3D8BFD;
  --accent-pressed: #2F74DA;
  --live: #32D074;
  --amber: #F5BE45;
  --problem: #EFA48F;

  --prose: Georgia, "Iowan Old Style", "Times New Roman", serif;
  --ui: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, "Helvetica Neue", sans-serif;

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);

  --topbar-h: 56px;
  --tabbar-h: 56px;
  --gutter: 18px;
  --column: 680px;

  /* Set from script so sheets sit above the on-screen keyboard. */
  --vvh: 100vh;
  --vvtop: 0px;
  --kb: 0px;

  color-scheme: dark;
}

* { box-sizing: border-box; }

html {
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--ui);
  font-size: 17px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

html.sheet-open, html.sheet-open body { overflow: hidden; }

button, select, textarea, input {
  font: inherit;
  color: inherit;
}

button {
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

a { color: var(--accent); }

/* ---------- Top bar & status pill ---------- */

#topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 20;
  padding: calc(var(--safe-top) + 6px) calc(var(--safe-right) + 12px) 6px calc(var(--safe-left) + 12px);
  min-height: calc(var(--safe-top) + var(--topbar-h));
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(30, 30, 28, 0.94);
  -webkit-backdrop-filter: saturate(140%) blur(18px);
  backdrop-filter: saturate(140%) blur(18px);
  border-bottom: 1px solid var(--hairline);
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  max-width: 100%;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--hairline);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
}
.pill:active { background: var(--surface-3); }

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

.pill-dot {
  flex: none;
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--text-3);
}
.pill.is-live .pill-dot { background: var(--live); box-shadow: 0 0 0 3px rgba(50, 208, 116, 0.18); }
.pill.is-readonly .pill-dot { background: var(--amber); box-shadow: 0 0 0 3px rgba(245, 190, 69, 0.16); }
.pill.is-checking .pill-dot { animation: pulse 1.1s ease-in-out infinite; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.25; }
}

/* ---------- Main column ---------- */

#view {
  display: block;
  outline: none;
  max-width: calc(var(--column) + 2 * var(--gutter));
  margin: 0 auto;
  padding:
    calc(var(--safe-top) + var(--topbar-h) + 14px)
    calc(var(--safe-right) + var(--gutter))
    calc(var(--safe-bottom) + var(--tabbar-h) + 40px)
    calc(var(--safe-left) + var(--gutter));
}

.large-title {
  margin: 8px 0 4px;
  font-size: 32px;
  font-weight: 700;
  letter-spacing: -0.01em;
  line-height: 1.15;
}

.subtitle {
  margin: 0 0 18px;
  color: var(--text-2);
  font-size: 17px;
}

.quiet { color: var(--text-3); }
.secondary { color: var(--text-2); }

.loading {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 28px 0;
  color: var(--text-2);
  font-size: 17px;
}

.problem {
  margin: 12px 0;
  color: var(--problem);
  font-size: 17px;
  line-height: 1.4;
}
.problem:empty { display: none; }

.empty {
  margin: 36px 0;
  color: var(--text-2);
  font-size: 18px;
  line-height: 1.5;
  text-align: center;
}

.section-label {
  margin: 30px 0 10px;
  color: var(--text-3);
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 50px;
  padding: 0 20px;
  border: 1px solid var(--hairline-strong);
  border-radius: 13px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 17px;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}
.btn:active { background: var(--surface-3); }

.btn.primary {
  border-color: transparent;
  background: var(--accent);
  color: #fff;
}
.btn.primary:active { background: var(--accent-pressed); }

.btn.wide { display: flex; width: 100%; }

.btn.link {
  min-height: 44px;
  padding: 0 4px;
  border: none;
  background: none;
  color: var(--accent);
  font-weight: 500;
}
.btn.link:active { opacity: 0.6; background: none; }

.btn[disabled], .btn.is-disabled { opacity: 0.42; }
.btn[disabled] { cursor: default; }

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 16px;
}
.btn-row .btn { flex: 1 1 0; min-width: 0; padding: 0 12px; }

.spinner {
  flex: none;
  display: inline-block;
  width: 20px; height: 20px;
  border: 2.5px solid rgba(255, 255, 255, 0.22);
  border-top-color: currentColor;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

/* ---------- Read-only notice ---------- */

.notice {
  margin: 6px 0 18px;
  padding: 16px;
  border: 1px solid var(--hairline);
  border-radius: 14px;
  background: var(--surface);
}
.notice-line {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin: 0;
  color: var(--text-2);
  font-size: 17px;
  line-height: 1.4;
}
.notice-line::before {
  content: "";
  flex: none;
  width: 9px; height: 9px;
  margin-top: 7px;
  border-radius: 50%;
  background: var(--amber);
}
.notice .btn { margin-top: 14px; }
.notice .problem { margin: 10px 0 0; }

/* ---------- Book tree ---------- */

.book-head { margin: 10px 0 20px; }
.book-title {
  margin: 0;
  font-family: var(--prose);
  font-size: 32px;
  font-weight: 400;
  line-height: 1.2;
}
.book-meta { margin: 6px 0 0; color: var(--text-3); font-size: 16px; }

.tree-section {
  margin: 0 0 14px;
  border: 1px solid var(--hairline);
  border-radius: 14px;
  background: var(--surface);
  overflow: hidden;
}
.tree-section.is-out { opacity: 0.62; }

.tree-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 48px;
  margin: 0;
  padding: 10px 16px;
  border: none;
  background: none;
  text-align: left;
  cursor: pointer;
}
.tree-row:active { background: var(--surface-2); }

.tree-chapter { min-height: 64px; padding-top: 14px; padding-bottom: 12px; }
.tree-chapter .tree-main { flex: 1; min-width: 0; }
.tree-title { display: block; font-size: 19px; font-weight: 600; line-height: 1.25; }
.tree-plan {
  display: block;
  margin-top: 3px;
  color: var(--text-3);
  font-family: var(--prose);
  font-size: 16px;
  font-style: italic;
}
.tree-words {
  flex: none;
  color: var(--text-3);
  font-size: 15px;
  font-variant-numeric: tabular-nums;
}
.chevron { flex: none; color: var(--text-3); font-size: 22px; line-height: 1; }

.tree-scenes { border-top: 1px solid var(--hairline); padding: 4px 0; }
.tree-scene { padding-left: 20px; }
.tree-scene .tree-scene-title {
  flex: 1;
  min-width: 0;
  color: var(--text-2);
  font-size: 17px;
  line-height: 1.3;
}
.tree-scene.is-dim .tree-scene-title { color: var(--text-3); }

.tag {
  display: inline-block;
  margin-left: 8px;
  padding: 1px 8px;
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text-3);
  font-size: 13px;
  font-weight: 600;
  vertical-align: 2px;
}

.pov-dot {
  flex: none;
  display: inline-block;
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--text-3);
}

.planning { margin: 22px 0 0; color: var(--text-3); font-size: 15px; text-align: center; }

/* ---------- Chapter reading ---------- */

.chapter-top { display: flex; align-items: center; margin: 0 0 6px; }
.chapter-head { margin: 8px 0 30px; text-align: center; }
.chapter-title {
  margin: 0;
  font-family: var(--prose);
  font-size: 31px;
  font-weight: 400;
  line-height: 1.2;
}
.chapter-plan {
  margin: 8px 0 0;
  color: var(--text-3);
  font-family: var(--prose);
  font-size: 17px;
  font-style: italic;
}

.scene { margin: 0 0 40px; }
.scene + .scene { padding-top: 26px; border-top: 1px solid var(--hairline); }

.scene-head {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 0 14px;
}
.scene-title {
  flex: 1;
  min-width: 0;
  margin: 0;
  color: var(--text-2);
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.02em;
  line-height: 1.3;
}
.scene.is-muted .scene-title { color: var(--text-3); }
.scene-head .btn.link { flex: none; font-size: 16px; }
.scene-head .btn.link.is-disabled { color: var(--text-3); opacity: 1; }

.prose {
  font-family: var(--prose);
  font-size: 21px;
  line-height: 1.55;
  color: var(--text);
  overflow-wrap: break-word;
}
.scene.is-muted .prose { color: var(--text-2); }

.para {
  margin: 0 -10px 0.8em;
  padding: 2px 10px;
  border-radius: 8px;
  cursor: pointer; /* iOS only sends taps to elements that look clickable */
  touch-action: manipulation;
}
.para:active { background: rgba(255, 255, 255, 0.045); }
.para.is-center { text-align: center; }
.para.is-right { text-align: right; }
.para.is-break {
  color: var(--text-3);
  text-align: center;
  letter-spacing: 0.5em;
  padding-left: calc(10px + 0.5em);
}
.para.is-saved { animation: saved 1.4s ease-out; }
@keyframes saved {
  0% { background: rgba(61, 139, 253, 0.16); }
  100% { background: transparent; }
}

.prose strong { font-weight: 700; }
.prose em { font-style: italic; }
.prose u { text-decoration-thickness: 1px; text-underline-offset: 3px; }

.chapter-foot {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: 36px 0 0;
  padding-top: 24px;
  border-top: 1px solid var(--hairline);
}
.chapter-foot .btn { justify-content: space-between; text-align: left; }
.chapter-foot .btn .dir { flex: none; color: var(--text-3); font-weight: 500; }
.chapter-foot .btn.primary .dir { color: rgba(255, 255, 255, 0.8); }
.chapter-foot .btn span:first-child { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Cards (line edit, notes, files, audio) ---------- */

.card {
  margin: 0 0 14px;
  padding: 16px;
  border: 1px solid var(--hairline);
  border-radius: 14px;
  background: var(--surface);
  transition: opacity 0.25s ease, transform 0.25s ease;
}
.card.is-leaving { opacity: 0; transform: scale(0.98); }

.card-head {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin: 0 0 8px;
}
.card-title {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  line-height: 1.3;
}

.kind {
  flex: none;
  padding: 2px 9px;
  border-radius: 7px;
  background: var(--surface-2);
  color: var(--text-2);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.card-note {
  margin: 0 0 10px;
  color: var(--text-2);
  font-size: 17px;
  line-height: 1.45;
}

.labels { display: flex; flex-wrap: wrap; gap: 6px; margin: 0 0 12px; }
.label-chip {
  padding: 2px 10px;
  border: 1px solid var(--hairline-strong);
  border-radius: 999px;
  color: var(--text-2);
  font-size: 14px;
}

.version {
  margin: 10px 0 0;
  padding: 12px 14px;
  border-radius: 11px;
  background: var(--bg);
}
.version-label {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 4px;
  color: var(--text-3);
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}
.version-label .now {
  color: var(--text-3);
  font-weight: 500;
  letter-spacing: 0;
  text-transform: none;
}
.version-text {
  margin: 0;
  font-family: var(--prose);
  font-size: 19px;
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: break-word;
}
.version-text .context { color: var(--text-3); }
.version-text del { text-decoration-thickness: 1.5px; color: var(--text-2); }
.version.is-editor { box-shadow: inset 3px 0 0 var(--accent); }

.group-label {
  margin: 26px 0 10px;
  color: var(--text-2);
  font-family: var(--prose);
  font-size: 20px;
}

.note-text {
  margin: 0 0 8px;
  font-size: 18px;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: break-word;
}
.card.is-done .note-text { color: var(--text-3); }
.meta { margin: 0; color: var(--text-3); font-size: 15px; line-height: 1.4; }

/* Files */
.file-row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 64px;
  margin: 0 0 10px;
  padding: 12px 16px;
  border: 1px solid var(--hairline);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
}
.file-row:active { background: var(--surface-2); }
.file-main { flex: 1; min-width: 0; }
.file-name { display: block; font-size: 17px; font-weight: 500; line-height: 1.3; overflow-wrap: anywhere; }
.file-row .kind { align-self: center; }
.file-main .meta { display: block; margin-top: 2px; }
.files-folder { margin: -2px 0 12px; }

.compile-hint { margin: 10px 0 0; text-align: center; }
.lineedit-count { margin: -8px 0 4px; }
.card > .btn.link { margin: 6px 0 -8px -4px; }
.field-group { display: flex; flex-direction: column; gap: 12px; }
.field-group .meta:empty { display: none; }

.compile-status {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 14px 0 0;
  color: var(--text-2);
  font-size: 17px;
  font-variant-numeric: tabular-nums;
}

details.log {
  margin: 14px 0 0;
  border: 1px solid var(--hairline);
  border-radius: 12px;
  background: var(--surface);
}
details.log summary {
  min-height: 48px;
  padding: 13px 16px;
  color: var(--text-2);
  font-size: 16px;
  cursor: pointer;
  list-style-position: inside;
}
details.log pre {
  margin: 0;
  padding: 0 16px 16px;
  color: var(--text-2);
  font: 14px/1.5 ui-monospace, "SF Mono", Menlo, monospace;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* Audio */
.audio-title { margin: 0 0 10px; font-size: 19px; font-weight: 600; }
.card audio { display: block; width: 100%; margin: 4px 0 6px; }
.progress {
  height: 8px;
  margin: 10px 0 8px;
  border-radius: 4px;
  background: var(--surface-3);
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  width: 0;
  border-radius: 4px;
  background: var(--accent);
  transition: width 0.4s ease;
}
.progress.is-unknown .progress-fill { width: 30%; animation: slide 1.4s ease-in-out infinite; }
@keyframes slide {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(340%); }
}

/* ---------- Tab bar ---------- */

#tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 20;
  display: flex;
  padding: 0 var(--safe-right) var(--safe-bottom) var(--safe-left);
  background: rgba(30, 30, 28, 0.96);
  -webkit-backdrop-filter: saturate(140%) blur(18px);
  backdrop-filter: saturate(140%) blur(18px);
  border-top: 1px solid var(--hairline);
}

.tab {
  flex: 1 1 0;
  min-width: 0;
  min-height: var(--tabbar-h);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 6px 0 4px;
  border: none;
  background: none;
  color: var(--text-3);
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
}
.tab svg {
  width: 27px; height: 27px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.tab span { white-space: nowrap; }
.tab[aria-current="page"] { color: var(--accent); }
.tab:active { opacity: 0.6; }

/* ---------- Toast ---------- */

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-bottom) + var(--tabbar-h) + 14px);
  z-index: 30;
  max-width: calc(100% - 32px);
  padding: 12px 18px;
  border: 1px solid var(--hairline-strong);
  border-radius: 14px;
  background: var(--surface-3);
  color: var(--text);
  font-size: 16px;
  line-height: 1.35;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 8px);
  transition: opacity 0.2s ease, transform 0.2s ease;
}
#toast.is-shown { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Sheets ---------- */

.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(0, 0, 0, 0.55);
  animation: fade-in 0.2s ease;
}

.sheet {
  position: fixed;
  left: 0; right: 0;
  top: calc(var(--vvtop) + var(--safe-top) + 10px);
  bottom: var(--kb);
  z-index: 41;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  border-radius: 16px 16px 0 0;
  background: var(--surface-2);
  box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.45);
  animation: rise 0.24s ease-out;
}

.sheet-head {
  flex: none;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 58px;
  padding: 4px 8px;
  border-bottom: 1px solid var(--hairline);
}
.sheet-head .btn.link { flex: none; min-width: 72px; padding: 0 10px; font-size: 17px; }
.sheet-head .sheet-action { font-weight: 700; }
.sheet-head .sheet-action[hidden] { display: inline-flex; visibility: hidden; }
.sheet-titles { flex: 1; min-width: 0; text-align: center; }
.sheet-title { margin: 0; font-size: 17px; font-weight: 600; }
.sheet-subtitle {
  margin: 2px 0 0;
  color: var(--text-3);
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sheet-body {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px calc(var(--safe-right) + 16px) calc(var(--safe-bottom) + 16px) calc(var(--safe-left) + 16px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
html.kb-open .sheet-body { padding-bottom: 14px; }

.sheet-body .problem { margin: 0; }
.sheet-body .lead { margin: 0; color: var(--text-2); font-size: 17px; line-height: 1.45; }

.editor {
  flex: 1;
  width: 100%;
  min-height: 150px;
  padding: 14px;
  border: 1px solid var(--hairline-strong);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  font-family: var(--prose);
  font-size: 20px;
  line-height: 1.5;
  resize: none;
  -webkit-appearance: none;
  appearance: none;
}
.editor:focus { outline: none; border-color: rgba(61, 139, 253, 0.7); }
.editor.is-short { flex: none; min-height: 120px; }
.editor[readonly] { color: var(--text-2); }

.field { display: flex; flex-direction: column; gap: 6px; }
.field-label { color: var(--text-3); font-size: 14px; font-weight: 700; letter-spacing: 0.05em; text-transform: uppercase; }
.field select {
  width: 100%;
  min-height: 50px;
  padding: 0 14px;
  border: 1px solid var(--hairline-strong);
  border-radius: 12px;
  background: var(--bg);
  font-size: 17px;
  -webkit-appearance: none;
  appearance: none;
}

.conflict-version { padding: 12px 14px; border-radius: 12px; background: var(--bg); }
.conflict-version .version-label { margin-bottom: 6px; }
.conflict-version .version-text { font-size: 19px; }
.conflict-version .editor { min-height: 130px; padding: 0; border: none; background: transparent; font-size: 19px; }
.sheet-body .btn-row { margin-top: 4px; }

@keyframes rise {
  from { transform: translateY(28px); opacity: 0; }
  to { transform: none; opacity: 1; }
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

/* ---------- Motion preferences ---------- */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  .spinner, .pill.is-checking .pill-dot { animation: none; }
}

/* Larger phones and landscape: a little more air. */
@media (min-width: 600px) {
  :root { --gutter: 28px; }
  .prose { font-size: 22px; }
}
