/* --- FONT FACE DEFINITION --- */
@font-face {
  font-family: 'Dot3';
  src: url('assets/Dot3.ttf') format('truetype');
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}

/* --- CSS VARIABLES & THEMING --- */
:root {
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-light: #e0e7ff;
  --bg-dashboard: #0f172a;
  --bg-panel: #1e293b;
  --bg-card: #334155;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --border: #334155;
  --border-focus: #6366f1;
  --white: #ffffff;
  
  /* User-customizable values (controlled via JS/CSS variables) */
  --dot-color: #000000; /* Default made darker as requested */
  --dot-weight: normal;
  --dot-stroke: 0.4px;
  --guide-color: #000000;
  --lead-char-color: #333333;
  --font-size-val: 56px;
}

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

body {
  font-family: 'Sarabun', 'Mitr', sans-serif;
  background-color: #f1f5f9;
  color: #334155;
  overflow: hidden;
  height: 100vh;
}

/* App layout */
.app-container {
  display: flex;
  height: 100vh;
  width: 100vw;
}

/* --- SIDEBAR STYLE --- */
.sidebar {
  width: 420px;
  min-width: 420px;
  background-color: var(--bg-dashboard);
  color: var(--text-main);
  display: flex;
  flex-direction: column;
  border-right: 1px solid #1e293b;
  z-index: 10;
  box-shadow: 4px 0 15px rgba(0, 0, 0, 0.2);
}

.sidebar-header {
  padding: 20px;
  border-bottom: 1px solid var(--border);
}

.logo-area {
  display: flex;
  align-items: center;
  gap: 15px;
}

.logo-icon {
  font-size: 2.2rem;
  background: rgba(255, 255, 255, 0.07);
  padding: 8px;
  border-radius: 12px;
}

.logo-area h1 {
  font-family: 'Mitr', sans-serif;
  font-size: 1.4rem;
  font-weight: 500;
  color: #fff;
  line-height: 1.2;
}

.logo-area p {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.sidebar-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
}

/* Scrollbar styling for sidebar */
.sidebar-scroll::-webkit-scrollbar {
  width: 6px;
}
.sidebar-scroll::-webkit-scrollbar-track {
  background: var(--bg-dashboard);
}
.sidebar-scroll::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}
.sidebar-scroll::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* Control sections */
.control-section {
  background: var(--bg-panel);
  border-radius: 14px;
  padding: 18px;
  margin-bottom: 20px;
  border: 1px solid rgba(255, 255, 255, 0.03);
}

.section-title {
  font-size: 0.95rem;
  font-weight: 600;
  color: #fff;
  margin-bottom: 15px;
  font-family: 'Outfit', 'Mitr', sans-serif;
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Presets */
.preset-buttons {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

/* Buttons */
.btn {
  font-family: 'Sarabun', sans-serif;
  font-size: 0.85rem;
  padding: 10px 16px;
  border-radius: 8px;
  border: none;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.2s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.btn-primary {
  background-color: var(--primary);
  color: #fff;
}
.btn-primary:hover {
  background-color: var(--primary-hover);
  transform: translateY(-1px);
}
.btn-primary:active {
  transform: translateY(0);
}

.btn-danger {
  background-color: #ef4444;
  color: #fff;
}
.btn-danger:hover {
  background-color: #dc2626;
  transform: translateY(-1px);
}
.btn-danger:active {
  transform: translateY(0);
}

.btn-secondary {
  background-color: var(--bg-card);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.05);
}
.btn-secondary:hover {
  background-color: #475569;
}

.btn-outline {
  background-color: transparent;
  color: var(--text-main);
  border: 1px solid var(--border);
}
.btn-outline:hover {
  background-color: rgba(255,255,255,0.05);
  border-color: var(--text-muted);
}

.btn-sm {
  padding: 6px 10px;
  font-size: 0.75rem;
  border-radius: 6px;
}

.btn-block {
  width: 100%;
}

/* Form Controls */
.form-group {
  margin-bottom: 15px;
}
.form-group:last-child {
  margin-bottom: 0;
}

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

label {
  display: block;
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 6px;
  font-weight: 500;
}

.label-header {
  font-weight: 600;
  margin-top: 10px;
  color: #cbd5e1;
}

input[type="text"], select, textarea, input[type="number"] {
  width: 100%;
  font-family: 'Sarabun', sans-serif;
  background-color: var(--bg-dashboard);
  border: 1px solid var(--border);
  color: #fff;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 0.9rem;
  outline: none;
  transition: border-color 0.2s ease;
}

input[type="text"]:focus, select:focus, textarea:focus, input[type="number"]:focus {
  border-color: var(--border-focus);
}

textarea {
  resize: vertical;
}

.helper-text {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 4px;
  display: block;
}

/* Checkbox Grid */
.checkbox-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 10px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  color: var(--text-main);
  cursor: pointer;
  user-select: none;
}

.checkbox-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--primary);
  cursor: pointer;
}

/* Tabs Card Grid */
.tab-container {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 20px;
}

.tab-btn {
  background: rgba(30, 41, 59, 0.4);
  border: 1px solid rgba(255, 255, 255, 0.05);
  border-radius: 12px;
  color: var(--text-muted);
  padding: 12px 8px;
  cursor: pointer;
  font-family: 'Mitr', sans-serif;
  font-size: 0.8rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -2px rgba(0, 0, 0, 0.1);
  text-align: center;
  position: relative;
  overflow: hidden;
}

.tab-premium-badge {
  position: absolute;
  top: 0px;
  right: 0px;
  background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
  color: #fff;
  font-size: 0.55rem;
  padding: 2px 6px;
  border-bottom-left-radius: 8px;
  font-weight: bold;
  letter-spacing: 0.5px;
  line-height: 1;
  font-family: 'Sarabun', sans-serif;
  box-shadow: -1px 1px 3px rgba(0, 0, 0, 0.2);
}


.tab-btn:hover {
  color: #fff;
  background: rgba(30, 41, 59, 0.7);
  transform: translateY(-2px);
  border-color: rgba(255, 255, 255, 0.15);
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.3), 0 4px 6px -4px rgba(0, 0, 0, 0.3);
}

.tab-btn.active {
  color: #fff;
  background: linear-gradient(135deg, #4f46e5 0%, #312e81 100%);
  border-color: rgba(255, 255, 255, 0.2);
  box-shadow: 0 10px 15px -3px rgba(79, 70, 229, 0.4), 0 4px 6px -4px rgba(79, 70, 229, 0.4);
  font-weight: 500;
}

.tab-btn.active::after {
  display: none;
}

.tab-icon {
  font-size: 1.3rem;
  transition: transform 0.2s ease;
}

.tab-btn:hover .tab-icon {
  transform: scale(1.15) rotate(3deg);
}

.tab-btn:active {
  transform: translateY(0) scale(0.97);
}

.tab-content {
  display: none;
}
.tab-content.active {
  display: block;
}

/* Scrollable Container for Selection */
.selector-scroll-container {
  max-height: 320px;
  overflow-y: auto;
  background-color: var(--bg-dashboard);
  border-radius: 8px;
  border: 1px solid var(--border);
  padding: 10px;
}

.selector-scroll-container::-webkit-scrollbar {
  width: 6px;
}
.selector-scroll-container::-webkit-scrollbar-track {
  background: var(--bg-dashboard);
}
.selector-scroll-container::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

.selector-subheading {
  font-family: 'Mitr', sans-serif;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin: 12px 0 6px 0;
  font-weight: 500;
  text-transform: uppercase;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  padding-bottom: 3px;
  letter-spacing: 0.5px;
}
.selector-subheading:first-child {
  margin-top: 0;
}

/* Consonant Selector Grid */
.consonant-selector-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 5px;
  margin-bottom: 10px;
}

.consonant-badge {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 38px;
  border-radius: 6px;
  border: 1px solid var(--border);
  font-size: 0.95rem;
  cursor: pointer;
  user-select: none;
  transition: all 0.15s ease;
  font-weight: 500;
  color: var(--text-main);
  background-color: rgba(255, 255, 255, 0.02);
}

.consonant-badge:hover {
  background-color: rgba(255, 255, 255, 0.06);
  border-color: var(--text-muted);
}

.consonant-badge.active {
  background-color: var(--primary);
  border-color: var(--primary);
  color: #fff;
  box-shadow: 0 0 8px rgba(99, 102, 241, 0.45);
  font-weight: bold;
}

/* Color pickers */
input[type="color"] {
  border: 1px solid var(--border);
  background: none;
  height: 38px;
  border-radius: 8px;
  padding: 2px;
  cursor: pointer;
  width: 100%;
}

.sidebar-footer {
  padding: 20px;
  border-top: 1px solid var(--border);
  background-color: rgba(0,0,0,0.2);
}

.btn-icon {
  font-size: 1.1rem;
}

/* --- RIGHT PREVIEW AREA --- */
.preview-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  background-color: #e2e8f0;
  height: 100vh;
}

.preview-toolbar {
  height: 60px;
  background-color: #fff;
  border-bottom: 1px solid #cbd5e1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 30px;
  box-shadow: 0 2px 5px rgba(0,0,0,0.03);
  z-index: 5;
}

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

.zoom-btn {
  background: #f1f5f9;
  border: 1px solid #cbd5e1;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 0.8rem;
  transition: all 0.15s ease;
}
.zoom-btn:hover {
  background-color: var(--primary-light);
  border-color: #a5b4fc;
}

.zoom-text {
  font-family: 'Outfit', sans-serif;
  font-size: 0.9rem;
  font-weight: 500;
  width: 45px;
  text-align: center;
}

.page-count {
  font-size: 0.85rem;
  font-weight: 500;
  color: #64748b;
  background: #f1f5f9;
  padding: 6px 12px;
  border-radius: 20px;
}

.preview-scroll-wrapper {
  flex: 1;
  overflow: auto;
  padding: 40px;
  display: flex;
  justify-content: center;
}

.pages-container {
  display: flex;
  flex-direction: column;
  gap: 30px;
}

/* --- A4 PAGE STYLE --- */
.a4-page {
  width: 210mm;
  height: 297mm;
  min-height: 297mm;
  background-color: var(--white);
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.08), 0 1px 3px rgba(0, 0, 0, 0.02);
  border-radius: 4px;
  padding: 18mm 15mm;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  position: relative;
  overflow: hidden;
}

/* Page header */
.page-header {
  border: 1px solid #94a3b8;
  border-radius: 8px;
  padding: 12px 18px;
  margin-bottom: 20px;
  font-size: 0.85rem;
  display: flex;
  flex-direction: column;
  gap: 12px; /* Gaps between information rows */
}

.student-info-row {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  width: 100%;
}

.info-item {
  display: flex;
  align-items: flex-end;
  flex: 1;
}
.info-item.small {
  flex: 0 0 12%;
}
.info-item.medium {
  flex: 0 0 20%;
}

.info-label {
  font-weight: 600;
  color: #334155;
  white-space: nowrap;
}

.info-line {
  flex: 1;
  border-bottom: 1px dashed #64748b;
  margin-left: 5px;
  height: 18px;
}

.score-box {
  border: 1px solid #475569;
  padding: 4px 12px;
  border-radius: 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 85px;
}
.score-title {
  font-size: 0.7rem;
  font-weight: 600;
  color: #475569;
}
.score-line {
  font-size: 0.9rem;
  font-weight: bold;
  height: 20px;
  display: flex;
  align-items: center;
}

/* Title & Subtitle */
.worksheet-title-area {
  text-align: center;
  margin-bottom: 25px;
}

.worksheet-title {
  font-family: 'Sarabun', sans-serif;
  font-size: 1.55rem;
  font-weight: bold;
  color: #1e293b;
  margin-bottom: 6px;
}

.worksheet-subtitle {
  font-size: 0.9rem;
  color: #475569;
  line-height: 1.4;
}

/* Page body */
.page-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 15px; /* Spacing between rows */
}

/* Tracing Row layout */
.tracing-row {
  display: flex;
  align-items: center;
  width: 100%;
  height: calc(var(--font-size-val) * 1.5);
  box-sizing: border-box;
}

.ref-cell {
  width: calc(var(--font-size-val) * 1.3);
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f8fafc;
  box-sizing: border-box;
}

.char-ref {
  font-family: 'Sarabun', sans-serif;
  font-size: calc(var(--font-size-val) * 0.9);
  font-weight: 600;
  color: var(--lead-char-color);
  line-height: 1;
  display: inline-block;
  transform-origin: center;
  white-space: nowrap;
}

.tracing-cells-container {
  display: flex;
  flex: 1;
  height: 100%;
  box-sizing: border-box;
  overflow: hidden;
}

.tracing-cell {
  flex: 1;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  box-sizing: border-box;
  overflow: visible;
}

.char-trace {
  font-family: 'Dot3', sans-serif;
  font-size: var(--font-size-val);
  color: var(--dot-color);
  font-weight: var(--dot-weight);
  -webkit-text-stroke: var(--dot-stroke) var(--dot-color);
  line-height: 1;
  position: relative;
  z-index: 2;
  user-select: none;
  margin-top: calc(var(--font-size-val) * 0.05);
  display: inline-block;
  transform-origin: center;
  white-space: nowrap;
}

/* --- GUIDELINES STYLES --- */

/* Default borders for active grids (except grid-none) */
.tracing-row:not(.grid-none-active) .ref-cell {
  border: 1.5px solid #64748b;
  border-right: 2.5px solid #475569;
  border-radius: 6px 0 0 6px;
}
.tracing-row:not(.grid-none-active) .tracing-cells-container {
  border: 1.5px solid #64748b;
  border-left: none;
  border-radius: 0 6px 6px 0;
}
.tracing-row:not(.grid-none-active):not(.has-ref) .tracing-cells-container {
  border-left: 1.5px solid #64748b;
  border-radius: 6px;
}

/* Dashed dividers between cells */
.tracing-row:not(.grid-none-active) .tracing-cell:not(:last-child) {
  border-right: 1px dashed var(--guide-color);
}

/* 5-Line Grid (Standard school notebook) */
.grid-5 {
  background:
    linear-gradient(to bottom, var(--guide-color), var(--guide-color)) no-repeat 0 0% / 100% 1px,
    linear-gradient(to bottom, var(--guide-color), var(--guide-color)) no-repeat 0 25% / 100% 1px,
    linear-gradient(to bottom, var(--guide-color), var(--guide-color)) no-repeat 0 50% / 100% 1px,
    linear-gradient(to bottom, var(--guide-color), var(--guide-color)) no-repeat 0 75% / 100% 1px,
    linear-gradient(to bottom, var(--guide-color), var(--guide-color)) no-repeat 0 100% / 100% 1px;
}

/* Double Line */
.grid-double {
  background:
    linear-gradient(to bottom, var(--guide-color), var(--guide-color)) no-repeat 0 25% / 100% 1px,
    linear-gradient(to bottom, var(--guide-color), var(--guide-color)) no-repeat 0 75% / 100% 1px;
}

/* Single Line */
.grid-single {
  background:
    linear-gradient(to bottom, var(--guide-color), var(--guide-color)) no-repeat 0 75% / 100% 1px;
}

/* Vertical Grid Box (Solid border with dashed cross inside) */
.grid-vertical {
  background-image:
    linear-gradient(to right, var(--guide-color) 50%, transparent 50%),
    linear-gradient(to bottom, var(--guide-color) 50%, transparent 50%);
  background-size: 8px 1px, 1px 8px;
  background-position: 0 50%, 50% 0;
  background-repeat: repeat-x, repeat-y;
}

/* Empty Grid Box (No lines inside, borders only) */
.grid-empty {
  background: none !important;
}

/* Page Footer */
.page-footer {
  height: 25px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.75rem;
  color: #94a3b8;
  margin-top: 20px;
  border-top: 1px solid #e2e8f0;
  padding-top: 8px;
}

/* --- PRINT OPTIMIZATION --- */
@media print {
  * {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }

  @page {
    size: A4 portrait;
    margin: 0;
  }
  
  body {
    background-color: #fff !important;
    overflow: visible;
    height: auto;
  }
  
  .app-container {
    display: block;
    height: auto;
    width: auto;
  }
  
  .sidebar, .preview-toolbar, .mobile-toggle-bar, .custom-modal-overlay {
    display: none !important;
  }
  
  .preview-area {
    display: block !important;
    background: none !important;
    height: auto;
    overflow: visible;
  }
  
  .preview-scroll-wrapper {
    display: block !important;
    overflow: visible;
    padding: 0 !important;
    margin: 0 !important;
  }
  
  .pages-container {
    display: block !important;
    transform: none !important;
    gap: 0;
  }
  
  .a4-page {
    margin: 0 !important;
    box-shadow: none !important;
    border-radius: 0 !important;
    page-break-inside: avoid;
    /* Strict dimensions to fit exactly in printer page */
    width: 210mm;
    height: 297mm;
    min-height: 297mm;
    padding: 15mm;
  }

  .a4-page:not(:last-child) {
    page-break-after: always;
  }
}

/* --- MEMBERSHIP & AUTH STYLES --- */
.auth-profile-area {
  margin-top: 10px;
  background: rgba(255, 255, 255, 0.05);
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.loading-auth {
  font-size: 0.8rem;
  color: var(--text-muted);
  text-align: center;
}

.auth-user-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.auth-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 1.5px solid var(--primary-light);
}

.auth-details {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.auth-name {
  font-size: 0.85rem;
  font-weight: 500;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.auth-status-badge {
  font-size: 0.7rem;
  padding: 2px 6px;
  border-radius: 10px;
  font-weight: 600;
  display: inline-block;
  width: fit-content;
}

.auth-status-badge.free {
  background-color: #64748b;
  color: #fff;
}

.auth-status-badge.premium {
  background-color: #fbbf24;
  color: #1e293b;
  box-shadow: 0 0 6px rgba(251, 191, 36, 0.4);
}

.btn-auth-action {
  font-size: 0.75rem;
  padding: 4px 8px;
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  white-space: nowrap;
}

.btn-auth-action:hover {
  color: #fff;
  border-color: var(--text-muted);
  background-color: rgba(255, 255, 255, 0.02);
}

/* Modals */
.custom-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(15, 23, 42, 0.75);
  backdrop-filter: blur(4px);
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.custom-modal-content {
  background-color: #fff;
  border-radius: 16px;
  width: 100%;
  max-width: 500px;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.2), 0 10px 10px -5px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  animation: modalFadeIn 0.3s ease-out;
}

@keyframes modalFadeIn {
  from {
    opacity: 0;
    transform: scale(0.95);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.modal-header-premium {
  background: linear-gradient(135deg, #4f46e5, #6366f1);
  color: #fff;
  padding: 20px 24px;
  display: flex;
  align-items: center;
  gap: 15px;
}

.premium-badge-icon {
  font-size: 2rem;
}

.modal-body-premium {
  padding: 24px;
  overflow-y: auto;
  max-height: 70vh;
}

.premium-desc {
  font-size: 0.95rem;
  line-height: 1.5;
  color: #334155;
  margin-bottom: 20px;
}

.payment-instructions h4 {
  font-size: 0.9rem;
  color: #1e293b;
  margin-bottom: 10px;
  font-weight: 600;
}

.payment-instructions ol {
  padding-left: 20px;
  font-size: 0.85rem;
  color: #475569;
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 20px;
}

.promptpay-box {
  background-color: #f0fdfa;
  border: 1px solid #99f6e4;
  border-radius: 10px;
  padding: 12px 16px;
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.promptpay-header {
  font-size: 0.75rem;
  font-weight: bold;
  color: #0d9488;
}

.promptpay-number {
  font-size: 1.25rem;
  font-weight: bold;
  color: #1e293b;
  letter-spacing: 0.5px;
}

.promptpay-name {
  font-size: 0.8rem;
  color: #0d9488;
}

.activation-form-group {
  border-top: 1px solid #e2e8f0;
  padding-top: 20px;
}

.activation-form-group label {
  color: #475569;
  font-weight: 600;
  margin-bottom: 8px;
}

.input-with-button {
  display: flex;
  gap: 8px;
}

.input-with-button input {
  flex: 1;
  color: #1e293b;
  background-color: #f8fafc;
  border: 1px solid #cbd5e1;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 0.9rem;
  outline: none;
}

.input-with-button input:focus {
  border-color: #6366f1;
}

.activation-error-msg {
  font-size: 0.8rem;
  color: #ef4444;
  margin-top: 8px;
  font-weight: 500;
}

.modal-footer-premium {
  padding: 16px 24px;
  background-color: #f8fafc;
  border-top: 1px solid #e2e8f0;
  display: flex;
  justify-content: flex-end;
}

/* Admin Panel */
.admin-modal-width {
  max-width: 700px;
  background-color: #0f172a;
  color: #f8fafc;
}

.modal-header-admin {
  padding: 20px 24px;
  border-bottom: 1px solid #334155;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header-admin h3 {
  font-size: 1.2rem;
  color: #fff;
}

.close-admin-btn {
  font-size: 1.5rem;
  cursor: pointer;
  color: #94a3b8;
}
.close-admin-btn:hover {
  color: #fff;
}

.modal-body-admin {
  padding: 24px;
  overflow-y: auto;
  max-height: 80vh;
}

.admin-dashboard-stats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background-color: #1e293b;
  border: 1px solid #334155;
  border-radius: 12px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.stat-num {
  font-size: 1.8rem;
  font-weight: bold;
  color: #38bdf8;
}

.stat-label {
  font-size: 0.8rem;
  color: #94a3b8;
  margin-top: 4px;
}

.admin-actions-box {
  background-color: #1e293b;
  border: 1px solid #334155;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 24px;
}

.admin-actions-box h4, .admin-key-list-section h4 {
  font-size: 0.95rem;
  color: #fff;
  margin-bottom: 12px;
}

.new-key-result {
  margin-top: 15px;
  border-top: 1px dashed #334155;
  padding-top: 15px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.new-key-result span {
  font-size: 0.8rem;
  color: #38bdf8;
}

.key-box-copy {
  display: flex;
  align-items: center;
  background-color: #0f172a;
  border: 1px solid #334155;
  border-radius: 8px;
  padding: 8px 12px;
  justify-content: space-between;
}

.key-box-copy code {
  font-family: monospace;
  font-size: 1.1rem;
  color: #fbbf24;
  letter-spacing: 0.5px;
}

.admin-key-list-section {
  display: flex;
  flex-direction: column;
}

.admin-table-container {
  border: 1px solid #334155;
  border-radius: 8px;
  overflow: hidden;
  max-height: 300px;
  overflow-y: auto;
}

.admin-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
  text-align: left;
}

.admin-table th, .admin-table td {
  padding: 10px 12px;
  border-bottom: 1px solid #334155;
}

.admin-table th {
  background-color: #1e293b;
  color: #94a3b8;
  font-weight: 600;
}

.admin-table td {
  color: #cbd5e1;
}

.admin-table tr:hover {
  background-color: rgba(255,255,255,0.02);
}

.key-status-label {
  padding: 2px 6px;
  border-radius: 10px;
  font-size: 0.7rem;
  font-weight: bold;
}

.key-status-label.unused {
  background-color: rgba(16, 185, 129, 0.15);
  color: #34d399;
}

.key-status-label.used {
  background-color: rgba(239, 68, 68, 0.15);
  color: #f87171;
}

.btn-delete-key {
  background-color: transparent;
  color: #ef4444;
  border: 1px solid rgba(239, 68, 68, 0.3);
  padding: 2px 6px;
  border-radius: 4px;
  cursor: pointer;
}
.btn-delete-key:hover {
  background-color: #ef4444;
  color: #fff;
}

/* --- PREMIUM LOCKS & WATERMARK --- */
.watermark {
  position: absolute;
  top: 55%;
  left: 50%;
  transform: translate(-50%, -50%) rotate(-30deg);
  font-size: 3.2rem;
  font-weight: 900;
  color: rgba(220, 220, 220, 0.3);
  white-space: nowrap;
  pointer-events: none;
  user-select: none;
  z-index: 99;
  font-family: 'Sarabun', sans-serif;
  letter-spacing: 2px;
}



/* --- COLORING & TRACING STYLES --- */
.coloring-item-input-block {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
}

.btn-regen-img {
  background: transparent;
  border: none;
  color: var(--primary);
  font-size: 0.75rem;
  font-weight: bold;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 3px;
  padding: 2px 6px;
  border-radius: 4px;
  transition: background-color 0.15s ease;
}
.btn-regen-img:hover {
  background-color: var(--primary-light);
}

/* Coloring grid inside A4 page */
.coloring-split-container {
  display: flex;
  flex-direction: column;
  gap: 30px;
  flex: 1;
  justify-content: flex-start;
}

.coloring-row {
  display: flex;
  gap: 20px;
  align-items: center;
  height: 250px;
  box-sizing: border-box;
}

.coloring-img-box {
  width: 250px;
  height: 250px;
  border: 1.5px solid #94a3b8;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
  background-color: #fff;
}

.coloring-img-box img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  display: block;
  filter: contrast(110%);
}

.coloring-trace-box {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 15px;
}

.coloring-guidelines-container {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
  height: 100%;
  position: relative;
  box-sizing: border-box;
  overflow: hidden;
}

/* Large layout */
.coloring-large-container {
  display: flex;
  flex-direction: column;
  gap: 25px;
  flex: 1;
  justify-content: space-between;
}

.coloring-large-img-box {
  width: 100%;
  height: 480px;
  border: 1.5px solid #94a3b8;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
  background-color: #fff;
}

.coloring-large-img-box img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  display: block;
}

.coloring-large-trace-box {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 18px;
  justify-content: center;
}

/* Image loading skeleton */
.image-loading-skeleton {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, #f1f5f9 25%, #e2e8f0 50%, #f1f5f9 75%);
  background-size: 200% 100%;
  animation: loading-pulse 1.5s infinite;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #64748b;
  font-size: 0.85rem;
  font-weight: 500;
  gap: 8px;
}

@keyframes loading-pulse {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

/* Matching Worksheet */
.matching-container {
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex: 1;
  justify-content: space-between;
  margin-top: 10px;
}

.matching-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 110px;
  box-sizing: border-box;
}

.matching-left-item {
  display: flex;
  align-items: center;
  gap: 25px;
  width: 45%;
}

.matching-img-box {
  width: 100px;
  height: 100px;
  border: 1.5px solid #94a3b8;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
  background-color: #fff;
}

.matching-img-box img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  display: block;
}

.matching-right-item {
  display: flex;
  align-items: center;
  gap: 25px;
  width: 45%;
  justify-content: flex-end;
}

.matching-word-box {
  display: flex;
  align-items: center;
  height: 100px;
  position: relative;
}

/* Dots for matching */
.matching-dot {
  width: 14px;
  height: 14px;
  border: 2px solid #000;
  border-radius: 50%;
  background-color: #fff;
  flex-shrink: 0;
  cursor: pointer;
  z-index: 10;
}

/* Tracing text */
.matching-trace-text {
  font-family: 'Sarabun', sans-serif;
  font-size: 56px;
  white-space: nowrap;
  display: flex;
  align-items: baseline;
  height: 100%;
}

.matching-text-solid {
  color: #000;
  font-family: 'Sarabun', sans-serif;
  font-weight: 600;
  font-size: 0.82em;
}

.matching-text-trace {
  font-family: 'Dot3', sans-serif;
  color: var(--dot-color);
  font-weight: var(--dot-weight);
  -webkit-text-stroke: var(--dot-stroke) var(--dot-color);
  user-select: none;
}

/* --- SPELLING MODE STYLES --- */
.spelling-container {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
}

.spelling-row {
  display: flex;
  align-items: center;
  height: 110px;
  box-sizing: border-box;
  width: 100%;
  padding: 0 10px;
}

.spelling-img-container {
  position: relative;
  width: 110px;
  height: 110px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.spelling-img-box {
  width: 95px;
  height: 95px;
  border: 1.5px solid #94a3b8;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
  background-color: #fff;
}

.spelling-img-box img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  display: block;
}

.spelling-number-badge {
  position: absolute;
  top: 0px;
  left: 0px;
  width: 24px;
  height: 24px;
  background-color: #fff;
  border: 1.5px solid #000;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Sarabun', sans-serif;
  font-size: 0.85rem;
  font-weight: 600;
  color: #000;
  z-index: 5;
}

.spelling-equation-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  padding-left: 40px;
  box-sizing: border-box;
}

.spelling-equation {
  display: flex;
  align-items: baseline;
  gap: 12px;
}

.spelling-underline {
  border-bottom: 2px solid #000;
  min-height: 52px;
  display: inline-flex;
  align-items: baseline;
  justify-content: center;
  box-sizing: border-box;
}

.spelling-underline.consonant {
  width: 90px;
}

.spelling-underline.combined {
  width: 170px;
}

.spelling-vowel-text {
  font-family: 'Sarabun', sans-serif;
  font-size: 52px;
  font-weight: 500;
  color: #000;
  display: inline-flex;
  align-items: baseline;
  line-height: 1;
}

.spelling-operator {
  font-family: 'Sarabun', sans-serif;
  font-size: 38px;
  font-weight: 500;
  color: #000;
  margin: 0 10px;
  display: inline-flex;
  align-items: baseline;
  line-height: 1;
}

.spelling-char-trace {
  font-family: 'Dot3', sans-serif;
  font-size: 56px;
  color: var(--dot-color);
  font-weight: var(--dot-weight);
  -webkit-text-stroke: var(--dot-stroke) var(--dot-color);
  user-select: none;
  line-height: 1;
  display: inline-flex;
  align-items: baseline;
}

.spelling-char-solid {
  font-family: 'Sarabun', sans-serif;
  font-size: 52px;
  font-weight: 500;
  color: #000;
  line-height: 1;
  display: inline-flex;
  align-items: baseline;
}

/* --- REBUS SPELLING MODE STYLES --- */
.rebus-container {
  display: flex;
  flex-wrap: wrap;
  gap: 18px 4%;
  width: 100%;
  box-sizing: border-box;
}

.rebus-row {
  display: flex;
  align-items: center;
  width: 48%; /* 2 columns layout */
  height: 110px;
  box-sizing: border-box;
}

.rebus-img-container {
  position: relative;
  width: 105px;
  height: 105px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.rebus-img-box {
  width: 95px;
  height: 95px;
  border: 1.5px solid #94a3b8;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
  background-color: #fff;
}

.rebus-img-box img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  display: block;
}

.rebus-number-badge {
  position: absolute;
  top: 0px;
  left: 0px;
  width: 24px;
  height: 24px;
  background-color: #fff;
  border: 1.5px solid #000;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Sarabun', sans-serif;
  font-size: 0.85rem;
  font-weight: 600;
  color: #000;
  z-index: 5;
}

.rebus-equation-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  padding-left: 15px;
  box-sizing: border-box;
}

.rebus-equation {
  display: flex;
  align-items: baseline;
  gap: 8px;
  width: 100%;
}

.rebus-vowel-text {
  font-family: 'Sarabun', sans-serif;
  font-size: 52px;
  font-weight: 500;
  color: #000;
  display: inline-flex;
  align-items: baseline;
  line-height: 1;
  margin-right: 12px;
  user-select: none;
}

.rebus-write-line {
  border-bottom: 2px dashed #94a3b8; /* Dashed writing line for child to write */
  min-height: 55px;
  flex: 1;
  display: inline-flex;
  align-items: baseline;
  justify-content: center;
  box-sizing: border-box;
  padding-bottom: 2px;
}

.rebus-char-trace {
  font-family: 'Dot3', sans-serif;
  font-size: 56px;
  color: var(--dot-color);
  font-weight: var(--dot-weight);
  -webkit-text-stroke: var(--dot-stroke) var(--dot-color);
  user-select: none;
  line-height: 1;
  display: inline-flex;
  align-items: baseline;
}

/* --- CHOICE MATCHING MODE STYLES --- */
.choice-match-container {
  display: flex;
  flex-wrap: wrap;
  gap: 18px 4%;
  width: 100%;
  box-sizing: border-box;
}

.choice-match-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 48%; /* 2 columns layout */
  height: 120px;
  border: 1.5px solid #94a3b8;
  border-radius: 12px;
  padding: 10px 15px;
  box-sizing: border-box;
  position: relative;
  background-color: #fff;
}

.choice-match-left-wrapper {
  display: flex;
  align-items: center;
  gap: 20px;
}

.choice-match-left {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.choice-match-img-box {
  width: 70px;
  height: 70px;
  border: 1px solid #94a3b8;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
  background-color: #fff;
}

.choice-match-img-box img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  display: block;
}

.choice-match-number-badge {
  position: absolute;
  top: 0px;
  left: 0px;
  width: 24px;
  height: 24px;
  background-color: #fff;
  border: 1.5px solid #000;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Sarabun', sans-serif;
  font-size: 0.85rem;
  font-weight: 600;
  color: #000;
  z-index: 5;
}

.choice-match-write-line {
  border-bottom: 2px dashed #94a3b8;
  width: 75px;
  min-height: 30px;
  display: inline-flex;
  align-items: baseline;
  justify-content: center;
  box-sizing: border-box;
  padding-bottom: 0px;
}

.choice-match-char-trace {
  font-family: 'Dot3', sans-serif;
  font-size: 32px;
  color: var(--dot-color);
  font-weight: var(--dot-weight);
  -webkit-text-stroke: var(--dot-stroke) var(--dot-color);
  user-select: none;
  line-height: 0.9;
  display: inline-flex;
  align-items: baseline;
}

.choice-match-mid-dot {
  width: 10px;
  height: 10px;
  background-color: #000;
  border: 1px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 0 1.5px #000;
  display: inline-block;
  margin-left: 10px;
  flex-shrink: 0;
  z-index: 3;
}

.choice-match-right {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 12px;
  height: 100%;
  padding-left: 5px;
}

.choice-match-option-wrapper {
  display: flex;
  align-items: center;
  gap: 8px;
}

.choice-match-opt-dot {
  width: 10px;
  height: 10px;
  background-color: #000;
  border: 1px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 0 1.5px #000;
  display: inline-block;
  flex-shrink: 0;
  z-index: 3;
}

.choice-match-circle {
  width: 44px;
  height: 44px;
  border: 1.5px solid #3b82f6;
  border-radius: 50%;
  background-color: #eff6ff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Sarabun', sans-serif;
  font-size: 26px;
  font-weight: 500;
  color: #000;
  user-select: none;
}

.choice-match-svg-line {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 2;
}

/* --- WORD BLENDING MODE STYLES --- */
.word-blend-container {
  display: flex;
  flex-wrap: wrap;
  gap: 15px 3.5%;
  width: 100%;
  box-sizing: border-box;
}

.word-blend-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  width: 31%; /* 3 columns layout */
  height: 180px;
  border: 1.5px solid #cbd5e1;
  border-radius: 12px;
  padding: 10px 8px;
  box-sizing: border-box;
  position: relative;
  background-color: #fff;
}

.word-blend-circles-row {
  display: flex;
  justify-content: space-around;
  width: 100%;
  margin-top: 5px;
  position: relative;
  z-index: 5;
}

.word-blend-circle {
  width: 38px;
  height: 38px;
  border: 1.5px solid #475569;
  border-radius: 50%;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Sarabun', sans-serif;
  font-size: 20px;
  font-weight: bold;
  color: #000;
  box-shadow: 0 2px 4px rgba(0,0,0,0.05);
  user-select: none;
}

/* Color codes to identify word segments */
.word-blend-circle.consonant {
  border-color: #1d4ed8;
  color: #1d4ed8;
  background-color: #eff6ff;
}

.word-blend-circle.vowel {
  border-color: #b45309;
  color: #b45309;
  background-color: #fffbeb;
}

.word-blend-circle.final-consonant {
  border-color: #047857;
  color: #047857;
  background-color: #ecfdf5;
}

.word-blend-write-box {
  width: 90%;
  height: 48px;
  border: 1.5px solid #94a3b8;
  border-radius: 10px;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  z-index: 5;
  box-shadow: inset 0 1px 3px rgba(0,0,0,0.05);
}

.word-blend-write-line-trace {
  font-family: 'Dot3', sans-serif;
  font-size: 28px;
  color: var(--dot-color);
  font-weight: var(--dot-weight);
  -webkit-text-stroke: var(--dot-stroke) var(--dot-color);
  user-select: none;
  line-height: 0.9;
  display: inline-flex;
  align-items: baseline;
}

.word-blend-badge {
  position: absolute;
  top: 4px;
  left: 6px;
  width: 18px;
  height: 18px;
  background-color: #f8fafc;
  border: 1px solid #cbd5e1;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Sarabun', sans-serif;
  font-size: 0.65rem;
  font-weight: bold;
  color: #64748b;
  z-index: 6;
}

.word-blend-svg-lines {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 2;
}

/* Base Desktop Hidden Style */
.mobile-toggle-bar {
  display: none;
}

/* --- Mobile/Tablet Responsiveness (max-width: 1024px) --- */
@media screen and (max-width: 1024px) {
  /* Stack app-container elements vertically */
  .app-container {
    flex-direction: column;
    height: 100vh;
    width: 100vw;
    overflow: hidden;
  }

  /* Show the mobile toggle bar */
  .mobile-toggle-bar {
    display: flex;
    background-color: var(--bg-dashboard);
    border-bottom: 1px solid #1e293b;
    height: 60px;
    min-height: 60px;
    width: 100%;
    z-index: 20;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
  }

  .mobile-tab-btn {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-family: 'Sarabun', sans-serif;
    font-size: 0.95rem;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.2s ease;
    border-bottom: 3px solid transparent;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
  }

  .mobile-tab-btn.active {
    color: #fff;
    border-bottom-color: var(--primary);
    background: rgba(255, 255, 255, 0.03);
  }

  /* Make sidebar full width and fill the vertical space remaining */
  .sidebar {
    width: 100% !important;
    min-width: 100% !important;
    height: calc(100vh - 60px) !important;
    border-right: none;
    box-shadow: none;
  }

  /* Make preview area full width and fill space */
  .preview-area {
    width: 100% !important;
    height: calc(100vh - 60px) !important;
    padding: 10px !important;
    display: none; /* Hidden by default when settings tab is active */
  }

  /* In mobile active state, show preview area and hide sidebar */
  .app-container.show-preview .sidebar {
    display: none !important;
  }

  .app-container.show-preview .preview-area {
    display: flex !important;
    flex-direction: column;
  }

  /* Adjust preview toolbar spacing on mobile */
  .preview-toolbar {
    padding: 10px !important;
    gap: 10px !important;
    flex-wrap: wrap;
    justify-content: space-between;
  }

  /* Scale the worksheets in scroll container so they don't break width constraints */
  .preview-scroll-wrapper {
    padding: 10px 0 !important;
    display: flex;
    justify-content: center;
    overflow-x: auto !important;
  }

  /* Adjust spacing in the control sidebar for better usability on touch screens */
  .control-section {
    padding: 15px !important;
    margin-bottom: 12px !important;
  }

  .tab-container {
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)) !important;
  }

  .btn {
    padding: 10px 16px !important;
  }
  
  /* Quick select presets grid adjustment */
  .preset-buttons {
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)) !important;
  }
}
