:root {
  color-scheme: dark;
  --bg: #000;
  --panel: #080808;
  --panel2: #0d0d0d;
  --line: #252525;
  --muted: #7b7b7b;
  --text: #f5f5f5;
  --accent: #fff;
  --accentSoft: rgba(255,255,255,.12);
  --beat: rgba(255,255,255,.34);
  --beatMajor: rgba(255,255,255,.72);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html, body { width: 100%; height: 100%; margin: 0; background: var(--bg); color: var(--text); }
body { overflow: hidden; overscroll-behavior: none; touch-action: manipulation; }
button, input, label { font: inherit; }
button, label { -webkit-tap-highlight-color: transparent; }

.app {
  width: min(100%, 920px);
  height: 100dvh;
  margin: 0 auto;
  padding: max(8px, env(safe-area-inset-top)) 10px max(8px, env(safe-area-inset-bottom));
  display: grid;
  grid-template-rows: 42px minmax(122px, 1.2fr) 82px minmax(132px, 1fr) auto;
  gap: 7px;
  overflow: hidden;
}

.topbar {
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.topbar h1 { margin: 0; font-size: 15px; line-height: 1; letter-spacing: .13em; }
.status { display: block; margin-top: 5px; color: var(--muted); font-size: 9px; font-weight: 750; letter-spacing: .08em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 52vw; }
.fileButtons { display: flex; gap: 6px; }
.fileButton {
  height: 32px;
  min-width: 57px;
  padding: 0 11px;
  display: grid;
  place-items: center;
  border: 1px solid #333;
  border-radius: 10px;
  background: #0b0b0b;
  color: #ddd;
  font-size: 10px;
  font-weight: 850;
  letter-spacing: .08em;
  cursor: pointer;
}
.fileButton:active { transform: scale(.97); background: #151515; }

.previewCard, .gifTimelineCard, .audioCard, .controlsCard {
  min-height: 0;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  overflow: hidden;
}

.previewCard { position: relative; display: grid; grid-template-rows: minmax(0, 1fr) 24px; }
.previewStage { min-height: 0; position: relative; display: grid; place-items: center; overflow: hidden; background: #030303; }
.previewStage.empty { background: radial-gradient(circle at center, #101010 0, #050505 52%, #000 100%); }
#gifCanvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  margin: auto;
  object-fit: contain;
  object-position: center;
  image-rendering: auto;
}
.emptyHint { color: #2e2e2e; font-weight: 900; letter-spacing: .22em; font-size: 18px; }
.previewMeta { border-top: 1px solid #171717; padding: 0 9px; display: flex; align-items: center; justify-content: space-between; gap: 12px; color: #8c8c8c; font-size: 9px; font-weight: 760; letter-spacing: .05em; }
.previewMeta span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.gifTimelineCard { padding: 7px 8px 8px; }
.sectionHead, .audioHead { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.sectionHead { height: 16px; color: #929292; font-size: 9px; font-weight: 850; letter-spacing: .09em; }
.sectionHead strong { color: #d0d0d0; font-size: 9px; letter-spacing: .03em; }
.gifTimelineWrap { position: relative; height: 49px; margin-top: 2px; border-radius: 9px; overflow: hidden; background: #020202; touch-action: none; }
.gifTimelineWrap.disabled { opacity: .28; }
#gifTimeline { width: 100%; height: 100%; display: block; }
.gifShade { position: absolute; inset-block: 0; background: rgba(0,0,0,.68); pointer-events: none; }
.gifHandle { position: absolute; top: 0; bottom: 0; width: 28px; border: 0; padding: 0; background: transparent; touch-action: none; cursor: ew-resize; }
.gifHandle.start { transform: none; }
.gifHandle.end { transform: translateX(-100%); }
.gifHandle::before { content: ""; position: absolute; top: 0; bottom: 0; width: 2px; background: #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.6); }
.gifHandle.start::before { left: 0; }
.gifHandle.end::before { right: 0; }
.gifHandle::after { content: ""; position: absolute; top: 2px; width: 11px; height: 12px; border-radius: 3px; background: #fff; }
.gifHandle.start::after { left: 0; }
.gifHandle.end::after { right: 0; top: auto; bottom: 2px; }

.audioCard { padding: 7px 8px 8px; display: grid; grid-template-rows: 30px minmax(0,1fr) 28px; }
.audioHead { min-width: 0; }
.audioHead > div:first-child { min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.sectionLabel { color: #8f8f8f; font-size: 9px; font-weight: 850; letter-spacing: .09em; }
#audioMeta { min-width: 0; max-width: 70vw; color: #d7d7d7; font-size: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bpmBox { display: flex; align-items: baseline; gap: 4px; flex: none; }
.bpmBox strong { font-size: 20px; line-height: 1; letter-spacing: -.03em; }
.bpmBox span { color: #858585; font-size: 8px; font-weight: 800; }
.waveShell { min-height: 0; position: relative; border-radius: 8px; background: #030303; overflow: hidden; touch-action: pan-x; }
.waveShell.disabled { opacity: .28; }
#waveform { width: 100%; height: 100%; }
#waveform ::part(scroll) { scrollbar-width: none; }
#waveform ::part(scroll)::-webkit-scrollbar { display: none; }
#waveform ::part(cursor) { width: 1px; background: rgba(255,255,255,.95); }
#waveform ::part(region-handle) {
  width: 32px !important;
  touch-action: none !important;
  cursor: ew-resize;
  z-index: 8 !important;
}
#waveform ::part(region-handle-left) {
  border-left: 4px solid #fff !important;
  border-radius: 4px 0 0 4px !important;
  background: linear-gradient(90deg, rgba(255,255,255,.32), rgba(255,255,255,0) 80%) !important;
}
#waveform ::part(region-handle-right) {
  border-right: 4px solid #fff !important;
  border-radius: 0 4px 4px 0 !important;
  background: linear-gradient(270deg, rgba(255,255,255,.32), rgba(255,255,255,0) 80%) !important;
}
.waveTools { display: grid; grid-template-columns: 31px minmax(0, 1fr) auto; align-items: end; gap: 8px; min-width: 0; }
.playButton { width: 29px; height: 26px; border: 1px solid #333; border-radius: 8px; background: #101010; color: #fff; font-size: 12px; }
.playButton:disabled { opacity: .3; }
.zoomGroup { min-width: 0; display: grid; grid-template-columns: auto minmax(0,1fr); gap: 6px; align-items: center; }
.zoomGroup span { color: #777; font-size: 8px; font-weight: 850; }
#audioSelectionLabel { color: #bbb; font-size: 9px; white-space: nowrap; }

.controlsCard { padding: 8px; display: grid; gap: 5px; }
.controlRow { min-width: 0; position: relative; display: grid; grid-template-columns: 68px minmax(0,1fr); gap: 8px; align-items: center; }
.controlName { min-width: 0; display: flex; align-items: baseline; justify-content: space-between; gap: 4px; }
.controlName span { color: #858585; font-size: 9px; font-weight: 850; letter-spacing: .08em; }
.controlName strong { color: #eee; font-size: 10px; font-weight: 850; white-space: nowrap; }
.controlName.compact { grid-column: 1 / -1; }
.gridRow { grid-template-columns: 68px 32px minmax(0,1fr) 32px; }
.nudge { height: 25px; border: 1px solid #2d2d2d; border-radius: 7px; background: #0e0e0e; color: #bbb; font-size: 9px; font-weight: 850; }
.nudge:disabled { opacity: .3; }
.edgeValue { position: absolute; left: 76px; top: -1px; color: #4d4d4d; font-size: 7px; pointer-events: none; transform: translateY(-100%); }
.edgeValue.right { left: auto; right: 1px; }

input[type="range"] {
  width: 100%;
  height: 24px;
  margin: 0;
  appearance: none;
  -webkit-appearance: none;
  background: transparent;
  touch-action: pan-y;
}
input[type="range"]::-webkit-slider-runnable-track { height: 4px; border-radius: 999px; background: #2c2c2c; }
input[type="range"]::-webkit-slider-thumb { appearance: none; -webkit-appearance: none; width: 21px; height: 21px; margin-top: -8.5px; border-radius: 7px; border: 1px solid #111; background: #f5f5f5; box-shadow: 0 0 0 2px rgba(255,255,255,.05); }
input[type="range"]::-moz-range-track { height: 4px; border-radius: 999px; background: #2c2c2c; }
input[type="range"]::-moz-range-thumb { width: 21px; height: 21px; border: 1px solid #111; border-radius: 7px; background: #f5f5f5; }
input[type="range"]:disabled { opacity: .28; }

.exportRow { min-width: 0; padding-top: 4px; border-top: 1px solid #1a1a1a; display: grid; grid-template-columns: minmax(0,1fr) auto 64px; gap: 8px; align-items: end; }
.beatsControl { min-width: 0; display: grid; grid-template-columns: 1fr; }
.outputMeta { padding-bottom: 3px; display: flex; flex-direction: column; align-items: flex-end; gap: 2px; color: #aaa; font-size: 9px; font-weight: 800; white-space: nowrap; }
#exportProgress { color: #666; font-size: 8px; min-height: 10px; }
.exportButton { height: 35px; border: 1px solid #eee; border-radius: 10px; background: #f5f5f5; color: #050505; font-size: 11px; font-weight: 950; letter-spacing: .08em; }
.exportButton:disabled { border-color: #303030; background: #151515; color: #555; }
.exportButton.busy { background: #111; color: #fff; border-color: #444; }

@media (pointer: coarse) {
  #waveform ::part(region-handle) { width: 48px !important; }
  #waveform ::part(region-handle-left) { border-left-width: 5px !important; }
  #waveform ::part(region-handle-right) { border-right-width: 5px !important; }
}

@media (max-height: 690px) {
  .app { grid-template-rows: 40px minmax(102px, 1fr) 70px minmax(112px, .92fr) auto; gap: 5px; padding-top: max(5px, env(safe-area-inset-top)); padding-bottom: max(5px, env(safe-area-inset-bottom)); }
  .gifTimelineWrap { height: 39px; }
  .gifTimelineCard { padding-top: 5px; padding-bottom: 5px; }
  .controlsCard { padding: 6px 8px; gap: 2px; }
  .controlRow { min-height: 24px; }
  .exportButton { height: 31px; }
}

@media (min-width: 700px) and (min-height: 650px) {
  .app { grid-template-columns: minmax(240px,.82fr) minmax(360px,1.18fr); grid-template-rows: 42px minmax(150px,1fr) 96px auto; }
  .topbar { grid-column: 1 / -1; }
  .previewCard { grid-column: 1; grid-row: 2 / 4; }
  .gifTimelineCard { grid-column: 1; grid-row: 4; }
  .audioCard { grid-column: 2; grid-row: 2 / 4; }
  .controlsCard { grid-column: 2; grid-row: 4; }
  #audioMeta { max-width: 36vw; }
}
