/* 資料ライブラリ - スタイル
 * 白地・細い罫線・アクセント 1 色。カードは薄い枠だけで、操作はホバー時に出す。
 * カテゴリ色（--cat）は小さなドットとカードの右上角の三角にだけ使う。
 * インライン style 属性は使わない（CSP style-src 'self'）。色は JS から
 * element.style.setProperty('--cat', …) で渡す（CSSOM は CSP 対象外）。
 */
:root {
  --bg: #ffffff;
  --bg-soft: #f6f7f9;
  --line: #e5e7eb;
  --line-strong: #d1d5db;
  --text: #111827;
  --muted: #6b7280;
  --faint: #9ca3af;
  --accent: #2563eb;
  --accent-soft: #eff4ff;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --star: #f59e0b;
  --warn-soft: #fffbeb;
  --warn-text: #92400e;
  --cat: #9ca3af;
  --r: 6px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", "Hiragino Sans", "Noto Sans JP", Meiryo, system-ui, sans-serif;
  font-size: 14px; line-height: 1.55;
  color: var(--text); background: var(--bg);
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button { font: inherit; cursor: pointer; color: inherit; }
input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

/* ---------- header ---------- */
.topbar {
  display: flex; align-items: center; gap: 16px;
  height: 52px; padding: 0 20px;
  background: var(--bg); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.brand { color: var(--text); font-weight: 700; font-size: 15px; white-space: nowrap; }
.brand:hover { text-decoration: none; }
.search {
  flex: 1; max-width: 460px; height: 34px; padding: 0 12px;
  border: 1px solid var(--line-strong); border-radius: var(--r); background: var(--bg);
}
.search:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.spacer { flex: 1; }
.user { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 250px; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  height: 32px; padding: 0 12px; border-radius: var(--r);
  border: 1px solid var(--line-strong); background: var(--bg); color: var(--text);
  white-space: nowrap;
}
.btn:hover { background: var(--bg-soft); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-accent { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-accent:hover { background: #1d4ed8; }
.btn-ghost { border-color: transparent; }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); border-color: #fca5a5; }
.btn-sm { height: 28px; padding: 0 10px; font-size: 13px; }
.select { height: 30px; border: 1px solid var(--line-strong); border-radius: var(--r); padding: 0 8px; background: var(--bg); font-size: 13px; }

/* ---------- layout ---------- */
.layout { display: flex; height: calc(100vh - 52px); }
.sidebar {
  width: var(--sidebar-w, 244px); flex: none; padding: 16px 10px 24px; overflow-y: auto;
  border-right: 1px solid var(--line);
}
/* 幅を手で変えるためのつまみ。見た目は罫線に溶け込ませ、当たり判定だけ広く取る */
.sidebar-resizer {
  flex: none; width: 7px; margin-left: -4px; margin-right: -3px; z-index: 5;
  cursor: col-resize; background: transparent; border: none; padding: 0;
}
.sidebar-resizer:hover, .sidebar-resizer:focus-visible, body.resizing .sidebar-resizer {
  background: var(--accent-soft); outline: none;
}
body.resizing { cursor: col-resize; user-select: none; }
.main { flex: 1; min-width: 0; padding: 20px 28px 48px; overflow-y: auto; }
.main.doc-mode { padding: 0; display: flex; flex-direction: column; overflow: hidden; }
.doc-view { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.doc-view .viewer-frame-wrap { flex: 1; min-height: 0; }
.doc-view .details { overflow-y: auto; max-height: 40%; }

/* 領域ごとのブロック（全公開 / 自分のみ） */
.nav-block + .nav-block { margin-top: 22px; padding-top: 18px; border-top: 1px solid var(--line); }
.nav-head { display: flex; align-items: center; gap: 2px; margin-bottom: 4px; }
.nav-area {
  flex: 1; display: flex; align-items: center; gap: 6px; min-width: 0;
  padding: 6px 10px; border: none; background: none; border-radius: var(--r);
  text-align: left; color: var(--text); font-size: 13.5px; font-weight: 700;
}
.nav-area:hover { background: var(--bg-soft); }
.nav-area.active { background: var(--accent-soft); color: var(--accent); }
.nav-area .label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-area .count { color: var(--faint); font-size: 12px; font-weight: 400; font-variant-numeric: tabular-nums; }
/* カテゴリツリーと特別なビューの区切り */
.nav-views { margin-top: 8px; padding-top: 8px; border-top: 1px dashed var(--line); }
.nav-title-btn { border: none; background: none; color: var(--muted); font-size: 14px; line-height: 1; padding: 4px 6px; border-radius: 4px; }
.nav-title-btn:hover { background: var(--bg-soft); color: var(--text); }
.nav-item {
  display: flex; align-items: center; gap: 6px;
  width: 100%; padding: 6px 10px; border-radius: var(--r); border: none; background: none;
  text-align: left; color: var(--text); font-size: 13.5px;
}
.nav-item:hover { background: var(--bg-soft); text-decoration: none; }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-item .label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item .count { color: var(--faint); font-size: 12px; font-variant-numeric: tabular-nums; }
.nav-item.cat .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--cat); flex: none; }
.nav-item.cat.depth-1 { padding-left: 20px; }
.nav-item.cat.depth-2 { padding-left: 32px; }
.nav-item.cat.depth-1 .dot { width: 6px; height: 6px; }
.nav-item.cat.depth-2 .dot { width: 5px; height: 5px; }
.caret, .caret-space { width: 16px; height: 16px; flex: none; }
.caret {
  display: inline-flex; align-items: center; justify-content: center;
  border: none; background: none; color: var(--muted); font-size: 11px; line-height: 1;
  padding: 0; border-radius: 3px; transform: rotate(90deg); transition: transform .12s ease;
}
.caret.closed { transform: rotate(0deg); }
.caret:hover { color: var(--text); background: var(--line); }
/* 資料をドラッグ中はドロップ先を示す */
body.doc-dragging .nav-item.cat { outline: 1px dashed var(--line-strong); outline-offset: -1px; }
.nav-item.drop-target { background: var(--accent-soft); outline: 2px solid var(--accent) !important; outline-offset: -2px; }

/* ---------- toolbar / chips ---------- */
.toolbar { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 10px; }
.toolbar h1 { font-size: 18px; font-weight: 700; margin: 0; }
.view-sub { color: var(--muted); font-size: 13px; }
.notice {
  margin: 0 0 12px; padding: 8px 12px; border-radius: var(--r);
  background: var(--warn-soft); color: var(--warn-text); font-size: 12.5px;
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
#tagChips { margin-bottom: 14px; }
.chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 9px; border-radius: 999px; font-size: 12px;
  border: 1px solid var(--line); color: var(--muted); background: var(--bg);
}
a.chip:hover { border-color: var(--line-strong); color: var(--text); text-decoration: none; }
.chip.active { background: var(--text); border-color: var(--text); color: #fff; }
.chip.muted { border-style: dashed; }
.chip .count { color: var(--faint); }
.chip.active .count { color: rgba(255,255,255,.7); }
.badge { display: inline-flex; align-items: center; gap: 5px; color: var(--muted); font-size: 12px; }
.badge.cat::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--cat); flex: none; }
a.badge.cat:hover { color: var(--text); text-decoration: none; }
.type-badge {
  display: inline-block; font-size: 11px; font-weight: 600; letter-spacing: .04em;
  color: var(--muted); border: 1px solid var(--line-strong); border-radius: 4px; padding: 0 5px; line-height: 16px;
}
.type-badge.md { color: #0f766e; border-color: #99d5cf; }
.type-badge.link { color: #7c3aed; border-color: #cbb8f7; }
.area-badge {
  display: inline-block; font-size: 11px; border-radius: 4px; padding: 0 6px; line-height: 17px;
  background: var(--bg-soft); color: var(--muted); border: 1px solid var(--line);
}
.area-badge.public { background: var(--accent-soft); color: var(--accent); border-color: #bfd4fe; }
.area-badge.private { background: #f5f3ff; color: #6d28d9; border-color: #ddd6fe; }
.area-badge.archived { background: var(--danger-soft); color: var(--danger); border-color: #fecaca; }
.views { color: var(--muted); font-size: 12.5px; white-space: nowrap; }

/* ---------- cards ---------- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(272px, 1fr)); gap: 12px; }
/* カテゴリの入れ子をそのまま反映したセクション */
.sections { display: block; }
.sections > .grid + .sec { margin-top: 26px; }
.sec { margin-top: 22px; }
.sec:first-child { margin-top: 0; }
.sec-d0 { margin-left: 0; }
.sec-d1 { margin-left: 16px; }
.sec-d2 { margin-left: 32px; }
.sec-head { display: flex; align-items: baseline; gap: 7px; margin-bottom: 8px; }
.sec-head .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--cat); flex: none; align-self: center; }
.sec-name { font-weight: 600; font-size: 13.5px; color: var(--text); }
a.sec-name:hover { color: var(--accent); text-decoration: none; }
.sec-count { color: var(--faint); font-size: 12px; font-variant-numeric: tabular-nums; }
.card {
  position: relative; display: flex; flex-direction: column; gap: 7px; overflow: hidden;
  padding: 14px 16px 12px; background: var(--bg);
  border: 1px solid var(--line); border-radius: var(--r);
  cursor: pointer; transition: border-color .12s ease, box-shadow .12s ease;
}
/* カテゴリ色は右上角の小さな三角で示す（overflow:hidden で角丸に沿って切られる） */
.card::before {
  content: ""; position: absolute; top: 0; right: 0; width: 0; height: 0;
  border-top: 14px solid var(--cat); border-left: 14px solid transparent;
  pointer-events: none;
}
.card:hover { border-color: var(--line-strong); box-shadow: 0 1px 3px rgba(17,24,39,.06); }
.card.dragging { opacity: .45; }
.card-head { display: flex; align-items: flex-start; gap: 6px; }
.card-title { flex: 1; margin: 0; font-weight: 600; font-size: 14.5px; line-height: 1.45; }
.card-title a { color: var(--text); display: block; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.card-title a:hover { color: var(--accent); text-decoration: none; }
.card-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.card-desc {
  margin: 0; color: var(--muted); font-size: 12.5px;
  overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
}
.card-desc:empty { display: none; }
.card-tags { margin-top: -1px; }
.card-foot {
  display: flex; align-items: center; gap: 10px; margin-top: auto; padding-top: 4px;
  color: var(--faint); font-size: 12px; white-space: nowrap;
}
.card-foot .who { overflow: hidden; text-overflow: ellipsis; max-width: 40%; }
.card-foot .date { margin-left: auto; font-variant-numeric: tabular-nums; }
/* 操作はホバー時にフッターへ重ねて出す（高さを予約せず、レイアウトも動かさない） */
.card-actions {
  position: absolute; left: 0; right: 0; bottom: 0; display: none; gap: 4px;
  padding: 7px 14px; background: var(--bg); border-top: 1px solid var(--line);
}
.card:hover .card-actions, .card:focus-within .card-actions { display: flex; }
.card-actions .btn { height: 26px; padding: 0 8px; font-size: 12px; border-color: transparent; color: var(--muted); }
.card-actions .btn:hover { border-color: var(--line-strong); color: var(--text); background: var(--bg); }
.star { border: none; background: none; font-size: 17px; color: #d1d5db; padding: 0; line-height: 1; width: 22px; }
.star:hover { color: var(--star); }
.star.on { color: var(--star); }
.empty { color: var(--muted); text-align: center; padding: 56px 20px; }

/* ---------- ranking ---------- */
.ranking { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--line); max-width: 860px; }
.rank-row {
  display: grid; grid-template-columns: 40px 1fr 90px; align-items: center; gap: 12px;
  padding: 12px 8px; border-bottom: 1px solid var(--line); cursor: pointer;
}
.rank-row:hover { background: var(--bg-soft); }
.rank-no { font-size: 18px; font-weight: 600; color: var(--faint); text-align: right; font-variant-numeric: tabular-nums; }
.rank-row.top .rank-no { color: var(--text); }
.rank-main { min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.rank-title { display: flex; align-items: center; gap: 8px; min-width: 0; }
.rank-title a { color: var(--text); font-weight: 600; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-sub { display: flex; gap: 12px; align-items: center; color: var(--muted); font-size: 12.5px; }
.rank-track { height: 4px; border-radius: 2px; background: var(--line); overflow: hidden; margin-top: 3px; }
.rank-bar { display: block; height: 100%; width: 0; border-radius: 2px; background: var(--cat); transition: width .5s ease; }
.rank-count { color: var(--muted); font-size: 12.5px; white-space: nowrap; text-align: right; }
.rank-count b { font-size: 16px; color: var(--text); font-variant-numeric: tabular-nums; font-weight: 600; }

/* ---------- modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(17, 24, 39, .45);
  display: flex; align-items: center; justify-content: center; z-index: 50; padding: 16px;
}
.modal {
  width: 100%; max-width: 540px; max-height: 90vh; overflow: auto;
  background: var(--bg); border-radius: 10px; box-shadow: 0 20px 50px rgba(0,0,0,.25);
  padding: 20px 22px;
}
.modal h2 { margin: 0 0 14px; font-size: 16px; font-weight: 700; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field label { font-size: 13px; color: var(--muted); }
.field input[type="text"], .field textarea, .field select {
  padding: 8px 10px; border: 1px solid var(--line-strong); border-radius: var(--r); background: var(--bg);
}
.field select:disabled { background: var(--bg-soft); color: var(--muted); }
.field input:focus, .field textarea:focus, .field select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.field textarea { min-height: 72px; resize: vertical; }
.hint { font-size: 12px; color: var(--muted); }
.modal-actions { display: flex; justify-content: flex-end; align-items: center; gap: 8px; margin-top: 16px; }
.dropzone {
  border: 1px dashed var(--line-strong); border-radius: var(--r); padding: 20px; text-align: center;
  color: var(--muted); background: var(--bg-soft); cursor: pointer;
}
.dropzone.over { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.dropzone .file-name { color: var(--text); font-weight: 600; margin-top: 6px; word-break: break-all; }
.error-text { color: var(--danger); font-size: 13px; }

/* ---------- quick upload（細い 1 行） ---------- */
.dropzone.quick {
  display: flex; align-items: center; gap: 10px; text-align: left;
  margin-bottom: 14px; padding: 9px 14px; background: var(--bg);
}
.dropzone.quick.over { background: var(--accent-soft); }
.quick-icon { font-size: 15px; flex: none; }
.quick-title { font-size: 13.5px; color: var(--text); }
.quick-note { font-size: 12px; color: var(--faint); margin-right: auto; }
.quick-folder { flex: none; }
.quick-progress { color: var(--accent); font-size: 13px; white-space: nowrap; }

/* ---------- toast / banner ---------- */
.toast-area { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 100; max-width: 92vw; }
.toast { padding: 9px 16px; border-radius: var(--r); background: var(--text); color: #fff; font-size: 13px; box-shadow: 0 6px 20px rgba(0,0,0,.2); }
.toast.error { background: var(--danger); }
.banner { padding: 10px 16px; background: var(--warn-soft); color: var(--warn-text); text-align: center; font-size: 13px; }

/* ---------- viewer ---------- */
.viewer-bar {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 8px 14px; background: var(--bg); border-bottom: 1px solid var(--line);
}
.viewer-title { font-weight: 600; font-size: 15px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 32vw; }
.area-bar {
  display: flex; align-items: center; gap: 10px; padding: 7px 14px;
  border-bottom: 1px solid var(--line); font-size: 12.5px;
  background: var(--bg-soft); color: var(--muted);
}
.area-bar.public { background: var(--bg-soft); color: var(--muted); }
.area-bar.private { background: #f5f3ff; color: #6d28d9; }
.area-bar.archived { background: var(--danger-soft); color: var(--danger); }
.area-bar .btn { border-color: currentColor; }
.area-bar .btn-accent { color: #fff; }
.viewer-frame-wrap { flex: 1; position: relative; background: #fff; }
.viewer-frame { position: absolute; inset: 0; width: 100%; height: 100%; border: none; }
.viewer-loading { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: var(--muted); background: #fff; padding: 24px; text-align: center; }
/* リンク資料：外部サイトは埋め込まず、開く導線だけを出す */
.link-panel {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 14px; padding: 32px 24px; text-align: center;
}
.link-panel .link-label { margin: 0; color: var(--faint); font-size: 12px; }
.link-panel .link-url {
  max-width: min(720px, 90%); word-break: break-all; color: var(--accent); font-size: 13.5px;
}
.link-panel .link-note { margin: 4px 0 0; max-width: 460px; color: var(--muted); font-size: 12.5px; line-height: 1.7; }
.details {
  margin: 0; padding: 12px 18px; background: var(--bg-soft); border-bottom: 1px solid var(--line);
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px 24px; font-size: 13px;
}
.details dt { color: var(--muted); font-size: 12px; }
.details dd { margin: 2px 0 0; word-break: break-word; }
.details .full { grid-column: 1 / -1; }
#dPath { font-family: Consolas, "SFMono-Regular", Menlo, monospace; font-size: 12.5px; }
.versions { list-style: none; margin: 0; padding: 0; }
.versions li { display: flex; gap: 10px; align-items: center; padding: 4px 0; border-bottom: 1px dashed var(--line); }
.versions li:last-child { border-bottom: none; }
.versions .v { font-weight: 600; min-width: 36px; }
.versions .cur { color: var(--accent); font-size: 12px; }
.versions .who { color: var(--muted); flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- markdown viewer ---------- */
.md-body {
  position: absolute; inset: 0; overflow-y: auto; padding: 32px 48px 80px;
  background: #fff; font-size: 15px; line-height: 1.8;
}
.md-body > * { max-width: 780px; margin-left: auto; margin-right: auto; }
.md-body h1, .md-body h2, .md-body h3 { font-weight: 700; line-height: 1.3; margin: 1.6em auto .6em; }
.md-body h1 { font-size: 28px; }
.md-body h2 { font-size: 22px; padding-bottom: .3em; border-bottom: 1px solid var(--line); }
.md-body h3 { font-size: 17px; }
.md-body p, .md-body ul, .md-body ol, .md-body blockquote, .md-body pre, .md-body table { margin-top: 0; margin-bottom: 1em; }
.md-body blockquote { border-left: 3px solid var(--line-strong); padding: 2px 16px; color: var(--muted); }
.md-body code { font-family: Consolas, "SFMono-Regular", Menlo, monospace; font-size: .92em; background: var(--bg-soft); padding: 1px 5px; border-radius: 4px; }
.md-body pre { background: var(--bg-soft); border: 1px solid var(--line); padding: 14px 18px; border-radius: var(--r); overflow-x: auto; }
.md-body pre code { background: none; padding: 0; }
.md-body table { border-collapse: collapse; width: 100%; font-size: 14px; }
.md-body th, .md-body td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; }
.md-body th { background: var(--bg-soft); font-weight: 600; }
.md-body img { max-width: 100%; }
.md-body hr { border: none; border-top: 1px solid var(--line); margin: 2em auto; }

.center-box { max-width: 420px; margin: 80px auto; padding: 24px; border: 1px solid var(--line); border-radius: 10px; text-align: center; }

@media (max-width: 860px) {
  .sidebar, .sidebar-resizer { display: none; }
  .main { padding: 12px; }
  .search { max-width: none; }
  .viewer-title { max-width: 50vw; }
  .details { grid-template-columns: 1fr; }
  .rank-row { grid-template-columns: 36px 1fr auto; }
  .card-actions { visibility: visible; }
  .md-body { padding: 20px 16px 60px; }
}
