:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --line: #e5e7eb;
  --text: #111827;
  --muted: #6b7280;
  --accent: #4f46e5;
  --accent-soft: #eef2ff;
  --danger: #dc2626;
  --ok: #059669;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 8px 24px rgba(16, 24, 40, .06);
}

@font-face {
  font-family: "NotoSC";
  src: url("/static/fonts/NotoSansCJKsc-Regular.otf") format("opentype");
  font-display: swap;
}

@font-face {
  font-family: "PuHuiTi";
  src: url("/static/fonts/AlibabaPuHuiTi-3-55-Regular.ttf") format("truetype");
  font-display: swap;
}

@font-face {
  font-family: "PuHuiTiXB";
  src: url("/static/fonts/AlibabaPuHuiTi-3-95-ExtraBold.ttf") format("truetype");
  font-display: swap;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
        "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  -webkit-font-smoothing: antialiased;
}

button { font: inherit; }

.topbar {
  height: 64px;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }

.logo {
  width: 36px; height: 36px;
  display: grid; place-items: center;
  border-radius: 10px;
  background: linear-gradient(135deg, #4f46e5, #7c3aed);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .5px;
  flex: none;
}

.brand h1 { margin: 0; font-size: 15px; font-weight: 650; }
.brand p { margin: 1px 0 0; font-size: 12px; color: var(--muted); }

.topbar .actions { margin-left: auto; display: flex; gap: 8px; }

.btn {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 9px;
  cursor: pointer;
  font-weight: 550;
  transition: .15s ease;
  white-space: nowrap;
}
.btn:hover { border-color: #c7cad1; background: #fafafa; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn.primary:hover { background: #4338ca; border-color: #4338ca; }
.btn.ghost { background: #fff; }

main { height: calc(100% - 64px); }

/* ---------- dropzone ---------- */

#dropzone {
  height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
}

.dropcard {
  width: min(640px, 100%);
  padding: 56px 40px;
  background: var(--panel);
  border: 1.5px dashed #d3d6dd;
  border-radius: 20px;
  text-align: center;
  cursor: pointer;
  transition: .18s ease;
}
.dropcard:hover, .dropcard.drag {
  border-color: var(--accent);
  background: #fbfbff;
  box-shadow: var(--shadow);
}
.dropicon {
  width: 56px; height: 56px;
  margin: 0 auto 18px;
  border-radius: 16px;
  background: var(--accent-soft);
  display: grid; place-items: center;
  color: var(--accent);
  font-size: 24px;
}
.dropcard h2 { margin: 0 0 8px; font-size: 18px; }
.dropcard p { margin: 0; color: var(--muted); font-size: 13px; }
.dropcard .tag {
  display: inline-block;
  margin-top: 18px;
  padding: 5px 10px;
  border-radius: 999px;
  background: #f3f4f6;
  color: var(--muted);
  font-size: 12px;
}

/* ---------- workspace ---------- */

#workspace {
  height: 100%;
  display: grid;
  grid-template-columns: 348px minmax(0, 1fr);
}

.sidebar {
  border-right: 1px solid var(--line);
  background: var(--panel);
  overflow-y: auto;
  padding: 16px;
}

.docinfo { padding: 4px 4px 14px; border-bottom: 1px solid var(--line); margin-bottom: 14px; }
.docinfo .name {
  font-weight: 600;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.docinfo .meta { color: var(--muted); font-size: 12px; margin-top: 3px; }

.panel { padding: 6px 2px 16px; }
.panel h2 {
  margin: 0 0 10px;
  font-size: 12px;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 8px;
}

.pill {
  display: inline-grid;
  place-items: center;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 999px;
  background: #f3f4f6;
  color: var(--muted);
  font-size: 11px;
}
.pill.on { background: var(--accent-soft); color: var(--accent); }

.field {
  display: grid;
  grid-template-columns: 76px minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}
.field label { color: var(--muted); font-size: 12.5px; }
.field input, #selInput {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  font: inherit;
  font-size: 13px;
  outline: none;
  transition: .15s ease;
}
.field input:focus, #selInput:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, .12);
}
.field input.changed { border-color: #a5b4fc; background: #fbfbff; }

.hint { color: var(--muted); font-size: 12px; margin: 8px 0 0; line-height: 1.6; }

#selectionBox.empty, #editList.empty {
  color: var(--muted);
  font-size: 12.5px;
  padding: 10px 12px;
  border: 1px dashed var(--line);
  border-radius: 10px;
  background: #fafafa;
}

.sel-old {
  font-size: 12.5px;
  color: var(--muted);
  margin: 0 0 8px;
  word-break: break-all;
}
.sel-old b { color: var(--text); font-weight: 600; }
.sel-actions { display: flex; gap: 8px; margin-top: 8px; }
.sel-actions .btn { padding: 6px 10px; font-size: 12.5px; }

.edit-row {
  display: flex;
  align-items: baseline;
  gap: 6px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 9px;
  margin-bottom: 6px;
  font-size: 12.5px;
  background: #fff;
}
.edit-row .old { color: var(--muted); text-decoration: line-through; max-width: 40%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.edit-row .arrow { color: #c0c4cc; }
.edit-row .new { color: var(--accent); font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.edit-row button {
  border: 0; background: none; color: #9ca3af; cursor: pointer;
  padding: 0 2px; font-size: 15px; line-height: 1;
}
.edit-row button:hover { color: var(--danger); }

/* ---------- viewer ---------- */

.viewer { display: flex; flex-direction: column; min-width: 0; min-height: 0; }

.toolbar {
  height: 50px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.toolbar button {
  width: 30px; height: 30px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  color: var(--text);
  font-size: 16px;
  line-height: 1;
}
.toolbar button:disabled { opacity: .4; cursor: not-allowed; }
#pageLabel { font-size: 12.5px; color: var(--muted); min-width: 54px; text-align: center; }
.toolbar .spacer { flex: 1; }
.zoom { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); }
.zoom input { width: 110px; accent-color: var(--accent); }

.canvas-scroll {
  flex: 1;
  overflow: auto;
  padding: 24px;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  min-height: 0;
}

.page-wrap {
  position: relative;
  background: #fff;
  box-shadow: 0 2px 6px rgba(16,24,40,.08), 0 18px 50px rgba(16,24,40,.10);
  border-radius: 4px;
  flex: none;
}
.page-wrap img { display: block; border-radius: 4px; user-select: none; }
#pageImg { border-radius: 4px; }

#overlay { position: absolute; inset: 0; }

#imgLayer { position: absolute; inset: 0; pointer-events: none; }

.img-obj {
  position: absolute;
  pointer-events: auto;
  cursor: move;
  touch-action: none;
}
.img-obj img {
  width: 100%;
  height: 100%;
  display: block;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
}
.img-obj.selected { outline: 2px solid var(--accent); }
.img-obj .handle {
  position: absolute;
  width: 11px;
  height: 11px;
  background: #fff;
  border: 2px solid var(--accent);
  border-radius: 50%;
  pointer-events: auto;
}
.handle.nw { left: -6px; top: -6px; cursor: nwse-resize; }
.handle.ne { right: -6px; top: -6px; cursor: nesw-resize; }
.handle.sw { left: -6px; bottom: -6px; cursor: nesw-resize; }
.handle.se { right: -6px; bottom: -6px; cursor: nwse-resize; }

.img-actions { display: flex; gap: 8px; margin-top: 8px; }

.span {
  position: absolute;
  cursor: text;
  border-radius: 2px;
  transition: background .08s ease, box-shadow .08s ease;
}
.span:hover {
  background: rgba(79, 70, 229, .10);
  box-shadow: 0 0 0 1.5px rgba(79, 70, 229, .55);
}
.span.selected {
  background: rgba(79, 70, 229, .14);
  box-shadow: 0 0 0 2px var(--accent);
}
.span.edited { background: #fff; }
.span.edited .newtext {
  position: absolute;
  left: 0;
  top: 0;
  white-space: nowrap;
  color: inherit;
  font-family: "NotoSC", "Microsoft YaHei", sans-serif;
  pointer-events: none;
}

/* ---------- misc ---------- */

#toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translate(-50%, 20px);
  background: #111827;
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: .22s ease;
  max-width: min(560px, 90vw);
  z-index: 60;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.err { background: #b91c1c; }

#busy {
  position: fixed;
  inset: 0;
  background: rgba(17, 24, 39, .32);
  display: none;
  place-items: center;
  z-index: 50;
}
#busy.show { display: grid; }
.busycard {
  background: #fff;
  border-radius: 14px;
  padding: 22px 28px;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: 13.5px;
}
.spinner {
  width: 20px; height: 20px;
  border: 2.5px solid #e5e7eb;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.notice {
  margin: 0 0 12px;
  padding: 9px 11px;
  border-radius: 9px;
  background: #fffbeb;
  border: 1px solid #fde68a;
  color: #92400e;
  font-size: 12.5px;
  line-height: 1.5;
}

@media (max-width: 900px) {
  #workspace { grid-template-columns: 1fr; grid-template-rows: auto minmax(0, 1fr); }
  .sidebar { border-right: 0; border-bottom: 1px solid var(--line); max-height: 42vh; }
  .brand p { display: none; }
  .canvas-scroll { padding: 12px; }
}
