* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.55 var(--font-body);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { font-family: var(--font-display); font-weight: 600; line-height: 1.2; letter-spacing: .01em; }
h1 { font-size: 2.1rem; margin: .2em 0 .4em; }
h2 { font-size: 1.5rem; margin: 1.4em 0 .5em; }
h3 { font-size: 1.2rem; margin: 1.2em 0 .4em; }
mark { background: var(--mark); color: inherit; border-radius: 2px; padding: 0 1px; }
.muted { color: var(--text-muted); }
.small { font-size: .85rem; }

/* ---------- Topbar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; flex-direction: column; line-height: 1.1; color: var(--text); white-space: nowrap; }
.brand:hover { text-decoration: none; }
.brand-name { font-family: var(--font-display); font-size: 1.35rem; font-weight: 600; }
.brand-sub { font-size: .72rem; letter-spacing: .14em; text-transform: uppercase; color: var(--text-muted); }
.top-search { flex: 1; max-width: 640px; }
.top-search input, .input, select, textarea {
  width: 100%; padding: 9px 12px; font: inherit; color: var(--text);
  background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius);
}
.top-search input:focus, .input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; background: var(--surface); }
.top-actions { display: flex; gap: 8px; align-items: center; margin-left: auto; }
.btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border-radius: var(--radius);
  background: var(--accent); color: var(--accent-contrast); border: 1px solid var(--accent);
  font: 500 .9rem var(--font-body); cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--accent-strong); text-decoration: none; }
.btn-ghost { background: transparent; color: var(--accent); }
.btn-ghost:hover { background: var(--accent-soft); }
.btn-danger { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn-sm { padding: 5px 10px; font-size: .82rem; }
.icon-btn { background: none; border: 1px solid transparent; border-radius: var(--radius); font-size: 1.1rem; padding: 6px 9px; cursor: pointer; color: var(--text); }
.icon-btn:hover { background: var(--surface-2); }
.menu-btn { display: none; }

/* ---------- Layout ---------- */
.layout { display: grid; grid-template-columns: 270px 1fr; min-height: calc(100vh - 62px); }
.sidebar { border-right: 1px solid var(--line); padding: 18px 10px 40px; position: sticky; top: 62px; height: calc(100vh - 62px); overflow-y: auto; background: var(--surface); }
.main { padding: 28px 40px 80px; max-width: 1180px; width: 100%; outline: none; }

.nav-area { margin-bottom: 2px; }
.nav-area > a, .nav-sub a {
  display: flex; justify-content: space-between; gap: 8px; padding: 6px 10px; border-radius: 8px; color: var(--text);
}
.nav-area > a { font-weight: 500; }
.nav-area > a:hover, .nav-sub a:hover { background: var(--surface-2); text-decoration: none; }
.nav-area > a.active, .nav-sub a.active { background: var(--accent-soft); color: var(--accent-strong); }
.nav-sub { margin: 2px 0 8px 12px; padding-left: 8px; border-left: 1px solid var(--line); font-size: .88rem; display: none; }
.nav-area.open .nav-sub { display: block; }
.nav-count { color: var(--text-muted); font-size: .78rem; font-weight: 400; }
.nav-heading { font-size: .72rem; letter-spacing: .14em; text-transform: uppercase; color: var(--text-muted); margin: 18px 10px 6px; }

/* ---------- Components ---------- */
.crumbs { font-size: .85rem; color: var(--text-muted); display: flex; flex-wrap: wrap; gap: 4px; }
.crumbs a { color: var(--text-muted); }
.crumbs span.sep { opacity: .5; }
.badge { display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: .74rem; font-weight: 500; background: var(--surface-2); color: var(--text-muted); border: 1px solid var(--line); white-space: nowrap; }
.badge.type { background: var(--accent-soft); color: var(--accent-strong); border-color: transparent; }
.badge.note { background: color-mix(in srgb, var(--highlight) 15%, transparent); color: var(--highlight); border-color: transparent; }
.badge.archived { border-style: dashed; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { padding: 5px 12px; border-radius: 99px; border: 1px solid var(--line); background: var(--surface); color: var(--text); cursor: pointer; font: inherit; font-size: .85rem; }
.chip:hover { border-color: var(--accent); text-decoration: none; }
.chip.on { background: var(--accent); color: var(--accent-contrast); border-color: var(--accent); }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); }
.area-card { padding: 18px 18px 14px; color: var(--text); display: block; }
.area-card:hover { border-color: var(--accent); text-decoration: none; }
.area-card h3 { margin: 0 0 4px; font-size: 1.35rem; }
.area-card .num { font-family: var(--font-display); color: var(--highlight); font-size: 1rem; }
.section-title { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }

.hero { padding: 26px 0 18px; }
.hero h1 { font-size: 2.6rem; margin-bottom: .1em; }
.hero-search { display: flex; gap: 8px; margin: 18px 0 14px; max-width: 720px; }
.hero-search input { font-size: 1.1rem; padding: 13px 16px; }

.list { list-style: none; margin: 0; padding: 0; }
.list li { border-bottom: 1px solid var(--line); }
.list li:last-child { border-bottom: none; }
.row { display: flex; gap: 12px; align-items: flex-start; padding: 12px 14px; color: var(--text); }
a.row:hover { background: var(--surface-2); text-decoration: none; }
.row-icon { width: 34px; height: 34px; flex: none; border-radius: 8px; background: var(--surface-2); display: grid; place-items: center; font-size: 1rem; overflow: hidden; }
.row-icon img { width: 100%; height: 100%; object-fit: cover; }
.row-main { flex: 1; min-width: 0; }
.row-title { font-weight: 500; }
.row-meta { font-size: .8rem; color: var(--text-muted); display: flex; flex-wrap: wrap; gap: 6px 10px; align-items: center; margin-top: 2px; }
.snip { font-size: .88rem; color: var(--text-muted); margin-top: 4px; overflow-wrap: anywhere; }

.gallery { display: grid; gap: 8px; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
.gallery a { position: relative; display: block; aspect-ratio: 1; border-radius: 8px; overflow: hidden; background: var(--surface-2); }
.gallery img { width: 100%; height: 100%; object-fit: cover; transition: transform .2s; }
.gallery a:hover img { transform: scale(1.03); }
.gallery .cap { position: absolute; inset: auto 0 0 0; padding: 14px 8px 6px; font-size: .72rem; color: #fff; background: linear-gradient(transparent, rgba(0,0,0,.65)); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.gallery .play { position: absolute; inset: 0; display: grid; place-items: center; font-size: 2rem; color: #fff; text-shadow: 0 1px 6px #000; }
.gallery a.current { outline: 3px solid var(--accent); }

/* ---------- Entry ---------- */
.entry-head { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 14px; align-items: flex-start; margin-bottom: 8px; }
.entry-head h1 { margin: 6px 0 8px; }
.toolbar { display: flex; flex-wrap: wrap; gap: 6px; }
.source-box { display: flex; gap: 12px; align-items: center; padding: 10px 14px; margin: 12px 0 22px; font-size: .88rem; }
.source-box .grow { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.entry-grid { display: grid; grid-template-columns: minmax(0, 1fr) 240px; gap: 32px; }
.toc { position: sticky; top: 84px; align-self: start; font-size: .86rem; max-height: calc(100vh - 110px); overflow-y: auto; }
.toc a { display: block; padding: 4px 10px; border-left: 2px solid var(--line); color: var(--text-muted); }
.toc a.current { border-color: var(--accent); color: var(--accent-strong); font-weight: 500; }
.prose { overflow-wrap: anywhere; }
.prose img { max-width: 100%; height: auto; border-radius: 8px; }
.prose table { border-collapse: collapse; margin: 1em 0; font-size: .9rem; display: block; overflow-x: auto; }
.prose td, .prose th { border: 1px solid var(--line); padding: 6px 9px; vertical-align: top; }
.prose th { background: var(--surface-2); text-align: left; }
.prose ul, .prose ol { padding-left: 1.4em; }
.prose li { margin: .2em 0; }
.prose input[type=checkbox] { accent-color: var(--accent); margin-right: 6px; transform: scale(1.1); }
.prose li:has(> input[type=checkbox]) { list-style: none; margin-left: -1.3em; }
.prose blockquote { border-left: 3px solid var(--accent); margin: 1em 0; padding: .2em 1em; color: var(--text-muted); }
.pdf-page { margin-bottom: 1.4em; }
.pdf-page p { margin: .15em 0; }
.pdf-page-no { font-size: .74rem; letter-spacing: .12em; text-transform: uppercase; color: var(--text-muted); border-bottom: 1px solid var(--line); padding-bottom: 4px; margin-bottom: 8px; }
.empty { padding: 40px; text-align: center; color: var(--text-muted); }

/* Tables (inventories) */
.table-tools { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 10px; }
.table-tools .input { max-width: 320px; }
.data-wrap { overflow: auto; max-height: 72vh; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
table.data { border-collapse: separate; border-spacing: 0; width: 100%; font-size: .86rem; }
table.data th { position: sticky; top: 0; background: var(--surface-2); text-align: left; font-weight: 600; cursor: pointer; user-select: none; white-space: nowrap; }
table.data th, table.data td { padding: 6px 10px; border-bottom: 1px solid var(--line); border-right: 1px solid var(--line); vertical-align: top; }
table.data tr:hover td { background: color-mix(in srgb, var(--accent-soft) 45%, transparent); }
table.data td[contenteditable=true] { background: var(--bg); outline: none; }
table.data td[contenteditable=true]:focus { box-shadow: inset 0 0 0 2px var(--accent); }
table.data th.sorted::after { content: " ▾"; } table.data th.sorted.desc::after { content: " ▴"; }

/* Media */
.media-view { text-align: center; background: var(--surface-2); border-radius: var(--radius); padding: 12px; margin-bottom: 18px; }
.media-view img, .media-view video { max-width: 100%; max-height: 72vh; border-radius: 6px; }
.pdf-frame { width: 100%; height: 78vh; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 16px; }
.tabs button { background: none; border: none; padding: 9px 14px; font: inherit; color: var(--text-muted); cursor: pointer; border-bottom: 2px solid transparent; }
.tabs button.on { color: var(--accent-strong); border-color: var(--accent); font-weight: 500; }

/* ---------- Forms ---------- */
.form { display: grid; gap: 14px; }
.form label { display: grid; gap: 5px; font-size: .85rem; font-weight: 500; color: var(--text-muted); }
.form .row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.editor { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.editor textarea { min-height: 460px; font: .9rem/1.55 ui-monospace, SFMono-Regular, Menlo, monospace; resize: vertical; }
.editor .preview { padding: 12px 16px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); max-height: 640px; overflow: auto; }
.md-tools { display: flex; flex-wrap: wrap; gap: 4px; }
.md-tools button { font: 500 .8rem var(--font-body); padding: 4px 9px; border: 1px solid var(--line); background: var(--surface); color: var(--text); border-radius: 6px; cursor: pointer; }
.md-tools button:hover { border-color: var(--accent); }
.dropzone { border: 2px dashed var(--line); border-radius: var(--radius); padding: 40px 20px; text-align: center; background: var(--surface); cursor: pointer; transition: .15s; }
.dropzone.over { border-color: var(--accent); background: var(--accent-soft); }
.hint { font-size: .82rem; color: var(--text-muted); }
.checkbox { display: flex !important; grid-template-columns: none; flex-direction: row; align-items: center; gap: 8px; font-weight: 400 !important; color: var(--text) !important; }

.toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 99px; font-size: .9rem; z-index: 50; box-shadow: var(--shadow); }

.version { padding: 14px; margin-bottom: 12px; }
.version summary { cursor: pointer; }

@media print {
  .topbar, .sidebar, .toolbar, .toc, .source-box .btn, .tabs { display: none !important; }
  .layout { display: block; } .main { padding: 0; }
  .entry-grid { display: block; }
}

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .menu-btn { display: inline-block; }
  .sidebar { position: fixed; top: 62px; left: 0; width: 290px; z-index: 30; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  body.nav-open .sidebar { transform: none; }
  .main { padding: 20px 16px 60px; }
  .entry-grid, .editor, .form .row2 { grid-template-columns: 1fr; }
  .toc { position: static; max-height: none; }
  .brand-sub, .top-actions .btn-ghost { display: none; }
  .topbar { gap: 8px; padding: 10px 12px; flex-wrap: wrap; }
  .top-search { order: 5; flex-basis: 100%; max-width: none; }
  .hero h1 { font-size: 2rem; }
}
table.data td { min-width: 90px; max-width: 360px; }
table.data td:first-child { min-width: 200px; }
.main, .layout > * { min-width: 0; }
html, body { overflow-x: hidden; }
@media (max-width: 900px) {
  .source-box { flex-wrap: wrap; }
  .source-box .grow { flex-basis: calc(100% - 50px); }
  .brand-name { font-size: 1.1rem; }
  .top-actions { gap: 4px; }
  .top-actions .btn { padding: 6px 10px; }
  .entry-head h1, .hero h1 { font-size: 1.7rem; overflow-wrap: anywhere; }
  .data-wrap { max-height: 65vh; }
}
.entry-head > div:first-child { min-width: 0; flex: 1 1 320px; }
.conflict-box { padding: 12px 14px; margin: -10px 0 22px; border-color: var(--highlight); background: color-mix(in srgb, var(--highlight) 8%, var(--surface)); }

/* user menu */
.user-menu-wrap { position: relative; }
.user-menu[hidden] { display: none; }
.user-menu { position: absolute; right: 0; top: calc(100% + 6px); min-width: 200px; padding: 6px; display: grid; z-index: 40; }
.user-menu a { padding: 8px 12px; border-radius: 8px; color: var(--text); }
.user-menu a:hover { background: var(--surface-2); text-decoration: none; }

/* change log */
.log-row > summary { list-style: none; cursor: pointer; }
.log-row > summary::-webkit-details-marker { display: none; }
.log-row[data-empty] > summary { cursor: default; }
.log-details { padding: 0 14px 14px; display: grid; gap: 8px; }
.diff { font: .82rem/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; border: 1px solid var(--line); border-radius: 8px; overflow: auto; max-height: 360px; background: var(--bg); }
.diff > div { padding: 1px 10px; white-space: pre-wrap; overflow-wrap: anywhere; }
.diff .add, table.data td.add { background: color-mix(in srgb, #3c8d40 16%, transparent); }
.diff .del, table.data td.del { background: color-mix(in srgb, var(--danger) 14%, transparent); }
.diff .same { color: var(--text-muted); font-style: italic; }

/* original viewer */
.viewer .docx-host { overflow: auto; border-radius: var(--radius); background: var(--surface-2); }
.viewer .docx-host .docx-wrapper { background: var(--surface-2); padding: 20px; }
.viewer .docx-host .docx-wrapper > section.docx { box-shadow: var(--shadow); margin-bottom: 20px; }
.sheet-view table { border-collapse: collapse; font-size: .85rem; }
.sheet-view td { border: 1px solid var(--line); padding: 4px 8px; white-space: nowrap; background: var(--surface); }
@media (max-width: 900px) { #userBtn { padding: 6px 8px; } }
#userBtn .caret { font-size: 1em; margin-left: 4px; line-height: 1; }

/* folder picker */
.field-label { font-size: .85rem; font-weight: 500; color: var(--text-muted); margin-bottom: 5px; }
.fpick { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.fpick-head { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between; padding: 10px 12px; border-bottom: 1px solid var(--line); }
.fpick-head .fpick-q { max-width: 240px; padding: 6px 10px; }
.fpick-tree { max-height: 340px; overflow-y: auto; padding: 6px 4px; }
.ftree { list-style: none; margin: 0; padding: 0; }
.ftree .ftree { padding-left: 20px; display: none; }
.ftree li.open > .ftree { display: block; }
.fnode { display: flex; align-items: center; gap: 2px; border-radius: 7px; }
.fnode:hover { background: var(--surface-2); }
.fnode.sel { background: var(--accent-soft); }
.fnode.sel .fname { color: var(--accent-strong); font-weight: 600; }
.fnode.arch .fname { color: var(--text-muted); }
.ftog { width: 24px; height: 28px; flex: none; border: 0; background: none; color: var(--text-muted); cursor: pointer; font-size: .8rem; transition: transform .15s; display: inline-grid; place-items: center; }
li.open > .fnode .ftog { transform: rotate(90deg); }
.fname { flex: 1; text-align: left; border: 0; background: none; font: inherit; color: var(--text); padding: 5px 6px; cursor: pointer; }
li.match > .fnode .fname { text-decoration: underline; text-decoration-color: var(--highlight); }

/* select mode (Manager) */
.select-bar { position: sticky; top: 72px; z-index: 15; display: flex; flex-wrap: wrap; gap: 10px; align-items: center; padding: 10px 14px; margin-bottom: 16px; border-color: var(--accent); }
.select-bar[hidden] { display: none; }
.selecting a[href^="#/doc/"] { position: relative; }
.selecting a[href^="#/doc/"]::after { content: ""; position: absolute; top: 8px; right: 8px; width: 20px; height: 20px; border-radius: 6px; border: 2px solid #fff; background: rgba(0,0,0,.25); box-shadow: 0 0 0 1px rgba(0,0,0,.3); }
.selecting a.picked::after { content: "✓"; background: var(--accent); color: var(--accent-contrast); display: grid; place-items: center; font-size: 13px; font-weight: 700; }
.selecting .gallery a.picked img { opacity: .7; }
.selecting .list a.picked { background: var(--accent-soft); }
