:root {
  --side-bg: #1f2a37; --side-fg: #d6dde6; --side-muted: #8b98a8; --side-hover: #2c3a4b; --side-active: #374a60;
  --bg: #ffffff; --bg2: #f6f7f9; --fg: #1d232b; --muted: #6b7684; --line: #e4e7eb;
  --accent: #4f46e5; --accent-fg: #fff; --sel: #eef0ff; --danger: #c2410c;
  --font: system-ui, -apple-system, "Segoe UI", "Hiragino Sans", "Yu Gothic UI", "Meiryo", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --side-bg: #12171e; --side-fg: #cfd6df; --side-muted: #7d8896; --side-hover: #1d252f; --side-active: #283444;
    --bg: #181c22; --bg2: #1f242b; --fg: #e6e9ed; --muted: #949eab; --line: #2c333c;
    --accent: #7c74ff; --sel: #262b45; --danger: #fb923c;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: var(--font); color: var(--fg); background: var(--bg); -webkit-text-size-adjust: 100%; }
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

#app { display: grid; grid-template-columns: 230px 330px 1fr; height: 100dvh; }

/* サイドバー */
#sidebar { background: var(--side-bg); color: var(--side-fg); display: flex; flex-direction: column; min-height: 0; padding-top: env(safe-area-inset-top); }
.side-head { display: flex; align-items: center; padding: 14px 16px 8px; }
.brand { font-weight: 700; letter-spacing: .04em; flex: 1; }
.brand.big { font-size: 1.4rem; text-align: center; margin-bottom: 8px; }
.new-btn { margin: 6px 14px 12px; padding: 10px 14px; border: 0; border-radius: 22px; background: var(--accent); color: var(--accent-fg); font-weight: 600; text-align: left; }
.new-btn:hover { filter: brightness(1.08); }
nav { flex: 1; overflow-y: auto; padding: 0 8px; }
.nav-item { display: flex; width: 100%; gap: 6px; align-items: center; padding: 7px 10px; border: 0; background: none; border-radius: 6px; text-align: left; font-size: .92rem; }
.nav-item:hover { background: var(--side-hover); }
.nav-item.active { background: var(--side-active); color: #fff; }
.nav-item .n { margin-left: auto; color: var(--side-muted); font-size: .8rem; }
.nav-group { margin: 10px 0; }
.nav-title { display: flex; align-items: center; justify-content: space-between; padding: 4px 10px; color: var(--side-muted); font-size: .75rem; letter-spacing: .05em; }
.mini-btn { border: 0; background: none; color: var(--side-muted); font-size: 1rem; padding: 0 4px; }
.mini-btn:hover { color: #fff; }
.side-foot { padding: 10px 14px calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--side-hover); font-size: .82rem; }
.sync { color: var(--side-muted); margin-bottom: 6px; }
.sync.err { color: #fca5a5; }
.menu summary { cursor: pointer; color: var(--side-muted); }
.menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 6px 4px; color: var(--side-fg); }
.menu button:hover { color: #fff; }

/* 一覧 */
#listPane { position: relative; display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--line); background: var(--bg2); }
.list-head { display: flex; align-items: center; gap: 8px; padding: 16px 16px 6px; padding-top: calc(16px + env(safe-area-inset-top)); }
.list-head h1 { font-size: 1.15rem; margin: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.count { color: var(--muted); font-size: .85rem; }
.search { padding: 6px 16px 8px; }
.search input { width: 100%; padding: 8px 12px; border: 1px solid var(--line); border-radius: 18px; background: var(--bg); color: var(--fg); font-size: .95rem; outline: none; }
.search input:focus { border-color: var(--accent); }
.list-tools { padding: 0 16px; }
.link-btn { border: 0; background: none; color: var(--accent); padding: 4px 0; font-size: .85rem; }
#noteList { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.note-item { padding: 12px 16px; border-bottom: 1px solid var(--line); cursor: pointer; background: var(--bg); }
.note-item:hover { background: var(--sel); }
.note-item.active { background: var(--sel); box-shadow: inset 3px 0 0 var(--accent); }
.note-item .t { font-weight: 600; font-size: .95rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.note-item .s { color: var(--muted); font-size: .85rem; margin-top: 4px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; line-height: 1.45; }
.note-item .d { color: var(--muted); font-size: .75rem; margin-top: 6px; display: flex; gap: 8px; }
.note-item .d .nb { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-empty { padding: 40px 16px; color: var(--muted); text-align: center; font-size: .9rem; }

/* エディタ */
#editorPane { min-width: 0; min-height: 0; display: flex; flex-direction: column; background: var(--bg); }
.empty { margin: auto; color: var(--muted); padding: 20px; text-align: center; }
#editor { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.ed-head { display: flex; align-items: center; gap: 6px; padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top)); border-bottom: 1px solid var(--line); }
.ed-head select { max-width: 45%; padding: 5px 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); color: var(--fg); font-size: .85rem; }
.spacer { flex: 1; }
.icon-btn { border: 0; background: none; font-size: 1.1rem; padding: 6px 8px; border-radius: 6px; line-height: 1; }
.icon-btn:hover { background: var(--bg2); }
#sidebar .icon-btn:hover { background: var(--side-hover); }
.icon-btn.on { background: var(--sel); }
.pill { border: 1px solid var(--line); background: var(--bg); border-radius: 16px; padding: 5px 12px; font-size: .85rem; white-space: nowrap; flex-shrink: 0; }
.pill:hover { border-color: var(--accent); color: var(--accent); }
.trash-bar { background: #fff7ed; color: #7c2d12; padding: 8px 16px; font-size: .88rem; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
@media (prefers-color-scheme: dark) { .trash-bar { background: #3a2414; color: #fed7aa; } }
.trash-bar button { border: 1px solid currentColor; background: none; border-radius: 6px; padding: 3px 10px; }
.trash-bar .danger { color: var(--danger); }
.toolbar { display: flex; gap: 2px; padding: 6px 12px; border-bottom: 1px solid var(--line); overflow-x: auto; scrollbar-width: none; }
.toolbar button { border: 0; background: none; min-width: 34px; padding: 5px 8px; border-radius: 5px; font-size: .9rem; white-space: nowrap; }
.toolbar button:hover { background: var(--bg2); }
.toolbar .sep { width: 1px; background: var(--line); margin: 4px 4px; }
.ed-scroll { flex: 1; overflow-y: auto; padding: 22px 40px 60px; }
.title-input { width: 100%; border: 0; outline: none; font-size: 1.7rem; font-weight: 700; background: none; color: var(--fg); padding: 0; }
.meta { color: var(--muted); font-size: .8rem; margin: 6px 0 16px; }
.body { outline: none; min-height: 50vh; font-size: 1rem; line-height: 1.75; word-break: break-word; }
.body:empty::before { content: attr(data-placeholder); color: var(--muted); }
.tags-row { margin-top: 24px; padding-top: 10px; border-top: 1px solid var(--line); color: var(--muted); display: flex; gap: 6px; align-items: center; }
.tags-row input { flex: 1; border: 0; outline: none; background: none; color: var(--fg); font-size: .9rem; }

/* 本文の装飾(エディタ・話す用 共通) */
.rich h1, .rich h2, .rich h3 { line-height: 1.4; margin: 1em 0 .4em; }
.rich h2 { font-size: 1.3em; }
.rich p, .rich div { margin: 0; }
.rich ul, .rich ol { padding-left: 1.6em; margin: .3em 0; }
.rich blockquote { border-left: 3px solid var(--line); margin: .5em 0; padding-left: 1em; color: var(--muted); }
.rich hr { border: 0; border-top: 1px solid var(--line); margin: 1em 0; }
.rich table { border-collapse: collapse; margin: .5em 0; }
.rich td, .rich th { border: 1px solid var(--line); padding: 4px 8px; }
.rich a { color: var(--accent); }
.rich ul.todo { list-style: none; padding-left: .2em; }
.rich ul.todo > li { position: relative; padding-left: 1.7em; }
.rich ul.todo > li::before { content: ""; position: absolute; left: 0; top: .32em; width: 1.05em; height: 1.05em; border: 2px solid var(--muted); border-radius: 4px; cursor: pointer; }
.rich ul.todo > li[data-checked="1"] { color: var(--muted); text-decoration: line-through; }
.rich ul.todo > li[data-checked="1"]::before { background: var(--accent); border-color: var(--accent); }
.rich ul.todo > li[data-checked="1"]::after { content: "✓"; position: absolute; left: .2em; top: .02em; color: #fff; font-size: .85em; font-weight: 700; text-decoration: none; }

/* 話す用モード */
#talk { position: fixed; inset: 0; z-index: 50; background: var(--bg); display: flex; flex-direction: column; }
.talk-bar { display: flex; align-items: center; gap: 8px; padding: 10px 14px; padding-top: calc(10px + env(safe-area-inset-top)); border-bottom: 1px solid var(--line); }
.talk-btn { border: 1px solid var(--line); background: var(--bg2); border-radius: 8px; padding: 8px 14px; font-size: 1rem; }
.talk-timer { font-variant-numeric: tabular-nums; font-size: 1.2rem; color: var(--muted); padding: 4px 10px; cursor: pointer; }
.talk-content { flex: 1; overflow-y: auto; padding: 24px 6vw 40vh; font-size: var(--talk-size, 28px); line-height: 1.65; }
.talk-content h1.talk-title { font-size: 1.2em; margin: 0 0 .6em; }

/* ログイン */
#login { position: fixed; inset: 0; z-index: 60; background: rgba(15, 20, 28, .55); display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { background: var(--bg); border-radius: 14px; padding: 28px 24px; width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 10px; text-align: center; box-shadow: 0 10px 40px rgba(0,0,0,.25); }
.login-card p { margin: 0; color: var(--muted); font-size: .9rem; }
.login-card input { padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; font-size: 1rem; background: var(--bg); color: var(--fg); }
.login-card button[type=submit] { padding: 10px; border: 0; border-radius: 8px; background: var(--accent); color: var(--accent-fg); font-weight: 600; }
.err { color: var(--danger); font-size: .85rem; min-height: 1.2em; }

#toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 70; background: #222b36; color: #fff; padding: 10px 18px; border-radius: 20px; font-size: .9rem; max-width: calc(100% - 32px); }

.fab { position: absolute; right: 18px; bottom: calc(22px + env(safe-area-inset-bottom)); width: 56px; height: 56px; border-radius: 50%; border: 0; background: var(--accent); color: #fff; font-size: 1.8rem; box-shadow: 0 4px 14px rgba(0,0,0,.25); }
.only-mobile { display: none; }
#scrim { display: none; }

/* タブレット: サイドバーを細く */
@media (max-width: 1100px) and (min-width: 761px) {
  #app { grid-template-columns: 190px 280px 1fr; }
  .ed-scroll { padding: 18px 24px 60px; }
}

/* スマホ: 1画面ずつ */
@media (max-width: 760px) {
  #app { grid-template-columns: 1fr; }
  .only-mobile { display: inline-flex; align-items: center; justify-content: center; }
  #sidebar { position: fixed; inset: 0 auto 0 0; width: 80%; max-width: 300px; z-index: 40; transform: translateX(-100%); transition: transform .2s; }
  body.side-open #sidebar { transform: none; }
  body.side-open #scrim { display: block; position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.4); }
  body.show-list #editorPane { display: none; }
  body.show-editor #listPane { display: none; }
  #listPane { border-right: 0; }
  .ed-scroll { padding: 16px 16px 50vh; }
  .ed-head { gap: 2px; padding-left: 8px; padding-right: 8px; }
  .ed-head select { min-width: 0; flex: 0 1 auto; }
  .toolbar button { padding: 5px 10px; }
  .title-input { font-size: 1.4rem; }
  .body { font-size: 1.05rem; }
  .talk-content { padding: 18px 18px 40vh; }
}
