/* Layout: a writing tool — quiet chrome above a split view; source on the left,
   a true-to-scale sheet of paper on a desk on the right. The paper is always white
   because it is the PDF; only the chrome follows light/dark. */

/* Colours, fonts and shapes come from Jive (https://ui.mapara.dev/v1/jive.css); these names map them
   onto the layout below. Jive tokens switch with light/dark on their own. */
:root {
  --bg: var(--surface);
  --panel: var(--surface-raised);
  --chip: var(--surface-sunken);
  --fg: var(--ink);
  --muted: var(--ink-muted);
  --accent: var(--brand);
  --accent-fg: var(--on-brand);
  --accent-ring: color-mix(in oklab, var(--brand) 22%, transparent);
  --desk: light-dark(#e9ecf1, #08090b);
  --paper-shadow: 0 1px 2px light-dark(rgba(16, 24, 40, .06), rgba(0, 0, 0, .4)), 0 12px 32px -8px light-dark(rgba(16, 24, 40, .16), rgba(0, 0, 0, .65));
  --seg-shadow: var(--shadow-sm), 0 0 0 1px var(--line);
  --font-ui: var(--font-body);
  --radius: var(--radius-md);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--fg);
  font: 400 14px/1.45 var(--font-ui);
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; flex: none; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 20px;
  padding-block: 10px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.brand { display: inline-flex; align-items: center; gap: 8px; min-width: 0; }
.brand-back { color: var(--muted); font: 500 14px/1 var(--font-ui); text-decoration: none; padding: 6px 8px; margin-left: -8px; border-radius: var(--radius-sm); }
.brand-back:hover { color: var(--fg); background: var(--chip); }
.brand-sep { color: var(--line-strong); }
.brand-mark {
  font: 600 15px/1 var(--font-mono);
  letter-spacing: -.02em;
}
.brand-mark span span { color: var(--accent); padding-inline: 2px; }

.controls { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; flex: 1; justify-content: center; }
.actions { display: flex; align-items: center; gap: 8px; }

/* segmented control built from radio buttons */
.seg {
  display: inline-flex;
  margin: 0;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: var(--radius-pill);
  background: var(--chip);
}
.seg legend {
  position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
}
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg label {
  display: inline-flex;
  align-items: center;
  height: 26px;
  padding: 0 12px;
  border-radius: var(--radius-pill);
  color: var(--muted);
  font-weight: 500;
  font-size: 13px;
  cursor: pointer;
  user-select: none;
  transition: color .15s, background-color .15s, box-shadow .15s;
}
.seg label:hover { color: var(--fg); }
.seg input:checked + label { background: var(--panel); color: var(--fg); box-shadow: var(--seg-shadow); }
.seg input:focus-visible + label { outline: 2px solid var(--focus); outline-offset: 1px; }
.seg .serif-label { font-family: Georgia, "Times New Roman", serif; font-size: 14px; }

/* "New page per H1" uses Jive's switch; compact label to match the segmented controls */
.page-breaks { gap: 8px; align-items: center; color: var(--muted); font-size: 13px; font-weight: 500; line-height: 1; }
.page-breaks:hover { color: var(--fg); }

/* Buttons are Jive's (.jv-btn); the theme button is icon-only */
.actions .jv-btn svg { width: 15px; height: 15px; }
.theme-btn { font-size: 0; gap: 0; width: 32px; padding: 0; }
.theme-btn .jv-themeflip__icon { font-size: 16px; line-height: 1; width: auto; }
.jv-btn kbd {
  font: 500 11px/1 var(--font-mono);
  padding: 3px 5px;
  border-radius: 4px;
  background: color-mix(in oklab, var(--on-brand) 18%, transparent);
}

/* ---------- workspace ---------- */
.workspace {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.15fr);
  min-height: 0;
}
.pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.pane-editor { background: var(--panel); border-right: 1px solid var(--line); }
.pane-preview { background: var(--desk); }
.pane-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  height: 38px;
  padding: 0 20px;
  border-bottom: 1px solid var(--line);
  font-size: 12px;
}
.pane-preview .pane-head { background: var(--bg); }
.pane-title { font-weight: 600; letter-spacing: .01em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pane-meta { color: var(--muted); font: 400 11.5px/1 var(--font-mono); font-variant-numeric: tabular-nums; white-space: nowrap; }

#editor {
  flex: 1;
  width: 100%;
  min-height: 0;
  resize: none;
  border: 0;
  padding: 22px 24px 40px;
  background: transparent;
  color: var(--fg);
  font: 400 13px/1.75 var(--font-mono);
  tab-size: 2;
  outline: none;
  caret-color: var(--accent);
}
#editor::placeholder { color: var(--muted); }
#editor::selection { background: var(--accent-ring); }

.desk { flex: 1; overflow: auto; padding: 32px 28px 56px; }
.paper {
  /* width, min-height and padding are set from the chosen paper size, in mm.
     Always light: the sheet is the PDF, so checkboxes etc. never take the dark theme. */
  color-scheme: light;
  margin: 0 auto;
  background: #fff;
  box-shadow: var(--paper-shadow);
  border-radius: 2px;
  transform-origin: top center;
}

/* ---------- footer ---------- */
.footer {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding-block: 9px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  background: var(--panel);
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}
.footer svg { width: 13px; height: 13px; }

/* ---------- drop overlay ---------- */
.drop {
  position: fixed; inset: 0;
  display: grid; place-items: center;
  background: color-mix(in srgb, var(--bg) 70%, transparent);
  backdrop-filter: blur(6px);
  pointer-events: none;
  z-index: 20;
}
.drop-card {
  padding: 28px 40px;
  border: 2px dashed var(--accent);
  border-radius: 14px;
  background: var(--panel);
  color: var(--fg);
  font-size: 16px;
  font-weight: 600;
}

/* ---------- narrow screens ---------- */
@media (max-width: 900px) {
  .controls { order: 3; flex-basis: 100%; justify-content: flex-start; }
  .brand { flex: 1; }
  html, body { height: auto; }
  .workspace { grid-template-columns: minmax(0, 1fr); }
  .pane-editor { border-right: 0; border-bottom: 1px solid var(--line); }
  #editor { height: 45vh; flex: none; }
  .desk { padding: 16px; }
}

/* Phones: name + theme on the first row, Open / Export sharing the second, page settings below */
@media (max-width: 640px) {
  .topbar { gap: 10px 8px; }
  .brand { flex: 1 1 auto; }
  .theme-btn { order: 1; }
  .actions { order: 2; flex-basis: 100%; }
  .actions > .jv-btn { flex: 1; }
  .jv-btn kbd { display: none; }
  .controls { order: 3; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

/* ---------- rendered document (always on white paper) ---------- */
.markdown-body {
  font-family: var(--font-ui);
  font-size: 11pt;
  line-height: 1.6;
  color: #1a1d23;
  word-wrap: break-word;
}
.markdown-body.serif { font-family: Georgia, "Times New Roman", serif; font-size: 11.5pt; }
.markdown-body > :first-child { margin-top: 0; }
.markdown-body h1, .markdown-body h2, .markdown-body h3,
.markdown-body h4, .markdown-body h5, .markdown-body h6 {
  margin: 1.5em 0 .55em; line-height: 1.25; font-weight: 650; letter-spacing: -.01em; color: #0f1115;
  text-wrap: balance;
}
.markdown-body h1 { font-size: 2em; letter-spacing: -.02em; padding-bottom: .3em; border-bottom: 1px solid #e3e6eb; }
.markdown-body h2 { font-size: 1.45em; padding-bottom: .25em; border-bottom: 1px solid #e3e6eb; }
.markdown-body h3 { font-size: 1.2em; }
.markdown-body h4 { font-size: 1em; }
.markdown-body p, .markdown-body ul, .markdown-body ol,
.markdown-body blockquote, .markdown-body table, .markdown-body pre { margin: 0 0 1em; }
.markdown-body ul, .markdown-body ol { padding-left: 1.6em; }
.markdown-body li + li { margin-top: .25em; }
.markdown-body a { color: #0b63c4; text-decoration: underline; text-decoration-color: #b9d3f0; text-underline-offset: 2px; }
.markdown-body strong { font-weight: 650; }
.markdown-body blockquote { padding: .1em 1em; color: #555d6b; border-left: 3px solid #d5dae2; }
.markdown-body code {
  font-family: var(--font-mono);
  font-size: .86em;
  padding: .15em .38em;
  background: #f1f3f6;
  border-radius: 4px;
}
.markdown-body pre {
  padding: 14px 16px;
  overflow: auto;
  background: #f6f7f9;
  border: 1px solid #eceff3;
  border-radius: 6px;
  line-height: 1.5;
}
.markdown-body pre code { padding: 0; background: none; font-size: .84em; white-space: pre-wrap; }
.markdown-body pre code.hljs { background: none; padding: 0; }
.markdown-body table { border-collapse: collapse; display: block; width: max-content; max-width: 100%; overflow: auto; font-variant-numeric: tabular-nums; }
.markdown-body th, .markdown-body td { padding: 6px 12px; border: 1px solid #e3e6eb; text-align: left; }
.markdown-body th { font-weight: 600; background: #f6f7f9; }
.markdown-body img { max-width: 100%; }
.markdown-body hr { height: 1px; border: 0; background: #e3e6eb; margin: 2em 0; }
.markdown-body input[type=checkbox] { margin: 0 .45em 0 -1.4em; vertical-align: middle; accent-color: #0b63c4; }
.markdown-body li:has(> input[type=checkbox]) { list-style: none; }

/* ---------- print = the PDF ---------- */
@media print {
  :root { color-scheme: light; }
  html, body { height: auto; display: block; background: #fff; }
  .topbar, .pane-editor, .pane-head, .footer, .drop { display: none !important; }
  .workspace, .pane, .desk { display: block; padding: 0; overflow: visible; background: #fff; }
  .paper {
    width: auto !important; min-height: 0 !important; padding: 0 !important;
    margin: 0; box-shadow: none; border-radius: 0; zoom: 1 !important;
  }
  .markdown-body pre, .markdown-body table, .markdown-body img, .markdown-body blockquote { break-inside: avoid; }
  .markdown-body h1, .markdown-body h2, .markdown-body h3, .markdown-body h4 { break-after: avoid; }
  .markdown-body.h1-breaks h1:not(:first-child) { break-before: page; }
  .markdown-body table { display: table; width: auto; }
  .markdown-body pre { overflow: visible; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
