:root {
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --bg: #f8fafc;
  --card-bg: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --radius: 12px;
  --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0f19;
    --card-bg: #111827;
    --border: #1f2937;
    --text: #f9fafb;
    --text-muted: #9ca3af;
    --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.5);
  }
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

body {
  background-color: var(--bg);
  color: var(--text);
  line-height: 1.6;
  padding: 24px 16px;
}

.app-container {
  max-width: 1280px;
  margin: 0 auto;
}

.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 16px;
  margin-bottom: 24px;
}

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

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

.btn-back {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius);
  text-decoration: none;
  font-size: 0.88rem;
  font-weight: 500;
  transition: all 0.2s;
}

.btn-back:hover {
  border-color: var(--primary);
  color: var(--primary);
}

.header-icon {
  width: 48px;
  height: 48px;
  background: rgba(37, 99, 235, 0.12);
  color: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
}

.header-icon svg {
  width: 28px;
  height: 28px;
}

h1 {
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.subtitle {
  color: var(--text-muted);
  font-size: 0.95rem;
}

.main-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
}

@media (max-width: 900px) {
  .main-grid {
    grid-template-columns: 1fr;
  }
}

.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.card-header h2 {
  font-size: 1.15rem;
  font-weight: 600;
}

.hint {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 12px;
}

textarea {
  width: 100%;
  height: 320px;
  background: transparent;
  color: inherit;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  font-size: 0.9rem;
  resize: vertical;
  outline: none;
  transition: border-color 0.2s;
  font-family: monospace;
}

textarea:focus {
  border-color: var(--primary);
}

.controls-row {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: center;
  margin: 16px 0;
}

.control-group {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
}

.control-group input[type="number"],
.control-group input[type="text"] {
  background: transparent;
  color: inherit;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  outline: none;
}

.control-group input:focus {
  border-color: var(--primary);
}

.btn-primary {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 10px 18px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-primary:hover {
  background: var(--primary-hover);
}

.btn-secondary {
  background: transparent;
  border: 1px solid var(--border);
  color: inherit;
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-secondary:hover:not(:disabled) {
  background: rgba(37, 99, 235, 0.08);
  border-color: var(--primary);
  color: var(--primary);
}

.btn-secondary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.tabs-list {
  display: flex;
  gap: 8px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 8px;
  margin-bottom: 16px;
  overflow-x: auto;
}

.tab-btn {
  padding: 6px 14px;
  border-radius: 6px;
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-weight: 500;
  font-size: 0.9rem;
}

.tab-btn.active {
  background: var(--primary);
  color: white;
}

.result-view {
  min-height: 250px;
  max-height: 500px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 16px;
  background: rgba(255, 255, 255, 0.02);
}

.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 200px;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.exam-paper {
  line-height: 1.7;
}

.exam-header {
  text-align: center;
  border-bottom: 2px solid var(--border);
  padding-bottom: 12px;
  margin-bottom: 16px;
}

.exam-header h3 {
  font-size: 1.1rem;
}

.exam-code-tag {
  display: inline-block;
  padding: 2px 8px;
  background: rgba(37, 99, 235, 0.15);
  color: var(--primary);
  border-radius: 4px;
  font-weight: 700;
  margin-top: 4px;
}

.exam-q {
  margin-bottom: 14px;
}

.q-title {
  font-weight: 600;
  margin-bottom: 4px;
}

.q-options {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 12px;
  padding-left: 12px;
}

@media (max-width: 600px) {
  .q-options {
    grid-template-columns: 1fr;
  }
}

.answer-key-section {
  border-top: 1px solid var(--border);
  padding-top: 14px;
}

.answer-key-section h3 {
  font-size: 1rem;
  margin-bottom: 10px;
}

.answer-matrix {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  max-height: 180px;
  overflow-y: auto;
}

.ans-badge {
  display: inline-flex;
  gap: 4px;
  background: rgba(37, 99, 235, 0.08);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 8px;
  font-size: 0.85rem;
  font-family: monospace;
}

.ans-badge strong {
  color: var(--primary);
}

/* Modal Xem Trước Bản In */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  padding: 20px;
  backdrop-filter: blur(4px);
}

.modal-container {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: 100%;
  max-width: 860px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 25px -5px rgb(0 0 0 / 0.3), 0 8px 10px -6px rgb(0 0 0 / 0.3);
  overflow: hidden;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 1.1rem;
}

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

.btn-modal-close {
  background: transparent;
  border: none;
  font-size: 1.6rem;
  line-height: 1;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0 4px;
}

.btn-modal-close:hover {
  color: var(--text);
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
  background: #525659;
  display: flex;
  justify-content: center;
}

.preview-page {
  background: #ffffff;
  color: #111827;
  width: 100%;
  max-width: 720px;
  padding: 40px 48px;
  box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.2);
  min-height: 800px;
  border-radius: 4px;
  font-family: 'Times New Roman', Times, serif;
  font-size: 14pt;
  line-height: 1.5;
}

.preview-page .exam-header {
  border-bottom: 2px solid #000;
  margin-bottom: 20px;
  padding-bottom: 12px;
}

.preview-page .exam-header h3 {
  font-size: 16pt;
  margin-bottom: 6px;
}

.preview-page .exam-meta {
  display: flex;
  justify-content: space-between;
  margin-top: 10px;
  font-style: italic;
  font-size: 12pt;
}

.preview-page .exam-code-tag {
  background: #eee;
  color: #000;
  border: 1px solid #333;
  font-weight: bold;
}

.preview-page .exam-q {
  margin-bottom: 16px;
  page-break-inside: avoid;
}

.preview-page .q-title {
  font-weight: bold;
}

.preview-page .q-options {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 20px;
  padding-left: 10px;
  margin-top: 4px;
}

.preview-page .preview-answers-section {
  margin-top: 30px;
  border-top: 1px dashed #666;
  padding-top: 16px;
  page-break-before: auto;
}

.preview-page .preview-answers-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 8px;
  font-size: 11pt;
}

.preview-page .preview-answers-table th,
.preview-page .preview-answers-table td {
  border: 1px solid #000;
  padding: 4px 6px;
  text-align: center;
}

@media print {
  body {
    background: white !important;
    color: black !important;
    padding: 0;
  }
  .input-card, .app-header, .tabs-container, .card-header, .answer-key-section, .modal-overlay {
    display: none !important;
  }
  .main-grid {
    display: block !important;
  }
  .card {
    border: none !important;
    box-shadow: none !important;
    padding: 0 !important;
  }
  .result-view {
    max-height: none !important;
    border: none !important;
    padding: 0 !important;
  }
}
