/* StarGrade Custom Template Designer Styles - Harmonized with StarGrade Theme */
dialog.template-designer-dialog {
  position: fixed;
  inset: 0;
  margin: 0;
  width: 100vw;
  height: 100dvh;
  max-width: none;
  max-height: none;
  box-sizing: border-box;
  padding: 0;
  border: 0;
  background: #eff5ed;
  color: #203524;
  display: flex;
  flex-direction: column;
  z-index: 1000;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

html:has(dialog.template-designer-dialog[open]) {
  overflow: hidden;
}

/* Header */
.td-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 18px;
  background: var(--grade-green, #129d08);
  border-bottom: 1px solid #0e7d06;
  color: #ffffff;
  flex-shrink: 0;
  gap: 12px;
}

.td-header-left {
  display: flex;
  align-items: center;
  gap: 14px;
}

.td-header-title {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  color: #ffffff;
  display: flex;
  align-items: center;
  gap: 8px;
}

.td-badge-omr {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  background: #ffffff;
  color: #129d08;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 12px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
}

.td-header-tabs {
  display: flex;
  align-items: center;
  background: rgba(0, 0, 0, 0.16);
  padding: 3px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  gap: 4px;
}

.td-main-tab-btn {
  padding: 6px 14px;
  border-radius: 7px;
  font-size: 13px;
  font-weight: 600;
  color: #e2efdf;
  background: transparent;
  border: 0;
  cursor: pointer;
  transition: all 0.15s ease;
}

.td-main-tab-btn:hover {
  color: #ffffff;
  background: rgba(255, 255, 255, 0.12);
}

.td-main-tab-btn.active {
  background: #ffffff;
  color: #127920;
  font-weight: 700;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
}

.td-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Tab Panels */
.td-tab-panel {
  display: flex;
  flex: 1;
  min-height: 0;
  position: relative;
  overflow: hidden;
  background: #eff5ed;
}

.td-tab-panel.hidden {
  display: none !important;
}

/* Sidebar */
.td-sidebar {
  width: 420px;
  max-width: 46vw;
  background: #ffffff;
  border-right: 1px solid #dbe7d7;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  box-sizing: border-box;
  flex-shrink: 0;
  color: #203524;
}

.td-sidebar-header {
  padding: 12px 16px;
  background: #f5faf2;
  border-bottom: 1px solid #dbe7d7;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.td-sidebar-header h3 {
  margin: 0;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #1d5327;
}

.td-sidebar-content {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.td-sidebar-footer {
  padding: 12px 16px;
  background: #f5faf2;
  border-top: 1px solid #dbe7d7;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
}

.td-section {
  background: #ffffff;
  border: 1px solid #dce8d7;
  border-radius: 10px;
  padding: 14px;
  box-shadow: 0 1px 4px rgba(32, 76, 36, 0.04);
}

.td-section-title {
  margin: 0 0 10px 0;
  font-size: 13px;
  font-weight: 700;
  color: #1d5327;
  display: flex;
  align-items: center;
  gap: 6px;
  border-bottom: 1px solid #edf4eb;
  padding-bottom: 6px;
}

.td-section-num {
  width: 18px;
  height: 18px;
  border-radius: 5px;
  background: #e2efdf;
  color: #127920;
  font-size: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
}

/* Sidebar Form Controls & Reset */
.td-sidebar input[type="checkbox"],
#tdDesignSidebarForm input[type="checkbox"] {
  display: inline-block !important;
  width: 16px !important;
  height: 16px !important;
  min-width: 16px !important;
  max-width: 16px !important;
  margin: 0 !important;
  padding: 0 !important;
  flex: 0 0 16px !important;
  flex-shrink: 0 !important;
  cursor: pointer !important;
  accent-color: var(--grade-green, #129d08) !important;
  vertical-align: middle !important;
  box-sizing: border-box !important;
}

.td-check-label {
  display: inline-flex !important;
  align-items: center !important;
  gap: 7px !important;
  font-size: 12px !important;
  font-weight: 600 !important;
  color: #1e293b !important;
  cursor: pointer !important;
  margin: 0 !important;
  line-height: 1.3 !important;
  user-select: none !important;
  white-space: nowrap !important;
}

.td-check-grid-2 {
  display: flex !important;
  flex-wrap: wrap !important;
  align-items: center !important;
  justify-content: space-between !important;
  gap: 8px 12px !important;
  margin-top: 8px !important;
}

.td-check-row {
  display: flex !important;
  align-items: center !important;
  gap: 8px !important;
  margin-top: 8px !important;
}

.td-card-header {
  display: flex !important;
  justify-content: space-between !important;
  align-items: center !important;
  margin-bottom: 6px !important;
  font-size: 12px !important;
  gap: 8px !important;
}

.td-card-header .td-check-label {
  font-weight: 700 !important;
  flex: 1 1 auto !important;
  white-space: nowrap !important;
}

.td-card-header .td-card-badge {
  font-weight: 700 !important;
  white-space: nowrap !important;
  flex-shrink: 0 !important;
  font-size: 12px !important;
  margin-left: 8px !important;
}

.td-form-group {
  margin-bottom: 10px;
}

.td-form-group:last-child {
  margin-bottom: 0;
}

.td-form-group label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  margin-bottom: 4px;
  color: #204524;
}

.td-form-group input[type="text"],
.td-form-group input[type="number"],
.td-form-group select {
  width: 100%;
  box-sizing: border-box;
  padding: 7px 10px;
  border: 1px solid #cfdbcc;
  border-radius: 6px;
  font-size: 13px;
  background: #fff;
  color: #203524;
  line-height: 1.45;
}

.td-form-group input:focus,
.td-form-group select:focus {
  border-color: #129d08;
  outline: 2px solid rgba(18, 157, 8, 0.25);
  outline-offset: 1px;
}

.td-form-row {
  display: flex;
  gap: 8px;
}

.td-form-row .td-form-group {
  flex: 1;
}

/* Radio Option Cards (Khổ giấy, Loại mẫu) */
.td-radio-cards-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.td-radio-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  border: 1.5px solid #cfdbcc;
  border-radius: 10px;
  background: #ffffff;
  color: #203524;
  cursor: pointer;
  transition: all 0.15s ease;
  user-select: none;
}

.td-radio-card:hover {
  background: #f7faf5;
  border-color: #129d08;
}

.td-radio-card.active {
  border-color: #129d08;
  background: #f0fdf4;
  color: #127920;
  box-shadow: 0 0 0 2px rgba(18, 157, 8, 0.15);
}

.td-radio-card-icon {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 11px;
  background: #e2efdf;
  color: #127920;
  flex-shrink: 0;
}

.td-radio-card.active .td-radio-card-icon {
  background: #129d08;
  color: #ffffff;
}

.td-radio-card-title {
  font-size: 12px;
  font-weight: 700;
  line-height: 1.25;
}

.td-radio-card-desc {
  font-size: 10.5px;
  color: #557059;
  margin-top: 2px;
}

/* Cắt phách options box */
.td-cut-stub-box {
  padding: 10px 12px;
  background: #f5faf2;
  border: 1px solid #cbe3c6;
  border-radius: 10px;
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.td-cut-stub-box.hidden {
  display: none !important;
}

/* Color radio selector */
.td-color-options {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.td-color-card {
  border: 1px solid #cfdbcc;
  border-radius: 8px;
  padding: 8px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  font-size: 11px;
  font-weight: 500;
  transition: all 0.15s ease;
  background: #fff;
  color: #204524;
}

.td-color-card.active {
  border-color: #129d08;
  background: #f0fdf4;
  box-shadow: 0 0 0 2px rgba(18, 157, 8, 0.2);
  font-weight: 700;
}

.td-color-dot {
  width: 18px;
  height: 18px;
  border-radius: 50%;
}

/* A4 & A5 Sheet Preview Area */
.td-preview-area {
  flex: 1;
  background: #dbe7d9;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

.td-preview-toolbar {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid #dbe7d7;
  padding: 7px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  z-index: 10;
  flex-shrink: 0;
  color: #203524;
}

.td-preview-viewport {
  flex: 1;
  overflow: auto;
  padding: 24px;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

/* Paper Document A4 & A5 CSS */
.paper-sheet,
.a4-sheet {
  background: #ffffff;
  box-shadow: 0 10px 30px rgba(32, 76, 36, 0.18);
  position: relative;
  box-sizing: border-box;
  overflow: hidden;
  transform-origin: top center;
  transition: width 0.2s ease, height 0.2s ease;
  color: #111827;
}

/* Kích thước chuẩn trang A4 - Dọc (210mm x 297mm) */
.paper-sheet.size-a4-portrait,
.a4-sheet.size-a4-portrait,
.a4-sheet:not(.size-a5-landscape) {
  width: 210mm;
  height: 297mm;
  padding: 12mm;
}

/* Kích thước chuẩn trang A5 - Ngang (210mm x 148mm) */
.paper-sheet.size-a5-landscape,
.a4-sheet.size-a5-landscape {
  width: 210mm;
  height: 148mm;
  padding: 8mm 10mm;
}

.sheet-font {
  font-family: 'Times New Roman', 'Noto Serif', serif;
}

.bubble-font {
  font-family: 'Inter', Arial, sans-serif;
}

/* 4 Điểm neo định vị góc 5mm x 5mm đen tuyền */
.anchor-marker {
  position: absolute;
  width: 5mm;
  height: 5mm;
  background-color: #000000 !important;
  z-index: 20;
}

/* Vị trí điểm neo cho A4 - Dọc */
.size-a4-portrait .anchor-tl,
.a4-sheet:not(.size-a5-landscape) .anchor-tl {
  top: 8mm;
  left: 8mm;
}

.size-a4-portrait .anchor-tr,
.a4-sheet:not(.size-a5-landscape) .anchor-tr {
  top: 8mm;
  right: 8mm;
}

.size-a4-portrait .anchor-bl,
.a4-sheet:not(.size-a5-landscape) .anchor-bl {
  bottom: 8mm;
  left: 8mm;
}

.size-a4-portrait .anchor-br,
.a4-sheet:not(.size-a5-landscape) .anchor-br {
  bottom: 8mm;
  right: 8mm;
}

/* Vị trí điểm neo cho A5 - Ngang */
.size-a5-landscape .anchor-tl {
  top: 4.5mm;
  left: 6mm;
}

.size-a5-landscape .anchor-tr {
  top: 4.5mm;
  right: 6mm;
}

.size-a5-landscape .anchor-bl {
  bottom: 4.5mm;
  left: 6mm;
}

.size-a5-landscape .anchor-br {
  bottom: 4.5mm;
  right: 6mm;
}

/* Đường cắt phách (Tear-off line) */
.cut-phach-divider {
  position: relative;
  border-top: 1.5px dashed #1e3a1f;
  margin: 2.2mm 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.cut-phach-badge {
  background: #ffffff;
  padding: 0 6px;
  font-size: 7pt;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: #1e3a1f;
  margin-top: -7px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

/* OMR Bubbles */
.omr-bubble {
  width: 4.1mm;
  height: 4.1mm;
  border-radius: 9999px;
  border-width: 1px;
  border-style: solid;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 7.5pt;
  font-weight: 600;
  line-height: 1;
  user-select: none;
}

.omr-bubble-sm {
  width: 3.6mm;
  height: 3.6mm;
  border-radius: 9999px;
  border-width: 1px;
  border-style: solid;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 6.8pt;
  font-weight: 600;
  line-height: 1;
  user-select: none;
}

/* Canvas Editor for Tab 2 */
.td-canvas-area {
  flex: 1;
  background: #1e2c22;
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.td-canvas-toolbar-calib {
  background: #141f16;
  border-bottom: 1px solid #283d2d;
  color: #b7d6bb;
  padding: 8px 16px;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  z-index: 10;
}

.td-canvas-stage-wrapper {
  flex: 1;
  overflow: auto;
  padding: 24px;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.td-canvas-stage {
  position: relative;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
  background: #ffffff;
}

#sheet-base-canvas {
  display: block;
}

#sheet-overlay-canvas {
  position: absolute;
  top: 0;
  left: 0;
}

/* Calibrator Mode Buttons */
.td-mode-btn-group {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.td-mode-btn {
  padding: 8px 4px;
  border-radius: 8px;
  border: 1px solid #cfdbcc;
  background: #ffffff;
  color: #204524;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  transition: all 0.15s ease;
}

.td-mode-btn.active {
  border-width: 2px;
  border-color: #129d08;
  background: #f0fdf4;
  color: #166534;
  font-weight: 700;
}

/* Zone Buttons in Sidebar */
.td-zone-type-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
}

.td-zone-btn {
  text-align: left;
  padding: 7px 10px;
  border-radius: 6px;
  border: 1px solid #dce8d7;
  background: #ffffff;
  color: #204524;
  font-size: 12px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.td-zone-btn.active {
  border-color: #129d08;
  background: #f0fdf4;
  color: #166534;
  font-weight: 700;
}

.td-zone-item-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  border-radius: 6px;
  border: 1px solid #dce8d7;
  background: #f7faf5;
  color: #204524;
  font-size: 11px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.td-zone-item-row:hover {
  background: #edf5eb;
}

.td-zone-item-row.selected {
  border-color: #129d08;
  background: #f0fdf4;
  box-shadow: 0 0 0 1px #129d08;
}

/* Results Panel on Right */
.td-results-aside {
  width: 330px;
  background: #ffffff;
  border-left: 1px solid #dbe7d7;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
  color: #203524;
}

.td-results-header {
  padding: 10px 14px;
  background: #144f1c;
  color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.td-results-body {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  font-size: 12px;
  color: #203524;
}

/* Toast container inside designer */
#td-toast-container {
  position: fixed;
  bottom: 16px;
  right: 16px;
  z-index: 1100;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

/* Custom Template Badge & Warning Badge */
.custom-template-badge {
  display: inline-block;
  background: #e1f4de;
  color: #0c7505;
  border: 1px solid #b7e3b1;
  padding: 2px 7px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 700;
  margin-left: 6px;
}

.td-warn-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 9999px;
  background: #fef3c7;
  color: #92400e;
  border: 1px solid #fde68a;
  font-size: 11px;
  font-weight: 600;
}

.td-warn-badge.hidden {
  display: none !important;
}

/* Print Rules */
@media print {
  @page {
    size: A4 portrait;
    margin: 0;
  }

  body {
    background: #ffffff !important;
    margin: 0 !important;
    padding: 0 !important;
  }

  .no-print,
  dialog.template-designer-dialog .td-header,
  dialog.template-designer-dialog .td-sidebar,
  dialog.template-designer-dialog .td-preview-toolbar,
  dialog.template-designer-dialog .td-results-aside,
  dialog.template-designer-dialog #td-tab-calibrate {
    display: none !important;
  }

  dialog.template-designer-dialog {
    position: static !important;
    width: 210mm !important;
    height: 297mm !important;
    background: transparent !important;
    overflow: visible !important;
  }

  .td-tab-panel,
  .td-preview-area,
  .td-preview-viewport {
    display: block !important;
    padding: 0 !important;
    margin: 0 !important;
    background: none !important;
    overflow: visible !important;
    width: 210mm !important;
    height: 297mm !important;
  }

  .paper-sheet,
  .a4-sheet {
    box-shadow: none !important;
    transform: none !important;
    margin: 0 !important;
    page-break-after: avoid;
  }
}

/* Night theme compatibility */
body.night dialog.template-designer-dialog {
  background: #17231b;
  color: #ecf4e9;
}

body.night .td-header {
  background: #0e6808;
  border-bottom: 1px solid #084d04;
}

body.night .td-tab-panel {
  background: #17231b;
}

body.night .td-sidebar {
  background: #223028;
  border-color: #415345;
  color: #ecf4e9;
}

body.night .td-sidebar-header {
  background: #1a281e;
  border-color: #415345;
}

body.night .td-sidebar-header h3 {
  color: #c9ecc2;
}

body.night .td-sidebar-footer {
  background: #1a281e;
  border-color: #415345;
}

body.night .td-section {
  background: #1f2c24;
  border-color: #415345;
  box-shadow: none;
}

body.night .td-section-title {
  color: #c9ecc2;
  border-color: #2b3d32;
}

body.night .td-section-num {
  background: #153c1b;
  color: #6ee787;
}

body.night .td-form-group label {
  color: #c9ecc2;
}

body.night .td-form-group input,
body.night .td-form-group select {
  background: #1a281e;
  border-color: #415345;
  color: #ecf4e9;
}

body.night .td-radio-card {
  background: #1a281e;
  border-color: #415345;
  color: #ecf4e9;
}

body.night .td-radio-card:hover {
  background: #233829;
}

body.night .td-radio-card.active {
  background: #1f3d25;
  border-color: #4ade80;
  color: #ecf4e9;
}

body.night .td-radio-card-icon {
  background: #223e27;
  color: #a7f3d0;
}

body.night .td-radio-card.active .td-radio-card-icon {
  background: #117b0a;
  color: #fff;
}

body.night .td-radio-card-desc {
  color: #a4cca8;
}

body.night .td-cut-stub-box {
  background: #1f2d22;
  border-color: #3b5c40;
  color: #c9ecc2;
}

body.night .td-color-card {
  background: #1a281e;
  border-color: #415345;
  color: #c9ecc2;
}

body.night .td-color-card.active {
  background: #1f3d25;
  border-color: #4ade80;
}

body.night .td-preview-area {
  background: #121c15;
}

body.night .td-preview-toolbar {
  background: rgba(26, 40, 30, 0.95);
  border-color: #415345;
  color: #ecf4e9;
}

body.night .td-canvas-area {
  background: #0e1610;
}

body.night .td-canvas-toolbar-calib {
  background: #121b14;
  border-color: #273b2c;
  color: #a4cca8;
}

body.night .td-results-aside {
  background: #223028;
  border-color: #415345;
  color: #ecf4e9;
}

body.night .td-results-header {
  background: #0e6808;
}

body.night .td-results-body {
  color: #ecf4e9;
}

body.night .td-mode-btn {
  background: #1a281e;
  border-color: #415345;
  color: #c9ecc2;
}

body.night .td-mode-btn.active {
  background: #184c20;
  border-color: #4ade80;
  color: #ecf4e9;
}

body.night .td-zone-btn {
  background: #1a281e;
  border-color: #415345;
  color: #c9ecc2;
}

body.night .td-zone-btn.active {
  background: #184c20;
  border-color: #4ade80;
  color: #ecf4e9;
}

body.night .td-zone-item-row {
  background: #1a281e;
  border-color: #415345;
  color: #c9ecc2;
}

body.night .td-zone-item-row:hover {
  background: #233829;
}

body.night .td-zone-item-row.selected {
  border-color: #4ade80;
  background: #1f3d25;
  box-shadow: 0 0 0 1px #4ade80;
}

@media (max-width: 1024px) {
  .td-tab-panel {
    flex-direction: column;
  }

  .td-sidebar {
    width: 100%;
    max-width: 100%;
    height: 45vh;
    border-right: none;
    border-bottom: 1px solid #dbe7d7;
  }

  .td-results-aside {
    width: 100%;
    height: 35vh;
    border-left: none;
    border-top: 1px solid #dbe7d7;
  }
}