:root {
  color-scheme: light dark;
  --fs: 26px;
  --bg: #faf7f0;
  --surface: #ffffff;
  --ink: #1c1b19;
  --soft: #4a4740;
  --line: #ddd6c7;
  --accent: #0b6b52;
  --accent-ink: #ffffff;
  --code: #f0ebdf;
  --danger: #b3261e;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17181a;
    --surface: #222428;
    --ink: #f2efe8;
    --soft: #c9c5bc;
    --line: #3a3c41;
    --accent: #5ad1ac;
    --accent-ink: #06261d;
    --code: #2b2d32;
    --danger: #ff8a80;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 20px;
  -webkit-font-smoothing: antialiased;
}

button {
  min-width: 54px;
  min-height: 54px;
  padding: 0 16px;
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: transform .08s ease, opacity .15s ease;
}
button:active { transform: scale(.96); }
button:disabled { opacity: .4; }
button.primary {
  padding: 0 24px;
  font-weight: 600;
  color: var(--accent-ink);
  background: var(--accent);
  border-color: var(--accent);
}
button:focus-visible, textarea:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

/* 顶部工具条 */
.bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) calc(20px + env(safe-area-inset-right)) 10px calc(20px + env(safe-area-inset-left));
  line-height: 1.2;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.brand {
  padding: 0 4px;
  font-size: 26px;
  font-weight: 700;
  color: var(--accent);
  background: none;
  border: 0;
}
.tools { display: flex; align-items: center; gap: 10px; }
.size { display: flex; align-items: center; gap: 2px; }
.size-now { min-width: 38px; font-variant-numeric: tabular-nums; text-align: center; }
#copy { min-width: 92px; }
#copy.done { color: var(--accent); border-color: var(--accent); font-weight: 600; }

/* 阅读区：字号都跟着 --fs 走 */
main {
  max-width: 40em;
  margin: 0 auto;
  padding: .7em max(20px, env(safe-area-inset-right)) 4em max(20px, env(safe-area-inset-left));
  font-size: var(--fs);
  line-height: 1.8;
}

#welcome h1 { margin: .5em 0 .5em; font-size: 1.3em; line-height: 1.35; }
.demo {
  margin: .8em 0;
  padding: .7em 1em;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
}
.demo code {
  display: block;
  color: var(--soft);
  font: .72em/1.5 ui-monospace, "SF Mono", Menlo, monospace;
  overflow-wrap: anywhere;
}
.demo .arrow { display: block; margin: .15em 0; color: var(--accent); font-weight: 700; line-height: 1; text-align: center; }
.steps { margin: .8em 0; padding-left: 1.5em; }
.steps li { margin: .25em 0; }

#paste-box { margin: 1em 0; }
#paste-box label { display: block; margin-bottom: .3em; color: var(--soft); font-size: .8em; }
#paste-area {
  display: block;
  width: 100%;
  min-height: 5em;
  padding: .7em .9em;
  color: var(--ink);
  background: var(--surface);
  border: 2px dashed var(--line);
  border-radius: 16px;
  font: inherit;
  font-size: .85em;
  line-height: 1.5;
  resize: vertical;
}
#translate-typed { margin-top: .5em; font-size: 20px; }

.tips { color: var(--soft); font-size: .72em; line-height: 1.65; }
.tips p { margin: .5em 0; }

/* 翻译结果 */
#result { overflow-wrap: break-word; }
#result h1, #result h2, #result h3, #result h4 { margin: 1.1em 0 .45em; font-weight: 700; line-height: 1.35; }
#result h1 { font-size: 1.4em; }
#result h2 { font-size: 1.25em; }
#result h3 { font-size: 1.12em; }
#result h4 { font-size: 1em; }
#result > :first-child { margin-top: .2em; }
#result p { margin: .7em 0; }
#result ul, #result ol { margin: .6em 0; padding-left: 1.4em; }
#result li { margin: .3em 0; }
#result strong, #result b { font-weight: 700; }
#result a { color: var(--accent); }
#result code {
  padding: .1em .35em;
  background: var(--code);
  border-radius: .3em;
  font: .88em ui-monospace, "SF Mono", Menlo, monospace;
}
#result pre {
  padding: .8em 1em;
  overflow-x: auto;
  background: var(--code);
  border-radius: .6em;
  line-height: 1.5;
}
#result pre code { padding: 0; background: none; }
#result blockquote { margin: .8em 0; padding: .1em 0 .1em .9em; border-left: .2em solid var(--accent); }
#result hr { margin: 1.4em 0; border: 0; border-top: 1px solid var(--line); }
#result .scroll { margin: .8em 0; overflow-x: auto; }
#result table { border-collapse: collapse; }
#result th, #result td { padding: .35em .7em; border: 1px solid var(--line); text-align: left; }
#result th { background: var(--code); font-weight: 700; }
#result .wide { overflow-x: auto; overflow-y: hidden; }

.katex { font-size: 1.12em; }
.katex-display { margin: .7em 0; padding: .25em 0; overflow-x: auto; overflow-y: hidden; }
.katex-error { color: var(--danger) !important; }

#foot { margin-top: 2em; padding-top: .6em; border-top: 1px solid var(--line); }
#toggle-raw { font-size: 20px; }
#raw {
  margin: .8em 0 0;
  padding: 1em;
  background: var(--code);
  border-radius: 12px;
  font: .72em/1.6 ui-monospace, "SF Mono", Menlo, monospace;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

/* 翻译记录 */
#scrim { position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, .4); }
#history {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  display: flex;
  flex-direction: column;
  width: min(480px, 92vw);
  padding: calc(12px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom)) 16px;
  background: var(--bg);
  border-left: 1px solid var(--line);
}
.history-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.history-head h2 { margin: 0; font-size: 26px; }
#history-list { flex: 1; margin: 0; padding: 0; overflow-y: auto; overscroll-behavior: contain; list-style: none; }
#history-list li { display: flex; align-items: stretch; gap: 6px; padding: 6px 0; border-bottom: 1px solid var(--line); }
#history-list li.empty { display: block; padding: 1em 0; color: var(--soft); border: 0; }
.h-open { flex: 1; min-width: 0; padding: 8px 6px; text-align: left; background: none; border: 0; }
.h-open b { display: block; overflow: hidden; font-weight: 600; text-overflow: ellipsis; white-space: nowrap; }
.h-open time { color: var(--soft); font-size: 17px; }
.h-del { flex: none; padding: 0; color: var(--soft); background: none; border: 0; font-size: 28px; }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  z-index: 40;
  max-width: min(680px, 90vw);
  padding: 12px 22px;
  color: var(--bg);
  background: var(--ink);
  border-radius: 28px;
  font-size: 20px;
  line-height: 1.35;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 12px);
  transition: opacity .18s ease, transform .18s ease;
}
#toast.on { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 560px) {
  .brand { display: none; }
  .bar { justify-content: flex-end; padding-left: 12px; padding-right: 12px; }
  .tools { gap: 6px; }
  .bar { font-size: 18px; }
  .size-now { display: none; }
  button { padding: 0 8px; min-width: 44px; }
  button.primary { padding: 0 12px; }
  #copy { min-width: 72px; }
}

@media (max-width: 345px) {
  .size { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  button, #toast { transition: none; }
}
