:root {
  --c-bg: #F5F5F5;
  --c-surface: #FFFFFF;
  --c-border: #E5E5E5;
  --c-border-strong: #D4D4D4;
  --c-text: #1E1E1E;
  --c-muted: #8A8A8A;
  --c-accent: #0D99FF;
  --c-accent-soft: #E5F4FF;
  --c-danger: #F24822;

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter", "Hiragino Sans", sans-serif;
  --mono: "SF Mono", Menlo, Consolas, monospace;

  --topbar-h: 48px;
  --panel-w: 256px;
  --radius: 6px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  font-size: 13px;
  color: var(--c-text);
  background: var(--c-bg);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; cursor: pointer; }

/* ─── TOPBAR ───────────────────────── */
.topbar {
  height: var(--topbar-h);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 0 12px;
  background: var(--c-surface);
  border-bottom: 1px solid var(--c-border);
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
}
.logo {
  font-weight: 800;
  letter-spacing: 0.06em;
  font-size: 13px;
  font-family: var(--mono);
}

.size-control {
  display: flex;
  align-items: center;
  gap: 6px;
  padding-left: 14px;
  margin-left: 4px;
  border-left: 1px solid var(--c-border);
  height: 28px;
}
.size-control select {
  height: 28px;
  padding: 0 22px 0 8px;
  border: 1px solid var(--c-border);
  border-radius: 4px;
  background: var(--c-surface);
  font: inherit;
  font-size: 12px;
  color: var(--c-text);
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 10 10'><path d='M2 4l3 3 3-3' stroke='%238A8A8A' stroke-width='1.4' fill='none' stroke-linecap='round' stroke-linejoin='round'/></svg>");
  background-repeat: no-repeat;
  background-position: right 7px center;
}
.size-control select:hover { border-color: var(--c-border-strong); }
.size-control select:focus { outline: 2px solid var(--c-accent); outline-offset: 1px; }
.size-fields { display: flex; gap: 4px; }
.size-fields .prop-field { width: 68px; height: 28px; }

.toolbar {
  display: flex;
  align-items: center;
  gap: 2px;
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  padding: 2px;
}
.tool-sep {
  width: 1px;
  height: 20px;
  background: var(--c-border);
  margin: 0 4px;
}
.tool {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 0;
  border-radius: 4px;
  color: var(--c-text);
  transition: background 120ms;
}
.tool:hover { background: var(--c-bg); }
.tool.is-active {
  background: var(--c-accent);
  color: #fff;
}
.tool:focus-visible { outline: 2px solid var(--c-accent); outline-offset: 1px; }

.topbar-right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
}
.icon-btn {
  width: 32px;
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  color: var(--c-text);
  transition: 120ms;
}
.icon-btn:hover:not(:disabled) { background: var(--c-bg); }
.icon-btn:disabled { color: #C5C5C5; cursor: not-allowed; }
.icon-btn:focus-visible { outline: 2px solid var(--c-accent); outline-offset: 1px; }
.btn-primary, .btn-ghost {
  height: 32px;
  padding: 0 14px;
  border-radius: var(--radius);
  font-weight: 500;
  font-size: 12px;
  border: 1px solid transparent;
  transition: 120ms;
}
.btn-primary {
  background: var(--c-accent);
  color: #fff;
  border-color: var(--c-accent);
}
.btn-primary:hover { filter: brightness(0.95); }
.btn-ghost {
  background: var(--c-surface);
  color: var(--c-text);
  border-color: var(--c-border);
}
.btn-ghost:hover { background: var(--c-bg); }

/* ─── WORKSPACE ───────────────────────── */
.workspace {
  display: grid;
  grid-template-columns: var(--panel-w) 1fr var(--panel-w);
  height: calc(100vh - var(--topbar-h));
}
.panel { background: var(--c-surface); overflow-y: auto; }
.panel-left { border-right: 1px solid var(--c-border); }
.panel-right { border-left: 1px solid var(--c-border); }

.panel-head {
  position: sticky;
  top: 0;
  padding: 12px 14px;
  background: var(--c-surface);
  border-bottom: 1px solid var(--c-border);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--c-muted);
  z-index: 1;
}

/* ─── LAYERS ───────────────────────── */
.layer-list { list-style: none; margin: 0; padding: 6px; }
.layer-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 4px 4px 8px;
  border-radius: 4px;
  cursor: pointer;
  user-select: none;
  font-size: 12px;
  min-height: 28px;
}
.layer-item:hover { background: var(--c-bg); }
.layer-item.is-selected {
  background: var(--c-accent-soft);
  color: var(--c-accent);
}
.layer-item .layer-icon {
  width: 14px;
  height: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.layer-item .layer-name {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.layer-actions { display: none; gap: 1px; flex-shrink: 0; }
.layer-item:hover .layer-actions,
.layer-item.is-selected .layer-actions { display: flex; }
.layer-action {
  width: 20px;
  height: 20px;
  border: 0;
  background: transparent;
  border-radius: 3px;
  color: inherit;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
}
.layer-action:hover { background: rgba(0,0,0,.08); }
.layer-action:disabled { opacity: 0.3; cursor: not-allowed; }
.layer-action:disabled:hover { background: transparent; }
.layer-empty {
  padding: 12px 14px;
  font-size: 12px;
  color: var(--c-muted);
}

/* ─── CANVAS AREA ───────────────────────── */
.canvas-area {
  position: relative;
  background:
    radial-gradient(circle, #DCDCDC 1px, transparent 1px) 0 0 / 16px 16px,
    var(--c-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
  padding: 60px;
}
.canvas-outer {
  position: relative;
  flex-shrink: 0;
  margin: auto;
}
.canvas-wrap {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
}
.canvas {
  position: relative;
  width: var(--cw, 1920px);
  height: var(--ch, 1080px);
  background: #fff;
  box-shadow: 0 1px 0 rgba(0,0,0,.04), 0 8px 32px -8px rgba(0,0,0,.10);
  outline: none;
  overflow: hidden;
}
.canvas.tool-create { cursor: crosshair; }

.canvas-label {
  position: absolute;
  top: -22px;
  left: 0;
  font-size: 11px;
  color: var(--c-muted);
  font-weight: 500;
  pointer-events: none;
}

/* ─── ZOOM ───────────────────────── */
.zoom-control {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 4px;
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  box-shadow: 0 4px 12px -4px rgba(0,0,0,.12);
  z-index: 50;
}
.zoom-btn, .zoom-level {
  height: 28px;
  border: 0;
  background: transparent;
  border-radius: 4px;
  font: inherit;
  font-size: 13px;
  color: var(--c-text);
}
.zoom-btn { width: 28px; font-size: 16px; line-height: 1; }
.zoom-level { min-width: 56px; padding: 0 8px; font-variant-numeric: tabular-nums; font-size: 12px; }
.zoom-btn:hover, .zoom-level:hover { background: var(--c-bg); }
.zoom-btn:focus-visible, .zoom-level:focus-visible { outline: 2px solid var(--c-accent); outline-offset: 1px; }

/* ─── CANVAS ELEMENTS ───────────────────────── */
.el {
  position: absolute;
  user-select: none;
  cursor: move;
}
.el.is-selected { outline: 2px solid var(--c-accent); }
.el-text {
  font-family: var(--font);
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.4;
  display: flex;
  align-items: center;
}
.el-text .text-content { width: 100%; outline: none; }
.el-text.is-editing { cursor: text; outline: 2px solid var(--c-accent); }
.el-text.is-editing .text-content { cursor: text; caret-color: var(--c-accent); }
.el-image {
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}
.shape-inner {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.marquee {
  position: absolute;
  background: rgba(13, 153, 255, 0.10);
  border: 1px solid var(--c-accent);
  pointer-events: none;
  z-index: 100;
}

/* resize handles */
.handle {
  position: absolute;
  background: #fff;
  border: 1.5px solid var(--c-accent);
  z-index: 10;
}
.handle.corner {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  box-shadow: 0 1px 3px rgba(0,0,0,.15);
}
.handle.edge {
  background: var(--c-accent);
  border: 1.5px solid #fff;
  border-radius: 3px;
}
.handle.h-nw { top: -6px; left: -6px; cursor: nwse-resize; }
.handle.h-ne { top: -6px; right: -6px; cursor: nesw-resize; }
.handle.h-sw { bottom: -6px; left: -6px; cursor: nesw-resize; }
.handle.h-se { bottom: -6px; right: -6px; cursor: nwse-resize; }
.handle.h-n { top: -4px; left: 50%; width: 24px; height: 7px; margin-left: -12px; cursor: ns-resize; }
.handle.h-s { bottom: -4px; left: 50%; width: 24px; height: 7px; margin-left: -12px; cursor: ns-resize; }
.handle.h-e { right: -4px; top: 50%; width: 7px; height: 24px; margin-top: -12px; cursor: ew-resize; }
.handle.h-w { left: -4px; top: 50%; width: 7px; height: 24px; margin-top: -12px; cursor: ew-resize; }

/* ─── PROPERTIES ───────────────────────── */
.props { padding: 12px 14px; }
.props .empty {
  margin: 0;
  color: var(--c-muted);
  font-size: 12px;
  line-height: 1.5;
}
.prop-group { margin-bottom: 16px; }
.prop-group + .prop-group {
  border-top: 1px solid var(--c-border);
  padding-top: 14px;
}
.prop-label {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--c-muted);
  margin: 0 0 8px;
}
.prop-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 6px;
}
.prop-row.full { grid-template-columns: 1fr; }
.prop-field {
  display: flex;
  align-items: center;
  background: var(--c-bg);
  border: 1px solid transparent;
  border-radius: 4px;
  padding: 0 6px;
  height: 28px;
  transition: 120ms;
}
.prop-field:hover { background: #ECECEC; }
.prop-field:focus-within {
  background: #fff;
  border-color: var(--c-accent);
}
.prop-field label {
  font-size: 10px;
  color: var(--c-muted);
  font-weight: 600;
  margin-right: 4px;
  text-transform: uppercase;
  width: 12px;
}
.prop-field input {
  flex: 1;
  width: 100%;
  border: 0;
  background: transparent;
  font: inherit;
  font-size: 12px;
  color: var(--c-text);
  outline: none;
  min-width: 0;
}
.prop-field input[type="color"] {
  width: 22px;
  height: 22px;
  padding: 0;
  border: 0;
  border-radius: 3px;
  cursor: pointer;
  background: transparent;
  flex: 0 0 auto;
}
.prop-field select {
  flex: 1;
  width: 100%;
  border: 0;
  background: transparent;
  font: inherit;
  font-size: 12px;
  color: var(--c-text);
  outline: none;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 10 10'><path d='M2 4l3 3 3-3' stroke='%238A8A8A' stroke-width='1.4' fill='none' stroke-linecap='round' stroke-linejoin='round'/></svg>");
  background-repeat: no-repeat;
  background-position: right 0 center;
  padding-right: 14px;
}
.prop-textarea {
  width: 100%;
  min-height: 60px;
  padding: 8px;
  background: var(--c-bg);
  border: 1px solid transparent;
  border-radius: 4px;
  font: inherit;
  font-size: 12px;
  color: var(--c-text);
  resize: vertical;
  outline: none;
}
.prop-textarea:focus { background: #fff; border-color: var(--c-accent); }

.prop-delete {
  width: 100%;
  height: 32px;
  background: #fff;
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  color: var(--c-danger);
  font-weight: 500;
  font-size: 12px;
  margin-top: 4px;
  transition: 120ms;
}
.prop-delete:hover { background: #FEF1EF; border-color: var(--c-danger); }

.prop-replace {
  width: 100%;
  height: 32px;
  background: var(--c-bg);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  color: var(--c-text);
  font-weight: 500;
  font-size: 12px;
  transition: 120ms;
}
.prop-replace:hover { background: #ECECEC; }

/* style toggle (italic / bold etc) */
.style-row {
  display: flex;
  gap: 4px;
}
.style-btn {
  width: 32px;
  height: 28px;
  border: 1px solid var(--c-border);
  background: #fff;
  border-radius: 4px;
  font-weight: 700;
  font-size: 13px;
  color: var(--c-text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.style-btn.italic { font-style: italic; font-family: Georgia, serif; }
.style-btn:hover { background: var(--c-bg); }
.style-btn.is-active {
  background: var(--c-text);
  color: #fff;
  border-color: var(--c-text);
}

/* alignment grid */
.align-stack {
  display: flex;
  gap: 6px;
}
.align-group {
  display: flex;
  gap: 2px;
  background: var(--c-bg);
  padding: 2px;
  border-radius: 4px;
}
.align-btn {
  width: 26px;
  height: 24px;
  border: 0;
  background: transparent;
  border-radius: 3px;
  color: var(--c-muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.align-btn:hover { background: rgba(0,0,0,.05); color: var(--c-text); }
.align-btn.is-active {
  background: var(--c-surface);
  color: var(--c-text);
  box-shadow: 0 1px 2px rgba(0,0,0,.06);
}

/* toggle switch */
.band-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}
.switch {
  position: relative;
  display: inline-block;
  width: 28px;
  height: 16px;
}
.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}
.switch span {
  position: absolute;
  inset: 0;
  background: #D4D4D4;
  border-radius: 999px;
  transition: 150ms;
  cursor: pointer;
}
.switch span::before {
  content: "";
  position: absolute;
  width: 12px;
  height: 12px;
  left: 2px;
  top: 2px;
  background: #fff;
  border-radius: 50%;
  transition: 150ms;
}
.switch input:checked + span { background: var(--c-accent); }
.switch input:checked + span::before { transform: translateX(12px); }

/* ─── PAINT CONTROL ───────────────────────── */
.paint-tabs {
  display: flex;
  gap: 2px;
  background: var(--c-bg);
  padding: 2px;
  border-radius: 5px;
  margin-bottom: 8px;
}
.paint-tab {
  flex: 1;
  height: 24px;
  border: 0;
  background: transparent;
  border-radius: 3px;
  font-size: 11px;
  font-weight: 500;
  color: var(--c-muted);
}
.paint-tab.is-active {
  background: var(--c-surface);
  color: var(--c-text);
  box-shadow: 0 1px 2px rgba(0,0,0,.06);
}
.grad-presets {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 3px;
  margin-top: 6px;
}
.grad-preset {
  height: 22px;
  border: 1px solid var(--c-border);
  background: #fff;
  border-radius: 3px;
  font-size: 11px;
  color: var(--c-muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.grad-preset:hover { background: var(--c-bg); color: var(--c-text); }
.grad-preset.is-active {
  background: var(--c-accent);
  color: #fff;
  border-color: var(--c-accent);
}
.paint-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 6px;
}
.paint-swatch {
  width: 22px;
  height: 22px;
  border-radius: 4px;
  border: 1px solid var(--c-border);
  flex-shrink: 0;
  position: relative;
  overflow: hidden;
  cursor: pointer;
}
.paint-swatch input {
  position: absolute;
  inset: -4px;
  width: calc(100% + 8px);
  height: calc(100% + 8px);
  border: 0;
  padding: 0;
  cursor: pointer;
  opacity: 0;
}

/* ─── MODAL ───────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(30,30,30,.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 24px;
}
.modal[hidden] { display: none; }
.modal-card {
  width: min(820px, 100%);
  max-height: calc(100vh - 48px);
  background: var(--c-surface);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 24px 64px -16px rgba(0,0,0,.3);
}
.modal-head, .modal-tabs, .palette-section, .modal-foot { flex-shrink: 0; }
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--c-border);
}
.modal-head h2 { margin: 0; font-size: 14px; font-weight: 600; }
.modal-close {
  width: 28px;
  height: 28px;
  border: 0;
  background: transparent;
  border-radius: 4px;
  font-size: 22px;
  line-height: 1;
  color: var(--c-muted);
}
.modal-close:hover { background: var(--c-bg); }
.modal-card-wide { width: min(960px, 100%); }
.modal-tabs {
  display: flex;
  gap: 4px;
  padding: 8px 12px 0;
  border-bottom: 1px solid var(--c-border);
}
.tab {
  padding: 8px 14px;
  background: transparent;
  border: 0;
  border-bottom: 2px solid transparent;
  font-size: 12px;
  color: var(--c-muted);
  font-weight: 500;
  margin-bottom: -1px;
}
.tab:hover { color: var(--c-text); }
.tab.is-active {
  color: var(--c-text);
  border-bottom-color: var(--c-accent);
}
.code-out {
  flex: 1 1 auto;
  min-height: 0;
  margin: 0;
  padding: 18px;
  background: #FAFAFA;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.6;
  color: var(--c-text);
  overflow: auto;
  white-space: pre;
}

/* templates modal */
.palette-section {
  padding: 14px 18px 8px;
  border-bottom: 1px solid var(--c-border);
}
.palette-label {
  margin: 0 0 8px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--c-muted);
}
.palette-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.palette-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border: 1px solid var(--c-border);
  border-radius: 999px;
  background: #fff;
  font-size: 12px;
  font-weight: 500;
  color: var(--c-text);
}
.palette-chip:hover { border-color: var(--c-border-strong); }
.palette-chip.is-active {
  background: var(--c-text);
  color: #fff;
  border-color: var(--c-text);
}
.palette-chip-swatches {
  display: inline-flex;
  gap: 2px;
}
.palette-chip-swatches span {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
}
.templates-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
}
.templates-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
  padding: 16px 18px 18px;
}
.template-card {
  border: 1px solid var(--c-border);
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  overflow: hidden;
  transition: 150ms;
}
.template-card:hover {
  border-color: var(--c-accent);
  transform: translateY(-2px);
  box-shadow: 0 8px 24px -12px rgba(0,0,0,.15);
}
.template-thumb {
  position: relative;
  width: 100%;
  padding-bottom: 56.25%; /* 16:9 — padding-bottom hack で確実に高さ確保 */
  overflow: hidden;
  border-bottom: 1px solid var(--c-border);
  background: #fff;
}
.template-thumb svg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: block;
}
.template-name {
  padding: 10px 12px;
  font-size: 12px;
  font-weight: 600;
  color: var(--c-text);
}
.modal-foot {
  display: flex;
  justify-content: flex-end;
  padding: 12px 18px;
  border-top: 1px solid var(--c-border);
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0.01ms !important; }
}
