:root {
  --ink: #0d0f14;
  --ink-2: #151922;
  --ink-3: #1d2230;
  --paper: #f4ead6;
  --paper-2: #e9dcc0;
  --gold: #c9a45c;
  --gold-dim: #8f753f;
  --gold-bright: #f0d090;
  --cinnabar: #b83b32;
  --jade: #4f8a72;
  --azure: #3f6f9e;
  --text: #e8dfc9;
  --text-dim: #a89c82;
  --line: rgba(201, 164, 92, .35);
  --lu: #e2564a;
  --quan: #e0952f;
  --ke: #4f9bd8;
  --ji: #b46bd0;
  --radius: 10px;
  --font-kai: "Kaiti SC", "STKaiti", KaiTi, "Noto Serif SC", serif;
  --font-song: "Songti SC", "STSong", SimSun, "Noto Serif SC", serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background:
    radial-gradient(1200px 600px at 20% -10%, rgba(201,164,92,.10), transparent 60%),
    radial-gradient(900px 500px at 110% 10%, rgba(63,111,158,.12), transparent 60%),
    var(--ink);
  color: var(--text);
  font-family: var(--font-song);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

body {
  padding: 0 0 calc(88px + env(safe-area-inset-bottom));
  overscroll-behavior-y: contain;
}

button, input, select { font-family: inherit; }

/* ---------- Top app bar ---------- */
.appbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) 14px 10px;
  background: linear-gradient(180deg, rgba(13,15,20,.97), rgba(13,15,20,.82) 70%, rgba(13,15,20,0));
  backdrop-filter: blur(10px);
}
.appbar .seal {
  width: 34px; height: 34px; flex: none;
  display: grid; place-items: center;
  border: 1px solid var(--gold);
  border-radius: 8px;
  color: var(--gold-bright);
  font-family: var(--font-kai);
  font-size: 16px;
  letter-spacing: 0;
  background: linear-gradient(160deg, rgba(201,164,92,.18), transparent);
  box-shadow: 0 0 14px rgba(201,164,92,.18) inset;
}
.appbar h1 {
  margin: 0;
  font-family: var(--font-kai);
  font-size: 19px;
  font-weight: 600;
  letter-spacing: 3px;
  color: var(--gold-bright);
  white-space: nowrap;
}
.appbar .sub {
  font-size: 11px;
  color: var(--text-dim);
  letter-spacing: 1px;
  margin-top: 1px;
}
.appbar .spacer { flex: 1; }
.icon-btn {
  appearance: none;
  border: 1px solid var(--line);
  background: rgba(255,255,255,.02);
  color: var(--gold);
  border-radius: 9px;
  padding: 7px 11px;
  font-size: 13px;
  cursor: pointer;
  transition: .18s;
}
.icon-btn:active { transform: scale(.96); background: rgba(201,164,92,.14); }

/* ---------- Layout ---------- */
.wrap { max-width: 1080px; margin: 0 auto; padding: 0 12px; }

.panel {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: linear-gradient(165deg, rgba(29,34,48,.86), rgba(15,18,26,.92));
  box-shadow: 0 10px 30px rgba(0,0,0,.35), 0 0 0 1px rgba(0,0,0,.4) inset;
}

/* ---------- Input form ---------- */
.form-panel { padding: 14px; margin-bottom: 14px; }
.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field.full { grid-column: 1 / -1; }
.field label {
  font-size: 11.5px;
  color: var(--text-dim);
  letter-spacing: 1px;
}
.field input, .field select {
  width: 100%;
  padding: 9px 10px;
  background: rgba(0,0,0,.28);
  border: 1px solid rgba(201,164,92,.22);
  border-radius: 8px;
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: .18s;
  min-width: 0;
}
.field input:focus, .field select:focus {
  border-color: var(--gold);
  box-shadow: 0 0 0 3px rgba(201,164,92,.12);
}
.field select option { background: #151922; color: var(--text); }

.seg {
  display: flex;
  gap: 6px;
  background: rgba(0,0,0,.26);
  border: 1px solid rgba(201,164,92,.2);
  border-radius: 9px;
  padding: 3px;
}
.seg button {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  padding: 7px 6px;
  border-radius: 7px;
  font-size: 13px;
  cursor: pointer;
  transition: .16s;
  font-family: var(--font-kai);
  letter-spacing: 1px;
}
.seg button.active {
  background: linear-gradient(160deg, rgba(201,164,92,.32), rgba(201,164,92,.14));
  color: var(--gold-bright);
  box-shadow: 0 0 0 1px rgba(201,164,92,.4) inset;
}

.form-actions { display: flex; gap: 10px; margin-top: 13px; }
.btn-primary {
  flex: 1;
  padding: 11px 16px;
  border: 1px solid var(--gold);
  border-radius: 9px;
  background: linear-gradient(160deg, rgba(201,164,92,.9), rgba(143,117,63,.85));
  color: #201a0d;
  font-size: 15px;
  font-weight: 700;
  font-family: var(--font-kai);
  letter-spacing: 4px;
  cursor: pointer;
  transition: .18s;
}
.btn-primary:active { transform: scale(.985); filter: brightness(1.08); }
.btn-ghost {
  padding: 11px 14px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: transparent;
  color: var(--gold);
  font-size: 14px;
  cursor: pointer;
}

/* ---------- Tabs ---------- */
.tabs {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  padding: 4px;
  margin-bottom: 12px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: rgba(0,0,0,.28);
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tabs button {
  flex: 1 0 auto;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  padding: 9px 13px;
  border-radius: 8px;
  font-size: 13.5px;
  cursor: pointer;
  font-family: var(--font-kai);
  letter-spacing: 2px;
  transition: .16s;
  white-space: nowrap;
}
.tabs button.active {
  background: linear-gradient(160deg, rgba(201,164,92,.3), rgba(201,164,92,.1));
  color: var(--gold-bright);
  box-shadow: 0 0 0 1px rgba(201,164,92,.4) inset;
}

/* ---------- Chart ---------- */
.chart-panel { padding: 12px; margin-bottom: 14px; }

.chart-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 4px 4px 12px;
  border-bottom: 1px dashed rgba(201,164,92,.25);
  margin-bottom: 12px;
}
.chart-head .person { font-family: var(--font-kai); font-size: 17px; color: var(--gold-bright); letter-spacing: 2px; }
.chart-head .meta { font-size: 12px; color: var(--text-dim); }
.chart-head .badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--gold);
  letter-spacing: 1px;
}

.chart {
  position: relative;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(4, minmax(96px, auto));
  gap: 3px;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 4px;
  background:
    linear-gradient(145deg, rgba(201,164,92,.05), transparent 40%),
    rgba(0,0,0,.22);
}

.chart-lines {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 3;
  pointer-events: none;
  overflow: visible;
}
.chart-lines .link {
  stroke: rgba(240, 208, 144, .62);
  stroke-width: .55;
  fill: none;
  stroke-linecap: round;
  filter: drop-shadow(0 0 1.5px rgba(240,208,144,.5));
}
.chart-lines .link.opp {
  stroke: rgba(159, 214, 255, .5);
  stroke-dasharray: 2 1.6;
}
.chart-lines .node {
  fill: rgba(240, 208, 144, .9);
  stroke: none;
}
.chart-lines .node.opp { fill: rgba(159, 214, 255, .9); }

.palace {
  position: relative;
  border: 1px solid rgba(201,164,92,.18);
  border-radius: 7px;
  padding: 6px 7px 7px;
  background: linear-gradient(170deg, rgba(244,234,214,.045), rgba(244,234,214,.015));
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-height: 96px;
  cursor: pointer;
  transition: .16s;
  overflow: hidden;
}
.palace:active { background: rgba(201,164,92,.12); }
.palace.ming { border-color: var(--gold); box-shadow: 0 0 0 1px rgba(201,164,92,.35) inset, 0 0 18px rgba(201,164,92,.12); }
.palace.body-palace::after {
  content: "身";
  position: absolute;
  right: 5px;
  bottom: 5px;
  font-size: 10px;
  color: var(--cinnabar);
  border: 1px solid rgba(184,59,50,.6);
  border-radius: 4px;
  padding: 0 3px;
  font-family: var(--font-kai);
}
.palace.hl {
  background: rgba(201,164,92,.14);
  border-color: var(--gold-bright);
  box-shadow: 0 0 0 1px rgba(240,208,144,.4) inset;
}
.palace.sanhe {
  background: rgba(79,138,114,.16);
  border-color: rgba(79,138,114,.65);
}
.palace.dim { opacity: .42; }

.palace .p-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 4px;
}
.palace .p-name {
  font-family: var(--font-kai);
  font-size: 12.5px;
  color: var(--gold-bright);
  letter-spacing: 1px;
  white-space: nowrap;
}
.palace .p-ganzhi {
  font-size: 10.5px;
  color: var(--text-dim);
  white-space: nowrap;
}
.palace .p-stars { display: flex; flex-direction: column; gap: 1px; }
.star-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 2px 5px;
  line-height: 1.35;
}
.star {
  font-size: 12px;
  color: #e7dcc4;
  white-space: nowrap;
}
.star.major { color: #fff3d6; font-weight: 700; font-size: 13px; }
.star.soft { color: #a9cfe8; }
.star.tough { color: #d98a82; }
.star.adjective { color: #9a947f; font-size: 10.5px; }
.star.lucun { color: #e6c46a; }
.star .bright { font-size: 9.5px; color: #8fa88a; margin-left: 1px; }

.mutagen {
  display: inline-block;
  font-size: 9.5px;
  line-height: 1;
  padding: 1px 3px;
  border-radius: 3px;
  margin-left: 2px;
  font-family: var(--font-kai);
  vertical-align: 1px;
}
.mutagen.lu { color: #1a120c; background: var(--lu); }
.mutagen.quan { color: #1a120c; background: var(--quan); }
.mutagen.ke { color: #0c1218; background: var(--ke); }
.mutagen.ji { color: #120c18; background: var(--ji); }
.mutagen.mini { font-size: 8.5px; padding: 0 2px; }
.mutagen.scope-decadal { box-shadow: 0 0 0 1px #f0d090, 0 0 6px rgba(240,208,144,.6); }
.mutagen.scope-yearly { box-shadow: 0 0 0 1px #9fd6ff, 0 0 6px rgba(159,214,255,.6); }

.palace .p-foot {
  margin-top: auto;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 4px;
  font-size: 9.5px;
  color: #7f7766;
}
.palace .ages { letter-spacing: .5px; }
.palace .daxian {
  color: var(--gold);
  font-size: 10px;
  font-family: var(--font-kai);
  white-space: nowrap;
}

/* Center block */
.center {
  grid-column: 2 / 4;
  grid-row: 2 / 4;
  border: 1px solid rgba(201,164,92,.35);
  border-radius: 8px;
  padding: 10px;
  background:
    radial-gradient(circle at 50% 20%, rgba(201,164,92,.12), transparent 70%),
    rgba(0,0,0,.3);
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 0;
  overflow: auto;
}
.center .c-title {
  font-family: var(--font-kai);
  font-size: 17px;
  color: var(--gold-bright);
  letter-spacing: 3px;
  text-align: center;
}
.center .c-sub {
  text-align: center;
  font-size: 11px;
  color: var(--text-dim);
  letter-spacing: 1px;
}
.center .c-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 3px 10px;
  font-size: 11.5px;
}
.center .c-grid div { display: flex; gap: 5px; align-items: baseline; }
.center .c-grid .k { color: var(--text-dim); flex: none; }
.center .c-grid .v { color: var(--text); }
.center .pillars {
  display: flex;
  justify-content: center;
  gap: 6px;
  flex-wrap: wrap;
  font-family: var(--font-kai);
  font-size: 13px;
  color: var(--gold);
  letter-spacing: 1px;
}
.center .pillars span {
  padding: 2px 7px;
  border: 1px solid rgba(201,164,92,.28);
  border-radius: 5px;
}

.plate-switch {
  display: flex;
  gap: 3px;
  padding: 3px;
  border: 1px solid rgba(201,164,92,.35);
  border-radius: 9px;
  background: rgba(0,0,0,.32);
  margin: 1px 0;
}
.plate-switch button {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  font-family: var(--font-kai);
  font-size: 13px;
  letter-spacing: 3px;
  padding: 6px 2px;
  border-radius: 7px;
  cursor: pointer;
  transition: .16s;
  white-space: nowrap;
}
.plate-switch button.active {
  background: linear-gradient(160deg, rgba(201,164,92,.9), rgba(143,117,63,.8));
  color: #201a0d;
  font-weight: 700;
  box-shadow: 0 0 10px rgba(201,164,92,.28);
}
.plate-switch button:active { transform: scale(.97); }

.plate-hint {
  text-align: center;
  font-size: 10px;
  color: #8b8270;
  letter-spacing: .5px;
  line-height: 1.35;
}
.center .c-note {
  font-size: 10.5px;
  color: #8b8270;
  text-align: center;
  margin-top: auto;
}

/* ---------- Info / list panels ---------- */
.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: var(--font-kai);
  font-size: 15px;
  color: var(--gold-bright);
  letter-spacing: 2px;
  margin: 2px 2px 10px;
}
.section-title::before {
  content: "";
  width: 3px; height: 15px;
  border-radius: 2px;
  background: linear-gradient(var(--gold-bright), var(--gold-dim));
}
.section-title .hint { font-size: 11px; color: var(--text-dim); letter-spacing: 0; font-family: var(--font-song); margin-left: auto; }

.cards { display: grid; gap: 10px; }
.card {
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 11px 12px;
  background: linear-gradient(165deg, rgba(29,34,48,.7), rgba(15,18,26,.8));
}
.card .cname { font-family: var(--font-kai); font-size: 14px; color: var(--gold-bright); letter-spacing: 1px; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.card .cbody { font-size: 12.5px; color: var(--text-dim); margin-top: 5px; }
.card .cbody b { color: var(--text); font-weight: 600; }

.flow-item {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 6px 0;
  border-bottom: 1px dashed rgba(201,164,92,.14);
  font-size: 12.5px;
  flex-wrap: wrap;
}
.flow-item:last-child { border-bottom: 0; }
.flow-item .from { color: var(--gold); font-family: var(--font-kai); min-width: 52px; }
.flow-item .arrow { color: var(--text-dim); }
.flow-item .to { color: var(--text); }
.flow-item .selftag {
  font-size: 9.5px;
  color: var(--cinnabar);
  border: 1px solid rgba(184,59,50,.6);
  border-radius: 4px;
  padding: 0 3px;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 12px;
  padding: 8px 2px 0;
  font-size: 11px;
  color: var(--text-dim);
}
.legend i {
  display: inline-block;
  width: 9px; height: 9px;
  border-radius: 2px;
  margin-right: 4px;
  vertical-align: -1px;
}

.empty {
  text-align: center;
  padding: 34px 14px;
  color: var(--text-dim);
  font-size: 13px;
}

/* ---------- Bottom nav ---------- */
.bottom {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 40;
  display: flex;
  padding: 7px 10px calc(7px + env(safe-area-inset-bottom));
  background: linear-gradient(0deg, rgba(13,15,20,.98) 55%, rgba(13,15,20,.72));
  border-top: 1px solid var(--line);
  backdrop-filter: blur(12px);
}
.bottom button {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  font-size: 11px;
  font-family: var(--font-kai);
  letter-spacing: 1px;
  padding: 4px 0;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}
.bottom button .dot {
  width: 5px; height: 5px; border-radius: 50%;
  background: transparent;
  transition: .2s;
}
.bottom button.active { color: var(--gold-bright); }
.bottom button.active .dot { background: var(--gold-bright); box-shadow: 0 0 8px var(--gold); }

/* ---------- Modal ---------- */
.modal-mask {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(5,6,9,.72);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 0;
  opacity: 0;
  pointer-events: none;
  transition: .2s;
}
.modal-mask.show { opacity: 1; pointer-events: auto; }
.modal {
  width: 100%;
  max-width: 560px;
  max-height: 82vh;
  overflow: auto;
  border: 1px solid var(--gold);
  border-bottom: 0;
  border-radius: 16px 16px 0 0;
  background: linear-gradient(170deg, #1b202c, #0e1118);
  padding: 16px 16px calc(20px + env(safe-area-inset-bottom));
  transform: translateY(20px);
  transition: .22s;
}
.modal-mask.show .modal { transform: translateY(0); }
.modal h3 {
  margin: 0 0 4px;
  font-family: var(--font-kai);
  font-size: 18px;
  color: var(--gold-bright);
  letter-spacing: 2px;
}
.modal .m-sub { font-size: 12px; color: var(--text-dim); margin-bottom: 12px; }
.modal .star-line {
  display: flex;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px dashed rgba(201,164,92,.16);
  font-size: 13px;
}
.modal .star-line .sn { font-family: var(--font-kai); color: #fff3d6; min-width: 60px; }
.modal .star-line .sd { color: var(--text-dim); font-size: 12px; }
.modal .close-row { margin-top: 14px; display: flex; gap: 10px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(96px + env(safe-area-inset-bottom));
  transform: translate(-50%, 10px);
  background: rgba(20,24,32,.96);
  border: 1px solid var(--gold);
  color: var(--gold-bright);
  padding: 9px 16px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 80;
  opacity: 0;
  pointer-events: none;
  transition: .22s;
  max-width: 88vw;
  text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Responsive ---------- */
@media (max-width: 640px) {
  .chart {
    grid-template-rows: repeat(4, minmax(84px, auto));
    gap: 2px;
    padding: 3px;
  }
  .palace { padding: 5px 5px 6px; min-height: 84px; gap: 2px; }
  .palace .p-name { font-size: 11px; }
  .palace .p-ganzhi { font-size: 9.5px; }
  .star { font-size: 11px; }
  .star.major { font-size: 12px; }
  .star.adjective { font-size: 9.5px; }
  .palace .p-foot { font-size: 9px; }
  .center .c-title { font-size: 15px; }
  .center .c-grid { font-size: 10.5px; gap: 2px 6px; }
  .center .pillars { font-size: 11.5px; gap: 4px; }
  .center .pillars span { padding: 1px 5px; }
  .appbar h1 { font-size: 17px; letter-spacing: 2px; }
}

@media (max-width: 400px) {
  .palace .p-ganzhi { display: none; }
  .center .c-grid { grid-template-columns: 1fr; }
  .center .c-sub { font-size: 10px; }
}

@media (min-width: 860px) {
  .chart { grid-template-rows: repeat(4, minmax(132px, auto)); }
  .star { font-size: 12.5px; }
  .star.major { font-size: 14px; }
  .center .c-title { font-size: 20px; }
  .center .c-grid { font-size: 12.5px; }
}

@media print {
  body { background: #fff; color: #000; padding: 0; }
  .appbar, .bottom, .form-panel, .tabs, .icon-btn, .toast { display: none !important; }
  .panel, .palace, .center, .card {
    background: #fff !important;
    color: #000 !important;
    box-shadow: none !important;
  }
  .palace .p-name, .star, .card .cname, .section-title, .center .c-title { color: #000 !important; }
}
