/* TradingView-style dark theme for the halving-cycle chart. */

:root {
  --bg: #131722;
  --bg-panel: rgba(19, 23, 34, 0.86);
  --bg-header: rgba(90, 96, 110, 0.42);
  --border: rgba(255, 255, 255, 0.14);
  --border-soft: rgba(255, 255, 255, 0.07);
  --text: #d1d4dc;
  --text-dim: #9aa0ab;
  --text-bright: #f0f3fa;
  --accent: #2962ff;
  --up: #26a69a;
  --down: #ef5350;
  --buy: #2e9b4b;
  --sell: #f43f5e;
  --halving: #ff7a00;
  --tech-buy: #00c2e8;
  --tech-sell: #d946ef;
  --font: -apple-system, "Trebuchet MS", Roboto, Ubuntu, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  overflow: hidden;
}

/* dvh keeps the time axis visible above mobile browser chrome (the URL bar
   overlaps the bottom of 100vh on phones); 100vh is the older-browser fallback. */
#app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }

/* ---------- top bar ---------- */

#topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 6px 12px;
  border-bottom: 1px solid #2a2e39;
  background: var(--bg);
  flex: 0 0 auto;
  flex-wrap: wrap;
}

.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--text-bright); }
.brand-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: linear-gradient(135deg, #f7931a, #ffb347);
  box-shadow: 0 0 8px #f7931a88;
}
.brand-name { font-size: 13px; letter-spacing: .2px; white-space: nowrap; }

.top-price { display: flex; align-items: baseline; gap: 8px; min-width: 150px; }
#top-price-value { font-size: 17px; font-weight: 700; color: var(--text-bright); font-variant-numeric: tabular-nums; }
#top-price-change { font-size: 12px; font-variant-numeric: tabular-nums; }
.chg-up { color: var(--up); }
.chg-down { color: var(--down); }

.controls { display: flex; align-items: center; gap: 8px; margin-left: auto; flex-wrap: wrap; }

.btn-group { display: flex; background: #1e222d; border-radius: 6px; padding: 2px; gap: 2px; position: relative; }
.btn {
  border: 0; background: transparent; color: var(--text-dim);
  font: 600 12px var(--font);
  padding: 4px 10px; border-radius: 4px; cursor: pointer;
  transition: background .12s, color .12s;
  white-space: nowrap;
}
.btn:hover { background: #2a2e39; color: var(--text); }
.btn.active { background: var(--accent); color: #fff; }

.dropdown-menu {
  display: none;
  position: absolute; top: calc(100% + 4px); right: 0; z-index: 40;
  background: #1e222d; border: 1px solid #2a2e39; border-radius: 6px;
  padding: 8px 10px; min-width: 140px;
  box-shadow: 0 6px 20px rgba(0,0,0,.5);
}
.dropdown-menu.open { display: block; }
.dropdown-menu label {
  display: flex; align-items: center; gap: 7px;
  font-size: 12px; padding: 4px 0; cursor: pointer; color: var(--text);
}
.dropdown-menu input { accent-color: var(--accent); }

.conn { display: flex; align-items: center; gap: 6px; font-size: 11px; color: var(--text-dim); }
.conn-dot { width: 8px; height: 8px; border-radius: 50%; background: #f6c343; }
.conn-dot.live { background: #26a69a; box-shadow: 0 0 6px #26a69a99; animation: pulse 2s infinite; }
.conn-dot.poll { background: #f6c343; }
.conn-dot.off { background: #ef5350; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .45; } }

/* ---------- chart ---------- */

#chart-wrap { position: relative; flex: 1 1 auto; min-height: 0; }
#chart { position: absolute; inset: 0; }

.overlay { position: absolute; z-index: 20; }

/* legend */
#legend { left: 12px; top: 8px; pointer-events: none; user-select: none; }
.legend-row1 { font-size: 14px; font-weight: 600; color: var(--text-bright); }
.legend-dim { color: var(--text-dim); font-weight: 400; }
.legend-row2 { font-size: 12px; margin-top: 3px; display: flex; gap: 10px; font-variant-numeric: tabular-nums; }
.legend-row2 span { color: var(--text-dim); }
.legend-row2 b { font-weight: 600; }
.legend-row3 { font-size: 11px; margin-top: 3px; }
.val-up { color: var(--up); }
.val-down { color: var(--down); }

/* ---------- event labels ---------- */

#labels-layer { position: absolute; inset: 0; overflow: hidden; z-index: 15; pointer-events: none; }

.tv-label {
  position: absolute;
  transform: translate(-50%, 0);
  padding: 3px 7px;
  border-radius: 4px;
  font-size: 10.5px;
  font-weight: 700;
  line-height: 1.25;
  text-align: center;
  color: #fff;
  white-space: pre;
  pointer-events: auto;
  cursor: default;
  box-shadow: 0 2px 6px rgba(0,0,0,.45);
}
.tv-label::after {
  content: "";
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  border: 5px solid transparent;
}
.tv-label.above::after { top: 100%; border-top-color: var(--lbl, #888); }
.tv-label.below::after { bottom: 100%; border-bottom-color: var(--lbl, #888); }
.tv-label .sub { font-weight: 600; font-size: 9.5px; opacity: .92; }

/* ---------- overlay tables ---------- */

.panel {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  backdrop-filter: blur(3px);
  max-width: min(46vw, 430px);
  overflow: hidden;
}
.panel table { border-collapse: collapse; width: 100%; }
.panel td {
  padding: 2.5px 8px;
  border: 1px solid var(--border-soft);
  white-space: nowrap;
}
.panel .hdr td { background: var(--bg-header); color: #fff; font-weight: 700; }
.panel .sub td { color: var(--text-dim); }
.panel .lbl { color: var(--text-dim); }
.panel .rt { text-align: right; }

/* Right offsets for rule/tech tables are set from JS to track the actual
   price-scale width, so tables never overlap the axis. */
#perf-table { left: 12px; top: 64px; }
#rule-table { right: 70px; top: 8px; }
#tech-table { right: 70px; bottom: 36px; }
#math-table { left: 12px; bottom: 36px; }
#laws-table { left: 50%; transform: translateX(-50%); top: 64px; }

.panel-close {
  position: absolute;
  top: 1px;
  right: 1px;
  width: 15px;
  height: 15px;
  border: 0;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  font: 700 11px/1 var(--font);
  cursor: pointer;
  opacity: 0;
  transition: opacity .15s;
  padding: 0;
}
.panel:hover .panel-close { opacity: .85; }
.panel-close:hover { background: rgba(255, 255, 255, 0.3); }

.c-amber { color: #f59e0b; }
.c-teal { color: #14b8a6; }
.c-dim { color: var(--text-dim); }

.c-buy { color: var(--buy); }
.c-sell { color: var(--sell); }
.c-halving { color: var(--halving); }
.c-up { color: #4caf50; }
.c-down { color: #ef5350; }
.c-techbuy { color: var(--tech-buy); }
.c-techsell { color: var(--tech-sell); }
.c-white { color: #fff; }
.c-silver { color: #c8ccd4; }
.hdr-halving td { background: var(--halving) !important; color: #fff !important; }

/* countdown pinned to the price axis */
.axis-countdown {
  position: absolute;
  right: 0;
  z-index: 25;
  font-size: 10px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: #fff;
  background: #434651;
  padding: 1px 5px;
  border-radius: 2px;
  pointer-events: none;
  display: none;
}

/* ---------- loading ---------- */

.loading {
  position: absolute; inset: 0; z-index: 60;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  background: var(--bg);
  color: var(--text-dim); font-size: 13px;
}
.loading.hide { display: none; }
.loading-spin {
  width: 34px; height: 34px; border-radius: 50%;
  border: 3px solid #2a2e39; border-top-color: var(--accent);
  animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* error toast */
.toast {
  position: absolute; left: 50%; bottom: 18px; transform: translateX(-50%);
  z-index: 70; background: #2a2e39; color: var(--text-bright);
  border: 1px solid var(--border); border-radius: 6px;
  padding: 8px 14px; font-size: 12px;
  box-shadow: 0 6px 24px rgba(0,0,0,.5);
}

@media (max-width: 1100px) {
  .panel { max-width: min(60vw, 430px); }
}

/* ---------- mobile ---------- */
@media (max-width: 760px) {
  .brand-name { display: none; }
  #topbar { gap: 8px; padding: 4px 8px; row-gap: 4px; }
  .top-price { min-width: 0; }
  #top-price-value { font-size: 14px; }
  #top-price-change { font-size: 11px; }

  /* One horizontally swipeable controls row (TradingView-style).
     NOTE: no -webkit-overflow-scrolling:touch here — on iOS it turns the
     row into the containing block for position:fixed children, which made
     the dropdown menus open invisibly inside the clipped 40px row. */
  .controls {
    order: 10;
    flex: 1 1 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
    margin-left: 0;
    padding-bottom: 2px;
    gap: 6px;
  }
  .controls::-webkit-scrollbar { display: none; }
  .btn { padding: 5px 8px; font-size: 11px; }

  /* Dropdowns escape the scrolling row as a full-width sheet (the elements
     are also re-parented to <body> on mobile — see wireToolbar). */
  .dropdown-menu {
    position: fixed;
    left: 8px; right: 8px; top: 64px;
    max-height: 70vh; overflow-y: auto;
    z-index: 60;
  }
  .dropdown-menu label { font-size: 13px; padding: 7px 0; }

  #legend { left: 8px; top: 4px; }
  .legend-row1 { font-size: 11.5px; }
  .legend-row2 { font-size: 9.5px; gap: 6px; margin-top: 2px; }
  .legend-row3 { font-size: 9px; }

  /* Panels: readable and scrollable instead of clipped. */
  .panel { font-size: 10px; max-width: calc(100vw - 70px); overflow-x: auto; }
  .panel td { padding: 2px 6px; }
  #rule-table { top: 46px; }   /* below the legend */
  #perf-table { top: 96px; }
  #laws-table { left: 8px; transform: none; max-width: calc(100vw - 70px); }
  #math-table { max-width: calc(100vw - 70px); }

  .tv-label { font-size: 8.5px; padding: 2px 5px; }
  .axis-countdown { font-size: 9px; }
}
