:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-soft: #f8fafc;
  --line: #d8dde6;
  --line-strong: #b9c1ce;
  --text: #20242b;
  --muted: #687386;
  --accent: #c0182f;
  --accent-dark: #961326;
  --green: #147d64;
  --danger: #b42318;
  --shadow: 0 12px 32px rgba(24, 32, 48, .08);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.topbar {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.logo {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: #fff;
  border-radius: 6px;
  font-weight: 800;
}
.brand h1 { margin: 0; font-size: 18px; line-height: 1.1; }
.brand p { margin: 3px 0 0; font-size: 12px; color: var(--muted); }

.top-actions { display: flex; gap: 8px; }
.icon-btn {
  width: 36px;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-soft);
  color: var(--text);
  cursor: pointer;
  font-size: 22px;
  line-height: 1;
}

.workspace {
  height: calc(100vh - 64px);
  display: grid;
  grid-template-columns: 260px minmax(420px, 1fr) minmax(360px, .95fr);
  gap: 14px;
  padding: 14px;
}

.rail, .chat-panel, .result-panel {
  min-height: 0;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.rail {
  display: flex;
  flex-direction: column;
  overflow: auto;
}
.rail-section { padding: 14px; border-bottom: 1px solid var(--line); }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.section-head h2, .result-head h2 { margin: 0; font-size: 13px; text-transform: uppercase; color: var(--muted); letter-spacing: .04em; }

.project-list { list-style: none; padding: 0; margin: 12px 0 0; display: grid; gap: 6px; }
.project-list li {
  padding: 10px;
  border: 1px solid transparent;
  border-radius: 6px;
  cursor: pointer;
}
.project-list li:hover { background: var(--surface-soft); border-color: var(--line); }
.project-list li.active { border-color: var(--accent); background: #fff4f5; }
.project-list strong { display: block; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.project-list small { display: block; margin-top: 3px; color: var(--muted); font-size: 12px; }
.muted-item { color: var(--muted); font-size: 13px; margin: 10px 0 0; }

.template-list { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.template-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-soft);
  font-size: 13px;
  cursor: pointer;
}
.template-maker { margin-top: 14px; font-size: 13px; color: var(--muted); }
.template-maker summary { cursor: pointer; color: var(--text); }

.chat-panel { display: grid; grid-template-rows: auto 1fr auto auto; overflow: hidden; }
.chat-head {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) auto;
  gap: 12px;
  padding: 14px;
  border-bottom: 1px solid var(--line);
}
.project-field { margin: 0; }

.mode-switch {
  display: grid;
  grid-template-columns: repeat(3, minmax(64px, 1fr));
  align-self: end;
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
}
.mode-switch input { position: absolute; opacity: 0; pointer-events: none; }
.mode-switch label {
  padding: 9px 12px;
  background: var(--surface-soft);
  font-size: 13px;
  text-align: center;
  cursor: pointer;
  border-right: 1px solid var(--line);
}
.mode-switch label:last-child { border-right: 0; }
.mode-switch input:checked + label { background: var(--accent); color: #fff; }

.chat-log {
  overflow: auto;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: linear-gradient(#fff, #fbfcfd);
}
.message {
  max-width: 82%;
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.45;
  white-space: pre-wrap;
}
.message.user { align-self: flex-end; background: #fff4f5; border-color: #efb7c0; }
.message.system { align-self: flex-start; background: var(--surface); color: var(--muted); }

.helper-panel { padding: 12px 14px; border-top: 1px solid var(--line); background: var(--surface-soft); }
.helper-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; margin-top: 10px; }
.toggle { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }

.composer { padding: 14px; border-top: 1px solid var(--line); }
.composer textarea { min-height: 118px; }
.composer-tools {
  display: grid;
  grid-template-columns: auto auto minmax(120px, 1fr) auto 70px 130px minmax(110px, 150px) auto;
  gap: 8px;
  align-items: center;
  margin-top: 10px;
}
.instruction-line { margin-top: 8px; }

.field { display: block; margin-bottom: 10px; }
.field.compact { margin: 10px 0 0; }
.field > span { display: block; margin-bottom: 5px; color: var(--muted); font-size: 12px; }
input[type=text], input[type=number], textarea, select, input[type=file] {
  width: 100%;
  min-width: 0;
  padding: 9px 10px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  font: inherit;
  font-size: 14px;
}
textarea { resize: vertical; }
input:disabled { opacity: .55; background: #eef1f5; }
input[type=file] { font-size: 12px; padding: 7px; }

.file-btn, .pages-control {
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--surface-soft);
  padding: 8px 10px;
  font-size: 13px;
  white-space: nowrap;
}
.file-btn input { display: none; }
.repo-input { min-width: 120px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 38px;
  padding: 8px 14px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--surface-soft);
  color: var(--text);
  text-decoration: none;
  font: inherit;
  font-size: 14px;
  cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 700; }
.btn.primary:hover { background: var(--accent-dark); }
.btn.secondary { background: #edf7f4; border-color: #b8ddd2; color: var(--green); }
.btn.small { min-height: 32px; padding: 6px 10px; font-size: 13px; }
.btn.strong { border-color: var(--accent); color: var(--accent); background: #fff4f5; }
.btn.full { width: 100%; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

.hint { min-height: 18px; margin: 8px 0 0; color: var(--muted); font-size: 12px; }
.hint.err { color: var(--danger); }

.result-panel { display: grid; grid-template-rows: auto auto 1fr; overflow: hidden; }
.result-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 14px;
  border-bottom: 1px solid var(--line);
}
.result-head p { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
.exports { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }

.progress { padding: 12px 14px 0; }
.bar { height: 8px; background: #e7ebf0; border-radius: 6px; overflow: hidden; }
.bar-fill { height: 100%; width: 0; background: linear-gradient(90deg, var(--accent), var(--green)); transition: width .25s ease; }
.progress span { display: block; margin-top: 7px; color: var(--muted); font-size: 13px; }

.gallery {
  min-height: 0;
  overflow: auto;
  padding: 14px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  align-content: start;
  gap: 12px;
}
.empty { color: var(--muted); grid-column: 1/-1; }
.page-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
}
.page-card img { width: 100%; aspect-ratio: 16 / 10; object-fit: cover; display: block; cursor: zoom-in; background: #eef1f5; }
.page-card .cap { display: flex; gap: 8px; align-items: flex-start; padding: 9px 10px; font-size: 12px; color: var(--muted); }
.page-card .cap strong { color: var(--accent); }

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 28px;
  background: rgba(17, 24, 39, .86);
}
.lightbox.open { display: flex; }
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; box-shadow: 0 16px 60px rgba(0,0,0,.35); }

@media (max-width: 1280px) {
  .workspace { grid-template-columns: 240px 1fr; grid-template-rows: minmax(520px, 1fr) minmax(360px, .9fr); }
  .result-panel { grid-column: 1 / -1; }
}

@media (max-width: 820px) {
  .topbar { padding: 0 12px; }
  .workspace { height: auto; min-height: calc(100vh - 64px); grid-template-columns: 1fr; grid-template-rows: auto auto auto; padding: 10px; }
  .chat-head, .helper-grid, .composer-tools { grid-template-columns: 1fr; }
  .rail, .chat-panel, .result-panel { min-height: auto; }
  .chat-log { min-height: 260px; }
  .message { max-width: 100%; }
}
