:root {
  --bg: #12141d;
  --panel: #1b1e29;
  --border: #2a3040;
  --text: #e8eaf0;
  --muted: #8b93a8;
  --accent: #4f9eff;
  --accent2: #f5a742;
  --success: #3ecf8e;
  --danger: #e05858;
  --timeline-label-w: 160px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, Segoe UI, Roboto, sans-serif;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 640px;
}

.header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.brand {
  font-weight: 700;
  letter-spacing: 0.03em;
  white-space: nowrap;
}

.menu {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.menu button,
.tool-btn,
.transport button,
.layer-actions button {
  background: #252836;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
}

.menu button:hover,
.tool-btn:hover,
.transport button:hover,
.layer-actions button:hover {
  border-color: var(--muted);
}

.header-spacer {
  flex: 1;
}

.confirm-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 8px 12px;
  background: rgba(224, 88, 88, 0.12);
  border-bottom: 1px solid rgba(224, 88, 88, 0.35);
  font-size: 0.88rem;
}

.confirm-bar.hidden {
  display: none;
}

.confirm-bar-text {
  color: var(--text);
  max-width: 52rem;
}

.confirm-bar-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.confirm-bar-actions button {
  background: #252836;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
}

.confirm-bar-actions button:hover {
  border-color: var(--muted);
}

.btn-danger {
  border-color: var(--danger);
  background: rgba(224, 88, 88, 0.2);
}

.btn-danger:hover {
  background: rgba(224, 88, 88, 0.35);
}

.resample-block {
  margin-bottom: 8px;
}

.resample-caption {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
  margin-bottom: 6px;
}

.resample-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.resample-row input[type='number'] {
  width: 56px;
  background: #252836;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 4px;
  padding: 6px;
}

.resample-mul {
  color: var(--muted);
  font-size: 0.9rem;
}

.resample-row button {
  flex: 1;
  min-width: 72px;
  padding: 6px;
  background: #252836;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
}

.resample-row button:hover:not(:disabled) {
  border-color: var(--muted);
}

.resample-row button:disabled,
.resample-row input:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.workspace {
  flex: 1;
  display: grid;
  grid-template-columns: 172px minmax(0, 1fr) 220px;
  min-height: 0;
}

.tool-panel,
.layer-panel {
  background: var(--panel);
  border-color: var(--border);
  border-style: solid;
  border-width: 0;
  overflow: auto;
  padding: 10px;
}

.tool-panel {
  border-right-width: 1px;
}

.layer-panel {
  border-left-width: 1px;
}

.panel-title {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--muted);
  margin-bottom: 10px;
}

.tool-btn {
  display: block;
  width: 100%;
  text-align: left;
  margin-bottom: 8px;
}

.tool-btn.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

.mesh-step-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.mesh-step-label {
  font-size: 0.8rem;
  color: var(--muted);
}

.tool-mini {
  min-width: 36px;
  padding: 6px;
  flex: 1;
  background: #252836;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  font-weight: 700;
}

.tool-mini:hover {
  border-color: var(--muted);
}

.task-panel .crop-help {
  font-size: 0.82rem;
  color: var(--muted);
  margin: 10px 0;
  line-height: 1.5;
}

.crop-stage {
  touch-action: none;
  cursor: crosshair;
  border: 1px solid var(--border);
  border-radius: 8px;
  display: block;
  max-width: 100%;
  height: auto;
  background: #0a0c12;
}

.crop-inset-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 10px;
}

.crop-inset-row label {
  font-size: 0.8rem;
}

.divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 12px 0;
}

.row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
  margin-bottom: 8px;
}

.canvas-wrap {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  padding: 8px;
}

.modal-root {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(24px, env(safe-area-inset-top))
    max(24px, env(safe-area-inset-right))
    max(24px, env(safe-area-inset-bottom))
    max(24px, env(safe-area-inset-left));
  /* Dim the app behind — keep this translucent */
  background: rgba(10, 12, 18, 0.58);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  overflow: auto;
}

.modal-root.hidden {
  display: none;
}

.modal-root .task-panel {
  margin: auto;
  flex-shrink: 0;
  max-height: calc(100vh - 48px);
  overflow-y: auto;
  overflow-x: hidden;
  /* Frosted sheet: see hint of canvas through the dialog */
  background: rgba(24, 27, 38, 0.82);
  backdrop-filter: blur(16px) saturate(1.15);
  -webkit-backdrop-filter: blur(16px) saturate(1.15);
  border: 1px solid rgba(255, 255, 255, 0.12);
  box-shadow:
    0 0 0 1px rgba(0, 0, 0, 0.35),
    0 20px 64px rgba(0, 0, 0, 0.55);
}

.canvas-stack {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.canvas-hud-top,
.canvas-hud-bottom {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-size: 0.85rem;
  color: var(--muted);
}

#view {
  display: block;
  width: 100%;
  max-height: calc(100vh - 260px);
  flex: 1 1 auto;
  min-height: 200px;
  background:
    repeating-linear-gradient(135deg, #141821 0 8px, #181c27 8px 16px),
    radial-gradient(ellipse at center, #0e1018, #090b10);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: crosshair;
  outline: none;
}

.zoom-label {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
}

#zoom-range {
  width: 140px;
}

.timeline-strip {
  background: var(--panel);
  border-top: 1px solid var(--border);
  padding: 8px;
}

.transport {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}

.transport-keys {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  padding-left: 10px;
  margin-left: 4px;
  border-left: 1px solid var(--border);
}

.transport .play {
  min-width: 72px;
  font-weight: 600;
}

/* Fit view — plain sidebar button matches theme */
.tool-panel > button:not(.tool-btn):not(.tool-mini) {
  background: #252836;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  font: inherit;
  display: block;
  width: 100%;
  text-align: left;
  margin-bottom: 8px;
}

.tool-panel > button:not(.tool-btn):not(.tool-mini):hover {
  border-color: var(--muted);
}

.inline {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85rem;
  color: var(--muted);
}

.inline input {
  width: 72px;
  background: #252836;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 4px;
  padding: 4px;
}

.timeline-scroll {
  overflow-x: auto;
  overflow-y: hidden;
  border: 1px solid var(--border);
  border-radius: 6px;
}

.timeline-ruler-row {
  display: flex;
  align-items: stretch;
  min-height: 32px;
  border-bottom: 1px solid var(--border);
  background: #151823;
}

.timeline-gutter {
  flex: 0 0 var(--timeline-label-w);
  min-width: var(--timeline-label-w);
  max-width: var(--timeline-label-w);
  position: sticky;
  left: 0;
  z-index: 3;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 6px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  background: linear-gradient(to right, #1b1f2c 88%, transparent);
  border-right: 1px solid var(--border);
  user-select: none;
}

.ruler-strip-host {
  position: relative;
  flex: 0 0 auto;
  cursor: pointer;
}

.ruler {
  position: relative;
  height: 32px;
  background: linear-gradient(to bottom, #222636, #1a1f2c);
  box-sizing: border-box;
  border-bottom: 1px solid transparent;
}

.timeline-tracks {
  position: relative;
  min-height: 120px;
  background: #151823;
}

.ruler-frametick {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 1px;
  margin-left: -0.5px;
  background: rgba(90, 100, 130, 0.35);
  pointer-events: none;
}

.ruler-frametick.major {
  background: rgba(122, 132, 168, 0.65);
}

.ruler-label {
  position: absolute;
  top: 3px;
  font-size: 10px;
  line-height: 1;
  color: #b4bcd0;
  transform: translateX(-50%);
  text-align: center;
  white-space: nowrap;
  pointer-events: none;
  font-variant-numeric: tabular-nums;
}

.ruler-segment {
  position: absolute;
  top: 20px;
  height: 8px;
  border-radius: 2px;
  opacity: 0.55;
  pointer-events: none;
}

.ruler-segment.smooth {
  background: var(--accent);
}

.ruler-segment.hold {
  background: var(--accent2);
}

.ruler-arrow {
  position: absolute;
  top: 18px;
  font-size: 10px;
  color: var(--text);
  transform: translateX(-50%);
  opacity: 0.75;
}

.ruler-current {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 2px;
  margin-left: -1px;
  background: var(--danger);
  z-index: 2;
  pointer-events: none;
}

.track-row {
  position: relative;
  height: 40px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
}

.track-label {
  position: sticky;
  left: 0;
  z-index: 1;
  min-width: var(--timeline-label-w);
  max-width: var(--timeline-label-w);
  height: 100%;
  padding: 0 8px;
  display: flex;
  align-items: center;
  gap: 6px;
  background: linear-gradient(to right, #1b1f2c 90%, transparent);
}

.track-label input[type="checkbox"] {
  accent-color: var(--accent);
}

.track-strip {
  position: relative;
  flex: 1;
  height: 100%;
}

.track-key {
  position: absolute;
  top: 50%;
  width: 10px;
  height: 10px;
  background: var(--accent);
  border: 1px solid #fff4;
  transform: translate(-50%, -50%) rotate(45deg);
  cursor: pointer;
}

.track-key.hold-end {
  background: var(--accent2);
}

.layer-list-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px;
  border-radius: 6px;
  border: 1px solid transparent;
  cursor: pointer;
  margin-bottom: 4px;
}

.layer-list-item.active {
  border-color: var(--accent);
  background: rgba(79, 158, 255, 0.12);
}

.layer-list-item.hidden-item {
  opacity: 0.45;
}

.layer-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-top: 8px;
}

.layer-z-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 10px;
  font-size: 0.8rem;
  color: var(--muted);
}

.layer-z-label span {
  white-space: nowrap;
}

.layer-z-label input {
  width: 96px;
  min-width: 0;
  flex: 1;
  background: #252836;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 4px;
  padding: 6px 8px;
  font: inherit;
}

.layer-actions button:last-child:nth-child(odd) {
  grid-column: span 2;
}

.task-panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  min-width: 280px;
  max-width: 92vw;
  color: var(--text);
}

.task-panel > h2:first-child {
  margin: 0 0 12px;
  font-size: 1rem;
}

.task-panel .actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 14px;
  flex-wrap: wrap;
}

.task-panel .actions button {
  background: rgba(37, 40, 54, 0.95);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  font: inherit;
  font-size: 0.88rem;
  transition:
    border-color 0.15s ease,
    background 0.15s ease,
    box-shadow 0.15s ease;
}

.task-panel .actions button:hover {
  border-color: rgba(255, 255, 255, 0.22);
  background: rgba(44, 48, 64, 0.98);
}

.task-panel .actions button:active {
  transform: translateY(1px);
}

/* Primary action (Import / Apply crop) — last in row */
.modal-root .task-panel .actions button:last-child {
  background: rgba(79, 158, 255, 0.22);
  border-color: rgba(79, 158, 255, 0.55);
  color: #e8f2ff;
}

.modal-root .task-panel .actions button:last-child:hover {
  background: rgba(79, 158, 255, 0.32);
  border-color: rgba(79, 158, 255, 0.75);
  box-shadow: 0 0 0 1px rgba(79, 158, 255, 0.2);
}

.task-panel label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 8px;
}

.task-panel .crop-inset-row input {
  background: rgba(18, 20, 30, 0.75);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--text);
  border-radius: 6px;
  padding: 8px;
  font: inherit;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.modal-root .task-panel .crop-inset-row input:focus {
  outline: none;
  border-color: rgba(79, 158, 255, 0.55);
  box-shadow: 0 0 0 2px rgba(79, 158, 255, 0.15);
}

.toast {
  position: fixed;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  background: #2a3144;
  border: 1px solid var(--border);
  padding: 10px 16px;
  border-radius: 8px;
  z-index: 120;
  font-size: 0.9rem;
}

.toast.hidden {
  display: none;
}

@media (max-width: 1100px) {
  .workspace {
    grid-template-columns: 150px minmax(0, 1fr);
  }

  .layer-panel {
    grid-column: 1 / -1;
    border-left-width: 0;
    border-top: 1px solid var(--border);
  }
}
