:root {
  --bg: #f0f0f0;
  --surface: #fff;
  --ink: #1a1a1a;
  --muted: #888;
  --accent: #e53935;
  --line: #e0e0e0;
  --toolbar-h: 56px;
  --nav-h: 48px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html, body {
  height: 100%;
  font-family: "PingFang SC", "Noto Sans SC", sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 480px;
  margin: 0 auto;
  background: var(--bg);
}

/* ── Nav ── */
.nav {
  display: flex;
  align-items: center;
  height: var(--nav-h);
  padding: 0 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
  z-index: 10;
}

.nav-back {
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  font-size: 20px;
  cursor: pointer;
  color: var(--ink);
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  flex-shrink: 0;
}

.nav-title {
  flex: 1;
  text-align: center;
  font-size: 17px;
  font-weight: 600;
}

.nav-actions {
  display: flex;
  gap: 4px;
}

.nav-btn {
  border: none;
  background: none;
  font-size: 14px;
  color: var(--accent);
  cursor: pointer;
  padding: 6px 8px;
  white-space: nowrap;
}

/* ── Preview ── */
.preview-wrap {
  flex: 1;
  overflow: auto;
  padding: 12px;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.sheet-shadow {
  box-shadow: 0 2px 12px rgba(0,0,0,.12);
  background: #fff;
  line-height: 0;
}

#sheetCanvas {
  display: block;
  width: 100%;
  height: auto;
}

/* ── Input bar ── */
.input-bar {
  padding: 8px 12px;
  background: var(--surface);
  border-top: 1px solid var(--line);
  flex-shrink: 0;
}

.input-bar label {
  font-size: 12px;
  color: var(--muted);
  display: block;
  margin-bottom: 4px;
}

.input-row {
  display: flex;
  gap: 8px;
}

.input-row input {
  flex: 1;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 16px;
  outline: none;
}

.input-row input:focus {
  border-color: var(--accent);
}

.input-row button {
  border: none;
  background: var(--accent);
  color: #fff;
  border-radius: 8px;
  padding: 0 16px;
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
}

/* ── Bottom toolbar ── */
.toolbar {
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding-bottom: var(--safe-bottom);
  flex-shrink: 0;
  z-index: 20;
}

.tool-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 0 6px;
  border: none;
  background: none;
  cursor: pointer;
  color: var(--muted);
  font-size: 10px;
  transition: color .15s;
}

.tool-item.active {
  color: var(--accent);
}

.tool-icon {
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
}

/* ── Bottom sheet popup ── */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.35);
  z-index: 30;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
}

.overlay.open {
  opacity: 1;
  pointer-events: auto;
}

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  max-width: 480px;
  margin: 0 auto;
  background: var(--surface);
  border-radius: 16px 16px 0 0;
  z-index: 40;
  transform: translateY(100%);
  transition: transform .25s ease;
  padding-bottom: calc(12px + var(--safe-bottom));
}

.sheet.open {
  transform: translateY(0);
}

.sheet-title {
  text-align: center;
  font-size: 15px;
  font-weight: 600;
  padding: 16px;
  border-bottom: 1px solid var(--line);
}

.sheet-list {
  list-style: none;
}

.sheet-scroll {
  max-height: min(65vh, 520px);
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  touch-action: pan-y;
}

.sheet-scroll .sheet-list {
  max-height: none;
  overflow: visible;
}

/* other panels keep compact scroll */
#sheetBody:not(:has(.sheet-scroll)) .sheet-list {
  max-height: 50vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.sheet-list li {
  display: flex;
  align-items: center;
  padding: 14px 20px;
  font-size: 15px;
  cursor: pointer;
  border-bottom: 1px solid #f5f5f5;
}

.sheet-list li:active {
  background: #fafafa;
}

.sheet-list li.selected {
  color: var(--accent);
  font-weight: 600;
}

.sheet-list li .radio {
  width: 20px;
  height: 20px;
  border: 2px solid #ccc;
  border-radius: 50%;
  margin-right: 12px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.sheet-list li.selected .radio {
  border-color: var(--accent);
}

.sheet-list li.selected .radio::after {
  content: '';
  width: 10px;
  height: 10px;
  background: var(--accent);
  border-radius: 50%;
}

.sheet-list li .hint {
  margin-left: auto;
  font-size: 12px;
  color: var(--muted);
  font-weight: 400;
}

.sheet-group-label {
  padding: 10px 20px 4px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-transform: none;
  letter-spacing: 0.05em;
}

.sheet-footnote {
  padding: 12px 20px 4px;
  font-size: 11px;
  color: var(--muted);
  text-align: center;
  line-height: 1.5;
}

.sheet-loading {
  padding: 24px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
}

.sheet-scroll-hint {
  position: sticky;
  bottom: 0;
  padding: 8px;
  text-align: center;
  font-size: 11px;
  color: var(--muted);
  background: linear-gradient(transparent, var(--surface) 40%);
  pointer-events: none;
}

/* color swatches */
.color-row {
  display: flex;
  gap: 12px;
  padding: 16px 20px;
  flex-wrap: wrap;
}

.color-swatch {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 3px solid transparent;
  cursor: pointer;
}

.color-swatch.selected {
  border-color: var(--ink);
}

/* trace count stepper */
.stepper-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 24px;
  padding: 20px;
}

.stepper-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: #fafafa;
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.stepper-val {
  font-size: 24px;
  font-weight: 600;
  min-width: 40px;
  text-align: center;
}

/* print — desktop only; mobile uses image export */
@media print {
  body * { visibility: hidden; }
  .preview-wrap, .preview-wrap * { visibility: visible; }
  .preview-wrap {
    position: fixed;
    inset: 0;
    padding: 0;
    overflow: visible;
  }
  .sheet-shadow { box-shadow: none; }
  #sheetCanvas { width: 100%; height: auto; }
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(80px + var(--safe-bottom));
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 12px 16px;
  background: rgba(26, 26, 26, 0.92);
  color: #fff;
  font-size: 14px;
  line-height: 1.45;
  border-radius: 10px;
  z-index: 100;
  text-align: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity .2s;
}

.toast.show {
  opacity: 1;
}

.export-preview {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0, 0, 0, 0.75);
  display: flex;
  flex-direction: column;
  padding: 16px;
  padding-bottom: calc(16px + var(--safe-bottom));
}

.export-preview-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  color: #fff;
  font-size: 13px;
  margin-bottom: 10px;
  flex-shrink: 0;
}

.export-preview-close {
  border: none;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 14px;
  cursor: pointer;
  flex-shrink: 0;
}

.export-preview-scroll {
  flex: 1;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  text-align: center;
  background: rgba(255, 255, 255, 0.06);
  border-radius: 8px;
}

.export-preview-scroll img {
  max-width: 100%;
  height: auto;
  background: #fff;
  border-radius: 4px;
  -webkit-touch-callout: default;
  user-select: none;
  pointer-events: auto;
}
