:root {
  color-scheme: dark;
  --bg: #000000;
  --bg2: #1c1c1e;
  --bg3: #2c2c2e;
  --bg4: #3a3a3c;
  --label: rgba(255, 255, 255, 1);
  --label2: rgba(255, 255, 255, 0.6);
  --label3: rgba(255, 255, 255, 0.3);
  --label4: rgba(255, 255, 255, 0.18);
  --blue: #0a84ff;
  --green: #30d158;
  --orange: #ff9f0a;
  --red: #ff453a;
  --amber: #ffd60a;
  --teal: #64d2ff;
  --purple: #bf5af2;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Display", Inter, "Noto Sans SC", sans-serif;
  --mono: "JetBrains Mono", "SF Mono", ui-monospace, Menlo, monospace;
  --spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --smooth: cubic-bezier(0.22, 1, 0.36, 1);
  --fade: cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--bg);
}

body {
  min-height: 100%;
  margin: 0;
  color: var(--label);
  font-family: var(--sans);
  background:
    radial-gradient(circle at top left, rgba(10, 132, 255, 0.18), transparent 32rem),
    radial-gradient(circle at top right, rgba(191, 90, 242, 0.12), transparent 28rem),
    var(--bg);
}

input,
select,
textarea {
  font: inherit;
}

button,
a {
  cursor: pointer;
}

input[type="file"] {
  cursor: pointer;
}

.check-field {
  align-content: end;
}

.check-field input {
  width: 22px;
  min-height: 22px;
  accent-color: var(--green);
}

.check-field > span {
  color: var(--label);
}

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

.app-shell {
  width: 100%;
  max-width: 1920px;
  min-height: 100vh;
  margin: 0 auto;
  padding: 24px 24px 40px;
}

.topbar {
  position: sticky;
  top: 16px;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 64px;
  margin-bottom: 20px;
  padding: 10px 14px;
}

.glass-panel {
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 18px;
  background: rgba(28, 28, 30, 0.72);
  backdrop-filter: blur(20px) saturate(200%);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.36);
}

.brand-block,
.topbar-meta,
.actions,
.key-row,
.ref-upload-row,
.panel-heading,
.task-title-row,
.image-title-row,
.image-actions {
  display: flex;
  align-items: center;
}

.brand-block {
  gap: 12px;
}

.brand-mark {
  display: grid;
  width: 42px;
  height: 42px;
  place-items: center;
  border-radius: 14px;
  color: var(--bg);
  font-family: var(--mono);
  font-weight: 800;
  background: linear-gradient(135deg, var(--amber), var(--teal));
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  display: flex;
  align-items: baseline;
  gap: 7px;
  font-size: 18px;
  letter-spacing: -0.02em;
}

.app-version {
  color: var(--label3);
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0;
}

h2 {
  font-size: 18px;
  letter-spacing: -0.02em;
}

.brand-block p,
.muted {
  color: var(--label2);
}

.topbar-meta {
  gap: 8px;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(300px, 380px) minmax(0, 1fr) minmax(300px, 360px);
  gap: 16px;
  align-items: start;
}

.card {
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 16px;
  background: rgba(28, 28, 30, 0.92);
}

.control-panel,
.section-card {
  padding: 18px;
}

.control-panel,
.task-panel {
  position: sticky;
  top: 96px;
}

.result-panel {
  display: grid;
  gap: 16px;
}

.panel-heading {
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.eyebrow {
  margin-bottom: 4px;
  color: var(--label3);
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.16em;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.span-full {
  grid-column: 1 / -1;
}

.field {
  display: grid;
  gap: 7px;
}

.field > span,
.field-heading label {
  color: var(--label2);
  font-size: 13px;
}

.field-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.api-key-link {
  color: var(--blue);
  font-size: 12px;
  text-decoration: none;
}

.api-key-link:hover {
  text-decoration: underline;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  outline: none;
  color: var(--label);
  background: rgba(0, 0, 0, 0.34);
  transition: border-color 150ms var(--fade), background 150ms var(--fade), box-shadow 150ms var(--fade);
}

input,
select {
  min-height: 40px;
  padding: 0 11px;
}

textarea {
  min-height: 172px;
  resize: vertical;
  padding: 11px;
  line-height: 1.6;
}

input:focus,
select:focus,
textarea:focus {
  border-color: color-mix(in srgb, var(--blue) 84%, white 16%);
  background: rgba(0, 0, 0, 0.48);
  box-shadow: 0 0 0 3px rgba(10, 132, 255, 0.16);
}

.key-row {
  gap: 8px;
}

.key-row input {
  font-family: var(--mono);
}

.key-row .icon-button {
  flex: 0 0 auto;
}

.ref-upload-row {
  gap: 8px;
}

.ref-upload-row input {
  flex: 1;
  min-width: 0;
}

.ref-upload-row .ghost-button {
  flex: 0 0 auto;
}

.reference-drop-zone {
  padding: 10px;
  margin: -10px;
  border: 1px dashed transparent;
  border-radius: 12px;
  transition: border-color 150ms var(--fade), background 150ms var(--fade);
}

.reference-drop-zone small {
  color: var(--label3);
  font-size: 11px;
}

.reference-drop-zone.is-dragover {
  border-color: var(--blue);
  background: rgba(10, 132, 255, 0.12);
}

.primary-button,
.ghost-button,
.danger-button,
.icon-button,
.task-stop-button {
  min-height: 38px;
  border: 0;
  border-radius: 999px;
  color: var(--label);
  text-decoration: none;
  transition: transform 150ms var(--spring), background 150ms var(--fade), border-color 150ms var(--fade);
}

.primary-button:hover,
.ghost-button:hover,
.danger-button:hover,
.icon-button:hover,
.task-stop-button:hover,
.image-card:hover {
  transform: translateY(-1px);
}

.primary-button {
  flex: 1;
  padding: 0 18px;
  color: white;
  font-weight: 700;
  background: var(--blue);
}

.ghost-button,
.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 14px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.04);
}

.danger-button,
.task-stop-button {
  padding: 0 16px;
  color: var(--red);
  border: 1px solid color-mix(in srgb, var(--red) 28%, transparent);
  background: color-mix(in srgb, var(--red) 12%, transparent);
}

.actions {
  gap: 10px;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 0 9px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 999px;
  color: var(--label2);
  font-family: var(--mono);
  font-size: 12px;
  background: rgba(255, 255, 255, 0.05);
}

.badge-ok {
  color: var(--green);
  border-color: color-mix(in srgb, var(--green) 32%, transparent);
  background: color-mix(in srgb, var(--green) 12%, transparent);
}

.badge-warn {
  color: var(--orange);
  border-color: color-mix(in srgb, var(--orange) 32%, transparent);
  background: color-mix(in srgb, var(--orange) 12%, transparent);
}

.badge-error {
  color: var(--red);
  border-color: color-mix(in srgb, var(--red) 32%, transparent);
  background: color-mix(in srgb, var(--red) 12%, transparent);
}

.mono {
  font-family: var(--mono);
}

.hidden {
  display: none !important;
}

.toast {
  position: fixed;
  top: max(22px, env(safe-area-inset-top));
  left: 50%;
  z-index: 1000;
  max-width: min(420px, calc(100vw - 32px));
  padding: 10px 16px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 999px;
  color: var(--label);
  font-size: 13px;
  line-height: 1.4;
  text-align: center;
  background: rgba(44, 44, 46, 0.92);
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.42);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -12px);
  transition: opacity 180ms var(--fade), transform 180ms var(--smooth);
  backdrop-filter: blur(16px) saturate(180%);
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.image-preview-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 8px;
}

.reference-thumb {
  position: relative;
  overflow: hidden;
  height: 84px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  background: var(--bg3);
  cursor: grab;
}

.reference-thumb.is-dragging {
  opacity: 0.45;
}

.reference-thumb:active {
  cursor: grabbing;
}

.reference-preview-button {
  width: 100%;
  height: 100%;
  padding: 0;
  border: 0;
  background: transparent;
}

.reference-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.reference-thumb span {
  position: absolute;
  right: 6px;
  bottom: 6px;
  padding: 2px 6px;
  border-radius: 999px;
  color: var(--label);
  font-family: var(--mono);
  font-size: 10px;
  background: rgba(0, 0, 0, 0.62);
  pointer-events: none;
}

.reference-remove-button,
.delete-image-button {
  position: absolute;
  top: 6px;
  right: 6px;
  z-index: 2;
  display: grid;
  width: 22px;
  height: 22px;
  min-height: 0;
  padding: 0;
  place-items: center;
  border: 0;
  border-radius: 999px;
  color: var(--label);
  font-size: 16px;
  line-height: 1;
  background: rgba(0, 0, 0, 0.68);
}

.delete-image-button {
  top: 15px;
}

.delete-image-button:hover {
  background: color-mix(in srgb, var(--red) 72%, black);
}

.task-list {
  display: grid;
  gap: 10px;
}

.image-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 12px;
  border: 1px dashed transparent;
  border-radius: 14px;
  transition: border-color 150ms var(--fade), background 150ms var(--fade);
}

.image-grid.is-dragover {
  border-color: var(--blue);
  background: rgba(10, 132, 255, 0.12);
}

.empty-state {
  display: grid;
  min-height: 160px;
  place-items: center;
  border: 1px dashed rgba(255, 255, 255, 0.1);
  border-radius: 14px;
  color: var(--label3);
}

.task-card,
.image-card {
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.24);
}

.task-card {
  overflow: visible;
}

.task-thumb {
  display: grid;
  aspect-ratio: 16 / 10;
  place-items: center;
  overflow: hidden;
  color: var(--label3);
  background: var(--bg3);
}

.image-button img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.task-thumb img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.skeleton {
  background: linear-gradient(90deg, var(--bg3), var(--bg4), var(--bg3));
  background-size: 220% 100%;
  animation: shimmer 1.4s linear infinite;
}

@keyframes shimmer {
  from {
    background-position: 220% 0;
  }

  to {
    background-position: -220% 0;
  }
}

.task-body,
.image-info {
  display: grid;
  gap: 9px;
  padding: 11px;
}

.task-body {
  gap: 7px;
}

.task-title-row,
.image-title-row {
  justify-content: space-between;
  gap: 8px;
}

.task-meta {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex: 0 0 auto;
}

.task-elapsed {
  color: var(--label2);
  font-size: 12px;
}

.task-title,
.image-title-row strong {
  overflow: hidden;
  font-size: 13px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.task-detail,
.image-info p {
  color: var(--label2);
  font-size: 11px;
  line-height: 1.5;
}

.task-actions {
  display: flex;
  justify-content: flex-end;
}

.task-stop-button {
  min-height: 28px;
  padding: 0 10px;
  color: var(--red);
  border-color: color-mix(in srgb, var(--red) 28%, transparent);
  font-size: 12px;
}

.task-error {
  display: none;
  color: var(--red);
  font-size: 12px;
  line-height: 1.5;
}

.task-card.has-error .task-error {
  display: block;
}

.task-card.is-complete {
  border-color: color-mix(in srgb, var(--green) 28%, transparent);
}

.task-card.has-error {
  border-color: color-mix(in srgb, var(--red) 28%, transparent);
}

.image-card {
  position: relative;
  min-width: 0;
  transition: transform 150ms var(--spring), border-color 150ms var(--fade);
}

.image-card:hover {
  border-color: rgba(255, 255, 255, 0.22);
}

.image-media {
  position: relative;
}

.image-select {
  position: absolute;
  top: 6px;
  left: 6px;
  z-index: 2;
}

.image-select input {
  display: block;
  width: 22px;
  height: 22px;
  margin: 0;
  accent-color: var(--blue);
  cursor: pointer;
}

.image-card.selected {
  border-color: var(--blue);
}

.image-button {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 10;
  padding: 0;
  border: 0;
  background: var(--bg3);
}

.image-title-row {
  min-width: 0;
}

.image-title-row strong {
  flex: 1 1 auto;
  min-width: 0;
}

.image-title-row .badge {
  flex: 0 0 auto;
  white-space: nowrap;
}

.image-actions {
  flex-wrap: wrap;
  gap: 8px;
}

.history-warning {
  margin-top: 4px;
  color: var(--orange);
  font-size: 12px;
  letter-spacing: 0;
}

.library-tabs {
  display: flex;
  gap: 4px;
}

.library-tab {
  min-height: 34px;
  padding: 0 12px;
  border: 0;
  border-radius: 999px;
  color: var(--label2);
  font: inherit;
  font-size: 18px;
  font-weight: 700;
  background: transparent;
}

.library-tab.active {
  color: var(--label);
  background: rgba(255, 255, 255, 0.1);
}

.history-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.history-actions .danger-button,
.history-actions .ghost-button {
  min-height: 30px;
  padding: 0 10px;
  font-size: 12px;
}

.history-pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 12px;
  margin-top: 16px;
}

.page-button {
  min-height: 32px;
  padding: 0 12px;
  font-size: 13px;
}

.page-info {
  color: var(--label2);
  font-size: 13px;
}

.image-actions .ghost-button {
  min-height: 30px;
  padding: 0 10px;
  font-size: 12px;
}

.preview-dialog {
  width: 100vw;
  max-width: none;
  height: 100dvh;
  max-height: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  border: 0;
  color: var(--label);
  background: rgba(0, 0, 0, 0.86);
  overscroll-behavior: none;
}

.preview-dialog::backdrop {
  background: rgba(0, 0, 0, 0.86);
  backdrop-filter: blur(12px);
}

.preview-stage {
  position: absolute;
  inset: 0;
  overflow: hidden;
  cursor: grab;
  touch-action: none;
  user-select: none;
}

.preview-stage.is-dragging {
  cursor: grabbing;
}

.preview-canvas {
  position: absolute;
  inset: 0;
  pointer-events: none;
  transform-origin: 0 0;
  will-change: transform;
}

.preview-canvas img {
  position: absolute;
  top: 50%;
  left: 50%;
  display: block;
  max-width: calc(100vw - 48px);
  max-height: calc(100dvh - 152px);
  border-radius: 12px;
  object-fit: contain;
  transform: translate(-50%, -50%);
  pointer-events: auto;
  -webkit-user-drag: none;
  user-select: none;
}

.preview-toolbar {
  position: absolute;
  top: max(16px, env(safe-area-inset-top));
  right: max(16px, env(safe-area-inset-right));
  z-index: 3;
  display: flex;
  gap: 8px;
}

.preview-toolbar .ghost-button,
.preview-toolbar .icon-button {
  min-height: 36px;
  padding: 0 14px;
  background: rgba(0, 0, 0, 0.68);
  backdrop-filter: blur(12px);
}

.preview-nav-button {
  position: absolute;
  top: 50%;
  z-index: 3;
  width: 46px;
  height: 46px;
  min-height: 46px;
  padding: 0;
  border-radius: 999px;
  font-size: 36px;
  line-height: 1;
  background: rgba(0, 0, 0, 0.68);
  transform: translateY(-50%);
  backdrop-filter: blur(12px);
}

.preview-nav-button:hover {
  transform: translateY(calc(-50% - 1px));
}

.preview-previous-button {
  left: max(16px, env(safe-area-inset-left));
}

.preview-next-button {
  right: max(16px, env(safe-area-inset-right));
}

.preview-meta {
  position: absolute;
  right: max(16px, env(safe-area-inset-right));
  bottom: max(16px, env(safe-area-inset-bottom));
  left: max(16px, env(safe-area-inset-left));
  z-index: 2;
  max-height: 82px;
  padding: 10px 12px;
  overflow: auto;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  color: var(--label2);
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.7;
  white-space: pre-wrap;
  background: rgba(0, 0, 0, 0.68);
  backdrop-filter: blur(12px);
}

@media (min-width: 1280px) {
  html,
  body {
    height: 100%;
    overflow: hidden;
  }

  .app-shell {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    gap: 12px;
    height: 100dvh;
    min-height: 0;
    padding: 16px;
  }

  .topbar {
    position: static;
    min-height: 56px;
    margin-bottom: 0;
    padding: 8px 12px;
  }

  .workspace {
    grid-template-columns: minmax(280px, 320px) minmax(0, 1fr) minmax(260px, 300px);
    gap: 12px;
    min-height: 0;
  }

  .control-panel,
  .task-panel {
    position: static;
    top: auto;
    height: 100%;
    min-height: 0;
    overflow-y: auto;
  }

  .control-panel,
  .section-card {
    padding: 14px;
  }

  .result-panel,
  .history-card {
    height: 100%;
    min-height: 0;
  }

  .result-panel {
    overflow: hidden;
  }

  .history-card {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    overflow: hidden;
  }

  .panel-heading {
    gap: 10px;
    margin-bottom: 12px;
  }

  .form-grid {
    gap: 8px;
  }

  .field {
    gap: 5px;
  }

  input,
  select {
    min-height: 34px;
  }

  textarea {
    min-height: clamp(96px, 14dvh, 136px);
    padding: 9px;
  }

  .primary-button,
  .ghost-button,
  .danger-button,
  .icon-button,
  .task-stop-button {
    min-height: 34px;
  }

  .reference-drop-zone {
    padding: 8px;
    margin: -8px;
  }

  .image-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    grid-template-rows: repeat(3, minmax(0, 1fr));
    gap: 8px;
    min-height: 0;
    overflow: hidden;
  }

  .image-card {
    display: grid;
    grid-template-rows: minmax(0, 1fr) auto;
    min-height: 0;
  }

  .image-media,
  .image-button {
    min-height: 0;
    height: 100%;
  }

  .image-button {
    aspect-ratio: auto;
  }

  .image-info {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto auto auto;
    align-content: stretch;
    gap: 5px 8px;
    min-height: 0;
    padding: 8px;
    overflow: hidden;
  }

  .image-title-row,
  .image-actions {
    grid-column: 1 / -1;
  }

  .image-actions {
    grid-row: 3;
    align-self: end;
  }

  .image-meta {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .image-duration {
    white-space: nowrap;
  }

  .image-actions {
    gap: 6px;
  }

  .image-actions .ghost-button {
    min-height: 26px;
    padding: 0 8px;
  }

  .history-pagination {
    min-height: 32px;
    gap: 10px;
    margin-top: 10px;
  }

  .page-button {
    min-height: 30px;
  }
}

@media (max-width: 980px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .control-panel,
  .task-panel {
    position: static;
  }
}

@media (max-width: 620px) {
  .app-shell {
    padding: 14px;
  }

  .topbar,
  .workspace,
  .panel-heading,
  .topbar-meta {
    align-items: stretch;
  }

  .topbar,
  .workspace,
  .panel-heading {
    flex-direction: column;
  }

  .topbar {
    display: grid;
    gap: 12px;
  }

  .form-grid {
    grid-template-columns: 1fr;
  }

  .actions {
    flex-direction: column;
  }

  .primary-button,
  .danger-button {
    width: 100%;
  }

  .preview-canvas img {
    max-width: calc(100vw - 24px);
    max-height: calc(100dvh - 168px);
  }

  .preview-toolbar {
    top: max(10px, env(safe-area-inset-top));
    right: max(10px, env(safe-area-inset-right));
  }

  .preview-toolbar .ghost-button,
  .preview-toolbar .icon-button {
    min-height: 34px;
    padding: 0 11px;
  }

  .preview-nav-button {
    width: 40px;
    height: 40px;
    min-height: 40px;
    font-size: 30px;
  }

  .preview-previous-button {
    left: max(8px, env(safe-area-inset-left));
  }

  .preview-next-button {
    right: max(8px, env(safe-area-inset-right));
  }

  .preview-meta {
    right: max(10px, env(safe-area-inset-right));
    bottom: max(10px, env(safe-area-inset-bottom));
    left: max(10px, env(safe-area-inset-left));
  }
}
