@font-face {
  font-family: "Alta";
  src: url("assets/alta-regular.otf") format("opentype");
}

@font-face {
  font-family: "Courier New Local";
  src: url("assets/courier-new.ttf") format("truetype");
}

:root {
  --ink: #936971;
  --ink-dark: #7e5961;
  --paper: #f7f1e9;
  --line: #cdb6bb;
  --panel: #fffaf4;
  --text: #493a3d;
  --shadow: 0 18px 50px rgba(76, 52, 56, 0.16);
  --font-scale: 1.15;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: #e8dfd5;
  color: var(--text);
  font-family: "Courier New Local", "Courier New", monospace;
}

body.auth-locked {
  min-height: 100vh;
  overflow: hidden;
}

body.auth-locked .app-shell {
  display: none;
}

.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  padding: 24px;
  background-color: var(--paper);
  background-image: url("assets/background.png");
  background-size: 100% 100%;
  background-position: center;
}

body:not(.auth-locked) .auth-gate {
  display: none;
}

.auth-card {
  width: min(420px, 100%);
  padding: 34px;
  background: rgba(255, 250, 244, 0.94);
  border: 1px solid #d6c4c1;
  box-shadow: var(--shadow);
  text-align: center;
}

.auth-logo {
  width: 56px;
  height: auto;
  margin-bottom: 18px;
  mix-blend-mode: multiply;
}

.auth-card h1 {
  margin-bottom: 22px;
}

.auth-card label {
  display: block;
  margin-bottom: 8px;
  text-align: left;
}

.auth-error {
  min-height: 18px;
  margin: -6px 0 12px;
  color: var(--ink-dark);
}

button,
input,
textarea {
  font: inherit;
}

button {
  border: 1px solid var(--ink);
  background: var(--ink);
  color: white;
  min-height: 38px;
  padding: 8px 12px;
  cursor: pointer;
}

button:hover {
  background: var(--ink-dark);
}

input,
textarea {
  width: 100%;
  border: 1px solid #d6c4c1;
  background: #fffdf9;
  color: var(--text);
  padding: 9px 10px;
}

textarea {
  min-height: 148px;
  resize: vertical;
  line-height: 1.35;
}

.raw-input {
  min-height: 330px;
}

.layout-controls {
  padding: 12px;
  border: 1px solid #e0d2cc;
  background: rgba(255, 253, 249, 0.72);
}

.ai-chat-section {
  padding-top: 4px;
}

.ai-messages {
  display: grid;
  gap: 8px;
  max-height: 230px;
  overflow: auto;
  padding: 10px;
  border: 1px solid #d6c4c1;
  background: #fffdf9;
}

.ai-message {
  padding: 8px 9px;
  border-radius: 8px;
  line-height: 1.3;
  white-space: pre-wrap;
}

.ai-message.user {
  margin-left: 20px;
  background: #f1e7df;
}

.ai-message.assistant,
.ai-message.system {
  margin-right: 20px;
  background: #eadde0;
  color: var(--ink-dark);
}

.ai-message.system {
  font-size: 13px;
  background: #fff4ec;
}

.ai-chat-input {
  min-height: 76px;
}

label,
.eyebrow {
  font-family: "Alta", Georgia, serif;
  color: var(--ink);
  text-transform: uppercase;
  letter-spacing: 0;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(360px, 430px) 1fr;
  min-height: 100vh;
}

.workspace-panel {
  height: 100vh;
  overflow: auto;
  padding: 22px;
  background: var(--panel);
  border-right: 1px solid #d8cbc6;
}

.brand-strip {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 24px;
}

.brand-logo {
  width: 46px;
  height: auto;
}

.eyebrow {
  margin: 0 0 4px;
  font-size: 14px;
}

h1,
h2 {
  font-family: "Alta", Georgia, serif;
  color: var(--ink);
  font-weight: 400;
  letter-spacing: 0;
  margin: 0;
}

h1 {
  font-size: 31px;
  line-height: 1;
}

h2 {
  font-size: 24px;
}

.control-section {
  display: grid;
  gap: 8px;
  margin-bottom: 18px;
}

.range-row,
.button-row,
.toolbar,
.data-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

output {
  color: var(--ink);
  font-weight: 700;
}

.button-row {
  margin-top: 10px;
}

.button-row button {
  flex: 1;
}

.preview-panel {
  height: 100vh;
  overflow: auto;
  padding: 22px 28px 80px;
}

.toolbar {
  position: sticky;
  top: 0;
  z-index: 20;
  padding: 0 0 16px;
  background: linear-gradient(#e8dfd5 75%, rgba(232, 223, 213, 0));
}

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.tab {
  background: transparent;
  color: var(--ink);
}

.tab.is-active {
  background: var(--ink);
  color: white;
}

#pageStatus {
  margin: 0;
  color: var(--ink);
}

.view {
  display: none;
}

.view.is-active {
  display: block;
}

.page-stack {
  display: grid;
  grid-template-columns: minmax(280px, 900px);
  gap: 28px;
  justify-content: center;
}

#treeView .tree-fit-frame {
  --tree-scale: 1;
  position: relative;
  width: 100%;
  height: var(--tree-frame-height, auto);
  overflow: visible;
}

#treeView .page-stack {
  grid-template-columns: 900px;
  width: 900px;
  gap: 0;
  margin: 0 auto;
  transform: scale(var(--tree-scale));
  transform-origin: top center;
}

#treeView .sheet.a3 {
  width: 100%;
  max-height: none;
}

.sheet {
  position: relative;
  overflow: hidden;
  width: 100%;
  background-color: var(--paper);
  background-image: url("assets/background.png");
  background-size: 100% 100%;
  background-position: center;
  background-repeat: no-repeat;
  box-shadow: var(--shadow);
}

.tree-page {
  background-image: url("assets/tree-line-layer.png"), url("assets/background.png");
  background-size: 118% 100%, 100% 100%;
  background-position: -62px center, center;
  background-blend-mode: normal;
}

.sheet.a3 {
  aspect-ratio: 1123 / 1588;
}

.sheet.a4 {
  aspect-ratio: 595 / 842;
  max-width: 700px;
  justify-self: center;
}

.sheet-inner {
  position: absolute;
  inset: 0;
  color: var(--ink);
}

.sheet-logo {
  position: absolute;
  z-index: 5;
  top: 1.8%;
  left: 50%;
  width: 8%;
  transform: translateX(-50%);
  mix-blend-mode: multiply;
}

.sheet-kicker,
.sheet-title,
.entry-title,
.entry-copy,
.source-copy {
  position: absolute;
  left: 8%;
  width: 84%;
  text-align: center;
}

.sheet-kicker,
.sheet-title,
.entry-title {
  font-family: "Alta", Georgia, serif;
  font-weight: 400;
  text-transform: uppercase;
  letter-spacing: 0;
}

.sheet-kicker {
  z-index: 5;
  top: 6.2%;
  font-size: 25px;
}

.sheet-title {
  z-index: 5;
  top: 8.1%;
  font-size: 43px;
}

.node,
.partner-node {
  position: absolute;
  z-index: 3;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: var(--ink);
  color: white;
  text-align: center;
  overflow: hidden;
  font-family: "Alta", Georgia, serif;
  line-height: 1;
  padding: 4px 9px;
}

.partner-node {
  border-radius: 10px;
}

.node-line {
  position: absolute;
  z-index: 2;
  height: 18px;
  overflow: visible;
}

.node-line path {
  fill: none;
  stroke: var(--ink);
  stroke-width: 2.2;
  stroke-linecap: round;
  opacity: 0.72;
}

.node-name,
.partner-name {
  text-transform: uppercase;
  overflow-wrap: anywhere;
  word-break: normal;
  hyphens: none;
}

.node-first {
  font-size: calc(1em - 1.5px);
}

.node-last,
.partner-name {
  font-size: 1em;
}

.node-years,
.partner-years {
  margin-top: 2px;
  white-space: nowrap;
  font-variant-numeric: lining-nums tabular-nums;
  line-height: 1.05;
}

.node-years {
  font-size: calc(0.95em - 0.5px);
}

.partner-years {
  font-size: calc(0.9em - 0.5px);
}

.card-text {
  display: grid;
  align-content: center;
  row-gap: 1px;
  width: 100%;
  max-height: 100%;
  overflow: hidden;
}

.entry-title {
  font-size: clamp(17px, 2.2vw, 31px);
  line-height: 1.05;
}

.entry-copy,
.source-copy {
  font-family: "Courier New Local", "Courier New", monospace;
  font-size: clamp(11px, 1.25vw, 18px);
  line-height: 1.23;
  white-space: pre-wrap;
}

.source-page .entry-title {
  text-transform: none;
}

[contenteditable="true"] {
  cursor: text;
}

[contenteditable="true"]:focus {
  outline: 1px dashed rgba(147, 105, 113, 0.75);
  outline-offset: 2px;
}

.page-number {
  position: absolute;
  bottom: 3%;
  left: 0;
  width: 100%;
  text-align: center;
  color: var(--ink);
  font-size: 11px;
}

.people-table {
  display: grid;
  gap: 14px;
  margin-top: 16px;
}

.person-row {
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  gap: 8px;
  padding: 12px;
  background: #fffaf4;
  border: 1px solid #d6c4c1;
}

.field {
  display: grid;
  gap: 4px;
  grid-column: span 2;
}

.field span {
  color: var(--ink);
  font-size: 13px;
}

.field-wide {
  grid-column: span 4;
}

.field-half {
  grid-column: span 6;
}

.person-row .field:nth-child(1),
.person-row .field:nth-child(2) {
  grid-column: span 3;
}

.person-row input,
.person-row textarea {
  min-width: 0;
}

.person-row textarea {
  min-height: 96px;
}

@media (max-width: 980px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .workspace-panel,
  .preview-panel {
    height: auto;
  }

  .toolbar {
    top: 0;
  }

  .table-head,
  .person-row {
    grid-template-columns: 1fr 1fr;
  }

  .field,
  .field-wide,
  .field-half {
    grid-column: span 2;
  }
}

@page {
  size: A3 portrait;
  margin: 0;
}

@page a3Sheet {
  size: A3 portrait;
  margin: 0;
}

@page a4Sheet {
  size: A4 portrait;
  margin: 0;
}

@media print {
  * {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }

  html,
  body {
    width: 100%;
    margin: 0;
    background: white;
  }

  .workspace-panel,
  .toolbar,
  .auth-gate,
  #dataView {
    display: none;
  }

  .app-shell {
    display: block;
  }

  .preview-panel {
    padding: 0;
    height: auto;
    overflow: visible;
    background: white;
  }

  .view {
    display: block;
  }

  #treeView .tree-fit-frame {
    width: auto;
    height: auto !important;
    overflow: visible;
  }

  #treeView .page-stack {
    width: auto;
    transform: none !important;
  }

  .page-stack {
    display: block;
    width: auto;
  }

  .sheet {
    margin: 0 auto;
    box-shadow: none;
    page-break-after: always;
    break-after: page;
    background-size: 100% 100%;
  }

  .sheet:last-child {
    page-break-after: auto;
    break-after: auto;
  }

  .sheet.a3 {
    page: a3Sheet;
    width: 297mm !important;
    height: 420mm !important;
  }

  .sheet.a4 {
    page: a4Sheet;
    width: 210mm !important;
    height: 297mm !important;
    max-width: none;
  }
}
