:root {
  --cover: #5a2e1c;
  --cover-dark: #3a1d11;
  --gold: #d9b26a;
  --paper: #fbf7ee;
  --ink: #2b2620;
  --muted: #7a6f5f;
  --rule: #cfd8e3;
  --margin: #e8a6a6;
  --navy: #1f3a5f;
  --ui-bg: #efece6;
  --card: #ffffff;
  --border: #d9d1bf;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--ui-bg);
  color: #2b2620;
  font-family: "Segoe UI", Arial, sans-serif;
  font-size: 15px;
}

h2 { font-size: 18px; margin: 0 0 12px; color: var(--cover-dark); }

/* ---------- Header & tabs ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  padding: 12px 20px;
  background: linear-gradient(135deg, var(--cover), var(--cover-dark));
  color: var(--gold);
}
.brand { font-family: Georgia, serif; font-size: 20px; letter-spacing: 0.04em; }
.tabs { display: flex; gap: 6px; flex-wrap: wrap; }
.tabs button {
  background: transparent;
  color: #f0e2bf;
  border: 1px solid rgba(217,178,106,0.5);
  border-radius: 999px;
  padding: 7px 16px;
  font: inherit;
  cursor: pointer;
}
.tabs button[aria-pressed="true"] { background: var(--gold); color: var(--cover-dark); border-color: var(--gold); }
.save-status { font-size: 13px; min-width: 120px; text-align: right; opacity: 0.9; }

/* Page menu: second row of the header, jumps to a page in the book */
.page-menu {
  flex-basis: 100%;
  display: flex;
  justify-content: center;
  gap: 4px;
  overflow-x: auto;
  padding-top: 8px;
  border-top: 1px solid rgba(217,178,106,0.25);
}
.page-menu button {
  flex: 0 0 auto;
  background: transparent;
  color: #f0e2bf;
  border: none;
  border-bottom: 2px solid transparent;
  padding: 6px 12px;
  font: inherit;
  cursor: pointer;
}
.page-menu button:hover { color: var(--gold); }
.page-menu button.active { color: var(--gold); border-bottom-color: var(--gold); }
.page-menu button:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }

/* ---------- Views ---------- */
.view { display: none; }
.view.active { display: block; }
#book-view.active {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
  padding: 36px 16px 48px;
  min-height: calc(100vh - 60px);
  background: radial-gradient(ellipse at center, #4a4540 0%, #262220 70%);
}

/* ---------- Editor ---------- */
.editor-layout {
  display: grid;
  grid-template-columns: 1fr 260px;
  gap: 20px;
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 16px 48px;
  align-items: start;
}
@media (max-width: 820px) {
  .editor-layout { grid-template-columns: 1fr; }
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 18px 20px;
  margin-bottom: 16px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}
.editor-side { position: sticky; top: 76px; }
@media (max-width: 820px) { .editor-side { position: static; } }

.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.card-head h2 { margin: 0; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 600px) { .grid-2 { grid-template-columns: 1fr; } }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field > span { font-size: 13px; color: #5a5144; font-weight: 600; }
.field input,
.field textarea,
#skills-input {
  font: inherit;
  padding: 9px 10px;
  border: 1px solid #cfc6b4;
  border-radius: 6px;
  background: #fffdf8;
  color: var(--ink);
  width: 100%;
}
.field textarea, #skills-input { resize: vertical; }
.field input:focus, .field textarea:focus, #skills-input:focus {
  outline: 2px solid var(--gold);
  outline-offset: 0;
  border-color: var(--cover);
}

.item {
  border: 1px solid #e5dfd1;
  border-radius: 8px;
  padding: 12px 14px 2px;
  margin-bottom: 12px;
  background: #fcfbf7;
}
.item-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; font-weight: 600; color: var(--cover-dark); }

.hint { font-size: 13px; color: var(--muted); margin: 6px 0 0; }
.muted { color: var(--muted); font-style: italic; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-block;
  padding: 9px 16px;
  border-radius: 6px;
  border: 1px solid var(--cover);
  background: #fff;
  color: var(--cover-dark);
  font: inherit;
  cursor: pointer;
  text-align: center;
}
.btn:hover { background: #f6f1e6; }
.btn.primary { background: var(--cover); color: #f5e6c4; }
.btn.primary:hover { background: var(--cover-dark); }
.btn.ghost { background: transparent; }
.btn.danger { color: #9b2c2c; border-color: #d9a3a3; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.wide { display: block; width: 100%; margin-bottom: 8px; }
.btn:focus-visible, .tabs button:focus-visible, .controls button:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}
.file-btn { position: relative; overflow: hidden; }
.file-btn input[type="file"] {
  position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%;
}

/* ---------- Send dialog ---------- */
dialog {
  border: none;
  border-radius: 12px;
  padding: 24px;
  width: min(520px, calc(100vw - 32px));
  box-shadow: 0 10px 40px rgba(0,0,0,0.35);
  font: inherit;
}
dialog::backdrop { background: rgba(20,15,12,0.55); }
dialog h2 { margin-bottom: 10px; }
.steps { padding-left: 20px; margin: 0 0 16px; color: #4a4238; line-height: 1.7; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }

/* ---------- Book ---------- */
.stage {
  width: min(960px, 94vw);
  perspective: 2600px;
  container-type: inline-size;
}

.book {
  position: relative;
  width: 100%;
  aspect-ratio: 960 / 640;
  transform-style: preserve-3d;
  transition: transform 0.8s ease;
}
.book.closed-front { transform: translateX(-25%); }
.book.closed-back  { transform: translateX(25%); }

.sheet {
  position: absolute;
  top: 0;
  left: 50%;
  width: 50%;
  height: 100%;
  transform-origin: left center;
  transform-style: preserve-3d;
  transition: transform 1s cubic-bezier(.45,.05,.55,.95);
  cursor: pointer;
}
.sheet.flipped { transform: rotateY(-180deg); }

.face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.face.back { transform: rotateY(180deg); }

/* Paper pages; the ruled lines are drawn by .page-inner (see above).
   Line spacing matches the text line height. */
.paper {
  --line: 2.9cqi;
  display: block;
  color: var(--ink);
  background-color: var(--paper);
  box-shadow: inset 0 0 40px rgba(120,90,40,0.12);
}
.face.front.paper {
  background-image: linear-gradient(to right, rgba(0,0,0,0.12), rgba(0,0,0,0) 7%);
}
.face.back.paper {
  background-image: linear-gradient(to left, rgba(0,0,0,0.12), rgba(0,0,0,0) 7%);
}

/* The text area of a page scrolls when its content is longer than the page.
   The ruled lines and margin are part of this box's background, so they
   scroll together with the text (background-attachment: local). */
.page-inner {
  position: relative;
  height: calc(100% - 7cqi);
  overflow-y: auto;
  overflow-x: hidden;
  padding: 9cqi 9cqi 6cqi 13cqi;
  font-size: clamp(11px, 1.9cqi, 18px);
  line-height: var(--line);
  background-image:
    linear-gradient(to right, transparent calc(10cqi - 1px), var(--margin) calc(10cqi - 1px), var(--margin) 10cqi, transparent 10cqi),
    repeating-linear-gradient(to bottom, transparent 0, transparent calc(var(--line) - 1px), var(--rule) calc(var(--line) - 1px), var(--rule) var(--line));
  background-attachment: local;
  scrollbar-width: thin;
  scrollbar-color: #bfae8a transparent;
}
.page-inner h2 {
  font-family: Georgia, serif;
  font-size: 4.6cqi;
  margin: 0 0 3cqi;
  font-weight: normal;
  color: var(--ink);
  border-bottom: 2px solid var(--ink);
  display: inline-block;
  padding-bottom: 0.6cqi;
}
.page-inner h3 {
  font-family: Georgia, serif;
  font-size: 3.6cqi;
  margin: 0 0 3cqi;
  font-weight: normal;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
}
.page-inner p { margin: 0 0 var(--line); line-height: var(--line); }
.page-inner .entry-title { font-weight: bold; font-size: 1.12em; margin-bottom: 0.2em; }
.entry-meta { font-style: italic; color: var(--muted); }
.page-inner .muted { font-style: italic; }
.page-inner ul, .page-inner ol { margin: 0 0 var(--line); padding-left: 1.2em; }

.toc { list-style: none; padding: 0; margin: 0; }
.toc li { display: flex; align-items: baseline; gap: 6px; margin-bottom: 2.4cqi; }
.toc .dots { flex: 1; border-bottom: 1px dotted var(--muted); transform: translateY(-0.3em); }

.skills { margin: 0; padding-left: 1.2em; line-height: 2.1; }

.folio {
  position: absolute;
  bottom: 4cqi;
  left: 0;
  right: 0;
  text-align: center;
  font-size: clamp(9px, 1.4cqi, 13px);
  color: var(--muted);
}

/* Hard covers */
.cover {
  background:
    linear-gradient(to right, rgba(0,0,0,0.35), rgba(255,255,255,0.05) 4%, rgba(0,0,0,0) 8%),
    linear-gradient(135deg, var(--cover), var(--cover-dark));
  color: var(--gold);
  box-shadow: inset 0 0 0 1.2cqi var(--cover-dark), inset 0 0 0 1.5cqi var(--gold);
  border-radius: 0 6px 6px 0;
}
.face.back.cover {
  border-radius: 6px 0 0 6px;
  background:
    linear-gradient(to left, rgba(0,0,0,0.35), rgba(255,255,255,0.05) 4%, rgba(0,0,0,0) 8%),
    linear-gradient(135deg, var(--cover), var(--cover-dark));
}
.cover-inner { text-align: center; padding: 8cqi; }
.cover-label {
  letter-spacing: 0.35em;
  text-transform: uppercase;
  font-size: clamp(10px, 1.6cqi, 15px);
  margin-bottom: 4cqi;
  opacity: 0.85;
}
.cover h1 {
  font-family: Georgia, serif;
  font-size: 7cqi;
  font-weight: normal;
  margin: 0 0 3cqi;
  line-height: 1.15;
  overflow-wrap: anywhere;
}
.cover-by { font-style: italic; font-size: clamp(12px, 2.2cqi, 22px); margin: 0; }

.book::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 14px;
  margin-left: -7px;
  background: linear-gradient(to right, rgba(0,0,0,0), rgba(0,0,0,0.25) 50%, rgba(0,0,0,0));
  pointer-events: none;
  z-index: 1000;
}

.controls { display: flex; align-items: center; gap: 16px; color: #e9e1cf; }
.controls button {
  padding: 10px 18px;
  border: 1px solid var(--gold);
  border-radius: 4px;
  background: var(--cover);
  color: var(--gold);
  cursor: pointer;
  font: inherit;
}
.controls button:hover:not(:disabled) { background: var(--cover-dark); }
.controls button:disabled { opacity: 0.4; cursor: default; }
.status { min-width: 140px; text-align: center; font-size: 14px; letter-spacing: 0.05em; }

/* ---------- Rich text editor (Word-style toolbar) ---------- */
.rte {
  border: 1px solid #cfc6b4;
  border-radius: 6px;
  background: #fffdf8;
  overflow: hidden;
  margin-bottom: 12px;
}
.rte-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 3px;
  padding: 6px;
  background: #f4efe4;
  border-bottom: 1px solid #ddd3bf;
}
.rte-toolbar button {
  min-width: 30px;
  padding: 4px 7px;
  border: 1px solid transparent;
  border-radius: 4px;
  background: transparent;
  color: #3a1d11;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}
.rte-toolbar button:hover { background: #e9e0cc; }
.rte-toolbar button[aria-pressed="true"] { background: #e2cfa4; border-color: #c9ad6b; }
.rte-toolbar select {
  font: inherit;
  font-size: 13px;
  padding: 3px 4px;
  border: 1px solid #cfc6b4;
  border-radius: 4px;
  background: #fff;
  max-width: 140px;
}
.rte-toolbar .sep { width: 1px; height: 20px; background: #cfc6b4; margin: 0 4px; }
.color-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 30px;
  height: 28px;
  padding: 0 6px;
  border-radius: 4px;
  font-size: 13px;
  color: #3a1d11;
  cursor: pointer;
  overflow: hidden;
}
.color-btn:hover { background: #e9e0cc; }
.color-btn input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  border: 0;
  padding: 0;
  cursor: pointer;
}
.rte-body {
  min-height: 110px;
  max-height: 340px;
  overflow-y: auto;
  padding: 10px 12px;
  line-height: 1.6;
  outline: none;
}
.rte-body:focus { box-shadow: inset 0 0 0 2px var(--gold); }
.rte-body p { margin: 0 0 0.4em; }
.rte-body h2 { font-size: 20px; margin: 0.4em 0; }
.rte-body h3 { font-size: 16px; margin: 0.4em 0; }
.rte-body ul, .rte-body ol { padding-left: 1.4em; margin: 0.3em 0; }

/* Rich text as shown on the book pages and the CV */
.rich p, .rich ul, .rich ol { margin: 0 0 var(--line, 0.5em); line-height: var(--line, 1.6); }
.rich ul, .rich ol { padding-left: 1.2em; }
.rich h2 { font-size: 1.25em; margin: 0 0 var(--line, 0.5em); font-weight: normal; }
.rich h3 { font-size: 1.1em; margin: 0 0 var(--line, 0.5em); font-weight: bold; }
.cv-sheet .rich p, .cv-sheet .rich ul, .cv-sheet .rich ol { margin: 0 0 0.4em; line-height: 1.55; }
.cv-sheet .rich h2, .cv-sheet .rich h3 { font-size: 15px; margin: 0 0 0.3em; font-weight: bold; border: 0; padding: 0; letter-spacing: 0; text-transform: none; color: #222; }

/* ---------- Single page (small screens) ----------
   On narrow screens the two-page spread is replaced by one page at a time.
   Shown only at or below 700px; the spread is hidden there. */
.single-stage { display: none; width: 100%; max-width: 640px; container-type: inline-size; }
.single-page {
  position: relative;
  height: calc(100vh - 250px);
  height: calc(100dvh - 250px);
  min-height: 440px;
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 6px 24px rgba(0,0,0,0.45);
}
.single-page.animate { animation: pageIn 0.35s ease; }
@keyframes pageIn {
  from { opacity: 0; transform: translateX(16px); }
  to { opacity: 1; transform: none; }
}
.single-page .face.back { transform: none; }

@media (max-width: 700px) {
  .stage { display: none; }
  .single-stage { display: block; }
  #book-view.active { padding: 20px 12px 28px; gap: 16px; }
  .controls { gap: 8px; width: 100%; justify-content: space-between; }
  .controls button { padding: 10px 12px; font-size: 14px; }
  .status { min-width: 0; font-size: 13px; }
}

/* ---------- CV (printable) ---------- */
.view-toolbar {
  max-width: 820px;
  margin: 24px auto 16px;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 0 16px;
}
.cv-sheet {
  max-width: 820px;
  margin: 0 auto 48px;
  background: #fff;
  padding: 48px 56px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
  font-family: "Segoe UI", Arial, sans-serif;
  color: #222;
  font-size: 14px;
  line-height: 1.55;
}
.cv-head h1 { margin: 0; font-size: 30px; color: var(--navy); }
.cv-title { margin: 4px 0 0; color: var(--cover); font-size: 16px; font-weight: 600; }
.cv-contact { margin: 8px 0 0; color: #555; font-size: 13px; }
.cv-section { margin-top: 22px; }
.cv-section h2 {
  font-size: 12px;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  color: var(--navy);
  border-bottom: 1.5px solid var(--navy);
  padding-bottom: 4px;
  margin: 0 0 10px;
}
.cv-section p { margin: 4px 0 0; }
.cv-entry { margin-bottom: 12px; }
.cv-row { display: flex; justify-content: space-between; gap: 12px; }
.cv-sub { color: #555; font-style: italic; }
.cv-skills { display: flex; flex-wrap: wrap; gap: 6px; list-style: none; padding: 0; margin: 0; }
.cv-skills li { background: #eef2f7; color: var(--navy); padding: 3px 10px; border-radius: 20px; font-size: 13px; }
@media (max-width: 600px) { .cv-sheet { padding: 28px 22px; } }

/* ---------- Print: only the CV ---------- */
@media print {
  @page { margin: 14mm; }
  body { background: #fff; }
  .no-print, .app-header, dialog { display: none !important; }
  .view:not(#cv-view) { display: none !important; }
  #cv-view { display: block !important; }
  .cv-sheet { box-shadow: none; padding: 0; margin: 0; max-width: none; }
}

@media (prefers-reduced-motion: reduce) {
  .sheet, .book { transition: none; }
}
