:root {
  color-scheme: light;
  --bg: #eef2f6;
  --panel: #f9fbfd;
  --surface: #ffffff;
  --text: #202a35;
  --muted: #657384;
  --line: #d4dce6;
  --blue: #0f6b99;
  --blue-dark: #0a5276;
  --green: #1f7a59;
  --red: #bc4b51;
  --purple: #7353ba;
  --gold: #9a6a16;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  color: var(--text);
  background: var(--bg);
}

button,
input {
  font: inherit;
}

.app-shell {
  display: grid;
  grid-template-columns: 380px minmax(720px, 1fr);
  min-height: 100vh;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 20px;
  border-right: 1px solid var(--line);
  background: var(--panel);
}

.brand h1 {
  margin: 0 0 6px;
  font-size: 30px;
  font-weight: 800;
  letter-spacing: 0;
}

.brand p,
footer {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}

.clock-panel {
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
}

.clock-panel-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.clock-panel-head h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 800;
}

.clock-panel-head span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 750;
}

.clock {
  position: relative;
  width: min(336px, 100%);
  aspect-ratio: 1;
  margin: 0 auto 16px;
  border: 1px solid #d8dee6;
  border-radius: 50%;
  background:
    radial-gradient(circle at center, #ffffff 0 38%, transparent 39%),
    radial-gradient(circle at center, transparent 0 74%, #f6f7f9 75% 100%),
    conic-gradient(from -15deg, #f3f8fd, #ffffff, #f7f4ef, #f3f8fd);
  box-shadow: inset 0 0 0 9px #fafbfc;
}

.clock::before {
  content: "";
  position: absolute;
  inset: 38px;
  border: 1px solid #dbe3ec;
  border-radius: 50%;
}

.clock::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--blue);
  transform: translate(-50%, -50%);
  z-index: 4;
}

.branch-item {
  position: absolute;
  left: 50%;
  top: 50%;
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  margin: -24px;
  border: 1px solid #e1e5ea;
  border-radius: 50%;
  background: #f7f7f8;
  color: #252b33;
  font-size: 20px;
  font-weight: 850;
  transform: rotate(calc(var(--i) * 30deg)) translateY(-136px) rotate(calc(var(--i) * -30deg));
  box-shadow: 0 1px 3px rgba(16, 24, 40, .06);
  z-index: 2;
}

.branch-item.active {
  border-color: #58a0ff;
  background: #eaf3ff;
  color: #0879ff;
  box-shadow: 0 0 0 3px rgba(8, 121, 255, .15), 0 2px 5px rgba(8, 121, 255, .18);
}

.clock-hand {
  position: absolute;
  left: calc(50% - 1.5px);
  bottom: 50%;
  width: 3px;
  height: 112px;
  border-radius: 99px;
  background: linear-gradient(to top, var(--blue), rgba(15, 107, 153, .2));
  transform: rotate(var(--hand-angle, 0deg));
  transform-origin: 50% 100%;
  z-index: 3;
}

.clock-center {
  position: absolute;
  inset: 78px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  z-index: 5;
}

.clock-time {
  font-variant-numeric: tabular-nums;
  font-size: 34px;
  font-weight: 800;
  letter-spacing: 0;
}

.clock-hour-pillar {
  margin-top: 8px;
  font-size: 28px;
  font-weight: 850;
}

.clock-hour-range {
  margin-top: 3px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
}

.clock-date-large {
  margin-top: 4px;
  color: var(--muted);
  font-size: 16px;
  font-weight: 700;
}

.clock-pillars {
  margin-top: 12px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.clock-pillars span {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 38px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  font-size: 15px;
  font-weight: 800;
}

.clock-pillars small {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.mode-actions,
.custom-panel,
.side-metrics {
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.custom-panel {
  display: grid;
  gap: 12px;
}

.custom-panel h2 {
  margin: 0;
  font-size: 17px;
}

button {
  appearance: none;
  min-height: 40px;
  border: 1px solid #c4cfda;
  border-radius: 7px;
  background: #fff;
  color: var(--text);
  font-weight: 750;
  cursor: pointer;
}

button:hover {
  border-color: #9eadbd;
  background: #f6f9fc;
}

.primary-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  border-color: var(--blue);
  background: var(--blue);
  color: white;
}

.primary-button:hover {
  border-color: var(--blue-dark);
  background: var(--blue-dark);
}

.button-icon {
  line-height: 1;
  font-size: 18px;
}

.button-row {
  display: grid;
  grid-template-columns: 1fr 46px;
  gap: 8px;
}

.icon-button {
  font-size: 20px;
}

.mode-line {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  color: var(--muted);
  font-size: 13px;
}

.tool-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  min-height: 40px;
  margin-top: 10px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  color: var(--text);
  font-weight: 750;
  text-decoration: none;
}

.tool-link:hover {
  border-color: var(--blue);
  background: rgba(24, 119, 242, 0.08);
  color: var(--blue);
}

.mode-chip {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 0 9px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.mode-chip.active {
  border-color: #9bc5da;
  background: #e7f3f9;
  color: var(--blue);
}

.field {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.field input {
  width: 100%;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 0 10px;
  color: var(--text);
  background: white;
}

.switch {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  font-size: 14px;
  font-weight: 700;
}

.switch input {
  width: 18px;
  height: 18px;
}

.side-metrics {
  display: grid;
  gap: 9px;
}

.side-metrics .metric {
  padding: 0;
  border: 0;
  background: transparent;
}

footer {
  margin-top: auto;
  line-height: 1.6;
}

.board-area {
  padding: 18px;
  overflow: auto;
  background: #fff;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 18px;
  margin-bottom: 14px;
}

.mode-title {
  margin: 0 0 4px;
  color: var(--blue);
  font-size: 14px;
  font-weight: 800;
}

.topbar h2 {
  margin: 0;
  font-size: 32px;
  letter-spacing: 0;
}

.topbar p {
  margin: 6px 0 0;
  color: var(--muted);
}

.top-metrics {
  display: grid;
  gap: 7px;
  min-width: 160px;
  text-align: right;
}

.top-metrics strong,
.top-metrics span {
  display: block;
}

.pillars {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin-bottom: 10px;
}

.pill {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  background: #f9fbfd;
}

.pill span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.pill strong {
  display: block;
  margin-top: 3px;
  font-size: 20px;
}

.info-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(260px, .75fr);
  gap: 10px;
  margin-bottom: 12px;
}

.summary {
  display: grid;
  gap: 8px;
}

.metric {
  display: grid;
  grid-template-columns: 86px 1fr;
  gap: 8px;
  align-items: start;
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  background: #f9fbfd;
}

.metric span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.metric strong {
  font-size: 14px;
  line-height: 1.45;
}

.muted-section .metric {
  background: #fbfcfd;
}

.board {
  display: grid;
  grid-template-columns: repeat(3, minmax(210px, 1fr));
  gap: 10px;
}

.palace {
  min-height: 205px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 11px;
  background: #fff;
  cursor: pointer;
  transition: border-color .16s ease, box-shadow .16s ease, transform .16s ease;
}

.palace:hover,
.palace:focus-visible,
.palace.selected {
  border-color: #81b7d2;
  box-shadow: 0 8px 22px rgba(15, 107, 153, .12);
  outline: 0;
}

.palace:active {
  transform: translateY(1px);
}

.palace.struck {
  border-color: #e6b3b7;
}

.palace.center {
  background: #fbfbf8;
}

.palace-head {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  min-height: 30px;
}

.palace-title {
  display: flex;
  gap: 6px;
  align-items: baseline;
}

.palace-title strong {
  font-size: 16px;
}

.palace-title span {
  color: var(--muted);
  font-size: 12px;
}

.round-markers,
.special-markers {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 4px;
}

.round,
.special {
  display: inline-grid;
  place-items: center;
  min-width: 24px;
  height: 24px;
  border-radius: 999px;
  padding: 0 6px;
  font-size: 12px;
  font-weight: 800;
}

.round.horse {
  background: #fff4d6;
  color: var(--gold);
}

.round.void {
  background: #f2eff8;
  color: var(--purple);
}

.special.heaven {
  background: #e7f3f9;
  color: var(--blue);
}

.special.earth {
  background: #eaf7f0;
  color: var(--green);
}

.special-markers {
  min-height: 28px;
  margin: 6px 0 8px;
  justify-content: flex-start;
}

.stem-response {
  min-height: 24px;
  margin-top: 4px;
  color: var(--blue);
  font-size: 12px;
  font-weight: 850;
  line-height: 1.45;
}

.palace-body {
  display: grid;
  gap: 8px;
  padding-top: 6px;
  border-top: 1px solid #edf1f5;
}

.row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  align-items: center;
  min-height: 30px;
  font-size: 18px;
  font-weight: 780;
}

.row .right {
  min-width: 46px;
  text-align: right;
}

.row .heaven {
  color: var(--blue);
}

.row .earth {
  color: var(--red);
}

.row .hidden {
  color: var(--purple);
  font-size: 15px;
}

.palace-popup {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(18, 28, 38, .32);
}

.palace-popup.open {
  display: flex;
}

.popup-panel {
  position: relative;
  width: min(820px, 100%);
  max-height: min(86vh, 920px);
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  box-shadow: 0 22px 60px rgba(18, 28, 38, .22);
  padding: 18px;
}

.popup-close {
  position: sticky;
  top: 0;
  float: right;
  display: inline-grid;
  place-items: center;
  width: 34px;
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--text);
  font-size: 22px;
  line-height: 1;
}

.popup-head {
  padding-right: 46px;
  margin-bottom: 12px;
}

.popup-head p,
.popup-head span {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.popup-head h2 {
  margin: 4px 0;
  font-size: 30px;
}

.knowledge-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 10px;
}

.knowledge-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f9fbfd;
  padding: 12px;
  margin-top: 9px;
}

.knowledge-card.warn {
  border-color: #e6b3b7;
  background: #fff7f7;
}

.knowledge-card.conclusion {
  border-color: #9bc5da;
  background: #eef6fb;
}

.knowledge-card strong {
  display: block;
  margin-bottom: 6px;
  font-size: 16px;
}

.knowledge-card p {
  margin: 0 0 6px;
  color: var(--muted);
  line-height: 1.65;
}

.knowledge-card p:last-child {
  margin-bottom: 0;
}

.ai-panel {
  margin-top: 16px;
  padding: 18px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.ai-panel-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
}

.ai-panel-head p {
  margin: 0 0 5px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.ai-panel-head h2 {
  margin: 0;
  font-size: 22px;
  line-height: 1.25;
}

.ai-status {
  flex: 0 0 auto;
  padding: 6px 10px;
  color: var(--blue);
  background: rgba(15, 107, 153, 0.12);
  border-radius: 8px;
  font-size: 13px;
  font-weight: 700;
}

.ai-status.error {
  color: #c23a3a;
  background: rgba(255, 77, 79, 0.1);
}

.ai-field {
  display: grid;
  gap: 8px;
}

.ai-field span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.ai-field textarea {
  width: 100%;
  min-height: 96px;
  resize: vertical;
  padding: 12px 13px;
  color: var(--text);
  background: #fbfcfd;
  border: 1px solid var(--line);
  border-radius: 8px;
  font: inherit;
  line-height: 1.55;
}

.ai-field input,
.ai-field select {
  width: 100%;
  height: 42px;
  padding: 0 12px;
  color: var(--text);
  background: #fbfcfd;
  border: 1px solid var(--line);
  border-radius: 8px;
  font: inherit;
}

.ai-field textarea:focus,
.ai-field input:focus,
.ai-field select:focus {
  outline: 2px solid rgba(15, 107, 153, 0.22);
  border-color: rgba(15, 107, 153, 0.48);
}

.ai-provider-grid {
  display: grid;
  grid-template-columns: minmax(150px, .55fr) minmax(190px, .8fr) minmax(280px, 1.35fr);
  align-items: end;
  gap: 12px;
  margin-top: 12px;
}

.ai-key-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
  gap: 12px;
  margin-top: 12px;
}

.ai-save-key {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 42px;
  padding: 0 12px;
  color: var(--muted);
  background: #fbfcfd;
  border: 1px solid var(--line);
  border-radius: 8px;
  white-space: nowrap;
}

.ai-save-key input {
  width: 16px;
  height: 16px;
}

.ai-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 12px 0;
}

.ai-actions button {
  min-width: 172px;
  height: 44px;
  padding: 0 18px;
  color: #fff;
  background: var(--blue);
  border: 1px solid var(--blue);
  border-radius: 8px;
  box-shadow: 0 6px 16px rgba(15, 107, 153, 0.18);
  font-weight: 700;
  cursor: pointer;
}

.ai-actions button:disabled {
  cursor: wait;
  opacity: 0.62;
}

.ai-actions .ai-secondary-button {
  min-width: 112px;
  color: var(--text);
  background: #fbfcfd;
  border-color: var(--line);
  box-shadow: none;
}

.ai-actions span {
  color: var(--muted);
  font-size: 13px;
}

.ai-result {
  min-height: 82px;
  padding: 14px;
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: 8px;
  line-height: 1.75;
}

.ai-result.error {
  background: rgba(255, 77, 79, 0.06);
  border-color: rgba(255, 77, 79, 0.25);
}

.ai-result p {
  margin: 0 0 12px;
}

.ai-result p:last-child {
  margin-bottom: 0;
}

.ai-followup-panel {
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.ai-followup-panel[hidden] {
  display: none;
}

.ai-followup-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.ai-followup-head strong {
  font-size: 16px;
}

.ai-followup-head span {
  flex: 0 0 auto;
  padding: 5px 9px;
  color: var(--green);
  background: rgba(24, 130, 86, .1);
  border-radius: 8px;
  font-size: 12px;
  font-weight: 800;
}

.ai-followup-head span.error {
  color: #c23a3a;
  background: rgba(255, 77, 79, 0.1);
}

.ai-followup-actions {
  margin-bottom: 12px;
}

.ai-followup-result {
  min-height: 58px;
  padding: 12px;
  background: #fbfcfd;
  border: 1px solid var(--line);
  border-radius: 8px;
  line-height: 1.72;
}

.ai-followup-result.error {
  background: rgba(255, 77, 79, 0.06);
  border-color: rgba(255, 77, 79, 0.25);
}

.ai-message {
  padding: 12px 0;
  border-bottom: 1px solid #edf1f5;
}

.ai-message:first-child {
  padding-top: 0;
}

.ai-message:last-child {
  padding-bottom: 0;
  border-bottom: 0;
}

.ai-message strong {
  display: inline-block;
  margin-bottom: 7px;
  color: var(--muted);
  font-size: 13px;
}

.ai-message.user strong {
  color: var(--blue);
}

.ai-message p,
.ai-followup-result p {
  margin: 0 0 10px;
}

.ai-message p:last-child,
.ai-followup-result p:last-child {
  margin-bottom: 0;
}

@media (max-width: 1050px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .clock {
    width: min(320px, 100%);
  }

  .board-area {
    padding: 14px;
  }
}

@media (max-width: 760px) {
  .pillars,
  .info-grid,
  .knowledge-grid {
    grid-template-columns: 1fr;
  }

  .board {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
  }

  .topbar {
    flex-direction: column;
    gap: 8px;
  }

  .top-metrics {
    text-align: left;
  }

  .board-area {
    padding: 8px;
  }

  .palace {
    min-height: 128px;
    padding: 7px;
    border-radius: 7px;
  }

  .palace-head {
    min-height: 22px;
    gap: 4px;
  }

  .palace-title {
    gap: 3px;
    flex-wrap: wrap;
  }

  .palace-title strong {
    font-size: 13px;
  }

  .palace-title span {
    font-size: 10px;
  }

  .round,
  .special {
    min-width: 18px;
    height: 18px;
    padding: 0 4px;
    font-size: 10px;
  }

  .round-markers,
  .special-markers {
    gap: 3px;
  }

  .special-markers {
    min-height: 20px;
    margin: 3px 0 5px;
  }

  .stem-response {
    min-height: 22px;
    margin-top: 3px;
    font-size: 10px;
    line-height: 1.3;
  }

  .palace-body {
    gap: 3px;
    padding-top: 4px;
  }

  .row {
    min-height: 22px;
    gap: 4px;
    font-size: 14px;
  }

  .row .right {
    min-width: 24px;
  }

  .row .hidden {
    font-size: 12px;
  }

  .palace-popup {
    align-items: flex-end;
    padding: 12px;
  }

  .popup-panel {
    max-height: 88vh;
  }

  .ai-panel {
    padding: 12px;
  }

  .ai-panel-head,
  .ai-actions,
  .ai-provider-grid,
  .ai-key-grid,
  .ai-followup-head {
    display: grid;
  }

  .ai-provider-grid,
  .ai-key-grid {
    grid-template-columns: 1fr;
  }

  .ai-save-key {
    justify-content: flex-start;
  }
}

@media (max-width: 420px) {
  .board-area {
    padding: 6px;
  }

  .board {
    gap: 5px;
  }

  .palace {
    min-height: 116px;
    padding: 6px;
  }

  .row {
    font-size: 13px;
  }

}
