/* ─────────────────────────────────────────────────────────────
   KRX 시장경보 스크리너 — "시장감시 관제 데스크"
   딥 잉크 터미널 + 에디토리얼 세리프, 3단계 신호등 체계
   ───────────────────────────────────────────────────────────── */

:root {
  --bg: #0a0c10;
  --bg-soft: #0e1117;
  --panel: #12161e;
  --panel-2: #171c26;
  --line: #232b3a;
  --line-soft: #1a2130;
  --ink: #e9ecf3;
  --ink-2: #98a1b3;
  --ink-3: #5f6878;

  --caution: #f5b841;
  --warning: #ff7a45;
  --risk: #ff4d5e;
  --radar: #3fd8c2;

  --up: #f04452;      /* 상승 = 빨강 (한국 관례) */
  --down: #3485fa;    /* 하락 = 파랑 */
  --flat: #8b93a7;

  --serif: 'Noto Serif KR', serif;
  --sans: 'Pretendard Variable', Pretendard, sans-serif;
  --mono: 'IBM Plex Mono', monospace;

  --tier: var(--caution);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

[hidden] { display: none !important; }

html { scrollbar-color: #2a3345 var(--bg); }

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

/* ── 배경: 관제실 그리드 + 상단 신호 스트립 ── */

.signal-strip {
  position: fixed; top: 0; left: 0; right: 0; height: 3px; z-index: 50;
  background: linear-gradient(90deg,
    var(--caution) 0 33.4%,
    var(--warning) 33.4% 66.7%,
    var(--risk) 66.7% 100%);
}

.bg-field {
  position: fixed; inset: 0; z-index: -1; pointer-events: none;
  background:
    radial-gradient(1100px 480px at 12% -8%, rgba(245, 184, 65, 0.07), transparent 60%),
    radial-gradient(900px 420px at 88% -12%, rgba(255, 77, 94, 0.05), transparent 60%),
    repeating-linear-gradient(0deg, rgba(255,255,255,0.016) 0 1px, transparent 1px 52px),
    repeating-linear-gradient(90deg, rgba(255,255,255,0.016) 0 1px, transparent 1px 52px),
    var(--bg);
}
.bg-field::after {
  content: ''; position: absolute; inset: 0; opacity: 0.35;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='140' height='140'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2'/%3E%3CfeColorMatrix values='0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0.05 0'/%3E%3C/filter%3E%3Crect width='140' height='140' filter='url(%23n)'/%3E%3C/svg%3E");
}

.shell { max-width: 1240px; margin: 0 auto; padding: 34px 22px 60px; }

/* ── 마스트헤드 ── */

.masthead {
  display: flex; justify-content: space-between; align-items: flex-end; gap: 24px;
  padding-bottom: 22px; margin-bottom: 18px;
  border-bottom: 1px solid var(--line);
  animation: rise .5s cubic-bezier(.2,.7,.2,1) both;
}

.kicker {
  font-family: var(--mono); font-size: 10.5px; font-weight: 500;
  letter-spacing: 0.32em; color: var(--ink-3); margin-bottom: 10px;
}
.kicker::before { content: '● '; color: var(--caution); font-size: 8px; vertical-align: 1px; }

.brand h1 {
  font-family: var(--serif); font-weight: 900; font-size: clamp(30px, 4.2vw, 44px);
  line-height: 1.12; letter-spacing: -0.01em;
}
.brand h1 em {
  font-style: normal;
  background: linear-gradient(90deg, var(--caution), var(--warning) 55%, var(--risk));
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.brand .sub { margin-top: 9px; color: var(--ink-2); font-size: 13px; max-width: 560px; }

.deck { text-align: right; flex-shrink: 0; }
.clock-line { display: flex; align-items: baseline; justify-content: flex-end; gap: 9px; }
.clock { font-family: var(--mono); font-size: 26px; font-weight: 600; letter-spacing: 0.04em; }
.tz { font-family: var(--mono); font-size: 11px; color: var(--ink-3); letter-spacing: 0.2em; }

.live-dot {
  width: 8px; height: 8px; border-radius: 50%; background: #29d07e; align-self: center;
  box-shadow: 0 0 0 0 rgba(41, 208, 126, 0.5);
  animation: pulse 2.2s ease-out infinite;
}
.live-dot.err { background: var(--risk); box-shadow: 0 0 0 0 rgba(255, 77, 94, 0.5); }

.status-line { font-family: var(--mono); font-size: 11px; color: var(--ink-2); margin-top: 7px; }
.status-line.dim { color: var(--ink-3); margin-top: 3px; }

@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(41, 208, 126, 0.45); }
  70% { box-shadow: 0 0 0 9px rgba(41, 208, 126, 0); }
  100% { box-shadow: 0 0 0 0 rgba(41, 208, 126, 0); }
}

/* ── 3단계 신호 탭 ── */

.tier-tabs {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px;
  margin: 22px 0 16px;
}

.tier-tab {
  --c: var(--caution);
  position: relative; text-align: left; cursor: pointer;
  display: grid; grid-template-columns: 1fr auto; grid-template-areas: 'head count' 'desc count';
  column-gap: 12px; row-gap: 3px; align-items: center;
  padding: 15px 18px 15px 24px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  color: var(--ink); font-family: var(--sans);
  transition: border-color .18s, background .18s, transform .18s;
  animation: rise .5s cubic-bezier(.2,.7,.2,1) both;
}
.tier-tab:nth-child(1) { animation-delay: .05s; }
.tier-tab:nth-child(2) { animation-delay: .12s; }
.tier-tab:nth-child(3) { animation-delay: .19s; }
.tier-tab:nth-child(4) { animation-delay: .26s; }
.t-radar { --c: var(--radar); }
.t-warning { --c: var(--warning); }
.t-risk { --c: var(--risk); }

.tier-tab .tick {
  position: absolute; left: 0; top: 10px; bottom: 10px; width: 4px;
  border-radius: 0 3px 3px 0; background: var(--c); opacity: 0.45;
  transition: opacity .18s;
}
.tier-tab:hover { border-color: color-mix(in srgb, var(--c) 45%, var(--line)); transform: translateY(-1px); }
.tier-tab.active {
  border-color: color-mix(in srgb, var(--c) 65%, var(--line));
  background: color-mix(in srgb, var(--c) 9%, var(--panel));
  box-shadow: 0 8px 28px -14px color-mix(in srgb, var(--c) 55%, transparent);
}
.tier-tab.active .tick { opacity: 1; }

.t-head { grid-area: head; display: flex; align-items: baseline; gap: 8px; }
.t-level { font-family: var(--mono); font-size: 10px; font-weight: 600; letter-spacing: .14em; color: var(--c); }
.t-name { font-weight: 800; font-size: 17px; letter-spacing: -0.01em; }
.t-desc { grid-area: desc; font-size: 11.5px; color: var(--ink-3); }
.t-count { grid-area: count; text-align: right; }
.t-count em {
  display: block; font-style: normal; font-family: var(--mono);
  font-size: 30px; font-weight: 600; line-height: 1; color: var(--c);
  font-variant-numeric: tabular-nums;
}
.t-count small { font-size: 10.5px; color: var(--ink-3); letter-spacing: .06em; }

/* ── 컨트롤 바 ── */

.controls {
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 12px 14px; display: flex; flex-direction: column; gap: 10px;
  animation: rise .5s cubic-bezier(.2,.7,.2,1) .24s both;
}

.ctrl-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }

.seg { display: inline-flex; background: var(--bg-soft); border: 1px solid var(--line-soft); border-radius: 7px; padding: 3px; gap: 2px; }
.seg button {
  border: 0; background: transparent; color: var(--ink-2); cursor: pointer;
  font-family: var(--sans); font-size: 12.5px; font-weight: 600;
  padding: 5px 12px; border-radius: 5px; transition: background .15s, color .15s;
}
.seg button:hover { color: var(--ink); }
.seg button.on { background: color-mix(in srgb, var(--tier) 16%, var(--panel-2)); color: var(--ink); }

.date-range { display: inline-flex; align-items: center; gap: 6px; }
.date-range input[type="date"] {
  background: var(--bg-soft); border: 1px solid var(--line-soft); border-radius: 6px;
  color: var(--ink); font-family: var(--mono); font-size: 12px; padding: 5px 8px;
  color-scheme: dark;
}
.tilde { color: var(--ink-3); }

.btn-min {
  background: var(--bg-soft); border: 1px solid var(--line); border-radius: 6px;
  color: var(--ink-2); font-family: var(--sans); font-size: 12.5px; font-weight: 600;
  padding: 6px 12px; cursor: pointer; transition: border-color .15s, color .15s;
}
.btn-min:hover { color: var(--ink); border-color: color-mix(in srgb, var(--tier) 50%, var(--line)); }

.chips {
  display: flex; align-items: center; gap: 6px; flex: 1; min-width: 0;
  flex-wrap: nowrap; overflow-x: auto; overscroll-behavior-x: contain;
  scrollbar-width: none;
  mask-image: linear-gradient(90deg, #000 calc(100% - 26px), transparent);
  -webkit-mask-image: linear-gradient(90deg, #000 calc(100% - 26px), transparent);
}
.chips::-webkit-scrollbar { display: none; }
.chips:empty { display: none; }
.chip {
  border: 1px solid var(--line); background: transparent; border-radius: 999px;
  color: var(--ink-2); font-size: 11.5px; font-weight: 600; font-family: var(--sans);
  padding: 4px 11px; cursor: pointer; white-space: nowrap; flex: 0 0 auto;
  transition: all .15s;
}
.chip .n { font-family: var(--mono); font-size: 10px; color: var(--ink-3); margin-left: 4px; }
.chip.on {
  border-color: color-mix(in srgb, var(--tier) 60%, var(--line));
  background: color-mix(in srgb, var(--tier) 12%, transparent);
  color: var(--ink);
}
.chip.on .n { color: var(--tier); }

.right-tools { display: flex; align-items: center; gap: 9px; margin-left: auto; }

.search {
  display: inline-flex; align-items: center; gap: 7px;
  background: var(--bg-soft); border: 1px solid var(--line-soft); border-radius: 7px;
  padding: 0 11px; color: var(--ink-3);
  transition: border-color .15s;
}
.search:focus-within { border-color: color-mix(in srgb, var(--tier) 55%, var(--line)); color: var(--tier); }
.search input {
  background: transparent; border: 0; outline: 0; color: var(--ink);
  font-family: var(--sans); font-size: 13px; padding: 7px 0; width: 148px;
}
.search input::placeholder { color: var(--ink-3); }

.row-count { font-family: var(--mono); font-size: 11.5px; color: var(--ink-3); white-space: nowrap; }

/* ── 테이블 ── */

.board { margin-top: 14px; animation: rise .5s cubic-bezier(.2,.7,.2,1) .3s both; }

.table-wrap {
  position: relative;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  overflow: auto; max-height: calc(100vh - 130px); min-height: 260px;
}

table { width: 100%; border-collapse: collapse; min-width: 980px; }

thead th {
  position: sticky; top: 0; z-index: 5;
  background: var(--panel-2);
  font-size: 10.5px; font-weight: 700; letter-spacing: 0.1em; color: var(--ink-3);
  text-align: right; padding: 10px 14px; white-space: nowrap;
  border-bottom: 1px solid var(--line); user-select: none;
}
thead th.txt { text-align: left; }
thead th.sortable { cursor: pointer; }
thead th.sortable:hover { color: var(--ink); }
thead th .arr { font-family: var(--mono); color: var(--tier); margin-left: 3px; }

tbody tr {
  border-bottom: 1px solid var(--line-soft);
  cursor: pointer; position: relative;
  animation: rowrise .34s cubic-bezier(.2,.7,.2,1) both;
  animation-delay: calc(var(--i) * 22ms);
}
tbody tr:hover { background: color-mix(in srgb, var(--tier) 5%, transparent); }
tbody tr:last-child { border-bottom: 0; }

tbody td {
  padding: 9px 14px; text-align: right; white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
tbody td.txt { text-align: left; }
td.mono, .mono { font-family: var(--mono); font-size: 12.5px; }

/* 종목 셀 */
.stock-cell { display: flex; align-items: center; gap: 9px; min-width: 190px; }
.stock-tick { width: 3px; height: 26px; border-radius: 2px; background: var(--tier); opacity: .55; flex-shrink: 0; }
.stock-meta { line-height: 1.25; }
.stock-name { font-weight: 700; font-size: 13.5px; display: flex; align-items: center; gap: 6px; }
.stock-code { font-family: var(--mono); font-size: 10.5px; color: var(--ink-3); letter-spacing: .05em; }

.mkt-tag {
  font-family: var(--mono); font-size: 9px; font-weight: 600; letter-spacing: .08em;
  padding: 1.5px 5px; border-radius: 4px;
  border: 1px solid var(--line); color: var(--ink-3);
}
.mkt-tag.kospi { color: #7fb2ff; border-color: #2a3f63; }
.mkt-tag.kosdaq { color: #66d9b8; border-color: #1f4a3d; }
.mkt-tag.konex { color: #c7a6ff; border-color: #3d3260; }

.new-badge {
  font-size: 9px; font-weight: 800; letter-spacing: .1em; color: #0a0c10;
  background: var(--tier); border-radius: 4px; padding: 1.5px 5px;
  animation: blink 1.6s ease-in-out infinite;
}
@keyframes blink { 50% { opacity: .55; } }

.reason-tag {
  display: inline-block; font-size: 11.5px; color: var(--ink-2);
  border: 1px solid var(--line); border-radius: 5px; padding: 2px 8px;
  background: var(--bg-soft);
}

/* 임박 레이더 구분 필 */
.kind-pill {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 11px; font-weight: 700; white-space: nowrap;
  padding: 2.5px 9px; border-radius: 999px; border: 1px solid var(--line);
}
.kind-pill.p-warn { color: var(--warning); border-color: color-mix(in srgb, var(--warning) 55%, transparent); background: color-mix(in srgb, var(--warning) 10%, transparent); }
.kind-pill.p-risk { color: var(--risk); border-color: color-mix(in srgb, var(--risk) 55%, transparent); background: color-mix(in srgb, var(--risk) 10%, transparent); }
.kind-pill.p-notice { background: transparent; border-style: dashed; }
.kind-pill.p-released { color: var(--ink-3); background: transparent; }

.due-badge {
  font-size: 9.5px; font-weight: 800; letter-spacing: .04em; white-space: nowrap;
  color: #0a0c10; background: var(--radar); border-radius: 4px;
  padding: 1.5px 6px; margin-left: 6px;
  animation: blink 1.6s ease-in-out infinite;
}
.due-badge.dminus {
  background: transparent; color: var(--radar);
  border: 1px solid color-mix(in srgb, var(--radar) 60%, transparent);
  animation: none;
}

/* 상태 배지 */
.state-badge { display: inline-flex; align-items: center; gap: 6px; font-size: 11.5px; font-weight: 700; }
.state-badge .dot { width: 6px; height: 6px; border-radius: 50%; }
.state-badge.active { color: var(--tier); }
.state-badge.active .dot { background: var(--tier); animation: pulse 2s infinite; }
.state-badge.released { color: var(--ink-3); }
.state-badge.released .dot { background: var(--ink-3); }
.halt-badge {
  font-size: 9.5px; font-weight: 800; color: var(--risk);
  border: 1px solid color-mix(in srgb, var(--risk) 55%, transparent);
  border-radius: 4px; padding: 1px 5px; margin-left: 5px; letter-spacing: .06em;
}

/* 등락 색상 */
.up { color: var(--up); }
.down { color: var(--down); }
.flat { color: var(--flat); }

.rate-cell { position: relative; }
.rate-bar {
  position: absolute; right: 10px; top: 50%; transform: translateY(-50%);
  height: 66%; border-radius: 3px; opacity: .13; pointer-events: none;
}
.rate-bar.up { background: var(--up); }
.rate-bar.down { background: var(--down); }
.rate-val { position: relative; font-weight: 600; }

.cell-flash { animation: flash .9s ease-out; }
@keyframes flash { 0% { background: rgba(255,255,255,.14); } 100% { background: transparent; } }

/* ── 상태 패널 (로딩/빈/에러) ── */

.state-panel {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  background: color-mix(in srgb, var(--panel) 82%, transparent);
  backdrop-filter: blur(2px); z-index: 8;
}
.state-panel[hidden] { display: none; }
.sp-inner { text-align: center; color: var(--ink-2); font-size: 13px; display: grid; gap: 12px; justify-items: center; }
.sp-spinner {
  width: 30px; height: 30px; border-radius: 50%;
  border: 2.5px solid var(--line); border-top-color: var(--tier);
  animation: spin .8s linear infinite;
}
.sp-spinner.hide { display: none; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── 푸터 ── */

.foot { margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--line-soft); display: grid; gap: 4px; }
.foot p { font-size: 11.5px; color: var(--ink-2); }
.foot .dim { color: var(--ink-3); }
.foot strong { color: var(--ink-2); font-weight: 700; }

/* ── 애니메이션 공통 ── */

@keyframes rise { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }
@keyframes rowrise { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: none; } }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: #262f40; border-radius: 6px; border: 2px solid var(--panel); }
::-webkit-scrollbar-track { background: transparent; }

/* ── 반응형 ── */

@media (max-width: 1080px) {
  .tier-tabs { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 900px) {
  .shell { padding: 24px 14px 44px; }
  .masthead { flex-direction: column; align-items: flex-start; gap: 14px; }
  .deck { text-align: left; }
  .clock-line { justify-content: flex-start; }
  .tier-tab { padding: 12px 14px 12px 20px; }
  .t-count em { font-size: 24px; }
}

@media (max-width: 620px) {
  .tier-tabs { grid-template-columns: 1fr; gap: 8px; }
  .right-tools { margin-left: 0; width: 100%; }
  .search { flex: 1; }
  .search input { width: 100%; }
  .table-wrap { max-height: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
