/* Loading state */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  font-size: 18px;
  color: var(--muted);
}
.loading-overlay.hidden { display: none; }

/* ===== CSS CUSTOM PROPERTIES ===== */
:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #1e293b;
  --muted: #64748b;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.10);
  --radius: 8px;
  --radius-sm: 4px;

  /* Section colors */
  --color-0: #3b82f6;
  --color-1: #f59e0b;
  --color-2: #22c55e;
  --color-3: #8b5cf6;
  --color-4: #ef4444;

  /* Active color (updated by JS) */
  --active-color: #3b82f6;
}

/* ===== RESET ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.5; }

/* ===== APP LAYOUT ===== */
.app {
  display: grid;
  grid-template-columns: 280px 1fr 260px;
  grid-template-rows: 100vh;
  height: 100vh;
  overflow: hidden;
}

/* ===== SIDEBAR ===== */
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sidebar-summary {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 12px;
}

.section-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: var(--radius);
  border: 1.5px solid var(--border);
  cursor: pointer;
  transition: all 0.15s;
  background: var(--surface);
}
.section-chip:hover { background: color-mix(in srgb, var(--active-color) 6%, white); }
.section-chip.active {
  border-color: var(--active-color);
  background: color-mix(in srgb, var(--active-color) 10%, white);
}

.section-chip-number {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
  flex-shrink: 0;
}
.section-chip.active .section-chip-number {
  background: var(--active-color);
  color: white;
}

.section-chip-name { font-weight: 500; font-size: 13px; flex: 1; }
.section-chip-meta { font-size: 11px; color: var(--muted); }

/* Section blocks */
.section-block { border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  cursor: pointer;
  font-weight: 500;
  font-size: 13px;
  background: var(--bg);
  transition: background 0.15s;
}
.section-title:hover { background: color-mix(in srgb, var(--active-color) 6%, white); }
.section-title.active { color: var(--active-color); }

.question-list { display: none; }
.question-list.open { display: block; }

.question-item {
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 10px 12px;
  cursor: pointer;
  border-top: 1px solid var(--border);
  transition: background 0.15s;
}
.question-item:hover { background: color-mix(in srgb, var(--active-color) 6%, white); }
.question-item.active { background: color-mix(in srgb, var(--active-color) 12%, white); border-left: 3px solid var(--active-color); }

.question-code { font-size: 11px; color: var(--muted); font-weight: 600; }
.question-name { font-size: 13px; font-weight: 500; }
.meta-row { display: flex; gap: 4px; flex-wrap: wrap; }

/* ===== MAIN ===== */
.main {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Toolbar */
.toolbar {
  background: linear-gradient(180deg, color-mix(in srgb, var(--active-color) 10%, white) 0%, color-mix(in srgb, var(--active-color) 4%, white) 100%);
  border-bottom: 1px solid var(--border);
  padding: 12px 20px;
  flex-shrink: 0;
}
.toolbar-inner {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}
.toolbar-title { flex: 1; }
.question-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--active-color);
  line-height: 1.3;
}
.question-subtitle { font-size: 13px; color: var(--muted); margin-top: 2px; }
.toolbar-actions { display: flex; gap: 8px; align-items: center; flex-shrink: 0; }

/* Scrollable content area */
.main > *:not(.toolbar) {
  overflow-y: auto;
  padding: 20px;
}

/* ===== INTAKE CARD ===== */
.intake-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

.card-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: color-mix(in srgb, var(--active-color) 6%, white);
}
.card-header h2 { font-size: 15px; font-weight: 600; }
.card-body { padding: 20px; display: flex; flex-direction: column; gap: 16px; }

/* ===== QUESTION CONFIG SHELL ===== */
.question-config-shell {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ===== CRITERIA SHELL ===== */
.criteria-shell {
  background: linear-gradient(180deg, color-mix(in srgb, var(--active-color) 10%, white) 0%, color-mix(in srgb, var(--active-color) 4%, white) 100%);
  border: 2px solid var(--active-color);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

.question-config-card {
  background: linear-gradient(180deg, color-mix(in srgb, var(--active-color) 10%, white) 0%, color-mix(in srgb, var(--active-color) 4%, white) 100%);
  border: 2px solid var(--active-color);
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--active-color) 6%, white);
}
.section-header h3 { font-size: 14px; font-weight: 600; }

/* Criteria items */
.criterion-item {
  border-bottom: 1px solid var(--border);
  padding: 14px 16px;
}
.criterion-item:last-child { border-bottom: none; }
.criterion-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.criterion-title { font-weight: 500; font-size: 13px; }
.criterion-badge {
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--active-color) 14%, white);
  color: var(--active-color);
  font-weight: 600;
}
.criterion-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 8px; }
.criterion-field label { display: block; font-size: 11px; color: var(--muted); margin-bottom: 3px; }
.criterion-field textarea, .criterion-field input { width: 100%; font-size: 13px; }

/* Scoring panel */
.score-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
}
.score-row:last-child { border-bottom: none; }
.score-label { font-size: 13px; flex: 1; }
.score-input {
  width: 60px;
  text-align: center;
  font-size: 14px;
  font-weight: 600;
}
.score-max { font-size: 12px; color: var(--muted); margin-left: 4px; }
.total-score-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: color-mix(in srgb, var(--active-color) 10%, white);
  font-weight: 600;
}
.total-score-value { font-size: 20px; color: var(--active-color); }

/* ===== INSPECTOR ===== */
.inspector {
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.inspector-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}
.inspector-header h3 { font-size: 14px; font-weight: 600; }

#versionsList { overflow-y: auto; flex: 1; }

.version-item {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.15s;
}
.version-item:hover { background: var(--bg); }
.version-item-label { font-size: 13px; font-weight: 500; }
.version-item-date { font-size: 11px; color: var(--muted); margin-top: 2px; }

/* ===== FORMS ===== */
.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-group label { font-size: 12px; font-weight: 500; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }

.form-control {
  padding: 8px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  color: var(--text);
  background: var(--surface);
  transition: border-color 0.15s;
  width: 100%;
}
.form-control:focus { outline: none; border-color: var(--active-color); }
.form-control::placeholder { color: var(--muted); }

textarea.form-control { resize: vertical; min-height: 72px; }

/* ===== BUTTONS ===== */
.btn {
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border: 1.5px solid transparent;
  transition: all 0.15s;
  white-space: nowrap;
}
.btn-primary { background: var(--active-color); color: white; border-color: var(--active-color); }
.btn-primary:hover { opacity: 0.88; }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: var(--bg); }
.btn-small { padding: 4px 10px; font-size: 12px; }
.btn-danger { background: #fee2e2; color: #dc2626; border-color: #fca5a5; }
.btn-danger:hover { background: #fca5a5; }

/* ===== PILLS ===== */
.pill {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 10px;
  font-size: 11px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--muted);
}

/* ===== FORM CHIP ===== */
.form-chip {
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--active-color) 14%, white);
  color: var(--active-color);
  font-weight: 500;
}

/* ===== FIELD WRAPPER ===== */
.field { display: flex; flex-direction: column; gap: 4px; }
.field label { font-size: 11px; color: var(--muted); font-weight: 500; }

/* ===== SAVE STATUS ===== */
#saveStatus { transition: color 0.3s; }
#saveStatus.status-dirty { color: #f59e0b; }
#saveStatus.status-saved { color: #22c55e; }
#saveStatus.status-autosaved { color: var(--muted); }
#saveStatus.status-error { color: #ef4444; }

/* ===== VALIDATION WARNING ===== */
.validation-warning {
  font-size: 11px;
  color: #f59e0b;
  padding: 4px 8px;
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-radius: var(--radius-sm);
  margin-top: 4px;
  grid-column: 1 / -1;
}

/* ===== QUESTION CONFIG CONTENT ===== */
.question-config-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.question-config-card .card-header { border-left: 4px solid var(--active-color); }
.config-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; padding: 16px; }
.rules-list { padding: 0 16px 16px; }
.rule-item {
  display: flex;
  gap: 8px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.rule-item:last-child { border-bottom: none; }

/* ===== RESPONSIVE ===== */
@media (max-width: 1200px) {
  .app { grid-template-columns: 280px 1fr; grid-template-rows: 100vh auto; }
  .inspector { grid-column: 1 / -1; height: auto; border-left: none; border-top: 1px solid var(--border); }
}

@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto; height: auto; overflow: auto; }
  .sidebar { border-right: none; border-bottom: 1px solid var(--border); max-height: 50vh; }
  .main > *:not(.toolbar) { overflow-y: visible; }
  .inspector { border-left: none; border-top: 1px solid var(--border); height: auto; }
  .criterion-fields { grid-template-columns: 1fr; }
  .config-grid { grid-template-columns: 1fr; }
  .sidebar-summary { display: grid; grid-template-columns: repeat(3, 1fr); }
  .section-chip { padding: 8px; }
  .section-chip-name { font-size: 11px; }
  .toolbar { padding: 12px; }
  .btn { min-height: 44px; }
}

@media (max-width: 480px) {
  .sidebar-summary { grid-template-columns: repeat(2, 1fr); }
  .toolbar-actions { flex-wrap: wrap; }
  .main > *:not(.toolbar) { padding: 12px; }
}
