:root {
  --page-gutter: 1.25rem;
  --page-max: 1180px;
  --page-width: min(var(--page-max), calc(100% - 2 * var(--page-gutter)));
  --page-edge-fade: linear-gradient(to right, transparent, #000 var(--page-gutter), #000 calc(100% - var(--page-gutter)), transparent);
  --page-end-space: 1.15rem;
  --section-space: 1.5rem;
  --radius-lg: 16px;
  --radius: 10px;
  --theme-danger-seed: #f09595;
}

:root:not([data-theme]),
[data-theme="coral"] {
  --theme-base: #392322;
}

[data-theme="lavender"] {
  --theme-base: #1f1c38;
}

[data-theme="sage"] {
  --theme-base: #1a2c23;
}

[data-theme="orange"] {
  --theme-base: #3b2612;
}

:root,
[data-theme] {
  color-scheme: light dark;
  --theme-canvas: oklch(from var(--theme-base) 0.88 calc(c * 2.2) h);
  --theme-canvas-deep: oklch(from var(--theme-base) 0.72 calc(c * 2.6) h);
  --theme-glow: oklch(from var(--theme-base) 0.97 calc(c * 2.0) calc(h + 8));
  --theme-surface: oklch(from var(--theme-base) 0.82 calc(c * 2.5) h);
  --theme-surface-raised: oklch(from var(--theme-base) 0.84 calc(c * 2.5) h);
  --theme-surface-hover: oklch(from var(--theme-base) 0.86 calc(c * 3.0) h);
  --theme-text: oklch(from var(--theme-base) 0.24 calc(c * 2) h);
  --theme-heading: oklch(from var(--theme-base) 0.26 calc(c * 0.94) calc(h + 3.46));
  --theme-text-muted: color-mix(in srgb, var(--theme-text) 72%, var(--theme-canvas));
  --theme-text-subtle: color-mix(in srgb, var(--theme-text) 55%, var(--theme-canvas));
  --theme-border: color-mix(in srgb, var(--theme-text) 14%, transparent);
  --theme-border-strong: color-mix(in srgb, var(--theme-text) 26%, transparent);
  --theme-accent: oklch(from var(--theme-base) 0.55 calc(c * 4) h);
  --theme-accent-strong: oklch(from var(--theme-base) 0.4 calc(c * 3.5) h);
  --theme-accent-deep: oklch(from var(--theme-base) 0.3 calc(c * 3) h);
  --theme-accent-contrast: var(--theme-surface);
  --theme-accent-soft: color-mix(in srgb, var(--theme-accent) 18%, white);
  --theme-decor: color-mix(in srgb, var(--theme-accent) 35%, var(--theme-canvas));
  --theme-control-border: color-mix(in srgb, var(--theme-accent) 48%, var(--theme-canvas));
  --theme-control-text-muted: color-mix(in srgb, var(--theme-accent-strong) 72%, var(--theme-canvas));
  --theme-danger: color-mix(in oklch, var(--theme-danger-seed) 68%, black);
  --theme-danger-soft: color-mix(in srgb, var(--theme-danger-seed) 20%, white);
  --theme-danger-border: color-mix(in srgb, var(--theme-danger-seed) 55%, white);
  --theme-highlight: #dac69b;
  --theme-highlight-strong: #926010;
}

[data-color-scheme="light"] {
  color-scheme: light;
}

[data-color-scheme="dark"],
[data-color-scheme="dark"] [data-theme] {
  color-scheme: dark;
  --theme-canvas: oklch(from var(--theme-base) calc(l * 0.8) calc(c * 0.8) h);
  --theme-canvas-deep: oklch(from var(--theme-base) calc(l * 0.65) calc(c * 0.5) h);
  --theme-glow: oklch(from var(--theme-base) 0.36 calc(c * 1.69) calc(h + 2.72));
  --theme-surface: oklch(from var(--theme-base) calc(l * 0.9) calc(c * 1.15) h);
  --theme-surface-raised: oklch(from var(--theme-base) l  calc(c * 1.35) h);
  --theme-surface-hover: oklch(from var(--theme-base) calc(l * 1.1) calc(c * 1.5) h);
  --theme-text: oklch(from var(--theme-base) 0.94 calc(c * 1.4) h);
  --theme-heading: oklch(from var(--theme-base) 0.95 calc(c * 0.46) calc(h + 9.68));
  --theme-text-muted: color-mix(in srgb, var(--theme-text) 72%, var(--theme-canvas));
  --theme-text-subtle: color-mix(in srgb, var(--theme-text) 55%, var(--theme-canvas));
  --theme-border: color-mix(in srgb, var(--theme-text) 10%, transparent);
  --theme-border-strong: color-mix(in srgb, var(--theme-text) 20%, transparent);
  --theme-accent: oklch(from var(--theme-base) 0.72 calc(c * 4) h);
  --theme-accent-strong: oklch(from var(--theme-base) 0.82 calc(c * 3) h);
  --theme-accent-deep: oklch(from var(--theme-base) 0.76 calc(c * 2.4) h);
  --theme-accent-contrast: var(--theme-canvas-deep);
  --theme-accent-soft: color-mix(in srgb, var(--theme-accent) 20%, var(--theme-canvas));
  --theme-decor: color-mix(in srgb, var(--theme-accent) 25%, var(--theme-canvas));
  --theme-control-border: color-mix(in srgb, var(--theme-accent) 35%, var(--theme-canvas));
  --theme-control-text-muted: color-mix(in srgb, var(--theme-accent-strong) 72%, var(--theme-canvas));
  --theme-danger: var(--theme-danger-seed);
  --theme-danger-soft: color-mix(in srgb, var(--theme-danger-seed) 20%, var(--theme-canvas));
  --theme-danger-border: color-mix(in srgb, var(--theme-danger-seed) 40%, var(--theme-canvas));
  --theme-highlight: #4a3a19;
  --theme-highlight-strong: #efbd6d;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  min-width: 320px;
  min-height: 100%;
}

body {
  display: flex;
  min-width: 320px;
  min-height: 100vh;
  flex-direction: column;
  margin: 0;
  background: radial-gradient(circle at 88% 0, var(--theme-glow), transparent 30rem), var(--theme-canvas);
  color: var(--theme-text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  line-height: 1.55;
}

[data-color-scheme="dark"] body {
  background: radial-gradient(circle at 88% 0, var(--theme-glow), transparent 30rem), var(--theme-canvas-deep);
}

body.loading {
  cursor: progress;
}

header,
main,
.footer-content {
  width: var(--page-width);
  margin-inline: auto;
}

.toolbar {
  width: calc(var(--page-width) + 2 * var(--page-gutter));
  margin-inline: auto;
  padding: 0.4rem var(--page-gutter) 0.7rem;
  -webkit-mask-image: var(--page-edge-fade);
  mask-image: var(--page-edge-fade);
}

.search-field {
  width: 100%;
  min-width: 0;
  height: 35px;
  flex: 1;
  border: 1px solid var(--theme-border-strong);
  border-radius: var(--radius-lg);
  padding: 6px 11px;
  background: color-mix(in srgb, var(--theme-surface) 88%, transparent);
  color: var(--theme-text);
}

main {
  flex: 1 0 auto;
}

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

input[type="checkbox"] {
  appearance: none; display: grid; place-content: center;
  border: 1px solid var(--checkbox-color, var(--theme-accent)); border-radius: 3px;
  background: transparent; color: var(--theme-accent-contrast);
}

input[type="checkbox"]::before {
  width: 9px; height: 9px; background: currentColor;
  clip-path: polygon(14% 44%, 0 59%, 40% 100%, 100% 18%, 84% 5%, 38% 72%);
  content: ""; transform: scale(0);
}

input[type="checkbox"]:checked { background: var(--checkbox-color, var(--theme-accent)); }
input[type="checkbox"]:checked::before { transform: scale(1); }

button,
a {
  -webkit-tap-highlight-color: transparent;
}

a {
  color: inherit;
}

:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--theme-accent) 35%, transparent) !important;
  outline-offset: -3px !important;
}

input::placeholder,
textarea::placeholder {
  color: var(--theme-text-subtle);
  opacity: 1;
}

.header-row {
  display: flow-root;
}

.header-copy {
  min-width: 0;
}

header {
  padding: 1.25rem 0 0.7rem;
}

header h1 {
  margin: 0 0 0.2rem;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 42px;
  font-weight: 500;
  line-height: 1.2;
}

header p {
  margin: 0;
  color: var(--theme-text-muted);
  font-size: 13px;
}

.file-actions {
  float: right;
  display: flex;
  width: max-content;
  flex-direction: column;
  align-items: stretch;
  margin-left: 0.75rem;
}

.file-action-buttons {
  display: flex;
  align-items: center;
  gap: 6px;
}

.btn {
  min-height: 31px;
  border: 1px solid color-mix(in srgb, var(--theme-accent) 45%, var(--theme-border-strong));
  border-radius: 8px;
  padding: 5px 10px;
  background: color-mix(in srgb, var(--theme-surface) 92%, transparent);
  color: var(--theme-accent-strong);
  cursor: pointer;
  font-size: 12px;
  font-weight: 650;
  transition: border-color 140ms ease, background 140ms ease, transform 140ms ease;
}

.btn:hover:not(:disabled) {
  border-color: var(--theme-accent);
  background: light-dark(var(--theme-surface-hover), var(--theme-surface-raised));
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

.btn:disabled {
  cursor: default;
  opacity: 0.38;
}

.btn.primary,
.file-actions .file-action-buttons .btn.confirming {
  border: 1px solid var(--theme-accent-strong);
  background: var(--theme-accent-strong);
  color: var(--theme-accent-contrast);
}

.btn.primary:focus-visible,
.file-actions .file-action-buttons .btn.confirming:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--theme-accent-contrast) 35%, transparent) !important;
}

.btn.primary:hover:not(:disabled),
.file-actions .file-action-buttons .btn.confirming:hover:not(:disabled) {
  border-color: light-dark(color-mix(in srgb, var(--theme-accent-strong) 82%, white), color-mix(in srgb, var(--theme-accent-strong) 82%, black));
  background: light-dark(color-mix(in srgb, var(--theme-accent-strong) 82%, white), color-mix(in srgb, var(--theme-accent-strong) 82%, black));
  color: var(--theme-accent-contrast);
}

:is(.toolbar .toolbar-actions .btn.icon-button, .controls .check-controls #undo, .controls .check-controls #redo) {
  font-size: 18px;
  line-height: 1;
}

.toolbar .toolbar-actions .btn.icon-button {
  width: 34px;
  padding-inline: 0;
}

.reset-button { display: inline-grid; align-items: center; }
.reset-button > span { grid-area: 1 / 1; }
.reset-confirm-label, .reset-button.confirming .reset-default-label { visibility: hidden; }
.reset-button.confirming .reset-confirm-label { visibility: visible; }

.file-actions input[type="file"] {
  display: none;
}

.file-messages {
  margin-top: 4px;
  color: var(--theme-text-subtle);
  font-size: 11px;
  text-align: right;
}

.file-status {
  color: var(--theme-accent-strong);
  font-weight: 650;
}

.file-status:empty {
  display: none;
}

.file-status.error {
  color: var(--theme-danger);
}

.file-status:not(:empty) + .file-auto-save {
  display: none;
}

.filter-button {
  padding: 0.48rem 0.72rem;
  border: 1px solid var(--theme-border);
  border-radius: 999px;
  background: var(--theme-surface);
  color: var(--theme-text-muted);
  cursor: pointer;
  font-size: 0.76rem;
  font-weight: 700;
  line-height: 1.5;
  text-transform: none;
  white-space: nowrap;
  user-select: none;
  transition: background 140ms ease, border-color 140ms ease, color 140ms ease;
}

.filter-button:hover { border-color: var(--theme-accent); }

.filter-button[aria-pressed="true"] {
  border-color: var(--theme-accent-deep);
  background: var(--theme-accent-deep);
  color: var(--theme-canvas);
}

.filter-button[aria-pressed="true"]:focus-visible {
  outline-color: var(--theme-accent-contrast) !important;
}

.filter-bar {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 0 var(--page-gutter);
  cursor: grab;
  overscroll-behavior-x: contain;
  scrollbar-width: none;
  touch-action: pan-y;
  user-select: none;
  margin-left: calc(-1 * var(--page-gutter));
  margin-right: calc(-1 * var(--page-gutter));
}

.filter-bar::-webkit-scrollbar {
  display: none;
}

.filter-bar.is-dragging {
  cursor: grabbing;
}

.filter-bar > * {
  flex: 0 0 auto;
}

.filter-scroll-indicator {
  position: relative;
  height: 3px;
  margin: 8px 0.35rem 0;
  overflow: hidden;
  border-radius: 99px;
  background: var(--theme-border);
}

.filter-scroll-indicator.is-hidden {
  display: none;
}

.filter-scroll-indicator > span {
  position: absolute;
  inset: 0 auto 0 0;
  min-width: 40px;
  border-radius: inherit;
  background: var(--theme-accent);
  transform: translateX(0);
  will-change: transform, width;
}

.editable-heading {
  border-radius: 4px;
  cursor: text;
  outline: 1px dashed transparent;
  outline-offset: 3px;
  transition: background 0.15s;
}

.editable-heading:empty::before {
  color: var(--theme-text-subtle);
  content: attr(data-placeholder);
}

.editable-heading:focus {
  background: var(--theme-surface);
}

@media (hover: hover) {
  .editable-heading:hover {
    background: var(--theme-surface-hover);
    outline-color: var(--theme-border-strong);
  }
}

.footer-content {
  padding-top: var(--page-end-space);
  padding-bottom: var(--page-end-space);
}

.footer-help {
  width: 100%;
  padding: 1.2rem;
  border: 1px solid var(--theme-border);
  border-radius: 16px;
  background: var(--theme-surface-raised);
}

.footer-help > div {
  display: flex;
  gap: 0.75rem;
}

.footer-help > div > span {
  display: grid;
  width: 1.6rem;
  height: 1.6rem;
  place-items: center;
  flex: 0 0 auto;
  border-radius: 50%;
  background: var(--theme-accent-strong);
  color: var(--theme-accent-contrast);
  font-family: Georgia, "Times New Roman", serif;
  font-size: 12px;
  font-weight: 700;
}

.footer-help h2 {
  margin: 0 0 0.18rem;
  color: var(--theme-text);
  font-size: 13px;
  line-height: 1.4;
}

.footer-help p {
  margin: 0;
  color: var(--theme-text-muted);
}

.footer-help > p {
  margin: 0.8rem 0 0 2.35rem;
  padding-top: 0.8rem;
  border-top: 1px solid var(--theme-border);
}

.footer-pages {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  border-top: 1px solid var(--theme-border);
}

.useful-pages-link {
  display: block;
  min-width: 0;
  padding: 1.25rem clamp(1.1rem, 4vw, 3.5rem);
  color: var(--theme-accent-strong);
  font-size: 12px;
  font-weight: 700;
  text-decoration: none;
}

.footer-controls {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.color-scheme-toggle {
  display: grid;
  width: 31px;
  height: 31px;
  place-items: center;
  flex: 0 0 auto;
  border: 1px solid var(--theme-control-border);
  border-radius: 8px;
  padding: 0;
  background: var(--theme-surface);
  color: var(--theme-accent-strong);
  cursor: pointer;
}

.color-scheme-toggle svg {
  width: 15px;
  height: 15px;
}

.color-scheme-toggle .icon-moon {
  fill: currentColor;
}

.color-scheme-toggle .icon-sun {
  display: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}

[data-color-scheme="dark"] .color-scheme-toggle .icon-moon {
  display: none;
}

[data-color-scheme="dark"] .color-scheme-toggle .icon-sun {
  display: block;
}

.theme-picker {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--theme-text-subtle);
  font-size: 12px;
  font-weight: 650;
}

.footer-pages .theme-picker {
  margin-right: max(4rem, env(safe-area-inset-right));
}

.theme-picker select {
  min-height: 31px;
  border: 1px solid var(--theme-control-border);
  border-radius: 8px;
  padding: 4px 24px 4px 8px;
  background: var(--theme-surface);
  color: var(--theme-accent-strong);
  cursor: pointer;
}

.scroll-to-top {
  position: fixed;
  z-index: 40;
  right: calc(0.75rem + env(safe-area-inset-right));
  bottom: calc(1rem + env(safe-area-inset-bottom));
  display: grid;
  width: 38px;
  height: 38px;
  place-items: center;
  border: 1px solid var(--theme-border-strong);
  border-radius: 50%;
  background: var(--theme-accent-strong);
  color: var(--theme-accent-contrast);
  cursor: pointer;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: translateY(8px);
  transition: opacity 0.15s, transform 0.15s, visibility 0.15s;
}

.scroll-to-top svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.scroll-to-top.visible {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transform: translateY(0);
}

@media (max-width: 760px) {
  :root {
    --page-gutter: 0.75rem;
  }

  .scroll-to-top {
    right: calc(0.5rem + env(safe-area-inset-right));
  }
}

@media (max-width: 600px) {
  .header-row {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
  }

  .header-copy {
    order: 1;
  }

  .file-actions {
    float: none;
    order: 2;
    width: 100%;
    margin-left: 0;
  }

  .file-action-buttons {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .file-action-buttons .btn {
    width: 100%;
    min-width: 0;
    padding-left: 4px;
    padding-right: 4px;
    font-size: 12px;
    white-space: nowrap;
  }

  .file-messages {
    width: 100%;
    text-align: left;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
  }
}
