/* ==========================================================================
   Tool In Thẻ Tên Bàn & Thẻ Học Sinh Hàng Loạt - TCT.VN
   CSS Styling: Responsive UI + Chuẩn in ấn khổ A4 (Print Media)
   ========================================================================== */

:root {
  --primary: #1e40af;
  --primary-dark: #1e3a8a;
  --primary-light: #eff6ff;
  --bg-main: #f1f5f9;
  --bg-card: #ffffff;
  --text-main: #0f172a;
  --text-muted: #64748b;
  --border-color: #cbd5e1;
  --radius-lg: 12px;
  --radius-md: 8px;
  --radius-sm: 6px;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.08);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.15);

  /* Theme color accents */
  --theme-primary: #1d4ed8;
  --theme-secondary: #3b82f6;
  --theme-accent: #dbeafe;
  --theme-border: #93c5fd;
  --theme-tag-bg: #eff6ff;
  --theme-tag-text: #1e40af;
}

/* Theme variations */
.theme-navy {
  --theme-primary: #1e3a8a;
  --theme-secondary: #2563eb;
  --theme-accent: #dbeafe;
  --theme-border: #93c5fd;
  --theme-tag-bg: #eff6ff;
  --theme-tag-text: #1d4ed8;
}

.theme-teal {
  --theme-primary: #0f766e;
  --theme-secondary: #0d9488;
  --theme-accent: #ccfbf1;
  --theme-border: #5eead4;
  --theme-tag-bg: #f0fdfa;
  --theme-tag-text: #0f766e;
}

.theme-purple {
  --theme-primary: #6b21a8;
  --theme-secondary: #9333ea;
  --theme-accent: #f3e8ff;
  --theme-border: #d8b4fe;
  --theme-tag-bg: #faf5ff;
  --theme-tag-text: #6b21a8;
}

.theme-coral {
  --theme-primary: #c2410c;
  --theme-secondary: #ea580c;
  --theme-accent: #ffedd5;
  --theme-border: #fdba74;
  --theme-tag-bg: #fff7ed;
  --theme-tag-text: #c2410c;
}

.theme-emerald {
  --theme-primary: #047857;
  --theme-secondary: #059669;
  --theme-accent: #d1fae5;
  --theme-border: #6ee7b7;
  --theme-tag-bg: #ecfdf5;
  --theme-tag-text: #047857;
}

.theme-mono {
  --theme-primary: #18181b;
  --theme-secondary: #3f3f46;
  --theme-accent: #f4f4f5;
  --theme-border: #a1a1aa;
  --theme-tag-bg: #f4f4f5;
  --theme-tag-text: #18181b;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: 'Be Vietnam Pro', system-ui, -apple-system, sans-serif;
  background-color: var(--bg-main);
  color: var(--text-main);
  line-height: 1.5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* App Header */
.app-header {
  background: #ffffff;
  border-bottom: 1px solid #e2e8f0;
  padding: 12px 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.04);
}

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

.btn-back {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: #475569;
  text-decoration: none;
  font-size: 0.875rem;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: var(--radius-md);
  background: #f1f5f9;
  transition: all 0.2s;
  border: 1px solid #cbd5e1;
}

.btn-back:hover {
  background: #e2e8f0;
  color: #0f172a;
}

.header-icon {
  width: 44px;
  height: 44px;
  background: linear-gradient(135deg, #2563eb, #1d4ed8);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-md);
  box-shadow: 0 4px 10px rgba(37, 99, 235, 0.25);
}

.app-header h1 {
  font-size: 1.15rem;
  font-weight: 800;
  color: #0f172a;
  margin: 0;
  letter-spacing: -0.01em;
}

.app-header .subtitle {
  font-size: 0.8rem;
  color: #64748b;
  margin: 0;
}

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

.btn-action {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 700;
  padding: 9px 18px;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all 0.2s;
  border: none;
}

.btn-backup {
  background: #ffffff;
  color: #1e293b;
  border: 1.5px solid #cbd5e1;
}

.btn-backup:hover {
  background: #f8fafc;
  border-color: #94a3b8;
  color: #0f172a;
  transform: translateY(-1px);
}

.btn-restore {
  background: #eff6ff;
  color: #1d4ed8;
  border: 1.5px solid #93c5fd;
}

.btn-restore:hover {
  background: #dbeafe;
  border-color: #60a5fa;
  color: #1e40af;
  transform: translateY(-1px);
}

.btn-export-png {
  background: #ffffff;
  color: #334155;
  border: 1.5px solid #cbd5e1;
}

.btn-export-png:hover {
  background: #f8fafc;
  border-color: #94a3b8;
  color: #0f172a;
}

.btn-print-all {
  background: linear-gradient(135deg, #2563eb, #1d4ed8);
  color: #ffffff;
  box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);
}

.btn-print-all:hover {
  background: linear-gradient(135deg, #1d4ed8, #1e40af);
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(37, 99, 235, 0.4);
}

/* Main Layout */
.main-container {
  display: flex;
  flex: 1;
  height: calc(100vh - 69px);
  overflow: hidden;
}

/* Sidebar Controls */
.sidebar-config {
  width: 440px;
  min-width: 440px;
  background: #ffffff;
  border-right: 1px solid #e2e8f0;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.card-config {
  background: #ffffff;
  border: 1px solid #e2e8f0;
  border-radius: var(--radius-lg);
  padding: 16px;
  box-shadow: var(--shadow-sm);
}

.config-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.925rem;
  font-weight: 700;
  color: #1e293b;
  margin-bottom: 12px;
  border-bottom: 1px solid #f1f5f9;
  padding-bottom: 8px;
}

.badge-count {
  margin-left: auto;
  font-size: 0.75rem;
  font-weight: 700;
  background: #eff6ff;
  color: #1d4ed8;
  padding: 3px 8px;
  border-radius: 999px;
  border: 1px solid #bfdbfe;
}

.segmented-control {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  background: #f1f5f9;
  padding: 4px;
  border-radius: var(--radius-md);
  border: 1px solid #e2e8f0;
}

.segment-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 10px;
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 700;
  border: none;
  background: transparent;
  color: #475569;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.2s;
}

.segment-btn.active {
  background: #ffffff;
  color: var(--primary);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.08);
}

.form-group {
  margin-bottom: 12px;
}

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

.form-group label {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  color: #475569;
  margin-bottom: 6px;
}

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

.form-group.half {
  flex: 1;
}

.form-control {
  width: 100%;
  padding: 8px 12px;
  font-family: inherit;
  font-size: 0.85rem;
  border: 1px solid #cbd5e1;
  border-radius: var(--radius-md);
  color: #0f172a;
  background: #ffffff;
  outline: none;
  transition: all 0.2s;
}

.form-control:focus {
  border-color: #2563eb;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.form-control.code-font {
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  font-size: 0.825rem;
  line-height: 1.5;
  white-space: pre;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.8rem;
  font-weight: 600;
  color: #334155;
  cursor: pointer;
  margin-top: 6px;
}

.checkbox-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: #2563eb;
  cursor: pointer;
}

.config-hint {
  font-size: 0.775rem;
  color: #64748b;
  margin-bottom: 8px;
  line-height: 1.45;
}

.btn-group-row {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.btn-secondary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  flex: 1;
  padding: 8px 12px;
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  background: #eff6ff;
  color: #1e40af;
  border: 1px solid #bfdbfe;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all 0.2s;
}

.btn-secondary:hover {
  background: #dbeafe;
}

.btn-secondary.btn-outline {
  background: #f8fafc;
  color: #64748b;
  border-color: #cbd5e1;
}

.btn-secondary.btn-outline:hover {
  background: #f1f5f9;
  color: #334155;
}

.card-guide {
  background: #f8fafc;
  border-color: #e2e8f0;
}

.guide-list {
  padding-left: 18px;
  font-size: 0.775rem;
  color: #475569;
  display: flex;
  flex-direction: column;
  gap: 6px;
  line-height: 1.45;
}

/* Preview Area */
.preview-area {
  flex: 1;
  background: #e2e8f0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.preview-toolbar {
  background: #ffffff;
  border-bottom: 1px solid #cbd5e1;
  padding: 10px 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.toolbar-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.preview-badge {
  background: #1e293b;
  color: #ffffff;
  font-size: 0.775rem;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 999px;
}

.preview-meta {
  font-size: 0.825rem;
  color: #475569;
  font-weight: 500;
}

.zoom-controls {
  display: flex;
  align-items: center;
  gap: 6px;
}

.btn-icon {
  width: 32px;
  height: 32px;
  background: #f1f5f9;
  border: 1px solid #cbd5e1;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.2s;
  color: #1e293b;
}

.btn-icon:hover {
  background: #e2e8f0;
}

.btn-icon.btn-reset {
  width: auto;
  padding: 0 10px;
  font-size: 0.75rem;
  font-weight: 600;
}

.zoom-level {
  font-size: 0.8rem;
  font-weight: 700;
  color: #334155;
  min-width: 42px;
  text-align: center;
}

.pages-wrapper {
  flex: 1;
  overflow: auto;
  padding: 30px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 30px;
  transform-origin: top center;
}

/* ==========================================================================
   A4 SHEET & CARD STYLING (Screen & Print)
   Standard A4 Portrait: 210mm x 297mm
   ========================================================================== */
.a4-sheet {
  width: 210mm;
  height: 297mm;
  min-height: 297mm;
  max-height: 297mm;
  background: #ffffff;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15), 0 2px 6px rgba(0, 0, 0, 0.08);
  box-sizing: border-box;
  padding: 8mm;
  position: relative;
  page-break-after: always;
  break-after: page;
  display: grid;
  overflow: hidden;
}

/* Grid Layout Variations for A4 */
/* 4 Cards per A4 Sheet: 2 columns x 2 rows */
.a4-sheet.grid-4 {
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 6mm;
}

/* 6 Cards per A4 Sheet: 2 columns x 3 rows */
.a4-sheet.grid-6 {
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 5mm;
}

/* 8 Cards per A4 Sheet: 2 columns x 4 rows */
.a4-sheet.grid-8 {
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 4mm;
}

/* Card Container Base */
.card-item {
  position: relative;
  border-radius: 8px;
  box-sizing: border-box;
  overflow: hidden;
  background: #ffffff;
  display: flex;
  flex-direction: column;
  transition: all 0.2s;
}

/* Cutlines option */
.with-cutlines .card-item {
  border: 1.5px dashed #94a3b8;
}

.without-cutlines .card-item {
  border: 1px solid #e2e8f0;
}

/* ==========================================================================
   TYPE 1: THẺ ĐỂ BÀN GẤP CHỮ V (TENT CARD)
   Gồm 2 mặt: Mặt Sau (Lộn 180° nhìn từ phía bảng/thầy cô) & Mặt Trước (học sinh nhìn)
   Hoặc nếp gập đôi ở giữa.
   ========================================================================== */
.tent-card {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
  background: #ffffff;
}

.tent-half {
  flex: 1;
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  position: relative;
  overflow: hidden;
}

/* Mặt trên: Quay 180 độ đối diện để khi gập chữ V thì thầy cô đứng trước bục nhìn thấy */
.tent-half-top {
  transform: rotate(180deg);
  border-bottom: 1.5px dashed #cbd5e1;
  background: #fafafa;
}

/* Nếp gấp ở giữa */
.tent-fold-line {
  position: relative;
  height: 0;
  width: 100%;
  border-top: 1.5px dashed #64748b;
  z-index: 10;
}

.tent-fold-indicator {
  position: absolute;
  top: -9px;
  left: 50%;
  transform: translateX(-50%);
  background: #f1f5f9;
  border: 1px solid #cbd5e1;
  border-radius: 10px;
  padding: 1px 10px;
  font-size: 9px;
  font-weight: 700;
  color: #64748b;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  pointer-events: none;
}

.tent-half-bottom {
  background: #ffffff;
}

/* Nội dung bên trong Tent Card */
.tent-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 2px solid var(--theme-secondary);
  padding-bottom: 4px;
}

.tent-school {
  font-size: 8.5pt;
  font-weight: 700;
  color: var(--theme-primary);
  text-transform: uppercase;
  letter-spacing: 0.2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 65%;
}

.tent-class {
  font-size: 8.5pt;
  font-weight: 800;
  color: #ffffff;
  background: var(--theme-primary);
  padding: 2px 7px;
  border-radius: 4px;
  letter-spacing: 0.5px;
}

.tent-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  padding: 4px 0;
}

.tent-role {
  display: inline-block;
  font-size: 7.5pt;
  font-weight: 700;
  color: var(--theme-tag-text);
  background: var(--theme-tag-bg);
  border: 1px solid var(--theme-border);
  padding: 2px 8px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 3px;
}

.tent-fullname {
  font-family: 'Montserrat', sans-serif;
  font-weight: 900;
  color: #0f172a;
  line-height: 1.15;
  text-transform: uppercase;
  letter-spacing: -0.3px;
  width: 100%;
  word-break: break-word;
}

/* Dynamic font-size based on grid */
.grid-4 .tent-fullname {
  font-size: 19pt;
}

.grid-6 .tent-fullname {
  font-size: 15pt;
}

.grid-8 .tent-fullname {
  font-size: 12.5pt;
}

.tent-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 7.5pt;
  color: #64748b;
  border-top: 1px solid #f1f5f9;
  padding-top: 3px;
}

.tent-meta-left {
  font-weight: 600;
}

.tent-meta-right {
  font-weight: 700;
  color: var(--theme-primary);
}

/* ==========================================================================
   TYPE 2: THẺ ĐEO NGỰC / CÁN BỘ LỚP (BADGE / ID CARD)
   Có móc bấm lỗ, Header màu thương hiệu, QR code, SBD & Chức vụ nổi bật
   ========================================================================== */
.badge-card {
  height: 100%;
  width: 100%;
  background: #ffffff;
  display: flex;
  flex-direction: column;
  border-radius: 8px;
  overflow: hidden;
  box-sizing: border-box;
}

/* Lỗ đeo dây thẻ (Punch hole indicator) */
.badge-punch-slot {
  position: absolute;
  top: 4px;
  left: 50%;
  transform: translateX(-50%);
  width: 20px;
  height: 4px;
  background: #cbd5e1;
  border-radius: 3px;
  border: 0.8px dashed #64748b;
  z-index: 5;
}

.badge-header {
  background: linear-gradient(135deg, var(--theme-primary), var(--theme-secondary));
  color: #ffffff;
  padding: 10px 12px 8px 12px;
  text-align: center;
  position: relative;
}

.badge-school-title {
  font-size: 7.5pt;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  opacity: 0.95;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.badge-tag-title {
  display: inline-block;
  font-size: 8pt;
  font-weight: 900;
  color: #ffffff;
  background: rgba(255, 255, 255, 0.22);
  border: 1px solid rgba(255, 255, 255, 0.4);
  padding: 1px 8px;
  border-radius: 4px;
  margin-top: 4px;
  letter-spacing: 0.8px;
  text-transform: uppercase;
}

.badge-body {
  flex: 1;
  padding: 8px 12px;
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 10px;
  background: #ffffff;
}

.badge-avatar-box {
  width: 58px;
  height: 72px;
  border: 1.5px dashed var(--theme-border);
  background: #f8fafc;
  border-radius: 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: #94a3b8;
  padding: 4px;
  text-align: center;
}

.badge-avatar-box svg {
  margin-bottom: 2px;
  color: #64748b;
}

.badge-avatar-box span {
  font-size: 6.5pt;
  font-weight: 600;
  line-height: 1.1;
}

.badge-info-col {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.badge-role-pill {
  align-self: flex-start;
  font-size: 7pt;
  font-weight: 800;
  color: var(--theme-tag-text);
  background: var(--theme-tag-bg);
  border: 1px solid var(--theme-border);
  padding: 1.5px 7px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  margin-bottom: 4px;
}

.badge-student-name {
  font-family: 'Montserrat', sans-serif;
  font-weight: 800;
  font-size: 13pt;
  color: #0f172a;
  line-height: 1.2;
  text-transform: uppercase;
  margin-bottom: 4px;
  word-break: break-word;
}

.badge-row-info {
  display: flex;
  gap: 10px;
  font-size: 8pt;
  color: #475569;
  font-weight: 600;
}

.badge-row-info strong {
  color: #0f172a;
}

/* QR Code Section */
.badge-qr-box {
  width: 54px;
  height: 54px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border: 1px solid #e2e8f0;
  border-radius: 4px;
  padding: 2px;
  background: #ffffff;
}

.badge-qr-canvas {
  width: 100%;
  height: 100%;
  display: block;
}

.badge-footer {
  background: #f8fafc;
  border-top: 1px solid #e2e8f0;
  padding: 4px 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 7pt;
  color: #64748b;
}

.badge-footer-left {
  font-weight: 600;
}

.badge-footer-right {
  font-weight: 700;
  color: var(--theme-primary);
}

/* ==========================================================================
   PRINT MEDIA STYLES - CHUẨN IN KHỔ A4 (Print Dialog & PDF)
   ========================================================================== */
@media print {
  @page {
    size: A4 portrait;
    margin: 0;
  }

  html, body {
    width: 210mm !important;
    height: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
    background: #ffffff !important;
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }

  .no-print {
    display: none !important;
  }

  .main-container {
    display: block !important;
    height: auto !important;
    overflow: visible !important;
  }

  .preview-area {
    background: transparent !important;
    overflow: visible !important;
  }

  .pages-wrapper {
    padding: 0 !important;
    gap: 0 !important;
    transform: none !important;
    display: block !important;
  }

  .a4-sheet {
    box-shadow: none !important;
    margin: 0 !important;
    width: 210mm !important;
    height: 297mm !important;
    min-height: 297mm !important;
    max-height: 297mm !important;
    page-break-after: always !important;
    break-after: page !important;
    padding: 8mm !important;
  }

  .with-cutlines .card-item {
    border: 1px dashed #94a3b8 !important;
  }
}

/* Responsive UI for screen */
@media (max-width: 1024px) {
  .main-container {
    flex-direction: column;
    height: auto;
    overflow: visible;
  }

  .sidebar-config {
    width: 100%;
    min-width: 100%;
    border-right: none;
    border-bottom: 1px solid #cbd5e1;
  }

  .preview-area {
    min-height: 600px;
  }
}
