:root,
[data-theme="dark"] {
  color-scheme: dark;
  --blue: #3d5afe;
  --blue-bright: #6e84ff;
  --ink: #e8eaf2;
  --muted: #8b93a7;
  --line: #262b3a;
  --panel: #161a26;
  --paper: #0d1018;
  --raised: #11141f;
  --focus: #c7f24f;
  --check-a: #161a26;
  --shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
}

[data-theme="light"] {
  color-scheme: light;
  --blue: #0000db;
  --blue-bright: #0000db;
  --ink: #111318;
  --muted: #646a76;
  --line: #dfe3ea;
  --panel: #f6f7f9;
  --paper: #ffffff;
  --raised: #ffffff;
  --focus: #c7f24f;
  --check-a: #f2f4f8;
  --shadow: 0 20px 60px rgba(17, 19, 24, 0.12);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

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

body {
  min-height: 100%;
  margin: 0;
  color: var(--ink);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

button,
input,
select {
  font: inherit;
}

button,
select,
.color-swatch {
  border-radius: 6px;
}

button {
  min-height: 42px;
  border: 1px solid var(--ink);
  background: var(--ink);
  color: var(--paper);
  cursor: pointer;
  font-weight: 700;
}

.secondary-button {
  border-color: var(--line);
  background: var(--raised);
  color: var(--ink);
}

.mini-button {
  min-height: 32px;
  padding: 0 12px;
  border-color: var(--line);
  background: var(--raised);
  color: var(--ink);
  font-size: 0.85rem;
}

.accent-button {
  border-color: var(--blue);
  background: var(--blue);
  color: #fff;
}

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

.timeline-download {
  border-color: var(--blue);
  background: var(--blue);
  color: #fff;
  white-space: nowrap;
}

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

button:not(:disabled):hover {
  border-color: var(--blue);
  background: var(--blue);
  color: #fff;
}

button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

.app-shell {
  width: min(1520px, 100%);
  min-height: 100vh;
  margin: 0 auto;
  padding: 28px;
}

.app-header {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 24px;
}

.header-actions {
  display: flex;
  gap: 8px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--blue-bright);
  font-size: 0.78rem;
  font-weight: 800;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: clamp(2.2rem, 5vw, 4.6rem);
  line-height: 0.88;
}

.version-tag {
  color: var(--blue-bright);
}

h2 {
  margin: 0 0 12px;
  font-size: 0.82rem;
  text-transform: uppercase;
}

.workspace {
  display: grid;
  min-height: calc(100vh - 170px);
  grid-template-columns: minmax(290px, 370px) minmax(0, 1fr);
  gap: 20px;
}

.control-panel,
.preview-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--raised);
  box-shadow: var(--shadow);
}

.control-panel {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 18px;
  max-height: calc(100vh - 140px);
  overflow-y: auto;
  scrollbar-width: thin;
}

.upload-target {
  position: relative;
  display: grid;
  grid-template-columns: 44px 1fr;
  gap: 12px;
  align-items: center;
  min-height: 88px;
  border: 1px dashed var(--blue);
  border-radius: 8px;
  padding: 16px;
  background: color-mix(in srgb, var(--blue) 7%, var(--paper));
  cursor: pointer;
}

.upload-target.dragging {
  box-shadow: inset 0 0 0 2px var(--blue);
}

.is-dragging .preview-stage {
  box-shadow: inset 0 0 0 3px var(--blue);
}

.upload-target input {
  position: absolute;
  inline-size: 1px;
  block-size: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

.upload-icon {
  display: grid;
  width: 44px;
  height: 44px;
  place-items: center;
  border-radius: 50%;
  background: var(--blue);
  color: #fff;
  font-size: 1.8rem;
  font-weight: 800;
  line-height: 1;
}

.upload-target strong,
.upload-target small {
  display: block;
}

.upload-target small {
  margin-top: 2px;
  color: var(--muted);
}

.source-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.control-group {
  border-top: 1px solid var(--line);
  padding-top: 16px;
}

details.collapsible summary {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  list-style: none;
  user-select: none;
}

details.collapsible summary::-webkit-details-marker {
  display: none;
}

details.collapsible summary::after {
  content: "▸";
  margin-left: auto;
  color: var(--muted);
  transition: transform 0.15s ease;
}

details.collapsible[open] summary::after {
  transform: rotate(90deg);
}

details.collapsible summary h2 {
  margin: 0;
}

.preset-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.preset-chip {
  min-height: 30px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 99px;
  background: var(--panel);
  color: var(--ink);
  font-size: 0.82rem;
  font-weight: 700;
}

.preset-chip.active {
  border-color: var(--blue);
  background: var(--blue);
  color: #fff;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.segmented.three {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.segmented label {
  min-width: 0;
}

.segmented input {
  position: absolute;
  inline-size: 1px;
  block-size: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

.segmented span {
  display: grid;
  min-height: 34px;
  place-items: center;
  border-radius: 6px;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 800;
  cursor: pointer;
}

.segmented input:checked + span {
  background: var(--blue);
  color: #fff;
}

.inline-control,
.stacked-control,
.range-control,
.check-control {
  display: grid;
  gap: 8px;
  margin-top: 12px;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 700;
}

.inline-control {
  grid-template-columns: 1fr auto;
  align-items: center;
}

.check-control {
  grid-template-columns: auto 1fr;
  align-items: center;
  color: var(--ink);
  cursor: pointer;
}

.check-control input {
  width: 18px;
  height: 18px;
  margin: 0;
  accent-color: var(--blue);
}

.palette-colors {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.palette-color {
  position: relative;
  display: grid;
}

.palette-color input[type="color"] {
  width: 52px;
  height: 38px;
  padding: 2px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  cursor: pointer;
}

.palette-color .remove-color {
  position: absolute;
  top: -7px;
  right: -7px;
  display: none;
  width: 18px;
  height: 18px;
  min-height: 0;
  padding: 0;
  border-radius: 50%;
  background: var(--ink);
  color: var(--paper);
  font-size: 0.65rem;
  line-height: 1;
}

.palette-color:hover .remove-color {
  display: grid;
  place-items: center;
}

.palette-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 12px;
}

.palette-actions .check-control {
  margin-top: 0;
}

.sticker-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  gap: 8px;
  margin: 12px 0;
}

.sticker-grid button {
  min-height: 0;
  aspect-ratio: 1;
  padding: 8px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background:
    linear-gradient(45deg, var(--check-a) 25%, transparent 25%),
    linear-gradient(-45deg, var(--check-a) 25%, transparent 25%),
    var(--panel);
  background-size: 12px 12px;
}

.sticker-grid button.active {
  border-color: var(--blue);
}

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

.hint {
  margin: 12px 0 0;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 600;
}

input[type="text"],
input[type="search"] {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
  color: var(--ink);
  padding: 0 10px;
}

select {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  padding: 0 10px;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--blue);
}

input[type="range"]:disabled {
  opacity: 0.45;
}

output {
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

.button-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 12px;
}

#copyResult {
  grid-column: 1 / -1;
}

.preview-panel {
  display: grid;
  min-width: 0;
  grid-template-rows: auto auto 1fr auto;
  overflow: hidden;
}

/* Mobile-only quick actions pinned under the preview toolbar */
.quick-bar {
  display: none;
  gap: 8px;
  border-bottom: 1px solid var(--line);
  padding: 10px 16px;
}

.quick-bar button {
  flex: 1;
  min-height: 44px;
}

.preview-toolbar {
  display: flex;
  min-height: 56px;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--line);
  padding: 0 16px;
  color: var(--muted);
  font-size: 0.9rem;
}

.preview-toolbar p {
  margin: 0;
}

.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

#compareBtn {
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

.preview-stage {
  position: relative;
  display: grid;
  min-height: 520px;
  place-items: center;
  overflow: auto;
  background:
    linear-gradient(45deg, var(--check-a) 25%, transparent 25%),
    linear-gradient(-45deg, var(--check-a) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, var(--check-a) 75%),
    linear-gradient(-45deg, transparent 75%, var(--check-a) 75%),
    var(--paper);
  background-position:
    0 0,
    0 10px,
    10px -10px,
    -10px 0;
  background-size: 20px 20px;
}

.empty-state {
  display: grid;
  place-items: center;
  gap: 6px;
  min-width: min(430px, calc(100% - 40px));
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 28px;
  background: color-mix(in srgb, var(--raised) 94%, transparent);
  text-align: center;
}

.empty-state strong {
  color: var(--blue-bright);
  font-size: 1.35rem;
}

.empty-state small {
  color: var(--muted);
}

.preview-stage canvas,
.preview-stage pre,
.preview-stage img {
  display: none;
}

#compareImage {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: var(--paper);
}

.preview-stage canvas {
  max-width: calc(100% - 32px);
  max-height: calc(100vh - 285px);
  background: var(--paper);
  image-rendering: auto;
}

.preview-stage pre {
  min-width: max-content;
  max-width: none;
  margin: 0;
  padding: 24px;
  background: var(--ascii-bg, var(--paper));
  color: var(--ascii-fg, var(--blue));
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 8px;
  line-height: 1;
  white-space: pre;
}

.has-image .empty-state {
  display: none;
}

/* The mirror img replaces the canvas for still sources so long-press /
   right-click offers "Save Image" — canvases don't. */
#previewImage {
  max-width: calc(100% - 32px);
  max-height: calc(100vh - 285px);
  background: var(--paper);
}

.has-image.mode-image #outputCanvas {
  display: block;
}

.has-image.mode-image.source-still #outputCanvas {
  display: none;
}

.has-image.mode-image.source-still #previewImage {
  display: block;
}

.has-image.mode-ascii #asciiOutput {
  display: block;
}

.has-image.mode-ascii.ascii-colored #asciiOutput {
  display: none;
}

.has-image.mode-ascii.ascii-colored #asciiCanvas {
  display: block;
}

.has-image.mode-ascii.ascii-colored.source-still #asciiCanvas {
  display: none;
}

.has-image.mode-ascii.ascii-colored.source-still #previewImage {
  display: block;
}

.comparing #compareImage {
  display: block !important;
  z-index: 5;
}

.gif-timeline {
  display: flex;
  align-items: center;
  gap: 12px;
  border-top: 1px solid var(--line);
  padding: 10px 16px;
}

.gif-timeline input[type="range"] {
  flex: 1;
}

.frame-label {
  min-width: 70px;
  color: var(--muted);
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

/* Dialogs */

.modal-dialog {
  width: min(760px, calc(100vw - 24px));
  max-height: calc(100vh - 24px);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px;
  background: var(--raised);
  color: var(--ink);
  box-shadow: var(--shadow);
}

.modal-dialog::backdrop {
  background: rgba(5, 7, 12, 0.62);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.modal-header h2 {
  margin: 0;
}

.gif-search-bar {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
}

.gif-search-status {
  margin: 12px 0 8px;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 700;
  text-transform: uppercase;
}

.gif-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 8px;
  max-height: min(440px, 55vh);
  overflow-y: auto;
}

.gif-grid button {
  min-height: 0;
  padding: 0;
  border: 2px solid transparent;
  border-radius: 6px;
  overflow: hidden;
  background: var(--panel);
}

.gif-grid button:hover {
  border-color: var(--blue);
  background: var(--panel);
}

.gif-grid img {
  display: block;
  width: 100%;
  height: 110px;
  object-fit: cover;
}

.load-more {
  width: 100%;
  margin-top: 10px;
}

.webcam-stage {
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  overflow: hidden;
}

.webcam-stage video {
  display: block;
  max-width: 100%;
  max-height: min(480px, 60vh);
}

.crop-stage {
  display: grid;
  max-height: min(540px, calc(100vh - 280px));
  place-items: center;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

#cropCanvas {
  display: block;
  max-width: 100%;
  cursor: grab;
  touch-action: none;
}

#cropCanvas.is-dragging {
  cursor: grabbing;
}

.joke-dialog {
  width: min(380px, calc(100vw - 24px));
  text-align: center;
}

.joke-dialog p {
  color: var(--muted);
  font-weight: 600;
}

.joke-dialog .button-row {
  grid-template-columns: 1fr;
}

.progress-dialog {
  width: min(420px, calc(100vw - 24px));
  text-align: center;
}

.progress-dialog progress {
  width: 100%;
  height: 14px;
  accent-color: var(--blue);
}

.progress-dialog p {
  color: var(--muted);
}

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  z-index: 50;
  transform: translateX(-50%);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px 20px;
  background: var(--ink);
  color: var(--paper);
  font-weight: 700;
  box-shadow: var(--shadow);
}

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

  .app-header {
    align-items: start;
    flex-direction: column;
  }

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

  /* Preview first on mobile so randomize/download stay within thumb reach */
  .preview-panel {
    order: -1;
  }

  .quick-bar {
    display: flex;
  }

  .control-panel {
    max-height: none;
    overflow-y: visible;
  }

  .preview-stage {
    min-height: 320px;
    max-height: 48vh;
  }

  .preview-stage canvas,
  #previewImage {
    max-height: 44vh;
  }
}

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

  .button-row,
  .segmented,
  .source-row {
    grid-template-columns: 1fr;
  }

  .preview-toolbar {
    align-items: start;
    flex-direction: column;
    justify-content: center;
    padding: 12px;
  }
}
