:root {
  color-scheme: light;
  --ink: #111;
  --paper: #fff;
  --panel: #f2f2f0;
  --line: #d4d4d0;
  --active: #111;
  --active-text: #fff;
}

* { box-sizing: border-box; }
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; background: var(--paper); font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
button, input, select { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }

.app { width: 100vw; height: 100dvh; display: grid; grid-template-rows: minmax(0, 1fr) auto; background: var(--paper); }
.stage { min-height: 0; position: relative; overflow: hidden; background: #e9e9e6; touch-action: none; user-select: none; }
.stage canvas { position: absolute; inset: auto; touch-action: none; background: #fff; }
#liveCanvas { z-index: 2; background: transparent; }
.touch-hint { position: absolute; z-index: 3; inset: 0; display: grid; place-items: center; pointer-events: none; color: #a3a3a0; font-size: clamp(20px, 3vw, 38px); letter-spacing: .01em; transition: opacity .3s ease; }
.touch-hint.hidden { opacity: 0; }

.controls { position: relative; z-index: 10; background: var(--panel); border-top: 1px solid var(--line); padding: 6px max(8px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left)); }
.control-row { display: flex; align-items: center; gap: 7px; }
.tools { justify-content: space-between; flex-wrap: wrap; }
.brand-group { display: flex; align-items: center; gap: 8px; min-width: 0; }
.brand { display: flex; align-items: center; gap: 7px; min-width: 0; padding-right: 2px; color: #111; user-select: none; }
.brand-mark { width: 30px; height: 30px; flex: 0 0 30px; object-fit: contain; }
.brand-name { font-size: 17px; font-weight: 850; letter-spacing: -.025em; white-space: nowrap; }
.about-trigger { border: 0; background: transparent; color: #666; padding: 5px 2px; font-size: 12px; font-weight: 700; cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }
.about-trigger:hover { color: #111; }
.swatches, .sizes, .actions { display: flex; align-items: center; gap: 6px; }
.actions { margin-left: auto; flex-wrap: wrap; justify-content: flex-end; }
.behavior, .utility, .size { min-height: 40px; border: 1px solid #bdbdb8; border-radius: 9px; background: #fff; color: var(--ink); font-weight: 700; padding: 0 11px; cursor: pointer; }
.behavior.active, .size.active { background: var(--active); color: var(--active-text); border-color: var(--active); }
.utility.primary { background: #111; color: #fff; border-color: #111; }
.utility.danger { border-color: #b42318; color: #b42318; }
.behavior:disabled, .utility:disabled, .size:disabled { opacity: .42; cursor: not-allowed; }
.utility.compact { min-height: 36px; padding: 0 10px; }

.swatch { position: relative; width: 38px; height: 38px; flex: 0 0 38px; padding: 0; border-radius: 50%; border: 3px solid transparent; background: var(--swatch); box-shadow: inset 0 0 0 1px rgba(0,0,0,.18); cursor: pointer; }
.swatch.active { border-color: #111; outline: 2px solid #fff; outline-offset: -5px; }
.custom-swatch span { display: grid; place-items: center; width: 100%; height: 100%; color: #fff; font-size: 21px; font-weight: 800; text-shadow: 0 1px 3px rgba(0,0,0,.45); }
.size { width: 42px; padding: 0; }

.popover-wrap { position: relative; }
.popover { position: absolute; bottom: calc(100% + 9px); left: 0; z-index: 30; border: 1px solid var(--line); border-radius: 14px; background: #f8f8f6; box-shadow: 0 16px 45px rgba(0,0,0,.18); padding: 12px; }
.popover[hidden] { display: none !important; }
.effects-menu { width: min(620px, calc(100vw - 16px)); }
.popover-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.bulk-actions { display: flex; gap: 10px; }
.text-button { border: 0; background: transparent; padding: 5px 2px; color: #444; text-decoration: underline; cursor: pointer; }
.behaviors { display: grid; grid-template-columns: repeat(4, minmax(92px, 1fr)); gap: 7px; }
.count-badge { display: inline-grid; place-items: center; min-width: 22px; height: 22px; margin-left: 5px; padding: 0 6px; border-radius: 999px; background: #e7e7e3; color: #222; font-size: 12px; }

.color-menu { width: min(340px, calc(100vw - 16px)); }
.color-menu > strong { display: block; margin-bottom: 12px; }
.visual-color-picker-wrap { margin-bottom: 12px; }
.visual-color-picker {
  position: relative;
  width: 100%;
  height: 168px;
  overflow: hidden;
  border: 1px solid #aaa;
  border-radius: 11px;
  background:
    linear-gradient(to bottom, rgba(255,255,255,1) 0%, rgba(255,255,255,0) 50%, rgba(0,0,0,0) 50%, rgba(0,0,0,1) 100%),
    linear-gradient(to right, #f00 0%, #ff0 16.666%, #0f0 33.333%, #0ff 50%, #00f 66.666%, #f0f 83.333%, #f00 100%);
  cursor: crosshair;
  touch-action: none;
}
.visual-color-picker:focus-visible { outline: 3px solid #111; outline-offset: 2px; }
.visual-color-cursor {
  position: absolute;
  left: 74%;
  top: 45%;
  width: 18px;
  height: 18px;
  border: 2px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 0 1px rgba(0,0,0,.7), 0 2px 5px rgba(0,0,0,.25);
  transform: translate(-50%, -50%);
  pointer-events: none;
}
.compact-field { gap: 5px; }
.color-menu-actions { min-height: 38px; margin-top: 10px; display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.input-error { color: #b42318; font-size: 12px; }

.finish-dialog, .settings-dialog, .about-dialog { max-height: 92dvh; border: 0; border-radius: 18px; padding: 0; box-shadow: 0 30px 100px rgba(0,0,0,.25); }
.finish-dialog { width: min(92vw, 960px); }
.settings-dialog { width: min(92vw, 520px); }
.about-dialog { width: min(92vw, 500px); }
.finish-dialog::backdrop, .settings-dialog::backdrop, .about-dialog::backdrop { background: rgba(0,0,0,.58); }
.finish-card { padding: 18px; display: grid; gap: 16px; background: #f4f4f1; }
.preview-wrap { min-height: 200px; max-height: 68dvh; background: #fff; border: 1px solid var(--line); overflow: hidden; display: grid; place-items: center; }
.preview-wrap img { display: block; max-width: 100%; max-height: 66dvh; object-fit: contain; }
.finish-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.settings-card { padding: 20px; display: grid; gap: 16px; background: #f4f4f1; }
.settings-card h2 { margin: 0; font-size: 22px; }
.field { display: grid; gap: 6px; font-weight: 700; }
.field select, .field input { width: 100%; min-height: 44px; border: 1px solid #bdbdb8; border-radius: 9px; background: #fff; color: #111; padding: 8px 10px; }
.dimension-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.dimension-fields.hidden { display: none; }
.check-row { display: flex; align-items: center; gap: 10px; font-weight: 600; }
.check-row input { width: 20px; height: 20px; }
.settings-note { margin: 0; color: #666; line-height: 1.4; font-size: 14px; }
.settings-actions { display: flex; justify-content: flex-end; gap: 8px; }
.about-card { padding: 22px; display: grid; gap: 14px; background: #f4f4f1; }
.about-heading { display: flex; align-items: center; gap: 12px; }
.about-heading img { width: 38px; height: 38px; flex: 0 0 38px; }
.about-heading h2 { margin: 0; font-size: 24px; letter-spacing: -.025em; }
.about-subtitle { margin: 2px 0 0; color: #666; font-size: 13px; }
.about-card > p { margin: 0; color: #333; font-size: 15px; line-height: 1.5; }
.about-card .about-support { color: #777; font-size: 12px; }
.about-card .about-support a { color: inherit; text-underline-offset: 2px; }
.about-card .about-support a:hover { color: #111; }
.about-footer { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin-top: 2px; padding-top: 12px; border-top: 1px solid var(--line); }
.app-version { color: #777; font-size: 12px; font-variant-numeric: tabular-nums; }


@media (max-width: 820px) {
  .controls { padding: 5px 6px max(5px, env(safe-area-inset-bottom)); }
  .tools { gap: 5px; }
  .actions { width: 100%; justify-content: stretch; }
  .actions .utility { flex: 1 1 auto; min-width: max-content; }
  .behaviors { grid-template-columns: repeat(3, minmax(84px, 1fr)); }
}

@media (max-width: 520px) {
  .brand-name { display: none; }
  .brand { padding-right: 0; }
  .behavior, .utility, .size { min-height: 38px; border-radius: 8px; padding: 0 9px; font-size: 14px; }
  .swatch { width: 36px; height: 36px; flex-basis: 36px; }
  .size { width: 38px; }
  .actions { gap: 4px; }
  .actions .utility { min-width: 0; padding-inline: 7px; }
  #fullscreenBtn { display: none; }
  .effects-menu { width: auto; max-width: none; }
  .behaviors { grid-template-columns: repeat(2, minmax(88px, 1fr)); }
  .popover-heading { align-items: flex-start; }
  .bulk-actions { flex-direction: column; gap: 2px; align-items: flex-end; }
  .dimension-fields { grid-template-columns: 1fr; }
}

@media (max-height: 520px) and (orientation: landscape) {
  .brand-name { display: none; }
  .brand-mark { width: 28px; height: 28px; flex-basis: 28px; }
  .behavior, .utility, .size { min-height: 34px; }
  .swatch { width: 34px; height: 34px; flex-basis: 34px; }
  .size { width: 36px; }
  .actions { width: auto; }
  .tools { flex-wrap: nowrap; }
  .behaviors { max-height: 46vh; overflow: auto; }
}

/* Performance GIF capture */
.gif-record.recording { background: #b42318; color: #fff; border-color: #b42318; }
.gif-record.recording::before { content: ""; display: inline-block; width: 8px; height: 8px; margin-right: 6px; border-radius: 50%; background: currentColor; animation: rec-pulse 1s steps(1) infinite; }
@keyframes rec-pulse { 50% { opacity: .25; } }
.gif-dialog { width: min(92vw, 760px); max-height: 92dvh; border: 0; border-radius: 18px; padding: 0; box-shadow: 0 30px 100px rgba(0,0,0,.25); }
.gif-dialog::backdrop { background: rgba(0,0,0,.58); }
.gif-card { max-height: 92dvh; overflow-y: auto; padding: 18px; display: grid; gap: 12px; background: #f4f4f1; -webkit-overflow-scrolling: touch; }
.gif-preview-wrap {
  position: relative;
  min-height: min(58dvh, 480px);
  padding: 28px;
  overflow: hidden;
  display: grid;
  place-items: center;
  border: 1px solid #d7d7d1;
  border-radius: 14px;
  background-color: #e9e9e4;
  background-image:
    linear-gradient(45deg, rgba(255,255,255,.58) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(255,255,255,.58) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, rgba(255,255,255,.58) 75%),
    linear-gradient(-45deg, transparent 75%, rgba(255,255,255,.58) 75%);
  background-size: 24px 24px;
  background-position: 0 0, 0 12px, 12px -12px, -12px 0;
  box-shadow:
    inset 0 0 0 1px rgba(255,255,255,.58),
    inset 0 10px 28px rgba(0,0,0,.035);
}
.gif-preview-label {
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 1;
  padding: 5px 8px;
  border: 1px solid rgba(0,0,0,.09);
  border-radius: 999px;
  background: rgba(250,250,247,.88);
  box-shadow: 0 1px 3px rgba(0,0,0,.06);
  color: #666;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: .08em;
  line-height: 1;
  text-transform: uppercase;
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
}
.gif-preview-wrap img {
  display: block;
  max-width: 100%;
  max-height: 58dvh;
  object-fit: contain;
  background: #fff;
  border-radius: 3px;
  box-shadow: 0 10px 32px rgba(0,0,0,.18), 0 1px 4px rgba(0,0,0,.12);
}
.gif-render-status {
  padding: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  text-align: center;
  font-weight: 750;
  color: #555;
}
.gif-render-spinner {
  width: 16px;
  height: 16px;
  flex: 0 0 auto;
  border: 2px solid rgba(0,0,0,.14);
  border-top-color: #555;
  border-radius: 50%;
  animation: gif-spin .8s linear infinite;
}
@keyframes gif-spin {
  to { transform: rotate(360deg); }
}
.gif-progress { height: 8px; border-radius: 999px; overflow: hidden; background: #dededa; }
.gif-progress span { display: block; width: 0%; height: 100%; background: #111; transition: width .08s linear; }
.gif-meta { min-height: 1.4em; margin: 0; color: #666; font-size: 14px; text-align: center; }
.gif-card .finish-actions { position: sticky; bottom: 0; z-index: 2; padding-top: 8px; padding-bottom: max(2px, env(safe-area-inset-bottom)); background: #f4f4f1; }

@media (max-width: 520px), (max-height: 520px) {
  .gif-dialog { width: min(96vw, 760px); max-height: 96dvh; }
  .gif-card { max-height: 96dvh; padding: 12px; gap: 9px; }
  .gif-preview-wrap {
    min-height: min(44dvh, 340px);
    padding: 22px 14px 14px;
    border-radius: 12px;
  }
  .gif-preview-label {
    top: 8px;
    left: 8px;
  }
  .gif-preview-wrap img { max-height: 44dvh; }
  .gif-card .finish-actions .utility { flex: 1 1 130px; }
}

/* Explicitly preserve hidden state against component display rules. */
.gif-preview-wrap img[hidden],
.gif-render-status[hidden] {
  display: none !important;
}
