/* ── Reset & Variables ─────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #1a1a1a;
  --surface: #252525;
  --border: #3a3a3a;
  --accent: #4f8ef7;
  --accent-hover: #6fa3ff;
  --text: #e0e0e0;
  --text-muted: #888;
  --danger: #e05555;
  --success: #4caf50;
  --warning: #f0a040;
  --toolbar-w: 60px;
  --panel-w: 280px;
  --topbar-h: 48px;
  --radius: 8px;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 13px;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* ── Top Bar ───────────────────────────────────────────── */
#topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  flex-wrap: nowrap;
  padding: 0 12px;
  gap: 8px;
  flex-shrink: 0;
  z-index: 10;
  height: var(--topbar-h);
  overflow: hidden;
}

#topbar .logo {
  font-weight: 700;
  font-size: 14px;
  color: var(--accent);
  white-space: nowrap;
  flex-shrink: 0;
}

#topbar .sep { flex: 1; min-width: 4px; }

.status-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  display: inline-block;
  margin-right: 4px;
}
.status-dot.ok { background: var(--success); }
.status-dot.err { background: var(--danger); }

/* ── Main Layout ───────────────────────────────────────── */
#layout {
  flex: 1;
  display: flex;
  overflow: hidden;
}

/* ── Toolbar (left) ────────────────────────────────────── */
#toolbar {
  width: var(--toolbar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px 0;
  gap: 2px;
  flex-shrink: 0;
  overflow-y: auto;
}

.tool-btn {
  width: 48px;
  min-height: 40px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: none;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 500;
  line-height: 1.3;
  text-align: center;
  padding: 4px 2px;
  transition: background .15s, color .15s, border-color .15s;
}
.tool-btn:hover { background: var(--border); color: var(--text); }
.tool-btn.active {
  background: rgba(79,142,247,.2);
  color: var(--accent);
  border-color: var(--accent);
}

.tool-sep {
  width: 24px;
  height: 1px;
  background: var(--border);
  margin: 4px 0;
}

/* ── Canvas Area ───────────────────────────────────────── */
#canvas-area {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #111;
  overflow: hidden;
  position: relative;
}

#canvas-wrap {
  position: relative;
  cursor: crosshair;
}

#canvas-image, #canvas-mask {
  position: absolute;
  top: 0; left: 0;
  display: block;
}

#canvas-image { z-index: 1; }
#canvas-mask  { z-index: 2; opacity: .6; }

.upload-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  color: var(--text-muted);
}
.upload-placeholder .icon { font-size: 56px; }
.upload-placeholder p { font-size: 15px; }

/* ── Right Panel ───────────────────────────────────────── */
#panel {
  width: var(--panel-w);
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow-y: auto;
}

.panel-section {
  padding: 12px;
  border-bottom: 1px solid var(--border);
}
.panel-section h3 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-muted);
  margin-bottom: 10px;
}

/* ── Form Controls ─────────────────────────────────────── */
label { display: block; margin-bottom: 4px; color: var(--text-muted); font-size: 11px; }

input[type="text"],
input[type="number"],
select,
textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  padding: 5px 8px;
  font-size: 12px;
  outline: none;
  transition: border-color .15s;
}
input[type="text"]:focus,
input[type="number"]:focus,
select:focus,
textarea:focus { border-color: var(--accent); }

textarea { resize: vertical; min-height: 56px; }

.row { display: flex; gap: 6px; align-items: center; margin-bottom: 8px; }
.row label { margin: 0; flex-shrink: 0; }
.row input { flex: 1; }

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
  background: none;
  border: none;
  padding: 0;
}

.range-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 8px;
}
.range-row label { flex-shrink: 0; width: 72px; margin: 0; }
.range-row input[type="range"] { flex: 1; }
.range-row .val { width: 36px; text-align: right; color: var(--text); }

.check-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 8px;
}
.check-row input[type="checkbox"] { accent-color: var(--accent); }

/* ── Buttons ───────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 6px 12px;
  border-radius: var(--radius);
  border: none;
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  transition: background .15s, opacity .15s;
}
.btn:disabled { opacity: .45; cursor: not-allowed; }

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }

.btn-secondary { background: var(--border); color: var(--text); }
.btn-secondary:hover:not(:disabled) { background: #4a4a4a; }

.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: #e86666; }

.btn-block { width: 100%; margin-bottom: 6px; }

/* ── Brush size indicator ──────────────────────────────── */
#brush-cursor {
  position: fixed;          /* 相对视口，与 clientX/Y 坐标系一致 */
  border: 2px solid rgba(255,255,255,.7);
  border-radius: 50%;
  pointer-events: none;
  z-index: 9999;
  display: none;
  transform: translate(-50%, -50%);  /* 圆心对齐指针 */
}

/* ── SAM tool sub-panels ───────────────────────────────── */
.sub-panel {
  display: none;
  margin-top: 6px;
}
.sub-panel.visible { display: block; }

/* ── Spinner / loading ─────────────────────────────────── */
.spinner {
  display: inline-block;
  width: 14px; height: 14px;
  border: 2px solid rgba(255,255,255,.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .6s linear infinite;
  vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Toast ─────────────────────────────────────────────── */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #333;
  color: var(--text);
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 13px;
  z-index: 1000;
  opacity: 0;
  transition: opacity .25s;
  pointer-events: none;
  max-width: 360px;
  text-align: center;
}
#toast.show { opacity: 1; }
#toast.error { background: var(--danger); }
#toast.success { background: var(--success); color: #fff; }

/* ── Mask opacity slider in topbar ────────────────────── */
.topbar-label { color: var(--text-muted); font-size: 11px; white-space: nowrap; }

.topbar-opacity-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.status-wrap {
  display: flex;
  align-items: center;
  gap: 0;
  flex-shrink: 0;
}

/* 移动端才显示的面板透明度控件 */
.mobile-only { display: none; }

/* 桌面端隐藏折叠按钮和浮动撤回 */
#panel-toggle { display: none; }
#float-undo   { display: none; }

/* ── Tab Buttons ───────────────────────────────────────── */
.tab-group {
  display: flex;
  gap: 2px;
  background: var(--bg);
  border-radius: var(--radius);
  padding: 2px;
}
.tab-btn {
  padding: 4px 12px;
  border: none;
  border-radius: 6px;
  background: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  transition: background .15s, color .15s;
}
.tab-btn:hover { color: var(--text); }
.tab-btn.active { background: var(--surface); color: var(--text); }

/* ── Composite Page ─────────────────────────────────────── */
.comp-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
}
.comp-label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .05em;
  margin-bottom: 8px;
}
.comp-preview {
  width: 100%;
  height: 140px;
  border: 1px solid var(--border);
  border-radius: 4px;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: #0a0a0a;
}
.comp-preview img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.op-btn {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
  cursor: pointer;
  padding: 8px 6px;
  font-size: 12px;
  font-weight: 600;
  text-align: center;
  transition: border-color .15s, color .15s, background .15s;
  line-height: 1.4;
}
.op-btn span { font-size: 10px; font-weight: 400; color: var(--text-muted); display: block; }
.op-btn:hover { border-color: var(--accent); color: var(--text); }
.op-btn.active { border-color: var(--accent); background: rgba(79,142,247,.15); color: var(--accent); }
.op-btn.active span { color: var(--accent); opacity: .7; }

/* ── Mobile (portrait ≤ 768px) ─────────────────────────── */
@media (max-width: 768px) {

  /* topbar 改成两行 */
  #topbar {
    height: auto;
    flex-wrap: wrap;
    padding: 6px 10px;
    gap: 6px;
    row-gap: 6px;
  }

  /* 第一行：Logo（左） + 状态（右） */
  #topbar .logo { order: 1; }
  #topbar .sep:first-of-type { order: 2; flex: 1; }
  #topbar .status-wrap { order: 3; display: flex; align-items: center; gap: 4px; }

  /* 第二行：Tab + 上传按钮，各占满宽 */
  .tab-group {
    order: 10;
    width: 100%;
    justify-content: stretch;
  }
  .tab-btn { flex: 1; text-align: center; padding: 5px 0; }

  #upload-btn { order: 11; width: 100%; }

  /* 透明度行隐藏（移到面板内） */
  .topbar-opacity-row { display: none !important; }

  /* topbar 里 sep（弹簧）在移动端不撑宽 */
  #topbar .sep { display: none; }

  /* 主布局：纵向 */
  #layout {
    flex-direction: column;
    overflow: hidden;
  }

  /* 工具栏：横向滚动条 */
  #toolbar {
    width: 100%;
    height: auto;
    min-height: 48px;
    flex-direction: row;
    flex-wrap: nowrap;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 6px 8px;
    overflow-x: auto;
    overflow-y: visible;
    gap: 4px;
    -webkit-overflow-scrolling: touch;
  }

  .tool-btn {
    flex-shrink: 0;
    width: 56px;
    min-height: 44px;
    font-size: 12px;
  }

  .tool-sep {
    width: 1px;
    height: 36px;
    margin: 0 2px;
    flex-shrink: 0;
  }

  /* Canvas 区域占满剩余高度 */
  #canvas-area { flex: 1; min-height: 0; }

  /* 底部面板：固定高度，纵向滚动 */
  #panel {
    width: 100%;
    height: 220px;
    flex-shrink: 0;
    border-left: none;
    border-top: 1px solid var(--border);
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    transition: height .2s ease;
  }

  .panel-section {
    flex-shrink: 0;
    min-width: 220px;
    max-width: 280px;
    border-bottom: none;
    border-right: 1px solid var(--border);
    overflow-y: auto;
  }

  /* 遮罩运算页在移动端也要正常滚动 */
  #page-composite {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 16px;
  }

  #page-composite > div {
    max-width: 100%;
  }

    /* 移动端专属显示 */
  .mobile-only { display: flex !important; }

  /* 折叠面板按钮 */
  #panel-toggle {
    display: block;
    position: fixed;
    bottom: 228px;   /* 面板高度 220px + 8px */
    right: 12px;
    z-index: 200;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 20px;
    padding: 6px 14px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0,0,0,.4);
    transition: bottom .2s;
  }
  #panel-toggle.panel-hidden {
    bottom: 12px;
  }

  /* 面板隐藏时 */
  #panel.panel-hidden {
    height: 0 !important;
    overflow: hidden;
    border-top: none;
  }

  /* 遮罩运算：单列 */
  #page-composite [style*="grid-template-columns"] {
    display: flex !important;
    flex-direction: column;
    gap: 12px;
  }

  /* 运算按钮：2列 */
  #op-buttons {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* 浮动撤回按钮（沉浸模式时左下角） */
  #float-undo {
    position: fixed;
    bottom: 12px;
    left: 12px;
    z-index: 200;
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 20px;
    padding: 6px 14px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0,0,0,.4);
  }
  #float-undo:disabled { opacity: .45; cursor: not-allowed; }

  /* 沉浸模式：隐藏顶栏和工具栏，显示浮动撤回按钮 */
  body.immersive #topbar  { display: none !important; }
  body.immersive #toolbar { display: none !important; }
  body.immersive #float-undo { display: block; }
}
