:root {
  color-scheme: light;
  --paper: #f4f2ec;
  --paper-strong: #fbfaf6;
  --ink: #182721;
  --muted: #647069;
  --line: #d9d3c6;
  --green: #20382f;
  --green-soft: #e5eee7;
  --red: #9d3f34;
  --gold: #ad7c27;
  --blue: #3d566f;
  --shadow: 0 18px 50px rgba(31, 42, 38, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  background:
    linear-gradient(90deg, rgba(32, 56, 47, 0.04) 1px, transparent 1px),
    linear-gradient(180deg, rgba(32, 56, 47, 0.04) 1px, transparent 1px),
    var(--paper);
  background-size: 32px 32px;
  color: var(--ink);
  font-family:
    ui-sans-serif,
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    sans-serif;
  letter-spacing: 0;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.shell {
  width: min(1480px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 22px 0 32px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  min-height: 74px;
  margin-bottom: 18px;
  padding: 18px 22px;
  border: 1px solid rgba(32, 56, 47, 0.18);
  border-radius: 8px;
  background: rgba(251, 250, 246, 0.88);
  box-shadow: var(--shadow);
}

.kicker,
.section-kicker {
  margin: 0 0 5px;
  color: var(--gold);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  line-height: 1.15;
  letter-spacing: 0;
}

h1 {
  font-size: 30px;
}

h2 {
  font-size: 18px;
}

.status-pill {
  flex: 0 0 auto;
  max-width: 240px;
  min-height: 34px;
  padding: 8px 13px;
  border: 1px solid rgba(32, 56, 47, 0.18);
  border-radius: 999px;
  background: var(--green-soft);
  color: var(--green);
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status-pill.is-error {
  background: #f7e4df;
  color: var(--red);
}

.workspace {
  display: grid;
  grid-template-columns: minmax(280px, 360px) minmax(420px, 1fr) minmax(300px, 380px);
  gap: 18px;
  align-items: start;
}

.panel,
.chart-area {
  border: 1px solid rgba(32, 56, 47, 0.18);
  border-radius: 8px;
  background: rgba(251, 250, 246, 0.93);
  box-shadow: var(--shadow);
}

.panel {
  padding: 18px;
}

.panel-heading,
.chart-header {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 16px;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

label {
  display: grid;
  gap: 7px;
  min-width: 0;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.full-span {
  grid-column: 1 / -1;
}

input,
select,
textarea {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fffdf8;
  color: var(--ink);
  outline: none;
}

input,
select {
  padding: 0 12px;
}

textarea {
  resize: vertical;
  padding: 11px 12px;
  line-height: 1.55;
}

input:focus,
select:focus,
textarea:focus {
  border-color: rgba(61, 86, 111, 0.75);
  box-shadow: 0 0 0 3px rgba(61, 86, 111, 0.12);
}

.primary-action {
  min-height: 46px;
  border: 0;
  border-radius: 6px;
  background: var(--green);
  color: #fffdf8;
  font-weight: 800;
}

.primary-action:disabled {
  cursor: wait;
  opacity: 0.68;
}

.text-button {
  border: 0;
  background: transparent;
  color: var(--blue);
  font-size: 13px;
  font-weight: 800;
}

.history-block,
.source-block {
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.history-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
  font-weight: 800;
}

.history-list,
.source-list {
  display: grid;
  gap: 9px;
}

.history-item,
.source-item {
  width: 100%;
  min-height: 58px;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fffdf8;
  color: var(--ink);
  text-align: left;
}

.history-item {
  display: grid;
  gap: 4px;
}

.history-item:hover,
.history-item.is-active {
  border-color: rgba(157, 63, 52, 0.5);
  background: #fbf0ed;
}

.history-item strong {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-item small,
.source-item small {
  color: var(--muted);
}

.chart-area {
  min-height: calc(100vh - 146px);
  padding: 18px;
}

.chart-header {
  align-items: start;
}

.chart-meta {
  max-width: 320px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
  text-align: right;
}

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

.palace {
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  min-height: 166px;
  padding: 13px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: linear-gradient(180deg, #fffdf8 0%, #f7f3e9 100%);
  overflow: hidden;
}

.palace.is-ming {
  border-color: rgba(157, 63, 52, 0.7);
  box-shadow: inset 0 0 0 2px rgba(157, 63, 52, 0.08);
}

.palace.is-shen {
  border-color: rgba(173, 124, 39, 0.7);
}

.palace-top,
.palace-flags,
.mini-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.palace-top strong {
  min-width: 0;
  color: var(--green);
  font-size: 17px;
  overflow-wrap: anywhere;
}

.palace-top small {
  flex: 0 0 auto;
  color: var(--muted);
}

.palace-stars {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  min-height: 33px;
  margin: 10px 0;
}

.star-chip {
  max-width: 100%;
  padding: 4px 7px;
  border-radius: 999px;
  background: var(--green-soft);
  color: var(--green);
  font-size: 12px;
  font-weight: 800;
  line-height: 1.2;
}

.star-chip.major {
  background: #f5e5df;
  color: var(--red);
}

.palace-notes {
  min-height: 38px;
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.palace-flags {
  justify-content: flex-start;
  min-height: 24px;
  margin-top: 10px;
}

.flag {
  padding: 3px 7px;
  border-radius: 999px;
  background: #ece6d8;
  color: var(--gold);
  font-size: 11px;
  font-weight: 800;
}

.palace-empty {
  opacity: 0.72;
}

.report-output {
  min-height: 220px;
  max-height: 52vh;
  overflow: auto;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffdf8;
  color: #26322d;
  line-height: 1.72;
  white-space: pre-wrap;
}

.report-output p {
  margin: 0;
}

.source-count {
  color: var(--muted);
  font-size: 13px;
}

.source-item {
  display: grid;
  gap: 7px;
}

.source-item strong {
  color: var(--blue);
  font-size: 13px;
}

.source-item p {
  margin: 0;
  color: #35413c;
  font-size: 13px;
  line-height: 1.55;
}

.disclaimer {
  margin: 16px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.6;
}

.toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 10;
  max-width: min(420px, calc(100vw - 40px));
  padding: 12px 14px;
  border: 1px solid rgba(157, 63, 52, 0.35);
  border-radius: 8px;
  background: #fff8f5;
  color: var(--red);
  box-shadow: var(--shadow);
  font-weight: 700;
}

@media (max-width: 1180px) {
  .workspace {
    grid-template-columns: minmax(280px, 340px) 1fr;
  }

  .report-panel {
    grid-column: 1 / -1;
  }

  .report-output {
    max-height: none;
  }
}

@media (max-width: 820px) {
  .shell {
    width: min(100vw - 20px, 720px);
    padding-top: 10px;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  h1 {
    font-size: 25px;
  }

  .workspace {
    grid-template-columns: 1fr;
  }

  .chart-area {
    min-height: auto;
  }

  .chart-header,
  .panel-heading {
    flex-direction: column;
  }

  .chart-meta {
    max-width: none;
    text-align: left;
  }

  .palace-board {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 520px) {
  .form-grid,
  .palace-board {
    grid-template-columns: 1fr;
  }

  .palace {
    min-height: 142px;
  }
}
