/* ===== Pena — estilos ===== */
:root {
  --fs: 17px;
  --measure: 72ch;
  --sidebar-w: 270px;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", "Cascadia Code", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  --font: var(--mono);
  --radius: 8px;
}
[data-font="sans"]  { --font: var(--sans); }
[data-font="serif"] { --font: var(--serif); }

[data-theme="light"] {
  --bg: #faf9f7; --bg2: #f1efeb; --bg3: #e7e4de; --fg: #26231f; --fg2: #6f6a62; --fg3: #a29c92;
  --line: #e2ded7; --accent: #c2410c; --accent-bg: #fbe9df; --sel: #f6d6c4; --code: #efece6;
}
[data-theme="dark"] {
  --bg: #17171a; --bg2: #1e1e22; --bg3: #2a2a30; --fg: #e6e3dd; --fg2: #9d988f; --fg3: #6b675f;
  --line: #2c2c32; --accent: #fb923c; --accent-bg: #3a2516; --sel: #5a3519; --code: #24242a;
}
[data-theme="sepia"] {
  --bg: #f4ecd8; --bg2: #ebe1c9; --bg3: #e0d4b8; --fg: #3b2f22; --fg2: #75644e; --fg3: #a39176;
  --line: #ddd0b3; --accent: #9a3412; --accent-bg: #ead2b8; --sel: #e4c49c; --code: #e9dfc6;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--fg);
  font: 14px/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
::selection { background: var(--sel); }
.muted { color: var(--fg2); }
.small { font-size: 12px; }
kbd { font: 11px var(--mono); background: var(--bg3); border-radius: 4px; padding: 1px 5px; color: var(--fg2); }

/* ===== layout ===== */
#app { display: flex; height: 100vh; height: 100dvh; overflow: hidden; }
#sidebar {
  width: var(--sidebar-w); flex: 0 0 var(--sidebar-w);
  background: var(--bg2); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0;
  transition: margin-left .18s ease;
}
.no-sidebar #sidebar { margin-left: calc(-1 * var(--sidebar-w)); }
#main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.side-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 12px 8px 16px; }
.brand { font: 600 16px var(--sans); letter-spacing: .2px; display: inline-flex; align-items: center; gap: 8px; }
.brand img { width: 28px; height: 28px; border-radius: 7px; box-shadow: 0 0 0 1px var(--line); }
.side-search { padding: 4px 12px 8px; }
.side-search input {
  width: 100%; padding: 7px 10px; border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--bg); outline: none;
}
.side-search input:focus { border-color: var(--accent); }
.active-tag { margin: 0 12px 6px; font-size: 12px; color: var(--accent); display: flex; justify-content: space-between; }
.active-tag button { border: 0; background: none; color: var(--fg2); padding: 0; }

.doc-list { flex: 1; overflow-y: auto; padding: 0 6px; min-height: 80px; }
.doc-item {
  display: block; width: 100%; text-align: left; border: 0; background: none;
  padding: 7px 10px; border-radius: 6px; margin-bottom: 1px;
}
.doc-item:hover { background: var(--bg3); }
.doc-item.active { background: var(--accent-bg); }
.doc-item .t { display: block; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-item .p { display: block; font-size: 12px; color: var(--fg2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-item .pin { color: var(--accent); margin-right: 4px; font-size: 11px; }
.doc-list .none { padding: 12px; color: var(--fg3); font-size: 13px; }

.side-section { border-top: 1px solid var(--line); padding: 6px 12px; max-height: 32%; overflow-y: auto; }
.side-section summary { cursor: pointer; font-size: 11px; text-transform: uppercase; letter-spacing: .6px; color: var(--fg2); padding: 4px 0; }
.tag-list { display: flex; flex-wrap: wrap; gap: 4px; padding: 4px 0 6px; }
.tag {
  border: 0; background: var(--bg3); color: var(--fg2); border-radius: 99px;
  padding: 1px 9px; font-size: 12px;
}
.tag:hover, .tag.on { background: var(--accent-bg); color: var(--accent); }
.tag small { opacity: .6; margin-left: 3px; }

.side-foot { border-top: 1px solid var(--line); padding: 8px 10px; display: flex; flex-wrap: wrap; gap: 2px 8px; }
.link-btn { border: 0; background: none; color: var(--fg2); font-size: 12px; padding: 3px 4px; text-decoration: none; }
.link-btn:hover { color: var(--fg); }

/* ===== toolbar ===== */
#toolbar {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px;
  border-bottom: 1px solid var(--line); min-height: 48px;
}
.icon-btn {
  border: 0; background: none; color: var(--fg2); width: 32px; height: 32px; border-radius: 6px;
  font-size: 16px; line-height: 1; display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: var(--bg3); color: var(--fg); }
.doc-title { flex: 1; min-width: 0; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.save-state { font-size: 12px; color: var(--fg3); white-space: nowrap; }
.save-state.err { color: #dc2626; }
.seg { display: inline-flex; background: var(--bg2); border-radius: 7px; padding: 2px; }
.seg button { border: 0; background: none; padding: 4px 10px; border-radius: 5px; font-size: 12px; color: var(--fg2); }
.seg button.on { background: var(--bg); color: var(--fg); box-shadow: 0 1px 2px rgba(0,0,0,.08); }

/* ===== workspace ===== */
#workspace { flex: 1; display: flex; min-height: 0; }
.pane { flex: 1; min-width: 0; overflow-y: auto; }
#workspace[data-view="edit"] .pane-preview { display: none; }
#workspace[data-view="preview"] .pane-edit { display: none; }
#workspace[data-view="split"] .pane-edit { border-right: 1px solid var(--line); }

#editor {
  display: block; width: 100%; height: 100%; margin: 0;
  padding: 40px max(24px, calc((100% - var(--measure)) / 2)) 40vh;
  border: 0; outline: none; resize: none; background: transparent; overflow-y: auto;
  font: var(--fs)/1.7 var(--font); color: var(--fg); caret-color: var(--accent);
  tab-size: 4;
}
#editor::placeholder { color: var(--fg3); }
.pane-edit { overflow: hidden; }

#preview { max-width: calc(var(--measure) + 96px); margin: 0 auto; padding: 40px 48px 30vh; }

/* ===== markdown render ===== */
.md { font: var(--fs)/1.7 var(--font); overflow-wrap: break-word; }
[data-font="mono"] .md { font-family: var(--sans); }
.md h1, .md h2, .md h3, .md h4 { line-height: 1.25; margin: 1.6em 0 .5em; font-weight: 650; }
.md h1 { font-size: 1.75em; margin-top: 0; }
.md h2 { font-size: 1.35em; }
.md h3 { font-size: 1.12em; }
.md p, .md ul, .md ol, .md blockquote, .md pre, .md table { margin: 0 0 1em; }
.md ul, .md ol { padding-left: 1.4em; }
.md li { margin: .15em 0; }
.md li:has(> input[type=checkbox]) { list-style: none; margin-left: -1.3em; }
.md input[type=checkbox] { accent-color: var(--accent); margin-right: .45em; cursor: pointer; }
.md blockquote { border-left: 3px solid var(--accent); margin-left: 0; padding: .1em 1em; color: var(--fg2); }
.md code { font: .88em var(--mono); background: var(--code); padding: .12em .35em; border-radius: 4px; }
.md pre { background: var(--code); padding: 14px 16px; border-radius: var(--radius); overflow-x: auto; }
.md pre code { background: none; padding: 0; }
.md table { border-collapse: collapse; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--line); padding: 6px 12px; text-align: left; }
.md th { background: var(--bg2); }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 2em 0; }
.md img { max-width: 100%; border-radius: var(--radius); }
.md a.wikilink, .md .wikilink { color: var(--accent); text-decoration: none; border-bottom: 1px dashed currentColor; cursor: pointer; }
.md a.wikilink.missing { opacity: .6; }
.md .hashtag { color: var(--accent); background: var(--accent-bg); border-radius: 99px; padding: 0 .45em; font-size: .88em; cursor: pointer; }

/* ===== statusbar ===== */
#statusbar {
  display: flex; gap: 14px; align-items: center; padding: 6px 14px; border-top: 1px solid var(--line);
  font-size: 12px; color: var(--fg3); min-height: 30px; overflow-x: auto; white-space: nowrap;
}
.st-tags .tag { font-size: 11px; }
.st-backlinks a { color: var(--fg2); margin-left: 6px; cursor: pointer; }
.st-backlinks a:hover { color: var(--accent); }

/* ===== modo foco ===== */
.focus #sidebar { margin-left: calc(-1 * var(--sidebar-w)); }
.focus #toolbar, .focus #statusbar { opacity: 0; transition: opacity .2s; }
.focus #toolbar:hover, .focus #statusbar:hover { opacity: 1; }

/* ===== popup (/ e [[) ===== */
.popup, .menu {
  position: fixed; z-index: 50; min-width: 220px; max-width: 340px; max-height: 280px; overflow-y: auto;
  background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(0,0,0,.14); padding: 4px;
}
.popup .it, .menu .it {
  display: block; width: 100%; text-align: left; border: 0; background: none; padding: 6px 10px; border-radius: 5px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.popup .it.on, .popup .it:hover, .menu .it:hover { background: var(--accent-bg); }
.popup .hint { padding: 4px 10px; font-size: 11px; color: var(--fg3); }
.menu .sep { height: 1px; background: var(--line); margin: 4px 0; }
.menu .it.danger { color: #dc2626; }

/* ===== modal / paleta ===== */
.overlay {
  position: fixed; inset: 0; z-index: 100; background: rgba(0,0,0,.28);
  display: flex; align-items: flex-start; justify-content: center; padding: 12vh 16px 16px;
}
.modal {
  width: 100%; max-width: 560px; max-height: 76vh; display: flex; flex-direction: column;
  background: var(--bg); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 20px 50px rgba(0,0,0,.25);
  overflow: hidden;
}
.modal.wide { max-width: 860px; }
.modal-head { padding: 14px 16px 10px; font-weight: 600; display: flex; justify-content: space-between; align-items: center; }
.modal-body { padding: 0 16px 16px; overflow-y: auto; }
.modal-foot { padding: 10px 16px; border-top: 1px solid var(--line); display: flex; gap: 8px; justify-content: flex-end; }
.btn {
  border: 1px solid var(--line); background: var(--bg2); padding: 6px 14px; border-radius: 7px;
}
.btn:hover { background: var(--bg3); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: #dc2626; }
.field { display: block; margin-bottom: 12px; font-size: 12px; color: var(--fg2); }
.field input, .field select, .field textarea {
  display: block; width: 100%; margin-top: 4px; padding: 7px 10px; border: 1px solid var(--line);
  border-radius: 7px; background: var(--bg2); color: var(--fg); outline: none;
}
.field textarea { font: 13px/1.5 var(--mono); min-height: 260px; resize: vertical; }
.field input:focus, .field textarea:focus, .field select:focus { border-color: var(--accent); }

.palette-input {
  width: 100%; border: 0; border-bottom: 1px solid var(--line); padding: 14px 16px; font-size: 16px;
  background: transparent; outline: none;
}
.palette-list { overflow-y: auto; padding: 6px; }
.palette-list .it {
  display: flex; justify-content: space-between; gap: 12px; width: 100%; text-align: left; border: 0; background: none;
  padding: 8px 10px; border-radius: 6px;
}
.palette-list .it.on { background: var(--accent-bg); }
.palette-list .it span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.palette-list .it .k { color: var(--fg3); font-size: 12px; flex-shrink: 0; }
.palette-list .grp { font-size: 11px; color: var(--fg3); text-transform: uppercase; letter-spacing: .6px; padding: 8px 10px 4px; }

.tpl-grid { display: grid; grid-template-columns: 200px 1fr; gap: 16px; min-height: 380px; }
.tpl-list { border-right: 1px solid var(--line); padding-right: 8px; overflow-y: auto; }
.tpl-list .it { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 6px 10px; border-radius: 6px; }
.tpl-list .it.on { background: var(--accent-bg); }
.help { font-size: 12px; color: var(--fg2); margin: 0 0 12px; }
.share-link { display: flex; gap: 6px; }
.share-link input { flex: 1; }

/* ===== toast ===== */
.toast {
  position: fixed; bottom: 46px; left: 50%; transform: translateX(-50%); z-index: 200;
  background: var(--fg); color: var(--bg); padding: 8px 16px; border-radius: 8px; font-size: 13px;
  box-shadow: 0 8px 24px rgba(0,0,0,.2);
}

/* ===== login / share ===== */
body.auth { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 16px; }
.auth-box { width: 100%; max-width: 360px; background: var(--bg2); border: 1px solid var(--line); border-radius: 14px; padding: 28px; }
.auth-box h1 { margin: 0 0 4px; font-size: 24px; color: var(--fg); }
.auth-logo { width: 72px; height: 72px; border-radius: 18px; box-shadow: 0 0 0 1px var(--line), 0 6px 18px rgba(0,0,0,.08); margin-bottom: 12px; }
.auth-box label { display: block; font-size: 12px; color: var(--fg2); margin: 14px 0 0; }
.auth-box input { display: block; width: 100%; margin-top: 4px; padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); }
.auth-box button { margin-top: 20px; width: 100%; padding: 10px; border: 0; border-radius: 8px; background: var(--accent); color: #fff; font-weight: 600; }
.auth-error { background: #fee2e2; color: #991b1b; padding: 8px 12px; border-radius: 8px; margin-top: 12px; font-size: 13px; }
body.share { padding: 0 16px; }
.share-wrap { max-width: 760px; margin: 0 auto; padding: 56px 0 80px; }
.share-wrap .md { font-family: var(--sans); font-size: 17px; }
.share-wrap footer { margin-top: 48px; padding-top: 16px; border-top: 1px solid var(--line); }

/* ===== mobile ===== */
@media (max-width: 760px) {
  #sidebar { position: fixed; z-index: 40; top: 0; bottom: 0; left: 0; box-shadow: 4px 0 24px rgba(0,0,0,.15); }
  .no-sidebar #sidebar, .focus #sidebar { margin-left: calc(-1 * var(--sidebar-w) - 30px); }
  #editor { padding: 24px 18px 40vh; }
  #preview { padding: 24px 18px 40vh; }
  #workspace[data-view="split"] .pane-preview { display: none; }
  .seg button[data-view="split"] { display: none; }
  .save-state { display: none; }
  .tpl-grid { grid-template-columns: 1fr; }
  .tpl-list { border-right: 0; border-bottom: 1px solid var(--line); max-height: 140px; }
}

/* ===== impressão ===== */
@media print {
  #sidebar, #toolbar, #statusbar, .pane-edit { display: none !important; }
  #workspace .pane-preview { display: block !important; overflow: visible; }
  #app, #main, #workspace { display: block; height: auto; overflow: visible; }
  body { background: #fff; color: #000; }
  #preview { padding: 0; max-width: none; }
}
