/* ===== 個人 Wiki — 全站樣式 ===== */
/* === ADMIN MAIN OVERRIDE (must come before main {} below) ===
   Sidebar width comes from --admin-side (defined in :root). */
main.admin-main {
  max-width: none !important;
  padding: 0 !important;
  display: block !important;
  grid-template-columns: none !important;
  gap: 0 !important;
  margin: 0 0 0 var(--admin-side, 220px) !important;
}
@media (max-width: 800px) {
  main.admin-main { margin-left: 0 !important; }
}

:root {
  --bg: #faf8f3;
  --fg: #2b2b2b;
  --muted: #777;
  --line: #e3ddd0;
  --primary: #6b4423;
  --primary-soft: #efe6d6;
  --accent: #a05a2c;
  --danger: #c0392b;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.04);

  /* Page layout — single source of truth.
     --page-max: outer container width on widescreen monitors (responsive,
     auto-fits narrower viewports thanks to max-width + margin auto).
     --admin-side: width of fixed admin sidebar.
     --reading-max: ideal max line-length for prose (article body). */
  /* 80% of viewport, capped at 2400px to avoid silly-wide on ultra-wide
     monitors. The 80vw scales naturally as user resizes the window. */
  --page-max: min(80vw, 2400px);
  --admin-side: 220px;
  --reading-max: 920px;
}

* { box-sizing: border-box; max-width: 100%; }
html, body {
  margin: 0; padding: 0;
  font-family: -apple-system, "PingFang TC", "Noto Sans TC",
               "Microsoft JhengHei", "Helvetica Neue", Arial, sans-serif;
  background: var(--bg); color: var(--fg);
  line-height: 1.65; overflow-x: hidden;
}
img, video, iframe { max-width: 100%; }

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ===== 頂端列 ===== */
.topbar {
  position: sticky; top: 0; z-index: 50;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px;
  background: #fff; border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow);
}
.brand { flex-shrink: 0; white-space: nowrap; }
.brand a { font-weight: 700; font-size: 17px; color: var(--primary); }
.searchbar { flex: 1; min-width: 0; max-width: 160px; display: flex; gap: 6px; }
.topbar-ask {
  flex-shrink: 0; font-size: 18px; line-height: 1;
  border: 0; background: none; cursor: pointer; padding: 4px;
  border-radius: 6px; color: var(--muted);
}
.topbar-ask:hover { background: var(--primary-soft); }
@media (max-width: 600px) {
  .topbar { padding: 8px 12px; gap: 8px; }
  .brand a { font-size: 15px; }
  .searchbar { max-width: 130px; }
  .searchbar input { padding: 5px 10px; font-size: 13px; }
  .searchbar button { width: 30px; height: 30px; font-size: 14px; }
  .topbar-admin { font-size: 17px; }
}
.searchbar input {
  flex: 1; padding: 6px 12px; border: 1px solid var(--line);
  border-radius: 999px; background: #fff; font-size: 14px;
}
.searchbar button {
  padding: 0; width: 34px; height: 34px; border: 0; border-radius: 999px;
  background: var(--primary); color: #fff; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; flex-shrink: 0;
}
.topbar-admin {
  font-size: 20px; line-height: 1; color: var(--muted);
  text-decoration: none; padding: 4px; border-radius: 6px;
}
.topbar-admin:hover { color: var(--primary); background: var(--primary-soft); }
.topbar nav { display: flex; gap: 12px; align-items: center; }
.topbar nav a, .topbar nav button {
  padding: 6px 12px; font-size: 14px;
  border: 1px solid var(--line); background: #fff; color: var(--fg);
  border-radius: 6px; cursor: pointer;
}
.topbar nav a:hover, .topbar nav button:hover { background: var(--primary-soft); }
.topbar nav .primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.badge {
  display: inline-block; min-width: 18px; padding: 1px 6px;
  background: var(--danger); color: #fff; border-radius: 10px;
  font-size: 11px; line-height: 1.4; margin-left: 4px;
  vertical-align: middle;
}

/* ===== 版面 ===== */
main {
  max-width: var(--page-max); margin: 0 auto; padding: 24px;
  display: grid; grid-template-columns: 240px 1fr; gap: 28px;
}
main > * { min-width: 0; }
.cat-tree-mobile { display: none; }
@media (max-width: 800px) {
  main { grid-template-columns: 1fr; padding: 12px; }
}

/* ===== 側欄 ===== */
.sidebar { font-size: 14px; }
.stats {
  display: grid; grid-template-columns: 1fr 1fr;
  gap: 1px; margin-bottom: 16px;
  background: var(--line); padding: 1px; border-radius: 12px; box-shadow: var(--shadow);
  overflow: hidden; border: 1px solid var(--line);
}
.stat-cell {
  text-align: center; background: #fff; padding: 14px 8px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  transition: background 0.15s;
}
.stat-cell:hover { background: var(--primary-soft, #f5efe6); }
.stat-icon { font-size: 15px; line-height: 1; opacity: 0.75; margin-bottom: 2px; }
.stats strong { font-size: 24px; color: var(--primary); font-weight: 700; line-height: 1.1; }
.stat-label { color: var(--muted); font-size: 12px; }

.sidebar h4 {
  margin: 16px 0 6px; font-size: 13px;
  color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px;
}
.taglist { list-style: none; padding: 0; margin: 0; }
.taglist li { margin: 2px 0; }
.taglist a {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 10px; border-radius: 999px;
  background: #fff; border: 1px solid var(--line);
  color: var(--fg); font-size: 13px;
}
.taglist a:hover, .taglist a.on {
  background: var(--primary-soft); border-color: var(--accent);
}
.taglist a em { color: var(--muted); font-style: normal; font-size: 11px; }
.tagcloud { display: flex; flex-wrap: wrap; gap: 4px; }

/* Hierarchical category tree */
.cat-tree {
  font-size: 13px; max-height: 360px; overflow-y: auto;
  padding-right: 4px; scrollbar-width: thin;
}
.cat-tree::-webkit-scrollbar { width: 7px; }
.cat-tree::-webkit-scrollbar-track { background: transparent; }
.cat-tree::-webkit-scrollbar-thumb { background: var(--line); border-radius: 4px; }
.cat-tree::-webkit-scrollbar-thumb:hover { background: var(--muted); }
.cat-root { margin-bottom: 4px; }
.cat-node { margin: 2px 0; }
.cat-node summary {
  list-style: none;
  cursor: pointer;
  padding: 2px 0;
  display: flex;
  align-items: center;
  gap: 4px;
}
.cat-node summary::before {
  content: "▶";
  font-size: 9px;
  color: var(--muted);
  transition: transform 0.15s;
  display: inline-block;
}
.cat-node[open] > summary::before { transform: rotate(90deg); }
.cat-node summary::-webkit-details-marker { display: none; }
.cat-children {
  list-style: none;
  padding-left: 14px;
  margin: 2px 0 4px;
  border-left: 1px dashed var(--line);
}
.cat-children li { margin: 2px 0; }
.cat-link {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  color: var(--fg);
  text-decoration: none;
  font-size: 13px;
}
.cat-link:hover { background: var(--primary-soft); text-decoration: none; }
.cat-link.on { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.cat-link em { color: var(--muted); font-style: normal; font-size: 11px; margin-left: 4px; }
.cat-link.leaf { display: block; padding-left: 14px; }

@media (max-width: 800px) {
  /* Sidebar moves ABOVE the article list (was: order 2 / stacked at the very
     bottom) and its normally-vertical blocks (stats / category tree / tags)
     render as compact horizontal scroll strips instead. */
  .sidebar { order: -1; }

  .stats { grid-template-columns: repeat(4, 1fr); margin-bottom: 10px; }
  .stat-cell { padding: 8px 4px; }
  .stat-icon { font-size: 13px; }
  .stats strong { font-size: 16px; }
  .stat-label { font-size: 10px; }

  .sidebar h4 { margin: 8px 0 6px; }
  .cat-tree { display: none; }
  .cat-select {
    display: block; width: 100%; padding: 8px 10px; font-size: 14px;
    border: 1px solid var(--line); border-radius: 8px; background: #fff;
    color: var(--fg); appearance: auto; margin-bottom: 4px;
  }

  .taglist.tagcloud { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 6px; -webkit-overflow-scrolling: touch; }
  .taglist.tagcloud li { flex: 0 0 auto; margin: 0; }
}

/* ===== 文章列表 ===== */
.list-head {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 16px;
}
.list-head h2 { margin: 0; }
.list-head h2 small { color: var(--muted); font-weight: normal; font-size: 14px; }
.list-head select { padding: 6px 10px; border-radius: 6px; border: 1px solid var(--line); }

.cards { list-style: none; padding: 0; margin: 0; display: grid; gap: 14px; min-width: 0; }
.card {
  display: grid; grid-template-columns: 160px 1fr; gap: 16px;
  background: #fff; padding: 14px; border-radius: 10px; box-shadow: var(--shadow);
  transition: transform 0.1s ease; min-width: 0; overflow: hidden;
}
.card:hover { transform: translateY(-1px); }
.card .thumb { display: block; overflow: hidden; border-radius: 6px; height: 110px; background: #f3eee0; }
.card .thumb img { width: 100%; height: 100%; object-fit: cover; }
/* User requested: reserve cover slot even when there's no image, so cards
   line up vertically. The placeholder is just a soft tinted box. */
.card h3 { margin: 0 0 4px; font-size: 17px; }
.card .dates {
  color: var(--muted); font-size: 12px; margin-bottom: 6px;
  display: flex; flex-wrap: wrap; align-items: center; gap: 4px 6px;
}
.card .dates .status {
  background: #fffbe6; color: #b8860b; padding: 1px 6px; border-radius: 4px;
}
.card .dates .cat-chip {
  background: var(--primary-soft); color: var(--primary); padding: 1px 6px;
  border-radius: 4px; text-decoration: none; white-space: nowrap;
}
.card .dates .cat-chip:hover { background: var(--primary); color: #fff; }
.card .excerpt { margin: 0; color: #555; font-size: 14px; }

@media (max-width: 600px) {
  .card { grid-template-columns: 100px 1fr; gap: 10px; }
  .card .thumb { height: 80px; }
}

.empty {
  padding: 40px; text-align: center; color: var(--muted);
  background: #fff; border-radius: 10px; border: 2px dashed var(--line);
}

.pager {
  display: flex; gap: 12px; justify-content: center; padding: 24px;
  align-items: center;
}

/* ===== 文章頁 ===== */
.post {
  grid-column: 1 / -1;
  width: 100%;
  max-width: 100%;
  margin: 0 auto;
  background: #fff; padding: 32px; border-radius: 10px; box-shadow: var(--shadow);
  box-sizing: border-box;
}
@media (max-width: 800px) {
  .post { padding: 14px; }
}
.post h1 { margin: 12px 0; font-size: 28px; line-height: 1.3; }
.post-actions {
  display: flex; justify-content: space-between; align-items: center;
  flex-wrap: wrap; gap: 8px; margin-bottom: 12px;
}
/* Nav links (回列表 + 上一篇 + 下一篇) always stay in one flex row */
.post-nav-links { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.post-actions > .post-actions-group { display: flex; align-items: center; gap: 6px; }
.post-actions a, .post-actions button {
  padding: 6px 12px; border-radius: 6px; border: 1px solid var(--line);
  background: #fff; color: var(--fg); cursor: pointer; font-size: 13px;
}
.post-actions .primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.post-actions .danger { background: #fff; color: var(--danger); border-color: var(--danger); }
.post-prevnext { display: flex; gap: 8px; }
.post-actions-cat { display: flex; align-items: center; gap: 6px; }
.post-actions-cat select { padding: 6px 10px; font-size: 13px; }
.post-actions-cat #quickCatStatus { font-size: 12px; color: var(--muted); white-space: nowrap; }
.post-actions-buttons { display: flex; align-items: center; gap: 6px; }
@media (max-width: 700px) {
  .post-actions { flex-direction: column; align-items: flex-start; gap: 10px; }
  .post-actions > .post-actions-group {
    flex-direction: column; align-items: flex-start; gap: 10px; width: 100%;
  }
  .post-actions-cat { width: 100%; justify-content: center; }
  .post-actions-cat select { flex: 1 1 auto; min-width: 0; max-width: 260px; }
  .post-actions-buttons { flex-wrap: wrap; justify-content: center; }
}
.post-prevnext-bottom {
  display: flex; justify-content: center; margin-top: 28px; padding-top: 16px;
  border-top: 1px dashed var(--line);
}

.post-meta {
  color: var(--muted); font-size: 13px; display: flex; gap: 14px; flex-wrap: wrap;
  margin-bottom: 16px;
}
.status-publish { color: green; }
.status-draft   { color: orange; }
.status-private { color: gray; }

.cover { margin: 16px 0; }
.cover img { width: 100%; max-height: 380px; object-fit: cover; border-radius: 8px; }

.post-tags { margin: 12px 0 24px; display: flex; flex-wrap: wrap; gap: 6px; }
.tag {
  padding: 2px 10px; border-radius: 999px; background: var(--primary-soft);
  color: var(--primary); font-size: 12px;
}
.tag.cat { background: #e6f0e6; color: #3a6b3a; }

.post-body {
  font-size: 16px; line-height: 1.85;
}
.post-body img { max-width: 100%; height: auto; border-radius: 6px; margin: 12px 0; }
/* #8: emoji-sized inline images (FB exports use these for 😀 reactions) get
   text-baseline alignment + sized to roughly one line of body text.
   The .inline-emoji class is added by article.html JS after each img loads,
   based on natural dimensions < 80px. */
.post-body img.inline-emoji {
  display: inline;
  width: auto;
  height: 1.2em;
  max-width: none;
  border-radius: 0;
  margin: 0 1px;
  vertical-align: -0.2em;   /* bottom-ish — matches text baseline visually */
}
.post-body p { margin: 0 0 1em; }
/* Collapse empty paragraphs so blank-line spacers don't create double gaps */
.post-body p:empty,
.post-body p:has(> br:only-child) { display: none; }
.post-body h2, .post-body h3, .post-body h4 { margin: 1.4em 0 0.6em; }
.post-body blockquote {
  border-left: 4px solid var(--primary-soft); padding: 4px 16px;
  color: #555; background: #fcfaf3; margin: 1em 0;
}
.post-body pre {
  background: #2b2b2b; color: #f5f5f5; padding: 12px;
  border-radius: 6px; overflow-x: auto;
}
.post-body code { background: #f0ece2; padding: 1px 6px; border-radius: 3px; }

/* ===== 編輯表單 =====
   Same outer width as .post / homepage — fills the main area, responsive
   padding shrinks on narrow viewports. */
.editor {
  grid-column: 1 / -1;
  width: 100%;
  max-width: 100%;
  margin: 0 auto;
  background: #fff; padding: 28px; border-radius: 10px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 14px;
  box-sizing: border-box;
}
@media (max-width: 800px) {
  .editor { padding: 14px; }
}
/* Quill mount fills the editor card */
.editor #editor { width: 100%; }
.editor .ql-toolbar, .editor .ql-container { width: 100%; box-sizing: border-box; }
.editor textarea[name="content"] { min-height: 480px; }
.editor h2 { margin: 0; }
.editor label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.editor input, .editor textarea, .editor select {
  padding: 10px 12px; font-size: 14px; border: 1px solid var(--line);
  border-radius: 6px; background: #fff; color: var(--fg); width: 100%;
  font-family: inherit;
}
.editor textarea { font-family: ui-monospace, "Cascadia Code", "Fira Mono", monospace; }
.editor .row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.editor .form-actions { display: flex; gap: 12px; align-items: center; margin-top: 8px; }
.editor .primary {
  padding: 10px 24px; background: var(--primary); color: #fff;
  border: 0; border-radius: 6px; cursor: pointer; font-size: 14px;
}
.upload-row { display: flex; gap: 12px; align-items: center; font-size: 14px; }
.hint { color: var(--muted); font-size: 12px; }
@media (max-width: 600px) {
  .editor .row { grid-template-columns: 1fr; gap: 10px; }
  .editor { padding: 12px; gap: 10px; }
  .editor-cover-row { flex-wrap: wrap; }
  .editor-cover-row input { min-width: 0; }
  .editor-cover-row .cover-btns { display: flex; gap: 6px; flex-shrink: 0; }
  .editor-mode-bar { flex-wrap: wrap; gap: 6px; }
  #editor { min-height: 300px !important; }
}

/* ===== 圖庫 ===== */
.image-section { grid-column: 1 / -1; }
.image-grid {
  display: grid; gap: 8px;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
}
.image-grid.small { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); }
.img-tile {
  position: relative; display: block; overflow: hidden;
  border-radius: 6px; background: #eee; aspect-ratio: 1; box-shadow: var(--shadow);
}
.img-tile img { width: 100%; height: 100%; object-fit: cover; }
.img-link {
  position: absolute; bottom: 0; left: 0; right: 0;
  background: rgba(0,0,0,0.6); color: #fff; padding: 4px;
  font-size: 11px; text-align: center; opacity: 0; transition: opacity 0.15s;
}
.img-tile:hover .img-link { opacity: 1; }
.img-link a { color: #fff; text-decoration: none; }

/* ===== 搜尋結果 ===== */
.results { grid-column: 1 / -1; max-width: 100%; margin: 0 auto; }
.result-list { list-style: none; padding: 0; margin: 0; }
.result-list li {
  background: #fff; padding: 16px; border-radius: 8px;
  margin-bottom: 10px; box-shadow: var(--shadow);
}
.result-list .title { font-size: 17px; font-weight: 600; }
.result-list .meta { color: var(--muted); font-size: 12px; margin: 4px 0; }
.result-list .snippet { margin: 6px 0 0; color: #444; font-size: 14px; }
.result-list mark { background: #fff3a3; padding: 0 2px; border-radius: 2px; }
.score { color: var(--accent); font-family: monospace; }

/* ===== 對話框 ===== */
dialog {
  border: 0; border-radius: 12px; padding: 24px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.2); max-width: 540px; width: 90%;
}
dialog::backdrop { background: rgba(0,0,0,0.4); }
dialog h3 { margin-top: 0; }
dialog .dlg-foot { margin-top: 16px; display: flex; justify-content: flex-end; gap: 10px; }
dialog button { padding: 8px 16px; border-radius: 6px; border: 1px solid var(--line); background: #fff; cursor: pointer; }
dialog button.primary { background: var(--primary); color: #fff; border-color: var(--primary); }
dialog input, dialog textarea {
  width: 100%; padding: 10px; border: 1px solid var(--line); border-radius: 6px;
  font-family: inherit; font-size: 14px;
}
.server-files { font-size: 13px; max-height: 160px; overflow-y: auto; }
.server-files .item {
  display: flex; justify-content: space-between; padding: 6px;
  border-bottom: 1px solid var(--line); cursor: pointer;
}
.server-files .item:hover { background: var(--primary-soft); }
.server-files .item.selected { background: var(--primary-soft); }
.log {
  margin-top: 12px; max-height: 200px; overflow-y: auto;
  font-family: monospace; font-size: 12px; color: #555;
  background: #faf8f3; padding: 8px; border-radius: 4px; display: none;
}
.log.on { display: block; }
.ask-answer {
  margin-top: 12px; max-height: 320px; overflow-y: auto;
  background: #faf8f3; padding: 12px; border-radius: 6px;
  font-size: 14px; line-height: 1.7; white-space: pre-wrap; display: none;
}
.ask-answer.on { display: block; }
.ask-answer .sources { margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--line); font-size: 12px; }
