/* ===== 감리일지 프로토타입 — 화면(UI) + 출력(A4 서식) ===== */
:root {
  --bg: #eef1f5;
  --surface: #ffffff;
  --text: #111827;
  --muted: #4b5563;
  --line: #cfd6df;
  --line-strong: #9aa5b4;
  --accent: #1d4ed8;
  --accent-weak: #e6edfc;
  --brand: #1e3a5f;
  --danger: #b42318;
  --ok: #067647;
  --radius: 12px;
  --tap: 48px;
  --ui-font: "Pretendard", "Malgun Gothic", "맑은 고딕", "Apple SD Gothic Neo", "Noto Sans KR", system-ui, sans-serif;
  --form-font: "Malgun Gothic", "맑은 고딕", "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
  --topbar-h: 56px;
  --bottomnav-h: 0px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 16px/1.6 var(--ui-font);
  -webkit-text-size-adjust: 100%;
}
button, input, textarea, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }

/* ---------- 상단바 ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30; height: var(--topbar-h);
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 0 16px; background: var(--brand); color: #fff;
}
.brand { font-weight: 800; font-size: 19px; letter-spacing: -.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-sub { font-weight: 500; font-size: 15px; opacity: .85; margin-left: 8px; }
.tb-actions { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.saved { font-size: 13px; opacity: .85; min-width: 52px; text-align: right; }
.topbar .btn.ghost { color: #fff; border-color: rgba(255,255,255,.45); background: transparent; }
.demo-banner { background: #fff4d6; color: #5c3d00; padding: 8px 16px; font-size: 14px; border-bottom: 1px solid #ecd28a; }

/* ---------- 버튼/입력 ---------- */
.btn {
  min-height: 44px; padding: 0 16px; border-radius: 10px; border: 1px solid var(--line-strong);
  background: var(--surface); cursor: pointer; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  white-space: nowrap; text-decoration: none;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--danger); border-color: #e4b4ae; }
.btn.block { width: 100%; }
.btn.big { min-height: 56px; font-size: 17px; flex: 1; }
.icon {
  width: 40px; height: 40px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface);
  cursor: pointer; font-size: 15px; display: inline-flex; align-items: center; justify-content: center;
}
.icon:disabled { opacity: .35; cursor: default; }
.icon.danger { color: var(--danger); }
:focus-visible { outline: 3px solid #93b4ff; outline-offset: 1px; }

.fld { display: block; margin: 0 0 12px; }
.fld > span { display: block; font-size: 14px; font-weight: 700; color: var(--muted); margin-bottom: 4px; }
.hint { font-style: normal; font-weight: 500; color: var(--muted); font-size: 13px; margin-left: 6px; }
.fld input[type=text], .fld input:not([type]), .fld input[type=date], .fld input[type=search], .fld textarea, .fld select {
  width: 100%; min-height: var(--tap); padding: 10px 12px; border: 1px solid var(--line-strong); border-radius: 10px;
  background: #fff; font-size: 17px; line-height: 1.5;
}
.fld textarea { resize: vertical; min-height: 48px; overflow: hidden; }
.fld input:focus, .fld textarea:focus, .fld select:focus { border-color: var(--accent); outline: 3px solid var(--accent-weak); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }

.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button {
  min-height: 42px; padding: 0 14px; border-radius: 999px; border: 1px solid var(--line-strong); background: #fff; cursor: pointer; font-weight: 600;
}
.seg button.on { background: var(--brand); border-color: var(--brand); color: #fff; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chips button { min-height: 36px; padding: 0 12px; border-radius: 999px; border: 1px dashed var(--line-strong); background: #fafbfc; cursor: pointer; font-size: 14px; }
.chips button:hover { border-color: var(--accent); color: var(--accent); }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin: 0 0 14px; }
.card-title { margin: 0 0 12px; font-size: 18px; font-weight: 800; }
.card-title-row { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.card-title-row .card-title { margin: 0; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 4px; }
.btn-row .btn { flex: 1 1 auto; }
.empty { color: var(--muted); text-align: center; padding: 24px 8px; margin: 0; }
.empty.small { padding: 12px 8px; font-size: 15px; }

/* ---------- 레이아웃 ---------- */
.app {
  display: grid; grid-template-columns: 290px minmax(440px, 1fr) minmax(420px, 44%);
  height: calc(100dvh - var(--topbar-h)); overflow: hidden;
}
body.has-demo .app { height: calc(100dvh - var(--topbar-h) - 38px); }
.sidebar, .editor, .preview { overflow-y: auto; min-height: 0; }
.sidebar { background: var(--surface); border-right: 1px solid var(--line); padding: 16px; }
.editor { display: flex; flex-direction: column; }
.editor-body { padding: 16px; max-width: 860px; width: 100%; margin: 0 auto; }
.preview { background: #d7dce3; border-left: 1px solid var(--line); display: flex; flex-direction: column; }
.pv-bar { position: sticky; top: 0; z-index: 2; display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: #c9cfd8; }
.pv-close { display: none; margin-left: auto; }
.pv-pages { padding: 16px; display: flex; flex-direction: column; align-items: center; gap: 16px; }
.pv-wrap { box-shadow: 0 2px 10px rgba(0,0,0,.18); background: #fff; }

.tabs { position: sticky; top: 0; z-index: 5; display: flex; gap: 4px; padding: 10px 16px 0; background: var(--bg); border-bottom: 1px solid var(--line); }
.tabs button { min-height: 46px; padding: 0 18px; border: 0; border-bottom: 3px solid transparent; background: none; cursor: pointer; font-weight: 700; font-size: 16px; color: var(--muted); }
.tabs button.on { color: var(--accent); border-bottom-color: var(--accent); }

/* 목록 */
.list-head { display: flex; align-items: baseline; justify-content: space-between; }
.list-head h2 { margin: 0 0 10px; font-size: 20px; }
.list-actions { display: grid; gap: 8px; margin-bottom: 14px; }
.entries { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.entry {
  width: 100%; text-align: left; border: 1px solid var(--line); border-radius: 12px; background: #fff; padding: 12px 14px; cursor: pointer;
  display: grid; gap: 2px;
}
.entry:hover { border-color: var(--accent); }
.entry.on { border-color: var(--accent); background: var(--accent-weak); box-shadow: inset 4px 0 0 var(--accent); }
.e-date { font-size: 18px; font-weight: 800; }
.e-wd { font-weight: 600; color: var(--muted); font-size: 15px; }
.e-sum { font-size: 15px; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.e-meta { font-size: 13px; color: var(--muted); }

/* 작업사항 행 카드 */
.rows { display: grid; gap: 12px; margin-bottom: 12px; }
.row-card { border: 1px solid var(--line); border-left: 4px solid var(--accent); border-radius: 10px; padding: 12px; background: #fbfcfe; }
.row-top { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.row-no { font-weight: 800; color: var(--accent); font-size: 15px; }
.row-tools { display: flex; gap: 6px; }

/* 사진 */
.add-photo { display: flex; gap: 10px; margin-bottom: 12px; }
.drop { border: 2px dashed var(--line-strong); border-radius: 12px; padding: 18px; text-align: center; color: var(--muted); margin-bottom: 12px; }
.drop.over { border-color: var(--accent); background: var(--accent-weak); color: var(--accent); }
.photo-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; }
.photo-card { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; display: flex; flex-direction: column; }
.photo-card.dragging { opacity: .4; }
.photo-card.drop-target { outline: 3px solid var(--accent); }
.ph-img { position: relative; aspect-ratio: 4 / 3; background: #e5e8ec; display: flex; align-items: center; justify-content: center; }
.ph-img img { width: 100%; height: 100%; object-fit: contain; background: #fff; }
.ph-no { position: absolute; top: 8px; left: 8px; background: var(--brand); color: #fff; font-weight: 800; border-radius: 8px; padding: 2px 10px; }
.ph-fields { padding: 12px; }
.ph-cap { font-size: 14px; color: var(--muted); background: #f5f7fa; border-radius: 8px; padding: 6px 10px; margin-bottom: 10px; word-break: keep-all; }
.ph-tools { display: flex; gap: 6px; justify-content: flex-end; }
.sign-box { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.sign-box img { height: 56px; max-width: 140px; object-fit: contain; border: 1px solid var(--line); border-radius: 8px; background: #fff; padding: 4px; }
.radio { display: flex; gap: 10px; align-items: flex-start; padding: 12px; border: 1px solid var(--line); border-radius: 10px; margin-bottom: 8px; cursor: pointer; }
.radio input { width: 22px; height: 22px; margin-top: 2px; flex-shrink: 0; }
.radio.on { border-color: var(--accent); background: var(--accent-weak); }
.check { display: flex; gap: 10px; align-items: center; min-height: 44px; cursor: pointer; }
.check input { width: 22px; height: 22px; }

/* 하단 내비 (모바일) */
.bottomnav { display: none; }

/* ---------- 모달 ---------- */
.modal { position: fixed; inset: 0; z-index: 50; background: rgba(15, 23, 42, .5); display: flex; align-items: center; justify-content: center; padding: 16px; }
.sheet-modal {
  background: #fff; border-radius: 16px; width: min(980px, 100%); max-height: calc(100dvh - 32px); display: flex; flex-direction: column; overflow: hidden;
}
.sm-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.sm-head h3 { margin: 0; font-size: 19px; }
.sm-body { padding: 16px 18px; overflow-y: auto; flex: 1; min-height: 0; }
.sm-foot { padding: 12px 18px; border-top: 1px solid var(--line); display: flex; gap: 10px; align-items: center; flex-wrap: wrap; background: #fafbfc; }
.sm-foot .fld { margin: 0; flex: 1 1 220px; }
.cl-layout { display: grid; grid-template-columns: 260px 1fr; gap: 16px; }
.cl-gongs { display: grid; gap: 4px; align-content: start; }
.cl-gongs button { text-align: left; min-height: 42px; padding: 6px 12px; border: 1px solid transparent; border-radius: 8px; background: none; cursor: pointer; font-size: 15px; }
.cl-gongs button.on { background: var(--accent-weak); border-color: var(--accent); color: var(--accent); font-weight: 700; }
.cl-gongs button .cnt { color: var(--muted); font-size: 13px; margin-left: 4px; }
.cl-select { display: none; }
.cl-group h4 { margin: 14px 0 6px; font-size: 15px; color: var(--brand); border-bottom: 1px solid var(--line); padding-bottom: 4px; }
.cl-group h4 .k { font-size: 12px; font-weight: 700; color: #fff; background: var(--muted); border-radius: 6px; padding: 1px 6px; margin-right: 6px; vertical-align: 2px; }
.cl-group h4 .k.base { background: var(--accent); }
.cl-item { display: flex; gap: 10px; align-items: flex-start; padding: 8px 6px; border-radius: 8px; cursor: pointer; font-size: 16px; line-height: 1.5; }
.cl-item:hover { background: #f3f6fb; }
.cl-item input { width: 22px; height: 22px; margin-top: 2px; flex-shrink: 0; }
.cl-item.sub { padding-left: 30px; font-size: 15px; color: #374151; }
.cl-item .from { display: block; font-size: 12px; color: var(--muted); }
.cl-search { position: sticky; top: -16px; background: #fff; padding-bottom: 8px; z-index: 1; }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--bottomnav-h)); transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none;
  background: #111827; color: #fff; padding: 10px 18px; border-radius: 10px; transition: .2s; z-index: 60; font-size: 15px; max-width: 90vw;
}
.toast.show { opacity: 1; transform: translateX(-50%); }

#measure { position: absolute; left: -10000px; top: 0; visibility: hidden; pointer-events: none; }
#print-root { display: none; }

/* ---------- 반응형 ---------- */
@media (max-width: 1279px) {
  .app { grid-template-columns: 270px 1fr; }
  .preview { display: none; }
  body.pv-open .preview { display: flex; position: fixed; z-index: 40; top: var(--topbar-h); right: 0; bottom: 0; width: min(720px, 100%); box-shadow: -6px 0 24px rgba(0,0,0,.25); }
  body.pv-open .pv-close { display: inline-flex; }
}
@media (min-width: 1280px) { .tb-pv { display: none; } }

@media (max-width: 899px) {
  :root { --bottomnav-h: 64px; --topbar-h: 52px; }
  body { font-size: 17px; }
  .brand { font-size: 18px; }
  .brand-sub { display: none; }
  .tb-pv { display: none; }
  .app { display: block; height: auto; overflow: visible; padding-bottom: calc(var(--bottomnav-h) + env(safe-area-inset-bottom)); }
  body.has-demo .app { height: auto; }
  .sidebar, .editor, .preview { display: none; overflow: visible; border: 0; }
  .tabs { display: none; }
  .editor-body { padding: 12px; }
  body[data-m="list"] .sidebar { display: block; padding: 12px; background: var(--bg); }
  body[data-m="diary"] .editor, body[data-m="photos"] .editor, body[data-m="settings"] .editor { display: block; }
  body[data-m="preview"] .preview { display: flex; position: static; width: auto; box-shadow: none; min-height: calc(100dvh - var(--topbar-h) - var(--bottomnav-h)); }
  body[data-m="preview"] .pv-close { display: none; }
  .pv-pages { padding: 10px; }
  .grid2 { grid-template-columns: 1fr; }
  .card { padding: 14px; }
  .photo-list { grid-template-columns: 1fr; }
  .bottomnav {
    display: grid; grid-template-columns: repeat(5, 1fr); position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    height: calc(var(--bottomnav-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
    background: #fff; border-top: 1px solid var(--line);
  }
  .bottomnav button { border: 0; background: none; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0; font-size: 13px; font-weight: 700; color: var(--muted); cursor: pointer; }
  .bottomnav .bn-i { font-size: 21px; line-height: 1.1; }
  .bottomnav button.on { color: var(--accent); }
  .modal { padding: 0; align-items: stretch; }
  .sheet-modal { border-radius: 0; max-height: none; height: 100dvh; }
  .cl-layout { grid-template-columns: 1fr; }
  .cl-gongs { display: none; }
  .cl-select { display: block; }
  .topbar .btn { min-height: 40px; padding: 0 12px; }
}

/* =====================================================================
   출력 서식 (A4) — 미리보기와 PDF 출력이 동일한 DOM/CSS를 사용
   ===================================================================== */
.sheet {
  width: 210mm; height: 297mm; padding: 14mm 15mm 11mm; background: #fff; color: #000; overflow: hidden; position: relative;
  font: 9.5pt/1.45 var(--form-font); -webkit-print-color-adjust: exact; print-color-adjust: exact;
}
.sheet * { box-sizing: border-box; }
.sheet-inner { width: 100%; }
.f-head { display: flex; justify-content: space-between; font-size: 8pt; margin-bottom: 1.5mm; }
.f-title { text-align: center; font-size: 17pt; font-weight: 700; letter-spacing: .4em; margin: 1mm 0 4mm; text-indent: .4em; }
.f-title small { font-size: 10pt; letter-spacing: 0; font-weight: 400; }
table.f { width: 100%; border-collapse: collapse; table-layout: fixed; }
table.f th, table.f td { border: .25mm solid #000; padding: 1.5mm 2mm; vertical-align: middle; word-break: keep-all; overflow-wrap: anywhere; }
table.f th { font-weight: 400; text-align: center; }
table.f .L { border-left: none; }
table.f .R { border-right: none; }
table.f tr:first-child > * { border-top: .6mm solid #000; }
table.f + table.f, table.f + .f-gap { margin-top: 2mm; }
table.hdr td.sign { position: relative; height: 11mm; }
.sg { float: right; font-size: 8pt; }
.sgimg { position: absolute; right: 1mm; top: 50%; transform: translateY(-50%); height: 10mm; max-width: 26mm; object-fit: contain; mix-blend-mode: multiply; }
.name-date { display: flex; justify-content: space-between; gap: 3mm; flex-wrap: wrap; }
table.work td { vertical-align: top; }
table.work td.c-gong { vertical-align: middle; text-align: center; }
.loc { font-size: 8.5pt; margin-top: .8mm; }
table.work tr.filler td { padding: 0; height: 0; }
.sec { vertical-align: top !important; height: 22mm; }
.sec-b { margin-top: 1mm; padding-left: 2mm; }
.g-h { background: #d9d9d9; padding: .8mm 2mm !important; }
.g-b { font-size: 7.8pt; line-height: 1.5; padding: 1.2mm 2mm !important; }
.f-foot { text-align: right; font-size: 7.5pt; margin-top: 1.2mm; }

/* 사진대지 */
.sheet-photo { padding: 14mm 21mm 11mm; }
.ph-head .f-title { font-size: 15pt; margin: 0 0 2mm; }
.ph-sub { display: flex; justify-content: space-between; gap: 4mm; font-size: 9pt; border-bottom: .6mm solid #000; padding-bottom: 1.2mm; margin-bottom: 5mm; }
.ph-grid { display: grid; gap: 7mm 4mm; justify-content: center; align-items: start; }
.per2 .ph-grid { grid-template-columns: 168mm; }
.per4 .ph-grid, .per6 .ph-grid { grid-template-columns: 82mm 82mm; gap: 5mm 4mm; }
table.pb { width: 100%; border-collapse: collapse; table-layout: fixed; }
table.pb td { border: .25mm solid #000; padding: 0; }
.pb-frame { width: 100%; display: flex; align-items: center; justify-content: center; background: #fff; overflow: hidden; }
.pb-frame img { width: 100%; height: 100%; object-fit: contain; display: block; }
.fit-cover .pb-frame img { object-fit: cover; }
.per2 .pb-frame { height: 109mm; }
.per4 .pb-frame { height: 90mm; }
.per6 .pb-frame { height: 58mm; }
td.pb-c { height: 10.5mm; padding: .8mm 2.5mm !important; font-size: 9.5pt; line-height: 1.3; vertical-align: middle; overflow: hidden; }
.per4 td.pb-c, .per6 td.pb-c { height: 7mm; font-size: 8.5pt; }
.per4 td.pb-c2, .per6 td.pb-c2 { height: 9.6mm; } /* 내용 2줄 고정 → 블록 높이 균일 */
.pb-c .clamp { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

/* ---------- 인쇄 ---------- */
@page { size: A4 portrait; margin: 0; }
@media print {
  html, body { background: #fff !important; height: auto; }
  body > *:not(#print-root) { display: none !important; }
  #print-root { display: block !important; }
  #print-root .sheet { break-after: page; page-break-after: always; box-shadow: none; }
  #print-root .sheet:last-child { break-after: auto; page-break-after: auto; }
}
/* 헤드리스 출력 검증용(?printview) */
body.printview > *:not(#print-root):not(#measure) { display: none !important; }
body.printview #print-root { display: block; }
body.printview { background: #888; }
body.printview #print-root .sheet { margin: 0 auto 8px; }
