:root {
  --ink: #29251f;
  --muted: #817667;
  --paper: #f3efe4;
  --paper-deep: #e8dfce;
  --red: #8c2f25;
  --red-dark: #692019;
  --gold: #a9884c;
  --line: #9b8c76;
  --card: rgba(255, 253, 247, .96);
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; overflow: hidden; }
body {
  color: var(--ink);
  font-family: "Noto Serif SC", "Songti SC", SimSun, serif;
  background: var(--paper);
}
body::before {
  content: ""; position: fixed; inset: 0; pointer-events: none; opacity: .2;
  background-image: url("data:image/svg+xml,%3Csvg width='120' height='120' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.7' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='.12'/%3E%3C/svg%3E");
}
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
.topbar {
  height: 116px; display: flex; align-items: center; justify-content: space-between;
  padding: 0 28px 34px; background: rgba(247,243,233,.96); border-bottom: 1px solid #d5c9b5;
  position: relative; z-index: 20; box-shadow: 0 4px 18px rgba(62,48,31,.06);
}
.brand { display: flex; gap: 13px; align-items: center; }
.seal {
  width: 46px; height: 46px; display: grid; place-items: center; color: transparent;
  background: url("assets/favicon.png") center / contain no-repeat; border: 0; font-size: 0;
  box-shadow: none;
}
.brand h1 { margin: 1px 0 0; font-size: 23px; letter-spacing: .2em; font-weight: 600; }
.brand p { margin: 0; color: var(--muted); font-size: 11px; letter-spacing: .25em; }
.toolbar { display: flex; align-items: center; gap: 9px; height: 38px; }
.mobile-menu-btn { display: none; }
.toolbar > * { flex: none; margin-top: 0; margin-bottom: 0; }
.user-badge { height: 38px; display: inline-flex; align-items: center; padding: 0 9px; border: 1px solid #cdbfa9; color: var(--muted); font-size: 11px; white-space: nowrap; }
.user-badge.admin { color: var(--red); border-color: rgba(140,47,37,.45); background: rgba(140,47,37,.06); }
.search { display: flex; align-items: center; width: 210px; height: 38px; padding: 0 12px; background: #fffdf8; border: 1px solid #d7cbb8; }
.search input { width: 100%; height: 36px; margin: 0; border: 0; outline: 0; background: transparent; padding: 0 0 0 7px; color: var(--ink); }
.icon-btn, .ghost-btn, .primary-btn, .danger-btn {
  border: 1px solid #cdbfa9; background: transparent; color: var(--ink); height: 38px; padding: 0 14px;
}
.icon-btn { width: 38px; padding: 0; font-size: 20px; }
.primary-btn { color: #fff; border-color: var(--red); background: var(--red); }
.primary-btn:hover { background: var(--red-dark); }
.ghost-btn:hover, .icon-btn:hover { background: #ebe3d5; }
#zoomLabel { min-width: 42px; text-align: center; font: 12px Arial; color: var(--muted); }
.announcement-bar {
  position: absolute; left: 0; right: 0; bottom: 0; height: 34px; display: flex; align-items: center;
  padding: 0 28px; color: #675b4d; background: rgba(234,226,212,.78); border-top: 1px solid rgba(205,191,169,.72);
}
.announcement-icon { width: 20px; height: 20px; flex: none; display: grid; place-items: center; color: var(--red); }
.announcement-icon svg { width: 17px; height: 17px; fill: currentColor; }
.announcement-window { flex: 1; min-width: 0; height: 28px; overflow: hidden; position: relative; }
.announcement-text {
  position: absolute; top: 6px; white-space: nowrap; font-size: 12px; letter-spacing: .08em;
  animation: announcement-scroll 22s linear infinite;
}
@keyframes announcement-scroll { from { transform: translateX(100vw); } to { transform: translateX(-100%); } }
.announcement-action { height: 26px; margin-left: 8px; padding: 0 9px; border: 1px solid #c8b99f; color: #776958; background: rgba(255,253,247,.65); font-size: 11px; }
.viewport { position: relative; height: calc(100vh - 116px); overflow: hidden; cursor: default; }
.viewport.panning { cursor: grabbing; }
.viewport.space-ready { cursor: grab; }
.canvas { position: absolute; transform-origin: 0 0; width: 100px; height: 100px; }
.links { position: absolute; inset: 0; overflow: visible; pointer-events: none; }
.blood-link { fill: none; stroke: var(--line); stroke-width: 1.5; stroke-linecap: square; stroke-linejoin: miter; }
.blood-link-bg { fill: none; stroke: var(--paper); stroke-width: 5; stroke-linecap: square; stroke-linejoin: miter; }
.marriage-link { stroke: var(--red); stroke-width: 1.4; stroke-dasharray: 4 3; }
.person {
  position: absolute; width: 64px; height: 84px; padding: 7px 7px 6px 9px;
  background: var(--card); border: 1px solid #cfc2ad; box-shadow: 0 4px 12px rgba(55,42,27,.08);
  transition: border-color .15s, box-shadow .15s, opacity .15s; user-select: none;
}
.person.spouse { width: 48px; padding-left: 8px; padding-right: 6px; background: rgba(250,247,239,.9); }
.person::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px; background: #536b62; }
.person.female::before { background: #a45d58; }
.person.deceased {
  background: linear-gradient(155deg, rgba(224,220,209,.98), rgba(244,239,228,.97) 62%, rgba(231,225,212,.98));
  border-color: #aa9d85; box-shadow: 0 4px 12px rgba(68,61,49,.11), inset 0 0 18px rgba(96,91,76,.06);
}
.person.deceased::after {
  content: ""; position: absolute; z-index: 1; top: 1px; left: 7px; right: 1px; height: 7px; opacity: .34; pointer-events: none;
  background:
    linear-gradient(90deg, rgba(151,113,52,.7) 0 1px, transparent 1px 8px),
    linear-gradient(90deg, transparent 0 3px, rgba(151,113,52,.62) 3px 4px, transparent 4px 8px),
    linear-gradient(90deg, transparent 0 4px, rgba(140,47,37,.45) 4px 8px, transparent 8px 12px),
    linear-gradient(180deg, rgba(151,113,52,.55), rgba(151,113,52,.18));
  background-size: 12px 7px, 12px 7px, 12px 1px, 100% 1px;
  background-repeat: repeat-x, repeat-x, repeat-x, no-repeat;
  background-position: 0 0, 0 5px, 0 3px, 0 0;
}
.person.deceased .person-name { color: #5e584e; }
.person:hover, .person.selected { border-color: var(--red); box-shadow: 0 6px 18px rgba(100,38,28,.15); z-index: 80; }
.person.search-dim { opacity: .22; }
.person.search-hit { border-color: var(--red); box-shadow: 0 0 0 3px rgba(140,47,37,.15); }
.person-top { position: relative; height: 53px; display: flex; flex-direction: row-reverse; justify-content: space-between; align-items: flex-start; }
.person.deceased .person-top::after {
  content: ""; position: absolute; z-index: 1; top: -6px; right: -6px; bottom: -23px; width: 7px; opacity: .32; pointer-events: none;
  background:
    linear-gradient(180deg, rgba(151,113,52,.7) 0 1px, transparent 1px 8px),
    linear-gradient(180deg, transparent 0 3px, rgba(151,113,52,.62) 3px 4px, transparent 4px 8px),
    linear-gradient(180deg, transparent 0 4px, rgba(140,47,37,.45) 4px 8px, transparent 8px 12px),
    linear-gradient(90deg, rgba(151,113,52,.18), rgba(151,113,52,.55));
  background-size: 7px 12px, 7px 12px, 1px 12px, 1px 100%;
  background-repeat: repeat-y, repeat-y, repeat-y, no-repeat;
  background-position: 0 0, 5px 0, 3px 0, right top;
}
.person-name, .zi-badge { position: relative; z-index: 2; }
.person-name { writing-mode: vertical-rl; font-size: 14px; font-weight: 600; letter-spacing: .08em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.person.spouse .person-name { font-size: 13px; }
.zi-badge { flex: none; color: var(--red); font-size: 10px; border: 1px solid rgba(140,47,37,.35); padding: 2px 3px; line-height: 1; }
.person-tags {
  position: absolute; z-index: 3; left: 6px; right: 5px; bottom: 4px; display: flex; flex-direction: column;
  justify-content: flex-end; align-items: stretch; gap: 2px;
}
.person-tag {
  display: block; min-width: 0; width: 100%; height: 10px; padding: 1px 2px; color: #fff; font-size: 6px; line-height: 8px;
  text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; border-radius: 1px;
}
.person.spouse .person-tag { font-size: 5px; padding-left: 1px; padding-right: 1px; }
.person-tooltip {
  position: absolute; z-index: 120; left: calc(100% + 10px); top: 0; width: 210px; padding: 12px 14px;
  color: var(--ink); background: rgba(251,247,238,.96); border: 1px solid #cdbfa9;
  box-shadow: 0 10px 30px rgba(49,37,24,.18); font-size: 11px; line-height: 1.7;
  opacity: 0; visibility: hidden; transform: translateY(4px); pointer-events: none;
  transition: opacity .15s ease, transform .15s ease, visibility 0s linear 1.15s;
}
.person:hover .person-tooltip { opacity: 1; visibility: visible; transform: translateY(0); transition-delay: 1s; }
.person-tooltip b { color: var(--red); font-weight: 500; }
.tag-editor { margin: 0 0 16px; }
.tag-editor-title { display: block; margin-bottom: 8px; color: var(--muted); font-size: 12px; }
.tag-rows { display: grid; grid-template-columns: 1fr 92px; gap: 8px 10px; }
.tag-rows label { margin-bottom: 0; }
.tag-rows input, .tag-rows select { margin-top: 5px; padding: 8px 9px; font-size: 12px; }
.tag-rows select { color: #fff; border-color: rgba(140,47,37,.25); text-shadow: 0 1px 1px rgba(0,0,0,.18); }
.generation-rail { position: absolute; left: 8px; top: 0; bottom: 0; z-index: 12; pointer-events: none; }
.gen-tag {
  position: absolute; width: 78px; min-height: 62px; padding: 8px 7px; color: #695d4e;
  border: 1px solid rgba(185,170,145,.48); border-left: 3px solid rgba(140,47,37,.55);
  background: rgba(247,242,232,.55); backdrop-filter: blur(9px) saturate(125%);
  box-shadow: 0 5px 18px rgba(74,57,37,.08);
}
.gen-order { display: block; font-size: 11px; letter-spacing: .08em; }
.gen-zi { display: block; margin-top: 1px; white-space: nowrap; font-size: 13px; color: var(--muted); }
.gen-zi b {
  display: inline-block; margin: 0 2px; font-size: 27px; line-height: 1; font-family: "STKaiti","KaiTi",serif;
  font-weight: 700; background: linear-gradient(110deg,#6a211a 5%,#c59a52 38%,#fff1be 50%,#8c2f25 64%,#6a211a 95%);
  background-size: 220% 100%; -webkit-background-clip: text; background-clip: text; color: transparent;
  filter: drop-shadow(0 1px 1px rgba(86,42,25,.2)); animation: zi-shimmer 3.4s linear infinite;
}
@keyframes zi-shimmer { to { background-position: -220% 0; } }
.canvas-tip, .legend {
  position: absolute; z-index: 9; bottom: 18px; background: rgba(255,253,247,.9);
  border: 1px solid #d3c6b1; color: var(--muted); font-size: 12px; padding: 9px 12px;
}
.canvas-tip { left: 18px; }
.legend { right: 18px; display: flex; gap: 16px; }
kbd { border: 1px solid #c8baa5; background: #fff; padding: 1px 5px; box-shadow: 0 1px 0 #c8baa5; }
.dot { display: inline-block; width: 8px; height: 8px; margin-right: 5px; background: #536b62; border-radius: 50%; }
.dot.female { background: #a45d58; }
.line-sample { display: inline-block; width: 19px; border-top: 2px solid var(--line); margin: 0 5px 3px 0; }
.line-sample.marriage { border-color: var(--red); border-top-style: dashed; }
.drawer {
  position: fixed; z-index: 40; right: 0; top: 0; width: 390px; height: 100%; padding: 22px 26px;
  background: #f7f2e8; border-left: 1px solid #cfc0aa; transform: translateX(105%); transition: transform .25s ease;
  box-shadow: -12px 0 35px rgba(48,34,20,.12); overflow-x: hidden; overflow-y: auto;
}
.drawer.open { transform: translateX(0); }
.drawer form { min-width: 0; }
.backdrop { display: none; position: fixed; z-index: 30; inset: 0; background: rgba(39,31,22,.18); }
.backdrop.show { display: block; }
.drawer-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; min-height: 38px; }
.drawer-head h2 { margin: 4px 0 0; font-size: 23px; font-weight: 500; }
.drawer-head h2:empty { display: none; }
.eyebrow { font-size: 13px; letter-spacing: .22em; color: var(--red); }
.close-btn { border: 0; background: transparent; font-size: 32px; line-height: 1; color: var(--muted); }
label { display: block; margin-bottom: 8px; font-size: 12px; color: var(--muted); }
input, select, textarea {
  display: block; width: 100%; margin-top: 5px; padding: 9px 10px; color: var(--ink);
  background: rgba(255,255,255,.65); border: 1px solid #cfc2ad; outline: none; resize: vertical;
}
input:focus, select:focus, textarea:focus { border-color: var(--red); }
input[readonly] { color: #766d60; background: #eee8dd; }
.date-options-row {
  display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto; gap: 10px;
  align-items: center; margin: 2px 0 8px;
}
.date-options-row .unknown-date { min-height: 32px; margin-top: 0; }
.date-calendar-switch {
  display: grid; grid-template-columns: auto 86px; gap: 7px; align-items: center; margin: 0; min-height: 32px;
  color: var(--muted); font-size: 12px;
}
.date-calendar-switch input { position: absolute; opacity: 0; pointer-events: none; }
.date-calendar-switch i {
  position: relative; display: grid; grid-template-columns: 1fr 1fr; width: 86px; height: 32px; padding: 2px;
  border: 1px solid #cfc2ad; background: rgba(255,255,255,.65); font-style: normal;
}
.date-calendar-switch i::before {
  content: ""; position: absolute; z-index: 0; left: 2px; top: 2px; width: calc(50% - 2px); height: calc(100% - 4px);
  background: var(--red); transition: transform .18s ease;
}
.date-calendar-switch input:checked + i::before { transform: translateX(100%); }
.date-calendar-switch b {
  position: relative; z-index: 1; display: grid; place-items: center; color: var(--muted); font-size: 12px; font-weight: 400;
}
.date-calendar-switch input:not(:checked) + i b:first-child,
.date-calendar-switch input:checked + i b:last-child { color: #fff; }
.switch-row { display: flex; align-items: center; justify-content: space-between; padding: 4px 0 10px; }
.switch-row input { width: 17px; height: 17px; margin: 0; accent-color: var(--red); }
.unknown-date { display: flex; align-items: center; gap: 7px; margin-top: 7px; color: var(--muted); font-size: 12px; }
.unknown-date input { width: 18px; height: 18px; margin: 0; accent-color: var(--red); }
.date-hint { display: block; min-height: 13px; margin-top: 3px; color: #8a6a35; font-size: 10px; line-height: 1.35; }
.date-hint.error { color: var(--red); }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.identity-grid { display: grid; grid-template-columns: 1.4fr .78fr .9fr .78fr; gap: 10px; }
.identity-grid label { min-width: 0; }
#personNote { min-height: 210px; line-height: 1.65; }
.drawer.readonly #personNote { min-height: 300px; }
.relation-box { border-top: 1px solid #d5c9b5; border-bottom: 1px solid #d5c9b5; padding: 8px 0; margin: 0 0 8px; }
.relation-box h3 { font-size: 15px; margin: 0 0 7px; font-weight: 500; }
.relation-actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; }
.relation-actions button { border: 1px solid #cdbfa9; background: #fffaf1; padding: 8px 4px; font-size: 12px; }
.relation-list {
  display: grid; grid-template-columns: 1fr 1fr; gap: 4px 12px; margin: 8px 0 0;
  font-size: 12px; color: var(--muted); line-height: 1.7;
}
.relation-list span { display: block; }
.relation-list .relation-children { grid-column: 1 / -1; }
.form-actions { display: flex; justify-content: space-between; }
.danger-btn { color: var(--red); }
button:disabled, input:disabled, select:disabled, textarea:disabled {
  cursor: not-allowed;
}
button:disabled {
  opacity: .48;
  color: #9a9185;
  border-color: #d6cbb9;
  background: #eee8dd;
}
button.primary-btn:disabled {
  color: #f1eee7;
  background: #b9aca0;
  border-color: #b9aca0;
}
dialog { width: 380px; margin: auto; padding: 26px; border: 1px solid #c9baa3; background: #f7f2e8; color: var(--ink); box-shadow: 0 24px 80px rgba(25,18,12,.25); }
dialog::backdrop { background: rgba(39,31,22,.35); }
.wide { width: 100%; margin-top: 5px; }
.auth-dialog { width: min(480px, calc(100vw - 40px)); }
.auth-tabs { display: grid; grid-template-columns: 1fr 1fr; margin: -4px 0 22px; border: 1px solid #cdbfa9; background: rgba(255,250,241,.58); }
.auth-tab { height: 40px; border: 0; border-right: 1px solid #cdbfa9; color: var(--muted); background: transparent; }
.auth-tab:last-child { border-right: 0; }
.auth-tab.active { color: #fff; background: var(--red); }
.auth-panels h3 { margin: 0 0 18px; font-size: 16px; font-weight: 500; }
.auth-panels form[hidden] { display: none; }
.auth-note, .form-message { color: var(--muted); font-size: 11px; }
.form-message { min-height: 16px; color: var(--red); }
.history-dialog { width: min(960px, calc(100vw - 40px)); }
.history-table-wrap { max-height: 60vh; overflow: auto; margin-bottom: 18px; border: 1px solid #d5c9b5; }
.history-table { width: 100%; border-collapse: collapse; font-size: 12px; background: rgba(255,255,255,.45); }
.history-table th, .history-table td { padding: 10px 12px; border-bottom: 1px solid #ddd1be; text-align: left; vertical-align: top; }
.history-table th { position: sticky; top: 0; background: #eee6d8; color: var(--muted); font-weight: 500; }
.history-empty { padding: 30px !important; text-align: center !important; color: var(--muted); }
.history-section-title { margin: 14px 0 8px; font-size: 14px; font-weight: 500; color: var(--ink); }
.history-tools {
  display: grid; grid-template-columns: 1fr 1fr auto; gap: 10px; align-items: end; margin: -6px 0 16px;
}
.history-tools label { margin: 0; }
.history-tools button { height: 38px; }
.history-pager {
  min-height: 34px; display: flex; align-items: center; justify-content: flex-end; gap: 10px;
  margin: -8px 0 14px; color: var(--muted); font-size: 12px;
}
.history-pager button { height: 30px; padding: 0 10px; font-size: 11px; }
.history-pager button:disabled { opacity: .45; cursor: default; }
.request-table-wrap { max-height: 32vh; }
.review-actions { display: flex; gap: 6px; }
.review-actions button { height: 28px; padding: 0 9px; font-size: 11px; }
.status-tag { display: inline-block; padding: 3px 7px; font-size: 10px; border: 1px solid #cdbfa9; color: var(--muted); }
.status-tag.pending { color: #8a631f; border-color: #c4a15d; }
.status-tag.approved { color: #416458; border-color: #74988c; }
.status-tag.rejected { color: var(--red); border-color: #b97971; }
.announcement-dialog { width: min(700px, calc(100vw - 40px)); }
.message-board-dialog { width: min(760px, calc(100vw - 40px)); }
.message-board-dialog form { margin-bottom: 16px; }
.message-board-dialog form button { margin-top: 0; }
.message-board-list { max-height: 54vh; overflow: auto; margin-top: 18px; }
.board-message {
  padding: 15px 16px; margin-bottom: 12px; border: 1px solid #d5c9b5;
  background: rgba(255,255,255,.38);
}
.board-message header, .board-reply header {
  display: flex; justify-content: space-between; gap: 12px; align-items: baseline;
  color: var(--muted); font-size: 11px;
}
.board-message header strong, .board-reply header strong { color: var(--red); font-weight: 500; }
.board-message p, .board-reply p { margin: 8px 0 0; font-size: 13px; line-height: 1.8; white-space: pre-wrap; }
.board-replies { margin: 12px 0 0 18px; border-left: 2px solid rgba(140,47,37,.18); }
.board-reply { padding: 10px 0 10px 14px; border-bottom: 1px solid rgba(213,201,181,.7); }
.board-reply-form { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: start; margin-top: 12px; }
.board-reply-form textarea { min-height: 58px; margin-top: 0; }
.board-reply-form button { height: 58px; }
.board-empty { padding: 34px; color: var(--muted); text-align: center; border: 1px dashed #cdbfa9; }
.announcement-history { max-height: 52vh; overflow: auto; margin-top: 18px; }
.announcement-item { padding: 13px 14px; border-bottom: 1px solid #ddd1be; background: rgba(255,255,255,.35); }
.announcement-item time { display: block; margin-bottom: 5px; color: var(--muted); font-size: 10px; }
.announcement-item p { margin: 0; line-height: 1.7; font-size: 13px; }
.announcement-item img { display: block; max-width: min(100%, 520px); max-height: 260px; margin-top: 10px; border: 1px solid #d5c9b5; object-fit: contain; background: #eee8dc; }
.announcement-preview { display: block; max-width: 100%; max-height: 220px; margin: -4px 0 16px; border: 1px solid #d5c9b5; object-fit: contain; }
.announcement-empty { padding: 30px; color: var(--muted); text-align: center; }
.data-port-dialog { width: min(520px, calc(100vw - 40px)); }
.data-port-note { margin: -8px 0 18px; color: var(--muted); font-size: 12px; line-height: 1.8; }
.data-port-actions { display: flex; gap: 12px; align-items: center; }
.import-file {
  height: 38px; margin: 0; padding: 0 14px; display: inline-flex; align-items: center;
  border: 1px solid #cdbfa9; background: #fffaf1; color: var(--ink); cursor: pointer;
}
.import-file input { display: none; }
.message-dialog {
  width: min(420px, calc(100vw - 40px)); padding: 0; overflow: hidden;
  background: linear-gradient(145deg, rgba(255,252,244,.98), rgba(239,231,216,.98));
  border: 1px solid #c8b99f; box-shadow: 0 28px 90px rgba(35,25,14,.32), inset 0 0 0 4px rgba(255,255,255,.28);
}
.message-dialog[open] { animation: dialog-pop .18s ease-out; }
@keyframes dialog-pop {
  from { opacity: 0; transform: translateY(10px) scale(.96); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
.message-dialog::before {
  content: ""; position: absolute; inset: 9px; pointer-events: none;
  border: 1px solid rgba(169,136,76,.34);
}
.message-ornament {
  height: 8px; background: linear-gradient(90deg, var(--red), #b4853f, var(--red));
  box-shadow: 0 1px 0 rgba(255,255,255,.5) inset;
}
.message-close { position: absolute; right: 14px; top: 12px; z-index: 2; font-size: 24px; }
.message-body { position: relative; padding: 28px 30px 14px; text-align: center; }
.message-body h2 { margin: 6px 0 10px; font-size: 22px; font-weight: 500; letter-spacing: .12em; }
.message-body p { margin: 0; color: #655b4e; font-size: 13px; line-height: 1.9; white-space: pre-line; }
.message-actions {
  display: flex; justify-content: center; gap: 12px; padding: 0 30px 28px;
}
.message-actions button { min-width: 92px; }
.message-dialog.alert .messageCancelHidden { display: none; }
.drawer.readonly input, .drawer.readonly select, .drawer.readonly textarea { color: #766d60; background: #eee8dd; }
@media (max-width: 800px) {
  html, body { height: 100dvh; }
  body { min-width: 0; }
  body.mobile-menu-open::after {
    content: ""; position: fixed; inset: 0; z-index: 25; background: rgba(0,0,0,.4); pointer-events: none;
  }
  body.mobile-menu-open .topbar { z-index: 45; }
  .topbar {
    height: 118px; display: grid; grid-template-columns: auto 1fr; align-items: center; gap: 8px 10px;
    padding: 8px max(12px, env(safe-area-inset-right)) 34px max(12px, env(safe-area-inset-left));
  }
  .brand { gap: 8px; min-width: 0; }
  .seal {
    width: 36px; height: 36px; border: 0; color: transparent; font-size: 0;
    background: url("assets/favicon.png") center / contain no-repeat; box-shadow: none;
  }
  .brand p { display: none; }
  .brand h1 { margin: 0; font-size: 17px; letter-spacing: .12em; white-space: nowrap; }
  .toolbar {
    position: relative; display: grid; grid-template-columns: minmax(0, 1fr) 42px; justify-self: stretch;
    align-items: center; gap: 8px; height: 42px; min-width: 0;
  }
  .toolbar .search {
    display: flex; width: 100%; height: 42px; padding: 0 10px; grid-column: 1;
  }
  .search input { height: 40px; font-size: 15px; }
  .mobile-menu-btn {
    display: grid; grid-column: 2; width: 42px; height: 42px; place-items: center; padding: 0;
    font-size: 22px; line-height: 1; background: #fffdf8;
  }
  .toolbar > :not(.search):not(.mobile-menu-btn) { display: none; }
  .toolbar.menu-open > :not(.search):not(.mobile-menu-btn):not([hidden]) {
    display: flex; align-items: center; justify-content: center;
  }
  .toolbar.menu-open {
    position: relative; z-index: 35; row-gap: 0;
  }
  .toolbar.menu-open::after {
    content: none;
  }
  .toolbar.menu-open > :not(.search):not(.mobile-menu-btn) {
    position: relative; grid-column: 1 / -1; justify-self: end; z-index: 50;
    width: min(280px, calc(100vw - 24px)); margin: 0; border-bottom-width: 0;
  }
  .toolbar.menu-open #zoomOut,
  .toolbar.menu-open #zoomIn,
  .toolbar.menu-open #zoomLabel { display: none !important; }
  .toolbar.menu-open #resetView { margin-top: 8px; }
  .toolbar.menu-open > .icon-btn { height: 40px; }
  .toolbar.menu-open .ghost-btn,
  .toolbar.menu-open .primary-btn,
  .toolbar.menu-open .user-badge {
    height: 42px; padding: 0 14px; font-size: 13px; background: #fffdf8;
    border-color: #d1c2aa; box-shadow: 0 8px 22px rgba(39,31,22,.14);
  }
  .toolbar.menu-open .primary-btn { color: #fff; background: var(--red); }
  .toolbar.menu-open > :last-child { border-bottom-width: 1px; }
  .announcement-bar {
    height: 34px; padding: 0 max(12px, env(safe-area-inset-right)) 0 max(12px, env(safe-area-inset-left));
  }
  .announcement-text { top: 7px; font-size: 12px; letter-spacing: .04em; }
  .announcement-action { height: 24px; padding: 0 7px; font-size: 10px; }
  .viewport {
    height: calc(100dvh - 118px); touch-action: none;
  }
  .canvas-tip { display: none; }
  .legend {
    left: 8px; right: 8px; bottom: max(8px, env(safe-area-inset-bottom)); justify-content: center;
    gap: 10px; padding: 7px 8px; font-size: 11px; background: rgba(255,253,247,.76);
  }
  .generation-rail { left: 4px; opacity: .72; }
  .gen-tag {
    width: 52px; min-height: 43px; padding: 5px 4px; backdrop-filter: blur(7px) saturate(110%);
  }
  .gen-order { font-size: 9px; }
  .gen-zi { font-size: 10px; }
  .gen-zi b { font-size: 19px; }
  .person-tooltip { display: none; }
  .drawer {
    top: auto; bottom: 0; left: 0; right: 0; width: 100%; height: min(86dvh, 720px);
    padding: 14px 14px calc(78px + env(safe-area-inset-bottom));
    border-left: 0; border-top: 1px solid #cfc0aa; border-radius: 14px 14px 0 0;
    transform: translateY(105%); box-shadow: 0 -18px 42px rgba(48,34,20,.18);
  }
  .drawer.open { transform: translateY(0); }
  .drawer-head { position: sticky; top: -14px; z-index: 2; margin: -14px -14px 6px; padding: 10px 14px 6px; min-height: 38px; background: #f7f2e8; border-bottom: 1px solid #ddd1be; }
  .drawer-head h2 { font-size: 19px; }
  .eyebrow { font-size: 13px; }
  .close-btn { min-width: 44px; min-height: 44px; display: grid; place-items: center; font-size: 30px; }
  label { margin-bottom: 7px; font-size: 12px; }
  input, select, textarea { min-height: 40px; margin-top: 5px; padding: 8px 10px; font-size: 15px; }
  .date-options-row { grid-template-columns: 1fr 1fr auto; gap: 7px; margin: 1px 0 7px; }
  .date-options-row .unknown-date { min-height: 30px; }
  .date-calendar-switch { grid-template-columns: 76px; gap: 3px; justify-items: end; font-size: 11px; }
  .date-calendar-switch > span { justify-self: start; }
  .date-calendar-switch i { width: 76px; height: 30px; }
  textarea { min-height: 78px; }
  #personNote { min-height: 210px; line-height: 1.65; }
  .drawer.readonly #personNote { min-height: 330px; }
  .identity-grid {
    grid-template-columns: minmax(96px, 1.55fr) minmax(50px, .65fr) minmax(70px, .9fr) minmax(50px, .65fr);
    gap: 8px;
  }
  .identity-grid label { margin-bottom: 8px; }
  .identity-grid input, .identity-grid select {
    min-height: 38px; padding: 7px 8px; font-size: 15px;
  }
  #personGender, #personZi { text-align: center; }
  .form-row { grid-template-columns: 1fr 1fr; gap: 10px; }
  .switch-row { padding: 4px 0 10px; }
  .tag-editor { margin-bottom: 10px; }
  .tag-editor-title { margin-bottom: 6px; font-size: 11px; line-height: 1.45; }
  .tag-rows { grid-template-columns: minmax(0, 1fr) minmax(92px, .72fr); gap: 8px 10px; }
  .tag-rows label { margin-bottom: 0; }
  .tag-rows input, .tag-rows select { min-height: 38px; margin-top: 4px; padding: 7px 9px; font-size: 14px; }
  .date-hint { min-height: 14px; margin-top: 3px; font-size: 9px; line-height: 1.35; }
  .unknown-date {
    min-height: 30px; margin-top: 7px; display: grid; grid-template-columns: 26px 1fr; align-items: center;
    justify-content: start; gap: 7px; font-size: 12px; line-height: 1.15;
  }
  .unknown-date input { width: 24px; height: 24px; margin: 0; }
  .relation-box { padding: 7px 0; margin: 0 0 7px; }
  .relation-box h3 { margin-bottom: 7px; }
  .relation-list {
    display: grid; grid-template-columns: 1fr 1fr; gap: 4px 12px; margin-top: 8px; line-height: 1.7;
  }
  .relation-list .relation-children { grid-column: 1 / -1; }
  .relation-actions { grid-template-columns: 1fr 1fr; }
  .relation-actions button { min-height: 42px; font-size: 13px; }
  .form-actions {
    position: sticky; left: 0; right: 0; bottom: calc(-1 * (78px + env(safe-area-inset-bottom)));
    z-index: 3; gap: 8px; margin: 10px -14px calc(-78px - env(safe-area-inset-bottom));
    padding: 8px 14px calc(8px + env(safe-area-inset-bottom));
    background: rgba(247,242,232,.97); border-top: 1px solid #d8cbb7;
  }
  .form-actions button { flex: 1; height: 42px; }
  dialog {
    width: calc(100vw - 20px); max-width: none; max-height: calc(100dvh - 20px);
    padding: 18px 16px; overflow: auto; margin: auto; overscroll-behavior: contain;
  }
  .auth-dialog, .history-dialog, .announcement-dialog, .message-board-dialog, .data-port-dialog, .message-dialog {
    width: calc(100vw - 20px);
  }
  .auth-tabs { margin-bottom: 16px; }
  .auth-tab { min-height: 44px; font-size: 13px; }
  .history-table-wrap, .request-table-wrap { max-height: 42dvh; overflow: auto; -webkit-overflow-scrolling: touch; }
  .history-table { min-width: 720px; font-size: 12px; }
  .history-table th, .history-table td { padding: 9px 10px; }
  .history-tools { grid-template-columns: 1fr; gap: 8px; }
  .history-tools button { width: 100%; }
  .history-pager { justify-content: center; margin-top: -4px; }
  .message-board-list, .announcement-history { max-height: 52dvh; }
  .board-message { padding: 13px 12px; }
  .board-message header, .board-reply header { display: block; line-height: 1.7; }
  .board-replies { margin-left: 8px; }
  .board-reply-form { grid-template-columns: 1fr; gap: 8px; }
  .board-reply-form button, .message-board-dialog form button { width: 100%; height: 44px; }
  .announcement-item img { max-width: 100%; max-height: 42dvh; }
  .announcement-preview { max-height: 34dvh; }
  .data-port-actions { display: grid; grid-template-columns: 1fr; }
  .data-port-actions button, .import-file { width: 100%; height: 44px; justify-content: center; }
  .message-dialog { padding: 0; }
  .message-body { padding: 28px 22px 14px; }
  .message-actions { padding: 0 18px calc(20px + env(safe-area-inset-bottom)); }
  .message-actions button { flex: 1; min-width: 0; height: 44px; }
}
