:root {
  --navy: #12365a;
  --navy-2: #0b2947;
  --red: #a51c30;
  --blue-soft: #e8f0f7;
  --paper: #ffffff;
  --canvas: #e9eef3;
  --ink: #182534;
  --muted: #647384;
  --line: #d6e0e9;
  --success: #16734a;
  --warning: #a65d00;
  --danger: #b42318;
  --shadow: 0 14px 35px rgba(19, 44, 69, .13);
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--canvas);
}

* { box-sizing: border-box; }
html { min-width: 320px; background: var(--canvas); }
body { margin: 0; min-height: 100vh; padding-top: env(safe-area-inset-top); }
button, input, textarea { font: inherit; }
button, label.file-button { touch-action: manipulation; }

.app-header {
  min-height: 72px; display: flex; align-items: center; justify-content: space-between; gap: 20px;
  padding: 10px clamp(16px, 3vw, 38px); color: white;
  background: linear-gradient(115deg, var(--navy-2), var(--navy) 72%, #174a77);
  border-bottom: 4px solid var(--red); position: sticky; top: env(safe-area-inset-top); z-index: 20;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand img { flex: 0 0 auto; border: 1px solid rgba(255,255,255,.25); border-radius: 10px; }
.brand h1 { font-family: Georgia, "Times New Roman", serif; font-size: clamp(1.05rem, 2vw, 1.35rem); margin: 0; letter-spacing: .01em; }
.brand p { margin: 3px 0 0; color: #d9e6f1; font-size: .78rem; }
.header-actions, .toolbar-actions { display: flex; align-items: center; gap: 8px; }
.offline-badge { display: inline-flex; align-items: center; gap: 7px; font-size: .78rem; color: #eaf4ff; margin-right: 4px; white-space: nowrap; }
.offline-badge i { width: 8px; height: 8px; border-radius: 99px; background: #86d4ae; box-shadow: 0 0 0 3px rgba(134,212,174,.15); }
.offline-badge.offline i { background: #ffc46b; }

.button {
  min-height: 40px; border: 1px solid transparent; border-radius: 8px; padding: 8px 13px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 7px; font-weight: 700; font-size: .88rem;
  transition: transform .15s ease, box-shadow .15s ease, background .15s ease;
}
.button:hover { transform: translateY(-1px); }
.button:focus-visible, input:focus-visible, textarea:focus-visible, summary:focus-visible { outline: 3px solid #75b8ee; outline-offset: 2px; }
.button.primary { background: var(--red); color: #fff; box-shadow: 0 4px 12px rgba(165,28,48,.22); }
.button.secondary { background: var(--navy); color: #fff; }
.button.ghost { background: #fff; color: var(--navy); border-color: #cdd9e4; }
.app-header .button.ghost { color: #fff; background: rgba(255,255,255,.1); border-color: rgba(255,255,255,.35); }
.button:disabled { opacity: .45; cursor: not-allowed; transform: none; }
.file-button input { display: none; }
.button.full { width: 100%; }

.workspace { display: grid; grid-template-columns: minmax(380px, 42%) minmax(560px, 58%); min-height: calc(100vh - 110px); }
.editor-panel { background: #f8fafc; border-right: 1px solid var(--line); padding: 24px clamp(18px, 2.4vw, 32px) 40px; overflow: auto; }
.preview-panel { min-width: 0; background: var(--canvas); }
.panel-heading, .section-title-row, .dialog-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.panel-heading h2, .section-title-row h3, .dialog-heading h2 { margin: 3px 0 0; font-family: Georgia, "Times New Roman", serif; color: var(--navy-2); }
.panel-heading h2 { font-size: 1.45rem; }
.section-title-row h3 { font-size: 1.22rem; }
.eyebrow { font-weight: 800; font-size: .68rem; letter-spacing: .13em; color: var(--red); }
.save-state, .source-count { font-size: .78rem; color: var(--muted); background: #edf2f6; border-radius: 99px; padding: 6px 9px; white-space: nowrap; }
.mobile-data-actions { display: none; gap: 8px; margin-top: 12px; }

.template-tools { display: flex; align-items: end; justify-content: space-between; gap: 12px; margin-top: 20px; }
.template-tools strong, .template-tools small { display: block; }
.template-tools strong { color: var(--navy-2); font-size: .9rem; }
.template-tools small { color: var(--muted); margin-top: 3px; font-size: .72rem; }
.template-actions { display: flex; gap: 7px; }
.button.compact { min-height: 34px; padding: 6px 9px; font-size: .76rem; }
.template-picker { display: grid; grid-template-columns: repeat(auto-fit, minmax(88px, 1fr)); gap: 7px; margin: 10px 0 16px; }
.template-card { border: 1px solid var(--line); background: #fff; border-radius: 9px; padding: 10px 5px; color: var(--navy); font-weight: 750; font-size: .78rem; cursor: pointer; min-height: 47px; }
.template-card.active { color: #fff; background: var(--navy); border-color: var(--navy); box-shadow: 0 4px 12px rgba(18,54,90,.17); }

.form-section, .legal-section { background: #fff; border: 1px solid var(--line); border-radius: 11px; margin-bottom: 13px; overflow: hidden; }
.form-section summary { list-style: none; cursor: pointer; font-weight: 800; color: var(--navy); padding: 15px 17px; display: flex; align-items: center; justify-content: space-between; }
.form-section summary::-webkit-details-marker { display: none; }
.form-section summary::after { content: "+"; font-size: 1.25rem; font-weight: 400; color: var(--muted); }
.form-section[open] summary::after { content: "−"; }
.form-section[open] summary { border-bottom: 1px solid #e7edf2; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 13px; padding: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; color: #3b4c5e; font-weight: 700; font-size: .82rem; }
.field.span-2 { grid-column: span 2; }
.field input, .field textarea {
  width: 100%; border: 1px solid #becbd7; border-radius: 7px; background: #fff; color: var(--ink);
  padding: 10px 11px; font-size: 1rem; font-weight: 450; line-height: 1.45; resize: vertical;
}
.field input:hover, .field textarea:hover { border-color: #8aa4bc; }
.field input:focus, .field textarea:focus { border-color: #3678ad; }

.legal-section { padding: 17px; }
.legal-notice { margin: 13px 0; padding: 10px 12px; border-left: 3px solid #4f84ad; color: #435567; background: #eef5fa; font-size: .82rem; line-height: 1.45; }
.legal-list { display: grid; gap: 9px; }
.empty-legal { text-align: center; padding: 20px 14px; border: 1px dashed #bdcbd8; border-radius: 8px; color: var(--muted); font-size: .87rem; }
.legal-card { display: grid; grid-template-columns: 1fr auto; gap: 9px 14px; border: 1px solid var(--line); border-radius: 9px; padding: 12px; background: #fff; }
.legal-card.high { border-left: 4px solid var(--success); }
.legal-card.medium { border-left: 4px solid #d88617; }
.legal-card h4 { margin: 0; color: var(--navy-2); font-size: .94rem; }
.legal-card p { grid-column: 1 / -1; margin: 0; color: #506174; font-size: .8rem; line-height: 1.45; }
.legal-meta { display: flex; gap: 7px; flex-wrap: wrap; margin-top: 5px; color: var(--muted); font-size: .72rem; }
.legal-meta a { color: #24699f; font-weight: 700; }
.confidence { display: inline-block; border-radius: 99px; padding: 3px 7px; color: var(--success); background: #e5f6ed; font-weight: 800; }
.confidence.medium { color: #8b4f00; background: #fff2dd; }
.legal-toggle { align-self: start; display: inline-flex; align-items: center; gap: 7px; color: var(--navy); font-size: .76rem; font-weight: 800; cursor: pointer; white-space: nowrap; }
.legal-toggle input { width: 18px; height: 18px; accent-color: var(--red); }
.manual-basis { margin-top: 13px; }

.preview-toolbar { min-height: 76px; display: flex; justify-content: space-between; align-items: center; gap: 15px; padding: 14px 22px; background: #fff; border-bottom: 1px solid var(--line); position: sticky; top: calc(72px + env(safe-area-inset-top)); z-index: 10; }
.score-wrap { display: flex; align-items: center; gap: 10px; }
.score { width: 48px; height: 48px; display: grid; place-items: center; border-radius: 50%; border: 4px solid var(--success); color: var(--navy); font-weight: 900; }
.score-wrap strong, .score-wrap span { display: block; }
.score-wrap span { color: var(--muted); font-size: .76rem; margin-top: 2px; }
.preview-stage { padding: 30px 24px 70px; overflow: auto; }
.paper {
  width: 210mm; min-height: 297mm; margin: 0 auto; padding: 20mm 15mm 20mm 30mm; background: var(--paper); color: #000;
  box-shadow: var(--shadow); font-family: "Times New Roman", Times, serif; font-size: 13pt; line-height: 1.15;
}
.doc-header { display: grid; grid-template-columns: 45% 55%; gap: 0; text-align: center; margin-bottom: 18pt; }
.doc-header p { margin: 0 0 3pt; }
.doc-header .agency, .doc-header .country { font-weight: 700; }
.doc-header .rule { display: block; width: 44%; margin: 5pt auto 7pt; border-top: 1px solid #000; }
.doc-header .right .rule { width: 56%; }
.doc-date { font-style: italic; }
.doc-main-title, .doc-title { text-align: center; font-weight: 700; margin: 0; }
.doc-main-title { font-size: 14pt; text-transform: uppercase; }
.doc-title { font-size: 14pt; margin-bottom: 15pt; }
.doc-recipient { font-weight: 700; margin: 0 0 10pt; }
.doc-basis { font-style: italic; text-align: justify; text-indent: 10mm; margin: 0 0 6pt; }
.doc-content p { text-align: justify; text-indent: 10mm; margin: 0 0 6pt; min-height: 1em; }
.doc-content p.heading { text-indent: 0; font-weight: 700; text-align: left; }
.doc-closing { display: grid; grid-template-columns: 1fr 1fr; gap: 20mm; margin-top: 20pt; }
.doc-recipients { font-size: 11pt; }
.doc-recipients strong { font-style: italic; }
.doc-recipients p { margin: 2pt 0; }
.doc-signature { text-align: center; }
.doc-signature strong { display: block; min-height: 65pt; }

.quality-dialog, .template-dialog { border: 0; border-radius: 13px; padding: 0; box-shadow: 0 24px 70px rgba(13,39,63,.35); }
.quality-dialog { width: min(560px, calc(100% - 28px)); }
.template-dialog { width: min(980px, calc(100% - 28px)); max-height: min(760px, calc(100vh - 32px)); overflow: auto; }
.quality-dialog::backdrop, .template-dialog::backdrop { background: rgba(10,29,47,.6); }
.quality-dialog form { padding: 22px; }
.sticky-dialog-heading { position: sticky; top: 0; z-index: 2; padding: 20px 22px; background: #fff; border-bottom: 1px solid var(--line); }
.template-manager-grid { display: grid; grid-template-columns: minmax(270px, .8fr) minmax(360px, 1.2fr); gap: 20px; padding: 20px 22px 24px; }
.template-list-heading { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; color: var(--navy-2); }
.template-list { display: grid; gap: 7px; max-height: 520px; overflow: auto; padding-right: 3px; }
.template-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px; border: 1px solid var(--line); border-radius: 8px; background: #f9fbfc; }
.template-row-copy strong, .template-row-copy small { display: block; }
.template-row-copy strong { color: var(--navy-2); font-size: .86rem; }
.template-row-copy small { color: var(--muted); margin-top: 3px; font-size: .7rem; }
.template-row-actions { display: flex; gap: 5px; }
.mini-button { border: 1px solid #b9c9d7; border-radius: 6px; padding: 5px 8px; background: #fff; color: var(--navy); font-weight: 750; font-size: .72rem; cursor: pointer; }
.mini-button.danger { color: var(--danger); border-color: #e7bbb7; }
.template-form { border-left: 1px solid var(--line); padding-left: 20px; }
.template-form h3 { margin: 0 0 12px; color: var(--navy-2); font-family: Georgia, "Times New Roman", serif; }
.compact-grid { padding: 0 0 14px; }
.check-field { display: flex; align-items: center; gap: 8px; min-height: 42px; color: #3b4c5e; font-weight: 700; font-size: .82rem; }
.check-field input { width: 18px; height: 18px; accent-color: var(--red); }
.check-field.span-2 { grid-column: span 2; }
.icon-button { width: 38px; height: 38px; border: 1px solid var(--line); border-radius: 8px; background: #fff; font-size: 1.5rem; cursor: pointer; }
.check-list { display: grid; gap: 8px; margin: 20px 0; }
.check-item { display: flex; justify-content: space-between; align-items: center; gap: 14px; border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; font-size: .9rem; }
.check-copy { min-width: 0; }
.check-copy .check-label, .check-copy small { display: block; }
.check-copy .check-group { display: inline-block; margin-bottom: 3px; color: var(--red); font-size: .62rem; font-weight: 850; letter-spacing: .08em; text-transform: uppercase; }
.check-copy small { color: var(--muted); margin-top: 3px; font-size: .72rem; line-height: 1.35; }
.check-status { font-weight: 900; }
.check-status.pass { color: var(--success); }
.check-status.warning { color: var(--warning); }
.check-status.error { color: var(--danger); }

.toast { position: fixed; right: 22px; bottom: 22px; max-width: min(390px, calc(100% - 32px)); padding: 12px 16px; border-radius: 9px; color: #fff; background: var(--navy-2); box-shadow: var(--shadow); opacity: 0; transform: translateY(12px); pointer-events: none; transition: .2s ease; z-index: 50; }
.toast.show { opacity: 1; transform: translateY(0); }
footer { padding: 11px 20px calc(11px + env(safe-area-inset-bottom)); text-align: center; color: #748190; background: #f7f9fb; border-top: 1px solid var(--line); font-size: .75rem; }

@media (max-width: 1120px) {
  .workspace { grid-template-columns: 1fr; }
  .editor-panel { border-right: 0; }
  .preview-toolbar { top: calc(72px + env(safe-area-inset-top)); }
  .preview-stage { padding-inline: 12px; }
  .paper { transform-origin: top center; width: 210mm; }
}
@media (max-width: 720px) {
  .app-header { min-height: 64px; align-items: flex-start; padding: 9px 12px; }
  .brand img { width: 38px; height: 38px; }
  .brand p { display: none; }
  .header-actions .offline-badge, .header-actions .button { display: none; }
  .header-actions .file-button { display: none; }
  .mobile-data-actions { display: grid; grid-template-columns: 1fr 1fr; }
  .mobile-data-actions .file-button { display: inline-flex; }
  .editor-panel { padding: 18px 12px 30px; }
  .template-picker { grid-template-columns: repeat(3, 1fr); }
  .template-tools { align-items: stretch; flex-direction: column; }
  .template-actions { display: grid; grid-template-columns: 1fr 1fr; }
  .form-grid { grid-template-columns: 1fr; padding: 13px; }
  .field.span-2 { grid-column: span 1; }
  .preview-toolbar { top: calc(64px + env(safe-area-inset-top)); padding: 10px 12px; align-items: stretch; flex-direction: column; }
  .toolbar-actions { display: grid; grid-template-columns: repeat(3, 1fr); }
  .toolbar-actions .button { padding-inline: 7px; font-size: .78rem; }
  .score-wrap { justify-content: center; }
  .preview-stage { padding: 16px 8px 35px; }
  .paper { width: 100%; min-height: auto; padding: 10vw 7vw 10vw 12vw; font-size: clamp(11px, 3.2vw, 13pt); box-shadow: 0 5px 20px rgba(19,44,69,.14); }
  .legal-card { grid-template-columns: 1fr; }
  .legal-toggle { grid-row: 3; }
  .doc-closing { gap: 8vw; }
  .template-manager-grid { grid-template-columns: 1fr; padding: 14px; }
  .template-form { border-left: 0; border-top: 1px solid var(--line); padding: 16px 0 0; }
  .template-list { max-height: 280px; }
}

@media print {
  @page { size: A4; margin: 20mm 15mm 20mm 30mm; }
  body { background: #fff; padding: 0; }
  body > :not(main), .editor-panel, .preview-toolbar { display: none !important; }
  .workspace, .preview-panel, .preview-stage { display: block; min-height: 0; padding: 0; margin: 0; background: #fff; overflow: visible; }
  .paper { width: auto; min-height: 0; margin: 0; padding: 0; box-shadow: none; font-size: 13pt; }
}
