* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "Segoe UI", "Malgun Gothic", system-ui, sans-serif;
  background: #0e1116;
  color: #d6dde6;
  display: flex;
  flex-direction: column;
}

/* ---- header ---- */
header {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 10px 18px;
  background: #161b22;
  border-bottom: 1px solid #21262d;
  flex: 0 0 auto;
}
h1 { font-size: 18px; margin: 0; color: #22c55e; font-weight: 700; }
.search { position: relative; }
#q {
  width: 360px;
  padding: 8px 10px;
  border: 1px solid #30363d;
  border-radius: 6px;
  background: #0d1117;
  color: #d6dde6;
  font-size: 14px;
}
.results {
  position: absolute;
  top: 38px;
  left: 0;
  width: 360px;
  max-height: 320px;
  overflow-y: auto;
  margin: 0;
  padding: 0;
  list-style: none;
  background: #161b22;
  border: 1px solid #30363d;
  border-radius: 6px;
  z-index: 30;
}
.results li {
  padding: 8px 10px;
  cursor: pointer;
  font-size: 13px;
  display: flex;
  justify-content: space-between;
}
.results li:hover, .results li.active { background: #1f6feb33; }
.results .mkt { color: #8b949e; font-size: 11px; }
.hidden { display: none; }

/* ---- app shell ---- */
.app { flex: 1 1 auto; display: flex; min-height: 0; }
.sidebar {
  flex: 0 0 220px;
  background: #11161d;
  border-right: 1px solid #21262d;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}
.side-h {
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 600;
  color: #8b949e;
  border-bottom: 1px solid #21262d;
}
.wl { list-style: none; margin: 0; padding: 0; }
.wl li {
  padding: 8px 12px;
  font-size: 14px;
  cursor: grab;
  border-bottom: 1px solid #1b212a;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.wl li:hover { background: #1f6feb22; }
.wl li:active { cursor: grabbing; }
.wl li.dragging { opacity: 0.4; }
.wl li.drag-over { background: #1f6feb44; }
.wl-main {
  flex: 1;                /* fill the row so the whole left area is clickable */
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
  cursor: pointer;
  font-weight: 600;       /* 종목명 bold */
}
.wl-sub { font-size: 12px; font-weight: 400; color: #8b949e; }
.wl .rm { color: #6e7681; cursor: pointer; padding-left: 6px; }
.wl .rm:hover { color: #e5484d; }
.side-empty { padding: 12px 14px; font-size: 12px; color: #6e7681; }
.results .star {
  margin-left: 8px;
  color: #f0b429;
  cursor: pointer;
  font-size: 11px;
}
.results .star:hover { color: #ffd43b; }

.main { flex: 1 1 auto; display: flex; flex-direction: column; min-width: 0; }
.tabs {
  display: flex;
  gap: 4px;
  padding: 8px 12px 0;
  background: #161b22;
  border-bottom: 1px solid #21262d;
  flex: 0 0 auto;
}
.tab {
  background: transparent;
  border: 1px solid transparent;
  border-bottom: none;
  color: #8b949e;
  padding: 8px 16px;
  font-size: 14px;
  cursor: pointer;
  border-radius: 6px 6px 0 0;
}
.tab:hover { color: #d6dde6; }
.tab.active {
  color: #58a6ff;
  background: #0e1116;
  border-color: #21262d;
}

.panel { flex: 1 1 auto; display: none; flex-direction: column; min-height: 0; }
.panel.active { display: flex; }
.placeholder {
  margin: 24px auto;
  color: #6e7681;
  font-size: 14px;
}

/* ---- forms + result tables ---- */
.form {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 14px;
  align-items: end;
  padding: 14px 18px;
  border-bottom: 1px solid #21262d;
  flex: 0 0 auto;
}
.form label {
  display: flex;
  flex-direction: column;
  font-size: 11px;
  color: #8b949e;
  gap: 3px;
}
.form input, .form select {
  background: #0d1117;
  border: 1px solid #30363d;
  color: #d6dde6;
  border-radius: 5px;
  padding: 6px 8px;
  font-size: 13px;
  color-scheme: dark;            /* native calendar/picker follows dark theme */
}
/* uniform control height — date/number/select/button render at different
   native heights, so pin them all to one box (border-box includes padding). */
.form input:not([type="checkbox"]), .form select, .form button {
  box-sizing: border-box;
  height: 32px;
}
.form input[type=number] { width: 96px; }
.form input[type=date] { width: 150px; }
/* 패턴 선택 — 아래로 펼쳐지는 카테고리별 체크박스 드롭다운 */
.pattern-field { position: relative; }
/* `.form .pattern-btn` (specificity) beats `.form button` so the pattern
   button matches the `패턴 결합` <select>, not the blue 검색 button. */
.form .pattern-btn {
  background: #0d1117;
  border: 1px solid #30363d;
  color: #d6dde6;
  border-radius: 5px;
  padding: 6px 8px;
  font-size: 13px;
  cursor: pointer;
  text-align: left;
  min-width: 140px;
}
.form .pattern-btn:hover { background: #161b22; }
/* 패턴 버튼을 종가·패턴결합 등 <select> 와 같은 모양으로 — 동일한 우측
   드롭다운 화살표. select 들도 네이티브 화살표 대신 같은 화살표를 쓴다. */
.form select,
.form .pattern-btn {
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'><path d='M1 1.5 6 6.5 11 1.5' fill='none' stroke='%238b949e' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/></svg>");
  background-repeat: no-repeat;
  background-position: right 9px center;
  padding-right: 26px;
}
.pattern-panel {
  position: absolute;
  top: 100%;
  right: 0;
  z-index: 30;
  margin-top: 4px;
  width: 230px;                  /* original pattern-control width */
  max-height: 300px;
  overflow-y: auto;
  background: #161b22;
  border: 1px solid #30363d;
  border-radius: 6px;
  padding: 8px 10px;
  box-shadow: 0 8px 24px #0009;
}
.pattern-panel.hidden { display: none; }
.pattern-panel .pat-cat {
  font-size: 11px;
  font-weight: 600;
  color: #8b949e;
  margin: 9px 0 4px;
}
.pattern-panel .pat-cat:first-child { margin-top: 0; }
.pattern-panel .pat-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 3px 10px;
}
.pattern-panel .pat-opt {
  flex-direction: row;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  color: #d6dde6;
  cursor: pointer;
}
.pattern-panel input[type="checkbox"] {
  width: auto;
  padding: 0;
  margin: 0;
  border: none;
  background: none;
  accent-color: #1f6feb;
  cursor: pointer;
}
.form button {
  background: #1f6feb;
  color: #fff;
  border: none;
  border-radius: 5px;
  padding: 8px 18px;
  font-size: 13px;
  cursor: pointer;
}
.form button:hover { background: #388bfd; }
.form label.chk {
  flex-direction: row;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: #d6dde6;
}
.form label.chk input { width: auto; }
/* 특징주 — 체크박스 두 개를 위아래로 묶어 한 항목으로 */
.form .chk-group { display: flex; flex-direction: column; gap: 4px; }
.form-info { font-size: 12px; color: #8b949e; align-self: center; }
.result-wrap { flex: 1 1 auto; overflow: auto; }
.rtable {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.rtable th, .rtable td {
  padding: 7px 10px;
  text-align: left;
  border-bottom: 1px solid #1b212a;
  white-space: nowrap;
}
.rtable thead th {
  position: sticky;
  top: 0;
  background: #161b22;
  color: #8b949e;
  font-weight: 600;
}
.rtable tbody tr { cursor: pointer; }
.rtable tbody tr:hover { background: #1f6feb22; }
.rtable .star { color: #f0b429; cursor: pointer; }
.rtable .star:hover { color: #ffd43b; }
.rtable .up   { color: #e5484d; font-weight: 600; white-space: nowrap; }
.rtable .down { color: #3b82f6; font-weight: 600; white-space: nowrap; }
.rtable .flat { color: #6b7280; white-space: nowrap; }

/* 특징주 — 전체/상승/하락 서브탭 */
.sub-tabs { display: flex; gap: 4px; padding: 8px 18px 0; border-bottom: 1px solid #2a2f37; }
.sub-tab  { background: transparent; color: #9aa3ad; border: 1px solid transparent;
            border-bottom: none; padding: 6px 14px; font-size: 13px; cursor: pointer;
            border-radius: 6px 6px 0 0; }
.sub-tab:hover { color: #e6e8eb; }
.sub-tab.active { color: #e6e8eb; background: #1a1d22; border-color: #2a2f37; }
.sub-tab.up.active   { color: #e5484d; }
.sub-tab.down.active { color: #3b82f6; }
.sub-panel { padding: 6px 18px 18px; }

/* ---- chart panel ---- */
.title-row { display: flex; align-items: center; gap: 12px; padding: 8px 18px 0; }
.title { flex: 1; min-width: 0; font-size: 15px; font-weight: 600; }
.title .sub {
  color: #8b949e; font-weight: 400; font-size: 12px; margin-left: 8px;
}
.legend {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  padding: 6px 18px;
  font-size: 12px;
  color: #8b949e;
}
.legend span::before {
  content: "";
  display: inline-block;
  width: 14px;
  height: 3px;
  margin-right: 5px;
  vertical-align: middle;
  background: var(--c);
}
.chart-tools { padding: 2px 18px 4px; flex: 0 0 auto; min-height: 22px; }
#volTopBtn {
  position: absolute;
  top: 8px;
  left: 12px;
  z-index: 16;
  background: #21262de6;
  color: #d6dde6;
  border: 1px solid #30363d;
  border-radius: 5px;
  padding: 5px 12px;
  font-size: 12px;
  cursor: pointer;
}
#volTopBtn:hover:not(:disabled) { background: #2d333b; }
#volTopBtn:disabled { opacity: 0.5; cursor: default; }
/* 관심종목 추가/제거 토글 — 헤더(종목명 줄) 우측 */
#wlToggleBtn {
  flex: 0 0 auto;
  background: #21262d;
  border: 1px solid #30363d;
  border-radius: 5px;
  padding: 4px 11px;
  font-size: 13px;
  line-height: 1.2;
  color: #f0b429;
  cursor: pointer;
}
#wlToggleBtn:hover:not(:disabled) { background: #2d333b; color: #ffd43b; }
#wlToggleBtn:disabled { opacity: 0.4; cursor: default; }
#wlToggleBtn.added { border-color: #6e5a1b; background: #2a2410; }
.chart {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  margin: 0 12px;
  cursor: grab;
}
.vol-top {
  position: absolute;
  top: 36px;          /* just below the top-left OHLCV/MA overlay */
  left: 12px;
  right: auto;
  z-index: 16;
  width: 340px;
  max-height: 70%;
  display: flex;
  flex-direction: column;
  background: #11161dF2;
  border: 1px solid #30363d;
  border-radius: 6px;
  font-size: 12px;
}
.vol-top.hidden { display: none; }
.vol-top .vt-h {
  padding: 7px 10px;
  font-weight: 600;
  color: #c9d1d9;
  border-bottom: 1px solid #30363d;
  display: flex;
  justify-content: space-between;
}
.vol-top .vt-x { cursor: pointer; color: #8b949e; }
.vol-top .vt-x:hover { color: #e5484d; }
.vol-top .vt-scroll { overflow-y: auto; }
.vol-top table { width: 100%; border-collapse: collapse; }
.vol-top th, .vol-top td {
  padding: 5px 10px;
  text-align: left;
  border-bottom: 1px solid #1b212a;
  white-space: nowrap;
}
.vol-top thead th {
  position: sticky;
  top: 0;
  background: #161b22;
  color: #8b949e;
}
.vol-top td.num { text-align: right; }
.vol-top tbody tr { cursor: pointer; }
.vol-top tbody tr:hover { background: #1f6feb33; }
.vol-top .up { color: #e5484d; }
.vol-top .down { color: #3b82f6; }
.chart.dragging { cursor: grabbing; }
.ohlcv-bar {
  display: inline-block;
  min-height: 16px;
  font-size: 12px;
  color: #c9d1d9;
  white-space: nowrap;
}
.ohlcv-bar b { color: #e6edf3; }
.ohlcv-bar .up { color: #e5484d; }
.ohlcv-bar .down { color: #3b82f6; }
.live-dot {
  display: inline-block;
  margin-left: 12px;
  font-size: 12px;
  color: #6e7681;
  white-space: nowrap;
}
.live-dot.live { color: #2ea043; }
.live-dot.off { color: #6e7681; }
.hint {
  padding: 6px 18px 10px;
  font-size: 12px;
  color: #6e7681;
  flex: 0 0 auto;
}
.hint-mobile { display: none; }    /* 데스크톱: 모바일 힌트 숨김 */

/* ---- 햄버거 + 모바일 드로어 ---- */
.nav-btn {
  display: none;                    /* 데스크톱에선 숨김 */
  background: transparent;
  border: 1px solid #30363d;
  color: #d6dde6;
  font-size: 18px;
  line-height: 1;
  width: 38px;
  height: 38px;
  border-radius: 6px;
  cursor: pointer;
}
.nav-btn:active { background: #1f6feb22; }
.nav-backdrop {
  position: fixed;
  inset: 0;
  background: #0008;
  z-index: 40;
}
.nav-backdrop.hidden { display: none; }

/* ============================================================
   모바일 (≤768px) — 사이드바를 슬라이드 드로어로, 폼·테이블 축소
   ============================================================ */
@media (max-width: 768px) {
  /* 헤더 — 햄버거 표시, 검색창은 다음 줄로 떨어뜨려 전폭 */
  header { flex-wrap: wrap; gap: 10px; padding: 8px 12px; }
  .nav-btn { display: inline-flex; align-items: center; justify-content: center; }
  h1 { font-size: 16px; flex: 1; }
  .search { flex: 1 0 100%; }
  #q { width: 100%; }
  .results { width: 100%; }

  /* 사이드바 — 좌측에서 슬라이드 인 */
  .sidebar {
    position: fixed;
    top: 0; bottom: 0; left: 0;
    z-index: 50;
    width: 260px; max-width: 80vw;
    transform: translateX(-100%);
    transition: transform 0.2s ease-out;
    border-right: 1px solid #21262d;
    box-shadow: 4px 0 16px #0006;
  }
  .sidebar.open { transform: translateX(0); }
  .app { display: block; min-height: 0; }
  .main { display: flex; min-height: calc(100vh - 56px); }

  /* 탭 */
  .tabs { padding: 6px 8px 0; gap: 2px; }
  .tab { padding: 7px 12px; font-size: 13px; }

  /* 폼 */
  .form { padding: 10px 12px; gap: 8px 10px; }
  .form label { font-size: 11px; }
  .form input, .form select, .form .pattern-btn { font-size: 12px; padding: 5px 7px; }
  .form input[type=number] { width: 80px; }
  .form input[type=date] { width: 130px; }
  .form .pattern-btn { min-width: 110px; }
  .form button { padding: 6px 14px; }

  /* 결과 테이블 */
  .rtable { font-size: 12px; }
  .rtable th, .rtable td { padding: 6px 8px; }

  /* 서브탭 (전체/상승/하락) */
  .sub-tabs { padding: 6px 10px 0; }
  .sub-tab { padding: 5px 10px; font-size: 12px; }
  .sub-panel { padding: 4px 10px 14px; }

  /* 차트 영역 */
  .title-row, .legend, .chart-tools { padding-left: 12px; padding-right: 12px; }
  .title { font-size: 14px; }
  .legend { gap: 8px; font-size: 11px; }
  .chart { margin: 0 6px; }
  .hint { padding: 6px 12px 8px; font-size: 11px; }
  .hint-desktop { display: none; }
  .hint-mobile  { display: block; }

  /* 차트 위 거래량 상위일 패널은 모바일 폭에 맞춰 */
  .vol-top { width: calc(100vw - 36px); max-width: 320px; }
}
