: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 { 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; }
.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; }
.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; }
.color-picker-row { display: flex; align-items: center; justify-content: space-between; gap: 14px; font-weight: 700; margin-bottom: 12px; }
.color-picker-row input[type="color"] { width: 72px; height: 44px; padding: 2px; border: 1px solid #aaa; border-radius: 9px; background: #fff; }
.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 { 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); }
.finish-dialog::backdrop, .settings-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; }

@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 { padding: 18px; display: grid; gap: 12px; background: #f4f4f1; }
.gif-preview-wrap { min-height: min(58dvh, 480px); background: #fff; border: 1px solid var(--line); overflow: hidden; display: grid; place-items: center; }
.gif-preview-wrap img { display: block; max-width: 100%; max-height: 58dvh; object-fit: contain; }
.gif-render-status { padding: 28px; text-align: center; font-weight: 750; color: #555; }
.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; }
