/* ===== Base ===== */
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  background: #f6f7fb;
  color: #111;
}

/* ===== Top Bar ===== */
.topbar {
  position: sticky;
  top: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  background: #ffffff;
  border-bottom: 1px solid #e6e8f0;
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 10px;
  min-width: 0;
}
.brand strong { font-size: 16px; }

/* ===== Dropdown Menu (details/summary) ===== */
details.menu {
  position: relative;
  z-index: 10000;
}

details.menu > summary {
  list-style: none;
  cursor: pointer;
  user-select: none;
  font-size: 18px;
  line-height: 1;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid #e6e8f0;
  background: #fff;
}

details.menu > summary::-webkit-details-marker { display: none; }

details.menu[open] > summary {
  background: #f3f6ff;
  border-color: #cdd9ff;
}

details.menu .menuPanel {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 10001;
  width: min(260px, 80vw);
  background: #fff;
  border: 1px solid #e6e8f0;
  border-radius: 14px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.12);
  padding: 10px;
  display: grid;
  gap: 8px;
}

/* Prevent menu clipping */
header.topbar,
main.layout,
section.stage,
aside.side {
  overflow: visible !important;
}

.menuPanel button {
  width: 100%;
  text-align: left;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid #e6e8f0;
  background: #fff;
  cursor: pointer;
  font-size: 14px;
}
.menuPanel button:active { transform: translateY(1px); }
.menuPanel button:hover { background: #f6f8ff; }
.menuPanel button:disabled { opacity: 0.5; cursor: not-allowed; }

/* ===== Layout ===== */
.layout {
  display: grid;
  grid-template-columns: 1fr 360px;
  gap: 12px;
  padding: 12px;
}

/* Canvas area */
.stage {
  position: relative;
  background: #ffffff;
  border: 1px solid #e6e8f0;
  border-radius: 16px;
  overflow: hidden;
  min-height: calc(100vh - 76px);
}

#svg {
  width: 100%;
  height: 100%;
  touch-action: none;
  display: block;
  background: #ffffff;
}

/* Floating undo/redo buttons */
.canvasFabs {
  position: absolute;
  left: 12px;
  bottom: 12px;
  display: flex;
  gap: 10px;
  z-index: 50;
  pointer-events: auto;

  /* Auto-hide default */
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 180ms ease, transform 180ms ease;
}

/* When JS adds this class, they appear */
.canvasFabs.show {
  opacity: 1;
  transform: translateY(0);
}

/* Optional: allow instant show without animation if needed later */
/* .canvasFabs.instant { transition: none; } */

.fab {
  width: 44px;
  height: 44px;
  border-radius: 14px;
  border: 1px solid #e6e8f0;
  background: #ffffff;
  box-shadow: 0 8px 18px rgba(0,0,0,0.12);
  display: grid;
  place-items: center;
  cursor: pointer;
}

.fab svg {
  width: 22px;
  height: 22px;
  color: #222;
}

.fab:hover { background: #f6f8ff; }
.fab:active { transform: translateY(1px); }
.fab:disabled { opacity: 0.45; cursor: not-allowed; box-shadow: none; }

/* Sidebar */
.side {
  background: #ffffff;
  border: 1px solid #e6e8f0;
  border-radius: 16px;
  overflow: hidden;
  min-height: calc(100vh - 76px);
  display: flex;
  flex-direction: column;
}

/* ===== Tabs + Panels ===== */
.tabbar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  padding: 10px;
  border-bottom: 1px solid #eef1f7;
  background: #fbfcff;
}

.tabbar .tab {
  padding: 10px 8px;
  border-radius: 12px;
  border: 1px solid #e6e8f0;
  background: #ffffff;
  cursor: pointer;
  font-size: 13px;
}
.tabbar .tab.active {
  background: #f3f6ff;
  border-color: #cdd9ff;
}

.panelViews {
  padding: 10px;
  overflow: auto;
  flex: 1;
}

.panelView { display: none; }
.panelView.active { display: block; }

/* ===== Tools ===== */
.toolRow {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}

.tool {
  flex: 1;
  padding: 10px 8px;
  border-radius: 12px;
  border: 1px solid #e6e8f0;
  background: #fff;
  cursor: pointer;
  font-size: 14px;
}
.tool.active {
  background: #f3f6ff;
  border-color: #cdd9ff;
}

/* ===== Icons ===== */
.iconGrid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.iconBtn {
  aspect-ratio: 1 / 1;
  border-radius: 14px;
  border: 1px solid #e6e8f0;
  background: #fff;
  display: grid;
  place-items: center;
  cursor: pointer;
}
.iconBtn svg { width: 70%; height: 70%; }

/* ===== Layers ===== */
.layerBtns {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  margin-bottom: 10px;
}

.layerBtns button {
  padding: 10px 8px;
  border-radius: 12px;
  border: 1px solid #e6e8f0;
  background: #fff;
  cursor: pointer;
  font-size: 13px;
}

.layersList {
  display: grid;
  gap: 8px;
}

.layerItem {
  border: 1px solid #e6e8f0;
  border-radius: 12px;
  padding: 10px;
  background: #fff;
  display: flex;
  justify-content: space-between;
  gap: 10px;
  cursor: pointer;
}

.layerItem.active {
  background: #f3f6ff;
  border-color: #cdd9ff;
}

.layerTag {
  font-size: 11px;
  color: #666;
  margin-top: 2px;
}

/* ===== Props ===== */
.propsBox {
  display: grid;
  gap: 10px;
}

.propsBox input[type="text"],
.propsBox input[type="number"] {
  width: 100%;
  padding: 10px 10px;
  border-radius: 12px;
  border: 1px solid #e6e8f0;
  font-size: 14px;
}

.propsBox input[type="color"] {
  width: 100%;
  height: 44px;
  border-radius: 12px;
  border: 1px solid #e6e8f0;
  padding: 6px;
  background: #fff;
}

.propsBox button {
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid #e6e8f0;
  background: #fff;
  cursor: pointer;
}

.rangeWrap label {
  display: block;
  font-size: 12px;
  color: #555;
  margin-bottom: 6px;
}
.rangeWrap input[type="range"] { width: 100%; }

/* =========================================================
   DUPLICATION FIX
   Desktop/tablet: show desktop containers only
   ========================================================= */
#iconGridMobile,
#layersMobile,
#propertiesMobile,
.layerBtns.mobileOnly {
  display: none !important;
}

/* ===== Mobile ===== */
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .side { min-height: auto; }
  .stage { min-height: 55vh; }

  /* On mobile: show mobile containers, hide desktop containers */
  #iconGrid,
  #layers,
  #properties {
    display: none !important;
  }

  #iconGridMobile,
  #layersMobile,
  #propertiesMobile {
    display: grid !important;
  }

  /* Layers buttons: show only mobile buttons on mobile */
  .panelView[data-panel="layers"] .layerBtns:not(.mobileOnly) {
    display: none !important;
  }
  .layerBtns.mobileOnly {
    display: grid !important;
    margin-top: 10px;
  }

  .fab {
    width: 48px;
    height: 48px;
    border-radius: 16px;
  }
  .fab svg {
    width: 24px;
    height: 24px;
  }
}