:root {
  --ink: #1e293b;
  --muted: #64748b;
  --purple: #6a42a2;
  --purple-hover: #563385;
  --purple-light: #f5f0fb;
  --purple-border: #d8c9ec;
  --line: #e2e8f0;
  --card-bg: rgba(255, 255, 255, 0.95);
  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 10px;
}

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

[hidden] {
  display: none !important;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
}

body {
  min-height: 100dvh;
  height: auto;
  overflow-x: hidden;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  color: var(--ink);
  font: 14px/1.5 'DM Sans', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: #0f172a;
  -webkit-font-smoothing: antialiased;
}

/* 4K Crisp Background */
body:before {
  content: '';
  position: fixed;
  inset: 0;
  z-index: -1;
  background: #fbf7f0 url('../background.png') center center / cover no-repeat fixed;
  image-rendering: -webkit-optimize-contrast;
  image-rendering: high-quality;
  transform: translateZ(0);
}

button, input, select {
  font: inherit;
}

button, a {
  -webkit-tap-highlight-color: transparent;
}

button {
  cursor: pointer;
  border: 0;
}

a {
  text-decoration: none;
  color: inherit;
}

button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible {
  outline: 3px solid #8b5cf6;
  outline-offset: 2px;
}

::selection {
  background: #ede9fe;
  color: #3b0764;
}

h1, h2, h3 {
  font-family: 'Manrope', -apple-system, sans-serif;
  margin: 0 0 6px;
  color: #0f172a;
}

/* =========================================================================
   HEADER (Fluid Responsive)
   ========================================================================= */
.header {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  gap: 16px;
  max-width: 1400px;
  width: 100%;
  margin: 0 auto;
  padding: 18px 24px;
  flex-shrink: 0;
}

.header .back {
  position: absolute;
  left: 24px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(226, 232, 240, 0.8);
  font-weight: 600;
  font-size: 13px;
  color: var(--ink);
  transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
  box-shadow: 0 2px 6px rgba(15, 23, 42, 0.04);
}

.header .back:hover {
  background: #ffffff;
  transform: translateX(-2px);
  color: var(--purple);
  box-shadow: 0 4px 12px rgba(106, 66, 162, 0.15);
}

.header-titles {
  text-align: center;
}

h1 {
  font-size: clamp(24px, 3.5vw, 38px);
  font-weight: 800;
  letter-spacing: -0.6px;
  margin: 0;
  line-height: 1.15;
  color: #0f172a;
}

h1 span {
  color: var(--purple);
}

.header p, .header-titles p {
  margin: 4px 0 0;
  font-size: clamp(12px, 2vw, 14px);
  color: #334155;
  font-weight: 600;
}

.header-badge {
  display: inline-block;
  font-size: 0.9em;
  color: #64748b;
  font-weight: 700;
}

/* =========================================================================
   MAIN BUILDER LAYOUT (Desktop & Tablet)
   ========================================================================= */
#builder {
  display: grid;
  grid-template-columns: 350px minmax(0, 1fr);
  gap: 24px;
  max-width: 1400px;
  width: calc(100% - 48px);
  margin: 0 auto 32px;
  flex: 1;
  min-height: 0;
  align-items: start;
}

.panel {
  background: var(--card-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: var(--radius-lg);
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.08), 0 1px 3px rgba(0, 0, 0, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.9);
}

/* =========================================================================
   SETTINGS SIDEBAR (Sticky on Desktop)
   ========================================================================= */
.settings {
  display: flex;
  flex-direction: column;
  padding: 0;
  position: sticky;
  top: 16px;
  align-self: start;
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: #cbd5e1 transparent;
}

.cover {
  width: 100%;
  height: 135px;
  overflow: hidden;
  background: #ffffff;
  position: relative;
  flex-shrink: 0;
  border-top-left-radius: inherit;
  border-top-right-radius: inherit;
  border-bottom: 1px solid var(--line);
}

.cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
  display: block;
}

#details {
  padding: 16px 20px 0;
  flex-shrink: 0;
}

#details h2 {
  font-size: 19px;
  font-weight: 800;
  letter-spacing: -0.4px;
}

.hint {
  font-size: 12.5px;
  color: var(--muted);
  margin: 0 0 14px;
}

label {
  display: block;
  font-size: 11px;
  text-transform: uppercase;
  font-weight: 700;
  letter-spacing: 0.6px;
  color: #475569;
  margin-bottom: 4px;
}

.field-full {
  margin-bottom: 10px;
}

input, select {
  width: 100%;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  border: 1.5px solid var(--line);
  background: #ffffff;
  color: var(--ink);
  transition: all 0.2s ease;
  font-size: 13.5px;
  min-height: 42px;
}

input:focus, select:focus {
  border-color: var(--purple);
  outline: none;
  box-shadow: 0 0 0 3px rgba(106, 66, 162, 0.15);
}

/* Frozen/Locked Class Input */
#className[readonly], input[readonly] {
  background: #f1f5f9 !important;
  color: #475569 !important;
  border-color: #cbd5e1 !important;
  cursor: default !important;
  pointer-events: none !important;
  user-select: none !important;
  font-weight: 600 !important;
}

.field-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 12px;
}

.key-toggle {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  cursor: pointer;
  padding: 10px 12px;
  background: var(--purple-light);
  border: 1.5px solid var(--purple-border);
  border-radius: var(--radius-sm);
  margin: 4px 0 14px;
}

.key-toggle input {
  width: 18px;
  height: 18px;
  accent-color: var(--purple);
  margin-top: 2px;
  flex-shrink: 0;
  cursor: pointer;
}

.key-toggle span {
  font-size: 12px;
  font-weight: 600;
  color: #4c2d77;
  text-transform: none;
  letter-spacing: 0;
  line-height: 1.35;
}

.key-toggle small {
  display: block;
  font-size: 11px;
  font-weight: 400;
  color: #6b4e94;
}

.summary {
  display: flex;
  justify-content: space-around;
  margin: 12px 20px;
  padding: 12px 14px;
  background: #faf5ff;
  border: 1.5px solid #e9d5ff;
  border-radius: var(--radius-sm);
  text-align: center;
}

.summary strong {
  display: block;
  font-size: 22px;
  font-weight: 800;
  color: #581c87;
  line-height: 1.1;
}

.summary span {
  font-size: 11px;
  font-weight: 600;
  color: #7e22ce;
}

.primary {
  background: linear-gradient(135deg, #6a42a2 0%, #4c2d77 100%);
  color: #ffffff;
  font-weight: 700;
  font-size: 14px;
  border-radius: var(--radius-sm);
  box-shadow: 0 4px 14px rgba(106, 66, 162, 0.35);
  transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 46px;
}

.primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 18px rgba(106, 66, 162, 0.45);
  background: linear-gradient(135deg, #744bb0 0%, #543282 100%);
}

.settings > .primary {
  margin: 0 20px 20px;
  padding: 12px 20px;
}

.validation {
  color: #b91c1c;
  font-size: 12px;
  margin: 0 20px 14px;
  font-weight: 600;
}

/* =========================================================================
   QUESTION PANEL (Main Content)
   ========================================================================= */
.question-panel {
  display: flex;
  flex-direction: column;
  padding: 24px 28px;
  min-height: 0;
}

.question-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.question-heading h2 {
  font-size: clamp(20px, 2.5vw, 25px);
  font-weight: 800;
  letter-spacing: -0.4px;
  margin: 0;
}

.heading-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.download-pdf-btn {
  background: linear-gradient(135deg, #7c3aed 0%, #5b21b6 100%);
  color: #ffffff !important;
  font-weight: 700;
  font-size: 13px;
  padding: 9px 16px;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: all 0.2s ease;
  box-shadow: 0 2px 8px rgba(124, 58, 237, 0.3);
  min-height: 40px;
}

.download-pdf-btn:hover {
  background: linear-gradient(135deg, #6d28d9 0%, #4c1d95 100%);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(124, 58, 237, 0.4);
}

.bank-count {
  font-size: 12px;
  font-weight: 700;
  color: #475569;
  background: #f1f5f9;
  border: 1px solid var(--line);
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  white-space: nowrap;
  min-height: 40px;
  display: inline-flex;
  align-items: center;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}

.toolbar input[type="search"] {
  flex: 1;
  min-height: 42px;
}

.selection-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.selection-actions button {
  padding: 9px 14px;
  border-radius: var(--radius-sm);
  border: 1.5px solid var(--line);
  background: #ffffff;
  font-weight: 600;
  font-size: 12.5px;
  color: var(--ink);
  transition: all 0.2s ease;
  min-height: 40px;
}

.selection-actions button:hover {
  border-color: #cbd5e1;
  background: #f8fafc;
}

.results-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 12px;
}

.questions {
  display: flex;
  flex-direction: column;
}

.question {
  background: #ffffff;
  border: 1.5px solid var(--line);
  border-radius: var(--radius-md);
  padding: 16px 18px;
  margin-bottom: 12px;
  transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

.question:hover {
  border-color: #cbd5e1;
  box-shadow: 0 4px 12px rgba(15, 23, 42, 0.04);
}

.question.selected {
  border-color: #8b5cf6;
  background: #faf8ff;
  box-shadow: 0 4px 14px rgba(139, 92, 246, 0.08);
}

.q-row {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  cursor: pointer;
  text-transform: none !important;
  letter-spacing: normal !important;
}

.q-row input[type="checkbox"] {
  width: 20px;
  height: 20px;
  accent-color: var(--purple);
  margin-top: 2px;
  flex-shrink: 0;
  cursor: pointer;
}

.q-row span {
  font-size: clamp(13px, 1.8vw, 14.5px);
  font-weight: 600;
  color: #1e293b;
  line-height: 1.45;
  flex: 1;
  text-transform: none !important;
  letter-spacing: normal !important;
}

.q-index {
  color: #7c3aed;
  font-weight: 800;
}

.points {
  font-size: 11px;
  font-weight: 700;
  color: #6b21a8;
  background: #f3e8ff;
  padding: 3px 8px;
  border-radius: 6px;
  white-space: nowrap;
  flex-shrink: 0;
}

.options {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 14px;
  margin-top: 12px;
  padding-left: 32px;
}

.options span {
  display: flex;
  align-items: center;
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-size: 13px;
  color: #334155;
  min-height: 40px;
}

.question.selected .options span {
  background: #ffffff;
  border-color: #ede9fe;
}

.options span b {
  width: 22px;
  height: 22px;
  min-width: 22px;
  border-radius: 6px;
  background: #ede9fe;
  color: #6b21a8;
  font-size: 11px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-right: 10px;
}

.list-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.list-footer span {
  font-size: 13px;
  color: var(--muted);
  font-style: italic;
}

.bottom-preview-btn {
  padding: 12px 22px;
  flex-shrink: 0;
}

/* =========================================================================
   PREVIEW SCREEN & PRINTABLE PAPER
   ========================================================================= */
#preview {
  max-width: 900px;
  width: calc(100% - 32px);
  margin: 0 auto 32px;
}

.preview-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 20px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(12px);
  border-radius: var(--radius-md);
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.9);
  margin-bottom: 20px;
}

.preview-toolbar .back {
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  background: #f1f5f9;
  font-weight: 600;
  font-size: 13px;
  color: var(--ink);
  transition: all 0.2s ease;
}

.preview-toolbar .back:hover {
  background: #e2e8f0;
}

.preview-toolbar > div {
  text-align: center;
}

.preview-toolbar strong {
  display: block;
  font-size: 15px;
  color: var(--ink);
}

.preview-toolbar span {
  font-size: 12px;
  color: var(--muted);
}

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

.preview-actions .primary {
  padding: 9px 18px;
  min-height: 40px;
}

#paper {
  background: #ffffff;
  border-radius: var(--radius-md);
  padding: 36px 42px;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.08);
  border: 1px solid var(--line);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: #1e293b;
}

.paperhead {
  text-align: center;
  border-bottom: 2.5px solid #5b21b6;
  padding-bottom: 8px;
  margin-bottom: 12px;
}

.paperhead h1 {
  font-size: clamp(18px, 3.2vw, 24px);
  font-weight: 800;
  color: #5b21b6;
  margin-bottom: 3px;
}

.paperhead h2 {
  font-size: clamp(12.5px, 2.2vw, 15px);
  font-weight: 600;
  color: #475569;
  margin-bottom: 4px;
}

.paper-meta-sub {
  font-size: 12px;
  color: #64748b;
  font-weight: 500;
}

.meta {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  padding: 8px 0;
  border-bottom: 1px dashed #cbd5e1;
  margin-bottom: 12px;
}

.instructions {
  font-size: 11.5px;
  color: #475569;
  margin-bottom: 14px;
  font-style: italic;
}

.paper-item {
  margin-bottom: 12px;
  page-break-inside: avoid;
}

.paper-q-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 12.5px;
  font-weight: 600;
  color: #0f172a;
  margin-bottom: 4px;
}

.paper-q-mark {
  color: #6b21a8;
  font-size: 11px;
  font-weight: 600;
}

.paper-q-options {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 16px;
  padding-left: 14px;
  font-size: 11.5px;
  color: #334155;
}

.paper-q-options b {
  color: #6b21a8;
}

.answer-key-print {
  page-break-before: always;
  margin-top: 24px;
  padding-top: 16px;
  border-top: 2px dashed #94a3b8;
}

.answer-key-print h3 {
  color: #5b21b6;
  font-size: 14px;
  margin-bottom: 10px;
}

.answer-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  font-size: 11px;
}

/* =========================================================================
   RESPONSIVE DESIGN: TABLETS (<= 992px)
   ========================================================================= */
@media (max-width: 992px) {
  #builder {
    grid-template-columns: 1fr;
    width: calc(100% - 24px);
    max-width: 720px;
    gap: 18px;
  }

  .settings {
    position: static;
    max-height: none;
    overflow: visible;
  }

  .cover {
    height: 130px;
  }

  .header {
    padding: 14px 18px;
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }

  .header .back {
    position: static !important;
    align-self: flex-start;
  }

  .header-titles {
    width: 100%;
    text-align: center;
  }
}

/* =========================================================================
   RESPONSIVE DESIGN: MOBILE (<= 640px)
   ========================================================================= */
@media (max-width: 640px) {
  .header {
    flex-direction: column;
    align-items: stretch;
    padding: 10px 12px 12px;
    gap: 6px;
  }

  .header .back {
    position: static !important;
    align-self: flex-start;
    font-size: 11.5px;
    padding: 5px 12px;
    margin-bottom: 2px;
  }

  .header-titles {
    width: 100%;
    text-align: center;
  }

  .header-titles h1, h1 {
    font-size: 23px;
  }

  .header-titles p, .header p {
    font-size: 12px;
    word-break: normal;
    overflow-wrap: break-word;
  }

  .header-badge {
    display: block;
    margin-top: 2px;
  }

  #builder {
    width: calc(100% - 16px);
    gap: 14px;
    margin-bottom: 20px;
  }

  .panel {
    border-radius: 16px;
  }

  .cover {
    height: 110px;
  }

  #details {
    padding: 14px 16px 0;
  }

  .field-grid {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .summary {
    margin: 10px 16px;
    padding: 10px 12px;
  }

  .settings > .primary {
    margin: 0 16px 16px;
    width: calc(100% - 32px);
  }

  .question-panel {
    padding: 18px 14px;
  }

  .question-heading {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }

  .heading-actions {
    width: 100%;
    display: flex;
    gap: 8px;
  }

  .download-pdf-btn {
    flex: 1;
    justify-content: center;
    font-size: 12px;
    min-height: 42px;
  }

  .bank-count {
    min-height: 42px;
    padding: 0 12px;
  }

  .toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }

  .toolbar input[type="search"] {
    width: 100%;
  }

  .selection-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    width: 100%;
  }

  .selection-actions button {
    width: 100%;
    justify-content: center;
    min-height: 40px;
  }

  .question {
    padding: 14px 12px;
    border-radius: 12px;
  }

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

  .options span {
    min-height: 42px;
    padding: 8px 10px;
    font-size: 12.5px;
  }

  .list-footer {
    flex-direction: column;
    text-align: center;
    gap: 10px;
  }

  .bottom-preview-btn {
    width: 100%;
    min-height: 46px;
  }

  /* Preview view mobile */
  #preview {
    width: calc(100% - 16px);
  }

  .preview-toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    padding: 12px;
  }

  .preview-actions {
    width: 100%;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .preview-actions .download-pdf-btn, .preview-actions .primary {
    justify-content: center;
    min-height: 42px;
    width: 100%;
  }

  #paper {
    padding: 18px 14px;
    border-radius: 12px;
  }

  .meta {
    flex-direction: column;
    gap: 4px;
  }

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

/* =========================================================================
   RESPONSIVE DESIGN: ULTRA-COMPACT (<= 360px)
   ========================================================================= */
@media (max-width: 360px) {
  #builder {
    width: calc(100% - 8px);
    gap: 10px;
  }

  .question-panel {
    padding: 14px 10px;
  }

  .question {
    padding: 12px 10px;
  }

  h1 {
    font-size: 21px;
  }

  .cover {
    height: 95px;
  }
}

/* =========================================================================
   PRINT STYLES (Strict Single Page)
   ========================================================================= */
@media print {
  @page {
    size: A4 portrait;
    margin: 8mm 12mm;
  }

  html, body {
    background: #ffffff !important;
    height: auto !important;
    color: #1e293b !important;
  }

  body:before, .header, #builder, .preview-toolbar {
    display: none !important;
  }

  #preview {
    display: block !important;
    padding: 0 !important;
    margin: 0 !important;
    width: 100% !important;
    max-width: 100% !important;
  }

  #paper {
    padding: 0 !important;
    border: none !important;
    box-shadow: none !important;
    border-radius: 0 !important;
  }

  .paperhead {
    border-bottom: 2px solid #5b21b6;
    padding-bottom: 5px;
    margin-bottom: 6px;
  }

  .paperhead h1 {
    font-size: 18px;
    color: #5b21b6 !important;
  }

  .paperhead h2 {
    font-size: 13px;
  }

  .paper-meta-sub {
    font-size: 11px;
  }

  .meta {
    font-size: 11px;
    padding: 4px 0;
    margin-bottom: 6px;
  }

  .instructions {
    font-size: 10.5px;
    margin-bottom: 6px;
  }

  .paper-item {
    margin-bottom: 7px;
  }

  .paper-q-head {
    font-size: 11.5px;
    margin-bottom: 2px;
  }

  .paper-q-options {
    gap: 2px 12px;
    font-size: 11px;
  }
}
