:root {
  --market-red: #b40000;
  --market-red-dark: #8c0000;
  --border: #c9c9c9;
  --panel: #f4f4f4;
  --text: #171717;
  --muted: #606060;
  color-scheme: light;
  font-family: "Segoe UI", Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body { margin: 0; background: #efefef; color: var(--text); }
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
button, input, textarea, select { font: inherit; }
button {
  padding: 6px 12px;
  border: 1px solid #b8b8b8;
  border-radius: 3px;
  color: #111;
  background: #fafafa;
  cursor: pointer;
}
button:hover:not(:disabled) { background: #f0f0f0; }
button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible {
  outline: 2px solid #1675c1;
  outline-offset: 1px;
}
button:disabled { opacity: .52; cursor: not-allowed; }
button.primary { font-weight: 700; }
button.primary:hover:not(:disabled) { border-color: var(--market-red); }

.app-shell { min-height: 100vh; display: flex; flex-direction: column; padding: 18px 28px 12px; }
.app-header { display: flex; align-items: center; justify-content: space-between; gap: 24px; margin-bottom: 14px; }
.app-header h1 { margin: 0; color: var(--market-red); font-size: 32px; line-height: 1; }
.header-actions { display: flex; align-items: center; gap: 12px; }
.api-status.ok { color: #235f27; }
.api-status.error { color: var(--market-red); }

.tabs { display: flex; align-items: end; }
.tab { border-radius: 0; border-bottom: 0; font-size: 16px; }
.tab.active { background: white; position: relative; z-index: 2; }
.panel {
  display: none;
  flex: 1;
  border: 1px solid var(--border);
  background: var(--panel);
  padding: 26px 28px;
  min-height: 690px;
}
.panel.active { display: block; }

.form-grid { display: grid; grid-template-columns: 190px minmax(420px, 1fr); gap: 12px 18px; align-items: start; }
.form-grid > label { padding-top: 6px; font-size: 16px; }
input[type="text"], input[type="password"], input[type="number"], textarea, select {
  width: 100%;
  border: 1px solid #aaa;
  background: white;
  padding: 7px 8px;
}
textarea { resize: vertical; min-height: 80px; }
.settings-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.settings-row label { white-space: nowrap; }
.settings-row input[type="number"] { width: 76px; margin-right: 20px; }
.settings-row select { width: 165px; }
.settings-row .check { display: flex; align-items: center; gap: 7px; margin-left: 22px; }
.settings-row .primary { margin-left: auto; }
.check input { width: auto; }

.preview-toolbar, .knowledge-actions, .quality-row, dialog footer {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.preview-toolbar { margin-bottom: 10px; }
.toolbar-spacer { flex: 1; }
.preview-layout { display: grid; grid-template-columns: minmax(340px, 2fr) minmax(440px, 3fr); gap: 12px; height: 590px; }
.question-list, .reading-view, .knowledge-list {
  background: white;
  border: 1px solid var(--border);
  padding: 14px;
  overflow: auto;
}
.question-section { margin-bottom: 8px; }
.section-button, .question-button {
  display: block;
  width: 100%;
  border: 0;
  border-radius: 0;
  text-align: left;
  background: transparent;
}
.section-button { padding: 7px 6px; color: var(--market-red); font-weight: 700; }
.question-button { padding: 6px 6px 6px 24px; line-height: 1.3; }
.question-button:hover, .section-button:hover { background: #f4f4f4 !important; }
.question-button.selected, .section-button.selected { background: #f4dede !important; }
.question-type { display: block; margin: 2px 0 0; color: var(--muted); font-size: 12px; }
.reading-view h2, #knowledge h2, dialog h2 { margin-top: 0; color: var(--market-red); }
.reading-view > h2 { font-size: 24px; margin-bottom: 5px; }
.reading-view .subtitle { margin-top: 0; color: var(--muted); }
.reading-view h3 { color: var(--market-red); margin: 25px 0 6px; font-size: 19px; }
.reading-view .section-intro { color: var(--muted); font-style: italic; }
.preview-question { padding: 10px 0 13px; border-bottom: 1px solid #ededed; }
.preview-question:last-of-type { border-bottom: 0; }
.question-heading { margin: 0 0 6px; font-weight: 700; }
.question-code { color: var(--market-red); margin-right: 8px; }
.instruction, .filter-note { margin: 4px 0; color: var(--market-red-dark); font-size: 13px; text-transform: uppercase; }
.help-text { margin: 4px 0; color: var(--muted); font-style: italic; }
.answer-list { list-style: none; padding: 0 0 0 14px; margin: 7px 0 0; }
.answer-list li { margin: 3px 0; }
.answer-code { display: inline-block; min-width: 32px; color: var(--market-red); font-weight: 700; }
.matrix-rows { margin: 8px 0 0 32px; }
.closing { color: var(--market-red); text-align: center; font-weight: 700; margin-top: 28px; }
.quality-row { justify-content: space-between; padding-top: 10px; }
.empty { color: var(--muted); }

#knowledge h2 { margin-bottom: 6px; }
.knowledge-actions { margin: 16px 0 10px; }
.knowledge-actions span { margin-left: auto; color: var(--muted); font-size: 13px; }
.knowledge-upload-result { margin: 0 0 10px; color: #235f27; font-size: 13px; white-space: pre-line; }
.knowledge-upload-result.partial, .knowledge-upload-result.error { color: var(--market-red-dark); }
.knowledge-list { height: 520px; }
.knowledge-item, .project-item {
  display: block;
  width: 100%;
  padding: 10px;
  border: 0;
  border-bottom: 1px solid #e5e5e5;
  border-radius: 0;
  text-align: left;
  background: white;
}
.knowledge-item.selected, .project-item.selected { background: #f4dede; }
.knowledge-meta { display: block; margin-top: 3px; color: var(--muted); font-size: 12px; }

.statusbar { display: flex; align-items: center; justify-content: space-between; gap: 20px; min-height: 38px; padding-top: 8px; }
.statusbar progress { width: 320px; accent-color: var(--market-red); }
.statusbar .error { color: var(--market-red); font-weight: 600; }

dialog {
  width: min(680px, calc(100vw - 32px));
  max-height: calc(100vh - 40px);
  padding: 0;
  border: 1px solid #9d9d9d;
  border-radius: 5px;
  box-shadow: 0 12px 48px rgb(0 0 0 / 25%);
}
dialog::backdrop { background: rgb(0 0 0 / 28%); }
.dialog-form { padding: 18px; overflow: auto; }
.dialog-form > header { display: flex; align-items: start; justify-content: space-between; gap: 20px; }
.dialog-form > header h2 { margin-bottom: 18px; }
.dialog-form > label { display: block; margin: 10px 0 4px; }
.dialog-form > footer { justify-content: flex-end; margin-top: 18px; }
.icon-close { border: 0; background: transparent; font-size: 27px; padding: 0 5px; line-height: 1; }
.hint { color: var(--muted); font-size: 12px; margin: 5px 0; }
.dialog-message { min-height: 20px; white-space: pre-line; color: var(--market-red); }
.dialog-message.success { color: #235f27; }
.project-list { height: 330px; border: 1px solid var(--border); overflow: auto; }
.question-dialog-form { width: 100%; }
#question-dialog { width: min(780px, calc(100vw - 32px)); }
.dialog-grid { display: grid; grid-template-columns: 130px 1fr; gap: 9px 12px; align-items: start; }
.dialog-grid > label { padding-top: 7px; }
.checkbox-row { display: flex; gap: 22px; flex-wrap: wrap; }
.quality-dialog-form { min-height: 300px; }
.quality-problems { max-height: 430px; overflow: auto; border: 1px solid var(--border); }
.quality-problem { padding: 10px; border-bottom: 1px solid #e6e6e6; }
.quality-problem:last-child { border-bottom: 0; }
.quality-problem strong { color: var(--market-red); }
.quality-problem p { margin: 4px 0 0; }

.toast {
  position: fixed;
  right: 24px;
  bottom: 22px;
  z-index: 20;
  max-width: min(500px, calc(100vw - 48px));
  padding: 11px 15px;
  border-radius: 4px;
  background: #222;
  color: white;
  opacity: 0;
  pointer-events: none;
  transform: translateY(10px);
  transition: .16s ease;
  white-space: pre-line;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { background: var(--market-red-dark); }

.login-page { display: grid; min-height: 100vh; place-items: center; padding: 20px; }
.login-card { width: min(440px, 100%); padding: 28px; border: 1px solid var(--border); background: white; }
.login-card h1 { margin-top: 0; color: var(--market-red); }
.login-card label { display: block; margin: 18px 0 5px; }
.login-card button { display: block; margin: 18px 0 0 auto; }
.login-error { color: var(--market-red); }

@media (max-width: 1000px) {
  .app-shell { padding: 14px; }
  .app-header { align-items: flex-start; flex-direction: column; }
  .header-actions { width: 100%; flex-wrap: wrap; }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid > div:empty { display: none; }
  .preview-layout { grid-template-columns: 1fr; height: auto; }
  .question-list { max-height: 360px; }
  .reading-view { min-height: 520px; }
  .panel { padding: 18px; min-height: 0; }
  .settings-row .check { margin-left: 0; }
  .settings-row .primary { margin-left: 0; }
  .knowledge-actions span { width: 100%; margin-left: 0; }
}

@media (max-width: 620px) {
  .app-header h1 { font-size: 27px; }
  .tabs { overflow-x: auto; }
  .tab { white-space: nowrap; font-size: 14px; }
  .panel { padding: 14px; }
  .dialog-grid { grid-template-columns: 1fr; }
  .dialog-grid > label { padding-top: 2px; }
  .dialog-grid > span { display: none; }
  .statusbar { align-items: stretch; flex-direction: column; padding-top: 10px; }
  .statusbar progress { width: 100%; }
}
