:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --line: #dfe4ea;
  --line-strong: #c3ccd6;
  --ink: #1f2329;
  --ink-2: #5a6472;
  --ink-3: #8b95a3;
  --brand: #b3251f;
  --brand-dark: #8f1d18;
  --accent: #1f6feb;
  --warn: #b7791f;
  --ok: #1a7f4b;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 8px 24px rgba(16, 24, 40, 0.06);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.6 "Microsoft YaHei", "微软雅黑", "PingFang SC", "Hiragino Sans GB", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}

button,
input,
select,
textarea {
  font-family: inherit;
  font-size: 13px;
  color: var(--ink);
}

/* ---------------- 顶栏 ---------------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 20px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  box-shadow: 0 1px 3px rgba(16, 24, 40, 0.04);
  flex-wrap: wrap;
}

/* 顶栏按钮多，窄屏会折行；按钮内部文字不要断成两行 */
.topbar button,
.topbar .who,
.topbar a.btn {
  white-space: nowrap;
  flex: 0 0 auto;
}

.topbar .brand {
  flex: 0 1 auto;
  min-width: 0;
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 10px;
  font-weight: 700;
  font-size: 16px;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

.brand small {
  font-weight: 400;
  font-size: 12px;
  color: var(--ink-3);
  letter-spacing: 0;
}

.topbar .spacer {
  flex: 1;
}

.topbar .status {
  font-size: 12px;
  color: var(--ink-3);
  white-space: nowrap;
}

.topbar .status.dirty {
  color: var(--warn);
}

/* ---------------- 按钮 ---------------- */
button {
  cursor: pointer;
  border: 1px solid var(--line-strong);
  background: #fff;
  border-radius: 8px;
  padding: 6px 12px;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

button:hover {
  background: #f6f8fa;
  border-color: var(--ink-3);
}

button:active {
  transform: translateY(1px);
}

button.primary {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
  font-weight: 600;
}

button.primary:hover {
  background: var(--brand-dark);
  border-color: var(--brand-dark);
}

button.ghost {
  border-color: transparent;
  background: transparent;
  color: var(--ink-2);
  padding: 4px 8px;
}

button.ghost:hover {
  background: #eef1f5;
}

button.mini {
  padding: 2px 7px;
  font-size: 12px;
  border-radius: 6px;
  line-height: 1.5;
}

button.danger:hover {
  color: var(--brand);
  border-color: var(--brand);
}

/* ---------------- 布局 ---------------- */
main {
  max-width: 1180px;
  margin: 0 auto;
  padding: 18px 20px 120px;
  display: grid;
  gap: 16px;
}

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

.panel > h2 {
  margin: 0 0 12px;
  font-size: 14px;
  font-weight: 700;
  color: var(--ink);
  display: flex;
  align-items: center;
  gap: 8px;
}

.panel > h2::before {
  content: "";
  width: 3px;
  height: 14px;
  border-radius: 2px;
  background: var(--brand);
}

.panel > h2 .hint {
  font-weight: 400;
  font-size: 12px;
  color: var(--ink-3);
}

/* 工程信息面板里的「更换项目」推到最右 */
.panel > h2 > button {
  margin-left: auto;
}

.row-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

/* ---------------- 表单 ---------------- */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 12px 16px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.field > label {
  font-size: 12px;
  color: var(--ink-2);
}

.field input,
.field select,
.field textarea {
  width: 100%;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  padding: 7px 10px;
  background: #fff;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.field input:focus,
.field select:focus,
.field textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(31, 111, 235, 0.12);
}

/* 只读字段（工程信息、序号、填写人）：淡底 + 灰字，一眼看出不可编辑 */
.field input[readonly] {
  background: #f5f7fa;
  color: var(--ink-2);
  cursor: default;
}

.field input[readonly]:focus {
  border-color: var(--line-strong);
  box-shadow: none;
}

.field.wide {
  grid-column: 1 / -1;
}

.note {
  margin: 8px 0 0;
  font-size: 12px;
  color: var(--ink-3);
  line-height: 1.7;
}

/* ---------------- 页（一张表） ---------------- */
.page-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 14px;
}

.page-card:last-child {
  margin-bottom: 0;
}

.page-head {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 10px 12px;
  background: #f7f9fb;
  border-bottom: 1px solid var(--line);
}

.page-head .tag {
  font-size: 12px;
  font-weight: 700;
  color: #fff;
  background: var(--ink-2);
  border-radius: 6px;
  padding: 2px 8px;
}

.page-head .field {
  flex: 0 0 auto;
  min-width: 220px;
  flex-direction: row;
  align-items: center;
  gap: 6px;
}

.page-head .field > label {
  white-space: nowrap;
  font-size: 12px;
}

.page-head .field input {
  width: 200px;
  padding: 5px 8px;
}

/* ---------------- 行编辑器 ---------------- */
.rows {
  display: grid;
  gap: 0;
}

.rows-head,
.row-item {
  display: grid;
  grid-template-columns: 132px minmax(220px, 1fr) 190px 92px;
  gap: 0;
}

.rows-head {
  background: #eef2f6;
  border-bottom: 1px solid var(--line);
  font-size: 12px;
  font-weight: 700;
  color: var(--ink-2);
}

.rows-head > div {
  padding: 8px 10px;
  border-right: 1px solid var(--line);
}

.rows-head > div:last-child {
  border-right: 0;
  text-align: center;
}

.row-item {
  border-bottom: 1px solid #eef1f5;
  background: #fff;
}

.row-item:hover {
  background: #fbfcfd;
}

.row-item > .cell {
  border-right: 1px solid #eef1f5;
  padding: 4px 6px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
}

.row-item > .cell:last-child {
  border-right: 0;
  align-items: center;
  justify-content: center;
  flex-direction: row;
  gap: 2px;
}

.row-item textarea {
  width: 100%;
  resize: none;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 4px 6px;
  line-height: 1.5;
  background: transparent;
  overflow: hidden;
  min-height: 28px;
}

.row-item textarea:hover {
  border-color: var(--line);
  background: #fff;
}

.row-item textarea:focus {
  border-color: var(--accent);
  background: #fff;
  box-shadow: 0 0 0 3px rgba(31, 111, 235, 0.1);
  outline: none;
}

.row-item.is-group-start > .cell:first-child {
  box-shadow: inset 3px 0 0 var(--brand);
}

.row-tools {
  display: flex;
  align-items: center;
  gap: 2px;
}

.style-select {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 2px 4px;
  font-size: 11px;
  color: var(--ink-2);
  background: #f8fafc;
  width: 100%;
}

.wrap-flag {
  font-size: 11px;
  color: var(--warn);
  white-space: nowrap;
}

.rows-foot {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  background: #fafbfc;
  border-top: 1px dashed var(--line);
}

/* ---------------- 签字 ---------------- */
.sign-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 14px;
}

.sign-box {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  background: #fcfdfe;
}

.sign-box .title {
  font-size: 12px;
  font-weight: 700;
  color: var(--ink-2);
  margin-bottom: 6px;
}

.sign-box input[type="text"] {
  width: 100%;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  padding: 6px 9px;
  margin-bottom: 8px;
}

.sign-pad {
  position: relative;
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  background: #fff;
  touch-action: none;
}

.sign-pad canvas {
  display: block;
  width: 100%;
  height: 84px;
  border-radius: 8px;
  cursor: crosshair;
}

.sign-pad .placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: var(--ink-3);
  pointer-events: none;
}

.sign-pad.drawn .placeholder {
  display: none;
}

.sign-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 6px;
  font-size: 12px;
  color: var(--ink-3);
}

/* ---------------- 历史抽屉 ---------------- */
.drawer {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  justify-content: flex-end;
  background: rgba(15, 23, 42, 0.35);
}

.drawer.hidden {
  display: none;
}

.drawer .sheet {
  width: min(440px, 92vw);
  background: #fff;
  height: 100%;
  display: flex;
  flex-direction: column;
  box-shadow: -8px 0 32px rgba(15, 23, 42, 0.18);
}

/* 模板维护要比历史抽屉宽得多，行编辑是三列输入 + 操作按钮 */
#tpl-drawer .sheet {
  width: min(1020px, 96vw);
}

.drawer .sheet header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  font-weight: 700;
  flex-wrap: nowrap;
}

.drawer .sheet header .hd-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 0 0 auto;
}

.drawer .sheet header button {
  white-space: nowrap;
  flex: 0 0 auto;
}

.drawer .sheet header .hd-title {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.drawer .sheet .body {
  overflow: auto;
  padding: 10px 16px 24px;
}

.rec-item {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 10px;
}

.rec-item .t {
  font-weight: 700;
  font-size: 13px;
  margin-bottom: 2px;
}

.rec-item .m {
  font-size: 12px;
  color: var(--ink-3);
  margin-bottom: 8px;
}

.rec-item .ops {
  display: flex;
  gap: 6px;
}

.empty {
  text-align: center;
  color: var(--ink-3);
  font-size: 13px;
  padding: 40px 0;
}

/* ---------------- 提示条 ---------------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%) translateY(12px);
  background: #1f2329;
  color: #fff;
  border-radius: 10px;
  padding: 10px 18px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 80;
  max-width: 80vw;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast.err {
  background: #b3251f;
}

/* ---------------- 进门页 ---------------- */
.gate {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: radial-gradient(1200px 600px at 50% -10%, #ffffff 0%, #eef1f5 60%, #e6eaef 100%);
}

.gate.hidden {
  display: none;
}

.gate-card {
  width: min(460px, 100%);
  background: #fff;
  border-radius: 16px;
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  padding: 28px 26px 22px;
}

.gate-brand h1 {
  margin: 0;
  font-size: 20px;
  letter-spacing: 0.5px;
  color: var(--ink);
}

.gate-brand h1::before {
  content: "";
  display: inline-block;
  width: 4px;
  height: 18px;
  margin-right: 8px;
  border-radius: 2px;
  background: var(--brand);
  transform: translateY(2px);
}

.gate-sub {
  margin: 4px 0 0 12px;
  font-size: 13px;
  color: var(--ink-3);
}

.gate-tip {
  margin: 18px 0 16px;
  font-size: 13px;
  line-height: 1.7;
  color: var(--ink-2);
}

.gate-card .field {
  margin-bottom: 12px;
}

.gate-card input[type="text"] {
  width: 100%;
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  font-size: 15px;
}

.gate-card input[type="text"]:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(179, 37, 31, 0.12);
}

.gate-recent {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 0 0 14px;
  min-height: 0;
}

/* 进门页：老用户选项目 / 新用户填档案 */
.gate-profiles {
  margin: 0 0 12px;
  padding: 12px 12px 2px;
  background: #f7f9fc;
  border: 1px solid var(--line);
  border-radius: 10px;
}

.gate-profiles select {
  width: 100%;
  height: 40px;
  padding: 0 10px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  font-size: 14px;
  background: #fff;
}

.gate-new {
  margin: 0 0 12px;
  padding: 12px 12px 2px;
  border: 1px dashed var(--line-strong);
  border-radius: 10px;
}

.gate-new .grid {
  gap: 12px;
}

.gate-new .field {
  margin-bottom: 10px;
}

.gate.hidden,
.gate-profiles.hidden,
.gate-new.hidden {
  display: none;
}

.gate-card .note {
  margin: 12px 0 0;
  text-align: center;
}

button.block {
  display: block;
  width: 100%;
  height: 40px;
  font-size: 14px;
}

/* ---------------- 顶栏填写人 ---------------- */
.who {
  font-size: 12px;
  color: var(--ink-2);
  background: #f1f4f8;
  border-radius: 999px;
  padding: 3px 10px;
  white-space: nowrap;
}

/* ---------------- 通用栅格 ---------------- */
.grid.grid-2 {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.auto-tag {
  display: inline-block;
  margin-left: 6px;
  font-size: 10px;
  font-weight: 400;
  color: var(--ok);
  background: rgba(26, 127, 75, 0.1);
  border-radius: 4px;
  padding: 0 5px;
  vertical-align: 1px;
}

/* ---------------- 模板维护（在线编辑） ---------------- */
.tpl-form {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px 14px;
  margin-bottom: 18px;
  background: #fcfdfe;
}

.tpl-form h3,
.drawer .sheet .body > h3 {
  margin: 0 0 10px;
  font-size: 13px;
  color: var(--ink-2);
}

.drawer .sheet .body > h3 {
  margin-top: 4px;
}

.tpl-form .field {
  margin-bottom: 10px;
}

#tpl-edit {
  margin-top: 12px;
}

#tpl-edit .field {
  margin-bottom: 10px;
}

#tpl-edit .field.wide input {
  width: 100%;
}

.tpl-sys {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  margin-bottom: 10px;
  overflow: hidden;
}

.tpl-sys > summary {
  cursor: pointer;
  padding: 10px 12px;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
  background: #f7f9fc;
  border-bottom: 1px solid transparent;
  list-style: none;
}

.tpl-sys > summary::-webkit-details-marker {
  display: none;
}

.tpl-sys > summary::before {
  content: "▸ ";
  color: var(--brand);
}

.tpl-sys[open] > summary {
  border-bottom-color: var(--line);
}

.tpl-sys[open] > summary::before {
  content: "▾ ";
}

.tpl-sys > .field {
  padding: 10px 12px 0;
}

.tpl-page {
  padding: 6px 12px 12px;
}

.tpl-page-head {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin: 6px 0 8px;
  font-size: 12px;
  color: var(--ink-3);
}

.tpl-page-head label {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--ink-2);
}

.tpl-page-head input {
  width: 220px;
  max-width: 46vw;
}

.tpl-rows {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.tpl-row {
  display: grid;
  grid-template-columns: minmax(90px, 0.8fr) minmax(160px, 1.6fr) minmax(120px, 1fr) 78px;
  gap: 6px;
  align-items: center;
}

.tpl-row input {
  min-width: 0;
  width: 100%;
}

.tpl-row .ops {
  display: flex;
  gap: 2px;
  justify-content: flex-end;
}

button.mini {
  padding: 3px 7px;
  font-size: 12px;
  line-height: 1.4;
  border-radius: 6px;
  color: var(--ink-2);
}

.tpl-rows > button.mini {
  align-self: flex-start;
  margin-top: 2px;
}

#tpl-list .rec-item .ops {
  margin-top: 2px;
}

/* 模板维护抽屉里的「类别」管理条 */
.tpl-cats {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 8px;
  margin: 0 0 10px;
  padding: 10px 12px;
  background: #f7f9fc;
  border: 1px solid var(--line);
  border-radius: 10px;
}

.tpl-cats .field {
  flex: 1 1 260px;
  min-width: 0;
}

.tpl-cats select {
  width: 100%;
  height: 34px;
  padding: 0 8px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  font-size: 13px;
  background: #fff;
}

.cat-new {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 10px;
  padding: 10px 12px;
  background: #fffdf7;
  border: 1px dashed var(--warn);
  border-radius: 10px;
}

.cat-new input {
  flex: 1 1 200px;
  min-width: 0;
  height: 34px;
  padding: 0 8px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  font-size: 13px;
}

.cat-new select {
  flex: 1 1 240px;
  min-width: 0;
  height: 34px;
  padding: 0 8px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  font-size: 13px;
  background: #fff;
}

.cat-new.hidden {
  display: none;
}

@media (max-width: 860px) {
  .tpl-row {
    grid-template-columns: 1fr 1fr;
    gap: 4px;
    padding-bottom: 8px;
    border-bottom: 1px dashed var(--line);
  }

  .tpl-row .ops {
    grid-column: 1 / -1;
    justify-content: flex-start;
  }
}

@media (max-width: 860px) {
  .rows-head,
  .row-item {
    grid-template-columns: 96px minmax(160px, 1fr) 140px 76px;
  }

  main {
    padding: 12px 10px 120px;
  }

  .page-head .field {
    min-width: 0;
    flex: 1 1 100%;
  }

  .page-head .field input {
    width: 100%;
  }

  .grid.grid-2 {
    grid-template-columns: 1fr;
  }
}
