:root {
  --bg: #f6f7fb;
  --card: #ffffff;
  --text: #2b3147;
  --muted: #8b93a8;
  --primary: #ff7a59;
  --primary-dark: #f25f3d;
  --soft: #eef1f8;
  --border: #e5e8f0;
  --radius-lg: 22px;
  --radius-md: 14px;
  --radius-sm: 10px;
  --shadow: 0 10px 30px rgba(40, 50, 90, 0.08);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  /* 游戏页不制造滚动高度；实际可见高度由 shared/screen.js 同步。 */
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", sans-serif;
  background:
    radial-gradient(1200px 600px at 10% -10%, #ffe9df 0%, transparent 60%),
    radial-gradient(900px 500px at 100% 0%, #e6f0ff 0%, transparent 60%),
    var(--bg);
  color: var(--text);
  /* manipulation 仍允许捏合；全页防缩放由 shared/screen.css 负责 */
  touch-action: manipulation;
}

/* ---------- 整屏网格：顶栏 + 左画布 + 右工具栏 ----------
   shared/screen.js 设置实际可见高度，沿 html/body/.app 的 100% 链传递 */
.app {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 220px;
  grid-template-rows: auto minmax(0, 1fr);
  grid-template-areas:
    "header header"
    "workspace toolbar";
  gap: 14px;
  padding: 14px;
}

.app-header {
  grid-area: header;
  display: flex;
  align-items: center;
  justify-content: center;
}

.app-header h1 {
  margin: 0;
  font-size: 24px;
  letter-spacing: 1px;
}

/* ---------- 工作区 / 画布：白色画布卡铺满整个工作区，
   图片 contain 居中（JS 按显示尺寸×DPR 动态设置画布内部分辨率，放大不糊） ---------- */
.workspace {
  grid-area: workspace;
  min-width: 0;
  min-height: 0;
  height: 100%;
  padding: 2px;
}

.stage {
  position: relative;
  width: 100%;
  height: 100%;
}

.canvas-wrap {
  position: absolute;
  inset: 0;
  background: var(--card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  overflow: hidden;
  touch-action: none;
}

#board,
#overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

#overlay {
  cursor: crosshair;
  touch-action: none;
}

/* ---------- 右侧工具栏：操作按钮横排在上，画廊 2 列网格纵向滚动填满剩余高度 ---------- */
.toolbar {
  grid-area: toolbar;
  min-width: 0;
  min-height: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.tool-group {
  background: var(--card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 14px;
}

.actions {
  display: flex;
  flex-direction: row;
  gap: 10px;
}

.actions .btn { flex: 1; }

.btn {
  appearance: none;
  border: 0;
  font-size: 15px;
  font-weight: 600;
  padding: 12px 10px;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: transform 0.05s, background 0.15s, box-shadow 0.15s;
  white-space: nowrap;
}

.btn:active { transform: translateY(1px); }

.btn-primary {
  background: var(--primary);
  color: #fff;
  box-shadow: 0 4px 12px rgba(255, 122, 89, 0.35);
}

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

.btn-soft {
  background: var(--soft);
  color: var(--text);
}

.btn-soft:hover { background: #e6eaf5; }

/* ---------- 画廊 ---------- */
.gallery-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex: 1;
  min-height: 0;
}

.gallery-group .tool-label {
  color: var(--muted);
  font-size: 14px;
  white-space: nowrap;
}

/* 横屏：2 列网格 + 纵向滚动，把右侧栏的高度用起来 */
.thumbs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  align-content: start;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.thumb {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 3;
  border-radius: var(--radius-sm);
  overflow: hidden;
  cursor: pointer;
  border: 3px solid transparent;
  background: var(--soft);
  padding: 0;
  transition: border-color 0.15s, transform 0.05s;
}

.thumb:hover { transform: translateY(-1px); }

.thumb.active { border-color: var(--primary); }

.thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  /* 缩略图也显示灰度态，与画布一致 */
  filter: grayscale(1) brightness(1.08);
}

/* 加载中流光：缩进 4px 并带圆角，避免和 active 外边框贴死/无圆角冲突 */
.thumb.loading::after {
  content: '';
  position: absolute;
  inset: 4px;
  border-radius: calc(var(--radius-sm) - 4px);
  background: linear-gradient(100deg, transparent 30%, rgba(255, 255, 255, 0.85) 50%, transparent 70%);
  background-size: 200% 100%;
  animation: shimmer 1.1s infinite linear;
  pointer-events: none;
}

@keyframes shimmer {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* 画布加载中转圈 */
.stage-loading {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.55);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  pointer-events: none;
}

.stage-loading.show { display: flex; }

.stage-loading .spinner {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 4px solid rgba(255, 122, 89, 0.25);
  border-top-color: var(--primary);
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.load-more {
  flex: 0 0 auto;
  width: 100%;
}

/* ---------- 竖屏：上下布局，工具栏分两行（画廊一行 + 操作一行），互不挤占 ---------- */
@media (orientation: portrait) {
  .app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas:
      "header"
      "workspace"
      "toolbar";
    gap: 8px;
    padding: 8px;
  }

  .app-header h1 { font-size: 18px; }

  .toolbar {
    flex-direction: column;
    gap: 8px;
    height: auto;
  }

  .tool-group { padding: 8px; }

  /* 第一行：画廊（缩略图横向滚动 + 加载更多固定在右端） */
  .gallery-group {
    order: 1;
    flex-direction: row;
    align-items: center;
    gap: 8px;
    flex: none;
  }

  .gallery-group .tool-label { display: none; }

  .thumbs {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    flex: 1;
    min-width: 0;
    gap: 8px;
  }

  .thumb {
    flex: 0 0 auto;
    width: 88px;
  }

  .load-more {
    width: auto;
    font-size: 13px;
    padding: 10px 12px;
  }

  /* 第二行：两个操作按钮平分 */
  .actions {
    order: 2;
    flex-direction: row;
    gap: 8px;
  }

  .actions .btn {
    font-size: 14px;
    padding: 10px 8px;
  }
}

/* 小高度横屏：只压缩控件。可见高度由 shared/screen.js 跟随地址栏变化，
   不再用大视口高度制造页面滚动。平板横屏仍优先保留宽画布。 */
@media (orientation: landscape) and (max-height: 500px) {
  html, body { overflow: hidden; }

  .app {
    height: 100%;
    grid-template-columns: minmax(0, 1fr) 170px;
    gap: 6px;
    padding: 6px;
  }

  .app-header h1 { font-size: 16px; }

  .toolbar { gap: 6px; }

  .tool-group { padding: 8px; }

  .actions { gap: 6px; }

  .btn {
    font-size: 12px;
    padding: 8px 4px;
  }

  .gallery-group { gap: 6px; }

  .gallery-group .tool-label { font-size: 11px; }

  .thumbs { gap: 6px; }

  .load-more {
    font-size: 11px;
    padding: 7px;
  }
}
