:root {
  --bg: #0f1419;
  --surface: #1a2332;
  --surface2: #243044;
  --border: #2d3a4f;
  --text: #e8edf4;
  --muted: #8b9cb3;
  --primary: #3b82f6;
  --green: #22c55e;
  --red: #ef4444;
  --ask-text: #93c5fd;
  --ask-bg: rgba(30, 58, 95, 0.65);
  --hoga-row-bg: rgba(30, 58, 95, 0.65);
  --ask-bar: rgba(96, 165, 250, 0.55);
  --bid-text: #f87171;
  --bid-bg: rgba(30, 58, 95, 0.65);
  --bid-bar: rgba(248, 113, 113, 0.5);
  --hoga-current-bg: #b8860b;
  --hoga-row-height: 25px;
  --hoga-depth: 10;
  --hoga-head-height: 23px;
  --trade-panel-height: 286px;
  --radius: 10px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: "Pretendard", "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow: hidden;
}

.app {
  max-width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 10px 12px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  flex-shrink: 0;
}

.brand { display: flex; align-items: center; gap: 12px; }
.brand h1 { font-size: 1.2rem; font-weight: 700; }

.badge {
  font-size: 0.72rem;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface2);
  color: var(--muted);
}
.badge.connected { background: #14532d; color: #86efac; }
.badge.paper { background: #1e3a5f; color: #93c5fd; }
.badge.error { background: #450a0a; color: #fca5a5; }

/* 3단 레이아웃 */
.workspace {
  display: grid;
  grid-template-columns: 510px 1fr 280px;
  gap: 10px;
  flex: 1;
  min-height: 0;
}

.col-left, .col-center, .col-right {
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.col-right { overflow-y: auto; gap: 8px; }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
}

.col-left.panel, .col-center.panel {
  flex: 1;
  overflow: hidden;
}

.col-title {
  font-size: 0.9rem;
  color: var(--muted);
  margin-bottom: 10px;
}

/* 왼쪽 종목조회 */
.screener-filters.compact {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 8px;
}
.screener-filters.compact select,
.screener-filters.compact .btn.full {
  grid-column: span 2;
}
.screener-filters select {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 7px 8px;
  border-radius: 6px;
  font-size: 0.82rem;
}
.screener-status {
  color: var(--muted);
  font-size: 0.78rem;
  margin-bottom: 6px;
}
.screener-table-wrap {
  flex: 1;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.screener-table-wrap table { font-size: 0.76rem; width: 100%; border-collapse: collapse; }
.screener-table-wrap th, .screener-table-wrap td {
  padding: 6px 4px;
  text-align: right;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.screener-table-wrap th:first-child,
.screener-table-wrap td:first-child,
.screener-table-wrap th:nth-child(2),
.screener-table-wrap td:nth-child(2) { text-align: left; }
.screener-table-wrap th {
  color: var(--muted);
  font-weight: 500;
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 1;
}
.screener-table-wrap tbody tr { cursor: pointer; }
.screener-table-wrap tbody tr:hover { background: var(--surface2); }
.screener-table-wrap tbody tr.selected { background: #3d2f1a; }

/* 중앙 차트 */
.col-center { padding: 10px; display: flex; flex-direction: column; min-height: 0; }
.chart-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 6px;
  flex-shrink: 0;
}
.quote-bar { display: flex; flex-wrap: wrap; align-items: baseline; gap: 8px; }
.quote-bar .price { font-size: 1.3rem; font-weight: 700; }
.period-tabs { display: flex; gap: 4px; flex-shrink: 0; }
.period-tab {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.82rem;
}
.period-tab.active { background: var(--primary); color: #fff; border-color: var(--primary); }

#chart-container {
  flex: 0 0 25%;
  min-height: 90px;
  border-radius: 8px;
  overflow: hidden;
  position: relative;
  border: 1px solid var(--border);
  background: #121820;
}
.chart-empty {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 0.85rem;
  pointer-events: none;
  z-index: 2;
}

.chart-tooltip {
  position: absolute;
  z-index: 5;
  min-width: 185px;
  padding: 7px 9px;
  background: rgba(245, 247, 250, 0.96);
  color: #111827;
  border: 1px solid #6b7280;
  border-radius: 4px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
  font-size: 0.72rem;
  line-height: 1.45;
  pointer-events: none;
}

.chart-tooltip .tooltip-title {
  font-weight: 700;
  margin-bottom: 4px;
  border-bottom: 1px solid #cbd5e1;
  padding-bottom: 2px;
}

.chart-tooltip .up { color: #dc2626; }
.chart-tooltip .down { color: #2563eb; }

.trade-bottom {
  flex: 0 0 calc(var(--trade-panel-height) + 6px);
  margin-top: 6px;
  border-top: 1px solid var(--border);
  padding-top: 4px;
  display: flex;
  gap: 8px;
  align-items: flex-start;
  min-height: 0;
  overflow: hidden;
}

.orderbook-panel {
  flex: 0 0 70%;
  min-width: 0;
  height: var(--trade-panel-height);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.trade-side {
  flex: 1;
  min-width: 220px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
}

.trade-side .order-panel {
  flex: 0 0 var(--trade-panel-height);
  padding: 9px 12px;
}

.trade-side-placeholder {
  flex: 1;
  min-height: 44px;
}

.profit-alert-panel {
  flex: 1;
  min-height: 100px;
}

.col-right .profit-alert-panel {
  flex: 0 0 auto;
}

.profit-alert-panel h2 {
  margin-bottom: 10px;
}

.alert-row {
  display: grid;
  grid-template-columns: auto 58px 1fr auto;
  align-items: center;
  gap: 6px;
  margin-bottom: 8px;
  font-size: 0.78rem;
  color: var(--text);
}

.alert-row input[type="number"] {
  width: 58px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 5px 6px;
  border-radius: 5px;
  font-size: 0.78rem;
  text-align: right;
}

.alert-check {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: var(--muted);
  white-space: nowrap;
}

.alert-check input {
  width: 14px;
  height: 14px;
}

/* 다크 테마 호가창 */
.hoga-hts {
  background: #152238;
  border-radius: 6px;
  padding: 0;
  flex: 0 0 70%;
  align-self: flex-start;
  min-height: 0;
  border: 1px solid var(--border);
  overflow: hidden;
}
.hoga-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.72rem;
  table-layout: fixed;
  color: var(--text);
}
.hoga-table thead th {
  background: var(--surface2);
  color: var(--muted);
  font-weight: 600;
  padding: 2px;
  border: 1px solid var(--border);
  text-align: center;
  font-size: 0.68rem;
}
.hoga-table td {
  border: 1px solid var(--border);
  padding: 0 3px;
  text-align: right;
  height: var(--hoga-row-height);
  line-height: var(--hoga-row-height);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.hoga-table tr.row-empty td {
  background: var(--hoga-row-bg);
}
.hoga-table .col-price,
.hoga-table td.price {
  text-align: center;
  font-weight: 600;
}
.hoga-table .col-chg { width: 11%; }
.hoga-table .col-qty { width: 18%; }
.hoga-table .col-price { width: 16%; }
.hoga-table .col-rate { width: 13%; }

/* 매도: 연한 파란 글씨 / 매수: 빨간 글씨 — 배경은 동일 파랑 */
.hoga-table tr.row-ask td,
.hoga-table tr.row-bid td {
  background: var(--hoga-row-bg);
}
.hoga-table tr.row-ask:not(.row-current) td,
.hoga-table tr.row-ask:not(.row-current) .hoga-chg,
.hoga-table tr.row-ask:not(.row-current) .hoga-rate,
.hoga-table tr.row-ask:not(.row-current) .hoga-qty-cell span,
.hoga-table tr.row-ask:not(.row-current) .info-label,
.hoga-table tr.row-ask:not(.row-current) .info-value {
  color: var(--ask-text);
}

.hoga-table tr.row-bid:not(.row-current) td,
.hoga-table tr.row-bid:not(.row-current) .hoga-chg,
.hoga-table tr.row-bid:not(.row-current) .hoga-rate,
.hoga-table tr.row-bid:not(.row-current) .hoga-qty-cell span,
.hoga-table tr.row-bid:not(.row-current) .info-label,
.hoga-table tr.row-bid:not(.row-current) .info-value {
  color: var(--bid-text);
}

/* 매도·매수 경계 구분선 */
.hoga-table tr.row-ask + tr.row-bid td {
  border-top: 3px solid #000;
}

/* 현재 체결가: 노란 바탕 + 흰색 굵은 글씨 */
.hoga-table tr.row-current td {
  background: var(--hoga-current-bg) !important;
  color: #fff !important;
  font-weight: 700;
}
.hoga-table tr.row-current td.price,
.hoga-table tr.row-current .hoga-chg,
.hoga-table tr.row-current .hoga-rate,
.hoga-table tr.row-current .hoga-qty-cell span,
.hoga-table tr.row-current .info-label,
.hoga-table tr.row-current .info-value {
  color: #fff !important;
  font-weight: 700;
  border: none;
  box-shadow: none;
}

.hoga-table tr.row-info td {
  background: var(--hoga-row-bg);
  font-size: 0.68rem;
}
.hoga-table tr.row-info .info-label { float: left; }
.hoga-table tr.row-info .info-value { float: right; }

.hoga-table tr.hoga-clickable { cursor: pointer; }
.hoga-table tr.hoga-clickable:hover td { filter: brightness(1.08); }

.hoga-qty-cell { position: relative; text-align: right; padding-right: 4px; }
.hoga-qty-bar {
  position: absolute;
  top: 1px;
  bottom: 1px;
  opacity: 0.7;
  z-index: 0;
  border-radius: 1px;
}
.hoga-qty-cell.ask .hoga-qty-bar { right: 0; background: var(--ask-bar); }
.hoga-qty-cell.bid .hoga-qty-bar { left: 0; background: var(--bid-bar); }
.hoga-qty-cell span { position: relative; z-index: 1; }

.hoga-empty {
  text-align: center !important;
  color: var(--muted) !important;
  padding: 20px !important;
  background: var(--surface) !important;
}
.hoga-table tbody {
  overflow: hidden;
}
.hoga-hts .hoga-table {
  height: 100%;
}

.auto-trade-panel {
  margin-top: 2px;
  flex: 0 0 185px;
  min-height: 160px;
  overflow: hidden;
}

.auto-trade-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 6px;
}

.auto-trade-head h2 {
  margin-bottom: 0;
}

.auto-trade-note {
  color: var(--muted);
  font-size: 0.72rem;
}

.auto-trade-table {
  height: calc(100% - 24px);
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
}

.auto-trade-table table {
  min-width: 1320px;
  width: 100%;
  border-collapse: collapse;
  font-size: 0.7rem;
}

.auto-trade-table th,
.auto-trade-table td {
  padding: 4px;
  border-bottom: 1px solid var(--border);
  border-right: 1px solid rgba(45, 58, 79, 0.6);
  text-align: right;
  white-space: nowrap;
}

.auto-trade-table th {
  position: sticky;
  top: 0;
  background: var(--surface2);
  z-index: 1;
  color: var(--muted);
}

.auto-trade-row {
  cursor: pointer;
}

.auto-trade-row:hover td {
  background: rgba(59, 130, 246, 0.08);
}

.auto-stage-cell {
  min-width: 118px;
}

.auto-check {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  margin-bottom: 3px;
  color: var(--muted);
}

.auto-inputs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 3px;
}

.auto-inputs input {
  width: 100%;
  min-width: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 3px 4px;
  border-radius: 4px;
  font-size: 0.7rem;
  text-align: right;
}

.pending-orders-panel {
  margin-top: 6px;
  flex: 0 0 140px;
  min-height: 120px;
  overflow: hidden;
}

.pending-orders-table {
  height: calc(100% - 24px);
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
}

.pending-orders-table table {
  min-width: 760px;
  width: 100%;
  border-collapse: collapse;
  font-size: 0.7rem;
}

.pending-orders-table th,
.pending-orders-table td {
  padding: 4px;
  border-bottom: 1px solid var(--border);
  border-right: 1px solid rgba(45, 58, 79, 0.6);
  text-align: right;
  white-space: nowrap;
}

.pending-orders-table th {
  position: sticky;
  top: 0;
  background: var(--surface2);
  z-index: 1;
  color: var(--muted);
}

/* 오른쪽 매매 */
.panel h2 { font-size: 0.85rem; margin-bottom: 8px; color: var(--muted); }
.mini-cards {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}
.mini-card {
  background: var(--surface2);
  border-radius: 6px;
  padding: 8px;
}
.mini-card.profit-card { cursor: pointer; }
.mini-card.profit-card:hover { outline: 1px solid var(--primary); }
.mini-card.highlight { border: 1px solid var(--primary); }
.mini-card .label { display: block; font-size: 0.72rem; color: var(--muted); }
.mini-card .value { font-size: 0.9rem; font-weight: 700; }
.mini-card .value.small { font-size: 0.75rem; }

.order-symbol {
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--primary);
}
.order-panel .form-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}
.order-panel input, .order-panel select {
  width: 58%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 7px;
  border-radius: 6px;
  font-size: 0.85rem;
}

.table-wrap { overflow: auto; max-height: 140px; }
table { width: 100%; border-collapse: collapse; font-size: 0.76rem; }
th, td { padding: 5px 4px; text-align: right; border-bottom: 1px solid var(--border); }
th:first-child, td:first-child { text-align: left; }
th { color: var(--muted); font-weight: 500; }

.btn {
  border: none;
  border-radius: 6px;
  padding: 8px 14px;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.85rem;
}
.btn.primary { background: var(--primary); color: white; }
.btn.secondary { background: var(--surface2); color: var(--text); border: 1px solid var(--border); }
.btn.full { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.up { color: var(--green); }
.down { color: var(--red); }
.hidden { display: none !important; }
.empty { color: var(--muted); text-align: center; padding: 16px; font-size: 0.82rem; }
.warning { font-size: 0.72rem; color: var(--muted); margin-top: 8px; text-align: center; }

.modal.hidden { display: none; }
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
}
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
}
.profit-modal-card {
  position: absolute;
  inset: 6vh 6vw;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow: auto;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
}
.profit-modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}
.profit-modal-head h2 { font-size: 1rem; color: var(--text); }
.profit-modal-actions { display: flex; align-items: center; gap: 8px; }
#profit-modal-title { min-width: 110px; text-align: center; font-weight: 700; }
.profit-range-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
}
.profit-range-bar label { color: var(--muted); font-size: 0.8rem; }
.profit-range-bar input {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 8px;
  border-radius: 6px;
}
.profit-range-summary {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.profit-range-summary div {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
}
.profit-range-summary span {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
  margin-bottom: 4px;
}
.profit-calendar {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
}
.cal-head {
  text-align: center;
  color: var(--muted);
  font-size: 0.75rem;
  padding: 4px;
}
.cal-day {
  min-height: 74px;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 5px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 3px;
  cursor: pointer;
  font-size: 0.72rem;
}
.cal-day.empty-day { opacity: 0; cursor: default; }
.cal-day.has-data { border-color: var(--primary); }
.cal-day.selected { outline: 2px solid var(--primary); }
.day-no { color: var(--muted); font-weight: 700; }
.profit-detail {
  flex: 0 0 auto;
  min-height: 48vh;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
}
.profit-detail h3,
.profit-detail h4 { margin: 6px 0; color: var(--muted); }
.profit-summary-line {
  display: flex;
  gap: 18px;
  margin-bottom: 8px;
}
.profit-detail table {
  margin-bottom: 10px;
}

.auto-notifications {
  position: fixed;
  left: 14px;
  bottom: 14px;
  z-index: 70;
  width: min(420px, calc(100vw - 28px));
  max-height: 38vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.auto-notification {
  pointer-events: auto;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
  background: rgba(26, 35, 50, 0.98);
  border: 1px solid var(--border);
  border-left: 4px solid var(--primary);
  border-radius: 8px;
  padding: 10px;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.38);
}

.auto-notification.success { border-left-color: var(--green); }
.auto-notification.error { border-left-color: var(--red); }

.auto-notification-title {
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 3px;
}

.auto-notification-message {
  font-size: 0.84rem;
  color: var(--text);
  line-height: 1.35;
}

.auto-notification-time {
  margin-top: 4px;
  font-size: 0.7rem;
  color: var(--muted);
}

.auto-notification-close {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  font-weight: 700;
}

.auto-notification-close:hover {
  border-color: var(--primary);
}

@media (max-width: 1200px) {
  body { overflow: auto; }
  .app { height: auto; }
  .workspace {
    grid-template-columns: 1fr;
    grid-template-rows: auto;
  }
  .col-left { max-height: 400px; }
  #chart-container { height: 400px; }
}
