/* Ferrante Writer. A calm, quiet writing surface. One palette, one set of type
   choices, carried across every view for consistency. */

:root {
  --paper: #f6f4ef;
  --paper-raised: #fbfaf6;
  --ink: #2a2723;
  --ink-soft: #6b655c;
  --ink-faint: #a49c8f;
  --line: #e4dfd4;
  --line-strong: #d3ccbd;
  --accent: #3f5a73;
  --accent-soft: #7d92a6;
  --warm: #b0553f;
  --warm-soft: #cf9384;
  --focus-ring: rgba(63, 90, 115, 0.28);

  --serif: "Iowan Old Style", "Palatino Linotype", "Palatino", "Georgia", serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: "iA Writer Mono", "SFMono-Regular", "SF Mono", "JetBrains Mono", ui-monospace, monospace;

  --col: 34rem;
  --ease: cubic-bezier(0.4, 0, 0.2, 1);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button {
  font-family: inherit;
  cursor: pointer;
  color: var(--ink-soft);
  background: none;
  border: none;
  font-size: 0.86rem;
  padding: 0.4rem 0.6rem;
  border-radius: 6px;
  transition: background 0.15s var(--ease), color 0.15s var(--ease);
}
button:hover { background: var(--line); color: var(--ink); }
button:focus-visible { outline: 2px solid var(--focus-ring); outline-offset: 1px; }

.btn-primary {
  background: var(--accent);
  color: #fff;
  padding: 0.6rem 1.1rem;
  border-radius: 8px;
  font-size: 0.9rem;
}
.btn-primary:hover { background: #35506a; color: #fff; }
.btn-warm { color: var(--warm); }
.btn-warm:hover { background: rgba(176, 85, 63, 0.1); color: var(--warm); }

input, textarea {
  font-family: inherit;
  color: var(--ink);
  background: var(--paper-raised);
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  padding: 0.7rem 0.85rem;
  font-size: 0.95rem;
  width: 100%;
}
input:focus, textarea:focus {
  outline: none;
  border-color: var(--accent-soft);
  box-shadow: 0 0 0 3px var(--focus-ring);
}

/* ---------- centered gate views (setup / login / recovery) ---------- */
.gate {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 2rem;
}
.gate-card {
  width: 100%;
  max-width: 25rem;
  background: var(--paper-raised);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 2.2rem 2rem;
  box-shadow: 0 1px 2px rgba(0,0,0,0.03), 0 12px 40px rgba(60, 50, 35, 0.06);
}
.gate-title {
  font-family: var(--serif);
  font-size: 1.7rem;
  margin: 0 0 0.3rem;
  letter-spacing: -0.01em;
}
.gate-sub {
  color: var(--ink-soft);
  font-size: 0.9rem;
  line-height: 1.55;
  margin: 0 0 1.6rem;
}
.field { margin-bottom: 1rem; }
.field label {
  display: block;
  font-size: 0.78rem;
  color: var(--ink-soft);
  margin-bottom: 0.35rem;
  letter-spacing: 0.02em;
}
.gate .btn-primary { width: 100%; margin-top: 0.4rem; }
.gate-link {
  display: block;
  text-align: center;
  margin-top: 1.1rem;
  font-size: 0.82rem;
  color: var(--accent);
}
.notice {
  font-size: 0.84rem;
  border-radius: 8px;
  padding: 0.65rem 0.8rem;
  margin-bottom: 1rem;
  line-height: 1.5;
}
.notice-error { background: rgba(176, 85, 63, 0.1); color: #8a3f2d; }
.notice-info { background: rgba(63, 90, 115, 0.09); color: var(--accent); }

.recovery-code {
  font-family: var(--mono);
  font-size: 1.05rem;
  letter-spacing: 0.04em;
  background: var(--paper);
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  padding: 0.9rem;
  text-align: center;
  word-break: break-all;
  margin-bottom: 0.9rem;
  color: var(--ink);
}
.checkline { display: flex; align-items: flex-start; gap: 0.5rem; font-size: 0.84rem; color: var(--ink-soft); margin-bottom: 1rem; }
.checkline input { width: auto; margin-top: 0.15rem; }

/* ---------- app shell ---------- */
.app { display: flex; height: 100%; }

.sidebar {
  width: 16.5rem;
  flex-shrink: 0;
  background: transparent;
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  transition: margin-left 0.28s var(--ease);
}
.app.sidebar-hidden .sidebar { margin-left: -16.5rem; }

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.9rem 1rem 0.6rem;
}
.wordmark {
  font-family: var(--serif);
  font-size: 1.02rem;
  color: var(--ink);
  letter-spacing: -0.01em;
}
.new-btn {
  margin: 0.2rem 0.8rem 0.6rem;
  padding: 0.55rem;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  text-align: center;
  color: var(--ink-soft);
}
.new-btn:hover { background: var(--paper-raised); color: var(--ink); }

.piece-list { overflow-y: auto; flex: 1; padding: 0.2rem 0.5rem 1rem; }
.piece-item {
  padding: 0.55rem 0.6rem;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 0.15rem;
}
.piece-item:hover { background: var(--paper-raised); }
.piece-item.active { background: var(--line); }
.piece-item .pi-title {
  font-size: 0.9rem;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.piece-item .pi-date { font-size: 0.72rem; color: var(--ink-faint); margin-top: 0.15rem; }

.sidebar-foot {
  padding: 0.6rem 0.9rem;
  border-top: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  font-size: 0.78rem;
}

/* ---------- main / editor ---------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }

.topbar {
  height: 3rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 0.8rem;
  border-bottom: 1px solid transparent;
  transition: border-color 0.2s var(--ease);
}
.main:hover .topbar { border-color: var(--line); }
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 0.1rem; }

.editor-scroll { flex: 1; overflow-y: auto; }
.editor-wrap {
  max-width: var(--col);
  margin: 0 auto;
  padding: 3.5rem 1.5rem 40vh;
}
.title-input {
  border: none;
  background: none;
  padding: 0;
  font-family: var(--serif);
  font-size: 1.9rem;
  font-weight: 600;
  letter-spacing: -0.015em;
  margin-bottom: 1.3rem;
  color: var(--ink);
}
.title-input:focus { outline: none; box-shadow: none; }
.title-input::placeholder { color: var(--ink-faint); }

.body-input {
  border: none;
  background: none;
  padding: 0;
  width: 100%;
  min-height: 50vh;
  resize: none;
  font-family: var(--serif);
  font-size: 1.18rem;
  line-height: 1.75;
  color: var(--ink);
}
.body-input:focus { outline: none; box-shadow: none; }
.body-input::placeholder { color: var(--ink-faint); }

body.type-mono .title-input,
body.type-mono .body-input { font-family: var(--mono); }
body.type-mono .body-input { font-size: 1.02rem; line-height: 1.7; }

.statusbar {
  height: 2rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1rem;
  font-size: 0.75rem;
  color: var(--ink-faint);
  opacity: 0;
  transition: opacity 0.25s var(--ease);
}
.main:hover .statusbar { opacity: 1; }

/* focus mode: hide chrome, dim everything but the writing */
.app.focus .sidebar,
.app.focus .topbar,
.app.focus .statusbar { opacity: 0; pointer-events: none; }
.app.focus .sidebar { margin-left: -16.5rem; }

.empty {
  flex: 1;
  display: grid;
  place-items: center;
  color: var(--ink-faint);
  font-family: var(--serif);
  font-size: 1.1rem;
}

/* ---------- overlay panels (rewrite, versions) ---------- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(40, 35, 28, 0.28);
  display: grid;
  place-items: center;
  padding: 2rem;
  z-index: 40;
}
.panel {
  background: var(--paper-raised);
  border: 1px solid var(--line);
  border-radius: 14px;
  width: 100%;
  max-width: 60rem;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 60px rgba(50, 40, 28, 0.18);
}
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.3rem;
  border-bottom: 1px solid var(--line);
}
.panel-head h3 { margin: 0; font-family: var(--serif); font-size: 1.2rem; font-weight: 600; }
.panel-body { padding: 1.2rem 1.3rem; overflow-y: auto; }
.panel-foot {
  padding: 0.9rem 1.3rem;
  border-top: 1px solid var(--line);
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
}

.rewrite-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 1.2rem; }
.rewrite-col h4 {
  margin: 0 0 0.5rem;
  font-size: 0.75rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--ink-faint);
}
.rewrite-text {
  font-family: var(--serif);
  font-size: 1.02rem;
  line-height: 1.7;
  white-space: pre-wrap;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 1rem;
  min-height: 12rem;
  max-height: 46vh;
  overflow-y: auto;
}
.rewrite-text.placeholder { color: var(--ink-faint); font-style: italic; }
.instruction-row { display: flex; gap: 0.6rem; margin-bottom: 1.1rem; }
.instruction-row input { flex: 1; }

.version-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.7rem 0.4rem;
  border-bottom: 1px solid var(--line);
}
.version-item:last-child { border-bottom: none; }
.version-meta .vlabel { font-size: 0.9rem; color: var(--ink); }
.version-meta .vdate { font-size: 0.75rem; color: var(--ink-faint); margin-top: 0.1rem; }

.spinner {
  width: 1rem; height: 1rem;
  border: 2px solid var(--accent-soft);
  border-top-color: transparent;
  border-radius: 50%;
  display: inline-block;
  animation: spin 0.7s linear infinite;
  vertical-align: -2px;
}
@keyframes spin { to { transform: rotate(360deg); } }

.hidden { display: none !important; }

@media (max-width: 640px) {
  .sidebar { position: absolute; z-index: 30; height: 100%; background: var(--paper); }
  .rewrite-cols { grid-template-columns: 1fr; }
}

/* ---------- dark theme ---------- */
body.dark {
  --paper: #17171a;
  --paper-raised: #202024;
  --ink: #e7e3db;
  --ink-soft: #a9a396;
  --ink-faint: #6f6a60;
  --line: #2a2a30;
  --line-strong: #3a3a42;
  --accent: #82a8cb;
  --accent-soft: #5f7d99;
  --warm: #d98b73;
  --warm-soft: #a9634e;
  --focus-ring: rgba(130, 168, 203, 0.3);
}
body.dark .btn-primary { color: #14141a; }
body.dark .btn-primary:hover { color: #14141a; }

/* ---------- format bar ---------- */
.format-bar {
  display: flex;
  gap: 0.2rem;
  margin-bottom: 1.1rem;
  padding-bottom: 0.6rem;
  border-bottom: 1px solid var(--line);
}
.format-bar button {
  font-size: 0.78rem;
  padding: 0.3rem 0.6rem;
  border: 1px solid transparent;
}
.format-bar button:hover { background: var(--line); color: var(--ink); }
.app.focus .format-bar { opacity: 0; pointer-events: none; }

/* ---------- contenteditable body ---------- */
.body-input:empty:before {
  content: attr(data-placeholder);
  color: var(--ink-faint);
  pointer-events: none;
}
.body-input h1 {
  font-family: var(--serif);
  font-size: 1.75rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  margin: 1.6rem 0 0.6rem;
}
.body-input h2 {
  font-family: var(--serif);
  font-size: 1.4rem;
  font-weight: 600;
  margin: 1.7rem 0 0.5rem;
}
.body-input h3 {
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--ink-soft);
  letter-spacing: 0.02em;
  margin: 1.3rem 0 0.4rem;
}
.body-input p { margin: 0 0 1.05rem; }
body.type-mono .body-input h1,
body.type-mono .body-input h2 { font-family: var(--mono); }

/* rendered reading blocks (version preview) */
.rewrite-text.reading h1 { font-family: var(--serif); font-size: 1.5rem; margin: 1rem 0 0.5rem; }
.rewrite-text.reading h2 { font-family: var(--serif); font-size: 1.25rem; margin: 1.2rem 0 0.4rem; }
.rewrite-text.reading h3 { font-size: 1rem; color: var(--ink-soft); margin: 1rem 0 0.3rem; }
.rewrite-text.reading p { margin: 0 0 0.9rem; }

/* ---------- proofread / cliche notes ---------- */
.panel-note {
  color: var(--ink-soft);
  font-size: 0.86rem;
  line-height: 1.5;
  margin: 0 0 1.1rem;
}
.notes-block {
  margin-top: 1.2rem;
  border-top: 1px solid var(--line);
  padding-top: 1rem;
}
.notes-block h4 {
  margin: 0 0 0.5rem;
  font-size: 0.75rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--ink-faint);
}
.notes-text {
  font-family: var(--serif);
  font-size: 1rem;
  line-height: 1.65;
  white-space: pre-wrap;
}
.notes-block .placeholder { color: var(--ink-faint); font-style: italic; }

/* ---------- inline assistant bar (rewrite / proofread) ---------- */
.assistant-bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.6rem 1.5rem;
  border-top: 1px solid var(--line);
  background: var(--paper-raised);
}
.assistant-bar.hidden { display: none; }
.assistant-bar .ai-instr {
  flex: 1;
  padding: 0.45rem 0.7rem;
  font-size: 0.9rem;
  background: var(--paper);
}
.ai-status { color: var(--ink-soft); font-size: 0.88rem; }
.ai-status.err { color: var(--warm); }
.app.focus .assistant-bar { display: none; }

/* ---------- side rail (cliche notes, what changed) ---------- */
.notes-rail {
  width: 0;
  flex-shrink: 0;
  border-left: 1px solid var(--line);
  background: var(--paper-raised);
  overflow: hidden;
  transition: width 0.25s var(--ease);
}
.app.rail-open .notes-rail { width: 21rem; }
.rail-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.9rem 1rem 0.7rem;
  border-bottom: 1px solid var(--line);
}
.rail-head h3 { margin: 0; font-family: var(--serif); font-size: 1.05rem; font-weight: 600; }
.rail-body {
  padding: 1rem;
  overflow-y: auto;
  height: calc(100% - 3.1rem);
}
.rail-body .notes-text { font-size: 0.95rem; }
.rail-body .placeholder { color: var(--ink-faint); font-style: italic; }
.app.focus .notes-rail { width: 0; }

@media (max-width: 640px) {
  .app.rail-open .notes-rail {
    position: absolute; right: 0; top: 0; height: 100%; z-index: 25; width: 88%;
  }
}
