:root {
  color-scheme: light dark;

  --paper: #f7f5f0;
  --surface: #ffffff;
  --surface-2: #f2efe8;
  --line: #e2ddd2;
  --line-strong: #cfc8b8;
  --text: #22201c;
  --muted: #6d6659;
  --accent: #b4302a;
  --accent-soft: #f3e2e0;

  --ink: #1b1917;
  --guide: #ddd6c7;

  --radius: 14px;
  --shadow: 0 1px 2px rgba(40, 34, 24, .06), 0 8px 24px rgba(40, 34, 24, .06);
  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --han: "Noto Serif SC", "Source Han Serif SC", "Songti SC", "SimSun", "Microsoft YaHei", serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #15151a;
    --surface: #1d1d23;
    --surface-2: #24242c;
    --line: #2f2f39;
    --line-strong: #3d3d49;
    --text: #eceaf1;
    --muted: #9b96a6;
    --accent: #ff7a6e;
    --accent-soft: #3a2422;

    --ink: #f4f2ee;
    --guide: #35353f;

    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 30px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--paper);
  color: var(--text);
  font: 15px/1.55 var(--sans);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); }

/* ----------------------------------------------------------------- topbar */

.topbar {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: center;
  justify-content: space-between;
  padding: 18px clamp(16px, 4vw, 40px);
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.brand { display: flex; align-items: center; gap: 14px; }

.brand-mark {
  display: grid;
  place-items: center;
  width: 46px;
  height: 46px;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-family: var(--han);
  font-size: 27px;
  line-height: 1;
  box-shadow: var(--shadow);
}

.brand h1 { margin: 0; font-size: 18px; letter-spacing: -.01em; }
.brand p { margin: 2px 0 0; font-size: 13px; color: var(--muted); }

.topbar-right { display: flex; align-items: center; gap: 10px; }
.engine-label { font-size: 13px; color: var(--muted); }

.engine-select {
  font: inherit;
  font-size: 13px;
  padding: 7px 10px;
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  background: var(--surface-2);
  color: var(--text);
}

.status {
  font-size: 12.5px;
  padding: 6px 11px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
  background: var(--surface-2);
  white-space: nowrap;
}
.status[data-tone="ok"]   { color: #2d7a4f; border-color: #b9dfc8; background: #eaf6ef; }
.status[data-tone="warn"] { color: #9a5f00; border-color: #ecd39a; background: #fdf3df; }

@media (prefers-color-scheme: dark) {
  .status[data-tone="ok"]   { color: #7fd6a2; border-color: #264835; background: #16241c; }
  .status[data-tone="warn"] { color: #f0c274; border-color: #4a3a1c; background: #2a2113; }
}

/* ----------------------------------------------------------------- layout */

.layout {
  display: grid;
  grid-template-columns: minmax(320px, 400px) minmax(0, 1fr);
  gap: clamp(16px, 2.5vw, 28px);
  align-items: start;
  padding: clamp(16px, 3vw, 32px) clamp(16px, 4vw, 40px);
  max-width: 1240px;
  margin: 0 auto;
}

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
}

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

.panel-write { position: sticky; top: 20px; }
@media (max-width: 900px) { .panel-write { position: static; } }

/* -------------------------------------------------------------------- pad */

.pad-wrap {
  position: relative;
  border: 1px solid var(--line-strong);
  border-radius: 12px;
  background: var(--surface-2);
  overflow: hidden;
}

.pad {
  display: block;
  width: 100%;
  aspect-ratio: 1;
  touch-action: none;
  cursor: crosshair;
  --ink: var(--ink);
  --guide: var(--guide);
}

.pad-tools { display: flex; gap: 8px; margin-top: 12px; }

.btn {
  font: inherit;
  font-size: 13.5px;
  padding: 8px 13px;
  border-radius: 9px;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: background .12s, border-color .12s, opacity .12s;
}
.btn:hover:not(:disabled) { background: var(--surface-2); border-color: var(--muted); }
.btn:disabled { opacity: .42; cursor: default; }

/* ------------------------------------------------------------- candidates */

.candidates-wrap { margin-top: 16px; min-height: 96px; }

.candidates {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(62px, 1fr));
  gap: 8px;
}

.candidate {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  padding: 9px 4px 7px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: transform .1s, border-color .12s, background .12s;
}
.candidate:hover { border-color: var(--accent); background: var(--accent-soft); transform: translateY(-1px); }
.candidate:first-child { border-color: var(--accent); }

.candidate-key {
  position: absolute;
  top: 3px;
  left: 5px;
  font-size: 10px;
  color: var(--muted);
}
.candidate-char { font-family: var(--han); font-size: 30px; line-height: 1.1; }
.candidate-pinyin {
  font-size: 11px;
  color: var(--muted);
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.hint { margin: 10px 2px 0; font-size: 12.5px; color: var(--muted); }

/* ------------------------------------------------------------------- tips */

.tips { margin-top: 14px; border-top: 1px solid var(--line); padding-top: 12px; }
.tips summary { cursor: pointer; font-size: 13px; color: var(--muted); }
.tips ul { margin: 10px 0 0; padding-left: 18px; font-size: 13px; color: var(--muted); }
.tips li { margin: 4px 0; }

kbd {
  font: inherit;
  font-size: 11.5px;
  padding: 1px 5px;
  border: 1px solid var(--line-strong);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--surface-2);
}

/* ------------------------------------------------------------------- word */

.word-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--line);
}

.word { display: flex; gap: 6px; min-height: 58px; align-items: center; flex-wrap: wrap; }

.word-char {
  font-family: var(--han);
  font-size: 46px;
  line-height: 1.15;
  padding: 0 6px;
  border-bottom: 3px solid var(--accent);
}

.word-placeholder { color: var(--muted); font-size: 14px; }
.word-actions { display: flex; gap: 8px; }

/* ---------------------------------------------------------------- results */

.result { margin-top: 4px; }

.block { margin-top: 22px; }

.block-title {
  margin: 0 0 12px;
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--muted);
}

.stack { display: grid; gap: 12px; }

.entry {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px 16px;
  background: var(--surface-2);
}

.entry-big {
  background: var(--surface);
  border-color: var(--line-strong);
  border-left: 4px solid var(--accent);
}
.entry + .entry { margin-top: 12px; }

.entry-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.entry-word { font-family: var(--han); font-size: 28px; line-height: 1.2; }
.entry-big .entry-word { font-size: 38px; }
.entry-pinyin { font-size: 17px; color: var(--accent); font-weight: 600; }
.entry-trad { font-size: 13px; color: var(--muted); }

.defs { margin: 10px 0 0; padding-left: 22px; }
.defs li { margin: 3px 0; }
.entry-big .defs li:first-child { font-size: 17px; }

.empty { color: var(--muted); margin: 0; max-width: 60ch; }

.charline {
  display: grid;
  grid-template-columns: 46px 90px 1fr;
  gap: 12px;
  align-items: baseline;
  padding: 9px 4px;
  border-bottom: 1px solid var(--line);
}
.charline:last-child { border-bottom: 0; }
.charline-char { font-family: var(--han); font-size: 30px; line-height: 1; }
.charline-pinyin { color: var(--accent); font-weight: 600; }
.charline-def { color: var(--muted); }

@media (max-width: 520px) {
  .charline { grid-template-columns: 40px 1fr; }
  .charline-def { grid-column: 1 / -1; }
}

/* ---------------------------------------------------------------- related */

.related { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 8px; }

.related-item {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 10px;
  align-items: center;
  text-align: left;
  font: inherit;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  transition: border-color .12s, background .12s;
}
.related-item:hover { border-color: var(--accent); background: var(--accent-soft); }

/* The word never shrinks or breaks; if the pair cannot fit on one line the
   item wraps and the reading drops below, which keeps long idioms readable. */
.related-word {
  flex: 0 0 auto;
  white-space: nowrap;
  font-family: var(--han);
  font-size: 22px;
  line-height: 1.15;
}

/* min-width:0 lets the flex child shrink, which is what makes the ellipsis
   below actually take effect on a long definition. */
.related-text { flex: 1 1 auto; min-width: 0; display: grid; }

.related-pinyin { font-size: 12.5px; color: var(--accent); }
.related-def { font-size: 12.5px; color: var(--muted); }

.related-pinyin,
.related-def {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ----------------------------------------------------------------- footer */

.footer {
  padding: 24px clamp(16px, 4vw, 40px) 40px;
  max-width: 1240px;
  margin: 0 auto;
}
.footer p { margin: 0; font-size: 12.5px; color: var(--muted); max-width: 78ch; }
