:root {
  color-scheme: light;
  --ink: #14213d;
  --muted: #64748b;
  --line: #dbe3ec;
  --line-strong: #bbc8d6;
  --surface: #ffffff;
  --surface-soft: #f5f8fb;
  --navy: #17324d;
  --teal: #007f78;
  --teal-soft: #dff3f0;
  --red: #c73e4d;
  --amber: #a56100;
  --blue: #2b6cb0;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: #edf2f6;
  color: var(--ink);
  font-family: "Segoe UI", "Malgun Gothic", Arial, sans-serif;
  font-size: 14px;
  letter-spacing: 0;
}

button, input { font: inherit; letter-spacing: 0; }

.app-header {
  min-height: 58px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 9px 20px;
  color: #fff;
  background: var(--navy);
  border-bottom: 3px solid var(--teal);
}

.brand-block, .header-actions, .toolbar, .segmented, .source-status, .date-field, .search-field {
  display: flex;
  align-items: center;
}

.brand-block { gap: 12px; }
.brand-block h1 { margin: 0; font-size: 19px; font-weight: 700; }
.brand-block p { margin: 3px 0 0; color: #b8cadb; font-size: 12px; }

.brand-mark {
  width: 34px;
  height: 34px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  align-items: end;
  gap: 3px;
  padding: 7px;
  border: 1px solid #52718e;
}
.brand-mark span { background: #69d1c5; }
.brand-mark span:nth-child(1) { height: 40%; }
.brand-mark span:nth-child(2) { height: 75%; }
.brand-mark span:nth-child(3) { height: 100%; }

.header-actions { gap: 10px; }
.date-field { gap: 8px; color: #bfd0df; font-size: 12px; }
.date-field input {
  width: 138px;
  height: 34px;
  padding: 0 10px;
  color: #fff;
  border: 1px solid #53708a;
  background: #203f5a;
}

.icon-button {
  width: 34px;
  height: 34px;
  border: 1px solid #66839b;
  color: #fff;
  background: #203f5a;
  cursor: pointer;
  font-size: 20px;
}
.icon-button:hover { background: #2a516e; }
.icon-button.loading { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.source-status {
  gap: 7px;
  min-width: 122px;
  height: 34px;
  padding: 0 10px;
  border-left: 1px solid #56718a;
  color: #c9d7e3;
  font-size: 12px;
}
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: #f0b45c; }
.source-status[data-state="live"] .status-dot { background: #5bd6a0; }
.source-status[data-state="cache"] .status-dot { background: #f1bf65; }
.source-status[data-state="error"] .status-dot { background: #f07882; }

main { padding: 0 18px 20px; }

.summary-band {
  display: grid;
  grid-template-columns: repeat(4, minmax(128px, 1fr));
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.summary-item {
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 10px 16px;
  border-right: 1px solid var(--line);
}
.summary-item span, .visual-heading { color: var(--muted); font-size: 10px; }
.summary-item strong { margin-top: 4px; font-size: 18px; font-weight: 700; }

.summary-visual { padding: 16px 20px 22px; overflow: hidden; }
.trend-panel { padding: 14px 14px 12px; overflow: hidden; }
.trend-panel.wide { min-height: 276px; }
.summary-visual { border-right: 1px solid var(--line); }
.visual-heading { display: flex; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
#trendTitle {
  flex: 1;
  color: var(--ink);
  font-weight: 700;
  text-align: center;
}
.chart-actions {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  white-space: nowrap;
}
#trendLatest { display: none; }
.chart-action {
  height: 24px;
  padding: 0 8px;
  border: 1px solid var(--line-strong);
  color: #35516a;
  background: #fff;
  cursor: pointer;
  font-size: 11px;
}
.chart-action:hover { background: #edf5f7; border-color: #8fb9bd; }
.trend-legend {
  display: flex;
  justify-content: center;
  gap: 16px;
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 10px;
}
.trend-legend span { display: inline-flex; align-items: center; gap: 6px; }
.trend-legend i { width: 18px; height: 3px; display: inline-block; }
.legend-ratio { background: #447cff; }
.legend-loan { background: #9a5cf7; }
.legend-cap { background: #111827; }
.ratio-chart { display: grid; grid-template-columns: repeat(8, minmax(18px, 1fr)); gap: 8px; height: 50px; align-items: end; }
.chart-bar {
  min-width: 0;
  border: 0;
  background: var(--teal);
  position: relative;
  cursor: pointer;
}
.chart-bar:hover { background: #006a65; }
.chart-bar span {
  position: absolute;
  left: 50%;
  bottom: -18px;
  transform: translateX(-50%);
  width: 100%;
  overflow: hidden;
  color: #718096;
  font-size: 9px;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: center;
}

.trend-chart { height: 228px; color: var(--muted); }
.trend-chart svg {
  display: block;
  width: 100%;
  height: 228px;
  overflow: visible;
}
.trend-axis {
  stroke: #cfd8e2;
  stroke-width: 1;
}
.trend-y-tick line {
  stroke: #edf2f6;
  stroke-width: 1;
}
.trend-y-tick text {
  fill: #718096;
  font-size: 10px;
  text-anchor: end;
}
.trend-y-tick.right text {
  fill: #718096;
  text-anchor: start;
}
.trend-x-tick line {
  stroke: #edf2f6;
  stroke-width: 1;
}
.trend-x-tick text {
  fill: #718096;
  font-size: 10px;
  text-anchor: middle;
}
.trend-line {
  fill: none;
  stroke-width: 2.2;
  stroke-linejoin: round;
  stroke-linecap: round;
}
.ratio-line { stroke: #447cff; }
.loan-line { stroke: #9a5cf7; stroke-width: 2; }
.cap-line { stroke: #111827; stroke-width: 2; opacity: 0.92; }
.cap-axis { stroke: #cfd8e2; }
.trend-hover-dot.ratio {
  fill: #fff;
  stroke: #447cff;
  stroke-width: 2;
}
.trend-hover-dot.loan {
  fill: #fff;
  stroke: #9a5cf7;
  stroke-width: 2;
}
.trend-hover-dot.cap {
  fill: #fff;
  stroke: #111827;
  stroke-width: 2;
}
.trend-hitbox {
  fill: transparent;
  pointer-events: all;
}
.trend-hover-line,
.trend-hover-dot,
.trend-tooltip {
  display: none;
}
.trend-hit:hover .trend-hover-line,
.trend-hit:hover .trend-hover-dot,
.trend-hit:hover .trend-tooltip {
  display: block;
}
.trend-hover-line {
  stroke: #c4ccd6;
  stroke-width: 1;
  stroke-dasharray: 4 4;
}
.trend-hover-dot {
  pointer-events: none;
}
.trend-tooltip rect {
  fill: #fff;
  stroke: #d9e1ea;
  filter: drop-shadow(0 4px 8px rgba(20, 33, 61, 0.16));
}
.trend-tooltip text {
  fill: #24364b;
  font-size: 10px;
}
.trend-note {
  margin-top: -5px;
  color: #6f4200;
  font-size: 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.trend-loading, .trend-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 218px;
  color: var(--muted);
  background: #f7fafc;
  border: 1px dashed #d6e0e8;
  font-size: 12px;
}

body.chart-expanded {
  overflow: hidden;
}
body.chart-expanded::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(12, 25, 39, 0.52);
}
body.chart-expanded .detail-panel {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 21;
  width: min(980px, calc(100vw - 84px));
  height: min(440px, calc(100vh - 92px));
  background: #fff;
  border: 1px solid #b8c7d5;
  box-shadow: 0 18px 40px rgba(10, 24, 39, 0.28);
}
body.chart-expanded .detail-panel .trend-panel {
  position: static;
  height: 100%;
  padding: 18px 20px;
}
body.chart-expanded .trend-chart {
  height: calc(100% - 68px);
  min-height: 300px;
}
body.chart-expanded .trend-chart svg {
  height: 100%;
}

.warning-banner {
  display: flex;
  gap: 12px;
  margin: 14px 0 0;
  padding: 10px 14px;
  color: #6f4200;
  background: #fff3d8;
  border-left: 3px solid #e3a432;
  font-size: 12px;
}

.tabs {
  display: flex;
  gap: 0;
  margin-top: 12px;
  border-bottom: 1px solid var(--line-strong);
}
.tab {
  padding: 9px 16px;
  border: 0;
  border-bottom: 3px solid transparent;
  color: #5e6e80;
  background: transparent;
  cursor: pointer;
}
.tab.active { color: var(--navy); border-bottom-color: var(--teal); font-weight: 700; }

.workspace { background: var(--surface); }
.toolbar {
  min-height: 50px;
  gap: 18px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
}
.segmented { border: 1px solid var(--line-strong); }
.segmented button {
  min-width: 78px;
  height: 34px;
  border: 0;
  border-right: 1px solid var(--line-strong);
  color: #5c6c7c;
  background: #fff;
  cursor: pointer;
}
.segmented button:last-child { border-right: 0; }
.segmented button.active { color: #fff; background: var(--navy); }

.view-title { min-width: 0; flex: 1; }
.view-title h2 { margin: 0; font-size: 15px; }
.view-title p { margin: 4px 0 0; color: var(--muted); font-size: 11px; }

.search-field {
  width: 260px;
  height: 34px;
  gap: 7px;
  padding: 0 10px;
  border: 1px solid var(--line-strong);
  color: #718096;
}
.search-field input { width: 100%; border: 0; outline: 0; color: var(--ink); background: transparent; }

.workbench {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 420px;
  min-height: calc(100vh - 226px);
}
.table-region { min-width: 0; border-right: 1px solid var(--line); }
.detail-panel {
  min-width: 0;
  background: #fbfdff;
}
.detail-panel .trend-panel {
  position: sticky;
  top: 0;
}
.table-wrap { position: relative; min-height: 430px; max-height: calc(100vh - 278px); overflow: auto; }
table { width: 100%; border-collapse: separate; border-spacing: 0; }
th, td {
  padding: 7px 9px;
  border-bottom: 1px solid #e5ebf1;
  text-align: left;
  white-space: nowrap;
}
th {
  position: sticky;
  top: 0;
  z-index: 2;
  color: #3c4d60;
  background: #f2f6f9;
  border-bottom-color: #cbd6e0;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}
tbody tr:hover td { background: #f7fafc; }
tbody tr.selected td { background: #eef8f6; }
td.numeric, th.numeric { text-align: right; font-variant-numeric: tabular-nums; }
td.rank { width: 54px; color: #6b7a8b; text-align: center; }
.stock-name { font-weight: 650; }
.stock-link {
  padding: 0;
  border: 0;
  color: var(--ink);
  background: transparent;
  cursor: pointer;
  font-weight: 650;
}
.stock-link:hover { color: var(--teal); text-decoration: underline; }
.stock-code { color: #6a7b8c; font-family: Consolas, monospace; font-size: 12px; }
.market-label { font-size: 11px; font-weight: 700; }
.market-label.kospi { color: var(--blue); }
.market-label.kosdaq { color: var(--teal); }

.ratio-cell { min-width: 130px; }
.ratio-value { display: flex; align-items: center; justify-content: flex-end; gap: 8px; }
.ratio-track { width: 62px; height: 5px; background: #e7edf2; }
.ratio-track i { display: block; height: 100%; background: var(--red); }
.loading-value {
  display: inline-block;
  padding: 2px 6px;
  color: #7a5a00;
  background: #fff5d7;
  border: 1px solid #ead18a;
  font-size: 11px;
}
.holder-button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 7px;
  border: 1px solid #bfd6d3;
  color: #006c67;
  background: #eff9f7;
  cursor: pointer;
}
.holder-button:disabled { color: #8493a1; border-color: #d9e1e8; background: #f6f8fa; cursor: default; }
.holder-detail td { padding: 0; background: #f6f9fb; }
.holder-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 12px 16px 14px 116px;
}
.holder-chip { padding: 4px 7px; color: #385267; background: #e6edf3; font-size: 11px; }

.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  background: #fff;
}
.empty-state[hidden] { display: none; }
.empty-state strong { margin-bottom: 7px; color: #415367; }

.table-footer {
  display: flex;
  justify-content: space-between;
  padding: 7px 12px;
  color: var(--muted);
  background: var(--surface-soft);
  border-top: 1px solid var(--line);
  font-size: 11px;
}

@media (max-width: 980px) {
  .app-header { align-items: flex-start; flex-direction: column; }
  .header-actions { width: 100%; }
  .summary-band { grid-template-columns: repeat(2, 1fr); }
  .summary-visual { min-height: 94px; border-right: 0; border-bottom: 1px solid var(--line); }
  .toolbar { align-items: flex-start; flex-wrap: wrap; }
  .view-title { order: -1; flex-basis: 100%; }
  .search-field { margin-left: auto; }
  .workbench { grid-template-columns: 1fr; }
  .table-region { border-right: 0; }
  .detail-panel { border-top: 1px solid var(--line); }
  .detail-panel .trend-panel { position: static; }
  .table-wrap { max-height: none; }
}

@media (max-width: 620px) {
  main { padding: 0 10px 18px; }
  .app-header { padding: 12px; }
  .header-actions { align-items: stretch; flex-wrap: wrap; }
  .date-field { flex: 1; }
  .date-field input { flex: 1; }
  .source-status { width: 100%; border-left: 0; padding-left: 0; }
  .summary-band { grid-template-columns: 1fr 1fr; }
  .summary-item { padding: 14px 12px; }
  .summary-item strong { font-size: 17px; }
  .tabs { overflow-x: auto; }
  .tab { flex: 0 0 auto; padding: 10px 12px; }
  .segmented, .search-field { width: 100%; }
  .segmented button { flex: 1; }
  .search-field { margin-left: 0; }
}
