:root {
  --bg: #f4f6fb;
  --panel: #ffffff;
  --ink: #1c2333;
  --muted: #64708a;
  --line: #e3e8f2;
  --accent: #3b6ef5;
  --accent-ink: #ffffff;
  --accent-soft: #eaf0ff;
  --success: #1f9d68;
  --danger: #d64545;
  --warn: #c98a17;
  --radius: 14px;
  --shadow: 0 10px 30px rgba(28, 35, 51, 0.08);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.app-header {
  display: flex;
  align-items: center;
  padding: 18px 28px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { font-size: 26px; }
.brand h1 { margin: 0; font-size: 19px; letter-spacing: -0.01em; }
.tagline { margin: 0; font-size: 12.5px; color: var(--muted); }

.layout {
  display: grid;
  grid-template-columns: 360px 1fr;
  gap: 20px;
  padding: 20px 28px;
  max-width: 1280px;
  margin: 0 auto;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.controls { padding: 20px; height: fit-content; }

.control-group { margin-bottom: 22px; }
.control-group:last-child { margin-bottom: 0; }
.control-group h2 { margin: 0 0 4px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.hint { margin: 0 0 10px; font-size: 12.5px; color: var(--muted); }

/* Topic list */
.topic-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 320px;
  overflow-y: auto;
  padding-right: 4px;
}
.topic-skeleton { color: var(--muted); font-size: 13px; padding: 8px 0; }

.topic-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
  transition: border-color 0.12s ease, background 0.12s ease;
  user-select: none;
}
.topic-item:hover { border-color: var(--accent); }
.topic-item.checked { background: var(--accent-soft); border-color: var(--accent); }
.topic-item input { accent-color: var(--accent); width: 16px; height: 16px; cursor: pointer; }
.topic-item .topic-label { font-size: 13.5px; line-height: 1.3; }

/* Mode */
.mode-group { display: flex; flex-direction: column; gap: 8px; }
.mode-option {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
  font-size: 13.5px;
  transition: border-color 0.12s ease, background 0.12s ease;
}
.mode-option:hover { border-color: var(--accent); }
.mode-option input { accent-color: var(--accent); margin-top: 2px; }
.mode-option span strong { display: block; margin-bottom: 1px; }

/* Count */
.count-row { display: flex; align-items: center; gap: 10px; }
#question-count {
  width: 90px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 9px;
  font-size: 14px;
  color: var(--ink);
}
.match-count { margin: 10px 0 0; font-size: 13px; color: var(--muted); }
.match-count b { color: var(--ink); }
.match-count.shortage { color: var(--warn); }
.match-count.none { color: var(--danger); }

/* Buttons */
.actions { display: flex; flex-direction: column; gap: 10px; }
.btn {
  border: none;
  border-radius: 10px;
  padding: 12px 18px;
  font-size: 14.5px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.12s ease, transform 0.05s ease, opacity 0.12s ease;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover:not(:disabled) { background: #2c5be0; }

/* Preview */
.preview { display: flex; flex-direction: column; min-height: 70vh; }
.preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
}
.preview-header h2 { margin: 0; font-size: 15px; }

.preview-body { padding: 24px; flex: 1; }

/* States */
.state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 10px;
  padding: 48px 24px;
  color: var(--muted);
}
.state-icon { font-size: 40px; }
.state p { margin: 0; font-size: 14px; max-width: 340px; line-height: 1.5; }
.state.error .state-icon { color: var(--danger); }
.state.error p { color: var(--danger); }

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--accent-soft);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Paper preview (document-style) */
.paper {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 32px 36px;
  box-shadow: 0 2px 8px rgba(28,35,51,0.05);
}
.paper-title { text-align: center; margin-bottom: 4px; }
.paper-title h3 { margin: 0; font-size: 20px; letter-spacing: -0.01em; }
.paper-meta { text-align: center; color: var(--muted); font-size: 12.5px; margin-bottom: 4px; }
.paper-topics { text-align: center; font-size: 12.5px; color: var(--muted); margin-bottom: 18px; }

.paper-question { margin-bottom: 18px; }
.pq-head { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; margin-bottom: 3px; }
.pq-num { font-weight: 700; font-size: 14.5px; }
.pq-ref { font-size: 12px; color: var(--muted); }
.pq-tagcount {
  font-size: 11px;
  color: var(--muted);
  background: #f0f2f8;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0 8px;
  line-height: 18px;
}
.pq-tags { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.pq-tags .tag-chip {
  display: inline-block;
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: 999px;
  padding: 1px 8px;
  margin: 0 4px 2px 0;
  font-size: 11px;
}
.pq-lines {
  height: 84px;
  background: repeating-linear-gradient(
    to bottom,
    transparent 0,
    transparent 20px,
    var(--line) 20px,
    var(--line) 21px
  );
}

.pq-image {
  display: block;
  width: 100%;
  max-width: 720px;
  height: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  margin: 6px 0 4px;
  background: #fff;
}

.notice {
  border: 1px solid var(--warn);
  background: #fff7e8;
  color: #8a5b08;
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 13px;
  margin-bottom: 16px;
}

.app-footer {
  text-align: center;
  padding: 18px;
  color: var(--muted);
  font-size: 12px;
}
.app-footer code { background: var(--accent-soft); padding: 1px 5px; border-radius: 4px; }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; padding: 14px; }
  .preview { min-height: 40vh; }
  .paper { padding: 20px 18px; }
}
