:root {
  --bg-root: #0b1120;
  --bg-panel: #111827;
  --bg-card: #1e293b;
  --bg-hover: #26354d;
  --bg-selected: #1e3a5f;
  --border: #1e293b;
  --border-light: #334155;
  --text-main: #f8fafc;
  --text-sub: #94a3b8;
  --text-muted: #64748b;
  --accent: #38bdf8;
  --accent-dark: #0284c7;
  --up-red: #ef4444;
  --down-green: #10b981;
  --warn-gold: #fbbf24;
}

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

body {
  background-color: var(--bg-root);
  color: var(--text-main);
  font-family: "Segoe UI", "Microsoft JhengHei UI", sans-serif;
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 12px;
  overflow: hidden;
}

/* Market Session Banner */
.market-session-banner {
  border-radius: 8px;
  padding: 4px 12px;
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  border: 1px solid #f59e0b;
  background: #292112;
  flex-shrink: 0;
  min-height: 28px;
}

.session-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  overflow: hidden;
}

.session-badge {
  background: #0b1120;
  border: 1px solid #f59e0b;
  color: #fbbf24;
  padding: 2px 7px;
  border-radius: 5px;
  font-size: 11.5px;
  font-weight: 700;
  white-space: nowrap;
  flex-shrink: 0;
}

.session-detail {
  font-size: 11.5px;
  color: #e2e8f0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-schedule {
  font-size: 11px;
  color: #94a3b8;
  white-space: nowrap;
  flex-shrink: 0;
}

/* Top Bar */
.top-bar {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 7px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
  flex-shrink: 0;
  flex-wrap: nowrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.brand-icon {
  background: var(--accent-dark);
  width: 32px;
  height: 32px;
  border-radius: 7px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
}

.brand-title {
  font-size: 14.5px;
  font-weight: 700;
  white-space: nowrap;
}

.filter-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: nowrap;
}

.control-group {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: var(--text-sub);
  white-space: nowrap;
}

.search-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.search-icon {
  position: absolute;
  left: 9px;
  font-size: 11.5px;
  color: var(--text-sub);
  pointer-events: none;
}

.search-wrap input[type="text"] {
  padding-left: 26px;
  width: 230px;
}

select, input[type="text"] {
  background: var(--bg-card);
  color: var(--text-main);
  border: 1px solid var(--border-light);
  border-radius: 6px;
  padding: 5px 8px;
  font-size: 12px;
  outline: none;
}

select:focus, input[type="text"]:focus {
  border-color: var(--accent);
}

.btn {
  background: var(--bg-card);
  color: var(--text-main);
  border: 1px solid var(--border-light);
  border-radius: 6px;
  padding: 6px 11px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.15s;
}

.btn:hover {
  background: #334155;
  border-color: var(--accent);
}

.btn-primary {
  background: var(--accent-dark);
  border-color: var(--accent);
  font-weight: 600;
}

.btn-gold {
  color: var(--warn-gold);
  border-color: #f59e0b;
  font-weight: 600;
}

.btn.active-pill {
  background: var(--selected, #1e3a5f);
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 600;
}

.btn-icon {
  width: 32px;
  height: 32px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  flex-shrink: 0;
}

.btn-download {
  border: 1px solid #38bdf8;
  color: #38bdf8;
  background: #1e293b;
}

.btn-download:hover {
  background: #26354d;
  border-color: #7dd3fc;
}

@keyframes sync-rotate {
  100% {
    transform: rotate(360deg);
  }
}

.sync-spinning {
  animation: sync-rotate 0.8s linear infinite;
}

.header-action-group {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.api-status-box {
  display: flex;
  align-items: center;
  flex-shrink: 0;
  cursor: help;
}

.status-time {
  font-size: 11px;
  color: var(--accent);
  white-space: nowrap;
  font-family: monospace, sans-serif;
  background: #0f172a;
  border: 1px solid var(--border-light);
  padding: 4px 7px;
  border-radius: 6px;
}

/* Main 3-Column Grid */
.workspace {
  display: grid;
  grid-template-columns: 350px 1fr 360px;
  gap: 10px;
  flex: 1;
  min-height: 0;
}

.col {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
}

.panel {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
}

.panel-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

.section-label {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--text-sub);
  margin-bottom: 5px;
}

.pill-wrap {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.pill-btn {
  padding: 3px 8px;
  font-size: 11px;
}

/* Sector Summary */
.sector-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}

.sector-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  font-size: 11.5px;
  color: var(--text-sub);
  margin-bottom: 5px;
}

/* Stock List Items */
.stock-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  overflow-y: auto;
  padding-right: 2px;
}

.stock-card {
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: 8px;
  padding: 8px 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  transition: all 0.12s;
}

.stock-card:hover {
  background: var(--bg-hover);
}

.stock-card.selected {
  background: var(--bg-selected);
  border-color: var(--accent);
  border-width: 1.5px;
}

.badge {
  display: inline-block;
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 10px;
  font-weight: 700;
  color: #fff;
}

.star-btn {
  background: transparent;
  border: none;
  color: var(--warn-gold);
  font-size: 16px;
  cursor: pointer;
  margin-right: 6px;
}

/* Center Column */
.stock-header-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

.quote-strip {
  display: grid;
  grid-template-columns: 1.15fr 1fr;
  gap: 8px;
}

.quote-grid-4 {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin-top: 5px;
}

.metric-6-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
}

.metric-card {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
}

.metric-label {
  font-size: 10.5px;
  color: var(--text-sub);
}

.metric-val {
  font-size: 15px;
  font-weight: 700;
  margin-top: 3px;
}

/* Chart Container */
.chart-panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.chart-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
  flex-wrap: wrap;
  gap: 6px;
}

.canvas-wrap {
  flex: 1;
  min-height: 260px;
  position: relative;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: #0f172a;
}

canvas {
  width: 100%;
  height: 100%;
  display: block;
  cursor: crosshair;
}

/* Right Column Factors */
.factor-card {
  background: #0f172a;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  margin-bottom: 6px;
}

.factor-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 3px;
}

/* ==========================================================================
   PWA & Responsive Support for Mobile & Tablet
   ========================================================================== */
@media (max-width: 1100px) {
  body {
    height: auto;
    min-height: 100vh;
    overflow-y: auto;
    padding: 8px;
  }

  .top-bar {
    flex-wrap: wrap;
    gap: 8px;
    padding: 9px 12px;
  }

  .brand {
    order: 1;
  }

  .header-action-group {
    order: 2;
  }

  .filter-controls {
    order: 3;
    flex-wrap: wrap;
    width: 100%;
  }

  .workspace {
    display: flex;
    flex-direction: column;
    height: auto;
    min-height: auto;
  }

  .col {
    min-height: auto;
  }

  .stock-list {
    max-height: 380px;
  }

  .metric-6-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
  }

  .canvas-wrap {
    min-height: 340px;
  }

  .factor-list-wrap {
    max-height: 300px;
  }
}

/* 針對行動裝置 (手機 / 窄螢幕平板) 的超高密度防擠壓排版 */
@media (max-width: 768px) {
  /* 1. 頂部交易時段橫幅：隱藏落落長的靜態交易時段，杜絕重疊擠壓 */
  .market-session-banner {
    padding: 4px 8px;
    margin-bottom: 6px;
    min-height: 26px;
  }

  .session-schedule {
    display: none !important;
  }

  .session-left {
    width: 100%;
    justify-content: flex-start;
    gap: 6px;
  }

  .session-badge {
    font-size: 11px;
    padding: 1px 6px;
  }

  .session-detail {
    font-size: 10.5px;
  }

  /* 2. 標題列頂部 Brand 與 按鈕並排第一行 */
  .top-bar {
    padding: 8px 10px;
    gap: 7px;
  }

  .brand {
    order: 1;
    flex: 1;
    min-width: 0;
  }

  .brand-icon {
    width: 26px;
    height: 26px;
    font-size: 13px;
    border-radius: 6px;
  }

  .brand-title {
    font-size: 13.5px;
  }

  .header-action-group {
    order: 2;
    display: flex;
    align-items: center;
    gap: 6px;
  }

  .header-action-group .btn-icon {
    width: 28px;
    height: 28px;
  }

  .status-time {
    font-size: 10px;
    padding: 3px 6px;
  }

  /* 3. 篩選與搜尋：雙欄緊湊排列，省下大半垂直空間 */
  .filter-controls {
    order: 3;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    width: 100%;
  }

  .control-group {
    width: 100%;
  }

  .control-label {
    display: none !important;
  }

  .control-group select {
    width: 100%;
    height: 32px;
    font-size: 11.5px;
  }

  .control-group-rank {
    grid-column: 1 / 2;
  }

  .control-group-signal {
    grid-column: 2 / 3;
  }

  .control-group-search {
    grid-column: 1 / -1;
    display: flex;
  }

  .search-wrap input[type="text"] {
    width: 100% !important;
    height: 32px;
    font-size: 11.5px;
  }

  /* 4. 快速切換標籤與按鈕排版優化 */
  .section-label {
    font-size: 11px;
    margin-bottom: 4px;
  }

  .pill-wrap {
    gap: 4px;
  }

  .pill-wrap .btn {
    padding: 4px 8px;
    font-size: 11px;
  }
}

@media (max-width: 640px) {
  .metric-6-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .quote-strip {
    flex-direction: column;
  }

  .chart-toolbar {
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
  }
}

