:root {
  --ink: #20231f;
  --muted: #697066;
  --line: #d6d8ce;
  --paper: #f6f4ea;
  --panel: #fffdf5;
  --green: #126a55;
  --red: #a54035;
  --blue: #285d8f;
  --gold: #b5862d;
  --shadow: 0 18px 60px rgba(35, 38, 31, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background:
    linear-gradient(90deg, rgba(32, 35, 31, 0.04) 1px, transparent 1px),
    linear-gradient(rgba(32, 35, 31, 0.035) 1px, transparent 1px),
    var(--paper);
  background-size: 28px 28px;
  font-family: "Avenir Next", "PingFang SC", "Hiragino Sans GB", sans-serif;
}

button,
input,
select {
  font: inherit;
}

.app-header {
  display: grid;
  grid-template-columns: minmax(220px, 0.8fr) minmax(360px, 1.2fr);
  gap: 24px;
  align-items: end;
  padding: 28px 32px 18px;
  border-bottom: 1px solid var(--line);
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--green);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-family: Georgia, "Songti SC", serif;
  font-size: 42px;
  line-height: 1;
}

h2 {
  font-size: 16px;
  line-height: 1.2;
}

.search {
  display: grid;
  grid-template-columns: 1fr 96px;
  gap: 10px;
}

.search input,
.daily-sync input,
.controls input,
.controls select {
  min-width: 0;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  color: var(--ink);
  padding: 0 12px;
  outline: none;
}

.search input:focus,
.daily-sync input:focus,
.controls input:focus,
.controls select:focus {
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(18, 106, 85, 0.12);
}

button {
  height: 40px;
  border: 1px solid #1d463d;
  border-radius: 6px;
  background: var(--green);
  color: #fffdf5;
  cursor: pointer;
}

button:hover {
  filter: brightness(1.05);
}

button:disabled {
  cursor: wait;
  opacity: 0.7;
}

.layout {
  display: grid;
  grid-template-columns: minmax(620px, 1fr) 420px;
  grid-template-areas:
    "overview detail"
    "candidates detail";
  gap: 18px;
  padding: 18px 32px 32px;
}

.panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 253, 245, 0.92);
  box-shadow: var(--shadow);
}

.overview {
  grid-area: overview;
}

.candidates {
  grid-area: candidates;
}

.detail {
  grid-area: detail;
  align-self: start;
  position: sticky;
  top: 18px;
  max-height: calc(100vh - 36px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.panel-title {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}

.panel-title p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.controls input {
  width: 74px;
}

.export-button {
  background: var(--panel);
  color: var(--green);
  border: 1px solid var(--green);
  padding: 0 14px;
  font-size: 13px;
  font-weight: 600;
}

.change-controls {
  flex-wrap: nowrap;
}

.window-button {
  width: 54px;
  height: 34px;
  border-color: var(--line);
  background: var(--panel);
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.window-button.active {
  border-color: var(--green);
  background: var(--green);
  color: var(--panel);
}

.window-button:disabled {
  cursor: not-allowed;
  opacity: 0.38;
}

.export-button:hover {
  background: var(--green);
  color: var(--panel);
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(120px, 1fr));
  gap: 1px;
  background: var(--line);
}

.metric {
  min-height: 84px;
  padding: 16px;
  background: var(--panel);
}

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

.metric strong {
  display: block;
  margin-top: 8px;
  font-family: Georgia, "Songti SC", serif;
  font-size: 26px;
  line-height: 1;
}

.daily-sync {
  display: grid;
  grid-template-columns: 1fr 128px 92px 92px;
  gap: 10px;
  align-items: center;
  padding: 14px 16px;
  border-top: 1px solid var(--line);
  background: rgba(248, 246, 236, 0.7);
}

.daily-sync h3 {
  margin: 0;
  font-size: 14px;
}

.daily-sync p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.daily-sync input {
  width: 100%;
}

.calc-status {
  display: grid;
  grid-template-columns: repeat(4, minmax(120px, 1fr));
  gap: 1px;
  border-bottom: 1px solid var(--line);
  background: var(--line);
}

.calc-chip {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  min-height: 44px;
  padding: 10px 12px;
  background: #fbf9ef;
}

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

.calc-chip strong {
  font-family: Georgia, "Songti SC", serif;
  font-size: 20px;
}

/* Real-time columns: hidden by default, shown when WS is active */
.rt-col {
  display: none;
}

.rt-col.rt-visible {
  display: table-cell;
}

.rt-price.up,
.rt-change.up {
  color: var(--red);
}

.rt-price.down,
.rt-change.down {
  color: var(--green);
}

td.up {
  color: var(--red);
  font-weight: 700;
}

td.down {
  color: var(--green);
  font-weight: 700;
}

.delta {
  margin-left: 4px;
  color: inherit;
  font-size: 12px;
  font-weight: 700;
}

.signal-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 4px;
  max-width: 190px;
}

.signal-pill {
  display: inline-flex;
  align-items: center;
  min-height: 22px;
  padding: 0 7px;
  border-radius: 999px;
  background: #f5e8d2;
  color: var(--gold);
  font-size: 12px;
  font-weight: 700;
}

.change-table th:nth-child(2),
.change-table td:nth-child(2) {
  min-width: 150px;
}

@keyframes flash-pulse {
  0% { background: rgba(181, 134, 45, 0.3); }
  100% { background: transparent; }
}

.flash {
  animation: flash-pulse 0.6s ease-out;
}

.table-wrap {
  overflow-x: auto;
  max-height: calc(100vh - 300px);
  overflow-y: auto;
}

.pagination {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  padding: 12px 16px;
  border-top: 1px solid var(--line);
  background: rgba(248, 246, 236, 0.72);
}

.pagination button {
  width: 82px;
  height: 34px;
  background: var(--panel);
  color: var(--green);
  border-color: var(--green);
  font-size: 13px;
  font-weight: 700;
}

.pagination button:disabled {
  border-color: var(--line);
  color: var(--muted);
  cursor: not-allowed;
}

.pagination span {
  min-width: 92px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

th,
td {
  padding: 11px 12px;
  border-bottom: 1px solid var(--line);
  text-align: right;
  white-space: nowrap;
}

th:first-child,
td:first-child {
  text-align: left;
}

th {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.sort-button {
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
  width: 100%;
  height: auto;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: inherit;
  font-size: inherit;
  font-weight: inherit;
  cursor: pointer;
}

th:first-child .sort-button {
  justify-content: flex-start;
}

.sort-button::after {
  content: "";
  width: 0;
  height: 0;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  opacity: 0;
}

.sort-button.active {
  color: var(--green);
}

.sort-button.active[data-direction="asc"]::after {
  border-bottom: 6px solid currentColor;
  opacity: 1;
}

.sort-button.active[data-direction="desc"]::after {
  border-top: 6px solid currentColor;
  opacity: 1;
}

tbody tr {
  cursor: pointer;
}

tbody tr:hover {
  background: #f0efe2;
}

.stock-name {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.stock-name b {
  font-size: 13px;
}

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

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 0 8px;
  border-radius: 999px;
  background: #e7efe7;
  color: var(--green);
  font-weight: 700;
}

.pill.warn {
  background: #f5e8d2;
  color: var(--gold);
}

.pill.bad {
  background: #f2ded9;
  color: var(--red);
}

.score-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 42px;
  min-height: 26px;
  padding: 0 8px;
  border-radius: 999px;
  background: #e7efe7;
  color: var(--green);
  font-weight: 800;
}

.status-badge {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 0 8px;
  border-radius: 999px;
  background: #eee8d8;
  color: var(--muted);
  font-weight: 700;
}

.status-badge.ok {
  background: #dfeee4;
  color: var(--green);
}

.detail-content {
  padding: 16px;
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.judgment-cell {
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.detail-content.empty,
.empty-row {
  color: var(--muted);
  padding: 24px 16px;
}

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

.headline h3 {
  margin: 0;
  font-size: 24px;
}

.headline span {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.score {
  min-width: 74px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  text-align: center;
  background: #f8f6ec;
}

.score strong {
  display: block;
  font-family: Georgia, "Songti SC", serif;
  font-size: 22px;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.detail-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: #fffdf8;
}

.detail-item span {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.detail-item strong {
  display: block;
  margin-top: 6px;
  font-size: 18px;
}

.detail-section {
  margin-top: 16px;
}

.detail-section h4 {
  margin: 0 0 8px;
  font-size: 14px;
}

.judgment-text {
  color: var(--ink);
  font-size: 13px;
  line-height: 1.65;
}

.change-list,
.score-line-list,
.detail-signal-list {
  display: grid;
  gap: 6px;
}

.detail-signal-list {
  display: flex;
  flex-wrap: wrap;
}

.change-row,
.score-line {
  display: grid;
  grid-template-columns: 54px 82px 1fr;
  gap: 8px;
  align-items: baseline;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
  font-size: 12px;
}

.score-line {
  grid-template-columns: 1fr 76px;
}

.score-line em {
  grid-column: 1 / -1;
}

.change-row span,
.score-line span {
  color: var(--muted);
}

.change-row strong.up,
.score-line strong.up {
  color: var(--red);
}

.change-row strong.down,
.score-line strong.down {
  color: var(--green);
}

.change-row em,
.score-line em {
  color: var(--muted);
  font-style: normal;
}

.profit-list {
  display: grid;
  gap: 8px;
  margin-top: 16px;
}

.profit-bar {
  display: grid;
  grid-template-columns: 68px 1fr 88px;
  gap: 8px;
  align-items: center;
  font-size: 12px;
}

.bar-track {
  height: 10px;
  border-radius: 999px;
  background: #e3e0d1;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--blue), var(--green));
}

.toast {
  position: fixed;
  right: 24px;
  bottom: 24px;
  max-width: 360px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--ink);
  color: var(--panel);
  box-shadow: var(--shadow);
}

@media (max-width: 980px) {
  .app-header,
  .layout {
    grid-template-columns: 1fr;
  }

  .layout {
    grid-template-areas:
      "overview"
      "detail"
      "candidates";
    padding: 14px;
  }

  .app-header {
    padding: 22px 14px 14px;
  }

  .detail {
    position: static;
    max-height: none;
    display: block;
    overflow: visible;
  }

  .detail-content {
    overflow: visible;
  }

  .summary-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

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

  .summary-grid,
  .detail-grid {
    grid-template-columns: 1fr;
  }

  .daily-sync {
    grid-template-columns: 1fr;
  }
}
