/*
 * CellSkin case designer.
 * Everything is scoped under #cellskin-customizer so it cannot collide with theme CSS,
 * and all sizing is intrinsic so it survives whatever container the theme drops it into.
 */

#cellskin-customizer {
  --cs-accent: #667eea;
  --cs-accent-dark: #5568d3;
  --cs-ink: #1a1a1a;
  --cs-muted: #8a8f98;
  --cs-line: #e6e8eb;
  --cs-bg: #fafbfc;
  --cs-radius: 12px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  color: var(--cs-ink);
  -webkit-font-smoothing: antialiased;
  box-sizing: border-box;
}
#cellskin-customizer *,
#cellskin-customizer *::before,
#cellskin-customizer *::after { box-sizing: border-box; }

#cellskin-customizer button { font-family: inherit; }

.cs-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 380px;
  grid-template-rows: auto minmax(0, 1fr);
  grid-template-areas:
    "stage topbar"
    "stage panel";
  gap: 12px 28px;
  align-items: start;
  max-width: 1240px;
  margin: 0 auto;
  padding: 0 16px;
}

/* ---------- top bar ---------- */
.cs-topbar {
  grid-area: topbar;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--cs-line);
  border-radius: var(--cs-radius);
  background: #fff;
}
.cs-back {
  flex: 0 0 auto;
  display: none;
  align-items: center;
  justify-content: center;
  width: 38px; height: 38px;
  border: none;
  border-radius: 50%;
  background: var(--cs-bg);
  color: #495057;
  cursor: pointer;
}
.cs-back:hover { background: #eef0f4; color: var(--cs-ink); }
.cs-back svg { width: 21px; height: 21px; fill: currentColor; }

.cs-topbar-text { flex: 1; min-width: 0; }
.cs-topbar-title {
  font-size: 14px; font-weight: 700; line-height: 1.25;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.cs-topbar-sub {
  display: flex; align-items: baseline; gap: 8px;
  font-size: 12px; color: var(--cs-muted); margin-top: 1px;
  white-space: nowrap; overflow: hidden;
}
.cs-price { font-weight: 700; color: var(--cs-ink); }

/* ---------- stage ---------- */
.cs-stage-col { grid-area: stage; position: sticky; top: 16px; }
.cs-stage-wrap { position: relative; max-width: 460px; margin: 0 auto; }

/* Controls that belong to the case sit on it, so nothing competes with it for room. */
.cs-float { position: absolute; z-index: 5; display: flex; gap: 6px; }
.cs-float-tl { top: 10px; left: 10px; }
.cs-float-tr { top: 10px; right: 10px; }

.cs-objbar {
  position: absolute;
  z-index: 5;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%) translateY(6px);
  display: flex;
  gap: 6px;
  padding: 5px;
  border-radius: 999px;
  background: rgba(255, 255, 255, .94);
  box-shadow: 0 4px 16px rgba(0, 0, 0, .16);
  backdrop-filter: blur(6px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .14s, transform .14s;
}
.cs-objbar.cs-on { opacity: 1; pointer-events: auto; transform: translateX(-50%) translateY(0); }

.cs-ico {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px; height: 36px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, .94);
  box-shadow: 0 2px 8px rgba(0, 0, 0, .14);
  color: #40454c;
  cursor: pointer;
  transition: color .15s, background .15s, opacity .15s;
}
.cs-objbar .cs-ico { box-shadow: none; background: none; }
.cs-ico svg { width: 19px; height: 19px; fill: currentColor; }
.cs-ico:hover:not(:disabled) { color: var(--cs-accent); }
.cs-ico:disabled { opacity: .35; cursor: default; }
.cs-ico-danger:hover:not(:disabled) { color: #e03131; }
.cs-ico-text { width: auto; padding: 0 10px; border-radius: 999px; font-size: 11.5px; font-weight: 700; font-variant-numeric: tabular-nums; }

/*
 * The viewport is the window onto the case; the frame inside it is grown in real pixels as
 * the customer zooms, so anything past the edges is reached by scrolling this box.
 */
.cs-stage-viewport {
  width: 100%;
  /* Set from the case's proportions; a variable rather than an inline style so the phone
     layout below can still swap it for a fixed height. */
  aspect-ratio: var(--cs-ratio, 1);
  max-height: 78vh;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
  overscroll-behavior: contain;
  scrollbar-width: none;
  touch-action: none;
}
.cs-stage-viewport::-webkit-scrollbar { display: none; }
/* Centring a box that overflows its scroll container makes the top-left unreachable. */
.cs-stage-viewport.cs-zoomed { align-items: flex-start; justify-content: flex-start; }

.cs-stage-frame {
  position: relative;
  flex: 0 0 auto;
  /* Blend modes must not reach through to the theme's background. */
  isolation: isolate;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}
.cs-stage-frame img { display: block; max-width: 100%; }

.cs-layer { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.cs-layer-base { z-index: 1; }
.cs-art { position: absolute; z-index: 2; overflow: hidden; }
.cs-layer-shading { z-index: 3; mix-blend-mode: multiply; }
.cs-layer-overlay { z-index: 4; }
.cs-layer-overlay.cs-blend-screen { mix-blend-mode: screen; }

/* The artwork is clipped to the exact case shape. */
.cs-art { -webkit-mask-repeat: no-repeat; mask-repeat: no-repeat; -webkit-mask-size: 100% 100%; mask-size: 100% 100%; }
.cs-art canvas { display: block; }
.cs-art .canvas-container { position: absolute !important; top: 0; left: 0; }

/* ---------- panel ---------- */
.cs-panel { grid-area: panel; border: 1px solid var(--cs-line); border-radius: var(--cs-radius); overflow: hidden; background: #fff; }

.cs-add {
  flex: 0 0 auto;
  border: none;
  border-radius: 999px;
  background: var(--cs-accent);
  color: #fff;
  padding: 11px 20px;
  font-size: 13.5px;
  font-weight: 700;
  white-space: nowrap;
  cursor: pointer;
  transition: background .15s;
}
.cs-add:hover:not(:disabled) { background: var(--cs-accent-dark); }
.cs-add:disabled { background: #adb5bd; cursor: default; }

/* ---------- tool strip ---------- */
.cs-dock { border-top: 1px solid var(--cs-line); background: #fff; }
.cs-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 8px;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}
.cs-tabs::-webkit-scrollbar { display: none; }
.cs-tab {
  position: relative;
  flex: 0 0 auto;
  width: 68px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  padding: 9px 4px;
  border: 1px solid transparent;
  border-radius: 11px;
  background: none;
  cursor: pointer;
  color: #6b7280;
  font-size: 11px;
  font-weight: 600;
  transition: all .15s;
}
.cs-tab:hover { color: var(--cs-accent); }
.cs-tab.cs-on { background: var(--cs-bg); border-color: var(--cs-line); color: var(--cs-accent); }
.cs-tab-ico { width: 22px; height: 22px; fill: currentColor; }
.cs-tab-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.cs-tab-dot { position: absolute; top: 6px; right: 12px; width: 6px; height: 6px; border-radius: 50%; background: var(--cs-accent); }

.cs-sheet-head { display: none; }
.cs-sheet-body { padding: 16px; }
.cs-msgwrap { padding: 0 16px; }

.cs-field { margin-bottom: 14px; }
.cs-field:last-child { margin-bottom: 0; }
.cs-field-label { display: block; font-size: 11.5px; font-weight: 600; color: #6b7280; margin-bottom: 6px; }

.cs-input, .cs-select, .cs-textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--cs-line);
  border-radius: 9px;
  font-size: 13.5px;
  font-family: inherit;
  color: var(--cs-ink);
  background: #fff;
}
.cs-input:focus, .cs-select:focus, .cs-textarea:focus { outline: none; border-color: var(--cs-accent); }
.cs-textarea { resize: vertical; min-height: 64px; }

.cs-chips { display: flex; flex-wrap: wrap; gap: 7px; }
.cs-chip {
  border: 1px solid var(--cs-line);
  background: #fff;
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 12.5px;
  font-weight: 600;
  color: #495057;
  cursor: pointer;
  transition: all .15s;
}
.cs-chip:hover { border-color: var(--cs-accent); }
.cs-chip.cs-on { background: var(--cs-ink); border-color: var(--cs-ink); color: #fff; }

.cs-swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.cs-swatch {
  width: 32px; height: 32px;
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px var(--cs-line);
  cursor: pointer;
  padding: 0;
  transition: transform .12s;
}
.cs-swatch:hover { transform: scale(1.12); }
.cs-swatch.cs-on { box-shadow: 0 0 0 2px var(--cs-accent); }

.cs-btn {
  border: 1px solid var(--cs-line);
  background: #fff;
  border-radius: 9px;
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 600;
  color: #495057;
  cursor: pointer;
  transition: all .15s;
}
.cs-btn:hover:not(:disabled) { border-color: var(--cs-accent); color: var(--cs-accent); }
.cs-btn:disabled { opacity: .4; cursor: default; }
.cs-btn-primary { background: var(--cs-accent); border-color: var(--cs-accent); color: #fff; }
.cs-btn-primary:hover:not(:disabled) { background: var(--cs-accent-dark); border-color: var(--cs-accent-dark); color: #fff; }
.cs-btn-block { width: 100%; }
.cs-btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

.cs-upload {
  display: block;
  border: 2px dashed var(--cs-line);
  border-radius: 10px;
  padding: 22px 14px;
  text-align: center;
  cursor: pointer;
  color: var(--cs-muted);
  font-size: 13px;
  transition: all .15s;
}
.cs-upload:hover { border-color: var(--cs-accent); color: var(--cs-accent); background: #f7f8ff; }
.cs-upload input { display: none; }

.cs-thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(62px, 1fr)); gap: 8px; margin-top: 12px; }
.cs-thumb { position: relative; aspect-ratio: 1; border-radius: 8px; overflow: hidden; border: 1px solid var(--cs-line); cursor: pointer; background: #f1f3f5; }
.cs-thumb img { width: 100%; height: 100%; object-fit: cover; }
.cs-thumb.cs-on { border-color: var(--cs-accent); box-shadow: 0 0 0 1px var(--cs-accent); }
.cs-thumb-spin { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(255,255,255,.72); font-size: 10px; font-weight: 700; color: var(--cs-accent); }

.cs-shapes { display: grid; grid-template-columns: repeat(auto-fill, minmax(52px, 1fr)); gap: 8px; }
.cs-shape {
  aspect-ratio: 1;
  display: flex; align-items: center; justify-content: center;
  border: 1px solid var(--cs-line);
  border-radius: 10px;
  background: #fff;
  cursor: pointer;
  padding: 10px;
  transition: all .15s;
}
.cs-shape:hover { border-color: var(--cs-accent); background: #f8f9ff; transform: translateY(-1px); }
.cs-shape svg { width: 100%; height: 100%; fill: #495057; }
.cs-shape:hover svg { fill: var(--cs-accent); }

.cs-range { width: 100%; accent-color: var(--cs-accent); }
.cs-row { display: flex; gap: 10px; align-items: center; }
.cs-row > * { min-width: 0; }

.cs-note { font-size: 11.5px; color: var(--cs-muted); line-height: 1.5; margin-top: 8px; }
.cs-msg { font-size: 12.5px; padding: 10px 12px; border-radius: 9px; margin-top: 12px; display: none; }
.cs-msg.cs-show { display: block; }
.cs-msg-err { background: #fff5f5; color: #c92a2a; border: 1px solid #ffc9c9; }
.cs-msg-ok { background: #ebfbee; color: #2b8a3e; border: 1px solid #b2f2bb; }
.cs-msg-info { background: #e7f5ff; color: #1864ab; border: 1px solid #a5d8ff; }

.cs-loading { text-align: center; padding: 70px 20px; color: var(--cs-muted); font-size: 14px; }
.cs-spinner {
  width: 30px; height: 30px; margin: 0 auto 14px;
  border: 3px solid var(--cs-line); border-top-color: var(--cs-accent);
  border-radius: 50%; animation: cs-spin .8s linear infinite;
}
@keyframes cs-spin { to { transform: rotate(360deg); } }

.cs-busy { position: fixed; inset: 0; background: rgba(255,255,255,.9); z-index: 99999; display: flex; align-items: center; justify-content: center; flex-direction: column; }
.cs-busy-text { margin-top: 14px; font-size: 14px; font-weight: 600; color: var(--cs-ink); }
.cs-busy-bar { width: 220px; height: 6px; background: var(--cs-line); border-radius: 3px; margin-top: 14px; overflow: hidden; }
.cs-busy-fill { height: 100%; width: 0; background: var(--cs-accent); transition: width .3s; }

.cs-done { text-align: center; padding: 26px 18px; }
.cs-done img { max-width: 240px; border-radius: 12px; margin: 0 auto 16px; }
.cs-code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 18px; font-weight: 700; letter-spacing: .5px; }

/*
 * ---------- phone ----------
 *
 * Most customers design on a phone, so the case gets the top of the screen and stays there
 * while the tools dock to the bottom: pick a tool from the strip, its options open just
 * above it, and the case never leaves view. The middle scrolls between the two.
 */
@media (max-width: 900px) {
  /* The designer owns the screen: theme header, page copy and footer are all behind it. */
  #cellskin-customizer.cs-fullscreen {
    position: fixed;
    inset: 0;
    z-index: 2147483000;
    background: #fff;
  }

  .cs-shell {
    display: flex;
    flex-direction: column;
    /* The desktop grid aligns its columns to the start; carried into a column flex layout
       that stops the rows filling the width and lets them overflow the screen. */
    align-items: stretch;
    height: 100vh;
    height: 100dvh;
    gap: 0;
    padding: 0;
    max-width: none;
  }

  /* Flex items default to min-width:auto, so the sideways-scrolling rows inside would
     otherwise push the whole editor wider than the screen. */
  .cs-topbar, .cs-stage-col, .cs-panel, .cs-tabs { min-width: 0; }

  .cs-topbar {
    flex: 0 0 auto;
    gap: 10px;
    padding: 8px 10px;
    padding-top: calc(8px + env(safe-area-inset-top));
    border: none;
    border-bottom: 1px solid var(--cs-line);
    border-radius: 0;
  }
  .cs-back { display: flex; }
  .cs-topbar-title { font-size: 13.5px; }

  .cs-stage-col {
    position: static;
    flex: 1 1 auto;
    min-height: 0;
    padding: 0;
  }
  .cs-stage-wrap { max-width: none; height: 100%; }
  .cs-stage-viewport {
    height: 100%;
    max-height: none;
    aspect-ratio: auto;
  }

  .cs-panel {
    flex: 0 0 auto;
    /* The case keeps the bigger share of the screen whatever tool is open; a tall set of
       options scrolls inside the sheet rather than pushing the case down. */
    max-height: 40%;
    display: flex;
    flex-direction: column;
    border: none;
    border-radius: 0;
    border-top: 1px solid var(--cs-line);
  }
  /* Only this middle strip scrolls, so the case and the tool bar never leave the screen. */
  .cs-sheet {
    order: 1;
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }
  .cs-msgwrap { order: 2; flex: 0 0 auto; }
  .cs-dock { order: 3; flex: 0 0 auto; padding-bottom: env(safe-area-inset-bottom); }
  .cs-tabs { flex-wrap: nowrap; }
  .cs-tab { width: 64px; }

  .cs-sheet-head {
    display: block;
    padding: 13px 16px 0;
    font-size: 13px;
    font-weight: 700;
    color: var(--cs-ink);
  }
  .cs-sheet-body { padding: 11px 16px 18px; }
}

/* Nothing behind the full-screen editor should scroll under it. */
html.cs-locked, html.cs-locked body { overflow: hidden; }
