/* Layout: top nav bar; home = full-height pannable tree; other pages = centered reading column (max 760px). */
:root {
  --bg: #f3f4ef; --surface: #ffffff; --ink: #1d2621; --muted: #66706a; --line: #c9cfc6;
  --accent: #2f6b4f; --accent-ink: #ffffff; --accent-soft: #e2ede6; --danger: #b23a2e; --scrim: rgba(10,20,15,.45);
  --shadow: 0 1px 2px rgba(20,40,30,.08), 0 4px 14px rgba(20,40,30,.06);
  --f-display: "Noto Serif Thai", Georgia, serif;
  --f-body: "IBM Plex Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif;
  --gutter-l: max(16px, env(safe-area-inset-left, 0px));
  --gutter-r: max(16px, env(safe-area-inset-right, 0px));
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121815; --surface: #1b231f; --ink: #e6ebe7; --muted: #9aa59f; --line: #38443e;
    --accent: #7cc19c; --accent-ink: #0e1a14; --accent-soft: #22362b; --danger: #e57a6d; --scrim: rgba(0,0,0,.6);
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 14px rgba(0,0,0,.3);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.6 var(--f-body); display: flex; flex-direction: column;
  padding-top: env(safe-area-inset-top, 0px); }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
img, video { max-width: 100%; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }
h1, h2, h3 { font-family: var(--f-display); line-height: 1.3; text-wrap: balance; margin: 0; }

/* top bar */
.topbar { display: flex; align-items: center; gap: 8px 20px; flex-wrap: wrap; padding: 10px var(--gutter-r) 10px var(--gutter-l);
  background: var(--surface); border-bottom: 1px solid var(--line); position: relative; z-index: 5; }
.brand { font-family: var(--f-display); font-weight: 700; font-size: 1.25rem; color: var(--ink); text-decoration: none; }
.brand-wrap { display: flex; align-items: center; gap: 6px; min-width: 0; }
.brand { overflow-wrap: anywhere; }
.brand-edit { width: 30px; height: 30px; border-radius: 8px; border: 1px solid var(--line); background: transparent; color: var(--muted); font-size: .95rem; display: grid; place-items: center; flex: none; }
.brand-edit:hover { color: var(--accent); border-color: var(--accent); }
.site-card { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 18px; }
.site-name { font-family: var(--f-display); font-weight: 700; font-size: 1.1rem; }
.nav { display: flex; gap: 4px; flex-wrap: wrap; margin-left: auto; }
.nav a { padding: 6px 12px; border-radius: 10px; color: var(--ink); text-decoration: none; white-space: nowrap; }
.nav a:hover { background: var(--accent-soft); }
.nav a.active { background: var(--accent); color: var(--accent-ink); }
@media (max-width: 560px) { .nav { margin-left: 0; width: 100%; } .nav a { padding: 5px 10px; font-size: .93rem; } }

main { flex: 1; min-height: 0; display: flex; flex-direction: column; outline: none; }
.page { width: 100%; max-width: 760px; margin: 0 auto; padding: 28px var(--gutter-r) 64px var(--gutter-l); display: flex; flex-direction: column; gap: 20px; }
.page-head { display: flex; align-items: flex-end; gap: 12px; flex-wrap: wrap; justify-content: space-between; }
.page-head h1 { font-size: 1.6rem; }
.lede { color: var(--muted); margin: 4px 0 0; }
.muted { color: var(--muted); }

/* buttons + forms */
.btn { border: 1px solid transparent; border-radius: 12px; padding: 9px 16px; font-weight: 600; background: var(--accent); color: var(--accent-ink);
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; text-decoration: none; }
.btn:disabled { opacity: .55; cursor: default; }
.btn.ghost { background: transparent; border-color: var(--line); color: var(--ink); }
.btn.ghost:hover { border-color: var(--accent); }
.btn.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn.danger.solid { background: var(--danger); color: var(--surface); }
.btn.small { padding: 5px 10px; font-size: .88rem; border-radius: 9px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row > .btn.grow { flex: 1 1 auto; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; color: var(--muted); }
.field input, .field textarea, .field select { border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 10px 12px; font-size: 1rem; color: var(--ink); width: 100%; }
.field textarea { min-height: 90px; resize: vertical; }
.check { display: flex; align-items: center; gap: 8px; }
.form { display: flex; flex-direction: column; gap: 14px; }
.error { color: var(--danger); font-size: .9rem; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 20px; box-shadow: var(--shadow); }
.narrow { max-width: 420px; width: 100%; margin: 0 auto; }

/* tree */
.tree-bar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; padding: 10px var(--gutter-r) 10px var(--gutter-l); border-bottom: 1px solid var(--line); }
.tree-bar .stats { color: var(--muted); font-size: .88rem; font-variant-numeric: tabular-nums; }
.tree-bar .tools { margin-left: auto; display: flex; gap: 6px; }
.icon-btn { width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); font-size: 1.05rem; display: grid; place-items: center; }
#viewport { flex: 1; min-height: 320px; overflow: auto; position: relative; -webkit-overflow-scrolling: touch; }
#stage { position: relative; transform-origin: 0 0; }
#links { position: absolute; inset: 0; overflow: visible; pointer-events: none; }
#links path { fill: none; stroke: var(--line); stroke-width: 2; }
.node { position: absolute; width: 132px; display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 12px 8px 10px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 18px 22px 16px 20px; box-shadow: var(--shadow); text-align: center; cursor: pointer; }
.node:hover { border-color: var(--accent); }
.node.sel { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft), var(--shadow); }
.avatar { width: 62px; height: 62px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center;
  font-family: var(--f-display); font-size: 1.5rem; overflow: hidden; flex: none; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.node .name { font-weight: 600; line-height: 1.3; word-break: break-word; max-width: 100%;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.node .order { font-size: .75rem; color: var(--muted); margin-top: auto; }
.node .order + .gen { margin-top: -2px; }
.gen { margin-top: auto; font-size: .72rem; letter-spacing: .04em; color: var(--accent); background: var(--accent-soft); border-radius: 99px; padding: 1px 9px; }
.add-child { position: absolute; bottom: -14px; left: 50%; transform: translateX(-50%); width: 28px; height: 28px; border-radius: 50%;
  border: 1px solid var(--accent); background: var(--surface); color: var(--accent); font-size: 1.05rem; line-height: 1; display: grid; place-items: center; }
.add-child:hover { background: var(--accent); color: var(--accent-ink); }
.empty { padding: 48px 16px; text-align: center; display: flex; flex-direction: column; align-items: center; gap: 10px; }
.empty p { color: var(--muted); margin: 0; max-width: 36ch; }

/* history */
.posts { display: flex; flex-direction: column; gap: 18px; }
.post { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 20px; display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.post h2 { font-size: 1.3rem; }
.post .meta { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; font-size: .85rem; color: var(--muted); }
.period { color: var(--accent); background: var(--accent-soft); border-radius: 99px; padding: 1px 10px; font-weight: 500; }
.post .body { white-space: pre-wrap; word-break: break-word; margin: 0; }
.post .body.clamp { display: -webkit-box; -webkit-line-clamp: 6; -webkit-box-orient: vertical; overflow: hidden; }
.linkish { background: none; border: none; padding: 0; color: var(--accent); font-weight: 600; align-self: flex-start; }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.gallery figure { margin: 0; display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.gallery img { width: 100%; aspect-ratio: 4/3; object-fit: cover; border-radius: 10px; background: var(--accent-soft); cursor: zoom-in; display: block; }
.gallery .vid { grid-column: 1 / -1; }
.gallery video { width: 100%; border-radius: 10px; background: #000; display: block; max-height: 70vh; }
.gallery figcaption { font-size: .8rem; color: var(--muted); }
.post .actions { display: flex; gap: 6px; justify-content: flex-end; border-top: 1px solid var(--line); padding-top: 10px; }

/* media editor */
.media-edit { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; }
.media-item { border: 1px solid var(--line); border-radius: 10px; padding: 6px; display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.media-item .thumb { aspect-ratio: 4/3; border-radius: 6px; overflow: hidden; background: var(--accent-soft); display: grid; place-items: center; color: var(--accent); font-size: .85rem; }
.media-item .thumb img, .media-item .thumb video { width: 100%; height: 100%; object-fit: cover; }
.media-item input { border: 1px solid var(--line); background: var(--bg); border-radius: 6px; padding: 4px 6px; font-size: .8rem; width: 100%; }
.media-item .row { justify-content: space-between; }
.progress { height: 6px; background: var(--accent-soft); border-radius: 99px; overflow: hidden; }
.progress > div { height: 100%; background: var(--accent); width: 0; transition: width .2s; }

/* users table */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 14px; background: var(--surface); }
table { border-collapse: collapse; width: 100%; min-width: 560px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: .78rem; letter-spacing: .05em; color: var(--muted); font-weight: 600; }
tr:last-child td { border-bottom: none; }
.pill { display: inline-block; border-radius: 99px; padding: 1px 10px; font-size: .8rem; font-weight: 600; }
.pill.admin { background: var(--accent); color: var(--accent-ink); }
.pill.editor { background: var(--accent-soft); color: var(--accent); }
.pill.off { background: transparent; border: 1px solid var(--line); color: var(--muted); }

/* sheet / dialogs */
#scrim { position: fixed; inset: 0; background: var(--scrim); z-index: 20; }
#sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 21; background: var(--surface); border-radius: 20px 20px 0 0; max-height: 90vh; overflow: auto;
  box-shadow: 0 -8px 30px rgba(0,0,0,.2); padding: 20px var(--gutter-r) calc(20px + env(safe-area-inset-bottom, 0px)) var(--gutter-l); }
@media (min-width: 720px) { #sheet { left: 50%; right: auto; bottom: auto; top: 50%; transform: translate(-50%, -50%); width: min(620px, 92vw); border-radius: 20px; } }
.sheet-inner { display: flex; flex-direction: column; gap: 14px; }
.sheet-head { display: flex; align-items: center; gap: 14px; }
.sheet-head .avatar { width: 84px; height: 84px; font-size: 2rem; }
.sheet-head h3 { font-size: 1.2rem; }
.sheet-head .sub { color: var(--muted); font-size: .86rem; }
.note-view { white-space: pre-wrap; margin: 0; }
.confirm { border: 1px solid var(--danger); border-radius: 12px; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
#lightbox { position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.9); display: grid; place-items: center; padding: 24px; cursor: zoom-out; }
#lightbox img { max-height: 90vh; max-width: 100%; border-radius: 6px; }
#toast { position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%); background: var(--ink); color: var(--bg);
  padding: 8px 14px; border-radius: 10px; font-size: .92rem; z-index: 50; max-width: calc(100vw - 32px); }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
