/* 落ち着いた「書斎」の見た目。暗い画面では黒地にこはく色のフォルダ */
/* 色は light-dark(明るいとき, 暗いとき) */
:root {
  --bg:      light-dark(#f7f6f3, #0f0f10);
  --panel:   light-dark(#ffffff, #18181a);
  --panel-2: light-dark(#f0eee9, #202023);
  --line:    light-dark(#e2dfd8, #2c2c30);
  --ink:     light-dark(#1f1d1a, #e8e6e1);
  --sub:     light-dark(#6b665d, #a29d94);
  --accent:  light-dark(#9a6412, #e5b25d);
  --accent-ink: light-dark(#ffffff, #1a1408);
  --accent-soft: light-dark(#f6ead6, #2e2414);
  --warn:    light-dark(#a3471c, #f0a070);
  --ok:      light-dark(#2f6b45, #8fd0a5);
  --danger:  light-dark(#a8323a, #f08a8f);
  --idea:    light-dark(#9a6412, #e5b25d);
  --reading: light-dark(#6a4bb0, #b49cf0);
  --study:   light-dark(#1d6f6a, #74cfc6);
  --project: light-dark(#2b5ea8, #8db5f2);
  --radius: 12px;
  --tabbar-h: 60px;
  --font: system-ui, -apple-system, "Hiragino Sans", "Yu Gothic UI", "Meiryo", sans-serif;
  --mono: ui-monospace, "Cascadia Mono", Consolas, monospace;

  color-scheme: light dark;
  &:has(> head > meta[name="color-scheme"][content="light"]) { color-scheme: light; }
  &:has(> head > meta[name="color-scheme"][content="dark"]) { color-scheme: dark; }
}

* { box-sizing: border-box; }
html, body {
  margin: 0; background: var(--bg); color: var(--ink);
  font-family: var(--font); font-size: 16px; line-height: 1.7;
  -webkit-text-size-adjust: 100%;
}
button, input, textarea, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }
a { color: var(--accent); }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.app {
  max-width: 640px; margin: 0 auto; min-height: 100dvh;
  padding: 0 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
}

/* 上の帯 */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  margin: 0 -16px; padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.topbar-title { flex: 1; margin: 0; font-size: 1.15rem; font-weight: 700; letter-spacing: .01em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn { display: grid; place-items: center; width: 40px; height: 40px; margin-left: -8px; border: 0; background: none; border-radius: 10px; cursor: pointer; }
.icon-btn:hover { background: var(--panel-2); }
.sync-chip {
  border: 1px solid var(--line); background: var(--panel); border-radius: 999px;
  padding: 4px 12px; font-size: .8rem; color: var(--sub); cursor: pointer; white-space: nowrap;
}
.sync-chip::before { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 6px; background: var(--sub); vertical-align: 1px; }
.sync-chip.is-ok::before { background: var(--ok); }
.sync-chip.is-warn { color: var(--warn); }
.sync-chip.is-warn::before { background: var(--warn); }
.sync-chip.is-busy::before { background: var(--accent); animation: pulse 1s infinite alternate; }
@keyframes pulse { to { opacity: .3; } }

.banner {
  display: flex; gap: 12px; align-items: center; justify-content: space-between; flex-wrap: wrap;
  margin: 12px 0 0; padding: 12px 14px; border-radius: var(--radius);
  background: var(--accent-soft); font-size: .9rem;
}

/* 区切り */
.section { margin-top: 22px; }
.section-title { margin: 0 0 10px; font-size: .8rem; font-weight: 700; color: var(--sub); letter-spacing: .08em; }
.card { margin-top: 16px; padding: 16px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
.card p { margin: 0 0 8px; }
.empty { color: var(--sub); text-align: center; padding: 28px 8px; }
.fineprint { color: var(--sub); font-size: .8rem; margin-top: 20px; }
.center { text-align: center; }

/* 種類ごとの色 */
.kind-idea    { --k: var(--idea); }
.kind-reading { --k: var(--reading); }
.kind-study   { --k: var(--study); }
.kind-project { --k: var(--project); }

/* 書くボタン */
.kind-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.kind-card {
  display: grid; grid-template-columns: auto 1fr; grid-template-rows: auto auto; column-gap: 10px;
  padding: 14px; border-radius: var(--radius); text-decoration: none; color: var(--ink);
  background: var(--panel); border: 1px solid var(--line);
  transition: border-color .15s, transform .15s;
}
.kind-card:hover { border-color: var(--k); }
.kind-card:active { transform: scale(.98); }
.kind-mark {
  grid-row: span 2; display: grid; place-items: center; width: 36px; height: 36px; border-radius: 10px;
  background: color-mix(in srgb, var(--k) 16%, transparent); color: var(--k); font-weight: 800;
}
.kind-name { font-weight: 700; }
.kind-hint { font-size: .75rem; color: var(--sub); line-height: 1.45; }

/* メモの一覧 */
.note-list { display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.note-row {
  display: flex; gap: 10px; align-items: flex-start; padding: 12px 14px;
  text-decoration: none; color: var(--ink); border-left: 3px solid var(--k);
}
.note-row + .note-row { border-top: 1px solid var(--line); }
.note-row:hover { background: var(--panel-2); }
.note-row-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.note-row-title { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.note-row-sub { font-size: .8rem; color: var(--sub); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.note-row-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; flex: none; }
.when { font-size: .72rem; color: var(--sub); font-variant-numeric: tabular-nums; }
.badge { font-size: .7rem; font-weight: 700; padding: 1px 8px; border-radius: 999px; color: var(--k); background: color-mix(in srgb, var(--k) 14%, transparent); }
.dot-pending { width: 7px; height: 7px; border-radius: 50%; background: var(--warn); }
.result-count { font-size: .8rem; color: var(--sub); margin: 16px 0 6px; }

/* 検索とフォルダ */
.search { margin-top: 14px; }
.search input, .field input[type=text], .field input[type=date], .field textarea {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel);
}
.search input:focus, .field input:focus, .field textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
.vault-box { margin-top: 12px; padding: 6px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
.folder {
  display: flex; align-items: center; gap: 10px; width: 100%; text-align: left;
  padding: 8px 10px 8px calc(10px + var(--depth) * 18px); border: 0; background: none; border-radius: 8px; cursor: pointer;
}
.folder svg { color: var(--accent); }
.folder:hover { background: var(--panel-2); }
.folder.is-active { background: var(--accent-soft); }
.folder-name { flex: 1; display: flex; flex-direction: column; font-weight: 650; line-height: 1.35; }
.folder-name small { font-weight: 400; font-size: .75rem; color: var(--sub); }
.count { font-size: .75rem; color: var(--sub); font-variant-numeric: tabular-nums; }

.tag-row, .chip-pick { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.chip {
  border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: 3px 12px;
  font-size: .82rem; cursor: pointer; color: var(--sub);
}
.chip.is-active { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.chip-static { cursor: default; }
.chip-pick { margin: 0 0 8px; }

/* メモを見る */
.note-view {
  margin-top: 16px; padding: 18px 18px 14px; background: var(--panel);
  border: 1px solid var(--line); border-top: 3px solid var(--k); border-radius: var(--radius);
}
.note-path { display: flex; align-items: center; gap: 6px; font-size: .78rem; color: var(--sub); }
.note-path svg { width: 16px; height: 16px; color: var(--accent); }
.note-stamp { margin: 14px 0 0; font-size: .75rem; color: var(--sub); border-top: 1px dashed var(--line); padding-top: 10px; word-break: break-all; }
.md { overflow-wrap: anywhere; }
.md h2 { font-size: 1.35rem; margin: 10px 0 8px; line-height: 1.4; }
.md h3 { font-size: 1rem; margin: 20px 0 6px; color: var(--k); }
.md h4, .md h5 { font-size: .95rem; margin: 14px 0 4px; }
.md p { margin: 0 0 10px; }
.md ul, .md ol { margin: 0 0 10px; padding-left: 1.4em; }
.md li.task { list-style: none; margin-left: -1.4em; display: flex; gap: 8px; align-items: baseline; }
.md li.task .box { flex: none; width: 14px; height: 14px; border: 1.5px solid var(--sub); border-radius: 4px; transform: translateY(2px); }
.md li.task.done .box { background: var(--accent); border-color: var(--accent); }
.md li.task.done { color: var(--sub); text-decoration: line-through; }
.md blockquote { margin: 0 0 10px; padding: 2px 12px; border-left: 3px solid var(--line); color: var(--sub); }
.md code { font-family: var(--mono); font-size: .88em; background: var(--panel-2); padding: 1px 5px; border-radius: 5px; }
.md pre { background: var(--panel-2); padding: 12px; border-radius: 8px; overflow-x: auto; }
.md pre code { background: none; padding: 0; }
.md mark { background: var(--accent-soft); color: inherit; padding: 0 2px; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }
.md .md-tag { color: var(--accent); }
.wikilink { text-decoration: underline dotted; -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; }
.wikilink.is-missing { color: var(--sub); }

/* バックリンク */
.backlinks { margin-top: 26px; }
.hint-box { margin: 0; padding: 12px 14px; font-size: .85rem; color: var(--sub); background: var(--panel); border: 1px dashed var(--line); border-radius: var(--radius); }
.hint-box code { font-family: var(--mono); font-size: .9em; color: var(--accent); }

/* リンクのプレビュー */
.peek {
  position: fixed; z-index: 20; max-height: min(60vh, 420px); overflow: auto;
  padding: 12px 14px; background: var(--panel); border: 1px solid var(--line); border-top: 3px solid var(--k, var(--line));
  border-radius: var(--radius); box-shadow: 0 12px 32px rgb(0 0 0 / .28);
  font-size: .9rem; line-height: 1.6;
}
.peek-head { display: flex; gap: 8px; align-items: center; justify-content: space-between; }
.peek-title { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.peek-path { font-size: .72rem; color: var(--sub); margin-bottom: 6px; }
.peek-body h3 { margin-top: 10px; font-size: .9rem; }
.peek-missing { color: var(--sub); margin: 6px 0; }
.peek-foot { display: flex; justify-content: flex-end; margin-top: 8px; position: sticky; bottom: -12px; padding: 6px 0 2px; background: var(--panel); }
.raw { margin-top: 12px; padding: 14px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); font-family: var(--mono); font-size: .8rem; white-space: pre-wrap; word-break: break-all; }

/* ボタン */
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; min-height: 42px; padding: 8px 16px;
  border-radius: 10px; border: 1px solid var(--line); background: var(--panel); text-decoration: none; color: var(--ink);
  font-weight: 650; cursor: pointer;
}
.btn:hover { background: var(--panel-2); }
.btn-accent { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-accent:hover { background: color-mix(in srgb, var(--accent) 88%, var(--ink)); }
.btn-danger { color: var(--danger); }
.btn-small { min-height: 34px; padding: 4px 12px; font-size: .85rem; }
.linkish { border: 0; background: none; color: var(--accent); text-decoration: underline; cursor: pointer; padding: 0 4px; }

/* フォーム */
.note-form { margin-top: 16px; }
.field { margin-bottom: 18px; }
.field-label { display: block; font-weight: 700; font-size: .9rem; margin-bottom: 6px; }
.field-label::before { content: ""; display: inline-block; width: 4px; height: 12px; margin-right: 8px; border-radius: 2px; background: var(--k); }
.req { font-size: .7rem; font-weight: 700; color: var(--warn); margin-left: 4px; }
.field textarea { min-height: 120px; resize: vertical; line-height: 1.65; }
.md-tools { display: flex; gap: 4px; flex-wrap: wrap; margin-bottom: 6px; }
.md-tools button {
  border: 1px solid var(--line); background: var(--panel-2); border-radius: 8px; padding: 2px 10px;
  font-size: .78rem; color: var(--sub); cursor: pointer; min-height: 32px;
}
.md-tools button:hover { color: var(--ink); }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg label { cursor: pointer; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: inline-block; padding: 6px 14px; border-radius: 999px; border: 1px solid var(--line); background: var(--panel); font-size: .88rem; }
.seg input:checked + span { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); font-weight: 700; }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.form-error { color: var(--danger); font-weight: 650; }
.form-actions {
  position: sticky; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 8px);
  display: flex; gap: 8px; justify-content: flex-end; padding: 10px; margin: 0 -10px;
  background: color-mix(in srgb, var(--bg) 90%, transparent); backdrop-filter: blur(8px); border-radius: var(--radius);
}
.form-actions .btn-accent { min-width: 140px; }
.restored { font-size: .85rem; background: var(--accent-soft); padding: 8px 12px; border-radius: 10px; }
.howto { margin: 0; padding-left: 1.3em; }
.howto li { margin-bottom: 6px; }

/* 下のタブ */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  display: flex; justify-content: center; gap: 4px;
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--panel) 92%, transparent); backdrop-filter: blur(10px);
  border-top: 1px solid var(--line);
}
.tabbar a {
  flex: 1; max-width: 160px; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 4px; border-radius: 10px; font-size: .72rem; color: var(--sub); text-decoration: none;
}
.tabbar a.is-active { color: var(--accent); }
.tabbar a.is-active svg { stroke-width: 2.2; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px); z-index: 10;
  transform: translateX(-50%); max-width: calc(100% - 32px);
  padding: 10px 16px; border-radius: 10px; background: var(--ink); color: var(--bg); font-size: .88rem;
  box-shadow: 0 6px 20px rgb(0 0 0 / .25);
}

@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
