@font-face {
  font-family: "HW Tibetan";
  src: url("NotoSansTibetan-Regular.ttf") format("truetype");
  font-display: swap;
  unicode-range: U+0F00-0FFF;
}

:root {
  color-scheme: light only;   /* 画板始终白底黑字，不让系统或浏览器的深色模式反色 */
  --bg: #f2f3f6;
  --surface: #ffffff;
  --ink: #15171c;
  --muted: #6c7385;
  --faint: #a6adbb;
  --line: #e3e6ec;
  --primary: #2f63f5;
  --primary-ink: #ffffff;
  --primary-soft: #e8eeff;
  --ok: #17a05d;
  --ok-soft: #e4f6ec;
  --warn: #b7791f;
  --warn-soft: #fdf3dc;
  --bad: #d5433f;
  --bad-soft: #fde9e8;
  --radius: 16px;
  --tap: 48px;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --ui: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "HarmonyOS Sans SC", "MiSans",
        "Noto Sans SC", "Microsoft YaHei", "Segoe UI", Roboto, system-ui, sans-serif;
  --script: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "HW Tibetan", "Noto Sans Tibetan",
            "Kailasa", "Microsoft Himalaya", "PingFang SC", system-ui, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none;
  background: var(--bg); color: var(--ink);
  font: 16px/1.45 var(--ui);
  -webkit-text-size-adjust: 100%; text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
button, input, textarea, select { font: inherit; color: inherit; }
button {
  border: 0; background: none; padding: 0; margin: 0; cursor: pointer;
  touch-action: manipulation; -webkit-user-select: none; user-select: none;
}
button:disabled { cursor: default; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.script { font-family: var(--script); }
.script.bo { line-height: 1.75; }

/* ---------------------------------------------------------------- 外壳 */
#app {
  position: fixed; inset: 0; height: 100vh; height: 100dvh;
  display: flex; flex-direction: column;
  padding: var(--sat) var(--sar) 0 var(--sal);
}
.top {
  flex: none; height: 52px; display: flex; align-items: center; gap: 8px; padding: 0 12px;
}
.pill {
  height: 36px; padding: 0 12px 0 14px; border-radius: 18px; background: var(--surface);
  display: inline-flex; align-items: center; gap: 6px; font-weight: 600; font-size: 15px;
  box-shadow: 0 1px 2px rgba(20, 24, 40, .06);
}
.pill::after {
  content: ""; width: 7px; height: 7px; margin-top: -3px;
  border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted); transform: rotate(45deg);
}
.tabs { margin: 0 auto; display: flex; background: #e6e8ee; border-radius: 12px; padding: 3px; }
.tabs button {
  height: 32px; padding: 0 16px; border-radius: 9px; font-size: 15px; font-weight: 600; color: var(--muted);
  position: relative;
}
.tabs button.on { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(20, 24, 40, .1); }
.tabs .badge {
  position: absolute; top: -4px; right: -4px; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--primary); color: #fff; font-size: 11px; line-height: 18px; font-weight: 700;
}
.avatar {
  width: 36px; height: 36px; border-radius: 50%; background: var(--primary-soft); color: var(--primary);
  font-weight: 700; font-size: 15px; flex: none;
}
.view { flex: 1; min-height: 0; }

/* ---------------------------------------------------------------- 书写 */
.write {
  display: grid; grid-template-rows: auto minmax(0, 1fr) auto; gap: 10px;
  grid-template-areas: "ask" "pad" "bar";
  padding: 2px 12px calc(10px + var(--sab));
  max-width: 1180px; width: 100%; margin: 0 auto;
}
.ask { grid-area: ask; min-width: 0; }
.ask-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.seg { display: inline-flex; background: #e6e8ee; border-radius: 10px; padding: 2px; }
.seg button { height: 30px; padding: 0 12px; border-radius: 8px; font-size: 14px; font-weight: 600; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(20, 24, 40, .1); }
.count { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.count b { color: var(--ink); font-weight: 700; }
.count .sync { color: var(--warn); margin-left: 8px; }

.ask-right { display: flex; align-items: center; gap: 10px; min-width: 0; }
.undo-last {
  height: 32px; padding: 0 12px; border-radius: 16px; background: var(--ok-soft); color: var(--ok); font-size: 14px; font-weight: 700;
  display: inline-flex; align-items: center; gap: 4px; animation: pop .22s ease-out; position: relative;
}
.undo-last::before { content: ""; position: absolute; inset: -8px -4px; }   /* 触摸范围比看上去大 */
.undo-last::after { content: "✓"; order: -1; }
@keyframes pop { from { transform: scale(.8); opacity: 0; } }

/* 词条区高度固定：画板大小不随词条长短变化 */
.ask-body { height: 100px; }
.prompt {
  height: 100%; background: var(--surface); border-radius: var(--radius);
  display: flex; align-items: center; gap: 8px; padding: 8px 8px 8px 18px;
  box-shadow: 0 1px 2px rgba(20, 24, 40, .05);
}
.prompt-text {
  flex: 1; min-width: 0; max-height: 100%; font-weight: 500; word-break: break-word;
  font-size: 34px; line-height: 1.25; -webkit-user-select: none; user-select: none;   /* 字号由 fitPrompt 按可用空间定 */
}
.prompt-text.bo { line-height: 1.7; }   /* 藏文上下叠字高，行距留足，不被裁切 */
.prompt-text.empty { font-size: 16px; color: var(--muted); font-weight: 400; }
.skip {
  flex: none; height: var(--tap); padding: 0 12px; border-radius: 12px; color: var(--muted); font-size: 14px; font-weight: 600;
}
.skip:active { background: var(--bg); }

.free {
  height: 100%; background: var(--surface); border-radius: var(--radius); padding: 8px 12px;
  display: flex; flex-direction: column; box-shadow: 0 1px 2px rgba(20, 24, 40, .05);
}
.free textarea {
  flex: 1; min-height: 0; display: block; width: 100%; border: 0; outline: 0; resize: none; background: transparent;
  font-family: var(--script); font-size: 24px; line-height: 1.4; padding: 2px 4px; overflow-y: auto;
}
.free textarea.bo { line-height: 1.6; }
.free textarea::placeholder { font-size: 16px; color: var(--faint); font-family: var(--ui); }
.chips { flex: none; display: flex; gap: 8px; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; padding-top: 4px; }
.chips::-webkit-scrollbar { display: none; }
.chips:empty { display: none; }
.chip {
  flex: none; height: 36px; padding: 0 14px; border-radius: 18px; background: var(--bg); font-family: var(--script);
  font-size: 18px; white-space: nowrap;
}
.chip:active { background: var(--primary-soft); }

.padwrap {
  grid-area: pad; position: relative; min-height: 120px; border-radius: 20px; background-color: #fff;
  box-shadow: 0 1px 3px rgba(20, 24, 40, .08), inset 0 0 0 1px var(--line);
  overflow: hidden; touch-action: none;
  background-image: repeating-linear-gradient(to bottom, transparent 0, transparent calc(var(--rule, 96px) - 1px),
                    #e9ecf2 calc(var(--rule, 96px) - 1px), #e9ecf2 var(--rule, 96px));
  background-position: 0 calc(var(--rule, 96px) * -0.28);
}
#pad {
  position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; cursor: crosshair;
}
.pad-hint {
  position: absolute; inset: 0; display: grid; place-items: center; color: #c3c8d3; font-size: 18px;
  pointer-events: none; transition: opacity .15s; -webkit-user-select: none; user-select: none;
}
.padwrap.inked .pad-hint { opacity: 0; }
.padwrap.flash { animation: flash .28s ease-out; }
@keyframes flash { from { box-shadow: 0 0 0 3px var(--ok), 0 1px 3px rgba(20, 24, 40, .08); } }

.bar { grid-area: bar; display: flex; gap: 10px; align-items: stretch; }
.btn {
  height: 56px; min-width: 76px; padding: 0 16px; border-radius: 16px; background: var(--surface);
  font-size: 16px; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  box-shadow: 0 1px 2px rgba(20, 24, 40, .06); transition: transform .06s, opacity .15s, background .15s;
}
.btn:active:not(:disabled) { transform: scale(.97); }
.btn:disabled { opacity: .4; }
.btn svg { width: 22px; height: 22px; flex: none; }
.btn.primary { flex: 1; background: var(--primary); color: var(--primary-ink); font-size: 18px; box-shadow: 0 4px 14px rgba(47, 99, 245, .28); }
.btn.primary:disabled { box-shadow: none; }
.btn.ghost { background: transparent; box-shadow: none; color: var(--muted); }
.btn.danger { color: var(--bad); }
.btn.small { height: 44px; min-width: 0; border-radius: 12px; font-size: 15px; }

/* 手机横屏：高度紧，控件挪到右侧，画板占满左边 */
@media (orientation: landscape) and (max-height: 520px) {
  .top { height: 44px; }
  .write {
    grid-template-columns: minmax(0, 1fr) minmax(210px, 30%); grid-template-rows: minmax(0, 1fr) auto;
    grid-template-areas: "pad ask" "pad bar"; padding-bottom: calc(8px + var(--sab));
  }
  .ask { display: flex; flex-direction: column; min-height: 0; }
  .ask-body { flex: 1; min-height: 0; height: auto; }
  .prompt { flex-direction: column; align-items: stretch; justify-content: center; padding: 10px 12px; }
  .prompt-text { flex: 0 1 auto; min-height: 0; }
  .skip { align-self: flex-end; height: 40px; }
  .bar { flex-wrap: wrap; }
  .bar .btn { flex: 1; height: 48px; min-width: 0; }
  .bar .btn.primary { flex-basis: 100%; height: 54px; order: 3; }
}
/* 平板与桌面：留白大一些，字大一些 */
@media (min-width: 760px) and (min-height: 600px) {
  .top { height: 60px; padding: 0 20px; }
  .write { padding: 4px 20px calc(18px + var(--sab)); gap: 14px; }
  .ask-body { height: 132px; }
  .prompt { padding: 12px 12px 12px 26px; }
  .free textarea { font-size: 30px; }
  .btn { height: 60px; min-width: 104px; }
  .bar .btn.primary { flex: 0 1 360px; margin-left: auto; }
}

/* ---------------------------------------------------------------- 校对 */
.scroll { overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.page { max-width: 1180px; margin: 0 auto; padding: 4px 12px calc(28px + var(--sab)); }
.filters { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 10px; position: sticky; top: 0;
  background: var(--bg); padding: 4px 0 8px; z-index: 2; }
.filters .seg { overflow-x: auto; max-width: 100%; scrollbar-width: none; }
.filters .seg button { white-space: nowrap; }
.filters .seg .n { font-weight: 500; color: var(--faint); margin-left: 4px; font-variant-numeric: tabular-nums; }
.filters .seg .on .n { color: var(--muted); }
.grid { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(min(100%, 300px), 1fr)); }
.card {
  background: var(--surface); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column;
  box-shadow: 0 1px 2px rgba(20, 24, 40, .05); transition: opacity .18s, transform .18s;
}
.card.gone { opacity: 0; transform: scale(.96); }
.card-open { display: block; width: 100%; text-align: left; }
.card-img { display: block; width: 100%; height: 132px; object-fit: contain; background: #fff; border-bottom: 1px solid var(--line); }
.card-body { padding: 10px 14px 4px; }
.card-text { font-size: 24px; line-height: 1.35; word-break: break-word; white-space: pre-wrap; }
.card-text.bo { line-height: 1.65; }
.card-text.none { font-size: 15px; color: var(--faint); font-family: var(--ui); }
.card-pred { margin-top: 2px; font-size: 15px; color: var(--muted); word-break: break-word; white-space: pre-wrap; }
.card-pred.diff { color: var(--warn); }
.card-pred span { font-family: var(--ui); font-size: 12px; margin-right: 6px; opacity: .8; }
.card-meta { padding: 4px 14px 0; font-size: 12px; color: var(--faint); }
.card-act { display: flex; gap: 8px; padding: 10px; margin-top: auto; }
.card-act button { flex: 1; height: 46px; border-radius: 12px; font-size: 15px; font-weight: 600; background: var(--bg); color: var(--muted); }
.card-act .yes { background: var(--ok-soft); color: var(--ok); flex: 1.6; }
.card-act .no:active { background: var(--bad-soft); color: var(--bad); }
.card-act .yes:active { background: var(--ok); color: #fff; }
.empty { text-align: center; color: var(--faint); padding: 64px 16px; font-size: 15px; }
.more { display: block; margin: 16px auto 0; }
.bulk {
  margin-left: auto; height: 36px; padding: 0 14px; border-radius: 10px; background: var(--ok); color: #fff;
  font-size: 14px; font-weight: 700; white-space: nowrap;
}
.bulk:active { opacity: .85; }

/* ---------------------------------------------------------------- 管理 */
.panel { background: var(--surface); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; box-shadow: 0 1px 2px rgba(20, 24, 40, .05); }
.panel h2 { margin: 0 0 12px; font-size: 16px; }
.table { width: 100%; border-collapse: collapse; font-size: 15px; }
.table th { text-align: right; font-weight: 500; color: var(--muted); font-size: 13px; padding: 4px 6px 8px; white-space: nowrap; }
.table td { text-align: right; padding: 10px 6px; border-top: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.table th:first-child, .table td:first-child { text-align: left; }
.table td .btn.small { height: 36px; padding: 0 12px; background: var(--primary-soft); color: var(--primary); box-shadow: none; }
.tablewrap { overflow-x: auto; }
.field { display: block; margin-bottom: 10px; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.input, .field select, .field textarea {
  width: 100%; height: 48px; padding: 0 14px; border-radius: 12px; border: 1px solid var(--line); background: #fff;
  font-size: 16px; outline: 0;
}
.field textarea { height: 140px; padding: 10px 14px; resize: vertical; font-family: var(--script); }
.input:focus, .field select:focus, .field textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.check { display: flex; align-items: center; gap: 8px; font-size: 15px; margin-bottom: 12px; }
.check input { width: 20px; height: 20px; }
.files a { display: flex; justify-content: space-between; gap: 12px; padding: 10px 0; border-top: 1px solid var(--line);
  color: var(--primary); text-decoration: none; font-size: 15px; }
.files a span { color: var(--faint); white-space: nowrap; }

/* ---------------------------------------------------------------- 登录 */
#login {
  position: fixed; inset: 0; display: grid; place-items: center; padding: 24px; background: var(--bg); z-index: 30;
  overflow-y: auto;
}
.login-card { width: 100%; max-width: 380px; }
.login-card h1 { font-size: 26px; margin: 0 0 6px; letter-spacing: .01em; }
.login-card p { margin: 0 0 24px; color: var(--muted); font-size: 15px; }
.login-card .btn.primary { width: 100%; margin-top: 8px; }
.err { color: var(--bad); font-size: 14px; min-height: 20px; margin: 4px 2px 0; }

/* ---------------------------------------------------------------- 弹层 */
.scrim { position: fixed; inset: 0; background: rgba(15, 18, 28, .42); z-index: 20; opacity: 0; transition: opacity .2s; pointer-events: none; }
.scrim.show { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21; background: var(--surface);
  border-radius: 22px 22px 0 0; padding: 10px 16px calc(16px + var(--sab));
  max-height: calc(100dvh - 24px - var(--sat)); overflow-y: auto; overscroll-behavior: contain;
  transform: translateY(102%); transition: transform .24s cubic-bezier(.2, .8, .2, 1);
  box-shadow: 0 -8px 30px rgba(15, 18, 28, .18); pointer-events: none;
}
.sheet.show { transform: none; pointer-events: auto; }
.sheet::before { content: ""; display: block; width: 40px; height: 4px; border-radius: 2px; background: var(--line); margin: 0 auto 12px; }
@media (min-width: 760px) {
  .sheet { left: 50%; right: auto; bottom: auto; top: 50%; width: min(640px, calc(100vw - 48px)); border-radius: 22px;
    transform: translate(-50%, -46%); opacity: 0; transition: transform .2s, opacity .2s; padding: 20px; }
  .sheet.show { transform: translate(-50%, -50%); opacity: 1; }
  .sheet::before { display: none; }
}
.sheet h3 { margin: 0 0 12px; font-size: 17px; }
.opt {
  display: flex; align-items: center; justify-content: space-between; width: 100%; min-height: 60px; padding: 8px 14px;
  border-radius: 14px; text-align: left; font-size: 17px;
}
.opt + .opt { margin-top: 4px; }
.opt small { color: var(--muted); font-size: 14px; }
.opt.on { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.opt.on small { color: var(--primary); }
.opt:active { background: var(--bg); }
.ed-img { display: block; width: 100%; max-height: 38vh; object-fit: contain; background: #fff; border: 1px solid var(--line); border-radius: 14px; }
.ed-text {
  display: block; width: 100%; margin-top: 12px; padding: 10px 14px; border-radius: 14px; border: 1px solid var(--line);
  font-family: var(--script); font-size: 26px; line-height: 1.4; resize: none; outline: 0; min-height: 58px;
}
.ed-text.bo { line-height: 1.7; }
.ed-text:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.ed-meta { font-size: 13px; color: var(--faint); margin-top: 8px; }
.ed-act { display: flex; gap: 10px; margin-top: 14px; }
.ed-act .btn { background: var(--bg); box-shadow: none; }
.ed-act .btn.primary { background: var(--ok); color: #fff; box-shadow: 0 4px 14px rgba(23, 160, 93, .28); }

/* ---------------------------------------------------------------- 提示 */
#toast {
  position: fixed; left: 50%; bottom: calc(86px + var(--sab)); transform: translate(-50%, 12px); z-index: 40;
  display: flex; align-items: center; gap: 4px; max-width: calc(100vw - 32px);
  background: rgba(22, 25, 34, .94); color: #fff; border-radius: 26px; padding: 0 6px 0 18px; min-height: 48px;
  font-size: 15px; opacity: 0; pointer-events: none; transition: opacity .18s, transform .18s; white-space: nowrap;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
#toast.plain { padding-right: 18px; }
#toast button { height: 40px; padding: 0 14px; border-radius: 20px; color: #8fb0ff; font-weight: 700; font-size: 15px; }
@media (orientation: landscape) and (max-height: 520px) { #toast { bottom: calc(12px + var(--sab)); left: 35%; } }

@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
