/* 読書メモ：辞書・単語帳のように簡潔に。狭い幅（250〜320px）優先 */

:root {
  --bg: #fafaf8;
  --surface: #ffffff;
  --fg: #1c1c1e;
  --fg2: #3f3f42;
  --muted: #74747a;
  --line: #e6e6e1;
  --chip: #eeeeea;
  --accent: #5f7d6b;
  --accent-fg: #ffffff;
  --accent-soft: #e5ece7;
  --pin-bg: #f6f4ec;
  --danger: #c0392b;
  --warn-bg: #fff4d4;
  --warn-fg: #7a5a00;
  --shadow: 0 8px 30px rgba(0, 0, 0, .18);
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #1c1c1e;
  --surface: #26262a;
  --fg: #ececee;
  --fg2: #cfcfd3;
  --muted: #9a9aa1;
  --line: #36363b;
  --chip: #303035;
  --accent: #8fb39d;
  --accent-fg: #14201a;
  --accent-soft: #2c3a31;
  --pin-bg: #2a2924;
  --danger: #ff6b5e;
  --warn-bg: #3a3220;
  --warn-fg: #f1d48a;
  --shadow: 0 8px 30px rgba(0, 0, 0, .5);
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1c1c1e;
    --surface: #26262a;
    --fg: #ececee;
    --fg2: #cfcfd3;
    --muted: #9a9aa1;
    --line: #36363b;
    --chip: #303035;
    --accent: #8fb39d;
    --accent-fg: #14201a;
    --accent-soft: #2c3a31;
    --pin-bg: #2a2924;
    --danger: #ff6b5e;
    --warn-bg: #3a3220;
    --warn-fg: #f1d48a;
    --shadow: 0 8px 30px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic UI", "Meiryo", sans-serif;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}
body.modal-open { overflow: hidden; }
#app { max-width: 760px; margin: 0 auto; min-height: 100vh; min-height: 100dvh; overflow-x: clip; }

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
/* iOSで入力時に画面が拡大されないよう16px以上にする */
input, select, textarea { font-size: max(16px, 1rem); }
[hidden] { display: none !important; }

.ic {
  width: 1.25em; height: 1.25em; flex: none;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}
.pin-ic { fill: currentColor; stroke-width: 1.2; }

/* ---------- 上部（スクロールしても固定） ---------- */
.sticky {
  position: sticky; top: 0; z-index: 10;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  padding-top: env(safe-area-inset-top);
}
.bar {
  display: flex; align-items: center; gap: 2px;
  min-height: 48px;
  padding: 0 4px 0 max(4px, env(safe-area-inset-left));
}
.bar-title {
  flex: 1; min-width: 0; margin: 0 4px;
  font-size: 1.05rem; font-weight: 700;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.bar-title.app-name { margin-left: 8px; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 44px; height: 44px; padding: 0; flex: none;
  border: 0; border-radius: 10px; background: transparent;
  font-size: 1rem;
}
.icon-btn:active { background: var(--chip); }
.icon-btn.accent { color: var(--accent); }
.icon-btn.accent .ic { width: 1.6em; height: 1.6em; stroke-width: 2.2; }
.icon-btn.sm { width: 36px; height: 36px; }
.icon-btn:disabled { opacity: .3; }

.tools { padding: 0 8px 6px; }
.search {
  position: relative; display: flex; align-items: center;
  background: var(--chip); border-radius: 10px;
  padding-left: 10px; color: var(--muted);
}
.search input {
  flex: 1; min-width: 0; height: 40px;
  border: 0; background: transparent; outline: none;
  padding: 0 4px 0 6px; color: var(--fg);
  -webkit-appearance: none; appearance: none;
}
.search input::-webkit-search-cancel-button { display: none; }
.search .clear {
  width: 36px; height: 36px; border: 0; background: transparent; color: var(--muted);
  display: inline-flex; align-items: center; justify-content: center;
}

.chips {
  display: flex; gap: 4px;
  margin: 6px -8px 0; padding: 0 8px;
  overflow-x: auto; scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none;
  display: inline-flex; align-items: center; gap: 3px;
  min-height: 32px; padding: 0 12px;
  border: 0; border-radius: 8px;
  background: transparent; color: var(--fg2);
  font-size: .85rem; white-space: nowrap;
}
.chip.on { background: var(--accent); color: var(--accent-fg); font-weight: 600; }
.chip.ghost { color: var(--muted); }
.chip.ghost .ic { width: 1em; height: 1em; }

.tool-row {
  display: flex; align-items: center; gap: 4px;
  margin-top: 4px;
}
.count { flex: 1; min-width: 0; font-size: .8rem; color: var(--muted); white-space: nowrap; }
.tool-row select {
  min-width: 0; max-width: 46%;
  height: 32px; padding: 0 22px 0 8px;
  border: 0; border-radius: 8px;
  background: var(--chip) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2374747a' stroke-width='2.5' stroke-linecap='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E") no-repeat right 6px center / 12px;
  -webkit-appearance: none; appearance: none;
  font-size: 16px; color: var(--fg2);
}

/* ---------- 一覧 ---------- */
.list { padding-bottom: calc(80px + env(safe-area-inset-bottom)); }
ul { list-style: none; margin: 0; padding: 0; }

.books .book-row {
  display: flex; align-items: center;
  border-bottom: 1px solid var(--line);
  padding-right: 4px;
}
.book-open {
  flex: 1; min-width: 0; text-align: left;
  border: 0; background: transparent;
  padding: 10px 8px 10px 14px;
  display: flex; flex-direction: column;
}
.book-open:active { background: var(--chip); }
.b-title { font-size: 1.05rem; font-weight: 600; overflow-wrap: anywhere; line-height: 1.4; }
.b-count { font-size: .78rem; color: var(--muted); font-weight: 400; }

.entries { border-bottom: 1px solid var(--line); }
.entries:last-child { border-bottom: 0; }
.entries.pinned { background: var(--pin-bg); }

.entry { border-bottom: 1px solid var(--line); }
.entry:last-child { border-bottom: 0; }
.e-row { display: flex; align-items: stretch; }
.e-main {
  flex: 1; min-width: 0;
  padding: 8px 12px 9px 14px;
  cursor: pointer;
}
.entry.open .e-main { padding-bottom: 4px; }
.e-reading {
  font-size: .75rem; color: var(--muted);
  line-height: 1.3; letter-spacing: .02em;
  overflow-wrap: anywhere;
}
.e-reading.none { opacity: .6; font-style: italic; }
.e-name {
  font-size: 1.22rem; font-weight: 700; line-height: 1.3;
  overflow-wrap: anywhere;
}
.e-pin {
  flex: none;
  display: flex; align-items: center;
  padding: 0 12px 0 2px;
  color: var(--accent);
}
.e-pin .ic { width: .95rem; height: .95rem; }
.e-pin + .drag-handle { margin-left: -8px; }
.e-cat {
  display: inline-block; vertical-align: 2px;
  margin-left: 6px; padding: 0 6px;
  border-radius: 5px; background: var(--chip);
  font-size: .68rem; font-weight: 400; color: var(--muted);
  line-height: 1.6; white-space: nowrap;
}
.e-alias { font-size: .8rem; color: var(--muted); overflow-wrap: anywhere; margin-top: 1px; }
.e-desc {
  font-size: .86rem; color: var(--fg2); margin-top: 2px;
  white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.5;
}

/* コンパクト表示 */
.entry.m-compact .e-main { padding-top: 7px; padding-bottom: 7px; }
.e-line { display: flex; flex-wrap: wrap; align-items: baseline; column-gap: 8px; row-gap: 0; }
.e-line .e-name { font-size: 1rem; }
.e-line .e-reading { font-size: .8rem; }
.e-line .e-cat { margin-left: 0; vertical-align: 0; }
.e-more { color: var(--muted); font-size: .8rem; }
.entry.m-compact.open .e-alias { margin-top: 4px; }

/* 読み方のみ表示 */
.entry.m-reading .e-reading { font-size: .8rem; }
.entry.m-reading .e-name { font-size: 1.3rem; }

.e-actions {
  display: flex; flex-wrap: wrap; gap: 6px;
  padding: 2px 12px 10px 14px;
}

.drag-handle {
  flex: none; width: 40px;
  border: 0; background: transparent; color: var(--muted);
  display: flex; align-items: center; justify-content: center;
  touch-action: none; cursor: grab;
}
.entry.dragging {
  position: relative; z-index: 2;
  background: var(--surface);
  box-shadow: var(--shadow);
  opacity: .95;
}

/* ---------- ボタン類 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 4px;
  min-height: 40px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface); color: var(--fg);
  font-size: .92rem; font-weight: 500;
}
.btn:active { filter: brightness(.95); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); font-weight: 600; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; font-weight: 600; }
.btn.sm { min-height: 34px; padding: 0 10px; font-size: .82rem; }
.btn.sm .ic { width: 1.05em; height: 1.05em; }
.btn.icon-only { padding: 0; width: 38px; }
.btn:disabled { opacity: .45; cursor: default; }
.danger-text { color: var(--danger) !important; }

/* ---------- 空のとき・案内 ---------- */
.empty {
  padding: 32px 20px; text-align: center; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; gap: 10px;
}
.empty p { margin: 0; font-size: .9rem; overflow-wrap: anywhere; }
.empty .empty-title { font-size: 1.05rem; color: var(--fg); font-weight: 600; }
.empty .btn { width: 100%; max-width: 280px; }

.banner {
  margin: 8px; padding: 10px 12px;
  border-radius: 10px; background: var(--warn-bg); color: var(--warn-fg);
  font-size: .85rem;
}
.banner p { margin: 0 0 8px; }
.intro {
  margin: 8px; padding: 10px 12px;
  border-radius: 10px; background: var(--accent-soft); color: var(--fg2);
  font-size: .84rem;
}
.intro-title { margin: 0 0 4px; font-weight: 700; color: var(--fg); }
.intro ul { list-style: disc; padding-left: 1.2em; margin: 0 0 8px; }
.intro li { margin: 2px 0; overflow-wrap: anywhere; }
.banner-actions { display: flex; gap: 6px; justify-content: flex-end; }

/* ---------- モーダル ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0, 0, 0, .35);
  display: flex; justify-content: center; align-items: flex-start;
  padding: max(8px, env(safe-area-inset-top)) 6px 8px;
  overflow-y: auto;
  animation: fade .12s ease-out;
}
@keyframes fade { from { opacity: 0; } }
.modal {
  width: 100%; max-width: 480px;
  margin: 2vh 0 0;
  background: var(--surface); color: var(--fg);
  border-radius: 14px; box-shadow: var(--shadow);
  display: flex; flex-direction: column;
  max-height: calc(100dvh - 24px);
}
.modal.small { max-width: 380px; }
.modal.wide { max-width: 560px; }
.modal-head {
  display: flex; align-items: center;
  padding: 4px 4px 0 14px;
}
.modal-head h2 { flex: 1; margin: 0; font-size: 1rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.modal-body { padding: 4px 14px 12px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.modal-foot {
  display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px;
  padding: 10px 14px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
}
.modal-foot .spacer { flex: 1; }
.modal-foot .btn { flex: 1 1 auto; }
.modal-foot .btn.danger-text { flex: 0 0 auto; border-color: transparent; background: transparent; padding: 0 8px; }

.pre { white-space: pre-wrap; overflow-wrap: anywhere; margin: 4px 0 8px; }
.note { font-size: .82rem; color: var(--muted); margin: 6px 0; overflow-wrap: anywhere; }
.note b { color: var(--fg2); }
.note.center { text-align: center; }
.warn {
  font-size: .82rem; margin: 6px 0;
  padding: 6px 10px; border-radius: 8px;
  background: var(--warn-bg); color: var(--warn-fg);
  overflow-wrap: anywhere;
}
.warn.small { font-size: .76rem; padding: 4px 8px; margin: 4px 0; }

.form { display: flex; flex-direction: column; gap: 10px; margin-top: 4px; }
.field { display: flex; flex-direction: column; gap: 3px; }
.field > span { font-size: .8rem; color: var(--muted); font-weight: 600; }
.field small { font-weight: 400; }
.req { font-style: normal; font-size: .7rem; color: var(--danger); margin-left: 4px; }
.field input, .field select, .field textarea, .cat-add input, .bulk {
  width: 100%; min-width: 0;
  padding: 8px 10px;
  border: 1px solid var(--line); border-radius: 9px;
  background: var(--bg); color: var(--fg);
  outline: none;
}
.field input:focus, .field select:focus, .field textarea:focus, .cat-add input:focus, .bulk:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.field textarea, .bulk { resize: vertical; line-height: 1.5; }
.form .warn { margin: -4px 0 0; }
.check { display: flex; align-items: center; gap: 8px; font-size: .9rem; min-height: 36px; }
.check input { width: 20px; height: 20px; accent-color: var(--accent); }

.menu-list { display: flex; flex-direction: column; margin: 0 -14px -12px; }
.menu-list hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }
.menu-item {
  text-align: left; border: 0; background: transparent;
  min-height: 46px; padding: 0 16px; font-size: .95rem;
}
.menu-item:active { background: var(--chip); }

/* 分類の管理 */
.cat-list li { display: flex; align-items: center; gap: 2px; padding: 4px 0; border-bottom: 1px solid var(--line); }
.cat-list .cat-name {
  flex: 1; min-width: 0;
  padding: 6px 8px; border: 1px solid transparent; border-radius: 8px;
  background: transparent; outline: none;
}
.cat-list .cat-name:focus { border-color: var(--accent); background: var(--bg); }
.cat-count { font-size: .75rem; color: var(--muted); min-width: 1.8em; text-align: right; margin-right: 2px; }
.cat-add { display: flex; gap: 6px; margin-top: 12px; }
.cat-add input { flex: 1; }

/* 取り込みプレビュー */
.imp-summary p { margin: 6px 0; font-size: .88rem; }
.preview { border-top: 1px solid var(--line); margin-top: 6px; }
.preview li { border-bottom: 1px solid var(--line); }
.preview li.off { opacity: .45; }
.preview label { display: flex; gap: 10px; align-items: flex-start; padding: 7px 2px; cursor: pointer; }
.preview input[type="checkbox"] { width: 20px; height: 20px; margin-top: 4px; flex: none; accent-color: var(--accent); }
.p-body { display: flex; flex-direction: column; min-width: 0; }
.p-reading { font-size: .72rem; color: var(--muted); line-height: 1.3; }
.p-name { font-weight: 700; overflow-wrap: anywhere; }
.p-name .pin { color: var(--accent); margin-left: 4px; }
.p-name .pin .ic { width: .8em; height: .8em; }
.p-sub { font-size: .76rem; color: var(--muted); overflow-wrap: anywhere; }
.badge {
  display: inline-block; margin-left: 6px; padding: 0 6px; border-radius: 5px;
  font-size: .68rem; font-weight: 600; line-height: 1.6; vertical-align: 1px;
  background: var(--accent-soft); color: var(--accent); white-space: nowrap;
}
.badge.warn-badge { background: var(--warn-bg); color: var(--warn-fg); }

.restore-list li { padding: 10px 0; border-bottom: 1px solid var(--line); }
.r-title { font-weight: 700; overflow-wrap: anywhere; margin-bottom: 4px; }
.restore-list select {
  width: 100%; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg);
}

/* 設定 */
.set { padding: 8px 0; border-bottom: 1px solid var(--line); }
.set h3 { font-size: .85rem; margin: 0 0 6px; color: var(--fg2); }
.seg { display: flex; flex-wrap: wrap; gap: 4px; background: var(--chip); padding: 3px; border-radius: 10px; }
.seg label { flex: 1 1 auto; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span {
  display: flex; align-items: center; justify-content: center;
  min-height: 34px; padding: 0 8px; border-radius: 8px;
  font-size: .85rem; cursor: pointer; white-space: nowrap;
}
.seg input:checked + span { background: var(--surface); font-weight: 600; box-shadow: 0 1px 3px rgba(0, 0, 0, .15); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); }
.btn-col { display: flex; flex-direction: column; gap: 6px; }

/* トースト */
#toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); z-index: 200;
  transform: translate(-50%, 20px);
  max-width: calc(100% - 24px);
  padding: 9px 16px; border-radius: 10px;
  background: var(--fg); color: var(--bg);
  font-size: .85rem; text-align: center;
  opacity: 0; pointer-events: none;
  transition: opacity .18s, transform .18s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* マウス操作の端末ではホバーを少しだけ */
@media (hover: hover) {
  .book-open:hover, .menu-item:hover, .icon-btn:hover { background: var(--chip); }
  .e-main:hover { background: color-mix(in srgb, var(--chip) 45%, transparent); }
}
