html, body {
  height: 100%;
  /* 动态视口：移动端浏览器 UI 收起时跟随变化 */
  height: 100dvh;
}

* { margin:0; padding:0; box-sizing:border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", sans-serif;
  background: #0d1117;
  color: #c9d1d9;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

/* ========= 顶部工具栏 ========= */
#toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: #161b22;
  border-bottom: 1px solid #30363d;
  z-index: 200;
  flex-shrink: 0;
  flex-wrap: wrap;
  gap: 6px 10px;
}

/* ========= 序号标签栏 ========= */
#slot-tabs {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  background: #1c2128;
  border-bottom: 1px solid #30363d;
  flex-shrink: 0;
  flex-wrap: nowrap;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
#slot-tabs::-webkit-scrollbar { height: 4px; }
#slot-tabs::-webkit-scrollbar-thumb { background: #30363d; border-radius: 2px; }
.slot-label {
  font-size: 13px;
  color: #8b949e;
  margin-right: 4px;
  white-space: nowrap;
  flex-shrink: 0;
  position: sticky;
  left: 0;
  background: #1c2128;
  padding-right: 8px;
  z-index: 1;
}
.slot-tab {
  width: 34px;
  height: 34px;
  border: 2px solid #30363d;
  border-radius: 6px;
  background: #21262d;
  color: #8b949e;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
}
.slot-tab:hover { border-color: #f78166; color: #c9d1d9; }
.slot-tab.active { border-color: #f78166; background: rgba(247,129,102,0.15); color: #f78166; }
.slot-tab.dirty { border-color: #3fb950; color: #3fb950; }
.slot-tab.active.dirty { border-color: #3fb950; background: rgba(63,185,80,0.15); color: #3fb950; }
.slot-add { color: #58a6ff; font-size: 18px; }
.slot-add:hover { border-color: #58a6ff; background: rgba(88,166,255,0.15); color: #fff; }

.toolbar-title {
  font-size: 18px;
  font-weight: 700;
  color: #f78166;
  white-space: nowrap;
  flex-shrink: 0;
}

.id-label {
  font-size: 36px;
  color: #8b949e;
  white-space: nowrap;
}

.id-group {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.session-id {
  font-size: 50px;
  font-weight: 700;
  color: #3fb950;
  font-family: 'Consolas', 'Courier New', monospace;
  letter-spacing: 3px;
  cursor: pointer;
  user-select: all;
  padding: 3px 10px;
  border-radius: 4px;
  border: 1px dashed #30363d;
  background: #161b22;
  transition: background 0.15s;
  white-space: nowrap;
}
.session-id:hover { background: #21262d; border-color: #3fb950; }

.copy-id-btn {
  width: auto; padding: 0 10px;
  height: 36px;
  border: 1px solid #30363d;
  border-radius: 6px;
  background: #21262d;
  color: #8b949e;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
  flex-shrink: 0;
}
.copy-id-btn:hover { border-color: #58a6ff; color: #c9d1d9; }

.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 1;
  flex-wrap: wrap;
}

/* 模板选择器 */
.template-selector {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 1;
  min-width: 0;
}

.template-label {
  font-size: 12px;
  color: #8b949e;
  white-space: nowrap;
}

#template-select {
  padding: 5px 28px 5px 10px;
  border: 1px solid #30363d;
  border-radius: 6px;
  background: #21262d;
  color: #c9d1d9;
  font-size: 13px;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  flex-shrink: 1;
  min-width: 60px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%238b949e'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 10px 6px;
  max-width: 180px;
  transition: border-color 0.15s;
}
#template-select:hover {
  border-color: #f78166;
}
#template-select:focus {
  outline: none;
  border-color: #f78166;
  box-shadow: 0 0 0 2px rgba(247, 129, 102, 0.2);
}
#template-select option {
  background: #161b22;
  color: #c9d1d9;
}

.toolbar-actions button {
  padding: 6px 14px;
  border: 1px solid #30363d;
  border-radius: 6px;
  background: #21262d;
  color: #c9d1d9;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
  flex-shrink: 1;
  overflow: hidden;
  text-overflow: ellipsis;
}
.toolbar-actions button:hover {
  background: #30363d;
  border-color: #f78166;
}

/* 按钮组：不可再拆 */
.tb-group {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
  white-space: nowrap;
  min-width: 0;
}
.tb-actions {
  flex-shrink: 1 !important;
  min-width: 0;
}
.tb-actions .action-btn {
  padding: 6px 10px !important;
  font-size: 13px !important;
  flex-shrink: 1;
  min-width: 0;
}
@media (max-width: 500px) {
  .tb-actions .action-btn { padding-left: 5px !important; padding-right: 5px !important; }
}
#btn-add { background: #f78166; color: #fff; border-color: #f78166; }
#btn-add:hover { background: #e06146; }
#btn-finish { border-color: #58a6ff; color: #58a6ff; }
#btn-upload { border-color: #3fb950; color: #3fb950; }
#btn-upload:hover { background: #3fb950; color: #fff; }

/* ========= 主布局 ========= */
#main-layout {
  flex: 1;
  display: flex;
  flex-direction: row;
  overflow: hidden;
  min-height: 0;
}

/* ========= 主画布区域 ========= */
#canvas-area {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
  background: #0d1117;
  transition: box-shadow 0.2s;
  background-image:
    linear-gradient(45deg, #161b22 25%, transparent 25%),
    linear-gradient(-45deg, #161b22 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #161b22 75%),
    linear-gradient(-45deg, transparent 75%, #161b22 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
  min-width: 0;
}

#canvas-wrapper {
  position: relative;
  box-shadow: 0 0 40px rgba(0,0,0,0.6);
  border: 1px solid #30363d;
  flex-shrink: 0;
}

#main-canvas {
  display: block;
  position: relative;
  z-index: 1;
  cursor: crosshair;
}

#template-canvas {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 5;
  pointer-events: none;
}

#overlay-canvas {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 200;
  pointer-events: none;
}

/* 缩放控件 */
#zoom-controls {
  position: absolute;
  bottom: 16px;
  right: 16px;
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(22,27,34,0.9);
  border: 1px solid #30363d;
  border-radius: 8px;
  padding: 4px 8px;
  z-index: 150;
}

#zoom-controls button {
  width: 30px;
  height: 30px;
  border: none;
  background: #21262d;
  color: #c9d1d9;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
#zoom-controls button:hover { background: #30363d; }
#zoom-level {
  font-size: 12px;
  min-width: 42px;
  text-align: center;
  color: #8b949e;
}

/* ========= 图层面板（横向=右侧面板） ========= */
#layer-panel {
  background: #161b22;
  border-left: 1px solid #30363d;
  display: flex;
  flex-direction: column;
  width: 260px;
  flex-shrink: 0;
  z-index: 200;
  transition: width 0.2s;
  overflow: hidden;
}

#layer-panel.collapsed {
  width: 36px !important;
}

/* JS 动态布局切换（兜底覆盖） */
#main-layout.layout-portrait {
  flex-direction: column;
}
#main-layout.layout-portrait #layer-panel {
  width: 100% !important;
  height: 35vh;
  min-height: 120px;
  border-left: none;
  border-top: 1px solid #30363d;
  transition: height 0.2s;
}
#main-layout.layout-portrait #layer-panel.collapsed {
  width: 100% !important;
  height: 36px !important;
  min-height: 36px;
}

#layer-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 16px;
  font-size: 13px;
  font-weight: 600;
  border-bottom: 1px solid #30363d;
  flex-shrink: 0;
  cursor: pointer;
}

#btn-toggle-layers {
  background: none;
  border: 1px solid #30363d;
  color: #8b949e;
  padding: 3px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
}
#btn-toggle-layers:hover { color: #c9d1d9; border-color: #f78166; }

#layer-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
}

.layer-item {
  display: flex;
  align-items: center;
  padding: 5px 16px;
  cursor: pointer;
  transition: background 0.12s;
  border-left: 3px solid transparent;
  gap: 8px;
}
.layer-item.locked {
  opacity: 0.6;
  cursor: default;
  background: rgba(139,148,158,0.05);
}
.layer-item.locked:hover {
  background: rgba(139,148,158,0.08);
}
.layer-item.active { background: rgba(247,129,102,0.15); border-left-color: #f78166; }

.layer-thumb {
  width: 36px;
  height: 36px;
  border-radius: 4px;
  background: #0d1117;
  overflow: hidden;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #21262d;
}
.layer-thumb img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.layer-name {
  flex: 1;
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.layer-btns {
  display: flex;
  gap: 2px;
  flex-shrink: 0;
}
.layer-btn {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: #8b949e;
  border-radius: 3px;
  cursor: pointer;
  font-size: 13px;
}
.layer-btn:hover { background: #21262d; color: #c9d1d9; }
.layer-btn.hidden { opacity: 0.3; }

/* 图层拖拽排序 */
.layer-item.dragging {
  opacity: 0.4;
  background: #21262d;
}

/* ========= 全局撤销/重做（工具栏内） ========= */
.undo-btn {
  width: 32px; height: 32px;
  padding: 0;
  border: 1px solid #30363d;
  border-radius: 6px;
  background: #21262d;
  color: #8b949e;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
}
.undo-btn:hover:not(:disabled) { border-color: #58a6ff; color: #c9d1d9; }
.undo-btn:disabled { opacity: 0.3; cursor: default; }

/* ========= 模板选择弹窗 ========= */
.modal-overlay {
  display: flex;
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(0,0,0,0.65);
  align-items: center;
  justify-content: center;
}
.modal-box {
  background: #161b22;
  border: 1px solid #30363d;
  border-radius: 12px;
  padding: 32px 36px;
  max-width: 450px;
  width: 92vw;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 8px 40px rgba(0,0,0,0.5);
}
.modal-title {
  font-size: 24px;
  font-weight: 700;
  color: #f78166;
  text-align: center;
  margin-bottom: 24px;
}
.template-modal-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.template-modal-item {
  padding: 16px 20px;
  border: 2px solid #30363d;
  border-radius: 10px;
  background: #21262d;
  cursor: pointer;
  transition: all 0.15s;
  font-size: 17px;
  font-weight: 600;
  color: #c9d1d9;
  text-align: center;
}
.template-modal-item:hover {
  border-color: #58a6ff;
  background: #1c2d42;
  color: #fff;
  transform: scale(1.02);
}

/* ========= 浮动工具栏（跟随选中图层左上角） ========= */
#float-toolbar {
  position: absolute;
  z-index: 500;
  display: flex;
  gap: 2px;
  background: rgba(22,27,34,0.92);
  border: 1px solid #58a6ff;
  border-radius: 6px;
  padding: 3px;
  pointer-events: auto;
  white-space: nowrap;
  transition: opacity 0.15s;
  align-items: center;
}

.ftb-btn {
  width: 26px;
  height: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: #c9d1d9;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  transition: all 0.12s;
}
.ftb-btn:hover { background: #30363d; }
.ftb-btn.active { background: #f78166; color: #fff; }
.ftb-btn.ftb-del { color: #f85149; }
.ftb-btn.ftb-del:hover { background: rgba(248,81,73,0.25); }
.ftb-btn.ftb-draw { color: #58a6ff; }
.ftb-btn.ftb-draw:hover { background: rgba(88,166,255,0.2); }
.ftb-btn.ftb-draw.active { background: #58a6ff; color: #fff; }

/* 浮动工具栏收起 */
#float-toolbar.collapsed .ftb-btn:not(.ftb-collapse),
#float-toolbar.collapsed .ftb-sep,
#float-toolbar.collapsed .ftb-size-label,
#float-toolbar.collapsed .ftb-size,
#float-toolbar.collapsed .ftb-color { display: none; }
#float-toolbar.collapsed { border-radius: 20px; padding: 4px; }
.ftb-collapse { width: 26px; height: 26px; padding: 0; font-size: 16px; line-height: 1; }

.eraser-icon {
  display: inline-block;
  width: 14px;
  height: 11px;
  background: linear-gradient(135deg, #f778a1 60%, #f9a8c4 60%);
  border-radius: 2px 2px 1px 1px;
  border: 1px solid #e0608a;
}

.ftb-sep { width:1px;height:22px;background:#30363d;margin:0 4px; }
.ftb-size-label { font-size:11px;color:#8b949e;margin:0 2px;white-space:nowrap; }
.ftb-size { width:60px;height:16px;cursor:pointer;accent-color:#58a6ff;margin:0; }
.ftb-color { width:24px;height:24px;border:none;cursor:pointer;border-radius:3px;padding:0;margin:0; }

.ftb-draw-group {
  position: absolute;
  top: -34px;
  display: flex;
  align-items: center;
  gap: 3px;
  background: rgba(22,27,34,0.95);
  border: 1px solid #58a6ff;
  border-radius: 5px;
  padding: 3px 6px;
  white-space: nowrap;
}
#draw-group-brush { left: 0; }
#draw-group-eraser { right: 0; }

/* ========= 确认对话框 ========= */
.confirm-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  z-index: 10000;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: overlayFadeIn 0.15s ease-out;
}

@keyframes overlayFadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.confirm-dialog {
  background: #1c2128;
  border: 1px solid #30363d;
  border-radius: 12px;
  padding: 28px 32px;
  max-width: 400px;
  width: 90%;
  text-align: center;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5);
  animation: dialogSlideIn 0.2s ease-out;
}

@keyframes dialogSlideIn {
  from { transform: translateY(-20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.confirm-icon {
  font-size: 42px;
  margin-bottom: 12px;
  color: #f78166;
}

.confirm-title {
  font-size: 18px;
  font-weight: 700;
  color: #fff;
  margin-bottom: 10px;
}

.confirm-msg {
  font-size: 14px;
  color: #8b949e;
  line-height: 1.6;
  margin-bottom: 24px;
}

.confirm-buttons {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.confirm-btn {
  padding: 10px 20px;
  border: 1px solid #30363d;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
  width: 100%;
}

.btn-save {
  background: #f78166;
  color: #fff;
  border-color: #f78166;
}
.btn-save:hover {
  background: #e06146;
}

.btn-discard {
  background: #21262d;
  color: #8b949e;
  border-color: #30363d;
}
.btn-discard:hover {
  background: #30363d;
  color: #c9d1d9;
}

.btn-cancel {
  background: transparent;
  color: #8b949e;
  border-color: transparent;
}
.btn-cancel:hover {
  background: #21262d;
  color: #c9d1d9;
}

/* ========= 提示 Toast ========= */
.toast-msg {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0,0,0,0.88);
  color: #fff;
  padding: 12px 28px;
  border-radius: 10px;
  font-size: 14px;
  z-index: 9999;
  pointer-events: none;
  animation: toastAnim 2s forwards;
  text-align: center;
  max-width: 80vw;
}

@keyframes toastAnim {
  0% { opacity:0; transform: translate(-50%,-50%) scale(0.95); }
  10% { opacity:1; transform: translate(-50%,-50%) scale(1); }
  75% { opacity:1; }
  100% { opacity:0; transform: translate(-50%,-50%) scale(0.95); }
}

/* ========= 选区/控制点样式 ========= */
.selection-handle {
  position: absolute;
  width: 10px;
  height: 10px;
  background: #f78166;
  border: 1.5px solid #fff;
  border-radius: 50%;
  z-index: 50;
  pointer-events: auto;
}

/* ========= 手机端 / 竖屏适配 ========= */
@media (max-width: 768px), (max-aspect-ratio: 1/1) {
  #main-layout {
    flex-direction: column;
  }

  #canvas-area {
    border-bottom: 1px solid #30363d;
  }

  #layer-panel {
    width: 100% !important;
    height: 35vh;
    min-height: 120px;
    border-left: none;
    border-top: 1px solid #30363d;
    transition: height 0.2s;
  }

  #layer-panel.collapsed {
    width: 100% !important;
    height: 36px !important;
    min-height: 36px;
  }
  #toolbar { padding: 6px 10px; flex-wrap: wrap; gap: 6px; }
  .toolbar-title { font-size: 14px; }
  .id-label { font-size: 16px; }
  .id-group { gap: 3px; flex-shrink: 0; min-width: 0; overflow: visible; }
  .session-id { font-size: 16px; letter-spacing: 2px; padding: 2px 6px; }
  .toolbar-actions { gap: 5px; flex-wrap: wrap; flex-shrink: 1; }
  .toolbar-actions button { padding: 5px 10px; font-size: 12px; flex-shrink: 1; }

  .template-selector { gap: 3px; }
  .template-label { font-size: 11px; }
  #template-select {
    font-size: 12px;
    padding: 5px 24px 5px 8px;
    max-width: 140px;
  }

  #layer-panel { height: 30vh; min-height: 100px; }
  .layer-item { padding: 4px 10px; gap: 6px; }
  .layer-thumb { width: 30px; height: 30px; }
  .layer-name { font-size: 11px; }

  #float-toolbar { gap: 1px; padding: 2px; }
  .ftb-btn { width: 24px; height: 24px; font-size: 12px; }

  #zoom-controls { bottom: 8px; right: 8px; padding: 2px 6px; }
  #zoom-controls button { width: 26px; height: 26px; font-size: 14px; }

  .confirm-dialog { padding: 20px 20px; }
  .confirm-title { font-size: 16px; }
  .confirm-msg { font-size: 13px; }
  .confirm-btn { font-size: 13px; padding: 9px 16px; }
}

/* ========= 背景色浮窗 ========= */
.color-picker-float {
  position: fixed;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  z-index: 1000;
}
.color-picker-box {
  background: #21262d;
  border: 1px solid #30363d;
  border-radius: 10px;
  padding: 16px 20px;
  width: 260px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.5);
}

/* ========= 滚动条 ========= */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: #0d1117; }
::-webkit-scrollbar-thumb { background: #30363d; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #f78166; }

/* ========= 4K/高分屏适配 ========= */
@media (min-resolution: 2dppx), (-webkit-min-device-pixel-ratio: 2) {
  body { font-size: 115%; }
  #toolbar { padding: 10px 20px; }
  .toolbar-title { font-size: 22px; }
  .id-label { font-size: 16px; }
  .session-id { font-size: 24px; }
  .toolbar-actions button { padding: 8px 18px; font-size: 14px; }
  #template-select { padding: 6px 32px 6px 12px; font-size: 14px; }
  .template-label { font-size: 13px; }
  #layer-panel-header { padding: 8px 18px; font-size: 14px; }
  .layer-item { padding: 6px 18px; gap: 10px; }
  .layer-thumb { width: 40px; height: 40px; }
  .layer-name { font-size: 13px; }
  .layer-btn { width: 28px; height: 28px; font-size: 14px; }
  .ftb-btn { width: 30px; height: 30px; font-size: 15px; }
  #zoom-controls button { width: 34px; height: 34px; font-size: 18px; }
  #zoom-level { font-size: 13px; }
  .confirm-dialog { padding: 32px 40px; }
  .confirm-title { font-size: 20px; }
  .confirm-btn { font-size: 15px; padding: 12px 24px; }
}
