:root {
  --primary-color: #2f6bd8;
  --primary-hover: #2559b4;
  --bg-color: #f4f6fa;
  --sidebar-bg: #ffffff;
  --text-dark: #333333;
  --text-light: #6c757d;
  --border-color: #e5e7eb;
  --danger-color: #dc3545;
  --danger-hover: #c82333;
  --header-height: 60px;
  --sidebar-width: 320px;
  
  /* Canvas dynamic variables */
  --puzzle-padding: 0px;
  --puzzle-gap: 10px;
  --puzzle-radius: 0px;
  --puzzle-bg: #ffffff;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background-color: var(--bg-color);
  color: var(--text-dark);
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* Header */
.top-nav {
  height: var(--header-height);
  background: linear-gradient(180deg, #ffffff 0%, #eef3fb 100%);
  border-bottom: 1px solid #e3e9f3;
  box-shadow: 0 1px 4px rgba(15, 23, 42, 0.05);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  flex-shrink: 0;
  z-index: 100;
}

.logo {
  display: flex;
  align-items: center;
  font-size: 1.2rem;
  font-weight: 600;
  color: #26324b;
  letter-spacing: 0.2px;
}

.logo i {
  color: var(--primary-color);
  font-size: 1.25rem;
  margin-right: 10px;
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: #e9f0fd;
  border: 1px solid #dbe6fa;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
}

.nav-links {
  display: flex;
  align-items: center;
  gap: 20px;
}

.nav-links a {
  text-decoration: none;
  color: #5c6b86;
  font-size: 0.95rem;
  transition: color 0.2s;
}

.nav-links a:hover {
  color: var(--primary-color);
}

/* Buttons */
.btn {
  border: none;
  border-radius: 4px;
  padding: 8px 16px;
  font-size: 0.9rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.2s;
}

.btn-primary {
  background-color: var(--primary-color);
  color: #fff;
}

.btn-primary:hover {
  background-color: var(--primary-hover);
}

.btn-outline {
  background-color: transparent;
  color: var(--text-dark);
  border: 1px solid var(--border-color);
}

.btn-outline:hover {
  background-color: #f1f5f9;
}

.btn-danger {
  background-color: #fff;
  border: 1px solid #fee2e2;
  color: var(--danger-color);
}

.btn-danger:hover {
  background-color: #fef2f2;
}

.btn-tool {
  background-color: #f8f9fa;
  border: 1px solid var(--border-color);
  color: var(--text-dark);
}

.btn-tool:hover {
  background-color: #e9ecef;
}

/* Main Container */
.main-container {
  display: flex;
  height: calc(100vh - var(--header-height));
  width: 100%;
  position: relative;
  overflow: hidden;
}

/* Sidebars */
.sidebar {
  width: var(--sidebar-width);
  background-color: var(--sidebar-bg);
  height: 100%;
  display: flex;
  flex-direction: column;
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  flex-shrink: 0;
  z-index: 10;
}

.left-sidebar {
  border-right: 1px solid var(--border-color);
}

.right-sidebar {
  border-left: 1px solid var(--border-color);
}

/* Sidebar toggles */
.toggle-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 24px;
  height: 48px;
  background: #fff;
  border: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 20;
  box-shadow: 0 2px 5px rgba(0,0,0,0.05);
  transition: all 0.2s;
}

.toggle-btn:hover {
  background: #f8f9fa;
}

.toggle-left {
  right: -24px;
  border-radius: 0 4px 4px 0;
  border-left: none;
}

.toggle-right {
  left: -24px;
  border-radius: 4px 0 0 4px;
  border-right: none;
}

/* Sidebar Hidden States */
.left-sidebar.collapsed {
  transform: translateX(-100%);
  margin-right: calc(var(--sidebar-width) * -1);
}

.right-sidebar.collapsed {
  transform: translateX(100%);
  margin-left: calc(var(--sidebar-width) * -1);
}

/* Center Workspace */
.center-workspace {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background-color: var(--bg-color);
  position: relative;
}

.toolbar {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 20px;
  gap: 20px;
  flex-shrink: 0;
}

.tool-group {
  display: flex;
  background: #fff;
  border-radius: 6px;
  padding: 4px;
  border: 1px solid var(--border-color);
  gap: 4px;
}

.view-controls {
  margin-left: auto;
}

/* Layout List (Left) */
.sidebar-header {
  padding: 20px;
  border-bottom: 1px solid var(--border-color);
}

.sidebar-header h3 {
  font-size: 1rem;
  font-weight: 600;
}

.layout-list {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
}

.layout-group {
  margin-bottom: 24px;
}

.layout-group-title {
  font-size: 0.85rem;
  color: var(--text-light);
  margin-bottom: 12px;
}

.layout-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.layout-item {
  aspect-ratio: 1;
  background-color: transparent;
  border-radius: 6px;
  border: 2px solid transparent;
  cursor: pointer;
  padding: 4px;
  transition: all 0.2s;
  display: flex;
  /* Mini CSS Grid to represent the layout visually */
}

.layout-item.active {
  border-color: #ff4d4f; /* 选中时的红色边框 */
}

.layout-item:hover:not(.active) {
  border-color: #d1d5db;
}

.layout-item .grid-preview-cell {
  background: #a5c8ff;
  border-radius: 2px;
  transition: background 0.2s;
}

.layout-item.active .grid-preview-cell {
  background: var(--primary-color);
}

.layout-item .custom-add {
  flex: 1;
  background: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  color: var(--text-light);
  border: 1px dashed var(--border-color);
  border-radius: 4px;
}

/* Accordion (Right) */
.sidebar-content {
  flex: 1;
  overflow-y: auto;
}

.accordion-item {
  border-bottom: 1px solid var(--border-color);
}

.accordion-header {
  padding: 16px 20px;
  display: flex;
  align-items: center;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.95rem;
  user-select: none;
}

.accordion-header i.ph-caret-down {
  margin-right: 8px;
  transition: transform 0.3s;
}

.accordion-item:not(.is-active) .accordion-header i.ph-caret-down {
  transform: rotate(-90deg);
}

.accordion-item:not(.is-active) .accordion-body {
  display: none;
}

.accordion-header .spacer {
  flex: 1;
}

.accordion-body {
  padding: 0 20px 20px 20px;
}

/* Ratio Grid */
.ratio-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}

.btn-ratio, .btn-quality {
  background: #f3f4f6;
  border: 1px solid transparent;
  border-radius: 4px;
  padding: 6px 0;
  font-size: 0.85rem;
  cursor: pointer;
  color: var(--text-dark);
}

.btn-ratio.active, .btn-quality.active {
  background: #fff;
  border-color: var(--primary-color);
  color: var(--primary-color);
}

.custom-ratio {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.9rem;
}

.ratio-inputs {
  display: flex;
  align-items: center;
  gap: 8px;
}

.ratio-inputs input {
  width: 50px;
  padding: 4px 8px;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  text-align: center;
}

/* Range sliders */
.control-group {
  margin-bottom: 16px;
}

.control-label {
  display: flex;
  justify-content: space-between;
  font-size: 0.9rem;
  margin-bottom: 8px;
}

.val-display {
  color: var(--text-light);
  font-variant-numeric: tabular-nums;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--primary-color);
}

input[type="color"] {
  width: 100%;
  height: 36px;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  cursor: pointer;
  padding: 2px;
}

/* Export Section */
.export-section {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.label-text {
  font-size: 0.9rem;
}

.quality-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.export-section .btn {
  width: 100%;
  padding: 10px;
  font-weight: 500;
}

/* Core Canvas CSS */
.canvas-wrapper {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  overflow: hidden; /* For zooming/scaling if necessary */
}

/* canvasContainer is the responsive bounding box that scales to fit */
.canvas-container {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  max-width: 800px; /* Base limit for desktop visibility */
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* puzzle-board is the actual grid holding images, sized via ratio */
.puzzle-board {
  background-color: var(--puzzle-bg);
  /* The layout grid will be generated by JS dynamically */
  display: grid;
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: var(--ratio, 1/1);
  padding: var(--puzzle-padding);
  gap: var(--puzzle-gap);
  border-radius: var(--puzzle-radius);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  position: relative;
  transition: padding 0.2s, gap 0.2s, border-radius 0.2s, background-color 0.2s;
  overflow: hidden;
  border: 2px solid #ff4d4f; /* 添加与参考图一样的中心红色边框 */
}

/* For empty initial state */
.placeholder-icon {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 3rem;
  color: #adb5bd;
}

/* Individual Grid Cells */
.grid-cell {
  background-color: #e9ecef;
  border-radius: calc(var(--puzzle-radius) / 2); /* slightly smaller radius for inner items */
  position: relative;
  overflow: hidden;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.2s;
}

.grid-cell:hover {
  background-color: #dee2e6;
}

.grid-cell .ph-plus {
  font-size: 2rem;
  color: #adb5bd;
  pointer-events: none;
}

.grid-cell img.puzzle-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: auto;
  /* Will be manipulated via JS transform for pan/zoom */
  transform-origin: center center;
}

/* Toast Notification */
#toastContainer {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: none;
}

.toast {
  background: rgba(0,0,0,0.8);
  color: white;
  padding: 10px 20px;
  border-radius: 4px;
  font-size: 0.9rem;
  animation: slideDown 0.3s ease-out, fadeOut 0.3s ease-in forwards 2.5s;
  box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}

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

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

/* Export loading state */
.btn-loading {
  position: relative;
  color: transparent !important;
  pointer-events: none;
}

.btn-loading::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 18px;
  height: 18px;
  border: 2px solid #fff;
  border-bottom-color: transparent;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  animation: rotate 1s linear infinite;
}

@keyframes rotate {
  100% { transform: translate(-50%, -50%) rotate(360deg); }
}

/* Image Selected Highlight */
.grid-cell.selected {
  outline: 2px dashed var(--primary-color);
  outline-offset: -2px;
  z-index: 5;
}

/* Bottom Hint Bar */
.bottom-hint {
  height: 40px;
  background-color: #374151; /* Dark gray */
  color: #f3f4f6;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  padding: 0 20px;
  flex-shrink: 0;
  width: 100%;
}

/* Image Toolbar */
.img-toolbar {
  position: absolute;
  right: 12px;
  bottom: 12px;
  background: rgba(31, 41, 55, 0.85); /* Semi-transparent dark */
  backdrop-filter: blur(4px);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  padding: 6px;
  gap: 6px;
  z-index: 10;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.tb-row {
  display: flex;
  gap: 6px;
  justify-content: center;
}

.img-toolbar button {
  background: rgba(255, 255, 255, 0.1);
  border: none;
  border-radius: 4px;
  color: #fff;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
  font-size: 1.1rem;
}

.img-toolbar button:hover {
  background: rgba(255, 255, 255, 0.25);
}

.img-toolbar button.tb-danger:hover {
  background: rgba(239, 68, 68, 0.8); /* Red */
}

/* Annotation Layer Styles */
.annotation-element {
  position: absolute;
  cursor: move;
  z-index: 20;
}
.annotation-element.active .anno-shape {
  outline: 2px dashed rgba(31, 41, 55, 0.4);
  outline-offset: 4px;
}
.anno-text {
  min-width: 50px;
  min-height: 20px;
  font-weight: bold;
  outline: none;
  cursor: text;
  white-space: nowrap;
}
.anno-shape-arrow svg {
  display: block;
}

/* Annotation Property Toolbar */
.anno-prop-toolbar {
  position: absolute;
  top: -50px;
  left: 0;
  background: #1e293b;
  border-radius: 8px;
  display: none;
  align-items: center;
  padding: 6px 10px;
  gap: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  z-index: 100;
  cursor: default;
  white-space: nowrap;
}
.annotation-element.active .anno-prop-toolbar {
  display: flex;
}
.anno-prop-toolbar input[type="number"] {
  width: 55px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: white;
  border-radius: 4px;
  padding: 4px;
  outline: none;
}
.anno-prop-toolbar input[type="color"] {
  width: 28px;
  height: 28px;
  padding: 0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  background: transparent;
}
.anno-prop-toolbar .anno-btn-delete {
  background: #ef4444;
  color: white;
  border: none;
  width: 28px;
  height: 28px;
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}
.anno-prop-toolbar .anno-btn-delete:hover {
  background: #dc2626;
}

/* ===== Watermark Controls ===== */
.wm-enable-label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-size: 0.9rem;
  color: var(--text-dark);
  user-select: none;
}

.wm-enable-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
  cursor: pointer;
  accent-color: var(--primary-color);
  flex-shrink: 0;
}

#wmControls {
  margin-top: 12px;
}

.wm-text-input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  font-size: 0.9rem;
  color: var(--text-dark);
  outline: none;
  transition: border-color 0.2s;
  background: #fff;
}

.wm-text-input:focus {
  border-color: var(--primary-color);
}

.wm-density-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  margin-bottom: 8px;
}

.btn-wm-density {
  padding: 7px 0;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  background: #f3f4f6;
  font-size: 0.85rem;
  cursor: pointer;
  text-align: center;
  color: var(--text-dark);
  transition: all 0.2s;
}

.btn-wm-density:hover {
  background: #e9ecef;
}

.btn-wm-density.active {
  background: #fff;
  border-color: var(--primary-color);
  color: var(--primary-color);
}

.wm-pos-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
}

.btn-wm-pos {
  padding: 7px 4px;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  background: #f3f4f6;
  font-size: 1rem;
  cursor: pointer;
  text-align: center;
  color: var(--text-dark);
  line-height: 1;
  transition: all 0.2s;
}

.btn-wm-pos:hover {
  background: #e9ecef;
}

.btn-wm-pos.active {
  background: #fff;
  border-color: var(--primary-color);
  color: var(--primary-color);
}

/* ===== 工具切换按钮（top-nav 右上角，多工具页面共用） ===== */
/* 顶部工具切换按钮（浅色顶栏上的白底胶囊；当前页为淡蓝高亮） */
.nav-links .tool-switch {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  border-radius: 18px;
  border: 1px solid #e1e8f3;
  background: #ffffff;
  font-size: 0.9rem;
  color: #4c5b76;
  text-decoration: none;
  transition: background 0.2s, border-color 0.2s, color 0.2s, box-shadow 0.2s;
}

.nav-links .tool-switch:hover {
  background: #f4f8ff;
  border-color: #c9dcfb;
  color: var(--primary-color);
}

.nav-links .tool-switch.active {
  background: #e6eefe;
  border-color: #c3d8f9;
  color: #1f5fd0;
  font-weight: 600;
}

.nav-links .tool-switch.active:hover {
  background: #e6eefe;
  border-color: #c3d8f9;
  color: #1f5fd0;
}

/* ===== 左栏工具内 Tab（模板拼图 / 长图拼接） ===== */
.left-tabs {
  display: flex;
  gap: 8px;
  width: 100%;
}

.left-tab {
  flex: 1;
  padding: 8px 0;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  background: #f3f4f6;
  font-size: 0.9rem;
  color: var(--text-dark);
  cursor: pointer;
  transition: all 0.2s;
}

.left-tab:hover {
  color: var(--primary-color);
  border-color: var(--primary-color);
}

.left-tab.active {
  background: var(--primary-color);
  border-color: var(--primary-color);
  color: #fff;
}

.left-tab.active:hover {
  color: #fff;
}

/* ===== 长图拼接面板 ===== */
.stitch-panel {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

.stitch-label {
  font-size: 0.9rem;
  color: var(--text-dark);
  font-weight: 600;
}

.stitch-direction {
  margin-bottom: 16px;
}

.stitch-dir-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 8px;
}

.btn-stitch-dir {
  padding: 8px 4px;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  background: #f3f4f6;
  font-size: 0.9rem;
  cursor: pointer;
  color: var(--text-dark);
  transition: all 0.2s;
}

.btn-stitch-dir:hover {
  background: #e9ecef;
}

.btn-stitch-dir.active {
  background: #fff;
  border-color: var(--primary-color);
  color: var(--primary-color);
}

.stitch-list-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.stitch-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.stitch-item {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 6px;
  background: #fff;
}

.stitch-item img {
  width: 52px;
  height: 40px;
  object-fit: cover;
  border-radius: 3px;
}

.stitch-item-idx {
  font-size: 0.8rem;
  color: var(--text-light);
  width: 1.4em;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.stitch-item-actions {
  margin-left: auto;
  display: flex;
  gap: 2px;
}

.stitch-item-actions button {
  border: none;
  background: transparent;
  color: var(--text-light);
  cursor: pointer;
  font-size: 0.95rem;
  padding: 2px 4px;
  border-radius: 4px;
}

.stitch-item-actions button:hover:not(:disabled) {
  color: var(--primary-color);
  background: #f0f7ff;
}

.stitch-item-actions button:disabled {
  opacity: 0.3;
  cursor: default;
}

/* ===== 长图拼接画布（可滚动） ===== */
.stitch-wrapper {
  overflow: auto;
  align-items: flex-start;
}

.stitch-board {
  display: flex;
  line-height: 0;
  box-shadow: 0 10px 32px rgba(0, 0, 0, 0.14);
  border-radius: 2px;
}

.stitch-board img {
  display: block;
}

.stitch-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  width: 360px;
  height: 360px;
  border: 2px dashed var(--border-color);
  border-radius: 12px;
  color: var(--text-light);
  font-size: 0.9rem;
  line-height: 1.5;
  cursor: pointer;
  transition: all 0.2s;
}

.stitch-empty:hover {
  border-color: var(--primary-color);
  color: var(--primary-color);
  background: #f0f7ff;
}

.stitch-empty i {
  font-size: 2.5rem;
}

