:root {
  --bg: #f3f4f7; --panel: #fff; --ink: #1d2433; --muted: #6b7385; --line: #e2e5ec;
  --accent: #1f3a5f; --accent-2: #2c5282; --danger: #b42318; --ok: #067647;
  --radius: 10px;
}
* { box-sizing: border-box; }
body { margin: 0; font: 14px/1.5 "Segoe UI", system-ui, -apple-system, sans-serif; color: var(--ink); background: var(--bg); }
[hidden] { display: none !important; }

.topbar { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 24px; padding: 8px 28px; min-height: 56px; white-space: nowrap; background: var(--accent); color: #fff; position: sticky; top: 0; z-index: 10; }
.brand { font-weight: 700; font-size: 17px; letter-spacing: .3px; }
.topbar nav { display: flex; flex-wrap: wrap; gap: 4px; }
@media (max-width: 900px) { .topbar { position: static; padding: 8px 14px; } .user-chip { display: none; } main { padding: 16px 12px; } }
.topbar nav a { color: #cfd9e8; text-decoration: none; padding: 8px 14px; border-radius: 6px; }
.topbar nav a:hover { background: rgba(255,255,255,.08); color: #fff; }
.topbar nav a.active { background: rgba(255,255,255,.16); color: #fff; }

main { padding: 24px 28px; max-width: 1500px; margin: 0 auto; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px 22px; margin-bottom: 18px; }
.panel-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 14px; }
.panel h2 { margin: 0; font-size: 18px; }
.panel h3 { margin: 18px 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .8px; color: var(--muted); border-bottom: 1px solid var(--line); padding-bottom: 4px; }
.muted { color: var(--muted); }
.mono { font-family: Consolas, "Cascadia Mono", monospace; font-size: 13px; }
.right { text-align: right; }

label { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; font-weight: 600; color: #3b4458; flex: 1; min-width: 0; }
label small { font-weight: 400; color: var(--muted); }
label.full { margin-top: 10px; }
label.grow { flex: 2; }
input, select, textarea { font: inherit; font-weight: 400; color: var(--ink); padding: 8px 10px; border: 1px solid #cdd2dc; border-radius: 7px; background: #fff; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid #9db6d8; border-color: var(--accent-2); }
input[type=color] { padding: 2px; height: 38px; }
textarea { resize: vertical; }
fieldset { border: 0; padding: 0; margin: 0; }
fieldset:disabled input, fieldset:disabled select, fieldset:disabled textarea { background: #f6f7f9; }
.row { display: flex; gap: 12px; margin-top: 10px; flex-wrap: wrap; }
.seg { display: flex; gap: 18px; }
.seg label { flex-direction: row; align-items: center; gap: 6px; flex: none; white-space: nowrap; font-weight: 500; }
.seg input { width: auto; }

.btn { font: inherit; font-weight: 600; padding: 8px 16px; border-radius: 7px; border: 1px solid #cdd2dc; background: #fff; color: var(--ink); cursor: pointer; text-decoration: none; display: inline-block; }
.btn:hover { background: #f1f3f7; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-2); }
.btn.danger { color: var(--danger); border-color: #f1c4bf; }
.link { background: none; border: 0; color: var(--accent-2); cursor: pointer; font: inherit; padding: 0; }
.link.danger { color: var(--danger); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 18px; }

.filters { display: grid; grid-template-columns: 1.3fr 1fr 1fr 2fr; gap: 10px; margin-bottom: 14px; }
.table-wrap { overflow-x: auto; }
.grid { width: 100%; border-collapse: collapse; }
.grid th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); padding: 8px 10px; border-bottom: 1px solid var(--line); }
.grid td { padding: 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.grid.compact td { padding: 6px 10px; }
.grid tr.clickable { cursor: pointer; }
.grid tr.clickable:hover td { background: #f7f9fc; }
.tag { display: inline-block; font-size: 11px; font-weight: 700; padding: 1px 6px; border-radius: 4px; background: #e6edf6; color: var(--accent); }
.tag.ext { background: #fdf0dc; color: #93500b; }
.tag.big { font-size: 14px; padding: 8px 10px; background: var(--brand); color: #fff; }
.status { font-size: 12px; font-weight: 600; padding: 2px 10px; border-radius: 20px; }
.status.draft { background: #fff4d6; color: #8a5a00; }
.status.final { background: #dcf5e7; color: var(--ok); }

.editor { display: grid; grid-template-columns: minmax(440px, 640px) 1fr; gap: 20px; align-items: start; }
.form-panel { position: sticky; top: 76px; max-height: calc(100vh - 96px); overflow-y: auto; }
.preview-wrap { overflow-x: auto; padding-bottom: 20px; }

.ent-title { display: flex; gap: 12px; align-items: center; }
.entity-card { border-top: 4px solid var(--brand); }
/* ---------------- Rich-text editor ---------------- */
.body-field { display: flex; flex-direction: column; gap: 4px; margin-top: 10px; }
.field-label { font-size: 12.5px; font-weight: 600; color: #3b4458; }
.re { border: 1px solid #cdd2dc; border-radius: 8px; background: #fff; display: flex; flex-direction: column; }
.re:focus-within { border-color: var(--accent-2); box-shadow: 0 0 0 2px #cfdcef; }
.re-toolbar { display: flex; flex-wrap: wrap; gap: 2px 0; padding: 4px 6px; background: #f6f7f9; border-bottom: 1px solid var(--line); border-radius: 8px 8px 0 0; position: sticky; top: 0; z-index: 3; }
.re-group { display: flex; align-items: center; gap: 1px; padding: 0 5px; border-right: 1px solid #e0e3ea; }
.re-group:last-child { border-right: 0; }
.re-btn { position: relative; display: inline-grid; place-items: center; width: 30px; height: 30px; border: 1px solid transparent; border-radius: 5px; background: transparent; color: #2b3444; cursor: pointer; padding: 0; }
.re-btn:hover { background: #e6eaf1; }
.re-btn.on { background: #dbe5f4; border-color: #b7c8e4; color: var(--accent); }
.re-btn:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 1px; }
.re-select { width: auto; padding: 4px 6px; font-size: 13px; height: 30px; border-radius: 5px; }
.re-style { width: 108px; } .re-font { width: 118px; } .re-size { width: 62px; }
.re-color-btn { padding-bottom: 5px; }
.re-swatch { position: absolute; left: 7px; right: 7px; bottom: 4px; height: 3px; border-radius: 1px; }
.re-pop-wrap { position: relative; }
.re-pop { position: absolute; top: 34px; left: 0; z-index: 20; background: #fff; border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 8px 24px rgba(20,30,50,.16); padding: 8px; display: grid; grid-template-columns: repeat(5, 22px); gap: 5px; }
.re-chip { width: 22px; height: 22px; border-radius: 4px; border: 1px solid rgba(0,0,0,.15); cursor: pointer; padding: 0; }
.re-chip:hover { transform: scale(1.12); }
.re-chip.re-none { background: linear-gradient(135deg, #fff 45%, #d33 46%, #d33 54%, #fff 55%) !important; }
.re-chip-wide { grid-column: 1 / -1; font: inherit; font-size: 12px; padding: 4px; border: 1px solid var(--line); border-radius: 4px; background: #fff; cursor: pointer; }
.re-grid-pop { display: block; }
.re-grid { display: grid; grid-template-columns: repeat(8, 16px); gap: 3px; }
.re-grid span { width: 16px; height: 16px; border: 1px solid #c7ced9; border-radius: 2px; background: #fff; cursor: pointer; }
.re-grid span.hot { background: #cfe0f7; border-color: #6d95cf; }
.re-grid-label { font-size: 12px; text-align: center; margin-top: 6px; color: var(--muted); }
.re-tablebar, .re-findbar { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; padding: 5px 8px; background: #fbfcfd; border-bottom: 1px solid var(--line); font-size: 12.5px; }
.re-tablebar button, .re-findbar button { font: inherit; font-size: 12px; padding: 3px 8px; border: 1px solid #cdd2dc; border-radius: 4px; background: #fff; cursor: pointer; }
.re-tablebar button:hover, .re-findbar button:hover { background: #eef1f6; }
.re-tablebar button.danger { color: var(--danger); }
.re-tablebar-label { font-weight: 600; color: var(--muted); margin-right: 2px; }
.re-findbar input:not([type=checkbox]) { width: 150px; padding: 4px 6px; font-size: 12.5px; }
.re-case { flex-direction: row; align-items: center; gap: 4px; flex: none; font-weight: 500; font-size: 12px; }
.re-case input { width: auto; }
.re-close { margin-left: auto; font-size: 16px !important; line-height: 1; }
.re-find-status { font-size: 12px; }
.re-area { min-height: 320px; max-height: 60vh; overflow-y: auto; padding: 14px 16px; outline: none; font-family: Cambria, Georgia, serif; font-size: 11.5pt; line-height: 1.5; color: #111; resize: vertical; }
.re-status { display: flex; justify-content: space-between; gap: 10px; padding: 4px 10px; font-size: 11.5px; color: var(--muted); border-top: 1px solid var(--line); background: #fafbfc; border-radius: 0 0 8px 8px; }
.re-full { position: fixed; inset: 16px; z-index: 50; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.re-full .re-area { max-height: none; flex: 1; padding: 32px max(24px, calc((100% - 760px) / 2)); }
body.re-lock { overflow: hidden; }
body.re-lock::before { content: ''; position: fixed; inset: 0; background: rgba(15,23,42,.45); z-index: 49; }

/* Formatted content — shared by the editor and the letterhead sheet */
.memo-rich p, .memo-body p { margin: 0 0 4mm; }
.memo-rich h1, .memo-body h1 { font-size: 17pt; margin: 5mm 0 2mm; }
.memo-rich h2, .memo-body h2 { font-size: 14.5pt; margin: 4mm 0 2mm; }
.memo-rich h3, .memo-body h3 { font-size: 12.5pt; margin: 3mm 0 1.5mm; }
.memo-rich ul, .memo-rich ol, .memo-body ul, .memo-body ol { margin: 0 0 4mm; padding-left: 7mm; }
.memo-rich ol ol, .memo-body ol ol { list-style-type: lower-alpha; }
.memo-rich ol ol ol, .memo-body ol ol ol { list-style-type: lower-roman; }
.memo-rich li, .memo-body li { margin: 0 0 1mm; }
.memo-rich blockquote, .memo-body blockquote { margin: 0 0 4mm 8mm; padding-left: 4mm; border-left: 3px solid #c9ced8; color: #444; font-style: italic; }
.memo-rich table, .memo-body table { width: 100%; border-collapse: collapse; margin: 0 0 4mm; table-layout: fixed; }
.memo-rich th, .memo-rich td, .memo-body th, .memo-body td { border: 1px solid #9aa3af; padding: 1.5mm 2mm; vertical-align: top; text-align: left; }
.memo-rich th, .memo-body th { background: #eef1f5; font-weight: 700; }
.memo-rich hr, .memo-body hr { border: 0; border-top: 1px solid #999; margin: 3mm 0 4mm; }
.memo-rich a, .memo-body a { color: #1f4e9e; }
.memo-body > :last-child { margin-bottom: 0; }

.check-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 6px 14px; margin-top: 6px; align-items: center; }
.check-grid .link { justify-self: start; font-size: 13px; }
.dir-row { padding: 2px 0; }
.dir-row .link { font-size: 12px; margin-left: 6px; }

.access-table td { vertical-align: middle; }
.access-table select, .access-table input:not([type=checkbox]) { padding: 6px 8px; }
.access-table select:disabled, .access-table input:disabled { opacity: .4; }
.access-table tr.muted-row td:first-child { color: var(--muted); }
.access-table td.center { text-align: center; }
.access-table input[type=checkbox] { width: auto; }
table.dimmed { opacity: .45; }
.titles-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 10px 12px; }
.small-titles { font-size: 12px; margin-top: 2px; }
.bg-thumb { height: 64px; width: 45px; object-fit: fill; border: 1px solid var(--line); border-radius: 3px; box-shadow: 0 1px 4px rgba(0,0,0,.12); }
.logo-thumb { height: 40px; max-width: 120px; object-fit: contain; }
.logo-row { display: flex; align-items: center; gap: 8px; min-height: 40px; }
.dept-add { display: grid; grid-template-columns: 160px 1fr auto; gap: 8px; margin-top: 12px; }

.ai-box { margin-top: 20px; padding: 14px 16px; border: 1px solid #c9d7ee; background: linear-gradient(180deg, #f5f8fe, #eef3fb); border-radius: var(--radius); }
.ai-head { font-weight: 700; display: flex; align-items: center; gap: 8px; }
.ai-badge { background: var(--accent); color: #fff; font-size: 11px; padding: 2px 7px; border-radius: 4px; letter-spacing: .5px; }
.ai-box .actions { margin-top: 12px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.small { font-size: 12px; margin: 10px 0 0; }

.user-menu { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.user-chip { color: #e3eaf4; text-decoration: none; font-size: 13px; padding: 6px 10px; border-radius: 6px; }
.user-chip:hover { background: rgba(255,255,255,.1); }
.btn.ghost { background: transparent; color: #fff; border-color: rgba(255,255,255,.35); padding: 6px 12px; }
.btn.ghost:hover { background: rgba(255,255,255,.1); }
.btn.wide { width: 100%; margin-top: 16px; padding: 10px; }
.auth-wrap { min-height: calc(100vh - 48px); display: grid; place-items: center; }
.auth-card { width: min(400px, 100%); padding: 32px; box-shadow: 0 10px 40px rgba(20,30,50,.08); }
.auth-card h2 { margin: 8px 0 2px; }
.auth-brand { font-weight: 800; color: var(--accent); letter-spacing: .3px; }
.auth-error { color: var(--danger); background: #fdecea; padding: 8px 10px; border-radius: 6px; margin: 12px 0 0; }
.narrow { max-width: 520px; }
label.check { flex-direction: row; align-items: center; gap: 8px; flex: none; }
label.check input { width: auto; }

.status.pending { background: #e5edff; color: #2446a8; }
.badge { display: inline-block; min-width: 18px; padding: 0 6px; margin-left: 4px; border-radius: 9px; background: #f04438; color: #fff; font-size: 11px; font-weight: 700; line-height: 18px; text-align: center; }
.notice { background: #eef4ff; border: 1px solid #c7d7fe; color: #1d3a8a; padding: 10px 12px; border-radius: 8px; margin-bottom: 12px; }
.notice.warn { background: #fff6e5; border-color: #f7d49b; color: #7a4b00; }
.approve-box { border: 2px solid #9fd8b8; background: #f1fbf5; border-radius: var(--radius); padding: 4px 14px 14px; margin-bottom: 14px; }
.approve-box h3 { color: var(--ok); border-color: #c9ecd7; }
.btn.success { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.success:hover { background: #05603a; }
.timeline { list-style: none; padding: 0; margin: 0; }
.timeline li { position: relative; padding: 6px 0 8px 18px; border-left: 2px solid var(--line); margin-left: 5px; font-size: 13px; }
.timeline li::before { content: ''; position: absolute; left: -6px; top: 11px; width: 10px; height: 10px; border-radius: 50%; background: #98a2b3; }
.timeline li.ev-approved::before, .timeline li.ev-finalized::before { background: var(--ok); }
.timeline li.ev-returned::before { background: #f79009; }
.timeline li.ev-submitted::before { background: #2e5bff; }
.ev-comment { background: #f6f7f9; border-radius: 6px; padding: 6px 8px; margin-top: 4px; white-space: pre-wrap; }
.lh-art { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: fill; z-index: 0; pointer-events: none; }
.sheet.has-art > *:not(.lh-art) { position: relative; z-index: 1; }
.sheet.has-art > .watermark { position: absolute; }
.sheet.has-art > .lh-qr { position: absolute; bottom: calc(var(--lh-bottom) + 4mm); }
.sheet.has-art .memo-title { border-top: 0; padding-top: 0; }
.lh-qr { position: absolute; right: 18mm; bottom: 24mm; text-align: center; font: 7pt "Segoe UI", sans-serif; color: #666; }
.lh-qr img { width: 26mm; height: 26mm; display: block; margin: 0 auto 1mm; }
.verify-card { width: min(480px, 100%); }
.verify-badge { font-size: 20px; font-weight: 800; padding: 12px 14px; border-radius: 8px; margin: 14px 0 8px; }
.verify-badge.ok { background: #dcf5e7; color: #067647; }
.verify-badge.bad { background: #fdecea; color: #b42318; }
.verify-table th { width: 34%; text-transform: none; font-size: 13px; letter-spacing: 0; }
.approved-by { margin-top: 6mm; font-size: 9pt; color: #067647; font-style: italic; }

.toast { position: fixed; bottom: 24px; right: 24px; background: #12325a; color: #fff; padding: 12px 18px; border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.2); z-index: 100; }
.toast.error { background: var(--danger); }

/* ---------------- A4 letterhead sheet ---------------- */
.sheet {
  --brand: #1f3a5f;
  position: relative; width: 210mm; min-height: 297mm; margin: 0 auto; background: #fff;
  padding: 16mm 18mm 26mm; box-shadow: 0 2px 14px rgba(20,30,50,.14);
  font-family: Cambria, Georgia, "Times New Roman", serif; font-size: 11.5pt; color: #111; overflow: hidden;
}
.empty-sheet { display: grid; place-items: center; color: var(--muted); font-family: inherit; }
.watermark { position: absolute; top: 45%; left: 50%; transform: translate(-50%, -50%) rotate(-30deg); font-size: 110pt; font-weight: 700; color: rgba(180, 35, 24, .07); pointer-events: none; letter-spacing: 10px; }
.lh-header { display: flex; align-items: center; gap: 16px; }
.lh-logo { height: 22mm; max-width: 45mm; object-fit: contain; }
.lh-monogram { width: 22mm; display: grid; place-items: center; background: var(--brand); color: #fff; font: 700 16pt "Segoe UI", sans-serif; border-radius: 3mm; }
.lh-org { flex: 1; text-align: right; }
.lh-name { font-size: 19pt; font-weight: 700; color: var(--brand); letter-spacing: .3px; }
.lh-addr, .lh-contact { font-size: 9.5pt; color: #444; }
.lh-rule { height: 3px; background: var(--brand); margin: 5mm 0 1.2mm; }
.lh-rule + .memo-title { border-top: 1px solid var(--brand); padding-top: 6mm; }
.memo-title { text-align: center; font-size: 15pt; letter-spacing: 6px; margin: 0 0 6mm; color: #111; }
.memo-meta { width: 100%; border-collapse: collapse; margin-bottom: 6mm; }
.memo-meta th { text-align: left; width: 22mm; font-weight: 700; padding: 1.6mm 0; vertical-align: top; }
.memo-meta td { padding: 1.6mm 3mm 1.6mm 0; vertical-align: top; }
.memo-meta tr.subj td, .memo-meta tr.subj th { border-top: 1px solid #999; border-bottom: 1px solid #999; padding: 2.4mm 0; }
.memo-meta tr.subj td { font-weight: 700; text-decoration: none; }
.pending { color: #999; font-style: italic; }
.memo-body p { margin: 0 0 4mm; text-align: justify; }
.signature { margin-top: 16mm; width: 70mm; }
.sig-line { border-top: 1px solid #333; margin-bottom: 1.5mm; }
.sig-name { font-weight: 700; }
.sig-title { font-size: 10pt; color: #333; }
.lh-footer { position: absolute; left: 18mm; right: 18mm; bottom: 10mm; border-top: 2px solid var(--brand); padding-top: 2mm; text-align: center; font-size: 8.5pt; color: #555; }

@media (max-width: 1100px) {
  .editor { grid-template-columns: 1fr; }
  .form-panel { position: static; max-height: none; }
  .filters { grid-template-columns: 1fr 1fr; }
}

@page { size: A4; margin: 0; }
@media print {
  body { background: #fff; }
  .no-print, .topbar { display: none !important; }
  main { padding: 0; max-width: none; }
  .editor { display: block; }
  .preview-wrap { padding: 0; overflow: visible; }
  .sheet { box-shadow: none; margin: 0; width: 210mm; height: 297mm; min-height: 0; }
  .watermark { color: rgba(180, 35, 24, .10); }
}
