:root {
  --bg: #f2f2f7;
  --bg-card: #ffffff;
  --text: #1c1c1e;
  --text-secondary: #8e8e93;
  --separator: #c6c6c8;
  --accent: #007aff;
  --accent-bg: rgba(0, 122, 255, 0.14);
  --red: #ff3b30;
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --bg-card: #1c1c1e;
    --text: #f2f2f7;
    --separator: #38383a;
    --accent: #0a84ff;
    --accent-bg: rgba(10, 132, 255, 0.22);
    --red: #ff453a;
  }
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Helvetica Neue', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
}

header {
  border-bottom: 0.5px solid var(--separator);
  padding: 8px 0 4px;
  position: sticky;
  top: 0;
  z-index: 10;
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
}

.header-content, main {
  max-width: 760px;
  margin: 0 auto;
  padding-left: 16px;
  padding-right: 16px;
}

header h1 { font-size: 1.5rem; font-weight: 700; letter-spacing: -0.02em; }

main { padding-top: 12px; padding-bottom: 32px; display: flex; flex-direction: column; gap: 14px; }
[hidden] { display: none !important; }

.card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.card-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
h2 { font-size: 1rem; font-weight: 600; }
.row { display: flex; gap: 8px; align-items: center; }
.row.tight { gap: 6px; }
#load-form { flex-direction: row; }

input[type="text"] {
  flex: 1;
  min-width: 0;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 0.5px solid var(--separator);
  border-radius: 8px;
  padding: 10px 12px;
}
input[type="text"]:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

button {
  font: inherit;
  font-size: 0.9rem;
  color: var(--accent);
  background: var(--accent-bg);
  border: none;
  border-radius: 8px;
  padding: 8px 12px;
  cursor: pointer;
}
button:active { opacity: 0.6; }
button.primary { background: var(--accent); color: #fff; }
button.link { background: none; padding: 4px 0; font-size: 0.8rem; text-align: left; }

.player-wrap { position: relative; width: 100%; aspect-ratio: 16 / 9; background: #000; border-radius: var(--radius); overflow: hidden; }
.player-wrap iframe { position: absolute; inset: 0; width: 100%; height: 100%; }

#timeline {
  width: 100%;
  height: 96px;
  display: block;
  border-radius: 8px;
  background: var(--bg);
  touch-action: none;
  cursor: crosshair;
  user-select: none;
}

.hint { font-size: 0.75rem; color: var(--text-secondary); }

.range-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.range-field { display: flex; flex-direction: column; gap: 6px; }
.range-field label { font-size: 0.8rem; color: var(--text-secondary); }
.range-field input { font-variant-numeric: tabular-nums; }
.nudge { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; }
.nudge button { padding: 6px 0; font-variant-numeric: tabular-nums; }

.transport { display: flex; justify-content: center; gap: 12px; }
button.big { font-size: 1.6rem; line-height: 1; width: 64px; height: 52px; padding: 0; }

.toggle { display: flex; align-items: center; gap: 8px; justify-content: center; font-size: 0.9rem; }
kbd { font: inherit; font-size: 0.7rem; color: var(--text-secondary); border: 0.5px solid var(--separator); border-radius: 4px; padding: 0 4px; }

#rate { width: 100%; accent-color: var(--accent); }
#rate-label { font-weight: 600; font-variant-numeric: tabular-nums; }
.presets { flex-wrap: wrap; }
.presets button.active { background: var(--accent); color: #fff; }

.error { color: var(--red); font-size: 0.9rem; padding: 0 4px; }

@media (max-width: 480px) {
  .range-grid { grid-template-columns: 1fr; }
  button.big { width: 56px; }
}

.history { list-style: none; display: flex; flex-direction: column; gap: 6px; }
.history li { display: flex; gap: 6px; align-items: stretch; }
.history .entry { flex: 1; min-width: 0; text-align: left; display: flex; flex-direction: column; gap: 2px; background: var(--bg); color: var(--text); }
.history .entry small { color: var(--text-secondary); font-variant-numeric: tabular-nums; }
.history .entry span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history .del { color: var(--red); background: var(--bg); width: 40px; padding: 0; }

.tools { gap: 8px; }
.tools button { flex: 1; }
.player-wrap:fullscreen { border-radius: 0; aspect-ratio: auto; }
.tools kbd, button kbd { color: inherit; opacity: 0.7; }

.grid-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.presets .hint { margin-right: 4px; }
button:disabled { opacity: 0.4; cursor: default; }
.history .entry.active { outline: 2px solid var(--accent); outline-offset: -2px; }

@media (max-width: 640px) {
  .transport-card {
    position: sticky;
    bottom: 8px;
    z-index: 5;
    box-shadow: 0 2px 14px rgba(0, 0, 0, 0.3);
    padding-bottom: calc(14px + env(safe-area-inset-bottom));
  }
  main { padding-bottom: calc(24px + env(safe-area-inset-bottom)); }
}

@media (pointer: coarse) {
  button { min-height: 40px; }
  .nudge button { min-height: 44px; }
  #timeline { height: 128px; }
  input[type="text"] { font-size: 16px; } /* stops iOS zooming into focused fields */
}

.range-field { min-width: 0; }
.range-field input { width: 100%; min-width: 0; }

/* ---------- layout ---------- */
.layout { display: flex; flex-direction: column; gap: 14px; }
/* on narrow screens the wrappers dissolve so cards can be ordered freely and the
   sticky transport bar keeps working across the whole page */
.col-main, .col-side, #side-tools, .controls-row { display: contents; }

@media (max-width: 959px) {
  .player-wrap { order: 1; }
  .tools { order: 2; }
  #section-card { order: 3; }
  .transport-card { order: 4; }
  #speed-card { order: 5; }
  #sections-card { order: 6; }
  #tempo-card { order: 7; }
  #library-card { order: 8; }
}

@media (min-width: 960px) {
  .header-content, main { max-width: 1280px; }
  .layout { display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; align-items: start; }
  .layout:not(.empty) { grid-template-columns: minmax(0, 1fr) 380px; }
  .layout.empty { max-width: 720px; width: 100%; margin: 0 auto; }
  .col-main, .col-side, #side-tools { display: flex; flex-direction: column; gap: 14px; min-width: 0; }
  .controls-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
  .controls-row .card { justify-content: center; }
  .col-side { position: sticky; top: 56px; max-height: calc(100dvh - 72px); overflow-y: auto; padding: 2px; }
  #timeline { height: 140px; }
  .player-wrap { border-radius: 12px; }
}

/* ---------- library ---------- */
.library > li { flex-direction: column; gap: 6px; }
.library .vrow { display: flex; gap: 6px; align-items: stretch; }
.library .vopen {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px; text-align: left;
  background: var(--bg); color: var(--text); padding: 6px;
}
.library .vopen img { width: 64px; height: 36px; object-fit: cover; border-radius: 4px; flex: none; background: #000; }
.library .vopen .vtext { min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.library .vopen .vtitle { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 0.85rem; }
.library .vopen small { color: var(--text-secondary); }
.library .vopen.current { outline: 2px solid var(--accent); outline-offset: -2px; }
.library .secs { list-style: none; margin-left: 74px; display: flex; flex-direction: column; gap: 4px; }
.library .secs .entry { padding: 4px 8px; font-size: 0.8rem; flex-direction: row; justify-content: space-between; gap: 8px; }
.library .secs .del { width: 32px; }
#sec-update { align-self: flex-start; }

@media (min-width: 960px) {
  .player-wrap { width: min(100%, calc(46vh * 16 / 9)); margin-inline: auto; }
  .tools { justify-content: flex-start; }
  .tools button { flex: none; }
}

@media (max-width: 640px) {
  /* compact single-row transport so the sticky bar leaves room for content */
  .transport-card { flex-direction: row; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); }
  .transport-card .transport { gap: 6px; }
  .transport-card button.big { width: 46px; height: 44px; font-size: 1.3rem; }
  .transport-card .toggle { font-size: 0.85rem; }
  .transport-card .toggle kbd { display: none; }
}

@media (max-width: 640px) {
  /* truly pinned (sticky would scroll away once you pass it) */
  .transport-card {
    position: fixed;
    left: 12px;
    right: 12px;
    bottom: calc(8px + env(safe-area-inset-bottom));
    z-index: 20;
    padding: 8px 10px;
    box-shadow: 0 2px 14px rgba(0, 0, 0, 0.35);
  }
  main { padding-bottom: calc(96px + env(safe-area-inset-bottom)); }
}

/* ---------- loop switch ---------- */
.loop-switch {
  display: inline-flex; align-items: center; gap: 10px; align-self: center;
  padding: 8px 14px; border-radius: 999px; cursor: pointer; user-select: none;
  font-weight: 600; font-size: 0.95rem;
  background: var(--bg); color: var(--text-secondary);
  border: 2px solid var(--separator);
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.loop-switch input { position: absolute; opacity: 0; pointer-events: none; }
.loop-switch .track { position: relative; width: 38px; height: 22px; border-radius: 11px; background: var(--separator); flex: none; transition: background 0.15s; }
.loop-switch .knob { position: absolute; top: 2px; left: 2px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: transform 0.15s; }
.loop-switch .state { font-size: 0.7rem; letter-spacing: 0.06em; opacity: 0.85; min-width: 2.2em; }
.loop-switch:has(input:checked) { background: var(--accent); border-color: var(--accent); color: #fff; }
.loop-switch:has(input:checked) .track { background: rgba(255, 255, 255, 0.35); }
.loop-switch:has(input:checked) .knob { transform: translateX(16px); }
.loop-switch:has(input:focus-visible) { outline: 3px solid var(--accent-bg); outline-offset: 2px; }
.loop-switch kbd { color: inherit; opacity: 0.7; border-color: currentColor; }
.loop-switch.flash { animation: loop-flash 0.9s ease-out 2; }
@keyframes loop-flash {
  0% { box-shadow: 0 0 0 0 var(--red); }
  100% { box-shadow: 0 0 0 12px transparent; }
}

.tools .loop-switch { margin-left: auto; }

@media (max-width: 640px) {
  .transport-card { justify-content: center; }
  .tools { gap: 8px; }
  .tools .loop-switch { padding: 6px 12px; }
}

.notice { color: var(--accent); font-size: 0.9rem; padding: 0 4px; }
#share.done { background: var(--accent); color: #fff; }

/* ---------- chapters ---------- */
.chapters .entry { flex-direction: row; justify-content: space-between; align-items: center; gap: 8px; padding: 6px 10px; }
.chapters .entry.active { outline: 2px solid var(--accent); outline-offset: -2px; }
#chapters { max-height: 260px; overflow-y: auto; }
@media (max-width: 959px) {
  #chapters-card { order: 6; }
  #sections-card { order: 7; }
  #tempo-card { order: 8; }
  #library-card { order: 9; }
}
