/* 차용증 양식 작성기(#3038) */
:root {
  --font: "Noto Sans KR", "Apple SD Gothic Neo", "Malgun Gothic", system-ui, -apple-system, "Segoe UI", sans-serif;
  --bg: #f2f6fb;
  --surface: #ffffff;
  --ink: #16202b;
  --sub: #55636f;
  --line: #d7e0ea;
  --accent: #1f5fae;
  --accent-soft: #e6eef8;
  --accent-ink: #ffffff;
  --danger: #b3261e;
  /* 이자 판정 상태색: 선(왼쪽 굵은 막대)·글자·배경 세 가지로 보인다(색만으로 구분하지 않게 「정상·안내·경고」 글자도 붙임) */
  --green: #1d7a46;
  --green-bg: #eef8f1;
  --blue: #1f5fae;
  --blue-bg: #eef4fb;
  --red: #b3261e;
  --red-bg: #fdf0ef;
  --radius: 12px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}

body {
  font-size: 16px;
  line-height: 1.6;
  word-break: keep-all;
  overflow-wrap: anywhere;
}

::selection { background: #cfe0f5; color: var(--ink); }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.site-header {
  padding: 24px 16px 8px;
  max-width: 1100px;
  margin: 0 auto;
}
.site-header .brand { margin: 0 0 4px; font-weight: 700; color: var(--accent); font-size: 0.9rem; }
.site-header h1 { margin: 0 0 8px; font-size: clamp(1.4rem, 4vw, 1.9rem); }
.site-header .lead { margin: 0; color: var(--sub); max-width: 70ch; }

.layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
  padding: 16px;
  max-width: 1100px;
  margin: 0 auto;
}

.form-panel, .preview-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  min-width: 0;
}
.form-panel h2, .preview-panel h2 { margin: 0 0 12px; font-size: 1.1rem; }
.pv-sub { font-size: 0.85rem; font-weight: 600; color: var(--sub); margin-left: 6px; }

fieldset {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin: 0 0 16px;
  padding: 12px;
  min-width: 0;
}
legend { font-weight: 700; padding: 0 6px; }
.legend-sub { font-weight: 600; color: var(--sub); font-size: 0.9rem; margin-left: 4px; }

.field { margin-bottom: 14px; min-width: 0; }
.field:last-child { margin-bottom: 0; }
.field-row { display: grid; grid-template-columns: 1fr; gap: 0 12px; }
.field-row .field:last-child { margin-bottom: 14px; }
fieldset .field-row:last-child .field:last-child { margin-bottom: 0; }

label, .label { display: block; font-weight: 600; margin: 0 0 6px; font-size: 0.95rem; }
.opt {
  display: inline-block;
  margin-left: 4px;
  padding: 0 7px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: #234a78;
  font-size: 0.75rem;
  font-weight: 700;
  vertical-align: 1px;
}

input[type="text"],
input[type="date"],
input[type="tel"],
textarea {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  caret-color: var(--accent);
}
textarea { resize: vertical; line-height: 1.5; }
input[type="date"] { -webkit-appearance: none; appearance: none; }
input[aria-invalid="true"] { border-color: var(--danger); }
::placeholder { color: #6b7783; opacity: 1; }

input:focus-visible,
textarea:focus-visible,
button:focus-visible,
a:focus-visible,
summary:focus-visible,
.chip input:focus-visible + span,
.check input:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.help-text { margin: 6px 0 0; font-size: 0.85rem; color: var(--sub); }
.field-error { margin: 6px 0 0; font-size: 0.85rem; color: var(--danger); font-weight: 600; }
.field-error:empty { display: none; }

/* 금액 한글 병기: 칸 바로 아래 한 줄(숫자는 고른 폭) */
.amount-ko {
  margin: 6px 0 0;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--accent-soft);
  color: #17406f;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.amount-ko:empty { display: none; }

.inline-unit { display: flex; align-items: center; gap: 8px; }
.inline-unit input { max-width: 120px; }
.inline-unit span { font-weight: 600; }

/* 체크 상자: 글자까지 눌러지는 44px 줄 */
.check {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  margin: 0;
  cursor: pointer;
}
.check input { width: 22px; height: 22px; margin: 0; accent-color: var(--accent); flex: 0 0 auto; }

/* 칩(라디오) */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { margin: 0; position: relative; cursor: pointer; font-weight: 600; }
.chip input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.chip span {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 8px 16px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  font-size: 0.95rem;
  transition: background-color 0.15s ease-out, border-color 0.15s ease-out, color 0.15s ease-out;
}
.chip:hover span { border-color: #9fb6d1; }
.chip input:checked + span { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

/* ---- 이자 판정: 입력 즉시, 이율 칸 바로 아래(r2 H1 — 폰 키보드가 열려도 칸과 함께 보이게) ---- */
.rate-block { display: grid; grid-template-columns: 1fr; gap: 8px 12px; margin-bottom: 14px; }
.rate-block > .field, .rate-block > .verdict { margin: 0; }
.verdict { margin: 0; }
.verdict:has(.verdict-list:empty) { display: none; }
.verdict-late { margin-top: 8px; }
/* 이율 칸이 포커스로 화면에 끌려올 때 아래 판정 상자 자리까지 같이 끌어올린다 */
#rate { scroll-margin-bottom: 224px; } /* 360폭 경고 상자 높이(약 203px) + 틈 8px 가 키보드 위에 같이 보이게 */
#lateRate { scroll-margin-bottom: 120px; }
.verdict-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.verdict-list:empty { display: none; }
.verdict-item {
  margin: 0;
  padding: 10px 12px 10px 14px;
  border-radius: 0 8px 8px 0;
  font-size: 0.92rem;
  line-height: 1.55;
}
.verdict-tag { display: inline-block; font-weight: 800; margin-right: 6px; }
.verdict-ok { border-left: 6px solid var(--green); background: var(--green-bg); color: #14532f; }
.verdict-ok .verdict-tag { color: var(--green); }
.verdict-info { border-left: 6px solid var(--blue); background: var(--blue-bg); color: #173c66; }
.verdict-info .verdict-tag { color: var(--blue); }
.verdict-warn { border-left: 6px solid var(--red); background: var(--red-bg); color: #6f1712; }
.verdict-warn .verdict-tag { color: var(--red); }

.privacy-note { margin: 14px 0 0; font-size: 0.85rem; color: var(--sub); }

/* 인쇄 전 검사에서 막혔을 때 버튼 바로 아래 한 줄(r2 M5) */
.print-msg { margin: 10px 0 0; padding: 10px 12px; border-left: 6px solid var(--red); border-radius: 0 8px 8px 0; background: var(--red-bg); color: #6f1712; font-weight: 600; font-size: 0.92rem; }
.print-msg:empty { display: none; }
/* 카톡 인앱 인쇄 안내(r2 M6): 버튼 줄 바로 아래, 안내=파랑 선 */
.kakao-hint { margin: 10px 0 0; padding: 10px 12px; border-left: 6px solid var(--blue); border-radius: 0 8px 8px 0; background: var(--blue-bg); color: #173c66; font-size: 0.92rem; }
.kakao-open { display: inline-flex; align-items: center; min-height: 44px; color: var(--accent); font-weight: 700; text-underline-offset: 3px; }
.kakao-open[hidden] { display: none; } /* r3 N1: 위 inline-flex 가 UA 의 [hidden] 을 이겨 iOS 카톡에서 빈 44px 상자가 남던 것 */

.actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 16px; }
.btn {
  min-height: 44px;
  padding: 10px 18px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  transition: filter 0.15s ease-out, transform 0.1s ease-out;
}
.btn-main { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn:hover { filter: brightness(0.96); }
.btn:active { transform: translateY(1px); }
/* 좁은 화면(≤1279px)은 미리보기가 폼 아래라 버튼 옆에 바로 내려가는 링크. 두 칸 화면에서는 숨김 */
.to-preview {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0 6px;
  color: var(--accent);
  font-weight: 700;
  text-underline-offset: 3px;
}

/* ---- A4 미리보기 ---- */
.a4-scale-wrap {
  width: 100%;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  background: #e7ecf2;
  border-radius: 8px;
  padding: 8px 0;
}

.a4 {
  background: #ffffff;
  color: #111;
  width: 210mm;
  min-height: 297mm;
  padding: 18mm 18mm 14mm;
  box-shadow: 0 2px 10px rgba(22, 32, 43, 0.14);
  font-family: var(--font);
  font-size: 11pt;
  line-height: 1.5;
  transform-origin: top center;
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
}

.a4-title {
  text-align: center;
  font-size: 26pt;
  font-weight: 800;
  letter-spacing: 0.3em;
  text-indent: 0.3em; /* 자간 때문에 오른쪽으로 쏠리는 것 보정 */
  margin: 2mm 0 8mm;
}

.a4-amount {
  margin: 0 0 6mm;
  padding: 3.5mm 4mm;
  border: 2px solid #111;
  text-align: center;
  font-size: 14pt;
  font-weight: 800;
  letter-spacing: 0.02em;
  font-variant-numeric: tabular-nums;
}

.a4-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  border-top: 2px solid #111;
  border-bottom: 2px solid #111;
}
.a4-table th, .a4-table td {
  border: 1px solid #444;
  padding: 2.2mm 3mm;
  text-align: left;
  vertical-align: middle;
  overflow-wrap: anywhere;
  word-break: keep-all;
}
.a4-table tr:first-child th, .a4-table tr:first-child td { border-top: 0; }
.a4-table tr:last-child th, .a4-table tr:last-child td { border-bottom: 0; }
.a4-table th:first-child { border-left: 0; }
.a4-table td:last-child { border-right: 0; }
.c-label { width: 32mm; }
.c-group { width: 30mm; }
.c-plabel { width: 34mm; }
.a4-label {
  white-space: nowrap;
  background: #f7f8fa;
  font-weight: 600;
}
.a4-group {
  background: #eef1f5;
  text-align: center !important;
  font-weight: 700;
  padding-left: 1.5mm !important;
  padding-right: 1.5mm !important;
}
.a4-val { height: 9mm; white-space: pre-wrap; }
.a4-group-sub { display: block; font-weight: 600; font-size: 0.9em; }
.a4-name { display: flex; align-items: baseline; justify-content: space-between; gap: 4mm; }
.a4-seal { flex: 0 0 auto; color: #555; font-size: 10pt; }

.a4-statement {
  text-align: center;
  font-size: 12.5pt;
  font-weight: 600;
  margin: 9mm 0 5mm;
}
.a4-date { text-align: center; font-size: 12pt; margin: 0 0 8mm; letter-spacing: 0.04em; }

.a4.measuring { min-height: 0; }
/* 긴 입력: app.js fitDensity 가 A4 높이를 넘을 때만 한 단계씩 붙인다(인쇄 1쪽 유지) */
.a4.dense-1 { font-size: 10pt; padding-top: 14mm; padding-bottom: 11mm; }
.a4.dense-1 .a4-title { font-size: 22pt; margin-bottom: 6mm; }
.a4.dense-1 .a4-amount { font-size: 13pt; padding: 2.5mm 3mm; margin-bottom: 5mm; }
.a4.dense-1 .a4-table th, .a4.dense-1 .a4-table td { padding: 1.6mm 2.6mm; }
.a4.dense-1 .a4-val { height: 8mm; }
.a4.dense-1 .a4-statement { margin: 7mm 0 4mm; font-size: 11.5pt; }
.a4.dense-1 .a4-date { margin-bottom: 6mm; }
.a4.dense-2 { font-size: 9pt; line-height: 1.4; padding-top: 11mm; padding-bottom: 9mm; }
.a4.dense-2 .a4-title { font-size: 19pt; margin: 0 0 4mm; }
.a4.dense-2 .a4-amount { font-size: 12pt; padding: 2mm 3mm; margin-bottom: 4mm; }
.a4.dense-2 .a4-table th, .a4.dense-2 .a4-table td { padding: 1.1mm 2.2mm; }
.a4.dense-2 .a4-val { height: 6.5mm; }
.a4.dense-2 .a4-statement { margin: 5mm 0 3mm; font-size: 10.5pt; }
.a4.dense-2 .a4-date { margin-bottom: 4mm; font-size: 10.5pt; }

/* ---- 도움말 ---- */
.help {
  max-width: 1100px;
  margin: 8px auto 0;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.help h2 { margin-top: 0; font-size: 1.1rem; }
.help h3 { font-size: 1rem; margin: 28px 0 10px; }
.help p { max-width: 75ch; }
.steps { padding-left: 1.4em; margin: 0; }
.steps li { margin-bottom: 10px; max-width: 75ch; }
.steps strong { display: block; }
.help-list { padding-left: 1.2em; }
.help-list li { margin-bottom: 8px; max-width: 75ch; }
.law { border: 1px solid var(--line); border-radius: 8px; padding: 12px; background: #fafcfe; }
.law-list { margin: 0; padding-left: 1.2em; }
.law-list li { margin-bottom: 8px; max-width: 75ch; }
.law-list li:last-child { margin-bottom: 0; }
.help details {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  margin-bottom: 8px;
}
.help summary { cursor: pointer; font-weight: 600; min-height: 44px; display: flex; align-items: center; }
.help details p { margin: 8px 0 0; color: var(--sub); }
.sources { list-style: none; margin: 0; padding: 0; }
.sources a { color: var(--accent); display: inline-flex; align-items: center; min-height: 44px; text-underline-offset: 3px; }
.asof { margin: 12px 0 0; font-size: 0.85rem; color: var(--sub); }
.disclaimer { margin: 12px 0 0; padding: 12px; border-radius: 8px; background: #f4f6f9; font-weight: 600; }
.related { margin: 16px 0 0; }
.related a { color: var(--accent); display: inline-flex; align-items: center; min-height: 44px; text-underline-offset: 3px; }

/* ---- 바닥글 ---- */
.site-footer {
  max-width: 1100px;
  margin: 24px auto 0;
  padding: 16px;
  color: var(--sub);
  font-size: 0.85rem;
}
.site-footer .footer-brand { font-weight: 700; color: var(--ink); margin-bottom: 6px; }
.footer-links { list-style: none; padding: 0; display: flex; gap: 12px; flex-wrap: wrap; margin: 10px 0; }
.footer-links a { color: var(--accent); display: inline-flex; align-items: center; min-height: 44px; }

/* 본문 바로가기·터치 반응·제목 줄바꿈 (형제와 같음) */
.skip { position: absolute; left: 8px; top: -60px; z-index: 10; padding: 8px 14px; border-radius: 8px; background: var(--accent); color: var(--accent-ink); font-weight: 700; text-decoration: none; }
.skip:focus-visible { top: 8px; }
button, a, summary, .chip, .check { touch-action: manipulation; -webkit-tap-highlight-color: rgba(31, 95, 174, 0.15); }
h1, h2, h3 { text-wrap: balance; }
#preview { scroll-margin-top: 12px; }

/* ---- 좁은 화면(갤럭시 Z 폴드5 접힘 약 360px · 펼침 약 1,100px 포함): 1279px 까지 한 칸 ---- */
@media (max-width: 1279px) {
  .layout { grid-template-columns: 1fr; }
}
@media (min-width: 480px) {
  .field-row { grid-template-columns: 1fr 1fr; }
  /* 이율·지급일 한 줄, 판정은 그 바로 아래 한 폭 */
  .rate-block { grid-template-columns: 1fr 1fr; grid-template-areas: "rate pay" "verdict verdict"; }
  .rate-block .rate-field { grid-area: rate; }
  .rate-block .payday-field { grid-area: pay; }
  .rate-block .verdict { grid-area: verdict; }
}
@media (min-width: 720px) {
  .field-row.three { grid-template-columns: 1fr 1.4fr 1fr; }
}
/* 1280px 이상에서만 입력·미리보기를 나란히, 미리보기는 따라 내려온다 */
@media (min-width: 1280px) {
  .to-preview { display: none; }
  .layout { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: start; max-width: 1240px; }
  .preview-panel { position: sticky; top: 12px; }
}
@media (prefers-reduced-motion: reduce) {
  .chip span, .btn { transition: none; }
}

/* ---- 인쇄: A4 한 장 ---- */
@page {
  size: A4;
  margin: 0;
}
@media print {
  html, body { margin: 0; padding: 0; background: #fff; }
  .skip,
  .site-header,
  .form-panel,
  .actions,
  .help,
  .site-footer,
  ins,
  .snx-ad,
  #snx-hd,
  .preview-panel > h2 { display: none !important; }
  .layout,
  .preview-panel,
  .a4-scale-wrap {
    display: block;
    max-width: none;
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: none;
    box-shadow: none;
    position: static;
    height: auto !important;
    overflow: visible;
  }
  .a4 {
    width: 210mm;
    min-height: 0;
    height: 296mm; /* 한 장 높이에 맞춤(297mm 는 반올림으로 빈 둘째 장이 붙을 수 있음) */
    margin: 0;
    box-shadow: none;
    transform: none !important;
    break-inside: avoid;
    page-break-inside: avoid;
    break-after: avoid;
  }
  .a4-table, .a4-table tr, .a4-statement, .a4-date, .a4-amount { break-inside: avoid; page-break-inside: avoid; }
}
