:root {
  --bg: #0e1116;
  --panel: #161b22;
  --panel-2: #1f2630;
  --border: #2a313c;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #f78166;
  --accent-2: #58a6ff;
  --star: #f5c518;
  --live: #ff5b5b;
  --green: #7ee787;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text);
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
a { color: var(--accent-2); text-decoration: none; }

.app {
  display: grid;
  grid-template-rows: auto 1fr auto auto;   /* topbar | tracklist | timeline | player */
  height: 100vh;
}

.topbar {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  padding: 10px 14px; background: var(--panel); border-bottom: 1px solid var(--border);
}
.logo { display: flex; align-items: baseline; gap: 6px; margin-right: 8px; user-select: none; }
.logo-mark {
  display: inline-flex; align-items: baseline; gap: 1px;
  font-weight: 900; font-size: 18px; letter-spacing: -0.5px;
  padding: 2px 6px; border-radius: 4px;
  background: linear-gradient(135deg, var(--accent), #d65d3a);
  color: #0e1116;
  font-style: italic;
}
.logo-mark .logo-you { letter-spacing: -1px; }
.logo-mark .logo-fm  { color: #fff; }
.logo-sub {
  font-size: 11px; color: var(--muted); font-weight: 600;
  text-transform: uppercase; letter-spacing: 1.5px;
}
.topbar input, .topbar select {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  padding: 6px 8px; border-radius: 6px; font-size: 13px;
}
.topbar input[type="search"] { min-width: 220px; }
.topbar button {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  padding: 6px 10px; border-radius: 6px; cursor: pointer;
}
.topbar button.active { background: var(--accent); color: #0e1116; border-color: var(--accent); }

.tracklist { overflow-y: auto; padding-bottom: 8px; }

.day-sep {
  position: sticky; top: 0; z-index: 2;
  padding: 6px 14px;
  background: linear-gradient(180deg, rgba(14,17,22,0.95), rgba(14,17,22,0.80));
  backdrop-filter: blur(4px);
  border-bottom: 1px solid var(--border);
  color: var(--accent-2);
  font-size: 12px; font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.6px;
}

.load-more {
  padding: 16px; text-align: center;
}
.load-more button {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  padding: 8px 16px; border-radius: 6px; cursor: pointer; font-size: 13px;
}
.load-more button:hover { background: var(--panel); }

.live-jump {
  background: var(--accent); color: #0e1116; border: 1px solid var(--accent);
  padding: 6px 10px; border-radius: 6px; cursor: pointer; font-size: 12px; font-weight: 600;
}
.live-jump:hover { filter: brightness(1.1); }
.station-select {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  padding: 5px 8px; border-radius: 6px; font-size: 12px; font-weight: 600;
  cursor: pointer; margin-right: 4px;
}

.track {
  display: grid; grid-template-columns: 56px 70px 1fr auto auto auto;
  gap: 10px; align-items: center;
  padding: 8px 14px; border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.track:hover { background: var(--panel); }
.track.playing { background: rgba(247, 129, 102, 0.12); }
.track .cover { width: 48px; height: 48px; background: var(--panel-2); border-radius: 4px; object-fit: cover; display: flex; align-items: center; justify-content: center; font-size: 22px; }
.cover-fallback {
  background: linear-gradient(135deg, var(--panel-2), #2a313c);
  color: var(--muted); font-weight: 600; font-size: 22px; user-select: none;
}
.mod-cover {
  background: linear-gradient(135deg, #443, #221) !important;
  font-size: 22px;
}
.track .time { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 12px; line-height: 1.2; }
.track .time-day   { font-size: 10px; color: var(--muted); opacity: 0.7; }
.track .time-clock { font-size: 14px; color: var(--text); font-weight: 500; }
.track .time-dur   { font-size: 11px; color: var(--muted); }
.meta-time-inline  { display: none; }    /* desktop uses dedicated .time column */
.track .meta .title { font-weight: 600; }
.track .meta .artist { color: var(--muted); font-size: 13px; }
.track .type {
  font-size: 11px; padding: 2px 6px; border-radius: 999px; background: var(--panel-2);
  color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px;
}
.track .star { font-size: 18px; color: var(--muted); cursor: pointer; padding: 0 6px; }
.track .star.on { color: var(--star); }
.track .menu { color: var(--muted); padding: 0 8px; cursor: pointer; user-select: none; }
.track .tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 3px; }
.track .tag {
  font-size: 10px; padding: 1px 6px; border-radius: 999px;
  background: rgba(88,166,255,0.12); color: var(--accent-2);
}

/* Track-State styling */
.track.state-future { opacity: 0.55; cursor: default; }
.track.state-future:hover { background: transparent; }
.track.state-live {
  background: linear-gradient(90deg, rgba(255,91,91,0.10), rgba(255,91,91,0.02) 60%);
  border-left: 3px solid var(--live);
  padding-left: 11px;
}
.track.state-gap { opacity: 0.65; }
.track.moderation { background: rgba(255,255,255,0.025); }
.track.moderation .meta .title { color: var(--text); }
.track.moderation .meta .artist { font-style: italic; }

/* Badges */
.badge {
  display: inline-block; font-size: 10px; padding: 1px 6px; border-radius: 999px;
  margin-left: 6px; text-transform: uppercase; letter-spacing: 0.5px; vertical-align: middle;
}
.badge.live    { background: rgba(255,91,91,0.20); color: var(--live); animation: pulse 1.6s infinite; }
.badge.future  { background: rgba(139,148,158,0.18); color: var(--muted); }
.badge.gap     { background: rgba(255,80,80,0.14); color: #ff7a7a; }
.badge.moderation { background: rgba(245,197,24,0.18); color: var(--star); }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.55; }
}

/* Timeline */
.timeline {
  background: var(--panel);
  border-top: 1px solid var(--border);
  user-select: none;
}
/* Click/scrub area lives in its own element so getBoundingClientRect() returns
   the SVG's actual top — required for click coords to align with what's drawn. */
.tl-canvas {
  position: relative;
  cursor: grab;
  touch-action: pan-y;       /* allow vertical page scroll, capture horizontal */
}
.timeline.dragging .tl-canvas { cursor: grabbing; }
.timeline svg { display: block; }
.timeline-tooltip {
  position: absolute; top: 4px;
  background: var(--panel-2); color: var(--text);
  font-size: 11px; padding: 4px 8px; border-radius: 4px;
  border: 1px solid var(--border);
  transform: translateX(-50%);
  pointer-events: none;
  white-space: nowrap;
  box-shadow: 0 2px 8px rgba(0,0,0,0.4);
  max-width: 320px;
}
.timeline-tooltip .tt-title { font-weight: 600; font-size: 12px; margin-bottom: 1px; }
.timeline-tooltip .tt-meta  { color: var(--muted); }
.timeline-tooltip .tt-type  { color: var(--accent-2); }
.timeline-tooltip .tt-time  { color: var(--muted); font-variant-numeric: tabular-nums; margin-top: 2px; }
.tl-legend {
  display: flex; flex-wrap: wrap; align-items: center; gap: 14px;
  font-size: 10px; color: var(--muted);
  padding: 4px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-2);
  user-select: none;
}
.tl-legend.open { padding: 6px 12px; }
.legend-toggle {
  background: transparent; border: 0; color: var(--muted);
  font-size: 11px; padding: 2px 4px; cursor: pointer; border-radius: 3px;
  font-family: inherit;
}
.legend-toggle:hover { color: var(--text); background: rgba(255,255,255,0.05); }
.tl-legend > span { display: flex; align-items: center; gap: 4px; pointer-events: none; }
.tl-legend .sw { display: inline-block; width: 12px; height: 8px; border-radius: 2px; }
.tl-legend .reset-hint { color: var(--accent); font-style: italic; pointer-events: auto; }

/* Player */
.player {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 14px; background: var(--panel); border-top: 1px solid var(--border);
}
.player .now { flex: 1; display: flex; align-items: center; gap: 14px; min-width: 0; }
.player .now .cover {
  width: 64px; height: 64px; border-radius: 6px; object-fit: cover; background: var(--panel-2);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  box-shadow: 0 2px 6px rgba(0,0,0,0.3);
}
.player .now .meta { min-width: 0; flex: 1; }
.player .now .title { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.player .now .artist { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 1px; }
.player .now .player-clock { color: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; margin-top: 4px; }

.player-controls {
  display: flex; align-items: center; gap: 8px;
  padding: 0 4px;
}
.ctl-skip {
  width: 38px; height: 38px; border-radius: 50%;
  background: transparent; color: var(--text); border: 1px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; padding: 0;
  transition: background 0.1s, transform 0.1s;
}
.ctl-skip:hover { background: var(--panel-2); }
.ctl-skip:active { transform: scale(0.94); }
.ctl-play {
  width: 48px; height: 48px; border-radius: 50%;
  background: var(--accent); color: #0e1116;
  border: 0; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: transform 0.1s, filter 0.1s;
  box-shadow: 0 2px 8px rgba(247, 129, 102, 0.35);
}
.ctl-play:hover { filter: brightness(1.1); }
.ctl-play:active { transform: scale(0.94); }
.ctl-volume { display: flex; align-items: center; gap: 6px; margin-left: 4px; }
.ctl-mute {
  background: transparent; border: 0; color: var(--text);
  font-size: 16px; cursor: pointer; padding: 4px 6px; border-radius: 4px;
}
.ctl-mute:hover { background: var(--panel-2); }
.ctl-volume input[type="range"] {
  width: 80px;
  accent-color: var(--accent);
}

.badge.timeshift {
  background: rgba(245,197,24,0.18); color: var(--star);
  border: 1px solid rgba(245,197,24,0.3);
  cursor: pointer; font-family: inherit;
}
.badge.timeshift:hover { background: rgba(245,197,24,0.3); }

/* Modal */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,0.6); display: flex; align-items: center; justify-content: center; z-index: 100; }
.modal { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 16px; min-width: 320px; max-width: 480px; }
.modal h3 { margin: 0 0 8px; }
.modal .tags-input { display: flex; flex-wrap: wrap; gap: 6px; padding: 6px; min-height: 36px; background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px; }
.modal .tags-input .tag { background: var(--accent-2); color: #0e1116; padding: 2px 8px; border-radius: 999px; font-size: 12px; }
.modal .tags-input input { flex: 1; min-width: 100px; background: transparent; border: 0; color: var(--text); outline: none; font-size: 13px; }
.modal .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; }
.modal .actions button { background: var(--panel-2); border: 1px solid var(--border); color: var(--text); padding: 6px 12px; border-radius: 6px; cursor: pointer; }
.modal .actions button.primary { background: var(--accent); color: #0e1116; border-color: var(--accent); }

/* Context menu — fixed positioning so x/y are viewport coords */
.menu-popup {
  position: fixed;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px;
  z-index: 50;
  min-width: 200px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
  transform: translateX(-100%);   /* anchor right edge to button */
}
.menu-popup button {
  display: block; width: 100%; text-align: left; background: transparent; border: 0;
  color: var(--text); padding: 8px 10px; cursor: pointer; border-radius: 4px;
  font: inherit;
}
.menu-popup button:hover:not(:disabled) { background: var(--panel-2); }
.menu-popup button:disabled { color: var(--muted); cursor: not-allowed; }

.empty { padding: 40px; text-align: center; color: var(--muted); }
.rec-status { font-size: 12px; font-variant-numeric: tabular-nums; }

/* -------------------------------------------------------------------------- */
/*  View toggle                                                               */
/* -------------------------------------------------------------------------- */
.view-toggle {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--border);
  padding: 6px 10px; border-radius: 6px; cursor: pointer; font-size: 12px;
}
.view-toggle:hover { background: var(--panel); }

/* -------------------------------------------------------------------------- */
/*  Stats page                                                                */
/* -------------------------------------------------------------------------- */
.stats {
  overflow-y: auto;
  padding: 20px 24px 40px;
  background: var(--bg);
  grid-row: 2;
}
.stats-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 24px; flex-wrap: wrap; gap: 12px;
}
.stats-title { display: flex; align-items: center; gap: 12px; }
.stats-title h2 { margin: 0; font-size: 22px; font-weight: 700; }
.stats-icon { font-size: 24px; }
.stats-period-label {
  background: var(--accent); color: #0e1116;
  padding: 4px 10px; border-radius: 999px;
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.5px;
}
.stats-period-select { display: flex; gap: 4px; }
.stats-period-select button {
  background: var(--panel-2); color: var(--muted); border: 1px solid var(--border);
  padding: 5px 10px; border-radius: 6px; cursor: pointer; font-size: 12px;
}
.stats-period-select button:hover { color: var(--text); }
.stats-period-select button.active { background: var(--accent); color: #0e1116; border-color: var(--accent); }

.stats-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px; margin-bottom: 32px;
}
.stat-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 14px 16px;
}
.stat-card .stat-value {
  font-size: 24px; font-weight: 800; color: var(--accent-2);
  font-variant-numeric: tabular-nums; line-height: 1.1;
}
.stat-card .stat-label {
  font-size: 11px; color: var(--muted); text-transform: uppercase;
  letter-spacing: 1px; margin-top: 4px;
}
.stat-card .stat-sub {
  font-size: 10px; color: var(--muted); opacity: 0.7; margin-top: 4px;
  font-variant-numeric: tabular-nums;
}

.stats-section { margin-bottom: 32px; }
.stats-section h3 {
  margin: 0 0 12px; font-size: 15px; font-weight: 700; color: var(--text);
  display: flex; align-items: baseline; gap: 8px;
}
.stats-section h3 small {
  font-size: 11px; color: var(--muted); font-weight: 400;
  text-transform: none; letter-spacing: 0.3px;
}
.stats-two-col {
  display: grid; grid-template-columns: 1fr 1fr; gap: 24px;
}

/* Rank lists ------------------------------------------------------------- */
.rank-list, .duration-list {
  list-style: none; margin: 0; padding: 0;
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px;
  overflow: hidden;
}
.rank-list li, .duration-list li {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px; border-bottom: 1px solid var(--border);
  cursor: default;
}
.rank-list li:last-child, .duration-list li:last-child { border-bottom: 0; }
.rank-list li[onclick], .rank-list li:has([onclick]) { cursor: pointer; }
.rank-list li:hover { background: var(--panel-2); }
.rank-pos {
  width: 28px; text-align: right; flex-shrink: 0;
  font-weight: 700; color: var(--muted); font-variant-numeric: tabular-nums;
  font-size: 12px;
}
.rank-cover {
  width: 36px; height: 36px; border-radius: 4px; object-fit: cover;
  background: var(--panel-2); flex-shrink: 0;
}
.rank-meta { flex: 1; min-width: 0; }
.rank-title {
  font-weight: 600; font-size: 13px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.rank-artist {
  font-size: 11px; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.rank-bar {
  flex: 0 0 120px;
  height: 6px; background: rgba(255,255,255,0.05); border-radius: 3px; overflow: hidden;
}
.rank-bar > div {
  height: 100%; background: var(--accent-2); border-radius: 3px;
  transition: width 0.3s;
}
.rank-count {
  flex-shrink: 0; min-width: 50px; text-align: right;
  font-variant-numeric: tabular-nums; font-size: 12px; font-weight: 600; color: var(--text);
}

/* Hour / weekday bar chart ---------------------------------------------- */
.hour-chart {
  display: flex; align-items: flex-end; gap: 3px; height: 140px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px;
  padding: 12px 14px;
}
.weekday-chart { gap: 8px; }
.bar-col {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px;
  height: 100%; min-width: 0;
}
.bar-col .bar {
  width: 100%; background: linear-gradient(180deg, var(--accent), var(--accent-2));
  min-height: 2px; border-radius: 3px 3px 0 0;
  transition: height 0.3s;
}
.bar-col:hover .bar { filter: brightness(1.2); }
.bar-label {
  font-size: 9px; color: var(--muted); font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.weekday-chart .bar-label { font-size: 11px; }

/* Daily timeline -------------------------------------------------------- */
.daily-chart {
  display: flex; flex-direction: column; gap: 4px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px;
  padding: 14px;
  height: 158px;
}
.daily-chart > * { display: flex; }
.daily-chart .day-col, .daily-chart > div:first-child > .day-col {
  flex: 1 1 0; min-width: 4px;
}
.daily-chart {
  flex-direction: column;
}
.daily-chart > .day-col { display: none; }   /* override above */
.daily-chart {
  display: grid;
  grid-template-rows: 1fr auto;
}
.daily-chart > div:not(.daily-axis) {
  display: flex; align-items: flex-end; gap: 2px; height: 100%;
}
.day-col {
  flex: 1; min-width: 4px;
  background: linear-gradient(180deg, var(--green), var(--accent-2));
  border-radius: 2px 2px 0 0;
  min-height: 2px;
  cursor: default;
}
.day-col:hover { filter: brightness(1.3); }
.daily-axis {
  display: flex; font-size: 10px; color: var(--muted); margin-top: 6px;
  font-variant-numeric: tabular-nums;
}

/* Type list / horizontal bars ------------------------------------------ */
.type-list { list-style: none; margin: 0; padding: 0; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.type-list li {
  display: grid; grid-template-columns: 90px 1fr auto; gap: 10px; align-items: center;
  padding: 8px 14px; border-bottom: 1px solid var(--border);
}
.type-list li:last-child { border-bottom: 0; }
.type-label { font-size: 12px; font-weight: 600; color: var(--text); }
.type-bar { background: rgba(255,255,255,0.05); height: 8px; border-radius: 4px; overflow: hidden; }
.type-bar > div { background: var(--accent); height: 100%; border-radius: 4px; }
.type-count {
  font-size: 12px; color: var(--text); font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.type-count small { color: var(--muted); margin-left: 4px; }

@media (max-width: 720px) {
  .stats { padding: 12px 12px 32px; }
  .stats-title h2 { font-size: 18px; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .stat-card .stat-value { font-size: 18px; }
  .stats-two-col { grid-template-columns: 1fr; }
  .rank-bar { display: none; }
  .rank-cover { width: 32px; height: 32px; }
  .type-list li { grid-template-columns: 70px 1fr auto; padding: 6px 10px; gap: 6px; }
}

/* -------------------------------------------------------------------------- */
/*  Mobile / narrow viewport                                                  */
/* -------------------------------------------------------------------------- */
@media (max-width: 720px) {
  .topbar { padding: 8px 10px; gap: 6px; }
  .logo-sub { display: none; }
  .logo-mark { font-size: 14px; padding: 1px 4px; }
  .topbar input[type="search"] { min-width: 0; flex: 1 1 140px; }
  .topbar input[type="date"] { display: none; }      /* date pickers hidden on mobile */
  .topbar select { font-size: 12px; padding: 4px 6px; }
  .topbar .live-jump { padding: 4px 8px; font-size: 11px; }
  .rec-status { display: none; }                     /* status moves into timeline area */

  .track {
    grid-template-columns: 44px 1fr auto auto;
    gap: 8px; padding: 6px 10px;
  }
  .track .cover { width: 40px; height: 40px; font-size: 18px; }
  .track .time { display: none; }                    /* time merged into meta below */
  .meta-time-inline {
    display: block; font-size: 10px; color: var(--muted);
    margin-top: 2px; font-variant-numeric: tabular-nums;
  }
  .track .meta .title { font-size: 13px; }
  .track .meta .artist { font-size: 11px; }
  .track .type { display: none; }
  .track .star { font-size: 16px; padding: 0 4px; }
  .track .menu { padding: 0 4px; }

  .day-sep { font-size: 11px; padding: 5px 10px; }

  .timeline svg { height: 90px; }
  .tl-legend { font-size: 9px; gap: 8px; }

  /* Player: cover smaller, controls right-aligned, stacks if needed */
  .player { padding: 8px 10px; gap: 8px; }
  .player .now { gap: 10px; min-width: 0; flex: 1 1 100%; }
  .player .now .cover { width: 48px; height: 48px; }
  .player .now .title { font-size: 13px; }
  .player .now .artist { font-size: 11px; }
  .player-controls { flex: 1 1 100%; justify-content: center; gap: 16px; }
  .ctl-volume input[type="range"] { width: 100px; }

  .modal { min-width: 0; max-width: calc(100vw - 32px); margin: 16px; }
  .menu-popup { min-width: 180px; max-width: calc(100vw - 16px); }
}

/* even narrower (very small phones) */
@media (max-width: 400px) {
  .topbar select.tag-select { display: none; }
  .topbar .live-jump { display: none; }
  .ctl-volume { display: none; }                     /* free up space, kept play+mute via long-press if needed */
}
