/* JECC Markdown Viewer — application chrome and layout. */

*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--font-ui);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
: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; }
.skip-link { position: absolute; left: -999px; top: 4px; z-index: 100; background: var(--bg); padding: 6px 10px; }
.skip-link:focus { left: 8px; }
kbd { font: 12px var(--font-mono); border: 1px solid var(--border); border-bottom-width: 2px; border-radius: 4px; padding: 0 5px; background: var(--surface); }
.icon-btn svg, .drop-icon { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Top bar ---------- */
.topbar {
  height: var(--topbar-h);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 10px;
  background: var(--navy);
  color: #fff;
  position: relative;
  z-index: 30;
  border-bottom: 3px solid var(--gold);
}
:root[data-theme="dark"] .topbar { background: #0A1220; }
.brand { display: flex; align-items: center; gap: 8px; color: #fff; text-decoration: none; font-weight: 600; white-space: nowrap; }
.brand img { border-radius: 50%; }
.docname { display: flex; align-items: center; gap: 6px; min-width: 0; max-width: 32vw; padding-left: 10px; margin-left: 4px; border-left: 1px solid rgba(255,255,255,.25); }
.docname span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; opacity: .92; }
.dirty-dot { color: var(--gold); }
.spacer { flex: 1 1 auto; }
.btn, .icon-btn, .mini-btn {
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  cursor: pointer;
  white-space: nowrap;
}
.topbar .btn { color: #fff; padding: 6px 10px; border-color: rgba(255,255,255,.28); }
.topbar .btn:hover, .topbar .icon-btn:hover { background: rgba(255,255,255,.12); }
.topbar .btn-primary { background: var(--gold); color: #0B2545; border-color: var(--gold); font-weight: 600; }
.topbar .btn-primary:hover { background: #d8b23a; }
.icon-btn { color: #fff; width: 36px; height: 36px; display: inline-flex; align-items: center; justify-content: center; font-weight: 700; font-size: 16px; }
.topbar :focus-visible { outline-color: var(--gold); }
.btn:disabled, .icon-btn:disabled { opacity: .45; cursor: not-allowed; }
.segmented { display: inline-flex; border: 1px solid rgba(255,255,255,.28); border-radius: 6px; overflow: hidden; }
.segmented button { background: transparent; border: 0; color: #fff; padding: 6px 10px; cursor: pointer; }
.segmented button[aria-pressed="true"] { background: rgba(255,255,255,.18); box-shadow: inset 0 -2px 0 var(--gold); }

/* Generic buttons outside the top bar */
main .btn, .dialog .btn, .empty-state .btn { padding: 7px 14px; border-color: var(--border); background: var(--surface); color: var(--text); }
main .btn:hover, .dialog .btn:hover { background: var(--surface-2); }
main .btn-primary, .dialog .btn-primary, .empty-state .btn-primary { background: var(--navy); color: #fff; border-color: var(--navy); }
:root[data-theme="dark"] main .btn-primary, :root[data-theme="dark"] .dialog .btn-primary, :root[data-theme="dark"] .empty-state .btn-primary { background: var(--gold); color: #0E1624; border-color: var(--gold); }
.mini-btn { padding: 3px 8px; border-color: var(--border); background: var(--bg); color: var(--text); font-size: 12.5px; }
.mini-btn:hover { background: var(--surface-2); }
.mini-btn[aria-pressed="true"] { background: var(--surface-2); border-color: var(--accent); }

/* Menus */
.menu-wrap { position: relative; }
.menu, .ctx-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  min-width: 250px;
  max-height: 75vh;
  overflow: auto;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 6px;
  z-index: 50;
}
.ctx-menu { position: fixed; top: 0; right: auto; }
.menu button, .ctx-menu button {
  display: flex; justify-content: space-between; gap: 18px; align-items: center;
  width: 100%; text-align: left; padding: 7px 10px; border: 0; border-radius: 5px; background: transparent; cursor: pointer;
}
.menu button:hover, .menu button:focus-visible, .ctx-menu button:hover, .ctx-menu button:focus-visible { background: var(--surface-2); outline: none; }
.menu kbd { font-size: 11px; }
.menu-sep { height: 1px; background: var(--border); margin: 5px 4px; }
.menu-label { font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted); padding: 4px 10px; }
.menu-recent .recent-item { display: flex; align-items: center; }
.menu-recent .recent-item button:first-child { flex: 1; min-width: 0; }
.menu-recent .recent-item .r-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.menu-recent .recent-item .r-meta { font-size: 11.5px; color: var(--text-muted); }
.menu-recent .recent-item .r-del { width: auto; flex: 0 0 auto; color: var(--text-muted); }
.menu-recent .empty { padding: 4px 10px; color: var(--text-muted); font-size: 13px; }

/* ---------- Progress ---------- */
.progress { height: 3px; flex: 0 0 auto; background: transparent; position: relative; z-index: 29; }
.progress-bar { height: 100%; width: 0; background: var(--gold); transition: width .1s linear; }

/* ---------- Layout ---------- */
.layout { flex: 1 1 auto; display: flex; min-height: 0; position: relative; }
.toc-panel {
  width: var(--toc-w);
  min-width: 180px;
  max-width: 420px;
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  background: var(--surface);
  position: relative;
}
body.toc-closed .toc-panel { display: none; }
body.no-doc .toc-panel { display: none; }
.toc-head { padding: 10px; display: flex; flex-direction: column; gap: 6px; border-bottom: 1px solid var(--border); }
.toc-head input[type="search"] { width: 100%; padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); }
.toc-tools { display: flex; gap: 6px; align-items: center; }
.toc-tools select { flex: 1; padding: 4px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); }
.toc { flex: 1 1 auto; overflow: auto; padding: 6px 4px 16px; overscroll-behavior: contain; }
.toc ul { list-style: none; margin: 0; padding: 0; }
.toc ul ul { padding-left: 14px; }
.toc li[hidden] { display: none; }
.toc .row { display: flex; align-items: flex-start; gap: 2px; border-radius: 5px; position: relative; }
.toc .row:hover { background: var(--surface-2); }
.toc .twisty { flex: 0 0 18px; height: 22px; border: 0; background: transparent; cursor: pointer; color: var(--text-muted); padding: 0; font-size: 10px; line-height: 22px; }
.toc .twisty.leaf { visibility: hidden; }
.toc a {
  flex: 1 1 auto; min-width: 0;
  color: var(--text); text-decoration: none;
  padding: 3px 4px; border-radius: 4px; line-height: 1.35;
  overflow-wrap: anywhere; /* full text, wrapped, never truncated (FR-21) */
}
.toc a.lvl-1 { font-weight: 700; }
.toc a.lvl-2 { font-weight: 600; }
.toc a[aria-current="true"] { color: var(--accent); background: var(--surface-2); box-shadow: inset 3px 0 0 var(--gold); }
:root[data-theme="dark"] .toc a[aria-current="true"] { color: var(--gold); }
.toc a .num { color: var(--text-muted); margin-right: 4px; }
.toc .badge { flex: 0 0 auto; font-size: 11px; color: var(--text-muted); padding: 4px 2px 0; white-space: nowrap; }
.toc .kebab { flex: 0 0 auto; border: 0; background: transparent; color: var(--text-muted); cursor: pointer; opacity: 0; padding: 2px 4px; }
.toc .row:hover .kebab, .toc .kebab:focus-visible { opacity: 1; }
.toc li.collapsed > ul { display: none; }
.toc mark { background: var(--mark); color: inherit; }
.toc-empty { color: var(--text-muted); padding: 10px; }
.toc-stats { border-top: 1px solid var(--border); padding: 8px 10px; font-size: 12px; color: var(--text-muted); }
.toc-resizer { position: absolute; top: 0; right: -4px; width: 8px; height: 100%; cursor: col-resize; z-index: 5; }
.toc-resizer:hover, .toc-resizer:focus-visible { background: linear-gradient(90deg, transparent 3px, var(--gold) 3px, var(--gold) 5px, transparent 5px); outline: none; }
.toc-resizer:focus-visible { box-shadow: inset 0 0 0 2px var(--focus); }
.toc-backdrop { display: none; }

.main { flex: 1 1 auto; min-width: 0; overflow: auto; position: relative; scroll-behavior: auto; }
.reader-wrap { padding: 12px 24px 60px; }
.doc-tools { display: flex; justify-content: flex-end; gap: 6px; max-width: var(--reader-max); margin: 0 auto 4px; flex-wrap: wrap; }
body.full-width .doc-tools { max-width: none; }
.split { display: flex; gap: 0; }
.reader { flex: 1 1 auto; min-width: 0; outline: none; }

/* Find bar */
.find-bar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
  padding: 8px 12px; background: var(--surface); border-bottom: 1px solid var(--border); box-shadow: var(--shadow);
}
.find-bar input[type="search"] { flex: 1 1 200px; padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); }
.find-count { min-width: 70px; color: var(--text-muted); font-size: 12.5px; text-align: center; }
.chk { display: inline-flex; align-items: center; gap: 4px; font-size: 13px; cursor: pointer; }

/* Warnings (link checker) */
#btn-warnings { color: var(--warning); border-color: var(--warning-border); }
.warnings-panel { max-width: var(--reader-max); margin: 6px auto 12px; border: 1px solid var(--warning-border); background: var(--warning-bg); border-radius: 8px; padding: 8px 12px; }
.warnings-panel h3 { margin: 0 0 6px; font-size: 14px; }
.warnings-panel ul { margin: 0; padding-left: 18px; }
.warnings-panel button { border: 0; background: none; color: var(--link); cursor: pointer; text-align: left; padding: 2px 0; text-decoration: underline; }

/* Empty state */
.empty-state { display: flex; align-items: center; justify-content: center; min-height: 100%; padding: 24px; }
.drop-zone {
  width: min(640px, 100%);
  border: 2px dashed var(--border);
  border-radius: 16px;
  padding: 40px 28px;
  text-align: center;
  background: var(--surface);
}
.drop-zone h1 { margin: 6px 0; color: var(--heading); font-family: var(--font-serif); }
.drop-icon { width: 56px; height: 56px; color: var(--gold); }
.empty-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin: 18px 0; }
.empty-recent { text-align: left; margin: 10px auto 0; max-width: 420px; }
.empty-recent h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted); margin: 0 0 6px; }
.empty-recent ul { list-style: none; padding: 0; margin: 0; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); }
.empty-recent li { display: flex; border-bottom: 1px solid var(--border); }
.empty-recent li:last-child { border-bottom: 0; }
.empty-recent li button { border: 0; background: transparent; cursor: pointer; padding: 8px 10px; text-align: left; }
.empty-recent li button:first-child { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty-recent li button:hover { background: var(--surface-2); }
.empty-recent .actions { display: flex; justify-content: space-between; margin-top: 6px; }
.empty-recent .actions button { border: 0; background: none; color: var(--link); cursor: pointer; padding: 2px; }
.privacy-note { color: var(--text-muted); font-size: 13px; margin-top: 16px; }

/* Status bar */
.statusbar {
  flex: 0 0 auto;
  display: flex; gap: 12px; justify-content: space-between; align-items: center;
  padding: 4px 12px; font-size: 12px; color: var(--text-muted);
  border-top: 1px solid var(--border); background: var(--surface);
}
.status-privacy { white-space: nowrap; }

/* Overlays, toasts, dialogs */
.back-to-top { position: fixed; right: 18px; bottom: 40px; width: 40px; height: 40px; border-radius: 50%; border: 1px solid var(--border); background: var(--bg); color: var(--text); box-shadow: var(--shadow); cursor: pointer; z-index: 25; font-size: 18px; }
.drop-overlay { position: fixed; inset: 0; z-index: 90; background: rgba(11, 37, 69, .72); display: flex; align-items: center; justify-content: center; pointer-events: none; }
.drop-overlay div { color: #fff; font-size: 22px; border: 3px dashed var(--gold); border-radius: 18px; padding: 48px 64px; }
.loading-overlay { position: fixed; inset: 0; z-index: 80; background: rgba(14, 22, 36, .35); display: flex; flex-direction: column; gap: 12px; align-items: center; justify-content: center; color: #fff; font-size: 15px; text-shadow: 0 1px 2px rgba(0,0,0,.5); }
.spinner { width: 44px; height: 44px; border-radius: 50%; border: 4px solid rgba(255,255,255,.35); border-top-color: var(--gold); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toasts { position: fixed; left: 50%; bottom: 36px; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 95; width: min(520px, calc(100vw - 32px)); }
.toast { display: flex; gap: 10px; align-items: center; background: var(--navy); color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); border-left: 4px solid var(--gold); }
.toast.error { background: #7A1F1F; border-left-color: #FFB4B4; }
.toast .msg { flex: 1; }
.toast button { border: 1px solid rgba(255,255,255,.5); background: transparent; color: #fff; border-radius: 5px; padding: 3px 9px; cursor: pointer; }

.dialog { border: 1px solid var(--border); border-radius: 12px; padding: 0; background: var(--bg); color: var(--text); width: min(640px, calc(100vw - 24px)); box-shadow: var(--shadow); }
.dialog-wide { width: min(900px, calc(100vw - 24px)); }
.dialog::backdrop { background: rgba(14, 22, 36, .55); }
.dialog form { padding: 18px 20px; }
.dialog h2 { margin: 0 0 12px; font-size: 18px; color: var(--heading); }
.dialog textarea { width: 100%; font: 13px/1.5 var(--font-mono); border: 1px solid var(--border); border-radius: 8px; padding: 10px; background: var(--surface); resize: vertical; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; align-items: center; margin-top: 14px; flex-wrap: wrap; }
.dialog-actions .spacer { flex: 1; }
.dialog .hint { color: var(--text-muted); font-size: 13px; }
.file-btn { display: inline-flex; align-items: center; cursor: pointer; }
.settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 12px; }
.settings-grid fieldset { border: 1px solid var(--border); border-radius: 8px; margin: 0; padding: 10px 12px; display: flex; flex-direction: column; gap: 8px; }
.settings-grid legend { font-weight: 600; padding: 0 4px; }
.settings-grid label:not(.chk) { display: flex; flex-direction: column; gap: 3px; font-size: 13px; }
.settings-grid select, .settings-grid input[type="text"] { padding: 5px 6px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); }
.logo-row { display: flex; align-items: center; gap: 8px; }
.logo-row img { max-height: 40px; max-width: 120px; }
table.shortcuts { border-collapse: collapse; width: 100%; }
table.shortcuts td { padding: 5px 8px; border-bottom: 1px solid var(--border); }
table.shortcuts td:first-child { white-space: nowrap; width: 40%; }

.lightbox { width: 96vw; max-width: 96vw; height: 92vh; max-height: 92vh; background: rgba(10, 16, 28, .96); border: 0; }
.lightbox form { padding: 0; position: absolute; right: 10px; top: 8px; z-index: 2; }
.lightbox-close { background: rgba(255,255,255,.15); color: #fff; border: 0; border-radius: 50%; width: 36px; height: 36px; cursor: pointer; font-size: 16px; }
.lightbox-stage { width: 100%; height: 100%; overflow: auto; display: flex; align-items: center; justify-content: center; }
.lightbox-stage img { max-width: 100%; max-height: 100%; cursor: zoom-in; background: #fff; }
.lightbox-stage.zoomed { display: block; }
.lightbox-stage.zoomed img { max-width: none; max-height: none; cursor: zoom-out; }

/* Focus mode */
body.focus-mode .topbar, body.focus-mode .toc-panel, body.focus-mode .statusbar, body.focus-mode .doc-tools { display: none; }
body.focus-mode .reader > .md-body > * { opacity: .28; transition: opacity .25s; }
body.focus-mode .reader > .md-body > .in-focus { opacity: 1; }
.focus-exit { position: fixed; top: 10px; right: 14px; z-index: 40; }

/* ---------- Mobile (< 768px) ---------- */
@media (max-width: 767.98px) {
  .brand-text, .docname, #btn-print, #btn-help, #view-mode [data-mode="split"] { display: none !important; }
  #view-mode button { padding: 6px 7px; }
  .topbar { gap: 4px; padding: 0 6px; }
  .topbar .btn { padding: 6px 8px; }
  .toc-panel {
    position: fixed; left: 0; top: 0; bottom: 0; z-index: 60;
    width: min(86vw, 340px) !important; max-width: none;
    transform: translateX(-102%); transition: transform .2s ease;
    box-shadow: var(--shadow);
  }
  body.toc-closed .toc-panel { display: flex; }
  body.toc-drawer-open .toc-panel { transform: none; }
  body.toc-drawer-open .toc-backdrop { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 55; }
  .toc-resizer { display: none; }
  .reader-wrap { padding: 8px 14px 60px; }
  .menu-sheet {
    position: fixed; left: 0; right: 0; bottom: 0; top: auto;
    border-radius: 14px 14px 0 0; max-height: 70vh; min-width: 0; padding: 10px 8px 18px;
  }
  .menu-sheet button { padding: 12px 12px; font-size: 15px; }
  .status-privacy { display: none; }
  .back-to-top { bottom: 34px; right: 12px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0s !important; transition-duration: 0s !important; scroll-behavior: auto !important; }
}

/* ---------- Editor: Edit / Split modes (FR-49) ---------- */
body.mode-edit .doc-tools, body.mode-split .doc-tools, body.mode-edit .docinfo, body.mode-split .docinfo { display: none; }
body.mode-edit .reader-wrap, body.mode-split .reader-wrap { padding-bottom: 12px; }
body.mode-split .split { gap: 18px; align-items: flex-start; }
.editor-pane {
  position: sticky; top: 12px; z-index: 5;
  display: flex; flex-direction: column; min-width: 0;
  border: 1px solid var(--border); border-radius: 8px; background: var(--bg); overflow: hidden;
}
body.mode-edit .editor-pane { flex: 1 1 auto; max-width: 1100px; margin: 0 auto; }
body.mode-split .editor-pane { flex: 0 0 calc(50% - 9px); }
body.mode-split .reader { flex: 1 1 0; }
body.mode-split .md-body { max-width: none; }
.editor-toolbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 2px;
  padding: 4px 6px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.tb-btn { min-width: 30px; height: 30px; padding: 0 6px; border: 1px solid transparent; border-radius: 5px; background: transparent; cursor: pointer; font-size: 14px; }
.tb-btn:hover, .tb-btn[aria-pressed="true"], .tb-btn[aria-expanded="true"] { background: var(--surface-2); border-color: var(--border); }
.tb-select { height: 30px; border: 1px solid var(--border); border-radius: 5px; background: var(--bg); padding: 0 4px; font-size: 13px; }
.tb-sep { width: 1px; height: 20px; background: var(--border); margin: 0 4px; }
.editor-toolbar .spacer { flex: 1 1 auto; }
.tb-popwrap { position: relative; }
.tb-grid { position: absolute; top: 34px; left: 0; z-index: 30; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow); padding: 8px; }
.tb-grid-cells { display: grid; grid-template-columns: repeat(8, 18px); gap: 3px; padding: 2px; border-radius: 4px; }
.tb-grid-cells span { width: 18px; height: 18px; border: 1px solid var(--border); border-radius: 2px; background: var(--surface); cursor: pointer; }
.tb-grid-cells span.on { background: var(--gold); border-color: #a8861c; }
.tb-grid-label { text-align: center; font-size: 12.5px; color: var(--text-muted); margin-top: 6px; }
.ed-find { padding: 6px 8px; border-bottom: 1px solid var(--border); background: var(--surface); display: flex; flex-direction: column; gap: 6px; }
.ed-find-row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.ed-find input[type="search"], .ed-find input[type="text"] { flex: 1 1 180px; padding: 5px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); }
.editor-body { position: relative; flex: 1 1 auto; min-height: 0; display: flex; }
.editor-gutter {
  --digits: 3;
  flex: 0 0 auto; width: calc(var(--digits) * 1ch + 18px); position: relative; overflow: hidden;
  background: var(--surface); border-right: 1px solid var(--border);
  font: 13px/1.6 var(--font-mono); color: var(--text-muted); user-select: none;
}
.editor-gutter div { position: absolute; right: 8px; line-height: 1.6; }
.editor-text, .editor-mirror {
  font: 13.5px/1.6 var(--font-mono); padding: 10px 12px; tab-size: 4;
  white-space: pre-wrap; overflow-wrap: anywhere; word-break: normal;
}
.editor-text {
  flex: 1 1 auto; min-width: 0; height: 100%; resize: none; border: 0; outline: none; margin: 0;
  background: var(--bg); color: var(--text); overflow: auto;
}
.editor-text.nowrap { white-space: pre; overflow-wrap: normal; }
.editor-text:focus-visible { box-shadow: inset 0 0 0 2px var(--focus); }
.editor-mirror { position: absolute; left: -99999px; top: 0; visibility: hidden; }
.editor-status { display: flex; justify-content: space-between; gap: 10px; padding: 3px 10px; font-size: 12px; color: var(--text-muted); border-top: 1px solid var(--border); background: var(--surface); }
@media (max-width: 767.98px) {
  body.mode-edit .editor-pane { max-width: none; }
  .tb-sep, .editor-toolbar .spacer { display: none; }
}
.reader.offscreen-capture { position: fixed; left: -12000px; top: 0; width: 900px; }

/* Batch dialog (FR-50a) */
.dialog label.stack { display: flex; flex-direction: column; gap: 6px; margin: 8px 0; }
.batch-list { max-height: 180px; overflow: auto; margin: 6px 0 10px; padding-left: 18px; font-size: 13px; }
.batch-list .warn { color: var(--warning); list-style: none; margin-left: -18px; }
