:root {
  color-scheme: dark;
  --bg: #000;
  --panel: #1c1c1e;
  --fill: #2c2c2e;
  --text: #f5f5f7;
  --muted: #aeaeb2;
  --line: rgba(255, 255, 255, 0.1);
  --accent: #0a84ff;
  --danger: #ff453a;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-text-size-adjust: 100%;
}

body {
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}

button, select { font: inherit; color: inherit; }

button {
  background: var(--fill);
  border: 0;
  border-radius: 8px;
  min-height: 40px;
  padding: 8px 12px;
  touch-action: manipulation;
}

button:active { opacity: 0.75; }

button:disabled { opacity: 0.4; }

button.primary { background: var(--accent); color: #fff; }

button.texty {
  background: transparent;
  color: var(--accent);
  min-height: 32px;
  margin-left: auto;
}

button:focus-visible,
select:focus-visible,
.event:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

[hidden] { display: none !important; }

#app {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
  overflow: hidden;
}

aside {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border-right: 1px solid var(--line);
}

.side-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px;
  border-bottom: 1px solid var(--line);
}

#count { color: var(--muted); font-size: 13px; }

#list {
  overflow: auto;
  padding: 8px;
  flex: 1;
  -webkit-overflow-scrolling: touch;
}

.section {
  color: var(--muted);
  font-size: 12px;
  padding: 12px 8px 4px;
}

.event {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border-radius: 8px;
  padding: 8px;
  min-height: 0;
}

.event.selected { background: rgba(10, 132, 255, 0.28); }

.event .t { font-weight: 600; }

.event .s,
.event .m,
.hint {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}

.hint { padding: 8px; }

main {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  min-height: 52px;
}

#title {
  flex: 1;
  min-width: 0;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#btn-events { display: none; }

#stage {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
}

#empty {
  width: min(100%, 40rem);
  max-height: 100%;
  overflow: auto;
  padding: 24px 20px;
  text-align: center;
}

#empty h1 { font-size: 22px; margin: 0 0 8px; }

#empty p { margin: 8px 0; line-height: 1.5; color: var(--muted); }

#empty .actions {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 16px;
}

#status:not(:empty) { color: #ffd60a; }

#grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(2, minmax(0, 1fr));
  gap: 2px;
  aspect-ratio: 2172 / 938;
  width: 100%;
  max-height: 100%;
  height: auto;
  background: #000;
}

#grid.loading { opacity: 0.7; }

#grid.focus {
  aspect-ratio: 1448 / 938;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr);
}

@supports (width: 1cqw) {
  #stage { container-type: size; }

  #grid {
    width: min(100cqw, calc(100cqh * 2172 / 938));
    height: min(100cqh, calc(100cqw * 938 / 2172));
    max-height: none;
  }

  #grid.focus {
    width: min(100cqw, calc(100cqh * 1448 / 938));
    height: min(100cqh, calc(100cqw * 938 / 1448));
  }
}

#grid.focus .cell { display: none; }

#grid.focus .cell.active { display: block; }

#grid.focus .tag { display: none; }

.cell {
  position: relative;
  background: #111;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  cursor: pointer;
}

.cell.empty,
.cell.broken { cursor: default; }

.cell video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  background: #000;
  pointer-events: none;
}

.tag {
  position: absolute;
  left: 6px;
  top: 6px;
  font-size: 11px;
  line-height: 1.3;
  padding: 2px 6px;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.55);
  pointer-events: none;
}

.placeholder {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  color: #636366;
  font-size: 12px;
  text-align: center;
  padding: 4px;
}

.cell.empty .placeholder,
.cell.broken .placeholder { display: flex; }

.cell.empty video,
.cell.broken video { display: none; }

#clock {
  position: absolute;
  left: 50%;
  bottom: 8px;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.55);
  padding: 2px 8px;
  border-radius: 6px;
  font-size: 12px;
  pointer-events: none;
  z-index: 2;
}

.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-variant-numeric: tabular-nums;
}

#focus-bar {
  position: absolute;
  top: 8px;
  left: 8px;
  right: 8px;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  pointer-events: none;
}

#focus-bar button {
  pointer-events: auto;
  background: rgba(28, 28, 30, 0.82);
}

#focus-label {
  font-size: 13px;
  padding: 4px 8px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.45);
}

.focus-nav { display: flex; gap: 6px; pointer-events: none; }

.focus-nav button { min-width: 44px; pointer-events: auto; }

#resume {
  position: absolute;
  z-index: 6;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background: var(--accent);
  color: #fff;
  padding: 12px 18px;
}

footer {
  background: var(--panel);
  border-top: 1px solid var(--line);
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#banner {
  background: #3a2a00;
  color: #ffd60a;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  line-height: 1.4;
}

.times {
  display: flex;
  align-items: center;
  gap: 8px;
}

#elapsed, #total {
  width: 58px;
  font-size: 12px;
  color: var(--muted);
}

#elapsed { text-align: right; }

#bar {
  position: relative;
  flex: 1;
  height: 28px;
  touch-action: none;
  cursor: pointer;
}

#bar::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  height: 4px;
  margin-top: -2px;
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.18);
}

#fill {
  position: absolute;
  left: 0;
  top: 50%;
  height: 4px;
  margin-top: -2px;
  width: 0;
  border-radius: 99px;
  background: var(--accent);
}

#marker,
#thumb {
  position: absolute;
  top: 50%;
  border-radius: 50%;
  transform: translate(-50%, -50%);
}

#marker {
  width: 8px;
  height: 8px;
  background: var(--danger);
  box-shadow: 0 0 0 2px rgba(255, 69, 58, 0.35);
}

#thumb {
  left: 0;
  width: 14px;
  height: 14px;
  background: #fff;
}

.transport,
.row2,
.speeds {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  flex-wrap: wrap;
}

#play {
  min-width: 72px;
  background: #fff;
  color: #000;
  font-weight: 650;
}

.speeds button { min-width: 44px; padding: 6px 8px; }

.speeds button.on { background: var(--accent); color: #fff; }

#jump { background: var(--danger); color: #fff; }

select {
  color-scheme: dark;
  background: var(--fill);
  border: 0;
  border-radius: 8px;
  min-height: 40px;
  max-width: 46vw;
  padding: 6px 8px;
}

#meta {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.4;
  text-align: center;
}

#meta a { color: #64b5ff; }

#backdrop {
  position: fixed;
  inset: 0;
  z-index: 25;
  background: rgba(0, 0, 0, 0.45);
}

#drop {
  position: fixed;
  inset: 12px;
  z-index: 50;
  display: grid;
  place-items: center;
  border: 2px dashed var(--accent);
  border-radius: 16px;
  background: rgba(10, 132, 255, 0.15);
  font-size: 20px;
  pointer-events: none;
}

.file-input {
  position: fixed;
  left: 0;
  top: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
}

@media (hover: none) {
  .desktop-only { display: none; }
}

@media (max-width: 860px) {
  #app { grid-template-columns: minmax(0, 1fr); }

  #btn-events { display: inline-block; }

  aside {
    position: fixed;
    z-index: 30;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(320px, 88vw);
    transform: translateX(-105%);
    transition: transform 0.2s ease;
    padding-left: env(safe-area-inset-left);
  }

  aside.open { transform: none; }

  }

@media (max-height: 480px) {
  #meta { display: none; }

  header { min-height: 40px; }

  footer { gap: 4px; padding-top: 4px; }
}

@media (prefers-reduced-motion: reduce) {
  aside { transition: none; }
}
