/* 우리의 족보 — 한지와 먹을 기준으로 한 화면 규칙.
   색·간격·글꼴을 여기 변수로 모으고, 화면별 규칙은 그 변수만 씁니다. */
:root {
  --paper: #f5f1e6;
  --paper-raised: #fffdf7;
  --paper-sunk: #efebdd;
  --ink: #26241f;
  --ink-soft: #6d675b;
  --ink-faint: #928b7c;
  --line: #ddd5c2;
  --line-soft: #ebe5d6;
  --green: #2f5d4a;
  --green-deep: #234438;
  --green-wash: #e8efe9;
  --accent: #9c3b2e;
  --accent-wash: #fbeee9;
  --gold: #a98a4f;
  --serif: "Nanum Myeongjo", "Batang", "AppleMyungjo", serif;
  --sans: system-ui, "Malgun Gothic", "Apple SD Gothic Neo", sans-serif;
  --radius: 10px;
  --radius-lg: 16px;
  --shadow: 0 1px 2px #2624200d, 0 6px 18px #2624200a;
  --shadow-lift: 0 2px 6px #26242014, 0 14px 32px #26242012;
}

* {
  box-sizing: border-box;
}

/* Thin, floating scrollbars. The standard properties cover Firefox and current
   Chrome; the WebKit rules keep older Chrome and Safari in line, where a
   transparent border inside a wider track is what makes the thumb read as a
   slim bar rather than a chunky column. */
/* These two must not be declared alongside the ::-webkit-scrollbar rules below:
   once Chrome sees either of them it drops the pseudo-elements and draws its own
   thin bar instead. Firefox has no pseudo-elements, so it gets them here. */
@supports not selector(::-webkit-scrollbar) {
  * {
    scrollbar-width: thin;
    scrollbar-color: #26242033 transparent;
  }
}

::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track,
::-webkit-scrollbar-corner {
  background: transparent;
}

::-webkit-scrollbar-button {
  display: none;
}

/* 3px of transparent border either side leaves a 2px bar — a line, not a column.
   Under the pointer it thickens to 4px so it is still easy to take hold of. */
::-webkit-scrollbar-thumb {
  border: 3px solid transparent;
  border-radius: 99px;
  background: #26242033;
  background-clip: content-box;
}

::-webkit-scrollbar-thumb:hover {
  border-width: 2px;
  background: #2624206b;
  background-clip: content-box;
}

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.65 var(--sans);
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1,
h2,
h3 {
  font-family: var(--serif);
  font-weight: 700;
  letter-spacing: -0.01em;
}

h1 {
  font-size: 34px;
  line-height: 1.35;
}

h2 {
  font-size: 20px;
}

h3 {
  font-size: 15px;
}

a {
  color: var(--green);
}

.eyebrow {
  margin-bottom: 8px;
  color: var(--gold);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 2.5px;
}

.muted {
  margin: 14px 0 0;
  color: var(--ink-soft);
  font-size: 13px;
}

[hidden] {
  display: none !important;
}

/* ── 머리말 ─────────────────────────────────────────── */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 clamp(16px, 5vw, 56px);
  height: 78px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(#fffdf8, #fbf8f0);
}

header > div {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

header strong {
  font-family: var(--serif);
  font-size: 21px;
}

header small {
  padding-left: 14px;
  border-left: 1px solid var(--line);
  color: var(--ink-faint);
  font-size: 12px;
}

.seal {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 6px;
  background: var(--accent);
  box-shadow: inset 0 0 0 2px #ffffff30;
  color: #fff;
  font: 24px/1 var(--serif);
}

/* ── 알림창 ─────────────────────────────────── */
/* A squared-off box held in the middle of the screen. Stretched along the top
   edge it fell outside a zoomed tree's view, so nobody read it. */
#message {
  position: fixed;
  top: 18vh;
  left: 50%;
  transform: translateX(-50%);
  z-index: 60;
  width: min(380px, calc(100vw - 32px));
  margin: 0;
  padding: 26px 28px 20px;
  border: 1px solid #b7c7bc;
  background: var(--paper-raised);
  box-shadow: 0 20px 52px #26242033, 0 2px 8px #2624201a;
  color: var(--green-deep);
  font-size: 14px;
  line-height: 1.7;
  text-align: center;
  white-space: pre-wrap;
  cursor: pointer;
}

/* The box says how to dismiss it, so no stray close glyph is needed. */
#message::after {
  display: block;
  margin-top: 18px;
  padding-top: 12px;
  border-top: 1px solid var(--line-soft);
  color: var(--ink-faint);
  font-size: 12px;
  content: "누르면 닫힙니다";
}

#message:empty {
  display: none;
}

/* ── 입력과 단추 ────────────────────────────────────── */
label {
  display: block;
  margin-bottom: 14px;
  color: var(--ink-soft);
  font-size: 12.5px;
  font-weight: 600;
}

input,
select,
textarea {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font: 15px/1.5 var(--sans);
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--green);
  outline: 0;
  box-shadow: 0 0 0 3px #2f5d4a22;
}

button {
  border: 1px solid var(--green);
  border-radius: 8px;
  padding: 10px 16px;
  background: var(--green);
  color: #fff;
  font: 600 14px/1.3 var(--sans);
  cursor: pointer;
  transition: background .12s, border-color .12s, color .12s;
}

button:hover {
  background: var(--green-deep);
  border-color: var(--green-deep);
}

button:disabled {
  opacity: .45;
  cursor: not-allowed;
}

.secondary {
  border-color: var(--line);
  background: #fff;
  color: var(--ink);
}

.secondary:hover {
  border-color: var(--green);
  background: var(--green-wash);
  color: var(--green-deep);
}

.danger {
  border-color: #e2bdb6;
  background: var(--accent-wash);
  color: var(--accent);
}

.danger:hover {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

button:focus-visible,
a:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}

.actions {
  display: flex;
  gap: 10px;
}

.panel {
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  background: var(--paper-raised);
  box-shadow: var(--shadow);
}

/* ── 첫 화면 ────────────────────────────────────────── */
.welcome {
  display: grid;
  grid-template-columns: 1.15fr .85fr;
  gap: clamp(32px, 7vw, 88px);
  align-items: center;
  max-width: 1120px;
  margin: clamp(36px, 7vw, 84px) auto;
  padding: 0 24px;
}

.welcome h1 {
  margin-bottom: 18px;
  font-size: clamp(30px, 4vw, 44px);
}

.welcome > div > p:not(.eyebrow) {
  color: var(--ink-soft);
  font-size: 16px;
}

.intro-mark {
  margin-top: 40px;
  padding-top: 20px;
  border-top: 1px solid var(--line);
  color: var(--ink-faint);
  font-family: var(--serif);
  letter-spacing: 1px;
}

/* ── 작업 화면 ──────────────────────────────────────── */
main {
  display: grid;
  grid-template-columns: var(--side-width, 286px) 1px minmax(0, 1fr);
  align-items: start;
  min-height: 74vh;
}

/* The rule stays a hairline; only the area you may grab is wider, which is how
   VS Code's explorer, Chrome's DevTools and Figma's inspector handle it. */
.side-resizer {
  position: sticky;
  top: 0;
  z-index: 8;
  height: 100vh;
  background: transparent;
  cursor: col-resize;
  /* A finger's drag must move the edge, not scroll the page: without this the
     browser takes a touch drag for panning and cancels the resize at once. */
  touch-action: none;
}

/* The grab area reaches out mostly to the right: the sidebar's scrollbar sits
   right against the rule on the left and must stay usable. */
.side-resizer::before {
  content: "";
  position: absolute;
  inset: 0 -20px 0 -8px;
}

.side-resizer::after {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--green);
  opacity: 0;
  transition: opacity .15s;
}

.side-resizer:hover,
.side-resizer:focus-visible {
  outline: 0;
}

/* Nothing shows at the edge until it is being dragged; the cursor alone says it can be. */
.side-resizer:focus-visible::after,
body.resizing .side-resizer::after {
  opacity: 1;
}

body.resizing {
  cursor: col-resize;
  user-select: none;
}

/* The book page uses <aside> for its title and branch columns too, so the
   sidebar rules are tied to the workspace sidebar alone. */
/* The sidebar stays put while the page scrolls, so it scrolls on its own — the
   details of a book used to run past the bottom with no way to reach them. */
#workspace > aside {
  position: sticky;
  top: 0;
  max-height: 100vh;
  overflow-y: auto;
  /* The wheel still scrolls it; the bar itself stays out of sight. */
  scrollbar-width: none;
  padding: 26px 22px 34px;
  background: var(--paper-sunk);
}

.side-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 18px;
}

.side-head h2 {
  margin: 0;
}

.side-head button {
  flex: none;
  padding: 5px 10px;
  font-size: 12px;
}

/* The view tabs and the script switch share one row, and that row rides with the
   page so both stay in reach part way down a long tree or book. */
.view-bar {
  position: sticky;
  top: 0;
  z-index: 7;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 14px;
  padding: 10px 0;
  background: var(--paper);
}

.view-bar nav {
  margin-bottom: 0;
}

.view-bar > button {
  flex: none;
  padding: 6px 12px;
  font-size: 12px;
}

/* The hangul reading of a hanja field, so the record stays as written while the
   reader can still sound it out. */
.reading {
  display: block;
  margin-top: 5px;
  color: var(--ink-faint);
  font-size: 12px;
  font-weight: 400;
}

.page-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

#workspace > aside form {
  margin: 20px 0;
  padding: 18px 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper-raised);
}

#workspace > aside form h3 {
  margin-bottom: 14px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--line-soft);
  color: var(--green-deep);
}

/* The submit button alone spans the panel; the per-field buttons must not. */
#workspace > aside form > button {
  width: 100%;
}

.content {
  min-width: 0;
  padding: 28px clamp(18px, 3.5vw, 46px) 54px;
}

.page-title {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 18px;
  padding-bottom: 20px;
  border-bottom: 2px solid var(--ink);
}

.page-title h1 {
  margin: 0;
  font-size: 30px;
}

/* The selected book's details, read across the page rather than stacked in the
   sidebar where the last fields fell below the fold. */
.book-facts {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 28px;
  margin: 16px 0 0;
  padding: 14px 18px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper-raised);
}

.book-facts div {
  min-width: 0;
}

.book-facts dt {
  color: var(--ink-faint);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 1px;
}

.book-facts dd {
  margin: 3px 0 0;
  font-family: var(--serif);
  font-size: 16px;
}

.book-facts dd span {
  margin-left: 7px;
  color: var(--ink-soft);
  font-family: var(--sans);
  font-size: 12px;
}

.toolbar {
  display: flex;
  align-items: flex-end;
  gap: 14px;
  margin: 20px 0 16px;
}

.toolbar label {
  flex: 1;
  margin: 0;
}

.toolbar button {
  flex: none;
  padding: 11px 18px;
}

/* 보기 전환 — 하나로 묶인 탭 */
nav {
  display: inline-flex;
  gap: 2px;
  margin-bottom: 22px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--paper-sunk);
}

nav button {
  border: 0;
  border-radius: 999px;
  padding: 8px 18px;
  background: transparent;
  color: var(--ink-soft);
  font-weight: 600;
}

nav button:hover {
  background: #ffffffaa;
  color: var(--ink);
}

nav .active,
nav .active:hover {
  background: var(--green);
  color: #fff;
  box-shadow: var(--shadow);
}

/* ── 인물 목록 ──────────────────────────────────────── */
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(232px, 1fr));
  gap: 14px;
}

.person-card {
  display: flex;
  gap: 14px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper-raised);
  box-shadow: var(--shadow);
  color: var(--ink);
  font-weight: 400;
  text-align: left;
}

.person-card:hover {
  border-color: var(--green);
  background: var(--paper-raised);
  box-shadow: var(--shadow-lift);
  color: var(--ink);
  transform: translateY(-1px);
}

.person-card.male {
  border-inline-start: 3px solid #5b7ea8;
}

.person-card.female {
  border-inline-start: 3px solid #b06f81;
}

.person-portrait {
  display: grid;
  flex: none;
  place-items: center;
  overflow: hidden;
  width: 56px;
  height: 68px;
  border: 1px solid var(--line-soft);
  border-radius: 6px;
  background: var(--paper-sunk);
  color: var(--ink-faint);
  font: 22px/1 var(--serif);
}

.person-portrait img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.person-body {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
}

.person-body strong {
  font-family: var(--serif);
  font-size: 18px;
}

.person-hanja {
  color: var(--ink-soft);
  font-family: var(--serif);
  font-size: 14px;
}

/* 字 龍鶴 용학 — another name the person went by, under the name itself. */
.person-other {
  color: var(--ink-soft);
  font-family: var(--serif);
  font-size: 13px;
}

.person-other i {
  color: var(--ink-faint);
  font-style: normal;
}

.person-other em {
  color: var(--ink-faint);
  font-family: var(--sans);
  font-size: 12px;
  font-style: normal;
}

.person-body small {
  color: var(--ink-faint);
  font-size: 12px;
}

.badge {
  align-self: flex-start;
  margin-top: 2px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--green-wash);
  color: var(--green-deep);
  font-size: 11px;
  font-weight: 700;
}

.empty {
  padding: 56px 24px;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  color: var(--ink-faint);
  text-align: center;
}

/* ── 가족 관계 ──────────────────────────────────────── */
#relationsPanel {
  margin-top: 30px;
}

#relationsPanel h2 {
  margin-bottom: 16px;
}

.relation-form {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 12px;
  padding-bottom: 18px;
  border-bottom: 1px solid var(--line-soft);
}

.relation-form label {
  flex: 1;
  min-width: 140px;
  margin: 0;
}

.relation-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 11px 0;
  border-bottom: 1px solid var(--line-soft);
}

.relation-row button {
  flex: none;
  padding: 5px 12px;
  font-size: 12px;
}

/* ── 인물 창 ────────────────────────────────────────── */
dialog {
  width: min(660px, 94vw);
  max-height: 90vh;
  overflow: auto;
  padding: 28px;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  background: var(--paper-raised);
  box-shadow: var(--shadow-lift);
  color: var(--ink);
}

/* Only the person window holds a row against its bottom edge, so only it gives
   up its padding there; the spacing moves onto what it ends with. */
#personDialog,
#scanDialog {
  padding-bottom: 0;
}

#personDialog > :last-child,
#scanDialog > :last-child {
  margin-bottom: 28px;
}

dialog::backdrop {
  background: #26242099;
}

dialog h2 {
  margin-bottom: 18px;
  padding-right: 40px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line-soft);
}

/* Heading and the script switch share one ruled row. */
.dialog-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
  padding-right: 40px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line-soft);
}

.dialog-head h2 {
  margin: 0;
  padding: 0;
  border: 0;
}

.dialog-head button {
  flex: none;
  padding: 5px 10px;
  font-size: 12px;
}

#bookDialog {
  width: min(480px, 94vw);
}

#hanjaDialog {
  width: min(430px, 94vw);
}

#hanjaDialog h2 {
  margin-bottom: 10px;
  padding-bottom: 12px;
  font-size: 17px;
}

#hanjaDialog p {
  margin: 0 0 12px;
}

#bookDialog form {
  margin: 0;
}

#bookDialog form > button {
  width: 100%;
  margin-top: 4px;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0 18px;
}

#attachments {
  margin-top: 24px;
  padding-top: 20px;
  border-top: 1px solid var(--line-soft);
}

#attachments h3 {
  margin-bottom: 12px;
}

/* ── 꼬리말 ─────────────────────────────────────────── */
footer {
  padding: 28px;
  border-top: 1px solid var(--line-soft);
  color: var(--ink-faint);
  font-size: 12px;
  text-align: center;
}

/* ── 좁은 화면 ──────────────────────────────────────── */
@media (max-width: 900px) {
  header {
    height: auto;
    padding: 14px 16px;
  }

  header small {
    display: none;
  }

  .welcome {
    grid-template-columns: 1fr;
    gap: 24px;
    margin: 24px auto;
  }

  main {
    grid-template-columns: 1fr;
  }

  #workspace > aside {
    border-bottom: 1px solid var(--line);
  }

  .side-resizer {
    display: none;
  }

  #workspace > aside {
    position: static;
    max-height: none;
    overflow-y: visible;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .content {
    padding: 22px 16px 40px;
  }

  .page-title {
    flex-direction: column;
    align-items: flex-start;
  }

  .toolbar {
    flex-wrap: wrap;
  }

  nav {
    display: flex;
    width: 100%;
  }

  nav button {
    flex: 1;
    padding: 9px 8px;
    font-size: 13px;
  }

  .form-grid {
    grid-template-columns: 1fr;
    gap: 0;
  }
}

/* ── 인쇄 ───────────────────────────────────────────── */
@page {
  size: A4;
  margin: 14mm;
}

/* The book's sheets carry their own head and foot; with no margin the browser
   has nowhere to print its own. */
@page book-sheet {
  size: A4;
  margin: 0;
}

/* The tree is wider than it is tall, so it takes the sheet lying down. */
@page tree-sheet {
  size: A4 landscape;
  margin: 0;
}

/* A tree too tall to read lying down stands up, top generation to bottom. */
@page tree-sheet-tall {
  size: A4 portrait;
  margin: 0;
}

@media print {
  .book-facts {
    display: none;
  }

  body {
    background: #fff;
    color: #000;
  }

  header,
  /* Only the app's own sidebar — the book page's title and branch columns are
     <aside> too, and hiding those blanked them out of every printed sheet. */
  #workspace > aside,
  footer,
  #message,
  .page-title,
  .toolbar,
  .view-bar,
  .search-notice,
  #relationsPanel,
  dialog {
    display: none !important;
  }

  main,
  .content {
    display: block !important;
    padding: 0;
  }
}
