html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #000;
}

html, body, body * {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

#chart-viewport {
  position: fixed;
  inset: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px)
    env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
  height: calc(100dvh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
  touch-action: none;
  overscroll-behavior: none;
}

canvas {
  display: block;
  touch-action: none;
}

#dataset-controls {
  position: absolute;
  z-index: 2;
  inset: 0 0 auto;
  height: max(44px, calc(100% / 12));
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  box-sizing: border-box;
  box-shadow: inset 0 -0.5px #fff;
  background: #000;
}
#dataset-controls button {
  min-width: 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: #a0a0a0;
  font: max(14px, min(2.381vh, 1.333vw)) Arial, sans-serif;
  cursor: pointer;
}
#dataset-controls button[aria-pressed="true"] { color: #ff8040; }
#dataset-controls button:hover { color: #fff !important; }
#dataset-controls button:focus-visible { outline-offset: -4px; border-radius: 5px; }
#dataset-controls button:disabled { cursor: default; }

#app-status {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  box-sizing: border-box;
  position: fixed;
  z-index: 1;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: min(560px, calc(100% - 32px));
  padding: 20px;
  border: 1px solid #49647c;
  border-radius: 10px;
  color: #edf3f8;
  background: #071d30;
  font: 14px/1.6 system-ui, sans-serif;
}
#app-status[hidden], #p5_loading { display: none; }
#app-status p { margin: 0; }
.loading-spinner {
  width: 24px;
  height: 24px;
  flex-shrink: 0;
  border: 3px solid #49647c;
  border-top-color: #9dd7ff;
  border-radius: 50%;
  animation: loading-spin 0.8s linear infinite;
}
#app-status[role="alert"] .loading-spinner { display: none; }
@keyframes loading-spin {
  to { transform: rotate(360deg); }
}
@media (prefers-reduced-motion: reduce) {
  .loading-spinner { animation: none; }
}

#data-info-button {
  position: absolute;
  z-index: 2;
  left: 12px;
  bottom: calc(25% + 12px);
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid #49647c;
  border-radius: 50%;
  background: #102b43;
  color: #edf3f8;
  font: bold 23px Georgia, serif;
  cursor: pointer;
}
#data-info-button:hover { background: #25435c; }
button:focus-visible, summary:focus-visible, a:focus-visible {
  outline: 2px solid #9dd7ff;
  outline-offset: 3px;
}
button { -webkit-tap-highlight-color: transparent; }

#timeline-event-controls {
  position: absolute;
  inset: 0;
  z-index: 1;
  pointer-events: none;
}
#timeline-event-controls button {
  position: absolute;
  box-sizing: border-box;
  padding: 0;
  border: 0;
  background: transparent;
  pointer-events: auto;
  touch-action: none;
  cursor: pointer;
}
#timeline-event-controls button[hidden], #timeline-details[hidden] { display: none; }
#timeline-event-controls button[data-active="true"] { background: #fff2; }
#timeline-event-controls button[data-dragging="true"] { cursor: move; }
#timeline-event-controls button:focus-visible { outline-offset: -2px; }
#timeline-details {
  position: fixed;
  z-index: 2;
  box-sizing: border-box;
  width: min(320px, calc(100% - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px) - 16px));
  max-height: calc(100dvh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px) - 16px);
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 12px 16px 16px;
  border: 1px solid #49647c;
  border-radius: 8px;
  background: #071d30;
  color: #d4dde6;
  box-shadow: 0 8px 24px #0008;
  font: 13px/1.5 system-ui, sans-serif;
  color-scheme: dark;
  scrollbar-width: thin;
  scrollbar-color: #49647c #0a233a;
}
#timeline-details header { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
#timeline-details h2 { margin: 5px 0 0; color: #edf3f8; font-size: 15px; font-weight: 600; }
#timeline-details .info-close { flex-shrink: 0; }
#timeline-details p { margin: 8px 0 12px; }
#timeline-details-date { color: #a9bccd; }
#timeline-details a { color: #9dd7ff; text-underline-offset: 3px; }
#timeline-details[data-expanded="false"] {
  width: max-content;
  max-width: min(280px, calc(100% - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px) - 16px));
  padding: 8px 12px;
}
#timeline-details[data-expanded="false"] h2 { margin: 0; font-size: 13px; }
#timeline-details[data-expanded="false"] p { margin: 4px 0 0; font-size: 12px; }
#timeline-details[data-expanded="false"] .info-close,
#timeline-details[data-expanded="false"] #timeline-details-description,
#timeline-details[data-expanded="false"] #timeline-details-link { display: none; }

dialog {
  inset: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px)
    env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
  box-sizing: border-box;
  width: min(620px, calc(100% - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px) - 32px));
  max-width: none;
  max-height: min(80dvh, calc(100dvh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px) - 32px));
  padding: 0;
  border: 1px solid #334b61;
  border-radius: 10px;
  color: #d4dde6;
  background: #071d30;
  box-shadow: 0 18px 60px #0008;
  font: 14px/1.6 system-ui, sans-serif;
  color-scheme: dark;
}
dialog[open] { display: flex; flex-direction: column; }
dialog::backdrop { background: #0009; backdrop-filter: blur(3px); }
.info-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  gap: 16px;
  padding: 16px 20px;
  border-bottom: 1px solid #334b61;
}
.info-header h2 { margin: 0; color: #edf3f8; font-size: 20px; font-weight: 600; }
.info-header form { margin: 0; }
.info-close {
  width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: #d4dde6;
  font: 26px/1 system-ui, sans-serif;
  cursor: pointer;
}
.info-close:hover { background: #25435c; color: #fff; }
.info-content {
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 4px 20px 20px;
  scrollbar-width: thin;
  scrollbar-color: #49647c #0a233a;
}
.info-content::-webkit-scrollbar { width: 8px; }
.info-content::-webkit-scrollbar-track { background: #0a233a; }
.info-content::-webkit-scrollbar-thumb { background: #49647c; border-radius: 6px; }
.info-content::-webkit-scrollbar-thumb:hover { background: #7391ab; }
dialog a { color: #9dd7ff; text-underline-offset: 3px; }
dialog p { margin: 16px 0; }
dialog details { border-top: 1px solid #334b61; }
dialog summary { padding: 12px 0; color: #edf3f8; cursor: pointer; font-weight: 600; }
dialog details p:first-of-type { margin-top: 0; }
.info-links { padding-top: 16px; border-top: 1px solid #334b61; }
.info-links p { margin: 0 0 8px; }

@media (max-width: 600px) and (orientation: portrait) {
  #dataset-controls {
    height: 88px;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-template-rows: repeat(2, 44px);
  }
}

#zoom-recorder[hidden], #recorder-guard[hidden], #recorder-download[hidden] { display: none; }
#recorder-guard { position: fixed; inset: 0; z-index: 3; }
#zoom-recorder {
  position: fixed;
  z-index: 4;
  top: max(12px, env(safe-area-inset-top, 0px));
  right: max(12px, env(safe-area-inset-right, 0px));
  width: min(340px, calc(100% - 24px));
  max-height: calc(100dvh - 24px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
  box-sizing: border-box;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 12px 16px;
  border: 1px solid #49647c;
  border-radius: 10px;
  background: #071d30;
  color: #d4dde6;
  font: 13px/1.5 system-ui, sans-serif;
  color-scheme: dark;
  scrollbar-width: thin;
  scrollbar-color: #49647c #0a233a;
}
#zoom-recorder header { display: flex; align-items: center; justify-content: space-between; }
#zoom-recorder h2 { margin: 0; font-size: 16px; font-weight: 600; }
#zoom-recorder label { display: block; margin-top: 8px; }
#zoom-recorder input, #zoom-recorder select, #zoom-recorder button:not(.info-close), #recorder-download {
  box-sizing: border-box;
  min-height: 36px;
  border: 1px solid #49647c;
  border-radius: 5px;
  background: #102b43;
  color: #edf3f8;
  font: inherit;
  padding: 6px 8px;
}
#zoom-recorder input, #zoom-recorder select { width: 100%; min-width: 0; font-size: 16px; }
#zoom-recorder button, #recorder-download { cursor: pointer; }
#zoom-recorder :disabled { opacity: 0.5; cursor: default; }
.recorder-range { display: flex; gap: 8px; }
.recorder-range input { flex: 1; }
.recorder-range button { flex-shrink: 0; }
.recorder-options { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
#zoom-recorder .recorder-loop { display: flex; align-items: center; gap: 8px; }
#zoom-recorder .recorder-loop input { width: auto; margin: 0; accent-color: #8dd3d5; }
.recorder-note, #recorder-status { margin: 12px 0; color: #a9bccd; }
.recorder-note { font-size: 12px; }
.recorder-actions { display: flex; gap: 8px; }
#recorder-download { display: block; text-align: center; text-decoration: none; }

@media (pointer: coarse) {
  #timeline-details { font-size: 14px; }
  dialog { font-size: 15px; }
}
