:root {
  color-scheme: light dark;
  --bg: #1c1e22;
  --panel-bg: #26282e;
  --text: #e8e8ea;
}

* {
  box-sizing: border-box;
}

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

#app {
  display: flex;
  min-height: 100vh;
  flex-wrap: wrap;
}

#canvas-wrap {
  flex: 1 1 600px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  min-width: 0;
}

#preview {
  max-width: 100%;
  height: auto;
  background: #fff;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
}

#panel {
  flex: 0 0 320px;
  padding: 16px;
  overflow-y: auto;
  max-height: 100vh;
}

/* The collapse handle is a mobile-only affordance (see media query). */
#panel-handle {
  display: none;
}

#image-drop-zone {
  border: 2px dashed #555;
  border-radius: 6px;
  padding: 12px;
  margin-bottom: 12px;
  text-align: center;
  font-size: 12px;
  color: #aaa;
  cursor: pointer;
}

#image-drop-zone.drag-over {
  border-color: #8fbfe0;
  color: #e8e8ea;
}

#image-drop-zone label {
  display: block;
  cursor: pointer;
}

#image-drop-zone input[type="file"] {
  display: none;
}

/* --- Phone / narrow: canvas fills the screen, controls in a bottom sheet --- */
@media (max-width: 820px) {
  html,
  body {
    /* dvh tracks the shrinking/growing mobile URL bar; vh is the fallback */
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }

  #app {
    display: block;
    min-height: 0;
    height: 100vh;
    height: 100dvh;
  }

  #canvas-wrap {
    height: 100vh;
    height: 100dvh;
    padding: 10px 10px 64px; /* room for the collapsed sheet handle */
    box-sizing: border-box;
  }

  #preview {
    max-height: 100%;
  }

  #panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    display: flex;
    flex-direction: column;
    max-height: 82dvh;
    padding: 0;
    background: var(--panel-bg);
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.55);
    overflow: hidden;
    transform: translateY(0);
    transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  }

  #panel.collapsed {
    transform: translateY(calc(100% - 52px));
  }

  #panel-handle {
    display: block;
    position: relative;
    flex: 0 0 auto;
    width: 100%;
    padding: 16px;
    border: 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    background: none;
    color: var(--text);
    font: inherit;
    font-weight: 600;
    text-align: center;
    cursor: pointer;
  }

  #panel-handle::before {
    content: "";
    position: absolute;
    top: 7px;
    left: 50%;
    width: 36px;
    height: 4px;
    border-radius: 2px;
    background: rgba(255, 255, 255, 0.25);
    transform: translateX(-50%);
  }

  #panel-handle::after {
    content: " ▲";
    font-size: 10px;
  }

  #panel:not(.collapsed) #panel-handle::after {
    content: " ▼";
  }

  #panel-body {
    flex: 1 1 auto;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding: 10px 12px calc(18px + env(safe-area-inset-bottom));
  }

  /* Bigger touch targets, and 16px inputs so iOS doesn't zoom on focus. */
  #panel-body .tp-dfwv {
    --tp-blade-unit-size: 24px;
  }

  #panel-body input,
  #panel-body select {
    font-size: 16px;
  }
}

/* ---- Preset browser dialog (preset-dialog.js) ------------------------ */
body.preset-dialog-open {
  overflow: hidden;
}

.preset-dialog__backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(0, 0, 0, 0.55);
}

.preset-dialog {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 900px;
  max-height: 85vh;
  background: var(--panel-bg);
  color: var(--text);
  border-radius: 10px;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.5);
  overflow: hidden;
}

.preset-dialog__header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 14px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.preset-dialog__title {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
}

.preset-dialog__search {
  flex: 1 1 auto;
  min-width: 0;
  padding: 6px 10px;
  font-size: 13px;
  color: var(--text);
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 6px;
}

.preset-dialog__sort,
.preset-dialog__close {
  flex: 0 0 auto;
  padding: 6px 10px;
  font-size: 12px;
  color: var(--text);
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 6px;
  cursor: pointer;
}

.preset-dialog__close {
  padding: 6px 9px;
}

.preset-dialog__body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 14px;
}

.preset-dialog__chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 14px;
}

.preset-dialog__chip {
  padding: 4px 10px;
  font-size: 12px;
  color: var(--text);
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 999px;
  cursor: pointer;
}

.preset-dialog__chip.is-active {
  background: #4a90d9;
  border-color: #4a90d9;
  color: #fff;
}

.preset-dialog__grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px;
}

.preset-dialog__empty {
  margin: 24px 0;
  text-align: center;
  opacity: 0.6;
  font-size: 13px;
}

.preset-card {
  display: flex;
  flex-direction: column;
  padding: 0;
  text-align: left;
  color: var(--text);
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
}

.preset-card:hover,
.preset-card:focus-visible {
  border-color: #4a90d9;
  outline: none;
}

.preset-card__thumb {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 1 / 1;
  background: #fff;
}

.preset-card__meta {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 7px 9px 9px;
}

.preset-card__name {
  font-size: 12px;
  font-weight: 600;
}

.preset-card__tag {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  opacity: 0.55;
}

/* ---- Unlock Pro dialog (upsell-dialog.js) --------------------------- */
.upsell-dialog {
  position: relative;
  width: 100%;
  max-width: 460px;
  max-height: 85vh;
  overflow-y: auto;
  padding: 22px 24px 24px;
  background: var(--panel-bg);
  color: var(--text);
  border-radius: 10px;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.5);
  font-size: 13px;
  line-height: 1.5;
}

.upsell-dialog .preset-dialog__close {
  position: absolute;
  top: 12px;
  right: 12px;
}

.upsell-dialog__title {
  margin: 0 0 12px;
  font-size: 17px;
  font-weight: 600;
}

.upsell-dialog__lead {
  margin: 0 0 8px;
}

.upsell-dialog__list {
  margin: 0 0 12px;
  padding-left: 18px;
}

.upsell-dialog__list li {
  margin: 2px 0;
}

.upsell-dialog__free {
  margin: 0 0 16px;
  opacity: 0.65;
  font-size: 12px;
}

.upsell-dialog__plans {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 12px;
  padding: 10px 12px;
  background: rgba(0, 0, 0, 0.22);
  border-radius: 8px;
}

.upsell-dialog__plan {
  display: flex;
  justify-content: space-between;
}

.upsell-dialog__plan span:last-child {
  font-weight: 600;
}

.upsell-dialog__buy {
  margin: 0 0 18px;
}

.upsell-dialog__buy a {
  color: #6db3f2;
}

.upsell-dialog__activate {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
  padding-top: 14px;
  border-top: 1px solid rgba(255, 255, 255, 0.12);
}

.upsell-dialog__activate-label {
  grid-column: 1 / -1;
  font-size: 12px;
  opacity: 0.75;
}

.upsell-dialog__key {
  min-width: 0;
  padding: 7px 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  color: var(--text);
  background: rgba(0, 0, 0, 0.28);
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 6px;
}

.upsell-dialog__submit {
  padding: 7px 14px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
  background: #2f6fb0;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 6px;
  cursor: pointer;
}

.upsell-dialog__submit:disabled {
  opacity: 0.5;
  cursor: default;
}

.upsell-dialog__status {
  grid-column: 1 / -1;
  margin: 2px 0 0;
  font-size: 12px;
  min-height: 1em;
}

.upsell-dialog__status.is-ok {
  color: #7fd18a;
}

.upsell-dialog__status.is-err {
  color: #f0a3a3;
}

@media (max-width: 820px) {
  .preset-dialog__backdrop {
    padding: 0;
  }

  .preset-dialog {
    max-width: none;
    max-height: 100dvh;
    height: 100dvh;
    border-radius: 0;
  }

  .upsell-dialog {
    max-width: none;
    max-height: 100dvh;
    height: 100dvh;
    border-radius: 0;
  }
}
