/* =========================================
   1. 全局重置与基础样式 (Global Reset)
   ========================================= */
*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: #f4f6fb;
  color: #0f172a;
  line-height: 1.5;
}

a { text-decoration: none; color: inherit; }
button, input, select, textarea { font-family: inherit; }

/* 顶部导航栏 (通用) */
.top-nav {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 24px; background: #fff; border-bottom: 1px solid #e2e8f0;
  position: sticky; top: 0; z-index: 1000;
}
.nav-brand { font-weight: 700; font-size: 18px; color: #0f172a; }
.nav-links { display: flex; gap: 8px; }
.nav-link {
  padding: 8px 16px; border-radius: 6px; font-size: 14px; font-weight: 500;
  color: #64748b; transition: all 0.2s;
}
.nav-link:hover { background: #f1f5f9; color: #334155; }
.nav-link.active { background: #eff6ff; color: #3b82f6; font-weight: 600; }

/* 页面容器 */
.app, .collage-app {
  max-width: 1400px; margin: 0 auto; padding: 20px;
  min-height: calc(100vh - 60px);
  display: flex; flex-direction: column; gap: 20px;
}

/* 页面标题头 (通用) */
.app-header, .collage-header {
  background: #fff; padding: 20px; border-radius: 12px;
  border: 1px solid #e2e8f0; box-shadow: 0 1px 3px rgba(0,0,0,0.05);
  display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 16px;
}
.app-header h1, .collage-header h1 { margin: 0 0 8px 0; font-size: 20px; color: #1e293b; }
.app-header p, .collage-header p { margin: 0; font-size: 13px; color: #64748b; max-width: 800px; line-height: 1.6; }

/* =========================================
   2. 通用组件样式 (Components)
   ========================================= */

/* 按钮 */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 8px 16px; border-radius: 6px; font-size: 14px; font-weight: 500;
  cursor: pointer; border: 1px solid transparent; transition: all 0.2s;
  white-space: nowrap; user-select: none;
}
.btn-primary { background: #3b82f6; color: #fff; border-color: #3b82f6; }
.btn-primary:hover:not(:disabled) { background: #2563eb; border-color: #2563eb; box-shadow: 0 2px 4px rgba(59,130,246,0.3); }
.btn.ghost { background: transparent; border-color: #cbd5e1; color: #475569; }
.btn.ghost:hover:not(:disabled) { border-color: #94a3b8; background: #fff; color: #1e293b; }
.btn.ghost.small { padding: 4px 10px; font-size: 12px; height: 28px; }
.btn:disabled { opacity: 0.6; cursor: not-allowed; filter: grayscale(1); }

/* 表单控件 */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; flex: 1; }
.field-inline { display: flex; gap: 12px; align-items: center; }
.field-group { display: flex; gap: 16px; padding: 8px 0; }
label { font-size: 13px; font-weight: 500; color: #475569; }
input[type="text"], input[type="number"], select {
  height: 36px; padding: 0 10px; border: 1px solid #cbd5e1; border-radius: 6px;
  background: #fff; font-size: 14px; width: 100%; transition: border-color 0.2s;
}
input:focus, select:focus { outline: none; border-color: #3b82f6; box-shadow: 0 0 0 2px rgba(59,130,246,0.1); }
input[type="color"] { -webkit-appearance: none;appearance: none; border: none; width: 100%; height: 36px; padding: 0; border-radius: 6px; cursor: pointer; overflow: hidden; }
input[type="range"] { width: 100%; margin: 8px 0; cursor: pointer; }
.hint { font-size: 12px; color: #94a3b8; line-height: 1.5; margin-top: 4px; }

/* =========================================
   3. 水印工具专用样式 (Watermark Tool)
   ========================================= */

/* 主布局：左侧设置，右侧预览 */
.app-main {
  display: grid;
  grid-template-columns: 360px 1fr;
  gap: 20px;
  align-items: start;
}

/* 通用面板容器 */
.panel {
  background: #fff; border-radius: 12px;
  border: 1px solid #e2e8f0; box-shadow: 0 1px 3px rgba(0,0,0,0.05);
  padding: 16px; display: flex; flex-direction: column; gap: 16px;
}

.panel-block h2 {
  font-size: 15px; font-weight: 600; color: #1e293b;
  margin: 0 0 12px 0; padding-bottom: 8px; border-bottom: 1px solid #f1f5f9;
}

/* 预览区头部 */
.preview-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 0px; padding-bottom: 0px; border-bottom: 1px solid #f1f5f9;
}
.preview-header h2 { margin: 0; font-size: 16px; }
.nav-controls { display: flex; align-items: center; gap: 10px; }
.nav-info { min-width: 100px; text-align: center; }

/* 缩略图列表 */
.thumbs { margin-bottom: 16px; }
.thumbs-title { font-size: 12px; color: #64748b; margin-bottom: 8px; }
.thumbs-grid {
  display: flex; gap: 8px; overflow-x: auto; padding-bottom: 8px;
  scrollbar-width: thin;
}
.thumb {
  flex: 0 0 60px; height: 60px; border-radius: 6px; overflow: hidden;
  border: 2px solid transparent; cursor: pointer; opacity: 0.7; transition: all 0.2s;
  background: #f1f5f9;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb:hover { opacity: 1; }
.thumb.active { border-color: #3b82f6; opacity: 1; box-shadow: 0 2px 4px rgba(59,130,246,0.2); }

/* 预览区提示文字 */
.preview-hint {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #94a3b8; /* 灰色文字 */
  font-size: 18px;
  font-weight: 500;
  pointer-events: none; /* 让鼠标点击穿透它 */
  user-select: none;    /* 防止文字被选中 */
  z-index: 5;           /* 确保在背景之上 */
}

/* 画布显示区 - 修复版 */
.canvas-wrapper {
  background-color: #f8fafc;
  /* 棋盘格背景表示透明 */
  background-image:
    linear-gradient(45deg, #e2e8f0 25%, transparent 25%),
    linear-gradient(-45deg, #e2e8f0 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #e2e8f0 75%),
    linear-gradient(-45deg, transparent 75%, #e2e8f0 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
  
  border: 1px solid #e2e8f0; border-radius: 8px;
  
  /* 关键修改：固定高度范围，防止塌陷或无限撑开 */
  width: 100%;
  height: 600px;       /* 默认高度 */
  max-height: 70vh;    /* 最大不超过屏幕高度的 70% */
  
  display: flex; 
  justify-content: center; 
  align-items: center;
  overflow: hidden;    /* 隐藏滚动条，让图片自适应缩放 */
  position: relative;
  padding: 10px;       /* 给图片一点呼吸空间 */
}

/* ====== 核心修复：画布样式分家 ====== */

/* 1. 所有画布的通用基础样式 */
canvas {
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
}

/* 2. 仅针对【水印工具】的画布：默认隐藏，有图时JS会把它切为 block */
#preview-canvas {
  display: none; 
}

/* 3. 仅针对【拼图工具】的画布：必须一直显示！ */
#collage-canvas {
  display: block; 
}

/* 交互遮罩层 (用于拖拽) */
.canvas-overlay {
  position: absolute; inset: 0; z-index: 10;
  cursor: default; touch-action: none;
}
.canvas-overlay.drag-enabled { cursor: grab; }
.canvas-overlay.dragging { cursor: grabbing; }

/* =========================================
   4. 拼图工具专用样式 (Collage Tool)
   ========================================= */

/* 布局：三栏结构 */
.layout-3col .collage-main {
  display: grid; grid-template-columns: 280px 1fr 280px; gap: 20px;
  align-items: start; min-height: 600px;
}

/* 侧边栏容器 */
.collage-sidebar-left, .collage-settings {
  background: #fff; border-radius: 12px;
  border: 1px solid #e2e8f0; box-shadow: 0 1px 3px rgba(0,0,0,0.05);
  display: flex; flex-direction: column; overflow: hidden;
  max-height: calc(100vh - 140px);
}

/* 左侧 Tab */
.sidebar-tabs { display: flex; background: #f8fafc; border-bottom: 1px solid #e2e8f0; }
.tab-btn {
  flex: 1; padding: 12px; background: transparent; border: none;
  font-size: 13px; font-weight: 600; color: #64748b; cursor: pointer;
  border-bottom: 2px solid transparent; transition: all 0.2s;
}
.tab-btn.active { color: #3b82f6; border-bottom-color: #3b82f6; background: #fff; }
.sidebar-content { display: none; padding: 16px; overflow-y: auto; }
.sidebar-content.active { display: block; }

/* 模板列表 */
.template-list { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.template-card {
  aspect-ratio: 1; background: #f1f5f9; border-radius: 6px; cursor: pointer;
  border: 2px solid transparent; transition: all 0.2s; padding: 4px;
}
.template-card:hover { border-color: #cbd5e1; transform: translateY(-2px); }
.template-card.active { border-color: #3b82f6; background: #eff6ff; }
.template-preview { width: 100%; height: 100%; position: relative; pointer-events: none; }
.template-preview span {
  position: absolute; background: #94a3b8; border: 2px solid #f1f5f9;
  box-sizing: border-box; border-radius: 2px;
}
.template-card.active .template-preview span { background: #3b82f6; border-color: #eff6ff; }

/* 图层列表 */
.layer-list { display: flex; flex-direction: column; gap: 8px; }
.layer-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px; background: #f8fafc; border: 1px solid #e2e8f0; border-radius: 6px;
  cursor: grab; font-size: 13px; color: #334155; transition: all 0.2s;
}
.layer-item:hover { border-color: #cbd5e1; }
.layer-item.active { border-color: #3b82f6; background: #eff6ff; color: #1e293b; }
.layer-item.dragging { opacity: 0.5; border-style: dashed; }
.layer-item.drag-over { border-top: 2px solid #3b82f6; }
.layer-del-btn {
  background: none; border: none; color: #94a3b8; font-size: 16px;
  cursor: pointer; padding: 0 4px; border-radius: 4px;
}
.layer-del-btn:hover { color: #ef4444; background: #fee2e2; }

/* 中间画布区 */
.collage-preview {
  background: #fff; border-radius: 12px; border: 1px solid #e2e8f0;
  padding: 20px; display: flex; flex-direction: column; gap: 16px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}
.preview-bar { display: flex; justify-content: space-between; align-items: center; font-size: 12px; color: #64748b; }
.collage-canvas-wrap {
  flex: 1; background: #f1f5f9; border: 1px dashed #cbd5e1; border-radius: 8px;
  display: flex; justify-content: center; align-items: center; position: relative;
  overflow: hidden; min-height: 400px;
}
.slot-overlay { position: absolute; inset: 0; z-index: 50; pointer-events: none; }
.slot-item {
  position: absolute; pointer-events: none; display: flex;
  justify-content: center; align-items: center; font-size: 24px;
  color: rgba(59,130,246,0.3); z-index: 100;
}
.slot-item.selected {
  box-shadow: inset 0 0 0 4px #3b82f6;
  background: rgba(59,130,246,0.05);
}

/* 右侧设置面板 */
.collage-settings { padding: 16px; }
.settings-group { display: flex; flex-direction: column; gap: 16px; animation: fadeIn 0.2s; }
.settings-group.hidden { display: none; }
.panel-header { display: flex; justify-content: space-between; align-items: center; padding-bottom: 12px; border-bottom: 1px solid #f1f5f9; margin-bottom: 12px; }
.panel-title { font-weight: 600; font-size: 14px; }
.back-btn { font-size: 12px; color: #64748b; background: #f1f5f9; padding: 4px 8px; border-radius: 4px; border: none; cursor: pointer; }
.back-btn:hover { background: #e2e8f0; color: #0f172a; }

.tool-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 8px; }
.tool-btn { padding: 8px; border: 1px solid #e2e8f0; background: #fff; border-radius: 6px; cursor: pointer; }
.tool-btn:hover { border-color: #3b82f6; color: #3b82f6; }

/* 动画 */
@keyframes fadeIn { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: translateY(0); } }

/* 响应式适配 */
@media (max-width: 1024px) {
  .layout-3col .collage-main { grid-template-columns: 240px 1fr; }
  .collage-settings { grid-column: 1 / -1; flex-direction: row; flex-wrap: wrap; max-height: none; }
  .settings-group { width: 100%; flex-direction: row; flex-wrap: wrap; }
  .field { min-width: 150px; }
}

@media (max-width: 768px) {
  .app-main { grid-template-columns: 1fr; }
  .layout-3col .collage-main { display: flex; flex-direction: column; }
  .app-header, .collage-header { flex-direction: column; align-items: flex-start; }
  .collage-sidebar-left, .collage-settings { max-height: none; }
}

/* ====== 修复：模板张数切换栏的间距 ====== */
#template-tabs {
  display: flex;
  flex-wrap: wrap;       /* 如果屏幕小，允许换行 */
  gap: 12px;             /* 核心：按钮之间的间距 */
  margin-bottom: 20px;   /* 核心：这一栏和下方模板列表的距离 */
  padding: 4px 0;        /* 增加一点上下的容错空间 */
}

/* 优化按钮的触控面积和外观，让它看起来更舒服 */
#template-tabs .btn {
  padding: 6px 16px;     /* 增加内部留白 */
  border-radius: 20px;   /* 变成圆角胶囊样式，更有现代感 */
  background-color: #f1f5f9;
  border: 1px solid transparent;
}

#template-tabs .btn:hover {
  background-color: #e2e8f0;
}

#template-tabs .btn.active {
  background-color: #eff6ff;
  color: #3b82f6;
  border-color: #3b82f6;
  font-weight: 600;
  box-shadow: 0 2px 4px rgba(59,130,246,0.15); /* 激活时加一点投影 */
}