:root {
  /* Traditional TCM — aged parchment + cinnabar / mahogany */
  --bg: #e7d9bd; --panel: #f6eeda; --panel-2: #eaddc0; --line: #d2bd96;
  --ink: #3a2a1c; --muted: #856a49; --accent: #8f3a2c; --accent-2: #703027;
  --good: #5f7d3a; --warn: #b07a24; --bad: #a5342a; --radius: 10px;
  --scrim: rgba(231, 217, 189, .86);   /* parchment wash over the backdrop image */
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #201711; --panel: #2c2118; --panel-2: #362a1e; --line: #4b3826;
    --ink: #f0e6d2; --muted: #b39a76; --accent: #cf6047; --accent-2: #e58468;
    --scrim: rgba(30, 21, 14, .9);
  }
}
* { box-sizing: border-box; }
body {
  margin: 0; color: var(--ink);
  background-color: var(--bg);
  background-image: linear-gradient(var(--scrim), var(--scrim)), url('image.jpg');
  background-size: cover; background-position: center; background-attachment: fixed; background-repeat: no-repeat;
  font: 15px/1.5 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  display: flex; flex-direction: column; min-height: 100vh;
}
h1, h2, .brand, #topbar .brand { font-family: Georgia, "Times New Roman", "Songti SC", serif; }
h1 { font-size: 1.5rem; margin: 0 0 .2rem; letter-spacing: .01em; }
h2 { font-size: 1.08rem; margin: 0 0 .5rem; }
a { color: var(--accent-2); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

[hidden] { display: none !important; }
#topbar {
  display: flex; align-items: center; gap: .6rem; padding: .6rem 1rem;
  background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5;
}
#topbar .brand { font-weight: 700; color: var(--accent-2); }
.nav { display: flex; gap: .3rem; margin-left: .8rem; }
.navlink {
  text-decoration: none; color: var(--muted); padding: .3rem .6rem; border-radius: 7px; font-size: .9rem;
}
.navlink:hover { background: var(--panel-2); color: var(--ink); }
.navlink.active { background: var(--accent); color: #fff; }

.tbl { width: 100%; border-collapse: collapse; font-size: .9rem; }
.tbl th, .tbl td { text-align: start; padding: .45rem .5rem; border-bottom: 1px solid var(--line); vertical-align: middle; }
.tbl th { color: var(--muted); font-weight: 600; }
.tbl select, .tbl input[type="date"] { padding: .3rem .4rem; }
.btn-danger { background: color-mix(in srgb, var(--bad) 20%, transparent); border-color: var(--bad); color: var(--ink); }
.btn-danger:hover { background: var(--bad); color: #fff; }

#app.wide { max-width: 1180px; }
.kb-head { display: flex; gap: .8rem; align-items: center; margin-bottom: .8rem; flex-wrap: wrap; }
.kb-grid { display: grid; grid-template-columns: minmax(0,1fr) minmax(0,1.15fr); gap: 1rem; align-items: start; }
@media (max-width: 820px) { .kb-grid { grid-template-columns: 1fr; } }
.kb-scroll { max-height: 64vh; overflow-y: auto; border: 1px solid var(--line); border-radius: 8px; }
.kb-row2 { padding: .5rem .6rem; border-bottom: 1px solid var(--line); cursor: pointer; display: flex; justify-content: space-between; gap: .5rem; }
.kb-row2:last-child { border-bottom: none; }
.kb-row2:hover { background: var(--panel-2); }
.kb-row2.active { background: color-mix(in srgb, var(--accent) 18%, transparent); }
.kb-ind { color: var(--muted); font-size: .8rem; margin-top: .15rem; }
.pcode { font-weight: 700; }
.pmer { color: var(--muted); font-size: .8rem; }
.empty { padding: 1rem; text-align: center; }
.chip.flag { background: color-mix(in srgb, var(--warn) 25%, transparent); border-color: var(--warn); }
.linkrow { display: flex; gap: .4rem; margin-bottom: .3rem; }
.thumb img { border-radius: 8px; border: 1px solid var(--line); }
.matrix-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: .5rem; padding: .5rem; }
.matrix-card { border: 1px solid var(--line); border-radius: 8px; padding: .4rem; cursor: pointer; text-align: center; }
.matrix-card.active { border-color: var(--accent); }
.matrix-card img, .matrix-noimg { width: 100%; height: 76px; object-fit: cover; border-radius: 6px; background: var(--panel-2);
  display: flex; align-items: center; justify-content: center; font-weight: 700; color: var(--muted); }
.matrix-label { font-size: .78rem; margin-top: .25rem; }
#topbar .spacer { flex: 1; }
.quota { font-size: .85rem; color: var(--muted); }
.quota strong { color: var(--ink); }

#app { flex: 1; width: 100%; max-width: 860px; margin: 0 auto; padding: 1rem; }
#disclaimer {
  padding: .7rem 1rem; text-align: center; font-size: .8rem; color: var(--muted);
  border-top: 1px solid var(--line); background: var(--panel);
}
.loading { color: var(--muted); padding: 2rem; text-align: center; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; margin-bottom: 1rem; }
.card.tight { padding: .7rem .8rem; }
.field { display: flex; flex-direction: column; gap: .3rem; margin-bottom: .7rem; }
.field > span { font-size: .85rem; color: var(--muted); }
input, textarea, select, button { font: inherit; }
input, textarea, select {
  width: 100%; padding: .55rem .7rem; border-radius: 8px; border: 1px solid var(--line);
  background: var(--panel-2); color: var(--ink);
}
input:focus, textarea:focus { outline: 2px solid var(--accent); border-color: var(--accent); }

button { cursor: pointer; border: 1px solid var(--line); background: var(--panel-2); color: var(--ink); border-radius: 8px; padding: .5rem .8rem; }
button:hover { border-color: var(--accent); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn-primary:hover { background: var(--accent-2); }
.btn-sm { padding: .35rem .6rem; font-size: .85rem; }
.btn-link { background: none; border: none; color: var(--accent-2); padding: 0 .3rem; }
.row { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.row.tight { gap: .3rem; }

.view-toggle { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.view-toggle button { border: none; border-radius: 0; background: var(--panel-2); }
.view-toggle button.active { background: var(--accent); color: #fff; }

.chip {
  display: inline-flex; align-items: center; gap: .3rem; padding: .2rem .55rem;
  border-radius: 999px; background: var(--panel-2); border: 1px solid var(--line);
  font-size: .82rem; margin: 0 .3rem .3rem 0;
}
.chip.hit { background: color-mix(in srgb, var(--accent) 22%, transparent); border-color: var(--accent); }
.chip.deny { text-decoration: line-through; color: var(--muted); }
.chip button { background: none; border: none; padding: 0 .1rem; color: inherit; }
.chip.click { cursor: pointer; }
.chip.click:hover { border-color: var(--accent); }

.q-card { border-color: var(--accent); }
.q-card .q { font-weight: 600; font-size: 1.1rem; margin: .3rem 0 .6rem; }

.pattern { border: 1px solid var(--line); border-radius: 8px; padding: .6rem .8rem; margin-bottom: .6rem; background: var(--panel-2); }
.pattern.top { border-color: var(--accent); }
.pattern .head { display: flex; justify-content: space-between; gap: .5rem; align-items: center; flex-wrap: wrap; }
.pattern .name { font-weight: 600; }
.badge { font-size: .75rem; padding: .1rem .45rem; border-radius: 999px; background: var(--panel); border: 1px solid var(--line); color: var(--muted); }
.badge.score { background: var(--accent); color: #fff; border-color: var(--accent); }

.result { border-color: var(--good); }
.result .points .pt { display: inline-block; padding: .15rem .5rem; margin: 0 .3rem .3rem 0; border-radius: 6px; background: var(--panel-2); border: 1px solid var(--line); font-size: .85rem; }
.notice { padding: .6rem .8rem; border-radius: 8px; border: 1px solid var(--warn); background: color-mix(in srgb, var(--warn) 15%, transparent); margin-bottom: .8rem; }
.notice.bad { border-color: var(--bad); background: color-mix(in srgb, var(--bad) 15%, transparent); }

#toast {
  position: fixed; bottom: 4.5rem; left: 50%; transform: translateX(-50%);
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  padding: .6rem 1rem; box-shadow: 0 6px 24px rgba(0,0,0,.3); opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 20;
}
#toast.show { opacity: 1; }
#toast.err { border-color: var(--bad); }

/* Login: full-bleed vintage backdrop with a scrim so the card stays readable */
body.auth {
  background-image: linear-gradient(rgba(34,22,12,.5), rgba(34,22,12,.66)), url('image.jpg');
  background-size: cover; background-position: center; background-attachment: fixed; background-repeat: no-repeat;
}
body.auth .auth-hero { display: none; }          /* the photo is the imagery now */
body.auth #disclaimer { background: rgba(246,238,218,.9); }
/* Needle click effect — a needle "acupunctures" the button, with a qi ripple */
.needle-fx { position: fixed; z-index: 9999; pointer-events: none; transform: translate(-50%, -100%); }
.needle-fx .needle { display: block; animation: needle-in .42s cubic-bezier(.3, 1.5, .5, 1) both; filter: drop-shadow(0 2px 2px rgba(0,0,0,.25)); }
.needle-fx.out .needle { animation: needle-out .28s ease forwards; }
@keyframes needle-in {
  0%   { transform: translateY(-32px) rotate(-2deg); opacity: 0; }
  55%  { transform: translateY(3px)  rotate(0deg);  opacity: 1; }
  72%  { transform: translateY(-2px); }
  100% { transform: translateY(0); opacity: 1; }
}
@keyframes needle-out { to { transform: translateY(-30px); opacity: 0; } }
.qi-ripple {
  position: fixed; z-index: 9998; pointer-events: none; width: 9px; height: 9px; border-radius: 50%;
  border: 2px solid var(--accent); animation: qi-ripple .5s ease-out forwards;
}
@keyframes qi-ripple {
  0%   { transform: translate(-50%, -50%) scale(.3); opacity: .75; }
  100% { transform: translate(-50%, -50%) scale(3.4); opacity: 0; }
}
@media (prefers-reduced-motion: reduce) { .needle-fx, .qi-ripple { display: none !important; } }

.auth-wrap { max-width: 400px; margin: 2.5rem auto; text-align: center; }
.auth-hero { width: 150px; height: auto; margin: 0 auto .4rem; display: block; }
.auth-wrap .card { text-align: start; border: 1px solid var(--accent); box-shadow: 0 10px 30px rgba(80,40,20,.15); }
.auth-wrap h1 { text-align: center; color: var(--accent); }
.card { box-shadow: 0 1px 2px rgba(80,40,20,.06); }
.btn-primary { box-shadow: 0 1px 0 rgba(0,0,0,.15) inset; }

/* autocomplete dropdown */
.ac-wrap { position: relative; }
.ac-list {
  position: absolute; z-index: 30; left: 0; right: 0; top: calc(100% + 2px);
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,.35); max-height: 260px; overflow-y: auto;
}
.ac-item { padding: .5rem .7rem; cursor: pointer; border-bottom: 1px solid var(--line); }
.ac-item:last-child { border-bottom: none; }
.ac-item:hover, .ac-item.active { background: color-mix(in srgb, var(--accent) 22%, transparent); }
