:root {
  --bg: #d7c39d;
  --bg-deep: #92794c;
  --panel: #fff8e8;
  --panel-strong: #fffdf7;
  --ink: #21170f;
  --muted: #745f44;
  --line: #d0ad72;
  --accent: #7c2418;
  --accent-strong: #54150e;
  --field-red: #b83025;
  --paper: #fff;
  --guide: rgb(124 36 24 / 42%);
  --shadow: 0 26px 70px rgb(41 26 12 / 22%);
  --ui-font: "SF Pro Text", "SF Pro Display", -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Arial, sans-serif;
  --print-font: "Times New Roman", "Nimbus Roman", Georgia, serif;
}

* {
  box-sizing: border-box;
}

html {
  min-width: 360px;
}

body {
  background:
    radial-gradient(circle at 8% -10%, rgb(255 239 196 / 70%), transparent 28rem),
    radial-gradient(circle at 100% 0%, rgb(124 36 24 / 14%), transparent 24rem),
    linear-gradient(135deg, #ead9b8 0%, var(--bg) 45%, #b99a63 100%);
  color: var(--ink);
  font-family: var(--ui-font);
  margin: 0;
}

button,
input,
select {
  font: inherit;
}

button {
  border: 0;
  border-radius: 999px;
  cursor: pointer;
  font-weight: 900;
  min-height: 2.75rem;
  padding: 0.7rem 1.05rem;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.48;
}

.primary {
  background: var(--accent);
  color: #fff8ed;
  box-shadow: 0 10px 24px rgb(84 21 14 / 18%);
}

.secondary {
  background: rgb(255 248 232 / 86%);
  border: 1px solid var(--line);
  color: var(--accent-strong);
}

.secondary.is-active {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
  color: #fff8ed;
}

.app-header {
  align-items: center;
  display: flex;
  gap: 1.25rem;
  justify-content: space-between;
  padding: 1.35rem clamp(1rem, 3vw, 2.25rem) 0.85rem;
}

.brand-block {
  min-width: 0;
}

.eyebrow {
  color: var(--accent);
  font-size: 0.72rem;
  font-weight: 900;
  letter-spacing: 0.18em;
  margin: 0 0 0.25rem;
  text-transform: uppercase;
}

h1 {
  font-size: clamp(2rem, 4vw, 3.6rem);
  line-height: 0.92;
  margin: 0;
}

.header-actions {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem;
  justify-content: flex-end;
}

.app-shell {
  display: grid;
  gap: 1rem;
  padding: 0 clamp(1rem, 3vw, 2.25rem) 2rem;
}

.control-panel {
  backdrop-filter: blur(18px);
  background: rgb(255 248 232 / 72%);
  border: 1px solid rgb(124 36 24 / 12%);
  border-radius: 1.35rem;
  box-shadow: 0 18px 48px rgb(59 36 12 / 12%);
  display: grid;
  gap: 0.8rem;
  padding: 0.85rem;
}

.panel-section {
  display: grid;
  gap: 0.75rem;
}

.template-section {
  align-items: center;
  grid-template-columns: minmax(16rem, 24rem) minmax(0, 1fr);
}

.layout-section {
  grid-template-columns: repeat(4, minmax(10rem, 1fr));
}

.content-section {
  grid-template-columns: repeat(6, minmax(10rem, 1fr));
}

.field {
  align-items: center;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 1rem;
  color: var(--accent-strong);
  display: grid;
  gap: 0.35rem;
  min-width: 0;
  padding: 0.62rem 0.72rem;
}

.field[hidden] {
  display: none !important;
}

.field span {
  font-size: 0.82rem;
  font-weight: 900;
  letter-spacing: 0.01em;
}

.field small {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
}

.field input,
.field select,
.field textarea {
  background: var(--panel-strong);
  border: 1px solid rgb(116 95 68 / 28%);
  border-radius: 0.72rem;
  color: var(--ink);
  font-weight: 800;
  min-height: 2.25rem;
  min-width: 0;
  outline: 0;
  padding: 0.36rem 0.55rem;
  width: 100%;
}

.field input:focus,
.field select:focus,
.field textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgb(124 36 24 / 12%);
}

.field textarea {
  line-height: 1.35;
  resize: vertical;
}

.field-number {
  grid-template-columns: 1fr 5.2rem auto;
}

.field-number span {
  align-self: center;
}

.field-number input {
  text-align: center;
}

.field-template select,
.field-select select {
  cursor: pointer;
}

.field-wide {
  grid-column: 1 / -1;
}

.field-multiline {
  grid-row: span 2;
}

.status-pill {
  align-items: center;
  align-self: stretch;
  background: rgb(84 21 14 / 8%);
  border: 1px solid rgb(124 36 24 / 14%);
  border-radius: 1rem;
  color: var(--accent-strong);
  display: flex;
  font-size: 0.9rem;
  font-weight: 900;
  justify-content: flex-end;
  padding: 0.65rem 0.8rem;
  text-align: right;
}

.preview-panel {
  min-width: 0;
}

.sheet-scroll {
  background:
    linear-gradient(90deg, rgb(84 21 14 / 7%) 1px, transparent 1px),
    linear-gradient(rgb(84 21 14 / 7%) 1px, transparent 1px),
    rgb(255 250 239 / 28%);
  background-size: 18px 18px;
  border: 1px solid rgb(41 26 12 / 14%);
  border-radius: 1.35rem;
  max-height: calc(100vh - 18rem);
  overflow: auto;
  padding: 1rem;
}

.sheet {
  background: var(--paper);
  border: 1px solid #ddd;
  box-shadow: var(--shadow);
  height: 297mm;
  margin: 0 auto;
  overflow: hidden;
  position: relative;
  width: 420mm;
}

.sheet + .sheet {
  margin-top: 1rem;
}

.paper-tint {
  background: #fff;
  inset: 0;
  position: absolute;
}

.ocr-layer {
  height: 297mm;
  inset: 0;
  position: absolute;
  width: 420mm;
}

.margin-guides {
  display: none;
  inset: 0;
  pointer-events: none;
  position: absolute;
  z-index: 2;
}

.show-guides .margin-guides {
  display: block;
}

.guide-line {
  color: var(--accent-strong);
  font-size: 3mm;
  font-weight: 900;
  position: absolute;
}

.guide-top {
  border-top: 0.45mm solid var(--guide);
  left: 0;
  padding: 1mm 0 0 2mm;
  right: 0;
  top: 15mm;
}

.guide-bottom {
  border-bottom: 0.45mm solid var(--guide);
  bottom: 10mm;
  left: 0;
  padding: 0 0 1mm 2mm;
  right: 0;
}

.ocr-column,
.date-fixed-word {
  color: #17110c;
  font-family: var(--print-font);
  font-style: normal;
  font-weight: 850;
  line-height: 1;
  position: absolute;
  text-align: center;
  z-index: 3;
}

.ocr-column {
  align-items: center;
  display: flex;
  flex-direction: column;
  gap: var(--gap, 1mm);
  min-width: 9mm;
  padding: 0.8mm;
}

.field-column,
.field-inline-word {
  color: var(--field-red);
}

.date-fixed-word {
  min-width: 13mm;
  padding: 0.8mm;
}

.ocr-word {
  white-space: nowrap;
}

.ocr-line-space {
  display: block;
  height: 1em;
  min-height: 1em;
  width: 1px;
}

.show-guides .ocr-column,
.show-guides .date-fixed-word {
  outline: 0.35mm dashed var(--guide);
  outline-offset: 0.45mm;
}

.column-tag {
  background: rgb(255 249 237 / 90%);
  border: 1px solid rgb(124 36 24 / 24%);
  border-radius: 999px;
  color: var(--accent-strong);
  display: none;
  font-family: var(--ui-font);
  font-size: 2.65mm;
  font-style: normal;
  font-weight: 900;
  left: 50%;
  padding: 0.3mm 1mm;
  position: absolute;
  top: -5.5mm;
  transform: translateX(-50%);
  white-space: nowrap;
}

.show-guides .column-tag {
  display: block;
}

.structure-layer {
  background:
    linear-gradient(90deg, rgb(20 80 74 / 14%) 0.22mm, transparent 0.22mm),
    linear-gradient(rgb(20 80 74 / 14%) 0.22mm, transparent 0.22mm);
  background-size: 10mm 10mm;
  display: none;
  inset: 0;
  pointer-events: none;
  position: absolute;
  z-index: 6;
}

.show-structure .structure-layer {
  display: block;
}

.structure-region {
  background: rgb(255 255 255 / 13%);
  border: 0.42mm solid rgb(20 80 74 / 72%);
  border-radius: 1.4mm;
  box-shadow: inset 0 0 0 0.22mm rgb(255 255 255 / 45%);
  position: absolute;
}

.structure-label {
  background: rgb(242 251 245 / 94%);
  border: 0.22mm solid rgb(20 80 74 / 32%);
  border-radius: 1.2mm;
  color: #16473f;
  font-family: var(--ui-font);
  left: 1.2mm;
  line-height: 1.15;
  max-width: calc(100% - 2.4mm);
  padding: 0.75mm 1mm;
  position: absolute;
  top: 1.2mm;
}

.structure-han {
  display: block;
  font-size: 2.35mm;
  font-weight: 950;
  white-space: nowrap;
}

.structure-meaning {
  display: block;
  font-size: 2.05mm;
  font-weight: 850;
  margin-top: 0.35mm;
}

.show-quoc-ngu .ocr-column,
.show-quoc-ngu .date-fixed-word {
  color: #21170f;
}

.show-quoc-ngu .field-column,
.show-quoc-ngu .field-inline-word {
  color: var(--field-red);
}

.show-quoc-ngu [data-column="F_HONG_LIEN_TOA_HA"] {
  color: #21170f;
}

.seal {
  height: 60mm;
  left: 21mm;
  position: absolute;
  top: 78mm;
  width: 60mm;
  z-index: 2;
}

.seal-image {
  display: block;
  height: 100%;
  object-fit: contain;
  width: 100%;
}

@media (max-width: 1180px) {
  .template-section,
  .layout-section,
  .content-section {
    grid-template-columns: repeat(2, minmax(12rem, 1fr));
  }

  .status-pill,
  .field-wide {
    grid-column: 1 / -1;
  }

  .sheet-scroll {
    max-height: 64vh;
  }
}

@media (max-width: 720px) {
  .app-header {
    align-items: stretch;
    flex-direction: column;
  }

  .header-actions {
    justify-content: flex-start;
  }

  .template-section,
  .layout-section,
  .content-section {
    grid-template-columns: 1fr;
  }
}

@page {
  size: A3 landscape;
  margin: 0;
}

@media print {
  :root {
    --shadow: none;
  }

  body {
    background: #fff;
  }

  .app-header,
  .control-panel {
    display: none !important;
  }

  .app-shell,
  .preview-panel,
  .sheet-scroll {
    display: block;
    margin: 0;
    padding: 0;
    width: 420mm;
  }

  .sheet-scroll {
    background: transparent;
    border: 0;
    border-radius: 0;
    max-height: none;
    overflow: visible;
  }

  .sheet {
    border: 0;
    box-shadow: none;
    break-after: auto;
    break-inside: avoid;
    display: block;
    height: 297mm;
    margin: 0 !important;
    page-break-after: auto;
    page-break-inside: avoid;
    width: 420mm;
  }

  .sheet + .sheet {
    break-before: page;
    margin-top: 0 !important;
    page-break-before: always;
  }

  .show-guides .ocr-column,
  .show-guides .date-fixed-word {
    outline: 0;
  }

  .margin-guides,
  .column-tag,
  .structure-layer {
    display: none !important;
  }
}
