:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --border: #d8dde3;
  --text: #1f2730;
  --muted: #748090;
  --accent: #2f6fed;
  --accent-dark: #1f54c0;
  --danger: #d64545;
  --head-h: 48px;
}
* { box-sizing: border-box; }
/* The hidden attribute must win over component display rules below. */
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, "Segoe UI", "Hiragino Kaku Gothic ProN", Meiryo, sans-serif;
  color: var(--text); background: var(--bg); font-size: 14px;
}

/* topbar */
.topbar {
  height: var(--head-h); display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px; background: #1d2733; color: #fff;
}
.brand { color: #fff; text-decoration: none; font-weight: 700; }
.topnav { display: flex; align-items: center; gap: 16px; }
.topnav a { color: #cdd6e0; text-decoration: none; }
.topnav a:hover { color: #fff; }
.topnav .who { color: #8fa0b3; font-size: 13px; }
.topnav .clock { color: #cdd6e0; font-size: 13px; font-family: "SFMono-Regular", Consolas, monospace; cursor: pointer; padding: 2px 8px; border: 1px solid #3a4658; border-radius: 6px; }
.topnav .clock:hover { color: #fff; border-color: #5a6b82; }
.topnav .clock-input { width: 175px; padding: 1px 4px; margin: 0; font-family: "SFMono-Regular", Consolas, monospace; font-size: 13px; }
.inline { display: inline; margin: 0; }
button.link { background: none; border: none; color: #cdd6e0; cursor: pointer; font-size: 14px; }
button.link:hover { color: #fff; }

/* generic */
.container { max-width: 980px; margin: 24px auto; padding: 0 16px; }
h1 { font-size: 22px; } h2 { font-size: 17px; } h3 { font-size: 15px; }
.muted { color: var(--muted); }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 18px; margin-bottom: 18px; }
code { background: #eef1f5; padding: 1px 5px; border-radius: 4px; font-size: 12.5px; }

label { display: block; margin: 10px 0; font-weight: 600; }
label input[type=text], label input[type=email], label input[type=password],
input[type=text], input[type=email], input[type=password] {
  width: 100%; padding: 9px 10px; margin-top: 4px; border: 1px solid var(--border);
  border-radius: 7px; font-size: 14px; font-weight: 400;
}
.form-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.form-row input { width: auto; flex: 1 1 160px; margin-top: 0; }
.check { display: inline-flex; align-items: center; gap: 6px; font-weight: 400; }
.check input { width: auto; }

button, .btn {
  display: inline-block; padding: 8px 14px; border: 1px solid var(--border);
  background: #fff; border-radius: 7px; cursor: pointer; font-size: 13px;
  text-decoration: none; color: var(--text);
}
button:hover, .btn:hover { background: #f0f3f8; }
.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.primary:hover { background: var(--accent-dark); }
.danger { color: var(--danger); border-color: #e8b4b4; }
.danger:hover { background: #fbeaea; }

/* flashes */
.flashes { max-width: 980px; margin: 12px auto; padding: 0 16px; }
.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 8px; }
.flash-error { background: #fbe9e9; color: #9a2b2b; border: 1px solid #efc5c5; }
.flash-success { background: #e7f5ec; color: #1f6b3b; border: 1px solid #bfe3cc; }

/* auth */
.auth-wrap { display: flex; justify-content: center; padding: 60px 16px; }
.auth-card { width: 360px; }

/* projects */
.project-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 16px; }
.project-card { display: block; color: inherit; text-decoration: none; transition: box-shadow .15s; }
.project-card:hover { box-shadow: 0 4px 14px rgba(0,0,0,.08); }
.project-card h2 { margin: 0 0 6px; }
.project-card .slug { color: var(--accent); font-size: 12px; }

/* tables */
table.grid { width: 100%; border-collapse: collapse; margin: 8px 0 16px; }
table.grid th, table.grid td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); }
table.grid th { color: var(--muted); font-size: 12px; }
.row-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.row-actions input { width: 140px; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 14px; margin: 0; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; word-break: break-all; }
.checklist { list-style: none; padding: 0; columns: 2; }
.badge { background: #eef1f5; color: var(--muted); font-size: 11px; padding: 1px 6px; border-radius: 10px; }

/* ===== workspace 3-pane ===== */
.workspace-page, .workspace-page body { height: 100%; }
.workspace { padding: 0; margin: 0; }
.ws {
  display: flex; height: calc(100vh - var(--head-h)); overflow: hidden;
}
.pane { background: var(--panel); display: flex; flex-direction: column; overflow: hidden; }
.pane-left { width: 260px; border-right: 1px solid var(--border); }
.pane-right { width: 300px; border-left: 1px solid var(--border); }
.pane-center { flex: 1; min-width: 0; }
.splitter { width: 5px; cursor: col-resize; background: transparent; flex: 0 0 5px; }
.splitter:hover { background: var(--accent); opacity: .4; }

.pane-head {
  height: 40px; flex: 0 0 40px; display: flex; align-items: center; justify-content: space-between;
  padding: 0 10px; border-bottom: 1px solid var(--border); background: #fafbfc;
}
.pane-title { font-weight: 700; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pane-tools { display: flex; gap: 4px; align-items: center; }
.icon { padding: 4px 7px; font-size: 12px; }
.seg { padding: 4px 9px; font-size: 12px; }
.seg.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.icon.save { background: var(--accent); color: #fff; border-color: var(--accent); }

/* tree */
.tree { overflow: auto; flex: 1; padding: 6px 0; }
.tree-list { list-style: none; margin: 0; padding: 0; }
.tree-row {
  display: flex; align-items: center; gap: 6px; padding: 4px 8px; cursor: pointer;
  font-size: 13px; user-select: none; position: relative; touch-action: none;
}
.tree-row:hover { background: #eef3fb; }
.tree-row.active { background: #dde8fc; }
.tree-row.selected { background: #cfe0ff; box-shadow: inset 2px 0 0 var(--accent); }
.tree-row.drop-target { background: #c7f0d2; outline: 1px dashed #2f9e54; outline-offset: -2px; }
.tree-row .lbl { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.tree-row .ico { flex: 0 0 auto; }
.tree-row[draggable="true"] { cursor: grab; }
.tree-del { display: none; border: none; background: none; color: var(--danger); cursor: pointer; padding: 0 4px; }
.tree-row:hover .tree-del { display: inline; }
li.collapsed > .tree-list { display: none; }
.tree.drop-root { background: #eef7f0; outline: 2px dashed #2f9e54; outline-offset: -4px; }
body.dragging { cursor: grabbing; user-select: none; }
.drag-ghost {
  position: fixed; z-index: 9999; pointer-events: none;
  background: var(--accent); color: #fff; padding: 4px 10px; border-radius: 6px;
  font-size: 12px; box-shadow: 0 2px 10px rgba(0,0,0,.35); opacity: .92;
}
.ws-toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px);
  z-index: 10000; background: #1d2733; color: #fff; padding: 9px 16px; border-radius: 8px;
  font-size: 13px; box-shadow: 0 4px 16px rgba(0,0,0,.3);
  opacity: 0; pointer-events: none; transition: opacity .15s, transform .15s;
}
.ws-toast.show { opacity: .96; transform: translateX(-50%) translateY(0); }

/* center body */
.center-body { flex: 1; overflow: hidden; position: relative; }
.empty { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--muted); gap: 10px; text-align: center; }

.md-editor { height: 100%; display: flex; }
.md-editor.view-split .md-input,
.md-editor.view-split .md-preview { width: 50%; }
.md-editor.view-edit .md-input { width: 100%; }
.md-editor.view-edit .md-preview { display: none; }
.md-editor.view-preview .md-preview { width: 100%; }
.md-editor.view-preview .md-input { display: none; }

/* editor source: a textarea layered over a highlight backdrop so search
   matches can be shown inside the edit pane too (textareas can't hold HTML). */
.md-input { position: relative; height: 100%; border-right: 1px solid var(--border); overflow: hidden; }
.md-backdrop {
  position: absolute; inset: 0; overflow: hidden; pointer-events: none; background: #fff; z-index: 1;
}
.md-highlights, #md-source {
  margin: 0; border: 0; padding: 16px; width: 100%; height: 100%; box-sizing: border-box;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace; font-size: 13px;
  line-height: 1.6; letter-spacing: normal; tab-size: 4;
  white-space: pre-wrap; word-wrap: break-word; overflow-wrap: break-word;
}
.md-highlights { height: auto; color: transparent; }
.md-highlights mark { background: #ffe58a; color: transparent; border-radius: 2px; }
.md-highlights mark.hl-cur { background: #ff9f1c; }
#md-source {
  position: absolute; inset: 0; z-index: 2; resize: none; outline: none; overflow: auto;
  background: transparent; color: var(--text);
}
.md-preview { overflow: auto; padding: 16px 22px; height: 100%; }

/* front matter — compact card (Word-like detail tucked into a collapsible) */
.cover { border: 1px solid var(--border); border-left: 4px solid var(--accent); border-radius: 8px; padding: 8px 12px; margin: 0 0 14px; background: #f7f9fc; }
.cover-line { display: flex; flex-wrap: wrap; align-items: baseline; gap: 8px; }
.cover-name { font-size: 15px; font-weight: 700; }
.cover-id { font-size: 11px; color: #fff; background: var(--accent); border-radius: 10px; padding: 1px 8px; }
.cover-type { font-size: 12px; color: var(--muted); }
.cover-conf { font-size: 11.5px; color: var(--danger); font-weight: 700; margin-left: auto; }
.cover-edit { padding: 1px 8px; font-size: 11px; }
.cover-line .cover-edit { margin-left: 8px; }
.cover-conf + .cover-edit { margin-left: 8px; }
.cover-empty { display: flex; align-items: center; gap: 10px; }
.cover-empty .cover-edit { margin-left: auto; }
.cover-header { font-size: 11.5px; color: var(--muted); margin-top: 2px; }
.cover-more { margin-top: 6px; }
.cover-more > summary { cursor: pointer; color: var(--accent); font-size: 12px; }
.cover-more-body { margin-top: 8px; }
.cover table { border-collapse: collapse; width: 100%; font-size: 12px; margin: 4px 0; display: block; overflow-x: auto; }
.cover th, .cover td { border: 1px solid var(--border); padding: 3px 6px; text-align: left; white-space: nowrap; }
.cover-sign th { background: #eef1f5; }
.cover-sign tbody th { width: 56px; }
.cover-meta { display: flex; gap: 16px; justify-content: flex-end; color: var(--muted); font-size: 11.5px; margin: 2px 0 6px; }
.cover-h { font-weight: 700; font-size: 12.5px; margin: 8px 0 2px; }
.cover-footer { color: var(--muted); font-size: 11px; margin-top: 6px; }
.fm-details { margin-top: 6px; }
.fm-details summary { cursor: pointer; color: var(--accent); font-size: 11.5px; }
.fm-details pre { background: #fff; border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; font-size: 11px; overflow: auto; max-height: 200px; margin: 4px 0 0; }
/* fallback banner */
.fm-banner { border: 1px solid var(--border); border-left: 4px solid var(--accent); background: #f7f9fc; border-radius: 8px; padding: 8px 12px; margin: 0 0 14px; }
.fm-banner .fm-title { font-size: 15px; font-weight: 700; }
.fm-banner .fm-id { font-size: 11px; color: #fff; background: var(--accent); border-radius: 10px; padding: 1px 8px; margin-left: 6px; }
/* fallback banner */
.fm-banner { border: 1px solid var(--border); border-left: 4px solid var(--accent); background: #f7f9fc; border-radius: 8px; padding: 10px 14px; margin: 0 0 16px; }
.fm-banner .fm-title { font-size: 16px; font-weight: 700; }
.fm-banner .fm-id { font-size: 12px; color: #fff; background: var(--accent); border-radius: 10px; padding: 1px 8px; margin-left: 6px; }

.viewer { height: 100%; }

/* multi-selection editor (center pane) */
.selection-view { height: 100%; overflow: auto; padding: 12px 16px; }
.sel-bulk { background: #f7f9fc; border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; margin-bottom: 12px; }
.sel-bulk-row { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.sel-bulk-row .bl { font-weight: 700; width: 40px; flex: 0 0 auto; }
.sel-bulk-row label { font-weight: 400; margin: 0; display: inline-flex; align-items: center; gap: 4px; }
.sel-bulk-row input[type="date"], .sel-bulk-row input[type="time"] { width: auto; margin: 0; padding: 4px 6px; }
.sel-bulk-row .tmode { display: inline-flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.sel-bulk .bulk-note { font-size: 11px; margin: 4px 0 0; }

.sel-edit-wrap { overflow: auto; }
.sel-edit { width: 100%; border-collapse: collapse; font-size: 13px; }
.sel-edit th, .sel-edit td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.sel-edit th { color: var(--muted); font-size: 12px; font-weight: 600; position: sticky; top: 0; background: #fff; z-index: 1; }
.sel-edit .sel-name { max-width: 220px; overflow: hidden; text-overflow: ellipsis; }
.sel-edit input[type="date"], .sel-edit input[type="time"] { width: auto; margin: 0; padding: 3px 5px; font-size: 12.5px; }
.sel-edit .r-save { padding: 3px 10px; font-size: 12px; }
.sel-actions { margin: 14px 0; }

/* embedded full pdf.js viewer (its toolbar has search/print/download/outline) */
.pdf-frame { width: 100%; height: 100%; border: none; }

/* toolbar separator */
.tool-sep { display: inline-block; width: 1px; height: 18px; background: var(--border); margin: 0 4px; }

/* markdown outline (table of contents) overlay */
.outline-panel {
  position: absolute; top: 0; left: 0; bottom: 0; width: 240px; z-index: 5;
  background: #fff; border-right: 1px solid var(--border); box-shadow: 2px 0 8px rgba(0,0,0,.08);
  display: flex; flex-direction: column;
}
.outline-head { padding: 8px 12px; font-weight: 700; border-bottom: 1px solid var(--border); background: #fafbfc; }
.outline-list { list-style: none; margin: 0; padding: 6px 0; overflow: auto; }
.outline-list li { padding: 4px 12px; cursor: pointer; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.outline-list li:hover { background: #eef3fb; }
.outline-list .out-empty { cursor: default; }
.outline-list .out-lv1 { font-weight: 600; }
.outline-list .out-lv2 { padding-left: 24px; }
.outline-list .out-lv3 { padding-left: 36px; }
.outline-list .out-lv4 { padding-left: 48px; }
.outline-list .out-lv5, .outline-list .out-lv6 { padding-left: 60px; }

/* in-document find bar */
.find-bar {
  position: absolute; top: 8px; right: 16px; z-index: 6; display: flex; align-items: center; gap: 4px;
  background: #fff; border: 1px solid var(--border); border-radius: 8px; padding: 4px 6px;
  box-shadow: 0 2px 10px rgba(0,0,0,.12);
}
.find-bar input { width: 160px; padding: 5px 8px; margin: 0; }
.find-bar .find-count { font-size: 12px; color: var(--muted); min-width: 44px; text-align: center; }
.find-bar .icon { padding: 3px 7px; }
mark.hl { background: #ffe58a; color: inherit; padding: 0 1px; border-radius: 2px; }
mark.hl-cur { background: #ff9f1c; color: #fff; }

.details { padding: 14px; overflow: auto; }
.details .kv dd { display: flex; gap: 6px; align-items: center; }
.dt-input {
  flex: 1; min-width: 0; padding: 4px 6px; margin: 0; font-size: 12.5px;
  font-family: "SFMono-Regular", Consolas, monospace;
  border: 1px solid var(--border); border-radius: 5px;
}
.dt-save { flex: 0 0 auto; padding: 4px 10px; font-size: 12px; }
.detail-actions { margin-top: 12px; display: flex; gap: 8px; flex-wrap: wrap; }

/* version history (right panel) */
.version-history { margin-top: 16px; border-top: 1px solid var(--border); padding-top: 10px; }
.vh-head { font-weight: 700; font-size: 12.5px; margin-bottom: 6px; display: flex; align-items: center; justify-content: space-between; gap: 6px; flex-wrap: wrap; }
.vh-tools { display: flex; align-items: center; gap: 8px; font-weight: 400; }
.vh-tools label { display: inline-flex; align-items: center; gap: 3px; font-size: 11px; margin: 0; }
.vh-tools .vh-chk, .vh-tools input { width: auto; }
.vh-tools .btn { padding: 2px 8px; font-size: 11px; }
.vh-empty { font-size: 11px; margin: 0; }
.vh-list { list-style: none; margin: 0; padding: 0; max-height: 220px; overflow: auto; }
.vh-list li { font-size: 12px; padding: 3px 0; border-bottom: 1px dotted var(--border); display: flex; align-items: center; gap: 6px; }
.vh-list .vh-chk { width: auto; flex: 0 0 auto; margin: 0; }
.vh-list a { font-family: "SFMono-Regular", Consolas, monospace; }
.vh-cur { font-size: 10px; color: #fff; background: #2f9e54; border-radius: 8px; padding: 0 6px; }

/* version viewer modal */
#ver-modal { width: 820px; max-width: 92vw; max-height: 88vh; padding: 0; }
#ver-modal[open] { display: flex; flex-direction: column; }
.ver-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: #fff; }
.ver-head h3 { margin: 0; font-size: 15px; }
.ver-actions { display: flex; gap: 6px; }
.ver-body { overflow: auto; padding: 16px 22px; }

/* markdown body */
.markdown-body { line-height: 1.65; }
.markdown-body h1, .markdown-body h2 { border-bottom: 1px solid var(--border); padding-bottom: .25em; }
.markdown-body pre { background: #1d2733; color: #e6edf3; padding: 12px; border-radius: 8px; overflow: auto; }
.markdown-body pre code { background: none; color: inherit; padding: 0; }
.markdown-body code { background: #eef1f5; }
.markdown-body blockquote { border-left: 4px solid var(--border); margin: 0; padding-left: 14px; color: var(--muted); }
.markdown-body table { border-collapse: collapse; }
.markdown-body th, .markdown-body td { border: 1px solid var(--border); padding: 6px 10px; }
.markdown-body img { max-width: 100%; }
.markdown-body .page-break { text-align: center; color: var(--muted); font-size: 11px; letter-spacing: 2px; border-top: 2px dashed var(--border); border-bottom: 2px dashed var(--border); padding: 3px 0; margin: 18px 0; user-select: none; }

/* modal */
dialog { border: 1px solid var(--border); border-radius: 10px; padding: 18px; min-width: 320px; }
dialog::backdrop { background: rgba(0,0,0,.35); }
dialog menu { display: flex; justify-content: flex-end; gap: 8px; padding: 0; margin: 14px 0 0; }

/* metadata editor modal */
#meta-modal { min-width: 640px; max-width: 90vw; max-height: 88vh; overflow: auto; }
#meta-modal h3 { margin: 0 0 10px; }
#meta-modal h4 { margin: 14px 0 6px; font-size: 13px; }
.meta-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 14px; }
.meta-grid label { margin: 0; font-size: 12px; font-weight: 600; }
.meta-grid label input { margin-top: 3px; }
.meta-grid .meta-wide { grid-column: 1 / -1; }
.meta-sign, .meta-rev { width: 100%; border-collapse: collapse; font-size: 12px; }
.meta-sign th, .meta-sign td, .meta-rev th, .meta-rev td { border: 1px solid var(--border); padding: 3px 5px; }
.meta-sign th { background: #f1f4f9; }
.meta-rev th { background: #f1f4f9; }
.meta-sign input, .meta-rev input { width: 100%; padding: 3px 5px; margin: 0; font-size: 12px; }
.meta-rev .rev-del { padding: 1px 8px; }
.meta-errors { background: #fbe9e9; color: #9a2b2b; border: 1px solid #efc5c5; border-radius: 6px; padding: 8px 12px; margin: 0 0 10px; font-size: 12px; }
.meta-errors ul { margin: 4px 0 0; padding-left: 18px; }
/* required-field error highlight (red) */
.field-error { color: var(--danger) !important; }
input.field-error, .meta-grid label.field-error input,
.meta-rev.field-error input { border-color: var(--danger) !important; background: #fdf2f2; }
.meta-rev.field-error { outline: 1px solid var(--danger); }
.meta-grid label.req::after { content: ""; }
