:root {
  --bg: #fcfaf2;
  --text: #333;
  --border: #000;
  --panel: #fff;
  --input-bg: #fff;
  --hl: rgba(0, 0, 0, 0.05);
  --editor-size: 18px;
  --ui-font: system-ui, -apple-system, 'Segoe UI', sans-serif;
  --writing-font: 'Sawarabi Mincho', serif;
  --text-xs: 11px;
  --text-sm: 12px;
  --text-md: 13px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 10px;
  --icon-size-base: 18px;
  --control-height: 36px;
}
[data-theme="dark"] {
  --bg: #121212;
  --text: #e0e0e0;
  --border: #444;
  --panel: #1e1e1e;
  --input-bg: #2a2a2a;
  --hl: rgba(255, 255, 255, 0.08);
}
body { margin: 0; font-family: var(--ui-font); background: var(--bg); color: var(--text); display: flex; flex-direction: column; height: 100dvh; overflow: hidden; }
main { display: flex; flex: 1; min-height: 0; overflow: hidden; position: relative; }
#editor-container { flex: 1; display: flex; flex-direction: column; position: relative; width: 100%; }
#editor, #memo-area { font-family: var(--writing-font); }
#editor { flex: 1; padding: 40px 10%; font-size: var(--editor-size); line-height: 1.7; border: none; outline: none; background: transparent; color: var(--text); resize: none; overflow-y: auto; width: 100%; box-sizing: border-box; z-index: 2; }
#line-highlight { position: absolute; left: 0; width: 100%; height: 1.7em; background: var(--hl); pointer-events: none; z-index: 1; transition: top 0.08s; display: none; }
.side-panel { width: 0; background: var(--panel); border-left: 1px solid var(--border); transition: 0.2s; display: flex; flex-direction: column; overflow: hidden; z-index: 50; min-width: 0; }
.side-panel.open { width: 350px; }
.panel-content { padding: 20px; flex: 1; overflow-y: auto; box-sizing: border-box; -webkit-overflow-scrolling: touch; }
.config-list { border: 1px solid var(--border); margin-bottom: 10px; font-size: 12px; background: var(--input-bg); min-height: 40px; }
.config-item { display: flex; border-bottom: 1px solid var(--border); align-items: center; padding: var(--space-2) var(--space-3); gap: var(--space-2); color: var(--text); font-size: var(--text-sm); }
.fav-btn {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--border);
  background: var(--input-bg);
  display: inline-flex;
  align-items: center;
  justify-content: flex-start;
  gap: 8px;
  padding: 8px 10px;
  box-sizing: border-box;
}
#favorite-actions {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 8px;
  align-items: stretch;
  padding: 8px;
}

.favorite-action-item {
  position: relative;
  min-width: 0;
}

.fav-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.config-input-group { display: flex; gap: 5px; margin-bottom: 10px; }
.config-input-group input, .config-input-group select, .config-input-group textarea { flex: 1; min-width: 0; padding: 6px; border: 1px solid var(--border); font-size: 11px; background: var(--input-bg); color: var(--text); border-radius: 0; }
.tab-bar { display: flex; background: var(--bg); border-bottom: 1px solid var(--border); overflow-x: auto; }
.tab { padding: 10px 14px; font-size: var(--text-sm); cursor: pointer; border-right: 1px solid var(--border); white-space: nowrap; flex: 0 0 auto; }
.tab.active { background: var(--panel); font-weight: bold; border-bottom: 2px solid var(--text); }
.chapter-list { border: 1px solid var(--border); margin-bottom: 10px; min-height: 80px; }
.chapter-item { padding: 10px; font-size: 13px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; }
.chapter-item.active { background: var(--bg); font-weight: bold; }
.chapter-item { align-items: flex-start; gap: 6px; }
.chapter-item > div:first-child { min-width: 0; }
.chapter-item > div:first-child button { width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chapter-item button { flex-shrink: 0; }
.chapter-tag-list { margin-top: 4px; display: flex; flex-wrap: wrap; gap: 4px; }
.chapter-tag-badge { display: inline-block; background: var(--input-bg); border: 1px solid var(--border); border-radius: 999px; padding: 1px 8px; font-size: 11px; line-height: 1.4; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
button { color: var(--text); background: none; border: none; border-radius: 0; font-size: var(--text-sm); padding: 0; }
.material-icons { font-size: var(--icon-size-base); line-height: 1; vertical-align: middle; }
footer { background: var(--panel); border-top: 1px solid var(--border); display: flex; justify-content: space-between; min-height: 45px; flex-shrink: 0; padding-bottom: env(safe-area-inset-bottom); z-index: 100; }
.btn-group { display: flex; overflow-x: auto; }
footer button { height: 45px; min-width: 45px; background: transparent; border: none; border-right: 1px solid var(--border); color: var(--text); cursor: pointer; display: flex; align-items: center; justify-content: center; }
.sys-btn { width: 100%; border: 1px solid var(--border); height: var(--control-height); margin-bottom: var(--space-1); font-size: var(--text-sm); background: var(--input-bg); color: var(--text); cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 6px; padding: 0 var(--space-2); }
h4 { margin: 15px 0 10px 0; font-size: 14px; border-bottom: 2px solid var(--border); padding-bottom: 5px; display: flex; justify-content: space-between; }
#preview-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: var(--bg); color: var(--text); display: none; overflow-y: auto; padding: 60px 20%; z-index: 200; box-sizing: border-box; line-height: 1.7; }
#toast { position: fixed; left: 50%; bottom: calc(58px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(12px); background: var(--panel); color: var(--text); border: 1px solid var(--border); padding: 10px 14px; font-size: 12px; max-width: min(90vw, 520px); opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 300; }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.success { border-color: #2e7d32; }
#toast.error { border-color: #c62828; }
.field {
  flex: 1;
  width: 100%;
  border: 1px solid var(--border);
  background: var(--input-bg);
  color: var(--text);
  border-radius: 0;
  box-sizing: border-box;
  padding: 8px 10px;
}
.scope-switch { font-size: 10px; padding: 10px; background: var(--bg); border-bottom: 1px solid var(--border); display: flex; gap: 15px; }
.scope-switch span { cursor: pointer; opacity: 0.5; font-weight: 400; }
.scope-switch span.active { opacity: 1; font-weight: 700; }
.memo-add-row { display: flex; border-bottom: 1px solid var(--border); }
.input-plain { flex: 1; border: none; padding: var(--space-3); font-size: var(--text-sm); background: var(--input-bg); color: var(--text); }
.icon-btn { border: none; background: var(--panel); border-left: 1px solid var(--border); width: 50px; cursor: pointer; color: var(--text); display: flex; align-items: center; justify-content: center; padding: 0; }
.memo-textarea { flex: 1; width: 100%; border: none; outline: none; padding: 14px; background: transparent; color: var(--text); resize: none; font-size: var(--text-md); line-height: 1.7; box-sizing: border-box; }
.memo-attach-wrap { border-top: 1px solid var(--border); padding: 8px; }
.attach-btn { margin: 0 0 6px 0; }
.floating-panel { position: fixed; top: 12px; right: 12px; width: min(84vw, 290px); border: 1px solid var(--border); background: var(--panel); padding: 8px; z-index: 280; display: none; border-radius: 0; box-shadow: none; }
.attachment-preview-media { max-width: 100%; max-height: 220px; display: block; margin: 0 auto; }
.preview-text { white-space: pre-wrap; overflow: auto; max-height: 220px; }

.memo-tab {
  flex: 0 0 auto;
  padding: 9px 12px;
  border-right: 1px solid var(--border);
  white-space: nowrap;
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.memo-tab-btn {
  flex: 0 0 auto;
  width: 36px;
  height: 36px;
  border-right: 1px solid var(--border);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

#quick-buttons button {
  min-width: 56px;
  padding: 0 10px;
  white-space: nowrap;
}

button .material-icons,
.sys-btn .material-icons,
footer button .material-icons,
.icon-btn .material-icons,
.icon-only-btn .material-icons {
  font-size: 20px;
}


.search-panel-head { display:flex; justify-content:space-between; align-items:center; margin-bottom:8px; }
.search-row { display:flex; align-items:center; gap:6px; margin-bottom:6px; }
.search-actions { display:grid; grid-template-columns:1fr; gap:6px; margin-top:8px; }
.search-options { display:flex; gap:10px; align-items:center; margin-top:8px; font-size:11px; }
.ai-panel-content { display:flex; flex-direction:column; overflow:hidden; min-height:0; }
.ai-chat-log { flex:1; overflow-y:auto; min-height:0; margin-bottom:6px; -webkit-overflow-scrolling: touch; }
.ai-composer { border-top:1px solid var(--border); padding-top:8px; margin-top:0; background:var(--panel); }
.ai-scope-row { margin-top: 6px; }
.md-content { white-space:normal; line-height:1.6; }
.md-content h1, .md-content h2, .md-content h3, .md-content h4, .md-content h5, .md-content h6 { font-size:1rem; margin:0.6em 0; }
.md-content ul, .md-content ol { padding-left: 1.2em; margin: 0.4em 0; }
.md-content p { margin:0.4em 0; }
.md-content code { background: var(--bg); padding: 0 4px; border-radius: 0; }
.md-content pre { background: var(--bg); border:1px solid var(--border); padding:8px; overflow:auto; }
.md-content table { width:100%; border-collapse:collapse; margin:0.45em 0; font-size:12px; }
.md-content th, .md-content td { border:1px solid var(--border); padding:5px 6px; text-align:left; vertical-align:top; }
.md-content thead { background: var(--bg); }


.chapter-item button, .memo-tab-btn, .icon-btn, .icon-only-btn { min-width: 32px; min-height: 32px; }
.chapter-item .material-icons, .memo-tab-btn .material-icons { font-size: 16px; }


.compact-search-panel .field { padding: 6px 8px; font-size: 12px; }
.compact-search-panel .material-icons { font-size: 17px; }
.compact-icon-btn { width: 32px; border-left: none; }
.compact-actions { grid-template-columns: repeat(4, 1fr); gap: 4px; }
.icon-only-btn { height: 30px; min-width: 30px; padding: 0; }
.icon-only-btn .material-icons { font-size: 18px; }
.ai-inner-tabs { margin: 0 0 8px 0; border-left:1px solid var(--border); border-right:1px solid var(--border); border-top:1px solid var(--border); }
.ai-inner-tabs .tab { flex:1; text-align:center; }
.ai-sub-panel { display:flex; flex-direction:column; flex:1; min-height:0; overflow:hidden; }
.ai-chat-log .config-item { margin-top: 4px; }
.ai-chat-input-row { display:flex; gap:6px; align-items:flex-end; }
.ai-chat-input-row .icon-only-btn { width:36px; min-width:36px; height:36px; border:1px solid var(--border); flex:0 0 36px; }
.ai-send-btn .material-icons { font-size:18px; }

#ai-suggestions { flex:1; overflow-y:auto; min-height:0; -webkit-overflow-scrolling: touch; }

/* --- Progress Toast --- */
.toast-progress-wrap { margin-top: 6px; }
.toast-progress-bar { width: 100%; height: 6px; background: var(--input-bg); border: 1px solid var(--border); overflow: hidden; }
.toast-progress-fill { height: 100%; background: var(--text); transition: width 0.15s ease; }
.toast-progress-label { font-size: 10px; margin-top: 2px; opacity: 0.7; }
#toast.progress { pointer-events: auto; opacity: 1; transform: translateX(-50%) translateY(0); }

/* --- GitHub Snapshot List --- */
.gh-snapshot-list { max-height: 260px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.gh-snapshot-item { display: flex; align-items: flex-start; gap: 8px; padding: 8px 10px; border-bottom: 1px solid var(--border); font-size: 11px; }
.gh-snapshot-item .snapshot-info { flex: 1; min-width: 0; }
.gh-snapshot-item .snapshot-date { font-weight: bold; }
.gh-snapshot-item .snapshot-detail { opacity: 0.7; font-size: 10px; }
.gh-snapshot-actions { display: flex; gap: 4px; flex-shrink: 0; }

/* --- Diff Dialog --- */
.diff-overlay { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.5); z-index: 10000; display: flex; align-items: center; justify-content: center; }
.diff-dialog { background: var(--panel); border: 1px solid var(--border); padding: 20px; max-width: 420px; width: 90vw; max-height: 80vh; overflow-y: auto; }
.diff-dialog h3 { margin: 0 0 12px 0; font-size: 14px; }
.diff-item { display: flex; align-items: center; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--border); font-size: 12px; }
.diff-item label { flex: 1; cursor: pointer; display: flex; align-items: center; gap: 6px; }
.diff-dialog-actions { display: flex; gap: 8px; margin-top: 12px; }

#editor,
.panel-content,
.ai-chat-log,
#ai-suggestions,
.gh-snapshot-list,
#preview-overlay {
  overscroll-behavior: contain;
}

@media (max-width: 900px) {
  #editor { padding: 28px 6vw; }
  .side-panel.open { width: min(100vw, 360px); }
}
