:root {
  --ink: #172034;
  --muted: #667085;
  --line: #d9dee8;
  --line-strong: #c4cad6;
  --paper: #ffffff;
  --canvas: #f3f5f8;
  --blue: #1261a8;
  --blue-dark: #0a467c;
  --blue-soft: #eaf4ff;
  --yellow: #ffc220;
  --red: #b42318;
  --green: #087a55;
  --shadow: 0 18px 42px rgba(30, 42, 66, 0.09);
  font-family: Inter, "Segoe UI", "Microsoft YaHei UI", Arial, sans-serif;
  color: var(--ink);
  background: var(--canvas);
  font-synthesis: none;
}

* { box-sizing: border-box; }

html { min-width: 320px; background: var(--canvas); }

body {
  min-height: 100vh;
  margin: 0;
  background:
    linear-gradient(90deg, rgba(18, 97, 168, 0.035) 1px, transparent 1px) 0 0 / 32px 32px,
    var(--canvas);
}

button, input, textarea { font: inherit; }

button {
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
  transition: border-color 140ms ease, background 140ms ease, color 140ms ease, transform 140ms ease;
}

button:hover { border-color: var(--blue); color: var(--blue-dark); }
button:active { transform: translateY(1px); }
button:focus-visible, input:focus-visible, textarea:focus-visible { outline: 3px solid rgba(18, 97, 168, 0.22); outline-offset: 1px; }
button:disabled { cursor: not-allowed; opacity: 0.38; }

.skip-link {
  position: fixed;
  top: 8px;
  left: 8px;
  z-index: 100;
  padding: 10px 14px;
  background: var(--ink);
  color: #fff;
  transform: translateY(-150%);
}
.skip-link:focus { transform: translateY(0); }

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 88px;
  padding: 18px clamp(20px, 3vw, 48px);
  background: #0e1b31;
  color: #fff;
  border-bottom: 4px solid var(--yellow);
}

.brand-lockup { display: flex; align-items: center; gap: 14px; }
.brand-mark { display: grid; place-items: center; width: 38px; height: 38px; color: var(--yellow); font-size: 30px; }
.step-label, .field-kicker { margin: 0 0 5px; font-size: 11px; font-weight: 800; letter-spacing: 0.14em; text-transform: uppercase; }
.app-header h1 { margin: 0; font-size: clamp(20px, 2.2vw, 29px); line-height: 1.1; letter-spacing: -0.025em; }

.save-indicator { display: flex; align-items: center; gap: 8px; color: #c8d5e4; font-size: 13px; white-space: nowrap; }
.save-indicator span { width: 8px; height: 8px; background: #45d49f; border-radius: 50%; box-shadow: 0 0 0 4px rgba(69, 212, 159, 0.13); }
.save-indicator.is-saving span { background: var(--yellow); box-shadow: 0 0 0 4px rgba(255, 194, 32, 0.13); }

main { width: min(1680px, 100%); margin: 0 auto; padding: 20px clamp(16px, 2.8vw, 42px) 42px; }

.toolbar {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 12px;
  padding: 14px 16px;
  background: rgba(255, 255, 255, 0.95);
  border: 1px solid var(--line);
  box-shadow: 0 6px 18px rgba(30, 42, 66, 0.04);
}

.project-name-field { display: grid; gap: 6px; min-width: min(350px, 100%); }
.project-name-field span { color: var(--muted); font-size: 12px; font-weight: 700; }
.project-name-field input { width: 100%; padding: 9px 10px; border: 0; border-bottom: 2px solid var(--line-strong); background: transparent; color: var(--ink); font-weight: 700; }
.project-name-field input:focus { border-color: var(--blue); outline: 0; }

.toolbar-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; }
.toolbar-actions button { min-height: 38px; padding: 8px 12px; font-size: 13px; font-weight: 650; }
.toolbar-actions .icon-button { width: 38px; padding: 0; font-size: 19px; }
.toolbar-actions .ai-config-button { border-color: #84acd2; background: var(--blue-soft); color: var(--blue-dark); }
.api-status { align-self: center; padding: 0 4px; color: var(--muted); font-size: 11px; font-weight: 700; }
.api-status.is-ready { color: var(--green); }
.danger-quiet { color: var(--red); }

.workspace {
  display: grid;
  grid-template-columns: minmax(320px, var(--library-width, 43%)) 9px minmax(480px, 1fr);
  min-height: 820px;
  height: calc(100vh - 190px);
  max-height: 1280px;
  overflow: hidden;
  border: 1px solid var(--line-strong);
  background: var(--paper);
  box-shadow: var(--shadow);
}

.library-panel, .outline-panel { min-width: 0; overflow-y: auto; overscroll-behavior: contain; scrollbar-gutter: stable; }
.library-panel { background: #f8fafc; }
.outline-panel { background: var(--paper); }

.workspace-splitter {
  position: relative;
  z-index: 8;
  width: 9px;
  cursor: col-resize;
  touch-action: none;
  background: #e8ebf1;
  border-right: 1px solid var(--line-strong);
  border-left: 1px solid var(--line-strong);
}
.workspace-splitter span {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 3px;
  height: 54px;
  transform: translate(-50%, -50%);
  background: repeating-linear-gradient(to bottom, #8f99aa 0 3px, transparent 3px 7px);
}
.workspace-splitter:hover, .workspace-splitter:focus-visible { background: #dbeafb; outline: 0; }
.workspace-splitter:hover span, .workspace-splitter:focus-visible span { background: repeating-linear-gradient(to bottom, var(--blue) 0 3px, transparent 3px 7px); }
body.is-resizing { cursor: col-resize; user-select: none; }

.panel-heading {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  min-height: 96px;
  padding: 20px 22px 16px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(10px);
}
.step-label, .field-kicker { color: var(--blue); }
.panel-heading h2, .review-copy h2 { margin: 0; font-size: 23px; letter-spacing: -0.025em; }
.count-label, .completion-status { color: var(--muted); font-size: 12px; font-weight: 700; }
.completion-status.is-complete { color: var(--green); }
.outline-heading-actions { display: flex; align-items: center; gap: 12px; }
.text-button { min-height: 34px; padding: 6px 9px; font-size: 12px; font-weight: 700; }

.search-field {
  position: sticky;
  top: 96px;
  z-index: 4;
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  padding: 12px 22px;
  border-bottom: 1px solid var(--line);
  background: rgba(248, 250, 252, 0.96);
  backdrop-filter: blur(10px);
  color: var(--muted);
}
.search-field input { width: 100%; min-height: 38px; border: 0; background: transparent; color: var(--ink); }
.search-field input:focus { outline: 0; }

.candidate-groups { padding: 0 22px 28px; }
.material-group { padding: 23px 0 6px; border-bottom: 1px solid var(--line-strong); }
.material-group:last-child { border-bottom: 0; }
.material-group-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.material-group-header h3 { margin: 0; font-size: 15px; }
.material-group-header p { margin: 4px 0 0; color: var(--muted); font-size: 12px; }
.copy-button { min-height: 30px; padding: 5px 8px; font-size: 11px; font-weight: 750; }

.material-group-textarea {
  display: block;
  width: 100%;
  min-height: 156px;
  max-height: 460px;
  resize: vertical;
  padding: 13px 15px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: #fff;
  color: #2a3448;
  font: 14px/1.8 Georgia, "Times New Roman", serif;
  box-shadow: inset 0 1px 2px rgba(30, 42, 66, 0.035);
}
.material-group-textarea:hover { border-color: #9da9ba; }
.material-group-textarea:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(18, 97, 168, 0.11); outline: 0; }
.material-group-textarea::placeholder { color: #a4adbb; }
.search-empty { color: var(--muted); font-size: 13px; }
.search-empty { display: grid; place-items: center; gap: 6px; min-height: 250px; text-align: center; }
.search-empty strong { color: var(--ink); }

.outline-fields { display: grid; gap: 0; padding: 0 24px; }
.outline-field { padding: 20px 0; border-bottom: 1px solid var(--line); }
.field-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 9px; }
.field-header label { display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 800; }
.field-header label span { padding: 2px 5px; border: 1px solid #f1c7c2; border-radius: 3px; color: var(--red); font-size: 9px; letter-spacing: 0.08em; }
.field-header-actions { display: flex; align-items: center; gap: 9px; }
.field-metric { color: var(--muted); font-family: "Cascadia Mono", Consolas, monospace; font-size: 11px; }
.ai-optimize-button { min-height: 32px; padding: 5px 9px; border-color: #8ab4dd; background: var(--blue-soft); color: var(--blue-dark); font-size: 11px; font-weight: 800; white-space: nowrap; }
.ai-optimize-button:hover { background: #dceeff; }
.ai-spinner { display: inline-block; width: 11px; height: 11px; margin-right: 3px; border: 2px solid #a7c6e3; border-top-color: var(--blue); border-radius: 50%; vertical-align: -2px; animation: ai-spin 700ms linear infinite; }
@keyframes ai-spin { to { transform: rotate(360deg); } }
.outline-textarea {
  display: block;
  width: 100%;
  min-height: 84px;
  resize: vertical;
  padding: 13px 14px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: #fbfcfe;
  color: #162034;
  font: 15px/1.65 Georgia, "Times New Roman", serif;
  box-shadow: inset 0 1px 2px rgba(30, 42, 66, 0.035);
}
.outline-textarea:hover { border-color: #9da9ba; }
.outline-textarea:focus { border-color: var(--blue); background: #fff; box-shadow: 0 0 0 3px rgba(18, 97, 168, 0.11); outline: 0; }
.outline-textarea::placeholder { color: #a4adbb; }

.ai-draft-panel { margin-top: 14px; padding: 14px; border: 1px solid #a9c8e5; border-left: 4px solid var(--blue); background: #f1f7fd; }
.ai-draft-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 9px; }
.ai-draft-header h4 { margin: 0; color: var(--blue-dark); font-size: 13px; }
.ai-draft-header p { margin: 3px 0 0; color: #58708a; font-size: 11px; }
.ai-draft-textarea { border-color: #a9c8e5; background: #fff; }
.ai-inline-error { margin: 10px 0 0; padding: 9px 11px; border-left: 3px solid var(--red); background: #fff3f1; color: #8a2e26; font-size: 12px; line-height: 1.45; }

.fixed-copy { margin: 24px; padding: 18px; border: 1px solid #c8d7e8; border-left: 4px solid var(--blue); background: var(--blue-soft); }
.fixed-copy-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.fixed-copy h3 { margin: 0; font-size: 16px; }
.fixed-copy-text { color: #33445a; font: 14px/1.55 Georgia, "Times New Roman", serif; }
.fixed-copy-text p { margin: 6px 0; }

.review-panel {
  display: grid;
  grid-template-columns: 1fr minmax(440px, 0.9fr);
  gap: 24px;
  margin-top: 16px;
  padding: 24px;
  border: 1px solid var(--line-strong);
  background: var(--paper);
  box-shadow: 0 8px 24px rgba(30, 42, 66, 0.05);
}
.review-actions { display: flex; align-items: center; justify-content: flex-end; gap: 18px; }
.confirmation-check { display: flex; align-items: flex-start; gap: 9px; max-width: 410px; color: #495467; font-size: 13px; line-height: 1.45; }
.confirmation-check input { width: 18px; height: 18px; margin: 0; accent-color: var(--blue); flex: 0 0 auto; }
.primary-button { min-height: 46px; padding: 10px 18px; border-color: var(--blue); background: var(--blue); color: #fff; font-weight: 800; white-space: nowrap; box-shadow: 0 6px 14px rgba(18, 97, 168, 0.2); }
.primary-button:hover { border-color: var(--blue-dark); background: var(--blue-dark); color: #fff; }

.validation-result { grid-column: 1 / -1; padding: 13px 16px; border-left: 4px solid var(--green); background: #edf9f4; color: #285647; font-size: 13px; }
.validation-result.is-error { border-color: var(--red); background: #fff3f1; color: #7d2922; }
.validation-result ul { margin: 8px 0 0; padding-left: 20px; }

.confirm-dialog { width: min(460px, calc(100vw - 32px)); padding: 0; border: 0; border-radius: 8px; box-shadow: 0 28px 80px rgba(16, 24, 40, 0.28); }
.confirm-dialog::backdrop { background: rgba(10, 21, 39, 0.58); backdrop-filter: blur(2px); }
.confirm-dialog form { display: grid; grid-template-columns: 42px 1fr; gap: 14px; padding: 24px; }
.dialog-marker { display: grid; place-items: center; width: 38px; height: 38px; border-radius: 50%; background: #fff0ee; color: var(--red); font-weight: 900; }
.dialog-copy h2 { margin: 2px 0 8px; font-size: 19px; }
.dialog-copy p { margin: 0; color: var(--muted); font-size: 14px; line-height: 1.5; }
.dialog-actions { grid-column: 1 / -1; display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.dialog-actions button { min-height: 38px; padding: 8px 14px; }
.dialog-danger { border-color: var(--red); background: var(--red); color: #fff; }
.dialog-danger:hover { background: #8f1d14; color: #fff; }

.api-dialog { width: min(680px, calc(100vw - 32px)); padding: 0; border: 0; border-radius: 9px; box-shadow: 0 28px 80px rgba(16, 24, 40, 0.28); }
.api-dialog::backdrop { background: rgba(10, 21, 39, 0.58); backdrop-filter: blur(2px); }
.api-dialog form { padding: 24px; }
.api-dialog-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 20px; }
.api-dialog-heading h2 { margin: 0; font-size: 22px; }
.dialog-close { width: 34px; height: 34px; padding: 0; border-color: transparent; background: transparent; color: var(--muted); font-size: 24px; line-height: 1; }
.api-form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.api-form-grid label { display: grid; gap: 6px; }
.api-form-grid label > span { color: #485467; font-size: 12px; font-weight: 750; }
.api-form-grid input, .api-form-grid select { width: 100%; min-height: 42px; padding: 9px 10px; border: 1px solid var(--line-strong); border-radius: 5px; background: #fff; color: var(--ink); }
.api-form-wide { grid-column: 1 / -1; }
.api-security-note { margin-top: 16px; padding: 13px 14px; border-left: 4px solid var(--yellow); background: #fff8da; color: #554d31; }
.api-security-note strong { display: block; margin-bottom: 4px; color: #2d2818; font-size: 13px; }
.api-security-note p { margin: 0; font-size: 12px; line-height: 1.55; }
.api-security-note code { padding: 1px 4px; background: rgba(23, 32, 52, 0.08); }
.api-dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
.api-dialog-actions button { min-height: 40px; padding: 8px 15px; }

.toast { position: fixed; right: 24px; bottom: 24px; z-index: 80; max-width: min(420px, calc(100vw - 48px)); padding: 12px 16px; border-left: 4px solid var(--yellow); background: #142239; color: #fff; box-shadow: 0 14px 38px rgba(16, 24, 40, 0.28); font-size: 13px; white-space: pre-line; }
.visually-hidden { position: absolute !important; width: 1px !important; height: 1px !important; padding: 0 !important; margin: -1px !important; overflow: hidden !important; clip: rect(0, 0, 0, 0) !important; white-space: nowrap !important; border: 0 !important; }

@media (max-width: 1050px) {
  .toolbar { align-items: stretch; flex-direction: column; }
  .toolbar-actions { justify-content: flex-start; }
  .workspace { grid-template-columns: minmax(300px, var(--library-width, 40%)) 9px minmax(420px, 1fr); }
  .review-panel { grid-template-columns: 1fr; }
  .review-actions { justify-content: space-between; }
}

@media (max-width: 800px) {
  .app-header { align-items: flex-start; gap: 16px; }
  .save-indicator { margin-top: 4px; }
  .workspace { display: block; height: auto; max-height: none; overflow: visible; }
  .library-panel, .outline-panel { max-height: none; overflow: visible; }
  .library-panel { border-bottom: 1px solid var(--line-strong); }
  .workspace-splitter { display: none; }
  .panel-heading, .search-field { position: static; }
  .review-actions { align-items: stretch; flex-direction: column; }
  .primary-button { width: 100%; }
}

@media (max-width: 560px) {
  .app-header { flex-direction: column; padding: 16px; }
  main { padding: 12px 10px 28px; }
  .toolbar { padding: 12px; }
  .toolbar-actions button { flex: 1 1 auto; }
  .api-status { width: 100%; text-align: right; }
  .panel-heading { padding: 18px 16px 14px; }
  .outline-heading-row { align-items: flex-start; }
  .outline-heading-actions { align-items: flex-end; flex-direction: column; gap: 6px; }
  .search-field { padding: 10px 16px; }
  .candidate-groups, .outline-fields { padding-left: 16px; padding-right: 16px; }
  .fixed-copy { margin: 20px 16px; }
  .review-panel { padding: 20px 16px; }
  .field-header { align-items: flex-start; }
  .field-header-actions { align-items: flex-end; flex-direction: column; gap: 5px; }
  .api-form-grid { grid-template-columns: 1fr; }
  .api-form-wide { grid-column: auto; }
}
