* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  color-scheme: dark;
  --ink: #f3efe6;
  --quiet: #a9b3b5;
  --shadow: #0b1018;
  --glass: rgb(17 24 32 / 88%);
  --hairline: rgb(255 255 255 / 14%);
  --line-height: 3rem;
  --editor-font: "Iowan Old Style", "Palatino Linotype", "Book Antiqua", Palatino, Georgia, serif;
  --ui-font: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--shadow);
}

body {
  position: relative;
  font-family: var(--ui-font);
  color: var(--ink);
}

button,
select,
input {
  font: inherit;
}

button,
select,
input[type="range"] {
  accent-color: #d8d0c4;
}

button:focus-visible,
select:focus-visible,
input:focus-visible,
textarea:focus-visible {
  outline: 2px solid rgb(243 239 230 / 90%);
  outline-offset: 3px;
}

.background {
  position: fixed;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  background: linear-gradient(145deg, #8ea6a0, #42535a 56%, #d8d0c4);
  transition: background 600ms ease;
}

.background.scene-snow { background: linear-gradient(145deg, #dde6e8, #a8b8c0 52%, #495a66); }
.background.scene-dusk { background: linear-gradient(145deg, #382f52, #8a5c78 52%, #d5a184); }
.background.scene-rain { background: linear-gradient(145deg, #18252d, #40505a 56%, #77838a); }
.background.scene-zen { background: linear-gradient(145deg, #b8a992, #667c74 52%, #d6c8b7); }
.background.scene-night { background: linear-gradient(145deg, #0b1018, #182536 58%, #38445a); }

#scene {
  display: block;
  width: 100%;
  height: 100%;
}

.editor-shell {
  position: relative;
  z-index: 1;
  width: min(100%, 900px);
  height: 100dvh;
  margin-inline: auto;
  padding-inline: clamp(1rem, 5vw, 4rem);
}

.editor {
  display: block;
  width: 100%;
  height: 100%;
  overflow-y: auto;
  resize: none;
  border: 0;
  outline: 0;
  background: transparent;
  color: rgb(243 239 230 / 94%);
  caret-color: #fffdf7;
  font-family: var(--editor-font);
  font-size: clamp(1.25rem, 1.1rem + 0.7vw, 1.72rem);
  font-weight: 400;
  line-height: var(--line-height);
  letter-spacing: 0.012em;
  text-shadow: 0 1px 9px rgb(0 0 0 / 35%);
  padding: calc(50dvh - (var(--line-height) / 2)) clamp(0.25rem, 2vw, 1.5rem);
  scrollbar-width: none;
  -webkit-mask-image: linear-gradient(to bottom, transparent 1%, #000 24%, #000 76%, transparent 99%);
  mask-image: linear-gradient(to bottom, transparent 1%, #000 24%, #000 76%, transparent 99%);
}

.editor::-webkit-scrollbar { display: none; }

.editor::placeholder {
  color: rgb(243 239 230 / 42%);
}

.caret-mirror {
  position: fixed;
  top: 0;
  left: -100000px;
  visibility: hidden;
  overflow-wrap: break-word;
  white-space: pre-wrap;
  word-break: normal;
}

.chrome {
  opacity: 0;
  pointer-events: none;
  transform: translateY(4px);
  transition: opacity 240ms ease, transform 240ms ease;
}

body.chrome-visible .chrome,
.chrome:focus-visible,
.chrome:hover {
  opacity: 0.78;
  pointer-events: auto;
  transform: translateY(0);
}

.settings-button,
.word-count {
  position: fixed;
  z-index: 10;
  border: 1px solid var(--hairline);
  background: rgb(11 16 24 / 52%);
  color: var(--ink);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}

.settings-button {
  top: max(1rem, env(safe-area-inset-top));
  right: max(1rem, env(safe-area-inset-right));
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  cursor: pointer;
}

.word-count {
  right: max(1rem, env(safe-area-inset-right));
  bottom: max(1rem, env(safe-area-inset-bottom));
  border-radius: 999px;
  padding: 0.55rem 0.9rem;
  color: rgb(243 239 230 / 78%);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.72rem;
  letter-spacing: 0.04em;
}

.word-count.hidden { display: none; }

.settings-panel {
  position: fixed;
  z-index: 30;
  top: 0;
  right: 0;
  width: min(390px, 100%);
  height: 100dvh;
  overflow-y: auto;
  padding: max(1.5rem, env(safe-area-inset-top)) 1.5rem max(1.5rem, env(safe-area-inset-bottom));
  border-left: 1px solid var(--hairline);
  background: var(--glass);
  box-shadow: -24px 0 60px rgb(0 0 0 / 25%);
  backdrop-filter: blur(24px) saturate(0.7);
  -webkit-backdrop-filter: blur(24px) saturate(0.7);
  transform: translateX(102%);
  transition: transform 280ms cubic-bezier(0.22, 1, 0.36, 1);
}

.settings-panel.open { transform: translateX(0); }

.settings-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 2.25rem;
}

.eyebrow {
  margin-bottom: 0.35rem;
  color: var(--quiet);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.66rem;
  letter-spacing: 0.22em;
  text-transform: uppercase;
}

.settings-header h1 {
  font-family: var(--editor-font);
  font-size: 1.55rem;
  font-weight: 400;
}

.icon-button,
.preview-close {
  border: 1px solid var(--hairline);
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}

.icon-button {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  font-size: 1.5rem;
  line-height: 1;
}

.setting-group {
  margin-bottom: 2rem;
  padding-top: 1.15rem;
  border-top: 1px solid var(--hairline);
}

.setting-group h2 {
  margin-bottom: 0.9rem;
  color: rgb(243 239 230 / 90%);
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.setting-group label:not(.toggle-row) {
  display: block;
  margin: 0.85rem 0 0.4rem;
  color: var(--quiet);
  font-size: 0.78rem;
}

.setting-group select,
.setting-group input[type="range"] { width: 100%; }

.setting-group select {
  padding: 0.65rem 0.7rem;
  border: 1px solid var(--hairline);
  border-radius: 5px;
  background: #172029;
  color: var(--ink);
}

.background-options {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.5rem;
}

.bg-option,
.action-button {
  border: 1px solid var(--hairline);
  border-radius: 5px;
  background: rgb(255 255 255 / 5%);
  color: rgb(243 239 230 / 82%);
  cursor: pointer;
  transition: background 180ms ease, border-color 180ms ease, color 180ms ease;
}

.bg-option {
  padding: 0.65rem 0.25rem;
  font-size: 0.76rem;
}

.bg-option:hover,
.bg-option.active,
.action-button:hover {
  border-color: rgb(255 255 255 / 32%);
  background: rgb(255 255 255 / 12%);
  color: var(--ink);
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.55rem 0;
  color: rgb(243 239 230 / 86%);
  font-size: 0.86rem;
}

.toggle-row input { width: 1rem; height: 1rem; }

.setting-toggles { display: grid; gap: 0.25rem; }
.actions { display: grid; gap: 0.6rem; }

.action-button {
  width: 100%;
  padding: 0.72rem 0.85rem;
  text-align: left;
}

.action-button.primary { background: rgb(216 208 196 / 14%); }
.action-button.danger { color: #e7b3ad; }

.preview {
  position: fixed;
  z-index: 20;
  inset: 0;
  display: none;
  overflow-y: auto;
  background: rgb(11 16 24 / 94%);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
}

.preview.open { display: block; }

.preview-close {
  position: sticky;
  z-index: 1;
  top: 1rem;
  left: calc(100% - 11rem);
  margin: 1rem;
  padding: 0.6rem 0.85rem;
  border-radius: 5px;
  background: rgb(17 24 32 / 88%);
}

.preview-content {
  width: min(100% - 2rem, 760px);
  margin: 0 auto;
  padding: clamp(3rem, 8vw, 6rem) 0 8rem;
  color: rgb(243 239 230 / 90%);
  font-family: var(--editor-font);
  font-size: 1.12rem;
  line-height: 1.85;
}

.preview-content > * + * { margin-top: 1.25rem; }
.preview-content h1,
.preview-content h2,
.preview-content h3 { line-height: 1.25; font-weight: 400; }
.preview-content ul,
.preview-content ol { padding-left: 1.4rem; }
.preview-content a { color: #cbdfe0; }
.preview-content code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.preview-content pre { overflow-x: auto; padding: 1rem; background: rgb(255 255 255 / 7%); }
.preview-content blockquote { padding-left: 1rem; border-left: 2px solid rgb(255 255 255 / 28%); color: var(--quiet); }

@media (hover: none), (pointer: coarse) {
  .settings-button {
    opacity: 0.38;
    pointer-events: auto;
    transform: none;
  }

  body.chrome-visible .settings-button,
  .settings-button:focus-visible { opacity: 0.88; }
}

@media (max-width: 640px) {
  :root { --line-height: 2.55rem; }

  .editor-shell { padding-inline: 0.8rem; }
  .editor { font-size: 1.18rem; }
  .word-count { font-size: 0.66rem; }
}

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