/* ==========================================================
   MAGPY FOLIO — Partnership Authoring
   Editorial cartography theme — shared family palette with Atlas
   ========================================================== */

:root {
  --atlas-bg:           #1c1815;
  --atlas-surface:      #211d1a;
  --atlas-elevated:     #28231f;
  --atlas-elevated-2:   #322d28;
  --atlas-border:       #322d27;
  --atlas-border-strong:#56504a;

  --atlas-text:         #f0eadd;
  --atlas-text-2:       #d4cebf;
  --atlas-muted:        #a09a8c;
  --atlas-dim:          #6e685e;

  --atlas-indigo:       #ece6d8;
  --atlas-indigo-deep:  #d4cdbd;
  --atlas-indigo-soft:  #b5b0a3;
  --atlas-indigo-glow:  rgba(236, 230, 216, 0.08);

  --atlas-emerald:      #8aab8e;
  --atlas-amber:        #c4a575;

  --atlas-paper:        #ffffff;

  /* R3PLICA brand orange (from official logo SVG) */
  --r3-orange:          #FF5F00;

  /* UI fonts */
  --font-display: "Geist", -apple-system, "Helvetica Neue", sans-serif;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --font-mono: "JetBrains Mono", "SF Mono", Menlo, monospace;

  /* Document fonts — Inter for body, Geist for headings (R3PLICA brand identity) */
  --font-doc-body: "Inter", -apple-system, "Helvetica Neue", sans-serif;
  --font-doc-display: "Geist", "Inter", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font-sans);
  background: var(--atlas-bg);
  color: var(--atlas-text);
  line-height: 1.45;
  font-size: 14px;
  overflow-x: clip;
}

/* Custom scrollbars */
* { scrollbar-color: var(--atlas-border-strong) var(--atlas-surface); scrollbar-width: thin; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: var(--atlas-surface); }
::-webkit-scrollbar-thumb { background: var(--atlas-border-strong); border-radius: 5px; border: 2px solid var(--atlas-surface); }
::-webkit-scrollbar-thumb:hover { background: var(--atlas-indigo); }

/* ==========================================================
   App layout
   ========================================================== */
.app {
  display: grid;
  grid-template-columns: 320px 1fr;
  min-height: 100vh;
}

.sidebar {
  background: var(--atlas-surface);
  border-right: 1px solid var(--atlas-border-strong);
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 20px 18px 80px;
  min-width: 0;
}

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

/* ==========================================================
   Brand block — 1:1 with Atlas (cream blueprint divider + register tick)
   ========================================================== */
.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 20px;
  padding-bottom: 14px;
  position: relative;
  border-bottom: 2px solid var(--atlas-text);
  min-width: 0;
}
.brand::after {
  content: "";
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 72px;
  height: 2px;
  background: var(--atlas-text-2);
}
.brand-mark {
  width: 64px;
  height: 64px;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--atlas-indigo);
  font-size: 56px;
  line-height: 1;
}
.brand-mark img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}
.brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1;
  min-width: 0;
  flex: 1;
}
.family-label {
  font-family: var(--font-display);
  font-size: 10px;
  font-weight: 500;
  color: var(--atlas-muted);
  letter-spacing: 0.24em;
  text-transform: lowercase;
  margin-bottom: 2px;
  margin-left: 1px;
}
.wordmark {
  font-family: var(--font-display);
  font-size: 46px;
  font-weight: 600;
  color: var(--atlas-text);
  letter-spacing: -0.015em;
  line-height: 0.9;
  margin-top: -1px;
}
/* Tagline — IDENTICAL spec to Atlas (JetBrains Mono, 8.5px, uppercase, dim) */
.tagline {
  font-family: var(--font-mono);
  font-size: 8.5px;
  color: var(--atlas-dim);
  letter-spacing: 0.185em;
  text-transform: uppercase;
  margin-top: 7px;
  font-weight: 500;
  white-space: nowrap;
}

/* ==========================================================
   Sidebar sections
   ========================================================== */
.section {
  margin-bottom: 18px;
  min-width: 0;
}
.section-title {
  display: flex;
  align-items: center;
  gap: 7px;
  font-family: var(--font-display);
  font-size: 10px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--atlas-muted);
  margin: 0 0 10px;
  font-weight: 600;
}
.section-title i { font-size: 13px; color: var(--atlas-muted); }

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 10px;
  min-width: 0;
}
.field label {
  font-size: 11px;
  color: var(--atlas-muted);
  font-weight: 500;
}
.field input,
.field select,
.field textarea {
  width: 100%;
  min-width: 0;
  background: var(--atlas-elevated);
  border: 1px solid var(--atlas-border-strong);
  color: var(--atlas-text);
  padding: 7px 9px;
  border-radius: 4px;
  font-family: var(--font-sans);
  font-size: 13px;
}
.field input:focus,
.field select:focus,
.field textarea:focus {
  outline: none;
  border-color: var(--atlas-indigo-deep);
  background: var(--atlas-elevated-2);
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  min-width: 0;
}
.field-row > .field { min-width: 0; }

.doc-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  min-width: 0;
}
.doc-type-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  background: var(--atlas-elevated);
  border: 1px solid var(--atlas-border-strong);
  border-radius: 999px;
  font-family: var(--font-display);
  font-size: 11px;
  letter-spacing: 0.04em;
  color: var(--atlas-text-2);
  white-space: nowrap;
}
.doc-type-pill i { color: var(--atlas-muted); font-size: 12px; }

.lang-toggle {
  display: inline-flex;
  background: var(--atlas-elevated);
  border: 1px solid var(--atlas-border-strong);
  border-radius: 999px;
  padding: 2px;
}
.lang-toggle button {
  background: transparent;
  border: none;
  color: var(--atlas-muted);
  font-family: var(--font-display);
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-weight: 600;
  padding: 5px 12px;
  cursor: pointer;
  border-radius: 999px;
  transition: all 0.15s;
}
.lang-toggle button.active {
  background: var(--atlas-indigo);
  color: var(--atlas-bg);
}

.clause-list { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.clause-toggle {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  padding: 9px 10px;
  background: var(--atlas-elevated);
  border: 1px solid var(--atlas-border);
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.12s;
  min-width: 0;
}
.clause-toggle:hover { border-color: var(--atlas-border-strong); background: var(--atlas-elevated-2); }
.clause-toggle.on { border-color: var(--atlas-indigo-soft); background: var(--atlas-indigo-glow); }
.clause-toggle input { margin-top: 2px; accent-color: var(--atlas-indigo-deep); flex: 0 0 auto; }
.clause-toggle > span { min-width: 0; }
.clause-toggle .clause-name { font-size: 12px; font-weight: 600; color: var(--atlas-text); display: block; }
.clause-toggle .clause-desc { font-size: 11px; color: var(--atlas-muted); margin-top: 2px; line-height: 1.35; display: block; }

/* ==========================================================
   Toolbar
   ========================================================== */
.toolbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 22px;
  background: var(--atlas-surface);
  border-bottom: 1px solid var(--atlas-border-strong);
}
.toolbar .spacer { flex: 1; }
.toolbar .hint { font-size: 11px; color: var(--atlas-muted); }
.toolbar .page-counter {
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--atlas-muted);
  letter-spacing: 0.05em;
  padding: 5px 9px;
  background: var(--atlas-elevated);
  border: 1px solid var(--atlas-border);
  border-radius: 4px;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--atlas-elevated);
  border: 1px solid var(--atlas-border-strong);
  color: var(--atlas-text);
  padding: 7px 12px;
  border-radius: 4px;
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.12s;
}
.btn:hover { background: var(--atlas-elevated-2); border-color: var(--atlas-indigo-soft); }
.btn.primary {
  background: var(--atlas-indigo);
  color: var(--atlas-bg);
  border-color: var(--atlas-indigo-deep);
  font-weight: 600;
}
.btn.primary:hover { background: var(--atlas-indigo-deep); }
.btn i { font-size: 14px; }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-square {
  padding: 6px 9px;
  min-width: 30px;
  justify-content: center;
  font-family: var(--font-display);
  font-weight: 600;
}
.tb-group { display: inline-flex; align-items: center; gap: 4px; }
.tb-divider {
  width: 1px;
  height: 22px;
  background: var(--atlas-border-strong);
  margin: 0 4px;
}
.font-readout {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--atlas-text-2);
  min-width: 40px;
  text-align: center;
  padding: 0 4px;
}

/* ==========================================================
   Paper stack — multiple A4 pages, one per element
   ========================================================== */
.preview-wrap {
  padding: 32px;
  display: flex;
  justify-content: center;
  background: var(--atlas-bg);
  background-image:
    radial-gradient(circle at 1px 1px, rgba(255,255,255,0.025) 1px, transparent 0);
  background-size: 18px 18px;
}

.paper-stack {
  display: flex;
  flex-direction: column;
  gap: 18px;
  align-items: center;
}

.paper-page {
  width: 210mm;
  height: 297mm;
  background: var(--atlas-paper);
  color: #1c1815;
  border-radius: 2px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.4);
  font-family: var(--font-doc-body);
  font-size: var(--doc-body-size, 10.5pt);
  line-height: 1.55;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

/* Header on every page (top-left logo) */
.paper-header {
  flex: 0 0 auto;
  padding: 12mm 18mm 4mm;
  display: flex;
  align-items: center;
  justify-content: flex-start;
}
.paper-header .r3-logo { height: 22px; width: auto; display: block; }
.paper-header .r3-logo-fallback {
  font-family: var(--font-doc-display);
  font-size: 18pt;
  font-weight: 800;
  letter-spacing: 1.5px;
  color: var(--r3-orange);
}

/* Body — content area, takes remaining space */
.paper-body {
  flex: 1 1 auto;
  padding: 4mm 18mm 6mm;
  overflow: hidden;
}

/* Footer on every page */
.paper-footer {
  flex: 0 0 auto;
  padding: 6mm 18mm 12mm;
  font-size: 7pt;
  color: #555049;
  line-height: 1.5;
}
.paper-footer .footer-block { margin-bottom: 3pt; }
.paper-footer .footer-block:last-child { margin-bottom: 0; }
.paper-footer .brand-line { font-weight: 700; color: #1c1815; font-family: var(--font-doc-display); }
.paper-footer .tagline-line { font-weight: 700; color: #1c1815; }
.paper-footer .copyright-line { color: #777067; font-size: 6.5pt; }

/* ==========================================================
   Document typography — applies to .paper-body content
   ========================================================== */
.paper-body h1 {
  font-family: var(--font-doc-display);
  font-size: 16pt;
  font-weight: 700;
  text-align: center;
  margin: 0 0 4px;
  letter-spacing: 0.01em;
}
.paper-body h2 {
  font-family: var(--font-doc-display);
  font-size: 12pt;
  font-weight: 600;
  text-align: center;
  margin: 0 0 18px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #555049;
}
.paper-body h3 {
  font-family: var(--font-doc-display);
  font-size: 11.5pt;
  font-weight: 600;
  margin: 12px 0 6px;
  letter-spacing: 0.01em;
}
.paper-body h4 {
  font-family: var(--font-doc-display);
  font-size: 11pt;
  font-weight: 600;
  margin: 12px 0 4px;
  font-style: italic;
  text-align: center;
}
.paper-body p { margin: 0 0 8px; text-align: justify; hyphens: auto; }
.paper-body ul, .paper-body ol { margin: 0 0 8px; padding-left: 22px; }
.paper-body li { margin-bottom: 3px; }
.paper-body hr {
  border: none;
  border-top: 1px solid #d8d2c5;
  margin: 14px 0;
}
.paper-body strong { font-weight: 700; }
.paper-body em { font-style: italic; }
.paper-body table.signatures { width: 100%; margin-top: 14px; }
.paper-body table.signatures td {
  width: 50%;
  vertical-align: top;
  font-size: 10pt;
  line-height: 1.5;
  padding: 0 12px;
}
.paper-body .article-block { margin-bottom: 2px; }

/* Hidden measure container — same width/typography as paper-body */
.measure-host {
  position: absolute !important;
  left: -10000px !important;
  top: 0 !important;
  width: 210mm;
  visibility: hidden;
  pointer-events: none;
  font-family: var(--font-doc-body);
  font-size: var(--doc-body-size, 10.5pt);
  line-height: 1.55;
  color: #1c1815;
  /* Same horizontal padding as paper-body so text wraps identically */
  padding: 0 18mm;
}

/* Editable mode — temporarily expand pages so user can type freely */
.app.editing .paper-page { height: auto; min-height: 297mm; overflow: visible; }
.app.editing .paper-body { overflow: visible; }
.paper-body[contenteditable="true"]:focus { outline: 2px solid #ffe0a8; outline-offset: 4px; }

/* Placeholder styling */
.placeholder-empty {
  background: #fff3c4;
  border-bottom: 1px dashed #c4a575;
  padding: 0 2px;
}

/* Page break indicator overlay (visible only on screen, not in print) */
.paper-page::after {
  content: attr(data-page-label);
  position: absolute;
  bottom: -14px;
  right: 4px;
  font-family: var(--font-mono);
  font-size: 9px;
  letter-spacing: 0.1em;
  color: var(--atlas-dim);
  text-transform: uppercase;
}

/* ==========================================================
   Mobile / tablet helpers — hidden on desktop, revealed in @media below
   ========================================================== */
.mobile-sidebar-btn {
  display: none;
  width: 40px;
  height: 40px;
  padding: 0;
  flex-shrink: 0;
  font-size: 18px;
  justify-content: center;
}
.sidebar-close {
  display: none;
  position: absolute;
  top: 14px;
  right: 14px;
  width: 32px;
  height: 32px;
  align-items: center;
  justify-content: center;
  background: var(--atlas-elevated);
  border: 1px solid var(--atlas-border-strong);
  color: var(--atlas-text);
  border-radius: 6px;
  cursor: pointer;
  font-size: 18px;
  z-index: 2;
  transition: background 0.12s, border-color 0.12s;
}
.sidebar-close:hover { background: var(--atlas-elevated-2); border-color: var(--atlas-text-2); }
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 40;
  backdrop-filter: blur(2px);
}
.app.sidebar-open .sidebar-backdrop { display: block; }

body.no-scroll { overflow: hidden; }

/* ==========================================================
   Responsive — tablet (≤1024px), mobile (≤768px), phone (≤480px)
   ========================================================== */

/* Tablet — paper preview no longer fits at 1:1, scale it down so it stays in view */
@media (max-width: 1024px) {
  .app, .app.detail-open { grid-template-columns: 280px 1fr; }
  .preview-wrap { padding: 22px; }
  .paper-page { transform-origin: top center; }
}

@media (max-width: 768px) {
  /* Sidebar collapses into full-width drawer over the main content */
  .app, .app.sidebar-open { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: min(360px, 100vw);
    max-width: 100vw;
    height: 100vh;
    z-index: 50;
    transform: translateX(-100%);
    transition: transform 0.22s ease-out;
    border-right: none;
    box-shadow: 8px 0 32px rgba(0, 0, 0, 0.45);
  }
  .app.sidebar-open .sidebar { transform: translateX(0); }
  .sidebar-close { display: inline-flex; }
  .sidebar .brand { padding-right: 46px; }
  .mobile-sidebar-btn { display: inline-flex; }

  /* Toolbar: keep it sticky but allow the controls to wrap onto a second row */
  .toolbar {
    flex-wrap: wrap;
    padding: 8px 12px;
    gap: 6px;
    row-gap: 8px;
  }
  .toolbar .spacer { flex: 1 1 100%; height: 0; }
  .toolbar .hint { font-size: 10px; flex-basis: 100%; order: 99; }
  .toolbar .page-counter { font-size: 9.5px; padding: 4px 7px; }
  .toolbar .btn { font-size: 11.5px; padding: 6px 9px; }
  .toolbar .btn i { font-size: 13px; }
  .toolbar .btn-square { padding: 5px 7px; min-width: 28px; }
  .tb-divider { margin: 0 2px; }

  /* Hide the explicit textual labels on B/I/Page-break button text and just keep icons/letters */
  #btn-edit-toggle #edit-label,
  #btn-page-break { display: inline-flex; }

  /* Paper preview fit-to-viewport. The scale factor is computed in JS
     (applyPaperScale) and exposed as --paper-scale on the document root.
     We apply transform: scale() AND counter the unscaled layout box with
     negative margins so the page genuinely takes only the scaled width
     in flow — without this trick the original 794×1123px box overflows
     the viewport horizontally and gets cropped on the sides. */
  .preview-wrap {
    padding: 14px;
    overflow-x: hidden;
  }
  .paper-stack { gap: 12px; }
  .paper-page {
    transform-origin: top left;
    transform: scale(var(--paper-scale, 1));
    margin-right: calc(-794px * (1 - var(--paper-scale, 1)));
    margin-bottom: calc(-1123px * (1 - var(--paper-scale, 1)) + 12px);
  }
  .app.editing .paper-page {
    /* In edit mode page height is variable; the y-collapse trick is approximate.
       Slight overlap is acceptable since editing on a phone is rare. */
    margin-bottom: 12px;
  }

  /* Field grid in sidebar: keep two columns where it makes sense, stack tight pairs */
  .field-row { gap: 8px; }
  .section { margin-bottom: 18px; }
  input[type=text], input[type=number], input[type=date] { font-size: 16px; /* prevent iOS zoom on focus */ }
}

@media (max-width: 480px) {
  .toolbar { padding: 7px 10px; }
  .toolbar .btn { font-size: 11px; padding: 5px 8px; }
  /* Drop verbose button labels on the smallest screens, keep icons */
  #btn-edit-toggle #edit-label,
  #btn-reset .btn-text,
  #btn-copy-md .btn-text,
  #btn-print .btn-text,
  #btn-page-break .btn-text { display: none; }
  .toolbar .page-counter { display: none; }
  .preview-wrap { padding: 10px; }
}

/* ==========================================================
   Print
   ========================================================== */
@page {
  size: A4;
  margin: 0;
}

@media print {
  body { background: #fff !important; overflow: visible !important; }
  .sidebar, .toolbar, .measure-host, .sidebar-backdrop, .mobile-sidebar-btn, .sidebar-close { display: none !important; }
  .app { display: block !important; }
  .preview-wrap {
    padding: 0 !important;
    background: #fff !important;
    background-image: none !important;
  }
  .paper-stack { gap: 0 !important; }
  .paper-page {
    transform: none !important;
    margin-bottom: 0 !important;
    box-shadow: none !important;
    border-radius: 0 !important;
    margin: 0 !important;
    width: 210mm !important;
    height: 297mm !important;
    overflow: hidden !important;
    break-after: page;
    page-break-after: always;
    break-inside: avoid;
    page-break-inside: avoid;
  }
  .paper-page:last-child {
    break-after: auto;
    page-break-after: auto;
  }
  .paper-page::after { display: none !important; }
  hr.page-break { display: none !important; }
}

/* Manual page break marker — visible only in edit mode so the user can see
   where they inserted a forced page split. The paginator removes it from
   the paginated output, and print CSS hides any stray instance. */
hr.page-break {
  display: none;
  height: 0;
  margin: 0;
  border: 0;
}
.app.editing hr.page-break {
  display: block;
  height: 0;
  margin: 12px 0;
  border: 0;
  border-top: 2px dashed var(--atlas-amber, #d4a35a);
  position: relative;
}
.app.editing hr.page-break::after {
  content: "↵ page break";
  position: absolute;
  top: -10px;
  right: 0;
  background: var(--atlas-bg, #1f1c17);
  padding: 0 6px;
  font: 500 9px/1 "JetBrains Mono", monospace;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--atlas-amber, #d4a35a);
}
