:root {
  color-scheme: light;
  --bg: #f3f5f8;
  --card: #ffffff;
  --ink: #1b2230;
  --muted: #687385;
  --line: #e1e5eb;
  --line-strong: #c9d0da;
  --primary: #1f4e8c;
  --primary-ink: #ffffff;
  --primary-soft: #e8eff8;
  --danger: #b4322a;
  --danger-soft: #fbeceb;
  --ok: #1d7a4c;
  --ok-soft: #e7f5ee;
  --warn: #8a5a00;
  --warn-soft: #fff6e0;
  --radius: 10px;
  --font: "Pretendard", "Malgun Gothic", "맑은 고딕", "Apple SD Gothic Neo", "Noto Sans KR", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.5 var(--font); -webkit-text-size-adjust: 100%; }
[hidden] { display: none !important; }
a { color: var(--primary); }
h2 { font-size: 17px; margin: 0 0 12px; }
h3 { font-size: 15px; margin: 18px 0 8px; }
.muted { color: var(--muted); }
small { color: var(--muted); font-weight: 400; }

/* ── 버튼 · 입력 ─────────────────────────── */
.btn, button {
  font: inherit; font-size: 14px; border: 1px solid var(--line-strong); background: #fff; color: var(--ink);
  border-radius: 8px; padding: 8px 13px; cursor: pointer; white-space: nowrap; min-height: 38px;
}
button:hover { border-color: var(--primary); }
button:disabled { opacity: .55; cursor: default; }
.btn.primary, button.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); font-weight: 600; }
button.danger { color: var(--danger); border-color: #e3b3ae; }
button.danger:hover { background: var(--danger-soft); }
button.ghost { background: transparent; border-color: transparent; color: var(--muted); }
button.sm { min-height: 30px; padding: 4px 9px; font-size: 13px; }
button.icon { min-height: 30px; padding: 2px 8px; font-size: 13px; }
input, select, textarea {
  font: inherit; font-size: 15px; color: var(--ink); background: #fff; border: 1px solid var(--line-strong);
  border-radius: 8px; padding: 7px 10px; min-height: 38px; width: 100%;
}
textarea { resize: vertical; line-height: 1.5; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
input[type="checkbox"] { width: 17px; height: 17px; min-height: 0; accent-color: var(--primary); }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
label.chk { flex-direction: row; align-items: center; gap: 7px; color: var(--ink); font-size: 14px; }

/* ── 머리 ─────────────────────────────── */
.topbar {
  position: sticky; top: 0; z-index: 20; background: var(--primary); color: #fff;
  display: flex; align-items: center; gap: 18px; padding: 0 16px; min-height: 52px;
}
.brand { color: #fff; text-decoration: none; font-weight: 700; font-size: 17px; white-space: nowrap; }
.brand small { color: #c9d8ee; font-weight: 500; }
.tabs { display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tabs a {
  color: #d7e3f4; text-decoration: none; padding: 14px 12px 12px; font-weight: 600; font-size: 15px;
  border-bottom: 3px solid transparent; white-space: nowrap;
}
.tabs a.on { color: #fff; border-bottom-color: #fff; }
main { max-width: 1500px; margin: 0 auto; padding: 16px; }

/* ── 카드 · 표 ─────────────────────────── */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; }
.card.error { color: var(--danger); background: var(--danger-soft); }
.loading { padding: 40px; text-align: center; color: var(--muted); }
.grid-form { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px 12px; align-items: end; }
.grid-form .wide { grid-column: span 2; }
.grid-form .full { grid-column: 1 / -1; }
.grid-form .actions { display: flex; gap: 8px; }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.toolbar > input, .toolbar > select { width: auto; min-width: 130px; }
.toolbar .spacer { flex: 1; }
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table.list { width: 100%; border-collapse: collapse; font-size: 14px; }
table.list th { text-align: left; font-size: 12.5px; color: var(--muted); font-weight: 600; padding: 8px; border-bottom: 1px solid var(--line-strong); white-space: nowrap; background: #fafbfc; }
table.list td { padding: 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.list tr.clickable { cursor: pointer; }
table.list tr.clickable:hover td { background: #f7f9fc; }
table.list tr.editing td { background: var(--warn-soft); }
table.list tfoot td { font-weight: 700; border-top: 2px solid var(--line-strong); border-bottom: 0; }
td.r, th.r { text-align: right; font-variant-numeric: tabular-nums; }
td.c, th.c { text-align: center; }
td.nowrap { white-space: nowrap; }
td.act { white-space: nowrap; text-align: right; }
.empty { text-align: center; color: var(--muted); padding: 28px 8px !important; }
.badge { display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 99px; background: var(--ok-soft); color: var(--ok); text-decoration: none; white-space: nowrap; }
.badge.gray { background: #eef0f3; color: var(--muted); }
.badge.blue { background: var(--primary-soft); color: var(--primary); }
.badge.red { background: var(--danger-soft); color: var(--danger); }
.note { font-size: 13px; color: var(--muted); }
.notice { border-radius: 8px; padding: 10px 12px; font-size: 14px; margin: 10px 0; background: var(--primary-soft); }
.notice.warn { background: var(--warn-soft); color: var(--warn); }
.notice.err { background: var(--danger-soft); color: var(--danger); }
.notice.ok { background: var(--ok-soft); color: var(--ok); }

/* ── 문서 편집 ─────────────────────────── */
.editor-wrap { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; align-items: start; }
.editor-wrap > * { min-width: 0; }
.editor-head { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 12px; }
.editor-head h2 { margin: 0; flex: 1; min-width: 140px; }
.btnrow { display: flex; flex-wrap: wrap; gap: 6px; }
.import { border: 1px dashed var(--line-strong); border-radius: 8px; padding: 12px; margin: 14px 0; background: #fafbfd; }
.import .row { display: flex; flex-wrap: wrap; gap: 10px 14px; align-items: center; }
.import .row input[type="date"] { width: auto; }
table.lines { width: 100%; border-collapse: collapse; min-width: 640px; }
table.lines th { font-size: 12px; color: var(--muted); font-weight: 600; text-align: left; padding: 4px 3px; border-bottom: 1px solid var(--line-strong); }
table.lines td { padding: 3px; border-bottom: 1px solid var(--line); }
table.lines input { min-height: 34px; padding: 5px 7px; font-size: 14px; border-radius: 6px; }
table.lines .num { text-align: right; }
table.lines td.calc { text-align: right; font-variant-numeric: tabular-nums; font-size: 14px; padding-right: 6px; white-space: nowrap; color: var(--muted); }
table.lines td.idx { color: var(--muted); font-size: 12px; text-align: center; width: 26px; }
.sumbar { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 6px 22px; padding: 12px 4px 4px; font-size: 15px; }
.sumbar b { font-variant-numeric: tabular-nums; }
.sumbar .grand { font-size: 17px; color: var(--primary); }
.maillog { margin-top: 12px; font-size: 13px; }
.maillog div { padding: 3px 0; color: var(--muted); }
.warn-text { color: var(--warn); font-size: 13px; }
.preview-pane { position: sticky; top: 64px; }
.preview-box { background: #dfe3e9; border-radius: var(--radius); padding: 12px; overflow: hidden; }
.pv-fit { position: relative; overflow: hidden; }
.pv-scale { transform-origin: top left; }
.pv-scale .sheet { box-shadow: 0 1px 6px rgba(0, 0, 0, .18); margin-bottom: 14px; }
.preview-title { display: flex; justify-content: space-between; align-items: center; margin: 0 0 8px; font-size: 13px; color: var(--muted); }

/* ── 모달 · 알림 ───────────────────────── */
.modal { position: fixed; inset: 0; z-index: 100; background: rgba(20, 28, 40, .45); display: flex; align-items: flex-start; justify-content: center; padding: 6vh 12px; overflow-y: auto; }
.modal-card { background: #fff; border-radius: 12px; width: 100%; max-width: 560px; padding: 20px; box-shadow: 0 10px 40px rgba(0, 0, 0, .25); }
.modal-card h2 { margin-bottom: 14px; }
.modal-card .grid-form { grid-template-columns: 1fr; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 200; background: #1b2230; color: #fff; padding: 11px 18px; border-radius: 10px; font-size: 14px; max-width: calc(100vw - 32px); box-shadow: 0 6px 24px rgba(0, 0, 0, .25); }
.toast.err { background: var(--danger); }
.toast.ok { background: var(--ok); }

/* ── 로그인 ─────────────────────────────── */
.login { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { background: #fff; border: 1px solid var(--line); border-radius: 14px; padding: 30px 26px; width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 12px; text-align: center; }
.login-card h1 { font-size: 19px; margin: 4px 0 0; }
.login-card p { margin: 0; }
.login-mark { width: 52px; height: 52px; border-radius: 12px; background: var(--primary); color: #fff; font-weight: 700; display: flex; align-items: center; justify-content: center; margin: 0 auto; }
.login-card .err { color: var(--danger); font-size: 14px; }

/* ── 설정 ─────────────────────────────── */
.settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 14px; align-items: start; }
.settings-grid .card { margin: 0; }
.stamp-box { display: flex; align-items: center; gap: 14px; margin-top: 12px; }
.stamp-box img { width: 64px; height: 64px; object-fit: contain; border: 1px solid var(--line); border-radius: 8px; background: #fff; }

@media (max-width: 1100px) {
  .editor-wrap { grid-template-columns: minmax(0, 1fr); }
  .preview-pane { position: static; }
}
@media (max-width: 640px) {
  main { padding: 10px; }
  .card { padding: 13px; }
  .topbar { gap: 8px; padding: 0 10px; }
  .brand small { display: none; }
  .tabs a { padding: 14px 9px 12px; font-size: 14px; }
  .grid-form { grid-template-columns: 1fr 1fr; }
  .grid-form .wide { grid-column: 1 / -1; }
  .toolbar > input, .toolbar > select { flex: 1 1 140px; }
  .settings-grid { grid-template-columns: 1fr; }
}

/* ══ A4 문서 (화면 미리보기 · 인쇄 · PDF 공통) ══════════════════ */
.sheet {
  width: 210mm; height: 297mm; padding: 26mm 14mm 10mm 19mm; background: #fff; color: #000; position: relative; overflow: hidden;
  font-family: Dotum, "돋움", "Malgun Gothic", "맑은 고딕", "Apple SD Gothic Neo", "Noto Sans KR", sans-serif; font-size: 10pt; line-height: 1.2;
}
.sheet * { box-sizing: border-box; }
.sheet h1 { margin: 0 0 7mm; text-align: center; font-size: 19pt; font-weight: 700; letter-spacing: .4em; text-indent: .4em; }
.sheet table { border-collapse: collapse; width: 100%; table-layout: fixed; }
.sheet th { font-weight: 400; }
.sheet .j { text-align: justify; text-align-last: justify; }
.sheet .r { text-align: right; }
.sheet .c { text-align: center; }

.sheet .head { display: flex; gap: 2.5mm; }
.sheet .head-left { flex: 1; display: flex; flex-direction: column; padding-left: 2mm; }
.sheet .hl { flex: 1; display: flex; align-items: flex-end; border-bottom: 1.3px solid #000; padding: 0 1mm 1.2mm; font-size: 10.5pt; }
.sheet .hl.date { padding-left: 9mm; border-bottom-width: 1.6px; }
.sheet .hl .to { flex: 1; }
.sheet .hl .gwiha { margin-right: 9mm; }
.sheet .hl.msg { border-bottom: 0; }
.sheet .supplier { width: 89mm; flex: none; border: 1.6px solid #000; }
.sheet .supplier th, .sheet .supplier td { border: .7px solid #000; height: 6.5mm; padding: 0 1.6mm; white-space: nowrap; overflow: hidden; font-size: 9.5pt; }
.sheet .supplier th.j { padding: 0 1.8mm; font-size: 9pt; }
.sheet .supplier th.vert { padding: 0; text-align: center; line-height: 2.1; }
.sheet .supplier td.ceo { position: relative; overflow: visible; }
.sheet .supplier .in { position: absolute; right: 1.2mm; top: 50%; transform: translateY(-50%); }
.sheet .supplier .stamp { position: absolute; right: -2.2mm; top: 50%; width: 13.5mm; height: 13.5mm; transform: translateY(-50%); object-fit: contain; }
.sheet .sumline { display: flex; align-items: baseline; margin: 3mm 0 0; padding: 0 2mm; font-size: 10.5pt; height: 6mm; }
.sheet .sumline .lbl { width: 28mm; }
.sheet .sumline .kor { width: 54mm; text-align: right; padding-right: 2mm; letter-spacing: .02em; }
.sheet .sumline .won { width: 14mm; }
.sheet .sumline .paren { width: 50mm; display: flex; }
.sheet .sumline .paren b { flex: 1; text-align: right; font-weight: 400; padding-right: 9mm; }
.sheet .sumline .vat { font-weight: 700; font-size: 11pt; white-space: nowrap; margin-left: auto; padding-right: 4mm; }
.sheet .items { margin-top: 7mm; border: 1.6px solid #000; }
.sheet .items th, .sheet .items td { border: .7px solid #000; height: 6.35mm; padding: 0 1.6mm; white-space: nowrap; overflow: hidden; font-size: 10pt; }
.sheet .items thead th { height: 8.2mm; border-bottom: 1.6px solid #000; }
.sheet .items td.long { font-size: 8.6pt; letter-spacing: -.02em; }
.sheet .items tr.filler td { text-align: center; letter-spacing: .05em; }
.sheet .items tr.sum th, .sheet .items tr.sum td { border-top: 1.6px solid #000; }
.sheet .items tr.grand th, .sheet .items tr.grand td { border-top: 1.6px solid #000; }
.sheet .items tr.mold th, .sheet .items tr.mold td { height: 8.2mm; border-bottom: 1.6px solid #000; }
.sheet .items tr.mold td { font-weight: 700; text-align: center; }
.sheet .signbox { border: 1.6px solid #000; border-top: 0; height: 19mm; display: flex; align-items: center; justify-content: flex-end; padding-right: 9mm; font-size: 10.5pt; }
.sheet .memobox { border: 1.6px solid #000; border-top: 0; height: 63mm; padding: 7mm 6mm; font-size: 10.5pt; line-height: 1.9; }
.sheet .memobox p { margin: 0 0 3.5mm; display: flex; }
.sheet .memobox .k { white-space: pre; }
.sheet .memobox .v { flex: 1; white-space: pre-wrap; word-break: keep-all; }
.sheet .foot { text-align: center; font-size: 8pt; letter-spacing: .3em; margin-top: 1.5mm; }
.sheet .pageno { position: absolute; bottom: 7mm; right: 14mm; font-size: 8pt; color: #444; }

.sheet.hist { padding: 16mm 14mm 12mm 14mm; }
.sheet.hist .htitle { display: flex; align-items: flex-end; margin-bottom: 1mm; font-weight: 700; }
.sheet.hist .htitle .t { flex: 1; text-align: center; font-size: 12.5pt; }
.sheet.hist .htitle .co { font-size: 10.5pt; padding-right: 4mm; }
.sheet.hist table { border: 1.3px solid #000; }
.sheet.hist th, .sheet.hist td { border: .7px solid #000; height: 6.05mm; padding: 0 1.6mm; font-size: 10pt; white-space: nowrap; overflow: hidden; }
.sheet.hist thead th { border-bottom: 1.3px solid #000; }
.sheet.hist tfoot td { border-top: 1.3px solid #000; font-weight: 700; }

/* ── 인쇄: 문서만 ───────────────────────── */
#print-root { display: none; }
@page { size: A4; margin: 0; }
@media print {
  html, body { background: #fff; }
  body > *:not(#print-root) { display: none !important; }
  #print-root { display: block; }
  #print-root .sheet { box-shadow: none; margin: 0; break-after: page; page-break-after: always; }
  #print-root .sheet:last-child { break-after: auto; page-break-after: auto; }
  .sheet { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
