/* layout.css — Bố cục khung app: thanh trên, thanh công cụ trái, bảng vẽ giữa, ô thuộc tính phải.
   Điện thoại (≤ 720px): thanh công cụ thành thanh dưới, ô thuộc tính thành ngăn kéo trượt lên (mở bằng nút "Thuộc tính"). */

.app {
  display: grid;
  grid-template-columns: var(--toolbar-w) minmax(0, 1fr) var(--props-w);
  grid-template-rows: var(--topbar-h) minmax(0, 1fr);
  grid-template-areas:
    "top   top   top"
    "tools stage props";
  height: 100vh;
  height: 100dvh;
}

/* ---- Thanh trên ---- */
.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 16px;
  background: var(--c-panel);
  border-bottom: 1px solid var(--c-border);
}
.topbar__title { font-size: 17px; font-weight: 650; white-space: nowrap; }
.topbar__actions { display: flex; align-items: center; gap: 8px; min-width: 0; }
.topbar__props-toggle { display: none; }

/* ---- Thanh công cụ ---- */
.toolbar {
  grid-area: tools;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
  padding: 10px 8px;
  background: var(--c-panel);
  border-right: 1px solid var(--c-border);
  overflow-y: auto;
}

/* ---- Bảng vẽ ----
   .stage là vùng nền xám quanh trang, căn giữa; .device-frame là "trang" có khổ cố định.
   Tỉ lệ trang lấy từ biến --page-w/--page-h (js/editor/page.js đặt, cùng số với viewBox của svg).
   Khung to nhất vừa trong stage mà vẫn giữ tỉ lệ (100cqw/100cqh = bề rộng/cao phần trong của stage):
   cửa sổ nhỏ thì cả trang thu nhỏ đều, toạ độ hình không đổi.
   Sau này "xem trước theo thiết bị": đặt data-device + gọi applyPage() với khổ thiết bị; svg bên trong giữ nguyên.
   Khung và svg KHÔNG cắt phần thừa (overflow: visible): hình bị cắt theo trang bằng clipPath #page-clip (index.html),
   còn lớp chọn được lòi ra mép trang bằng vòng chạm để chấm góc sát mép không bị cắt nửa (js/editor/selection.js).
   Góc trang vuông (không bo) để phần cắt của hình trùng khít mép khung. */
.stage {
  grid-area: stage;
  display: grid;
  place-items: center;
  padding: 16px;
  overflow: auto;
  container-type: size;
  touch-action: none; /* bấm/kéo ở phần trống quanh trang cũng do pointer events xử lý */
}
.stage:has(> .device-frame > .canvas:not([data-tool="select"])) { cursor: crosshair; }
.device-frame {
  width: min(100cqw, 100cqh * var(--page-w) / var(--page-h));
  aspect-ratio: var(--page-w) / var(--page-h);
  background: var(--c-canvas);
  box-shadow: 0 0 0 1px var(--c-border), var(--shadow); /* viền mảnh để thấy rõ mép trang */
}
.canvas {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
  touch-action: none; /* để đợt 2 tự xử lý kéo/chạm bằng pointer events, không bị trang cuộn/zoom */
  user-select: none;
  -webkit-user-select: none;
}
.canvas__bg { fill: var(--c-canvas); }
.canvas__grid-dot { fill: var(--c-grid); }

/* ---- Ô thuộc tính ---- */
.props {
  grid-area: props;
  padding: 14px 16px;
  background: var(--c-panel);
  border-left: 1px solid var(--c-border);
  overflow-y: auto;
}
.props__head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.props__title { font-size: 15px; font-weight: 650; }
.props__close { display: none; }
.backdrop { display: none; }

/* ---- Điện thoại ---- */
@media (max-width: 720px) {
  .app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: var(--topbar-h) minmax(0, 1fr) calc(var(--bottombar-h) + env(safe-area-inset-bottom, 0px));
    grid-template-areas:
      "top"
      "stage"
      "tools";
  }
  .topbar { padding: 0 12px; }
  .topbar__props-toggle { display: inline-flex; }

  .toolbar {
    flex-direction: row;
    justify-content: center;
    gap: 8px;
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom, 0px));
    border-right: 0;
    border-top: 1px solid var(--c-border);
    overflow-x: auto;
    overflow-y: hidden;
  }

  .stage { padding: 10px; }

  .props {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    max-height: 70vh;
    max-height: 70dvh;
    padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    border-left: 0;
    border-top: 1px solid var(--c-border);
    border-radius: 14px 14px 0 0;
    box-shadow: 0 -6px 24px rgba(20, 30, 50, .15);
    transform: translateY(100%);
    visibility: hidden;
    transition: transform .2s ease, visibility 0s linear .2s;
  }
  .props.is-open {
    transform: translateY(0);
    visibility: visible;
    transition: transform .2s ease;
  }
  .props__close { display: inline-flex; }
  .backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 15;
    background: rgba(15, 20, 30, .35);
  }
}

@media (prefers-reduced-motion: reduce) {
  .props, .props.is-open { transition: none; }
}
