:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --panel-2: #fafbfc;
  --line: #e3e7ec;
  --line-strong: #d3d9e0;
  --text: #16202b;
  --text-2: #3d4a58;
  --muted: #788593;
  --accent: #2563eb;
  --accent-2: #1d4ed8;
  --accent-soft: #eaf1ff;
  --hl: #ffd84d;
  --hl-soft: #fff6d1;
  --danger: #d33a3a;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 8px 24px rgba(16, 24, 40, .06);
  --radius: 10px;
  --ui: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", "Microsoft YaHei", sans-serif;
  --serif: "Iowan Old Style", "Source Serif Pro", Georgia, "Songti SC", "Noto Serif SC", serif;
}

[data-theme="dark"] {
  --bg: #0e1116;
  --panel: #161a21;
  --panel-2: #1a1f27;
  --line: #262c36;
  --line-strong: #333b48;
  --text: #e8ecf2;
  --text-2: #c2cad6;
  --muted: #8a94a3;
  --accent: #5b9bff;
  --accent-2: #7cb0ff;
  --accent-soft: #1a2740;
  --hl: #b9901f;
  --hl-soft: #33290c;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 12px 32px rgba(0, 0, 0, .28);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--ui);
  font-size: 14px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; color: inherit; }

.hidden { display: none !important; }

/* ------------------------------- topbar -------------------------------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  z-index: 20;
  flex: 0 0 auto;
}

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

.brand .logo {
  width: 34px; height: 34px;
  display: grid; place-items: center;
  border-radius: 9px;
  background: linear-gradient(140deg, #2563eb, #4f46e5);
  color: #fff; font-size: 17px;
  box-shadow: 0 2px 8px rgba(37, 99, 235, .3);
}

.brand h1 { font-size: 14.5px; margin: 0; font-weight: 650; letter-spacing: .01em; }
.brand p { margin: 1px 0 0; font-size: 11px; color: var(--muted); }

.doc-meta { min-width: 0; flex: 1 1 auto; display: flex; align-items: center; gap: 8px; }

.chip {
  display: inline-flex; align-items: center; gap: 6px;
  max-width: 100%;
  padding: 4px 9px;
  border-radius: 999px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chip strong { color: var(--text-2); font-weight: 600; }
.chip .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); }

.actions { display: flex; align-items: center; gap: 8px; flex: 0 0 auto; }

.btn {
  appearance: none; border: 1px solid transparent;
  background: var(--panel-2);
  border-radius: 8px;
  padding: 7px 13px;
  font-size: 13px; font-weight: 550;
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .05s, opacity .15s;
  display: inline-flex; align-items: center; gap: 6px;
  white-space: nowrap;
}
.btn:hover { background: var(--accent-soft); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-2); }
.btn.ghost { background: transparent; border-color: var(--line); }
.btn.ghost:hover { background: var(--panel-2); border-color: var(--line-strong); }
.btn.danger { background: transparent; border-color: currentColor; color: var(--danger); }
.btn.danger:hover { background: rgba(211, 58, 58, .08); }
.btn.icon { padding: 7px 9px; font-size: 15px; line-height: 1; }

.select {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--panel-2);
  border-radius: 8px;
  padding: 7px 26px 7px 11px;
  font-size: 13px; cursor: pointer;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 14px) 52%, calc(100% - 9px) 52%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}

.menu { position: relative; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 6px);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 5px; min-width: 190px; z-index: 50;
}
.menu-list button {
  display: block; width: 100%; text-align: left;
  border: 0; background: transparent; border-radius: 7px;
  padding: 8px 10px; font-size: 13px; cursor: pointer;
}
.menu-list button:hover { background: var(--accent-soft); }
.menu-list .hint { font-size: 11px; color: var(--muted); padding: 2px 10px 6px; }

/* ------------------------------ progress ------------------------------- */
.progress {
  display: flex; align-items: center; gap: 12px;
  padding: 6px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  font-size: 12px; color: var(--muted);
  flex: 0 0 auto;
}
.progress-track { flex: 1; height: 4px; background: var(--line); border-radius: 999px; overflow: hidden; }
.progress-bar { height: 100%; width: 0; background: linear-gradient(90deg, var(--accent), #7c3aed); transition: width .25s ease; }

/* ------------------------------ workspace ------------------------------ */
.workspace {
  flex: 1 1 auto;
  display: flex;
  min-height: 0;
  overflow: hidden;
}

.pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--bg); }
.pane-src { flex: 0 0 50%; }
.pane-dst { flex: 1 1 50%; background: var(--panel); }

.splitter {
  flex: 0 0 6px;
  cursor: col-resize;
  background: var(--line);
  position: relative;
}
.splitter::after {
  content: ""; position: absolute; inset: 0 2px;
  background: transparent; transition: background .15s;
}
.splitter:hover::after { background: var(--accent); opacity: .5; }

.pane-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px;
  height: 38px; padding: 0 14px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  font-size: 12px; font-weight: 600; letter-spacing: .04em;
  color: var(--text-2);
  text-transform: uppercase;
  flex: 0 0 auto;
}
.pane-tools { display: flex; align-items: center; gap: 12px; font-weight: 400; text-transform: none; letter-spacing: 0; color: var(--muted); font-size: 12px; }
.pane-tools label { display: inline-flex; align-items: center; gap: 5px; cursor: pointer; user-select: none; }
.pane-tools input[type="checkbox"] { accent-color: var(--accent); margin: 0; }
.pane-tools .sep { width: 1px; height: 14px; background: var(--line); }

.mini {
  border: 1px solid var(--line);
  background: var(--panel-2);
  border-radius: 6px;
  min-width: 22px; height: 22px;
  display: inline-grid; place-items: center;
  padding: 0 4px;
  font-size: 12px;
  color: var(--text-2);
  cursor: pointer;
}
.mini:hover { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.mini.wide { padding: 0 7px; }
.zoom { min-width: 40px; text-align: center; font-variant-numeric: tabular-nums; font-size: 11.5px; }
.zoom-input {
  width: 52px; height: 22px;
  text-align: center;
  border: 1px solid var(--line);
  background: var(--panel-2);
  border-radius: 6px;
  padding: 0 2px;
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
  outline: none;
}
.zoom-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); background: var(--panel); }

.pane-body { flex: 1 1 auto; overflow: auto; min-height: 0; scroll-behavior: auto; }
.pane-src .pane-body { padding: 18px 14px 60px; background: var(--bg); }
.pane-dst .pane-body { padding: 0 0 80px; position: relative; }

/* -------------------------------- empty -------------------------------- */
.empty {
  max-width: 560px;
  margin: 8vh auto 0;
  text-align: center;
  padding: 32px;
  border: 1.5px dashed var(--line-strong);
  border-radius: 16px;
  background: var(--panel);
}
.empty .big { font-size: 34px; margin-bottom: 10px; }
.empty h2 { margin: 0 0 6px; font-size: 17px; }
.empty p { margin: 0 auto 18px; color: var(--muted); line-height: 1.65; max-width: 420px; }
.empty .row { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.empty kbd {
  font-family: ui-monospace, monospace; font-size: 11px;
  border: 1px solid var(--line-strong); border-bottom-width: 2px;
  border-radius: 5px; padding: 1px 5px; background: var(--panel-2);
}

/* --------------------------------- pdf --------------------------------- */
.pdf-stack {
  display: flex; flex-direction: column; align-items: center; gap: 16px;
  width: max-content; min-width: 100%;
}
#viewer.pdf-mode { cursor: grab; }
#viewer.pdf-mode.panning { cursor: grabbing; user-select: none; }
#viewer.pdf-mode.panning * { pointer-events: none; }

.view-switch {
  display: inline-flex;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: var(--panel-2);
  flex: 0 0 auto;
}
.vs-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  padding: 5px 11px;
  cursor: pointer;
  line-height: 1.5;
}
.vs-btn:hover { color: var(--text); }
.vs-btn.active { background: var(--accent); color: #fff; }
.vs-btn:disabled { opacity: .45; cursor: not-allowed; }

/* ---- layout (版式) view: translation typeset into the original geometry ---- */
.lay-stack {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  padding: 18px 14px 60px;
  width: max-content;
  min-width: 100%;
}
.lay-page {
  position: relative;
  background: #fff;
  border-radius: 2px;
  box-shadow: var(--shadow);
  overflow: hidden;
  flex: 0 0 auto;
}
.lay-seg {
  position: absolute;
  color: #14181f;
  overflow: hidden;
  font-family: "PingFang SC", "Hiragino Sans GB", "Noto Sans CJK SC", "Microsoft YaHei", system-ui, sans-serif;
  word-break: break-word;
  overflow-wrap: anywhere;
  cursor: text;
  border-radius: 2px;
}
.lay-seg:hover { background: rgba(56, 132, 255, .07); }
.lay-seg.untranslated { color: #98a1b0; }
.lay-seg.active { outline: 2px solid var(--accent); outline-offset: 1px; background: rgba(56, 132, 255, .1); }
[data-theme="dark"] .lay-page { background: #171b21; }
[data-theme="dark"] .lay-seg { color: #e3e8ef; }
[data-theme="dark"] .lay-seg.untranslated { color: #6d7789; }

.pane-body::-webkit-scrollbar { width: 11px; height: 11px; }
.pane-body::-webkit-scrollbar-thumb {
  background: var(--line-strong); border-radius: 8px;
  border: 2px solid transparent; background-clip: padding-box;
}
.pane-body::-webkit-scrollbar-thumb:hover { background: var(--muted); background-clip: padding-box; }
.pane-body::-webkit-scrollbar-track { background: transparent; }
.pane-body::-webkit-scrollbar-corner { background: transparent; }

.pdf-page {
  position: relative;
  background: #fff;
  box-shadow: 0 1px 3px rgba(16,24,40,.12), 0 10px 28px rgba(16,24,40,.08);
  border-radius: 3px;
  overflow: hidden;
  flex: 0 0 auto;
}
[data-theme="dark"] .pdf-page { box-shadow: 0 1px 3px rgba(0,0,0,.5); }
.pdf-page canvas { display: block; width: 100%; height: 100%; }
.pdf-page .page-no {
  position: absolute; right: 8px; bottom: 6px;
  font-size: 10px; color: #9aa4ae;
  background: rgba(255,255,255,.85); border-radius: 4px; padding: 1px 5px;
  pointer-events: none;
}
.pdf-ph {
  position: absolute; inset: 0; display: grid; place-items: center;
  color: #c3cad2; font-size: 12px;
}
.hl-layer { position: absolute; inset: 0; pointer-events: none; }
.hl {
  position: absolute;
  background: rgba(255, 208, 0, .38);
  border-radius: 2px;
  box-shadow: 0 0 0 1.5px rgba(230, 160, 0, .55);
  animation: hl-in .18s ease-out;
}
@keyframes hl-in { from { opacity: 0; transform: scaleY(.7); } to { opacity: 1; transform: none; } }

/* ----------------------------- text doc -------------------------------- */
.doc-flow { max-width: 720px; margin: 0 auto; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 26px 30px 40px; box-shadow: var(--shadow); }
.doc-flow .para { margin: 0 0 12px; line-height: 1.8; color: var(--text-2); font-family: var(--serif); font-size: 15px; cursor: pointer; border-radius: 6px; padding: 3px 8px; margin-left: -8px; margin-right: -8px; transition: background .15s; }
.doc-flow .para:hover { background: var(--panel-2); }
.doc-flow .para.active { background: var(--hl-soft); }
.doc-flow h1.para { font-size: 24px; font-weight: 700; color: var(--text); }
.doc-flow h2.para { font-size: 19px; font-weight: 650; color: var(--text); }
.doc-flow h3.para { font-size: 16.5px; font-weight: 650; color: var(--text); }
.doc-flow .para.li { padding-left: 26px; position: relative; }
.doc-flow .para.li::before { content: "•"; position: absolute; left: 9px; color: var(--muted); }
.doc-flow .para.quote { border-left: 3px solid var(--line-strong); padding-left: 14px; color: var(--muted); }

/* ------------------------------- segments ------------------------------ */
.seg-list { display: flex; flex-direction: column; }

.seg {
  display: grid;
  grid-template-columns: 42px 1fr;
  gap: 0 4px;
  padding: 14px 18px 14px 0;
  border-bottom: 1px solid var(--line);
  position: relative;
  transition: background .12s;
}
.seg:hover { background: var(--panel-2); }
.seg.active { background: var(--accent-soft); }
.seg.active::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px;
  background: var(--accent);
}

.seg-num {
  font-size: 11px; color: var(--muted);
  text-align: right; padding-top: 4px;
  font-variant-numeric: tabular-nums;
  user-select: none;
}
.seg-num .pg { display: block; font-size: 10px; opacity: .75; }

.seg-main { min-width: 0; }

.seg-src {
  font-size: 12.5px; line-height: 1.65; color: var(--muted);
  margin: 0 0 7px; font-family: var(--serif);
  white-space: pre-wrap;
}
.seg-src.hidden { display: none; }

.seg-dst {
  font-size: 15px; line-height: 1.8; color: var(--text);
  white-space: pre-wrap;
  outline: none;
  border-radius: 6px;
  padding: 2px 6px; margin: -2px -6px;
  min-height: 1.8em;
}
.seg-dst:focus { background: var(--panel); box-shadow: 0 0 0 1.5px var(--accent); }
.seg-dst:empty::before {
  content: attr(data-ph);
  color: var(--muted);
  font-style: italic;
  pointer-events: none;
}
.seg-dst.placeholder { color: var(--muted); font-style: italic; }
.seg-dst.error { color: var(--danger); }

.seg-head { display: flex; align-items: center; gap: 6px; margin-bottom: 4px; }
.seg-type {
  font-size: 10px; text-transform: uppercase; letter-spacing: .05em;
  color: var(--muted); border: 1px solid var(--line); border-radius: 4px;
  padding: 0 4px; line-height: 15px;
}
.seg-actions {
  position: absolute; right: 12px; top: 10px;
  display: flex; gap: 2px;
  opacity: 0; transition: opacity .12s;
  background: var(--panel); border: 1px solid var(--line); border-radius: 7px; padding: 2px;
  box-shadow: var(--shadow);
}
.seg:hover .seg-actions, .seg.active .seg-actions { opacity: 1; }
.seg-actions button {
  border: 0; background: transparent; cursor: pointer;
  font-size: 12px; padding: 3px 7px; border-radius: 5px; color: var(--muted);
}
.seg-actions button:hover { background: var(--accent-soft); color: var(--accent); }

.seg.working .seg-dst { color: var(--muted); }
.seg.working .seg-dst::after {
  content: ""; display: inline-block; width: 7px; height: 7px; margin-left: 6px;
  border-radius: 50%; background: var(--accent);
  animation: pulse 1s infinite;
}
@keyframes pulse { 0%,100% { opacity: .25; } 50% { opacity: 1; } }

/* -------------------------------- status ------------------------------- */
.statusbar {
  flex: 0 0 auto;
  display: flex; align-items: center; gap: 14px;
  height: 28px; padding: 0 16px;
  background: var(--panel);
  border-top: 1px solid var(--line);
  font-size: 11.5px; color: var(--muted);
  white-space: nowrap; overflow: hidden;
}
.statusbar > span { flex: 0 0 auto; }
.statusbar .spacer { flex: 1 1 auto; }
#statusDoc {
  flex: 0 1 auto;
  min-width: 0;
  max-width: 46vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.statusbar .ok { color: #157f3c; }
[data-theme="dark"] .statusbar .ok { color: #4ac47a; }
.statusbar .warn { color: #b06a00; }

/* ------------------------------- dialog -------------------------------- */
dialog {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 14px;
  padding: 0;
  width: min(620px, calc(100vw - 32px));
  box-shadow: 0 24px 64px rgba(16,24,40,.24);
}
dialog::backdrop { background: rgba(10, 15, 22, .45); backdrop-filter: blur(2px); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.dlg-head h2 { margin: 0; font-size: 15px; }
.dlg-body { padding: 16px 20px; max-height: 70vh; overflow: auto; }
.dlg-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 20px; border-top: 1px solid var(--line); background: var(--panel-2); border-radius: 0 0 14px 14px; }

.field { margin-bottom: 14px; }
.field > label { display: block; font-size: 12px; font-weight: 600; color: var(--text-2); margin-bottom: 5px; }
.field .tip { font-size: 11px; color: var(--muted); margin-top: 4px; line-height: 1.5; }
.field input[type="text"], .field input[type="password"], .field input[type="number"], .field select, .field textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--panel-2);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  outline: none;
}
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.field textarea { resize: vertical; min-height: 70px; font-family: ui-monospace, monospace; font-size: 12px; line-height: 1.6; }
.field .input-row { display: flex; gap: 8px; align-items: center; }
.field .input-row input, .field .input-row select { flex: 1 1 auto; min-width: 0; }
.field .input-row .select { width: auto; }
.model-select { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }
.model-select:disabled { opacity: .6; }
.btn.sm { padding: 7px 10px; font-size: 12px; white-space: nowrap; }
.tip.conn { min-height: 18px; }
.tip.conn.ok { color: #157f3c; }
.tip.conn.err { color: var(--danger); }
.tip.conn.busy { color: var(--accent); }
.tip.conn.mock { color: #b06a00; }
[data-theme="dark"] .tip.conn.ok { color: #4ac47a; }
[data-theme="dark"] .tip.conn.mock { color: #e0a33a; }
input[type="text"][list] { background-image: none; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.row-check { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--text-2); }

/* ------------------------------- overlay ------------------------------- */
.drop-overlay {
  position: fixed; inset: 0; z-index: 100;
  display: grid; place-items: center;
  background: rgba(37, 99, 235, .10);
  border: 3px dashed var(--accent);
  color: var(--accent);
  font-size: 18px; font-weight: 600;
  backdrop-filter: blur(1.5px);
  pointer-events: none;
}

.toast {
  position: fixed; left: 50%; bottom: 44px; transform: translateX(-50%) translateY(8px);
  background: #16202b; color: #fff;
  padding: 9px 16px; border-radius: 9px; font-size: 13px;
  opacity: 0; transition: opacity .2s, transform .2s;
  pointer-events: none; z-index: 200; max-width: 70vw;
  box-shadow: 0 8px 24px rgba(0,0,0,.25);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.err { background: #a12b2b; }

/* ------------------------------- responsive ---------------------------- */
@media (max-width: 900px) {
  body { overflow: auto; overflow-x: hidden; }
  .workspace { flex-direction: column; overflow: visible; }
  .pane { flex: none !important; }
  .pane-src .pane-body, .pane-dst .pane-body { max-height: 62vh; }
  .splitter { display: none; }
  .topbar { flex-wrap: wrap; height: auto; padding: 8px 12px; gap: 10px 8px; }
  .brand { flex: 1 1 auto; }
  .actions { order: 2; flex-wrap: wrap; width: 100%; gap: 6px; }
  .actions .select { flex: 1 1 110px; min-width: 0; }
  .actions .btn.primary { flex: 1 1 auto; justify-content: center; }
  .doc-meta { order: 3; width: 100%; }
  .doc-meta .chip { max-width: 100%; }
  .pane-head { padding: 0 10px; }
  .pane-tools { gap: 6px; }
  #zoomActual, #zoomFit { display: none; }
  .zoom-input { width: 46px; }
  .seg { padding-right: 12px; grid-template-columns: 30px 1fr; }
  .seg-actions { opacity: 1; }
  .doc-flow { padding: 20px 18px 30px; }
  .statusbar { gap: 10px; padding: 0 10px; }
  #statusDoc { max-width: 52vw; }
  #statusCache { display: none; }
}
