@font-face {
  font-family: 'TH Sarabun New';
  src: url('/fonts/THSarabun.ttf') format('truetype');
  font-weight: normal;
  font-style: normal;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'TH Sarabun New', 'Sarabun', 'Segoe UI', sans-serif;
  background-color: #f0f2f5;
  min-height: 100vh;
}

.app-container {
  display: flex;
  min-height: 100vh;
}

.control-panel {
  width: 400px;
  background: #fff;
  padding: 20px;
  box-shadow: 2px 0 10px rgba(0,0,0,0.1);
  overflow-y: auto;
  max-height: 100vh;
}

.control-panel h1 {
  font-size: 24px;
  color: #1a237e;
  margin-bottom: 20px;
  text-align: center;
  border-bottom: 2px solid #1a237e;
  padding-bottom: 10px;
}

.section {
  margin-bottom: 25px;
  padding: 15px;
  background: #f8f9fa;
  border-radius: 8px;
}

.section h2 {
  font-size: 18px;
  color: #333;
  margin-bottom: 15px;
}

.template-settings {
  background: #e8f4f8;
}

.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 15px;
}

.setting-group {
  background: white;
  padding: 12px;
  border-radius: 6px;
  border: 1px solid #ddd;
}

.setting-group h3 {
  font-size: 14px;
  color: #1976d2;
  margin-bottom: 10px;
  padding-bottom: 5px;
  border-bottom: 1px solid #eee;
}

.setting-item {
  display: flex;
  flex-direction: column;
  margin-bottom: 10px;
}

.setting-item:last-child {
  margin-bottom: 0;
}

.setting-item label {
  font-size: 12px;
  color: #666;
  margin-bottom: 4px;
}

.setting-item input[type="range"] {
  width: 100%;
  cursor: pointer;
}

.setting-value {
  font-size: 12px;
  color: #1976d2;
  font-weight: bold;
  text-align: right;
}

.doc-type-selector {
  display: flex;
  gap: 20px;
}

.radio-label {
  display: flex;
  align-items: center;
  cursor: pointer;
  font-size: 16px;
}

.radio-label input {
  margin-right: 8px;
  width: 18px;
  height: 18px;
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: bold;
  color: #555;
}

.form-group input[type="text"],
.form-group input[type="number"],
.form-group textarea {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 14px;
  font-family: inherit;
}

.form-group textarea {
  resize: vertical;
}

.form-row {
  display: flex;
  gap: 15px;
  margin-bottom: 15px;
}

.form-row .form-group {
  flex: 1;
  margin-bottom: 0;
}

.upload-area {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 15px;
  flex-wrap: wrap;
}

.btn {
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 14px;
  font-family: inherit;
  transition: all 0.3s;
}

.btn-primary {
  background-color: #1976d2;
  color: white;
}

.btn-primary:hover {
  background-color: #1565c0;
}

.btn-secondary {
  background-color: #6c757d;
  color: white;
}

.btn-secondary:hover {
  background-color: #5a6268;
}

.btn-success {
  background-color: #28a745;
  color: white;
  width: 100%;
  padding: 15px;
  font-size: 16px;
}

.btn-success:hover {
  background-color: #218838;
}

.btn-danger {
  background-color: #dc3545;
  color: white;
  width: 100%;
  padding: 15px;
  font-size: 16px;
  margin-top: 10px;
}

.btn-danger:hover {
  background-color: #c82333;
}

.signature-list {
  margin-top: 15px;
}

.signature-list h3 {
  font-size: 16px;
  color: #333;
  margin-bottom: 10px;
}

.signature-list ul {
  list-style: none;
}

.signature-list li {
  background: white;
  padding: 10px;
  margin-bottom: 8px;
  border-radius: 5px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border: 1px solid #ddd;
}

.signature-list li .sig-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.signature-list li .sig-preview {
  width: 50px;
  height: 30px;
  object-fit: contain;
  border: 1px solid #ddd;
}

.signature-list li .sig-name {
  font-weight: bold;
}

.signature-list li .sig-width {
  color: #666;
  font-size: 12px;
}

.signature-list li .delete-btn {
  background: #dc3545;
  color: white;
  border: none;
  padding: 5px 10px;
  border-radius: 3px;
  cursor: pointer;
  font-size: 12px;
}

.button-group {
  margin-top: 20px;
  display: flex;
  align-items: center;
  gap: 15px;
  flex-wrap: wrap;
}

.font-selector {
  display: flex;
  align-items: center;
  gap: 8px;
}

.font-selector label {
  font-weight: bold;
  white-space: nowrap;
}

.font-selector select {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  background: white;
  cursor: pointer;
}

.number-selector {
  display: flex;
  align-items: center;
  gap: 15px;
}

.number-selector label {
  font-weight: bold;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
}

.number-selector input[type="radio"] {
  cursor: pointer;
  margin: 0;
}

.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.5);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.spinner {
  width: 50px;
  height: 50px;
  border: 5px solid #f3f3f3;
  border-top: 5px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-overlay p {
  color: white;
  margin-top: 15px;
  font-size: 18px;
}

.preview-panel {
  flex: 1;
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: #e0e0e0;
  overflow-y: auto;
}

.preview-header h2 {
  font-size: 20px;
  color: #333;
  margin-bottom: 15px;
}

.a4-preview {
  width: 100%;
  display: flex;
  justify-content: center;
}

.a4-page {
  width: 210mm;
  min-height: 297mm;
  background: white;
  padding: var(--margin-top, 2.5cm) var(--margin-right, 2cm) var(--margin-bottom, 2cm) var(--margin-left, 3cm);
  box-shadow: 0 0 20px rgba(0,0,0,0.2);
  font-family: 'TH Sarabun New', 'Sarabun', sans-serif;
  font-size: var(--font-size-body, 16pt);
  line-height: var(--line-height, 1.5);
  position: relative;
  
  --font-size-title: 17pt;
  --font-size-body: 11pt;
  --font-size-subject: 13pt;
  --line-height: 1.5;
  --line-spacing: 1.5;
  --body-line-spacing: 1.5;
  --text-indent: 2em;
  --padding-left: 2em;
  --margin-top: 2.5cm;
  --margin-bottom: 2cm;
  --margin-left: 3cm;
  --margin-right: 2cm;
  --seal-top: 0cm;
  --seal-left: 0cm;
  --seal-width: 1.5cm;
}

.document-header {
  margin-bottom: 20px;
  position: relative;
}

.document-type {
  font-size: var(--font-size-title);
  font-weight: bold;
  margin-bottom: 10px;
  display: inline-block;
  vertical-align: middle;
}

.document-number {
  font-size: var(--font-size-subject);
  margin-bottom: 15px;
}

.document-to {
  margin-bottom: 15px;
  text-align: left;
}

.document-to strong {
  font-weight: normal;
}

.document-subject {
  font-size: var(--font-size-subject);
  font-weight: bold;
  text-align: left;
  margin-bottom: 15px;
}

.document-agency {
  text-align: left;
  margin-bottom: 15px;
  font-size: var(--font-size-subject);
}

.document-body {
  text-align: justify;
  margin-bottom: 30px;
}

.document-body p {
  margin-bottom: 15px;
}

.document-footer {
  margin-top: 50px;
  text-align: center;
}

.signature-section {
  margin-top: 40px;
  text-align: center;
  display: flex;
  justify-content: center;
  gap: 20px;
}

.signature-row {
  display: flex;
  justify-content: center;
}

.signature-name-col {
  flex: 1;
  max-width: 50%;
  text-align: left;
}

.signature-position-col {
  flex: 1;
  max-width: 50%;
  text-align: center;
}

.signer-name-plain {
  font-weight: normal;
  font-size: var(--font-size-body);
}

.signer-role {
  font-weight: normal;
  font-size: var(--font-size-body);
}

.signature-box {
  display: inline-block;
  text-align: center;
  vertical-align: middle;
}

.signature-box img {
  display: block;
  margin: 0 auto 5px;
}

.signer-name {
  font-weight: bold;
}

.signer-role {
  font-size: 14pt;
}

.chakra-symbol {
  display: inline-block;
  vertical-align: middle;
}

.chakra-symbol.left {
  position: absolute;
  top: var(--seal-top, 0cm);
  left: var(--seal-left, 0cm);
  width: var(--seal-width, 1.5cm);
}

.chakra-symbol.center {
  display: block;
  width: var(--seal-width, 1.5cm);
  margin: 0 auto;
}

.chakra-symbol img {
  width: 100%;
  height: auto;
}

.header-content {
  position: relative;
}

.document-meta {
  display: flex;
  justify-content: center;
  margin-top: 10px;
}

.meta-row {
  display: flex;
  flex: 1;
  max-width: 50%;
  align-items: baseline;
}

.meta-label {
  font-weight: bold;
  font-size: var(--font-size-subject);
  vertical-align: baseline;
}

.meta-value {
  font-size: var(--font-size-subject);
  vertical-align: baseline;
}

.document-to,
.document-from,
.document-attachment {
  margin-bottom: 15px;
  line-height: 1.8;
  text-align: left;
  font-size: var(--font-size-body);
}

.inline-label {
  display: inline;
  min-width: auto;
}

.document-from {
  display: none;
}

.document-body .body-section {
  margin-bottom: 15px;
  line-height: var(--body-line-spacing, 1.5);
}

.body-section .section-label {
  margin-bottom: 10px;
  font-size: var(--font-size-subject);
  font-weight: bold;
}

.body-section .section-content {
  text-indent: 2em;
  padding-left: 2em;
}

.signature-area {
  margin-top: 40px;
}

.signature-area .signature-section {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  gap: 20px;
  margin-top: 20px;
}

.signature-info {
  text-align: left;
}

.signer-name-plain {
  font-weight: bold;
  min-width: 150px;
}

.signature-placeholder {
  background: transparent;
}

[contenteditable="true"] {
  border: 1px dashed transparent;
  padding: 2px 5px;
  transition: border-color 0.2s;
}

[contenteditable="true"]:hover,
[contenteditable="true"]:focus {
  border-color: #1976d2;
  background: rgba(25, 118, 210, 0.05);
  outline: none;
}

.modal {
  display: none;
  position: fixed;
  z-index: 1000;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  animation: fadeIn 0.2s;
}

.modal.show {
  display: flex;
  align-items: center;
  justify-content: center;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal-content {
  background-color: white;
  border-radius: 8px;
  width: 90%;
  max-width: 500px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
  animation: slideIn 0.2s;
}

@keyframes slideIn {
  from { transform: translateY(-20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
}

.modal-header h2 {
  margin: 0;
  font-size: 18px;
  color: #333;
}

.close-modal {
  font-size: 24px;
  font-weight: bold;
  color: #666;
  cursor: pointer;
  line-height: 1;
}

.close-modal:hover {
  color: #333;
}

.modal-body {
  padding: 20px;
}

.modal-body .form-group {
  margin-bottom: 15px;
}

.modal-body .form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: bold;
  color: #333;
}

.modal-body .form-group input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  box-sizing: border-box;
}

.modal-body .form-group input:focus {
  outline: none;
  border-color: #1976d2;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 15px 20px;
  border-top: 1px solid #eee;
}

@media print {
  .control-panel,
  .preview-header,
  .loading-overlay {
    display: none !important;
  }
  
  .app-container {
    display: block;
  }
  
  .preview-panel {
    padding: 0;
    background: white;
  }
  
  .a4-page {
    box-shadow: none;
    margin: 0;
    padding: 2.5cm 2cm 2cm 3cm;
  }
}
