:root {
  --paper: #e8e0c0;
  --paper-light: #f7f1df;
  --ink: #171714;
  --ink-soft: #393832;
  --muted: #767368;
  --line: #bdb59b;
  --line-dark: #272720;
  --orange: #ed653f;
  --orange-dark: #b94328;
  --blue: #5965be;
  --sage: #778e79;
  --white: #fffdf4;
  --shadow: 5px 5px 0 rgba(23, 23, 20, .85);
  --shadow-soft: 0 10px 24px rgba(23, 23, 20, .08);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  color: var(--ink);
  background: var(--paper);
  font-family: Arial, "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 14px;
  line-height: 1.55;
}
button, input, textarea { font: inherit; }
button { cursor: pointer; }
button:focus-visible, input:focus-visible, textarea:focus-visible {
  outline: 3px solid var(--blue);
  outline-offset: 2px;
}
.icon { width: 16px; height: 16px; stroke-width: 2; }

.app-shell { min-height: 100vh; display: flex; flex-direction: column; }
.topbar {
  min-height: 72px; padding: 12px 22px; display: flex; align-items: center; justify-content: space-between;
  border-bottom: 2px solid var(--line-dark); background: var(--paper-light);
}
.brand-lockup { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  width: 36px; height: 36px; display: grid; place-items: center; background: var(--ink); color: var(--paper);
  font-family: Georgia, "Times New Roman", serif; font-weight: 700; font-size: 18px; transform: rotate(-4deg);
  box-shadow: 3px 3px 0 var(--orange);
}
.brand-kicker, .section-marker { margin: 0; font-family: "Courier New", monospace; font-size: 10px; font-weight: 700; letter-spacing: .08em; }
.brand-kicker { color: var(--orange-dark); }
.brand-lockup h1 { margin: 1px 0 0; font-family: Georgia, "Times New Roman", serif; font-size: 21px; letter-spacing: -.02em; }
.topbar-meta, .output-tools { display: flex; align-items: center; gap: 9px; }
.save-state { display: inline-flex; align-items: center; gap: 6px; color: var(--sage); font-size: 12px; }
.icon-button, .text-button, .primary-button, .send-button {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px; border: 1px solid var(--line-dark); transition: background .18s, color .18s, transform .18s, box-shadow .18s;
}
.icon-button { width: 36px; height: 36px; padding: 0; background: transparent; color: var(--ink); }
.icon-button:hover, .text-button:hover { background: var(--ink); color: var(--paper); transform: translateY(-1px); }
.text-button { min-height: 34px; padding: 0 10px; background: var(--paper-light); color: var(--ink); font-size: 12px; }
.primary-button { min-height: 40px; padding: 0 15px; background: var(--orange); color: #fffdf4; border-color: var(--ink); font-weight: 700; box-shadow: 3px 3px 0 var(--ink); }
.primary-button:hover { background: var(--orange-dark); transform: translate(-1px, -1px); box-shadow: 5px 5px 0 var(--ink); }

.workspace { flex: 1; min-height: 0; display: grid; grid-template-columns: 302px minmax(600px, 1fr) 326px; }
.library-panel, .chat-panel { min-height: 0; background: var(--paper-light); }
.library-panel { display: flex; flex-direction: column; border-right: 2px solid var(--line-dark); }
.chat-panel { display: flex; flex-direction: column; border-left: 2px solid var(--line-dark); }
.panel-intro, .chat-header { padding: 22px 18px 15px; border-bottom: 1px solid var(--line); }
.section-marker { color: var(--orange-dark); }
.panel-intro h2, .chat-header h2, .canvas-header h2, .output-header h2 { margin: 3px 0 1px; font-family: Georgia, "Times New Roman", serif; font-size: 20px; letter-spacing: -.025em; }
.panel-intro p, .chat-header p, .canvas-header p { margin: 0; color: var(--muted); font-size: 12px; }
.stage-list { display: flex; padding: 13px 12px 11px; border-bottom: 1px solid var(--line); overflow-x: auto; gap: 5px; }
.stage-tab { flex: 1 1 0; min-width: 0; border: 1px solid var(--line); background: transparent; color: var(--muted); padding: 8px 7px 7px; text-align: left; position: relative; }
.stage-tab::before { content: ''; position: absolute; left: 0; top: 0; width: 100%; height: 3px; background: transparent; }
.stage-tab:hover { color: var(--ink); border-color: var(--ink); }
.stage-tab.active { color: var(--ink); border-color: var(--ink); background: #f1e8cf; font-weight: 700; }
.stage-tab.active::before { background: var(--orange); }
.stage-count { display: block; margin-top: 2px; color: var(--muted); font-family: "Courier New", monospace; font-size: 10px; font-variant-numeric: tabular-nums; }
.library-content { min-height: 0; overflow: auto; padding: 15px 13px 22px; }
.library-heading { display: flex; justify-content: space-between; align-items: baseline; margin: 0 3px 10px; }
.library-heading strong { font-family: Georgia, "Times New Roman", serif; font-size: 17px; }
.library-heading span { color: var(--muted); font-family: "Courier New", monospace; font-size: 10px; }
.module-card { position: relative; margin-bottom: 10px; padding: 12px 12px 11px; border: 1px solid var(--line-dark); background: var(--white); box-shadow: 3px 3px 0 var(--line); text-align: left; transition: transform .18s, box-shadow .18s, background .18s; }
.module-card:nth-child(3n + 1) { transform: rotate(-.35deg); }
.module-card:nth-child(3n) { transform: rotate(.35deg); }
.module-card:hover, .module-card.selected { background: #fff8dc; transform: translate(-1px, -2px) rotate(0deg); box-shadow: 5px 5px 0 var(--orange); }
.card-meta { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.tag { display: inline-flex; align-items: center; min-height: 20px; padding: 0 6px; background: #dfe4d4; color: #36513a; font-family: "Courier New", monospace; font-size: 10px; }
.tag.orange { background: #ffd8c9; color: var(--orange-dark); }
.module-card h3 { margin: 8px 0 4px; font-family: Georgia, "Times New Roman", serif; font-size: 15px; }
.module-card p { margin: 0; color: var(--muted); font-size: 12px; line-height: 1.45; }
.card-action { display: flex; align-items: center; gap: 5px; margin-top: 9px; color: var(--orange-dark); font-family: "Courier New", monospace; font-size: 10px; font-weight: 700; }

.writing-area { min-width: 0; min-height: 0; display: grid; grid-template-rows: 410px minmax(340px, 1fr); background: var(--paper); }
.canvas-section, .output-section { min-height: 0; padding: 0 20px; }
.canvas-section { border-bottom: 2px solid var(--line-dark); }
.canvas-header { padding: 20px 0 14px; display: flex; align-items: flex-end; justify-content: space-between; gap: 20px; }
.section-title-row { display: flex; align-items: flex-end; gap: 18px; }
.stage-hint { padding: 4px 7px; border: 1px solid var(--line-dark); color: var(--blue); font-family: "Courier New", monospace; font-size: 10px; white-space: nowrap; }
.topic-form { min-width: min(47%, 500px); }
.topic-form label { display: block; margin-bottom: 5px; color: var(--muted); font-family: "Courier New", monospace; font-size: 10px; }
.topic-controls { display: flex; gap: 9px; }
.topic-controls input { min-width: 0; flex: 1; height: 40px; padding: 0 11px; border: 1px solid var(--line-dark); background: var(--white); color: var(--ink); }
.topic-controls input::placeholder { color: #aaa38f; }
.canvas { height: 300px; padding: 12px 13px 14px; overflow: auto; border: 1px solid var(--line-dark); background-color: var(--paper-light); background-image: radial-gradient(rgba(23, 23, 20, .2) .8px, transparent .8px); background-size: 15px 15px; }
.canvas-ruler { display: flex; justify-content: space-between; margin: 0 2px 12px; color: var(--muted); font-family: "Courier New", monospace; font-size: 9px; letter-spacing: .06em; }
.canvas-inner { min-height: 220px; display: flex; flex-wrap: wrap; align-content: flex-start; gap: 13px; }
.canvas-empty { width: 100%; padding: 48px 20px; border: 1px dashed var(--line-dark); background: rgba(255,253,244,.78); color: var(--muted); text-align: center; }
.canvas-empty strong { display: block; margin-bottom: 4px; color: var(--ink); font-family: Georgia, "Times New Roman", serif; font-size: 17px; }
.canvas-card { position: relative; width: min(100%, 220px); padding: 12px 12px 13px; border: 1px solid var(--ink); background: var(--white); box-shadow: var(--shadow); cursor: grab; }
.canvas-card:nth-child(3n + 1) { transform: rotate(-.5deg); }
.canvas-card:nth-child(3n) { transform: rotate(.5deg); }
.canvas-card:active { cursor: grabbing; }
.canvas-card .remove { position: absolute; top: 7px; right: 7px; border: 0; padding: 2px; background: transparent; color: var(--muted); }
.canvas-card .remove:hover { color: var(--orange); }
.canvas-card .card-order { color: var(--orange-dark); font-family: "Courier New", monospace; font-size: 10px; font-weight: 700; }
.canvas-card h3 { margin: 9px 20px 4px 0; font-family: Georgia, "Times New Roman", serif; font-size: 15px; }
.canvas-card p { margin: 0; color: var(--muted); font-size: 12px; line-height: 1.45; }

.output-section { display: flex; flex-direction: column; }
.output-header { padding: 18px 0 11px; display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.output-status { display: inline-flex; align-items: center; gap: 5px; color: var(--sage); font-family: "Courier New", monospace; font-size: 10px; }
.output { min-height: 0; flex: 1; overflow: auto; margin-bottom: 20px; padding: 24px 28px; border: 1px solid var(--line-dark); background: var(--white); box-shadow: var(--shadow-soft); }
.output h3 { max-width: 780px; margin: 0 0 16px; font-family: Georgia, "Times New Roman", serif; font-size: 28px; line-height: 1.18; }
.output p { max-width: 760px; margin: 0 0 14px; font-family: Georgia, "Times New Roman", serif; font-size: 16px; line-height: 1.8; }
.output p.highlight { padding-left: 14px; border-left: 4px solid var(--orange); }
.output .placeholder { color: var(--muted); font-size: 14px; }

.chat-header { display: flex; align-items: flex-start; justify-content: space-between; }
.chat-context { padding: 13px 16px; border-bottom: 1px solid var(--line); background: #eee5cb; }
.chat-context > span { color: var(--muted); font-family: "Courier New", monospace; font-size: 10px; }
.context-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 7px; }
.context-chip { display: inline-flex; align-items: center; min-height: 24px; padding: 0 7px; border: 1px solid var(--line-dark); background: var(--white); color: var(--ink); font-family: "Courier New", monospace; font-size: 10px; }
.messages { flex: 1; min-height: 0; overflow: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.message { max-width: 90%; padding: 10px 11px; border: 1px solid var(--line-dark); background: var(--white); color: var(--ink-soft); font-size: 12px; line-height: 1.6; box-shadow: 3px 3px 0 var(--line); }
.message.user { align-self: flex-end; background: var(--ink); color: var(--paper-light); box-shadow: 3px 3px 0 var(--orange); }
.message-label { display: block; margin-bottom: 3px; color: var(--orange-dark); font-family: "Courier New", monospace; font-size: 9px; font-weight: 700; }
.message.user .message-label { color: #f4a28c; }
.chat-composer { display: flex; gap: 8px; padding: 12px; border-top: 2px solid var(--line-dark); background: var(--paper-light); }
.chat-composer textarea { min-width: 0; height: 68px; flex: 1; resize: none; padding: 9px 10px; border: 1px solid var(--line-dark); background: var(--white); color: var(--ink); }
.send-button { align-self: flex-end; width: 40px; height: 40px; padding: 0; border-color: var(--ink); background: var(--orange); color: var(--white); box-shadow: 3px 3px 0 var(--ink); }
.send-button:hover { background: var(--orange-dark); transform: translate(-1px, -1px); }
.toast { position: fixed; right: 22px; bottom: 22px; z-index: 10; padding: 10px 13px; border: 1px solid var(--ink); background: var(--ink); color: var(--paper-light); font-family: "Courier New", monospace; font-size: 11px; opacity: 0; transform: translateY(10px); pointer-events: none; transition: opacity .2s, transform .2s; }
.toast.show { opacity: 1; transform: translateY(0); }

@media (max-width: 1220px) {
  .workspace { grid-template-columns: 270px minmax(560px, 1fr); }
  .chat-panel { grid-column: 1 / -1; min-height: 330px; border-top: 2px solid var(--line-dark); border-left: 0; }
  .writing-area { grid-column: 2; grid-row: 1; }
  .library-panel { grid-column: 1; grid-row: 1; }
}
@media (max-width: 800px) {
  .topbar { padding: 11px 14px; }
  .brand-lockup h1 { font-size: 18px; }
  .save-state { display: none; }
  .workspace { display: block; }
  .library-panel, .writing-area, .chat-panel { display: block; }
  .library-panel { border-right: 0; border-bottom: 2px solid var(--line-dark); }
  .stage-list { padding: 10px; }
  .stage-list { overflow-x: auto; }
  .stage-tab { flex: 0 0 68px; min-width: 68px; }
  .library-content { max-height: 330px; }
  .canvas-section, .output-section { padding: 0 13px; }
  .canvas-header { display: block; }
  .section-title-row { align-items: flex-start; justify-content: space-between; }
  .topic-form { min-width: 0; margin-top: 14px; }
  .topic-controls { flex-wrap: wrap; }
  .topic-controls input { flex-basis: 100%; }
  .canvas-section { min-height: 535px; }
  .canvas { height: 320px; }
  .output-section { min-height: 500px; }
  .output { padding: 19px 17px; }
  .output h3 { font-size: 24px; }
  .output p { font-size: 15px; }
  .chat-panel { min-height: 470px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; }
}
