.editor{
  flex: 1;
  display: grid;
  grid-template-columns: 76px 1fr 300px;
  grid-template-rows: 1fr auto;
  grid-template-areas:
    "toolbar canvas props"
    "toolbar exportbar props";
  height: calc(100vh - 57px);
  overflow: hidden;
  background: var(--bg-secondary);
}

/* ---------------- Toolbar (left rail) ---------------- */
.toolbar{
  grid-area: toolbar;
  background: var(--surface);
  border-inline-end: 1px solid var(--border);
  display: flex; flex-direction: column;
  align-items: center;
  padding: 12px 8px;
  gap: 4px;
  overflow-y: auto;
}
.tool-group{ display: flex; flex-direction: column; gap: 4px; width: 100%; align-items: center; }
.tool-group + .tool-group{ margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--border); }
.tool-btn{
  width: 56px; height: 52px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  background: transparent;
  cursor: pointer;
  color: var(--text-secondary);
  transition: all var(--dur-fast) var(--ease);
}
.tool-btn svg{ width: 19px; height: 19px; }
.tool-btn span{ font-size: 9.5px; font-weight: 600; }
.tool-btn:hover{ background: var(--bg-secondary); color: var(--text-primary); }
.tool-btn.active{ background: var(--accent-soft); color: var(--accent-strong); border-color: var(--accent-ring); }

/* ---------------- Canvas workspace ---------------- */
.canvas-workspace{
  grid-area: canvas;
  position: relative;
  overflow: auto;
  display: flex; align-items: center; justify-content: center;
  padding: 28px;
}
.canvas-stage{
  position: relative;
  box-shadow: var(--shadow-lg);
  border-radius: 4px;
  overflow: visible;
  touch-action: none;
}
.canvas-stage canvas{ display: block; border-radius: 4px; }
#mainCanvas{ position: relative; z-index: 1; }
#overlayCanvas{ position: absolute; inset: 0; z-index: 2; touch-action: none; pointer-events: none; }
#layerOverlay{ position: absolute; inset: 0; z-index: 3; pointer-events: none; }
#layerOverlay .layer-box{ pointer-events: auto; }
#cropOverlay{ position: absolute; inset: 0; z-index: 5; pointer-events: none; }
#cropOverlay .crop-box{ pointer-events: auto; }

.crop-box{
  position: absolute; z-index: 5;
  border: 1.5px solid #fff;
  box-shadow: 0 0 0 4000px hsl(224 30% 4% / .55);
  cursor: move;
}
.crop-box .grid-line{ position: absolute; background: hsl(0 0% 100% / .55); }
.crop-handle{
  position: absolute; width: 13px; height: 13px;
  background: #fff; border: 1.5px solid var(--accent-strong);
  border-radius: 3px;
  z-index: 6;
}
.crop-handle.nw{ top: -7px; left: -7px; cursor: nwse-resize; }
.crop-handle.ne{ top: -7px; right: -7px; cursor: nesw-resize; }
.crop-handle.sw{ bottom: -7px; left: -7px; cursor: nesw-resize; }
.crop-handle.se{ bottom: -7px; right: -7px; cursor: nwse-resize; }
.crop-handle.n{ top: -7px; left: calc(50% - 6px); cursor: ns-resize; }
.crop-handle.s{ bottom: -7px; left: calc(50% - 6px); cursor: ns-resize; }
.crop-handle.e{ right: -7px; top: calc(50% - 6px); cursor: ew-resize; }
.crop-handle.w{ left: -7px; top: calc(50% - 6px); cursor: ew-resize; }

.layer-box{
  position: absolute; z-index: 4;
  border: 1.5px dashed transparent;
  cursor: move;
}
.layer-box.selected{ border-color: var(--accent); }
.layer-handle{
  position: absolute; bottom: -8px; inset-inline-end: -8px;
  width: 13px; height: 13px; border-radius: 50%;
  background: var(--accent); border: 2px solid #fff;
  cursor: nwse-resize;
}
.layer-rotate-handle{
  position: absolute; top: -26px; left: calc(50% - 6px);
  width: 13px; height: 13px; border-radius: 50%;
  background: var(--accent); border: 2px solid #fff;
  cursor: grab;
}

/* Zoom / compare controls floating over canvas */
.canvas-toolbar{
  position: absolute; bottom: 16px; left: 50%; transform: translateX(-50%);
  z-index: 20;
  display: flex; align-items: center; gap: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px;
  box-shadow: var(--shadow-md);
}
.canvas-toolbar .icon-btn{ width: 32px; height: 32px; }
.canvas-toolbar .icon-btn svg{ width: 15px; height: 15px; }
.zoom-level{ font-size: 12px; font-weight: 700; min-width: 44px; text-align: center; color: var(--text-secondary); }

.compare-toggle-wrap{
  position: absolute; top: 16px; left: 50%; transform: translateX(-50%);
  z-index: 20;
}
.compare-slider-handle{
  position: absolute; top: 0; bottom: 0; width: 3px; background: #fff;
  box-shadow: 0 0 0 1px hsl(224 30% 4% / .3), var(--shadow-md);
  cursor: ew-resize; z-index: 10;
}
.compare-slider-handle::after{
  content: "";
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: 32px; height: 32px; border-radius: 50%;
  background: #fff url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23222' stroke-width='2'><path d='M8 8l-4 4 4 4M16 8l4 4-4 4'/></svg>") center/16px no-repeat;
  box-shadow: var(--shadow-md);
}
.compare-label{
  position: absolute; top: 10px; font-size: 11px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
  background: hsl(224 30% 4% / .55); color: #fff; padding: 4px 9px; border-radius: 999px; z-index: 9;
}
.compare-label.before{ left: 10px; }
.compare-label.after{ right: 10px; }

/* ---------------- Properties panel ---------------- */
.props-panel{
  grid-area: props;
  background: var(--surface);
  border-inline-start: 1px solid var(--border);
  overflow-y: auto;
  display: flex; flex-direction: column;
}
.props-header{
  padding: 16px 18px 10px;
  font-weight: 700; font-size: 14px;
  display: flex; align-items: center; justify-content: space-between;
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--surface); z-index: 2;
}
.props-body{ padding: 16px 18px 100px; display: flex; flex-direction: column; gap: 18px; }

.field{ display: flex; flex-direction: column; gap: 8px; }
.field-row{ display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.field-label{ font-size: 12.5px; font-weight: 600; color: var(--text-secondary); }
.field-value{ font-size: 12px; font-weight: 700; color: var(--text-primary); font-variant-numeric: tabular-nums; }

input[type="range"]{
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 5px; border-radius: 999px;
  background: var(--border);
  outline: none;
}
input[type="range"]::-webkit-slider-thumb{
  -webkit-appearance: none; appearance: none;
  width: 16px; height: 16px; border-radius: 50%;
  background: var(--accent); border: 2px solid var(--surface);
  box-shadow: 0 0 0 1px var(--accent);
  cursor: pointer; margin-top: -5.5px;
}
input[type="range"]::-moz-range-thumb{
  width: 16px; height: 16px; border-radius: 50%;
  background: var(--accent); border: 2px solid var(--surface);
  box-shadow: 0 0 0 1px var(--accent);
  cursor: pointer;
}
input[type="range"]::-moz-range-track{ height: 5px; border-radius: 999px; background: var(--border); }

input[type="text"], input[type="number"], select{
  width: 100%;
  padding: 9px 11px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 13px;
}
input[type="color"]{
  width: 36px; height: 32px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 2px; background: var(--bg-primary); cursor: pointer;
}

.seg-control{
  display: flex; background: var(--bg-secondary); border-radius: var(--radius-sm); padding: 3px; gap: 2px;
  flex-wrap: wrap;
}
.seg-control button{
  flex: 1; min-width: 40px;
  padding: 7px 6px; border-radius: 5px; border: none; background: transparent;
  font-size: 11.5px; font-weight: 700; cursor: pointer; color: var(--text-secondary);
}
.seg-control button.active{ background: var(--surface); color: var(--accent-strong); box-shadow: var(--shadow-sm); }

.chip-grid{ display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.chip{
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 8px 4px; text-align: center; font-size: 11px; font-weight: 700;
  background: var(--bg-primary); cursor: pointer; color: var(--text-secondary);
}
.chip.active{ border-color: var(--accent); color: var(--accent-strong); background: var(--accent-soft); }

.filter-grid{ display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.filter-item{ cursor: pointer; text-align: center; }
.filter-thumb{
  width: 100%; aspect-ratio: 1; border-radius: var(--radius-sm);
  border: 2px solid transparent; overflow: hidden; margin-bottom: 5px;
  background-size: cover; background-position: center;
}
.filter-item.active .filter-thumb{ border-color: var(--accent); }
.filter-item span{ font-size: 10.5px; font-weight: 600; color: var(--text-secondary); }

.check-row{ display: flex; align-items: center; justify-content: space-between; padding: 2px 0; }
.switch{ position: relative; width: 38px; height: 22px; flex-shrink: 0; }
.switch input{ opacity: 0; width: 0; height: 0; }
.switch-track{
  position: absolute; inset: 0; background: var(--border); border-radius: 999px;
  transition: background var(--dur-fast) var(--ease); cursor: pointer;
}
.switch-track::before{
  content: ""; position: absolute; width: 16px; height: 16px; border-radius: 50%;
  background: #fff; top: 3px; inset-inline-start: 3px;
  transition: transform var(--dur-fast) var(--ease);
  box-shadow: var(--shadow-sm);
}
.switch input:checked + .switch-track{ background: var(--accent); }
.switch input:checked + .switch-track::before{ transform: translateX(16px); }
[dir="rtl"] .switch input:checked + .switch-track::before{ transform: translateX(-16px); }

.layer-list{ display: flex; flex-direction: column; gap: 6px; }
.layer-list-item{
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px; border-radius: var(--radius-sm); border: 1px solid var(--border);
  background: var(--bg-primary); cursor: pointer; font-size: 12.5px;
}
.layer-list-item.selected{ border-color: var(--accent); background: var(--accent-soft); }
.layer-list-item .grow{ flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.layer-list-item button{ background: none; border: none; color: var(--text-tertiary); cursor: pointer; width: 22px; height: 22px; display: grid; place-items: center; border-radius: 5px; }
.layer-list-item button:hover{ background: var(--bg-secondary); color: var(--danger); }

.info-table{ width: 100%; border-collapse: collapse; font-size: 12.5px; }
.info-table tr{ border-bottom: 1px solid var(--border); }
.info-table tr:last-child{ border-bottom: none; }
.info-table td{ padding: 8px 0; }
.info-table td:first-child{ color: var(--text-tertiary); font-weight: 600; }
.info-table td:last-child{ text-align: end; font-weight: 700; }

.size-compare{ display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px; border-radius: var(--radius-md); background: var(--bg-secondary); }
.size-compare .from{ text-decoration: line-through; color: var(--text-tertiary); font-size: 13px; }
.size-compare .to{ color: var(--success); font-weight: 800; font-size: 15px; }
.size-compare .arrow{ color: var(--text-tertiary); }
.size-compare .pct{ font-size: 11px; font-weight: 700; color: var(--success); background: var(--success-soft); padding: 2px 7px; border-radius: 999px; }

.hint-text{ font-size: 11.5px; color: var(--text-tertiary); line-height: 1.5; }
.section-title{ font-size: 12px; font-weight: 800; text-transform: uppercase; letter-spacing: .04em; color: var(--text-tertiary); margin: 2px 0 -6px; }

/* ---------------- Export bar (bottom of canvas col) ---------------- */
.export-bar{
  grid-area: exportbar;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 10px 20px;
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  flex-wrap: wrap;
}
.export-bar-left{ display: flex; align-items: center; gap: 10px; font-size: 12px; color: var(--text-tertiary); }
.export-bar-right{ display: flex; align-items: center; gap: 8px; }
.history-count{ font-variant-numeric: tabular-nums; }

/* ---------------- Empty canvas state ---------------- */
.canvas-empty{
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  color: var(--text-tertiary);
}

/* ---------------- Responsive: tablet & mobile ---------------- */
@media (max-width: 980px){
  .editor{
    grid-template-columns: 1fr;
    grid-template-rows: 1fr auto auto;
    grid-template-areas:
      "canvas"
      "props"
      "toolbar";
    height: calc(100vh - 53px);
  }
  .toolbar{
    flex-direction: row;
    border-inline-end: none;
    border-top: 1px solid var(--border);
    padding: 6px 8px;
    overflow-x: auto; overflow-y: hidden;
    justify-content: flex-start;
  }
  .tool-group{ flex-direction: row; width: auto; }
  .tool-group + .tool-group{ margin-top: 0; margin-inline-start: 8px; padding-top: 0; border-top: none; border-inline-start: 1px solid var(--border); padding-inline-start: 8px; }
  .tool-btn{ width: 50px; height: 46px; flex-shrink: 0; }

  .props-panel{
    border-inline-start: none; border-top: 1px solid var(--border);
    max-height: 40vh;
  }
  .props-panel.collapsed .props-body{ display: none; }
  .props-header{ cursor: pointer; }

  .canvas-workspace{ padding: 14px; }
}

@media (max-width: 560px){
  .export-bar{ padding: 8px 12px; }
  .export-bar-left{ display: none; }
  .canvas-toolbar{ bottom: 10px; }
}
