:root {
  --bg: #ffffff;
  --fg: #262220;
  --placeholder: #a39c96;
  --selection-bg: rgba(0, 0, 0, 0.12);
  --indicator-fg: #a39c96;
  --sidebar-fg: #262220;
  --sidebar-muted: #a39c96;
  --sidebar-hover-bg: rgba(0, 0, 0, 0.05);
  --rule: rgba(38, 34, 32, 0.14);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1917;
    --fg: #e8e2da;
    --placeholder: #6f6a64;
    --selection-bg: rgba(255, 255, 255, 0.18);
    --indicator-fg: #6f6a64;
    --sidebar-fg: #e8e2da;
    --sidebar-muted: #6f6a64;
    --sidebar-hover-bg: rgba(255, 255, 255, 0.06);
    --rule: rgba(232, 226, 218, 0.16);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
}

body {
  transition: background-color 0.2s ease;
}

::selection {
  background: var(--selection-bg);
}

main {
  display: flex;
  justify-content: center;
  width: 100%;
}

.page {
  width: 100%;
  max-width: 720px;
  margin: 0 24px;
  padding-top: 14vh;
}

.title,
#editor {
  display: block;
  width: 100%;
  border: none;
  outline: none;
  resize: none;
  overflow: hidden;
  background: transparent;
  color: var(--fg);
  font-family: "Times New Roman", Times, "Liberation Serif", serif;
  caret-color: var(--fg);
}

.title {
  padding: 0 8px 20px;
  font-size: 1.05rem;
  line-height: 1.4;
  text-align: center;
}

#editor {
  padding: 0 8px 40vh;
  font-size: 1.05rem;
  line-height: 1.85;
  text-align: justify;
  text-justify: inter-word;
}

.title::placeholder,
#editor::placeholder {
  color: var(--placeholder);
  font-style: italic;
}

.sidebar {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(280px, 90vw);
  overflow-y: auto;
  background: var(--bg);
  padding: 24px 18px 40px;
  transform: translateX(100%);
  transition: transform 0.25s ease;
  z-index: 25;
}

.sidebar.open {
  transform: translateX(0);
}

.corner-btn {
  position: fixed;
  right: 20px;
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: var(--sidebar-fg);
  opacity: 0.35;
  cursor: pointer;
  transition: opacity 0.2s ease;
  z-index: 30;
}

.corner-btn:hover,
.corner-btn:focus-visible {
  opacity: 1;
}

.corner-btn svg {
  width: 20px;
  height: 16px;
}

.corner-btn .frame {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.4;
}

.corner-btn .panel {
  fill: currentColor;
}

.corner-btn .stack-front {
  fill: var(--bg);
}

.sidebar-btn {
  bottom: 104px;
}

.files-btn {
  bottom: 62px;
}

.download-btn {
  bottom: 20px;
}

.download-btn .shaft,
.download-btn .head,
.download-btn .tray {
  fill: currentColor;
}

.file-menu {
  position: fixed;
  right: 62px;
  bottom: 62px;
  width: 220px;
  max-height: min(360px, 60vh);
  overflow-y: auto;
  background: var(--bg);
  border: 1px solid var(--rule);
  padding: 10px;
  transform-origin: bottom right;
  transform: scale(0.96);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s ease, transform 0.15s ease;
  z-index: 30;
}

.file-menu.open {
  opacity: 1;
  transform: scale(1);
  pointer-events: auto;
}

.file-menu-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
  padding: 0 2px;
}

.file-menu-title {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--sidebar-muted);
}

.new-doc-btn {
  width: 22px;
  height: 22px;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: var(--sidebar-fg);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
}

.new-doc-btn:hover {
  background: var(--sidebar-hover-bg);
}

.file-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.file-link {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  color: var(--sidebar-fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 0.85rem;
  padding: 7px 8px;
  border-radius: 4px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.file-link:hover,
.file-link:focus-visible {
  background: var(--sidebar-hover-bg);
}

.file-link.current {
  font-weight: 700;
}

.sidebar-title {
  margin: 0 0 16px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 0.95rem;
  font-weight: 600;
  line-height: 1.4;
  color: var(--sidebar-fg);
  overflow-wrap: break-word;
}

.chapter-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.chapter-sublist {
  list-style: none;
  margin: 2px 0 10px;
  padding-left: 14px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.outline-part {
  margin-top: 14px;
}

.outline-part:first-child {
  margin-top: 0;
}

.chapter-link {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  color: var(--sidebar-fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 0.88rem;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
}

.chapter-link:hover,
.chapter-link:focus-visible {
  background: var(--sidebar-hover-bg);
}

.part-link {
  font-weight: 700;
}

.chapter-empty {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 0.82rem;
  color: var(--sidebar-muted);
  line-height: 1.5;
}

.chapter-list:not(:empty) + .chapter-empty {
  display: none;
}

.save-indicator {
  position: fixed;
  bottom: 14px;
  left: 20px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 0.75rem;
  color: var(--indicator-fg);
  opacity: 0;
  transition: opacity 0.6s ease;
  pointer-events: none;
}

.save-indicator.visible {
  opacity: 1;
  transition: opacity 0.1s ease;
}

@media (max-width: 640px) {
  .page {
    padding-top: 10vh;
  }

  .title,
  #editor {
    font-size: 0.95rem;
  }
}

/* --- print / PDF export --------------------------------------------------
 *
 * A textarea's printed output is inconsistent across browsers, so the
 * download button builds a plain-HTML mirror of the title and body just
 * before printing (see beforeprint in app.js) and only that mirror is
 * shown on the page. Real paragraphs instead of raw tab characters also
 * means a proper text-indent instead of a printed tab's inconsistent width.
 */

.print-view {
  display: none;
}

@media print {
  body > *:not(.print-view) {
    display: none !important;
  }

  .print-view {
    display: block;
    color: #000;
  }

  #print-title {
    font-family: "Times New Roman", Times, "Liberation Serif", serif;
    font-weight: 400;
    font-size: 22pt;
    text-align: center;
    margin: 0 0 2em;
  }

  #print-body p {
    font-family: "Times New Roman", Times, "Liberation Serif", serif;
    font-size: 12pt;
    line-height: 1.6;
    text-align: justify;
    margin: 0;
  }

  #print-body p.indent {
    text-indent: 2em;
  }

  #print-body p.break {
    margin-top: 1.5em;
  }
}
