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

:root {
  --bg: #111111;
  --panel: #1c1c1e;
  --text: #f2f2f7;
  --dim: #8e8e93;
  --accent: #0a84ff;
  --border: #2c2c2e;
}

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

.app {
  max-width: none;
  margin: 0;
  padding: 0 20px calc(24px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 4px 6px;
}

h1 { font-size: 17px; font-weight: 600; }

.btn {
  display: inline-block;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 9px 14px;
  font-size: 14px;
  cursor: pointer;
  user-select: none;
}
.btn:active { opacity: 0.7; }
.btn.primary { background: var(--accent); border-color: var(--accent); }
.dim-btn { margin-top: 8px; }

.dropzone {
  border: 2px dashed var(--border);
  border-radius: 14px;
  padding: 60px 20px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.dim { color: var(--dim); font-size: 13px; }

.filmstrip {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 4px 2px;
}

.thumb {
  position: relative;
  flex: 0 0 64px;
  height: 64px;
  border-radius: 8px;
  overflow: hidden;
  border: 2px solid transparent;
  cursor: pointer;
}
.thumb.selected { border-color: var(--accent); }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb .badge {
  position: absolute;
  right: 3px;
  bottom: 3px;
  font-size: 10px;
  background: rgba(0,0,0,0.6);
  border-radius: 4px;
  padding: 1px 4px;
}
.thumb .badge.thumb-edited {
  left: 3px;
  right: auto;
  top: 3px;
  bottom: auto;
  color: var(--accent);
  pointer-events: none;
}
.thumb-star {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 18px;
  height: 18px;
  border: none;
  border-radius: 4px;
  background: rgba(0,0,0,0.6);
  color: #f5c542;
  font-size: 11px;
  line-height: 1;
  padding: 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.editor { display: flex; flex-direction: column; gap: 10px; }

.controls { display: flex; flex-direction: column; gap: 10px; }

.preview { display: flex; justify-content: center; }

.canvas-wrap {
  position: relative;
  width: fit-content;
  max-width: 100%;
  margin: 0 auto;
}
/* Width is the largest value that fits both the viewport width and the
   viewport height given the image aspect ratio (--ar, set from Rust). */
.canvas-wrap canvas, .canvas-wrap video {
  display: block;
  width: min(calc(100vw - 40px), calc((100vh - 360px) * var(--ar, 1.5)));
  height: auto;
  max-width: calc(100vw - 40px);
  max-height: calc(100vh - 360px);
  border-radius: 10px;
  background: #000;
}
/* Cropped view: clip layer overlays that fall outside the crop region. */
.canvas-wrap.cropped { overflow: hidden; border-radius: 10px; }

.crop-overlay { position: absolute; inset: 0; touch-action: none; }

.crop-rect {
  position: absolute;
  border: 2px solid #fff;
  cursor: move;
  touch-action: none;
}

/* Dim outside the crop box with four plain rects — a giant box-shadow here
   repaints a 9999px region on every window-resize frame and lags the page. */
.crop-dim {
  position: absolute;
  background: rgba(0, 0, 0, 0.45);
  pointer-events: none;
}

.handle {
  position: absolute;
  width: 26px;
  height: 26px;
  background: #fff;
  border-radius: 50%;
  touch-action: none;
}
.handle.nw { left: -13px; top: -13px; cursor: nwse-resize; }
.handle.ne { right: -13px; top: -13px; cursor: nesw-resize; }
.handle.sw { left: -13px; bottom: -13px; cursor: nesw-resize; }
.handle.se { right: -13px; bottom: -13px; cursor: nwse-resize; }

.tabs {
  display: flex;
  gap: 6px;
  background: var(--panel);
  border-radius: 12px;
  padding: 4px;
}

.tab {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--dim);
  padding: 8px 4px;
  font-size: 14px;
  border-radius: 9px;
  cursor: pointer;
}
.tab.active { background: var(--border); color: var(--text); }

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

.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 999px;
  padding: 8px 16px;
  font-size: 14px;
  cursor: pointer;
}
.chip.active { background: var(--accent); border-color: var(--accent); }

.row { display: flex; gap: 10px; }

.slider { display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
.slider input[type="range"] { width: 100%; accent-color: var(--accent); }

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}
.overlay .card {
  background: var(--panel);
  border-radius: 14px;
  padding: 22px 26px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 240px;
  text-align: center;
}
.overlay progress { width: 100%; }

.layers-tab { display: flex; flex-direction: column; gap: 12px; }

.layer-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 180px;
  overflow-y: auto;
}

.layer-row {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  cursor: pointer;
  user-select: none;
  touch-action: manipulation;
}
.layer-row.selected { border-color: var(--accent); }

.layer-label {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
}

.icon-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 4px 6px;
  font-size: 12px;
  cursor: pointer;
  touch-action: manipulation;
}
.icon-btn.delete { color: #ff453a; border-color: #ff453a; }

.layer-row input[type="range"] {
  width: 60px;
  accent-color: var(--accent);
}

.text-editor {
  display: flex;
  flex-direction: column;
  gap: 10px;
  border-top: 1px solid var(--border);
  padding-top: 12px;
}
.text-editor input[type="text"] {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 14px;
}
.text-editor select {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 8px;
  font-size: 14px;
}
.text-editor .row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.text-editor label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
}
.text-editor .slider.compact { flex: 1; min-width: 80px; }
.text-editor input[type="color"] {
  width: 36px;
  height: 28px;
  border: none;
  background: transparent;
}
.font-upload {
  padding: 6px 10px;
  font-size: 13px;
}

.layer-add-row { flex-wrap: wrap; }
.layer-add-row .btn { flex: 1; min-width: 70px; }

.toolbar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  border-top: 1px solid var(--border);
  padding-top: 12px;
}

.path-editor, .brush-editor {
  display: flex;
  flex-direction: column;
  gap: 10px;
  border-top: 1px solid var(--border);
  padding-top: 12px;
}
.path-editor .row, .brush-editor .row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.path-editor label, .brush-editor label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
}
.path-editor input[type="color"], .brush-editor input[type="color"] {
  width: 36px;
  height: 28px;
  border: none;
  background: transparent;
}

.video-wrap { position: relative; }
.video-overlays {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}
.video-text-overlay {
  position: absolute;
  pointer-events: none;
  user-select: none;
}
.text-overlay.selected {
  pointer-events: auto;
  touch-action: none;
  cursor: grab;
  user-select: none;
  z-index: 10;
}
.text-overlay.selected:active {
  cursor: grabbing;
}
.text-overlay.selected::after {
  content: "";
  position: absolute;
  inset: -16px;
  z-index: -1;
}

.select-tab {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.select-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.select-row .btn {
  flex: 1;
  min-width: 80px;
}

button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.curves-canvas {
  width: 100%;
  aspect-ratio: 1;
  background: rgba(0, 0, 0, 0.35);
  border-radius: 8px;
  touch-action: none;
  cursor: crosshair;
}

.curves-info {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.6);
  min-height: 16px;
  font-variant-numeric: tabular-nums;
}

.curves-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 4px;
}

.grain-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 4px;
}

.heal-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.clone-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.blur-panel {
  margin-top: 4px;
}

@media (min-width: 900px) {
  .editor { flex-direction: row; align-items: flex-start; }
  .preview { flex: 1; min-width: 0; }
  /* Fill the space left of the 320px controls column, up to the viewport
     height minus header, filmstrip and panel overhead. */
  .canvas-wrap canvas, .canvas-wrap video {
    width: min(calc(100vw - 400px), calc((100vh - 230px) * var(--ar, 1.5)));
    max-width: calc(100vw - 400px);
    max-height: calc(100vh - 230px);
  }
  .controls {
    flex: 0 0 320px;
    position: sticky;
    top: 12px;
    max-height: calc(100vh - 24px);
    overflow-y: auto;
  }
  .tabs { display: grid; grid-template-columns: 1fr 1fr; }
}

.drive-panel { gap: 10px; }
.drive-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  background: none;
  border: none;
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  padding: 0;
}
.drive-files {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  gap: 8px;
}
.drive-file {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px;
  overflow: hidden;
  position: relative;
}
.drive-file.disabled { opacity: 0.4; cursor: default; }
.drive-file img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 6px; }
.drive-badge {
  position: absolute;
  top: 4px;
  left: 4px;
  background: var(--accent);
  color: #fff;
  font-size: 9px;
  font-weight: 600;
  padding: 1px 5px;
  border-radius: 4px;
  pointer-events: none;
}
.drive-icon { font-size: 28px; line-height: 1.4; }
.drive-name {
  font-size: 11px;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.drive-error { color: #e06c5c; }

.drive-crumbs {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-wrap: wrap;
  font-size: 12px;
}
.drive-crumb {
  background: none;
  border: none;
  color: var(--text);
  font-size: 12px;
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 4px;
}
.drive-crumb:hover { background: var(--border); }

.drive-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.drive-sort {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 12px;
  padding: 3px 6px;
}
.drive-size { width: 90px; accent-color: var(--accent); }

.drive-select,
.drive-star {
  position: absolute;
  top: 4px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: rgba(20, 20, 24, 0.75);
  color: #fff;
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.12s;
}
.drive-file:hover .drive-select,
.drive-file:hover .drive-star,
.drive-select.on,
.drive-star.on { opacity: 1; }
.drive-select { left: 4px; }
.drive-select.on { background: var(--accent); border-color: var(--accent); }
.drive-star { right: 4px; }
.drive-star.on { color: #f5c542; border-color: #f5c542; opacity: 1; }

.drive-hover {
  position: fixed;
  z-index: 70;
  pointer-events: none;
  background: #111;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px;
  box-shadow: 0 10px 34px rgba(0, 0, 0, 0.55);
}
.drive-hover img {
  width: 480px;
  height: 360px;
  object-fit: contain;
  display: block;
  border-radius: 5px;
  background: #000;
}
.drive-hover canvas {
  position: absolute;
  left: 4px;
  top: 4px;
  width: 480px;
  height: 360px;
  display: none;
  border-radius: 5px;
  background: #000;
}

.loupe-hover {
  position: fixed;
  z-index: 70;
  pointer-events: none;
  background: #111;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px;
  box-shadow: 0 10px 34px rgba(0, 0, 0, 0.55);
}
.loupe-hover canvas {
  width: 480px;
  height: 360px;
  display: block;
  border-radius: 5px;
  background: #000;
}
.loupe-tag {
  position: absolute;
  right: 10px;
  bottom: 10px;
  font-size: 11px;
  color: #fff;
  background: rgba(0, 0, 0, 0.55);
  padding: 1px 6px;
  border-radius: 4px;
}

.drive-folder { justify-content: center; }
.drive-folder .drive-icon { font-size: 34px; }

.loupe {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 60;
  padding: 24px;
}
.loupe-body {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 100%;
  max-height: 100%;
}
.loupe-img {
  max-width: min(92vw, 1100px);
  max-height: calc(100vh - 140px);
  object-fit: contain;
  border-radius: 8px;
  background: #111;
}
.loupe-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: space-between;
  flex-wrap: wrap;
}
.loupe-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1;
}
.loupe-info span:first-child {
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.loupe-star.on { color: #f5c542; border-color: #f5c542; }
.loupe-preload { display: none; }
