:root {
  --bg: #f8f9fa;
  --surface: #ffffff;
  --border: #dee2e6;
  --text: #212529;
  --text-muted: #6c757d;
  --nav-bg: #1a1a2e;
  --nav-text: #e0e0e0;
  --up: #d32f2f;
  --down: #1565c0;
  --premium-pos: #d32f2f;
  --premium-neg: #1565c0;
  --header-bg: #f1f3f5;
  --row-hover: #f8f9fa;
  --shadow: 0 2px 8px rgba(0,0,0,0.08);
}

body.night {
  --bg: #0d0d1a;
  --surface: #1a1a2e;
  --border: #2d2d4a;
  --text: #e0e0e0;
  --text-muted: #8888aa;
  --header-bg: #16213e;
  --row-hover: #16213e;
  --shadow: 0 2px 8px rgba(0,0,0,0.4);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 13px;
  background: var(--bg);
  color: var(--text);
  transition: background 0.2s, color 0.2s;
}

/* NAV */
#topNav {
  background: var(--nav-bg);
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 50px;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}
.nav-brand {
  font-size: 18px;
  font-weight: 700;
  color: #f0b90b;
  text-decoration: none;
  letter-spacing: -0.5px;
  display: flex;
  align-items: baseline;
  gap: 8px;
}
.nav-brand-sub {
  font-size: 11px;
  font-weight: 400;
  color: #aaa;
  letter-spacing: 0;
}
.nav-links { display: flex; gap: 4px; }
.nav-links a {
  color: var(--nav-text);
  text-decoration: none;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 13px;
  transition: background 0.15s;
}
.nav-links a:hover, .nav-links a.active { background: rgba(255,255,255,0.1); color: #fff; }
.nav-right { display: flex; gap: 8px; align-items: center; }
.btn-night {
  background: transparent;
  border: 1px solid rgba(255,255,255,0.2);
  color: var(--nav-text);
  padding: 5px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
}
.btn-night:hover { background: rgba(255,255,255,0.1); }
.btn-night.active { background: rgba(240,185,11,0.25); border-color: #f0b90b; color: #f0b90b; }

/* TOP BAR */
#topBar {
  background: #16213e;
  padding: 6px 16px;
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
  align-items: center;
  font-size: 12px;
  color: #bbb;
}
.top-item { display: flex; align-items: center; gap: 5px; }
.top-item span.val { color: #fff; font-weight: 500; }
.top-item img { height: 14px; }

/* CONTAINER */
/* Page-level two-column layout: main + chat sidebar */
.page-layout {
  display: flex;
  align-items: flex-start;
  max-width: 1520px;
  margin: 0 auto;
}
.main-container { flex: 1; min-width: 0; padding: 12px 12px; }

/* Inline chat sidebar */
.chat-sidebar {
  width: 300px;
  min-width: 300px;
  height: calc(100vh - 60px);
  max-height: calc(100vh - 60px);
  position: sticky;
  top: 60px;
  border-left: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.chat-sidebar-header {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--header-bg);
  font-size: 13px;
  font-weight: 600;
  flex-shrink: 0;
}
.chat-section {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.chat-sidebar-body {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
@media (max-width: 1024px) {
  .chat-sidebar { display: none; }
}

/* PREMIUM CARD */
.premium-card {
  background: var(--surface);
  border-radius: 12px;
  padding: 20px 24px;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 24px;
  flex-wrap: wrap;
}
.premium-card .ex-block { display: flex; align-items: center; gap: 10px; }
.premium-card .ex-logo { height: 28px; }
.premium-card .ex-name { font-size: 11px; color: var(--text-muted); }
.premium-card .ex-price { font-size: 20px; font-weight: 700; color: var(--text); }
.premium-card .ex-sub { font-size: 11px; color: var(--text-muted); }
.premium-sign { font-size: 28px; color: var(--text-muted); padding: 0 8px; }
.premium-result { text-align: center; }
.premium-result .label { font-size: 11px; color: var(--text-muted); margin-bottom: 2px; }
.premium-result .pct { font-size: 28px; font-weight: 800; }
.premium-result .krw { font-size: 14px; font-weight: 600; }
.pos { color: var(--premium-pos) !important; }
.neg { color: var(--premium-neg) !important; }
.neutral { color: var(--text-muted) !important; }

/* CONTROLS */
.controls {
  background: var(--surface);
  border-radius: 12px;
  padding: 12px 16px;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}
.ctrl-btn {
  padding: 6px 14px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  cursor: pointer;
  font-size: 12px;
  transition: all 0.15s;
}
.ctrl-btn:hover { background: var(--header-bg); }
.ctrl-btn.active { background: #f0b90b; border-color: #f0b90b; color: #000; font-weight: 600; }
.search-input {
  padding: 6px 12px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 12px;
  width: 160px;
  margin-left: auto;
}
.search-input:focus { outline: none; border-color: #f0b90b; }

/* FILTER SECTION */
.filter-section {
  background: var(--surface);
  border-radius: 12px;
  padding: 12px 16px;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
}
.filter-row { display: flex; gap: 6px; flex-wrap: wrap; margin: 6px 0; align-items: center; }
.filter-label { font-size: 11px; color: var(--text-muted); min-width: 60px; }
.filter-chip {
  padding: 3px 10px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  cursor: pointer;
  font-size: 11px;
  transition: all 0.15s;
}
.filter-chip:hover { border-color: #f0b90b; }
.filter-chip.active { background: #f0b90b; border-color: #f0b90b; color: #000; font-weight: 600; }

/* TABLE */
.pair-section {
  background: var(--surface);
  border-radius: 12px;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
  overflow: hidden;
}
.pair-header {
  background: var(--header-bg);
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid var(--border);
}
.pair-header img { height: 22px; }
.pair-header .pair-name { font-weight: 600; font-size: 13px; }
.pair-header .pair-sub { font-size: 11px; color: var(--text-muted); }

table.coin-table {
  width: 100%;
  border-collapse: collapse;
}
table.coin-table thead tr {
  background: var(--header-bg);
}
table.coin-table th {
  padding: 8px 10px;
  text-align: right;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  white-space: nowrap;
  border-bottom: 1px solid var(--border);
  user-select: none;
}
table.coin-table th:first-child { text-align: left; }
table.coin-table th:hover { color: var(--text); }
table.coin-table th .sort-icon { opacity: 0.4; font-size: 9px; }
table.coin-table th.sorted .sort-icon { opacity: 1; color: #f0b90b; }

table.coin-table td {
  padding: 7px 10px;
  text-align: right;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  vertical-align: middle;
}
table.coin-table td:first-child { text-align: left; }
table.coin-table tr:last-child td { border-bottom: none; }
table.coin-table tbody tr:hover { background: var(--row-hover); }

.coin-cell { display: flex; align-items: center; gap: 6px; }
.coin-icon { width: 20px; height: 20px; border-radius: 50%; }
.coin-symbol { font-weight: 600; font-size: 13px; }
.fav-btn { background: none; border: none; cursor: pointer; font-size: 14px; opacity: 0.4; padding: 0; }
.fav-btn.on { opacity: 1; }

.td-up { color: var(--up); }
.td-down { color: var(--down); }
.td-neutral { color: var(--text-muted); }

.premium-cell { font-weight: 700; }

/* LOADING */
.loading-row td { text-align: center; color: var(--text-muted); padding: 40px; }
.loading-overlay {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 80px 0; gap: 16px; color: var(--text-muted); font-size: 14px;
}
.loading-spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--text-muted);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* COIN SEARCH FILTER */
.coin-chips { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 8px; }

/* RESPONSIVE */
@media (max-width: 768px) {
  .premium-card { gap: 12px; }
  .premium-card .ex-price { font-size: 16px; }
  .premium-result .pct { font-size: 22px; }
  .nav-links { display: none; }
  table.coin-table th, table.coin-table td { padding: 6px 6px; font-size: 11px; }
  .hide-mobile { display: none; }
  #topBar { gap: 10px; }
}

/* FLASH ANIMATION */
@keyframes flashUp { 0%,100%{background:transparent} 50%{background:rgba(211,47,47,0.15)} }
@keyframes flashDown { 0%,100%{background:transparent} 50%{background:rgba(21,101,192,0.15)} }
.flash-up { animation: flashUp 1.2s ease; }
.flash-down { animation: flashDown 1.2s ease; }

/* ALARM PANEL */
.alarm-panel {
  position: fixed; right: 16px; bottom: 16px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 16px; width: 300px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.2); z-index: 200;
  display: none;
}
.alarm-panel.open { display: block; }
.alarm-panel h4 { margin-bottom: 12px; font-size: 14px; }
.alarm-item { display: flex; justify-content: space-between; align-items: center; padding: 6px 0; border-bottom: 1px solid var(--border); font-size: 12px; }
.alarm-del { background: none; border: none; color: var(--text-muted); cursor: pointer; font-size: 16px; }

/* CHART SECTION */
.chart-section {
  background: var(--surface);
  border-radius: 12px;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
  padding: 16px;
}
.chart-section h3 { font-size: 14px; margin-bottom: 12px; color: var(--text-muted); }
.chart-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.chart-box { height: 350px; }
@media (max-width: 768px) { .chart-grid { grid-template-columns: 1fr; } }

/* TABS */
.tab-bar { display: flex; gap: 4px; margin-bottom: 12px; }
.tab-btn {
  padding: 8px 18px; border-radius: 8px; border: 1px solid var(--border);
  background: transparent; color: var(--text); cursor: pointer; font-size: 13px;
}
.tab-btn.active { background: #f0b90b; border-color: #f0b90b; color: #000; font-weight: 600; }

/* COIN CHART PANEL */
.coin-chart-section {
  background: var(--surface);
  border-radius: 12px;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
  overflow: hidden;
}
.chart-controls-bar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 16px;
  background: var(--header-bg);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.chart-coin-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 700;
  min-width: 80px;
}
.chart-ctrl-group {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-wrap: wrap;
}
.chart-ctrl-label {
  font-size: 11px;
  color: var(--text-muted);
  margin-right: 2px;
}
.chart-radio {
  padding: 3px 10px;
  border-radius: 12px;
  border: 1px solid var(--border);
  cursor: pointer;
  font-size: 11px;
  color: var(--text);
  transition: all 0.15s;
  user-select: none;
  white-space: nowrap;
}
.chart-radio:hover { border-color: #f0b90b; }
.chart-radio.active { background: #f0b90b; border-color: #f0b90b; color: #000; font-weight: 600; }

table.coin-table tbody tr { cursor: pointer; }
table.coin-table tbody tr.row-active > td:first-child { border-left: 3px solid #f0b90b; }

/* Inline chart row */
.chart-inline-row { cursor: default; }
.chart-inline-row > td {
  padding: 0;
  border-bottom: 2px solid #f0b90b;
}
.chart-inline-row:hover { background: transparent !important; }

/* Chart + trades side-by-side layout */
.chart-layout {
  display: flex;
  align-items: stretch;
  height: 540px;
}

.coin-chart-pane {
  flex: 1;
  min-width: 0;
  height: 100%;
}

.trades-panel {
  width: 260px;
  min-width: 260px;
  height: 100%;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.trades-header {
  padding: 8px 12px;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  background: var(--header-bg);
  flex-shrink: 0;
}
.trades-list {
  flex: 1;
  overflow: hidden;
}
.trades-empty {
  padding: 12px 10px;
  font-size: 11px;
  color: var(--text-muted);
}
.trade-item {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 6px;
  padding: 5px 10px;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  align-items: center;
}
.trade-buy  .trade-price { color: #26a69a; font-weight: 600; }
.trade-sell .trade-price { color: #ef5350; font-weight: 600; }
.trade-qty  { color: var(--text-muted); text-align: right; font-size: 11px; }
.trade-time { color: var(--text-muted); font-size: 10px; opacity: 0.6; }

@keyframes tradeSlideIn {
  from { opacity: 0; transform: translateY(-4px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* Mobile: stack trades above chart */
@media (max-width: 768px) {
  .chart-layout {
    flex-direction: column;
    height: auto;
  }
  .trades-panel {
    width: 100%;
    min-width: unset;
    height: auto;
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: 150px;
  }
  .coin-chart-pane {
    height: 360px;
  }
  .trade-time { display: none; }
  .trade-item { font-size: 11px; padding: 4px 8px; }
}

/* Top-20 market cap badge */
.top20-badge {
  font-size: 10px;
  font-weight: 700;
  color: #b8860b;
  background: rgba(240,185,11,0.15);
  border: 1px solid rgba(240,185,11,0.4);
  border-radius: 4px;
  padding: 1px 4px;
  margin-left: 4px;
  white-space: nowrap;
}

.tg-join-link {
  font-size: 11px;
  font-weight: 400;
  color: #2CA5E0;
  text-decoration: none;
}
.tg-join-link:hover { text-decoration: underline; }

/* Nickname badge in chat header */
.chat-nick-wrap { display: flex; align-items: center; }
.chat-nick-badge {
  font-size: 12px;
  font-weight: 600;
  color: #2CA5E0;
  cursor: pointer;
  padding: 3px 8px;
  border-radius: 4px;
  white-space: nowrap;
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-nick-badge:hover { background: rgba(44,165,224,0.12); }
.chat-nick-edit {
  width: 160px;
  font-size: 12px;
  padding: 3px 8px;
  border: 1px solid #2CA5E0;
  border-radius: 4px;
  background: var(--bg);
  color: var(--text);
  outline: none;
}

/* Messages area */
.chat-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  scroll-behavior: smooth;
}
.chat-empty { color: var(--text-muted); font-size: 12px; text-align: center; margin-top: 20px; }
.chat-msg {
  background: var(--header-bg);
  border-radius: 10px;
  padding: 7px 10px;
  max-width: 85%;
  align-self: flex-start;
}
.msg-tg  { border-left: 3px solid #2CA5E0; }
.msg-web { border-left: 3px solid #f0b90b; }
.msg-own {
  align-self: flex-end;
  background: #1a73e8;
  border-radius: 10px;
}
.msg-own .chat-text { color: #fff; }
.msg-own .chat-ts   { color: rgba(255,255,255,0.7); }
.chat-msg-header {
  display: flex;
  align-items: center;
  gap: 5px;
  margin-bottom: 3px;
}
.chat-online-count {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  font-weight: 500;
  color: #4caf50;
  margin-left: 6px;
}
.chat-online-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #4caf50;
  animation: pulse 2s infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}
.chat-emoji { font-size: 14px; line-height: 1; }
.chat-from { font-size: 11px; font-weight: 700; color: var(--text); }
.chat-tg-badge {
  font-size: 9px;
  font-weight: 700;
  background: #2CA5E0;
  color: white;
  border-radius: 3px;
  padding: 1px 4px;
}
.chat-ts { font-size: 10px; color: var(--text-muted); margin-left: auto; }
.chat-text { font-size: 12px; line-height: 1.5; word-break: break-word; }
.chat-error { font-size: 11px; color: #ef5350; padding: 4px 12px; }

/* Input row */
.chat-input-row {
  display: flex;
  gap: 6px;
  padding: 8px 10px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.chat-input {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 12px;
  outline: none;
}
.chat-input:focus { border-color: #2CA5E0; }
.chat-send-btn {
  padding: 8px 14px;
  background: #2CA5E0;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.chat-send-btn:disabled { background: #aaa; cursor: default; }
.chat-send-btn:not(:disabled):hover { background: #1a8fc7; }

/* NEWS PANEL */
.news-panel {
  flex-shrink: 0;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 128px;
  overflow: hidden;
}
.news-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}
.news-empty { font-size: 11px; color: var(--text-muted); padding: 10px; text-align: center; }
.news-item {
  display: flex;
  align-items: baseline;
  gap: 6px;
  padding: 4px 10px;
  border-bottom: 1px solid var(--border);
  text-decoration: none;
  color: var(--text);
  transition: background 0.15s;
  min-height: 0;
}
.news-item:last-child { border-bottom: none; }
.news-item:hover { background: var(--hover); }
.news-title {
  flex: 1;
  font-size: 11px;
  line-height: 1.3;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  color: var(--text);
}
.news-age {
  flex-shrink: 0;
  font-size: 10px;
  color: var(--text-muted);
}

/* LIQUIDATION PANEL */
.liq-panel {
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 260px;
}
.liq-totals {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
}
.liq-total strong { font-weight: 700; }
.liq-long-total strong  { color: #26a69a; }
.liq-short-total strong { color: #ef5350; }
.liq-divider { color: var(--border); }
.liq-24h-badge {
  font-size: 9px;
  background: var(--border);
  color: var(--text-muted);
  padding: 1px 4px;
  border-radius: 3px;
  font-weight: 700;
}
.liq-feed {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}
.liq-empty { font-size: 11px; color: var(--text-muted); padding: 12px; display: flex; justify-content: center; }
.liq-item {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  font-size: 11px;
  border-bottom: 1px solid var(--border);
  border-left: 3px solid transparent;
  animation: tradeSlideIn 0.12s ease both;
}
.liq-item.liq-long  { border-left-color: #26a69a; }
.liq-item.liq-short { border-left-color: #ef5350; }
.liq-item.liq-big   { font-weight: 700; background: rgba(239,83,80,0.06); }
.liq-item.liq-big.liq-long { background: rgba(38,166,154,0.06); }
.liq-sym { font-weight: 700; min-width: 38px; font-size: 11px; }
.liq-side-badge {
  font-size: 10px;
  font-weight: 700;
  min-width: 20px;
}
.liq-item.liq-long  .liq-side-badge { color: #26a69a; }
.liq-item.liq-short .liq-side-badge { color: #ef5350; }
.liq-usd { flex: 1; text-align: right; font-weight: 600; }
.liq-price { color: var(--text-muted); font-size: 10px; min-width: 52px; text-align: right; }


/* STATUS DOT */
.status-dot { width: 6px; height: 6px; border-radius: 50%; display: inline-block; margin-right: 4px; }
.status-dot.connected { background: #4caf50; }
.status-dot.disconnected { background: #f44336; }
.status-dot.connecting { background: #ff9800; }
