:root {
  --bg: #ffffff;
  --bg-side: #f6f5f2;
  --bg-list: #fbfaf8;
  --bg-hover: #efede8;
  --bg-active: #e6e2d9;
  --text: #1f1e1c;
  --text-sub: #6f6b64;
  --border: #e4e1da;
  --accent: #d9a400;
  --accent-text: #1f1e1c;
  --danger: #c0392b;
  --font: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", "Meiryo", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1c1e;
    --bg-side: #161617;
    --bg-list: #19191a;
    --bg-hover: #2a2a2c;
    --bg-active: #34342f;
    --text: #ecebe8;
    --text-sub: #9a978f;
    --border: #2f2f31;
    --accent: #e8b923;
    --danger: #ff6b5b;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font-family: var(--font); font-size: 15px; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
input, select { font: inherit; color: inherit; }

.app {
  display: grid;
  grid-template-columns: 230px 310px 1fr;
  /* 行の高さを画面の高さに固定する（長いノートを開いても画面全体が伸びず、本文・一覧・左の欄がそれぞれの中でスクロールする） */
  grid-template-rows: minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
}
.sidebar, .list, .editor { min-height: 0; }

/* ---- サイドバー ---- */
.sidebar {
  background: var(--bg-side);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  padding: 16px 10px 10px;
  overflow: hidden;
}
.brand { font-weight: 700; font-size: 18px; padding: 0 8px 14px; }
.btn-primary {
  background: var(--accent); color: var(--accent-text);
  border-radius: 8px; padding: 9px 12px; font-weight: 600; margin-bottom: 14px;
}
.btn-primary:hover { filter: brightness(1.05); }
/* ノートブック・タグが増えたら、この部分だけがスクロールする（下の「同期・データ」はいつも見える） */
nav { flex: 1; min-height: 0; overflow-y: auto; margin: 0 -4px; padding: 0 4px; }
.data-area { flex-shrink: 0; max-height: 60%; overflow-y: auto; overflow-x: hidden; }
.data-menu { border-top: 1px solid var(--border); }
.data-menu > summary {
  list-style: none; cursor: pointer; font-size: 12px; color: var(--text-sub); white-space: nowrap;
  padding: 7px 6px 7px 10px; border-radius: 6px; display: flex; justify-content: space-between; align-items: center; gap: 4px;
}
.data-menu > summary::-webkit-details-marker { display: none; }
.data-menu > summary::after { content: "▸"; font-size: 11px; }
.data-menu[open] > summary::after { content: "▾"; }
.data-menu > summary:hover { background: var(--bg-hover); color: var(--text); }
.data-menu .sidebar-footer { border-top: 0; padding-top: 0; }
.nav-item {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  width: 100%; text-align: left; padding: 7px 10px; border-radius: 6px;
}
.nav-item span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item:hover { background: var(--bg-hover); }
.nav-item.active { background: var(--bg-active); font-weight: 600; }
.nav-item.trash { margin-top: 12px; }
.count { color: var(--text-sub); font-size: 12px; }
.nav-section {
  display: flex; justify-content: space-between; align-items: center;
  margin: 16px 10px 4px; font-size: 12px; color: var(--text-sub); font-weight: 600;
}
.nav-row { position: relative; }
.nav-row .row-menu {
  position: absolute; right: 30px; top: 50%; transform: translateY(-50%);
  display: none; font-size: 12px; color: var(--text-sub); padding: 2px 6px; border-radius: 4px;
}
.nav-row:hover .row-menu { display: block; }
.nav-row .row-menu:hover { background: var(--bg-active); }
/* ---- 同期 ---- */
.sync-box { border-top: 1px solid var(--border); padding: 10px 6px 8px; font-size: 13px; }
.sync-status { display: flex; align-items: flex-start; gap: 7px; color: var(--text-sub); line-height: 1.4; }
.sync-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--border); flex-shrink: 0; margin-top: 5px; }
.sync-dot.ok { background: #3aa76d; }
.sync-dot.busy { background: var(--accent); animation: pulse 1s infinite alternate; }
.sync-dot.error { background: var(--danger); }
.sync-dot.offline { background: var(--text-sub); }
.sync-detail { display: grid; gap: 2px; margin-top: 4px; padding-left: 15px; font-size: 12px; color: var(--text-sub); }
.sync-detail .warn-link { color: var(--danger); padding: 2px 0; text-align: left; }
.detail-error { color: var(--danger); }
.save-state { font-size: 12px; color: var(--text-sub); }
.save-state.error { color: var(--danger); font-weight: 600; }
.backup-list { list-style: none; margin: 0 0 8px; padding: 0; display: grid; gap: 4px; }
.backup-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; }
.backup-row b { display: block; font-size: 14px; }
.backup-row small { color: var(--text-sub); font-size: 12px; }
.backup-list .note { list-style: none; color: var(--text-sub); font-size: 13px; }
.code-steps { margin: 6px 0 12px; padding-left: 1.3em; line-height: 1.9; font-size: 14px; }
.code-steps a { color: var(--accent); font-weight: 600; }
.restore-area { margin-top: 16px; border-top: 1px solid var(--border); padding-top: 4px; }
.check-list { list-style: none; padding-left: 0; }
.check-list .issue { padding: 6px 0; border-bottom: 1px solid var(--border); line-height: 1.6; }
.check-list .issue small { display: block; color: var(--text-sub); }
.check-list .issue.error, .check-list .issue.warn { color: var(--danger); }
.check-list .issue.ok { color: #3aa76d; font-weight: 600; }
@keyframes pulse { from { opacity: 1; } to { opacity: .3; } }
.sync-buttons { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; align-items: center; }
.sync-buttons [hidden] { display: none; }
.btn.small, .link-btn.small { font-size: 12px; padding: 4px 10px; }
.sidebar-footer { border-top: 1px solid var(--border); padding-top: 8px; display: grid; gap: 2px; }
.link-btn { text-align: left; font-size: 13px; color: var(--text-sub); padding: 5px 10px; border-radius: 6px; }
.link-btn:hover { background: var(--bg-hover); color: var(--text); }
.link-btn.danger { color: var(--danger); }
.icon-btn {
  min-width: 30px; height: 30px; border-radius: 6px; font-size: 16px; line-height: 1;
  display: inline-flex; align-items: center; justify-content: center; color: var(--text-sub);
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.icon-btn.on { color: var(--accent); }

/* ---- ノート一覧 ---- */
.list {
  background: var(--bg-list); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-width: 0;
}
.list-header { display: flex; align-items: center; gap: 6px; padding: 14px 14px 6px; }
.list-header h1 { font-size: 18px; margin: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.search { padding: 4px 14px 10px; }
.search input {
  width: 100%; padding: 8px 10px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--bg); outline: none;
}
.search input:focus { border-color: var(--accent); }
.trash-actions {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  padding: 0 14px 8px; font-size: 12px; color: var(--text-sub);
}
.trash-actions[hidden] { display: none; }
.note-list { list-style: none; margin: 0; padding: 0 8px 20px; overflow-y: auto; flex: 1; }
.note-item {
  padding: 10px 10px; border-radius: 8px; cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.note-item:hover { background: var(--bg-hover); }
.note-item.active { background: var(--bg-active); border-bottom-color: transparent; }
.note-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.note-sub { display: flex; gap: 8px; font-size: 13px; color: var(--text-sub); margin-top: 3px; }
.note-sub .date { flex-shrink: 0; }
.note-sub .snip { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.note-tags { margin-top: 4px; font-size: 12px; color: var(--accent); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.empty { color: var(--text-sub); text-align: center; padding: 30px 16px; font-size: 14px; }

/* ---- エディタ ---- */
.editor { display: flex; flex-direction: column; min-width: 0; background: var(--bg); }
.editor-empty { margin: auto; color: var(--text-sub); }
.editor-body { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.content { min-height: 0; }
.editor-body[hidden] { display: none; }
.editor-header { display: flex; align-items: center; gap: 6px; padding: 10px 16px; border-bottom: 1px solid var(--border); }
.editor-header select {
  border: 1px solid var(--border); border-radius: 6px; padding: 4px 8px; background: var(--bg); max-width: 50%;
}
.spacer { flex: 1; }
.trash-banner {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--bg-side); border-bottom: 1px solid var(--border); font-size: 14px;
}
.trash-banner[hidden] { display: none; }
.conflict-banner {
  display: grid; gap: 8px; padding: 10px 16px; border-bottom: 1px solid var(--border);
  background: #fff1ef; color: #6b1d12; font-size: 13px; line-height: 1.6;
}
.conflict-banner[hidden] { display: none; }
.conflict-banner .banner-buttons { display: flex; gap: 6px; flex-wrap: wrap; }
@media (prefers-color-scheme: dark) { .conflict-banner { background: #3a1d19; color: #ffd2ca; } }
.trash-banner span { flex: 1; color: var(--text-sub); }
.btn { border: 1px solid var(--border); border-radius: 6px; padding: 5px 12px; background: var(--bg); }
.btn:hover { background: var(--bg-hover); }
.btn.danger { color: var(--danger); }
.toolbar { display: flex; gap: 2px; padding: 6px 12px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.toolbar[hidden] { display: none; }
.toolbar button { padding: 4px 9px; border-radius: 6px; font-size: 13px; color: var(--text-sub); }
.toolbar button:hover { background: var(--bg-hover); color: var(--text); }

.content {
  flex: 1; overflow-y: auto; padding: 22px max(24px, calc((100% - 760px) / 2)) 60px;
  outline: none; line-height: 1.75; font-size: 16px; overflow-wrap: anywhere;
}
.content:empty::before { content: attr(data-placeholder); color: var(--text-sub); }
.content.is-empty > div:first-child::before { content: "1行目がタイトルになります"; color: var(--text-sub); font-weight: 400; pointer-events: none; }
/* 1行目はタイトル（先頭が折りたたみのときは、その見出しをタイトルとして扱う） */
.content > :first-child:not(.fold) { font-size: 22px; font-weight: 700; margin-top: 0; }
.content > .fold:first-child > .fold-title { font-size: 22px; margin-top: 0; }
.content h2 { font-size: 19px; margin: 18px 0 6px; }
.content h3 { font-size: 17px; margin: 14px 0 4px; }
.content p, .content div { margin: 0; }
.content ul, .content ol { margin: 4px 0; padding-left: 1.6em; }
.content blockquote { border-left: 3px solid var(--border); margin: 6px 0; padding-left: 12px; color: var(--text-sub); }
.content a { color: var(--accent); }
.content ul.checklist { list-style: none; padding-left: 0.2em; }
.content ul.checklist > li { position: relative; padding-left: 1.8em; }
.content ul.checklist > li::before {
  content: ""; position: absolute; left: 0; top: 0.35em;
  width: 1.05em; height: 1.05em; border: 1.5px solid var(--text-sub); border-radius: 4px; cursor: pointer;
}
.content ul.checklist > li.done { color: var(--text-sub); text-decoration: line-through; }
.content ul.checklist > li.done::before { background: var(--accent); border-color: var(--accent); }
.content ul.checklist > li.done::after {
  content: "✓"; position: absolute; left: 0.18em; top: 0.05em; font-size: 0.85em; color: #fff; font-weight: 700;
}
.content[contenteditable="false"] { opacity: 0.75; }

/* ---- 画像 ---- */
.content img { display: block; max-width: 100%; height: auto; margin: 6px 0; border-radius: 6px; }
.content img[data-missing], .content img:not([src]) {
  width: 180px; height: 110px; background: var(--bg-side); border: 1px dashed var(--border);
}

/* ---- 折りたたみ ---- */
.content .fold { margin: 4px 0; }
.content .fold-title { position: relative; padding-left: 1.5em; margin: 12px 0 2px; }
.content .fold-title::before {
  content: "▾"; position: absolute; left: 0; top: 0; width: 1.3em; text-align: center;
  color: var(--text-sub); cursor: pointer; font-weight: 400;
}
.content .fold.closed > .fold-title::before { content: "▸"; }
.content .fold-title:empty::after { content: "見出し"; color: var(--text-sub); font-weight: 400; }
.content .fold-body { margin-left: .55em; padding-left: calc(.95em - 2px); border-left: 2px solid var(--border); }
.content .fold.closed > .fold-body { display: none; }

/* ---- 目次 ---- */
.editor-body { position: relative; }
.toc-btn { padding: 3px 10px; white-space: nowrap; flex-shrink: 0; }
.toc {
  position: absolute; right: 12px; top: 50px; z-index: 5;
  width: min(320px, calc(100% - 24px)); max-height: min(60vh, 480px); overflow-y: auto;
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 8px 28px rgba(0,0,0,.18); padding: 6px;
}
.toc[hidden] { display: none; }
.toc-item {
  display: block; width: 100%; text-align: left; padding: 7px 10px; border-radius: 6px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px;
}
.toc-item:hover { background: var(--bg-hover); }
.toc-item.level2 { padding-left: 26px; color: var(--text-sub); font-size: 13px; }
.toc-empty { margin: 8px 10px; color: var(--text-sub); font-size: 13px; line-height: 1.6; }
.toc-search { width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); margin-bottom: 6px; outline: none; }
.toc-search:focus { border-color: var(--accent); }
.toc-item.hit { white-space: normal; line-height: 1.5; }
.toc-item.hit small { display: block; color: var(--text-sub); font-size: 11px; }
.toc-item mark { background: #ffe58a; color: inherit; border-radius: 2px; }

.editor-footer {
  display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
  padding: 8px 16px; border-top: 1px solid var(--border); font-size: 13px;
}
.tags { display: flex; gap: 4px; flex-wrap: wrap; }
.chip {
  display: inline-flex; align-items: center; gap: 2px;
  background: var(--bg-side); border: 1px solid var(--border); border-radius: 999px; padding: 1px 4px 1px 9px;
}
.chip button { color: var(--text-sub); padding: 0 4px; font-size: 12px; }
.chip button:hover { color: var(--danger); }
/* タグ：ノートの上の1行（本文の長さに関係なく、いつも見える） */
.tag-bar {
  display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
  padding: 5px 16px; border-bottom: 1px solid var(--border); font-size: 13px; min-height: 36px;
}
.tag-bar[hidden] { display: none; }
.tag-bar .chip { color: var(--accent); }
#tagInput {
  border: 1px dashed var(--border); border-radius: 999px; background: transparent; outline: none;
  width: 9.5em; padding: 2px 10px; color: var(--text);
}
#tagInput:focus { border-style: solid; border-color: var(--accent); width: 14em; }
#tagInput::placeholder { color: var(--text-sub); }
#tagInput[hidden] { display: none; }
.tag-suggest { display: inline-flex; gap: 4px; flex-wrap: wrap; align-items: center; }
.tag-suggest:empty { display: none; }
.tag-suggest .hint { color: var(--text-sub); font-size: 12px; }
.tag-suggest button {
  border: 1px dashed var(--accent); border-radius: 999px; padding: 1px 9px; font-size: 12px; color: var(--text);
}
.tag-suggest button:hover { background: var(--bg-hover); }
.tag-suggest .dismiss { border: 0; color: var(--text-sub); padding: 1px 4px; }
.meta { color: var(--text-sub); font-size: 12px; }

/* ---- 取り込み画面 ---- */
.dialog {
  border: 1px solid var(--border); border-radius: 12px; padding: 22px 22px 18px;
  background: var(--bg); color: var(--text); width: min(520px, calc(100% - 32px));
  box-shadow: 0 10px 40px rgba(0,0,0,.25); position: relative;
}
.dialog::backdrop { background: rgba(0,0,0,.35); }
.dialog h2 { margin: 0 30px 12px 0; font-size: 18px; }
.dialog p { margin: 0 0 12px; line-height: 1.6; }
.dialog .note { color: var(--text-sub); font-size: 13px; }
.dialog-close { position: absolute; top: 12px; right: 12px; }
.dialog-buttons { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }
.dialog-buttons .btn-primary { margin: 0; }
.dialog-buttons .btn { padding: 8px 14px; }
.howto { background: var(--bg-side); border-radius: 8px; padding: 10px 12px; font-size: 14px; }
.howto summary { cursor: pointer; font-weight: 600; }
.howto ol { margin: 10px 0 6px; padding-left: 1.3em; line-height: 1.7; }
.howto .note { margin: 0; }
.summary { margin: 0; padding-left: 1.2em; line-height: 1.9; }
.dialog { max-height: calc(100dvh - 32px); overflow-y: auto; }
.field-label { display: block; font-weight: 600; font-size: 14px; margin: 14px 0 6px; }
.dialog p.field-label { margin: 0 0 6px; }
.field-select { width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); }
.choices { display: grid; gap: 6px; }
.choice {
  display: flex; gap: 10px; align-items: flex-start; text-align: left; width: 100%;
  border: 1px solid var(--border); border-radius: 8px; padding: 9px 12px; background: var(--bg);
}
.choice:hover { background: var(--bg-hover); }
.choice[aria-checked="true"] { border-color: var(--accent); background: var(--bg-side); }
.choice .dot { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--text-sub); flex-shrink: 0; margin-top: 3px; }
.choice[aria-checked="true"] .dot { border-color: var(--accent); background: radial-gradient(var(--accent) 45%, transparent 50%); }
.choice b { display: block; font-size: 14px; }
.choice small { display: block; color: var(--text-sub); font-size: 12px; line-height: 1.5; margin-top: 2px; }
.export-howto { margin-top: 10px; }
.export-howto p { margin: 0 0 6px; font-weight: 600; }
.export-howto ul { margin: 0; padding-left: 1.2em; line-height: 1.7; }
.summary .warn { color: var(--text-sub); }
.readonly-bar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 20; display: flex; gap: 10px; align-items: center; flex-wrap: wrap;
  padding: max(8px, env(safe-area-inset-top)) 14px 8px; background: #fff4d6; color: #5a4300; font-size: 14px;
  border-bottom: 1px solid #e8d38c;
}
.readonly-bar[hidden] { display: none; }
.readonly-bar span { flex: 1; min-width: 200px; }
@media (prefers-color-scheme: dark) { .readonly-bar { background: #3a3110; color: #f5e3a6; border-color: #5a4a14; } }
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 8px; font-size: 14px;
  box-shadow: 0 4px 16px rgba(0,0,0,.2); z-index: 10; max-width: calc(100% - 32px);
}
.toast[hidden] { display: none; }

.mobile-only { display: none; }

/* ---- スマホ：1画面ずつ表示 ---- */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar, .list, .editor { display: none; border-right: 0; }
  .app[data-pane="nav"] .sidebar { display: flex; }
  .app[data-pane="list"] .list { display: flex; }
  .app[data-pane="editor"] .editor { display: flex; }
  .mobile-only { display: inline-flex; }
  .content { padding: 16px 16px 60px; }
  .nav-row .row-menu { display: block; padding: 6px 10px; }
  /* iPhoneは16px未満の入力欄を押すと画面が勝手に拡大されるため、入力欄は16pxにする */
  input, select, textarea, #tagInput { font-size: 16px; }
  /* 指で押しやすい大きさにする */
  .icon-btn { min-width: 40px; height: 40px; }
  .nav-item { padding: 10px 12px; }
  .toolbar button { padding: 7px 10px; }
  .note-item { padding: 12px 10px; }
  .link-btn { padding: 8px 10px; }
  /* iPhoneの画面上部の切り欠き・下部のバーに重ならないようにする */
  .sidebar, .list-header, .editor-header { padding-top: max(10px, env(safe-area-inset-top)); }
  .editor-footer, .sidebar-footer { padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .toc { top: calc(58px + env(safe-area-inset-top)); }
  .editor-header { gap: 2px; padding-left: 6px; padding-right: 6px; }
  .editor-header select { max-width: 42%; }
}
@media (min-width: 761px) and (max-width: 1020px) {
  .app { grid-template-columns: 200px 260px 1fr; }
}
