:root {
  --bg: #faf8f5;
  --surface: #ffffff;
  --surface2: #f5f3ef;
  --border: #e0dbd4;
  --text: #2c2420;
  --text2: #8a7e74;
  --accent: #c0562b;
  --accent2: #a04520;
  --accent-glow: rgba(192, 86, 43, 0.15);
  --accent-light: #fdf5f0;
  --blue: #2563eb;
  --green: #16a34a;
  --yellow: #d97706;
  --purple: #7c3aed;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.08);
}

body.dark {
  --bg: #1a1816;
  --surface: #252320;
  --surface2: #2d2a25;
  --border: #3d3830;
  --text: #e8e2d8;
  --text2: #9a8e80;
  --accent: #d9774a;
  --accent2: #b85d35;
  --accent-glow: rgba(217, 119, 74, 0.2);
  --accent-light: #2d251f;
  --shadow: 0 1px 3px rgba(0,0,0,0.2);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.3);
}

body.dark #sealCanvas.bg-white { background: #2c2420; }

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
}

.header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 14px 28px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  overflow: hidden;
}

.logo img {
  width: 100%;
  height: 100%;
  display: block;
}

.header-title {
  font-size: 19px;
  font-weight: 800;
  color: var(--text);
}

.header-sub {
  font-size: 11px;
  color: var(--text2);
  margin-top: 1px;
  letter-spacing: 0.5px;
}

.header-actions {
  display: flex;
  gap: 8px;
}

.main {
  display: flex;
  flex: 1;
  min-height: 0;
}

.main-tabs {
  display: flex;
  padding: 6px 28px 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  gap: 2px;
  position: sticky;
  top: 67px;
  z-index: 99;
}

.main-tab {
  padding: 10px 20px;
  font-size: 14px;
  font-weight: 700;
  color: var(--text2);
  cursor: pointer;
  border: none;
  background: none;
  border-bottom: 3px solid transparent;
  transition: all 0.2s;
  margin-bottom: -1px;
}

.main-tab:hover { color: var(--text); }
.main-tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.main-content { display: none; }
.main-content.active { display: flex; height: calc(100vh - 112px); }

/* Stamp mode panels */
.stamp-upload-area {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 20px;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s;
  margin-bottom: 12px;
  color: var(--text2);
  font-size: 13px;
}

.stamp-upload-area:hover {
  border-color: var(--accent);
  background: var(--accent-light);
  color: var(--accent);
}

.stamp-upload-area.has-images {
  border-style: solid;
  padding: 10px;
}

.stamp-img-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
  max-height: 200px;
  overflow-y: auto;
}

.stamp-img-item {
  width: 70px;
  height: 70px;
  border-radius: 6px;
  border: 2px solid var(--border);
  cursor: pointer;
  position: relative;
  overflow: hidden;
  background-size: cover;
  background-position: center;
}

.stamp-img-item:hover { border-color: var(--accent); }
.stamp-img-item.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-glow);
}

.stamp-img-item .remove-btn {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 18px;
  height: 18px;
  background: rgba(0,0,0,0.6);
  color: #fff;
  border: none;
  border-radius: 50%;
  font-size: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.stamp-preview-wrap {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-md);
  background:
    repeating-conic-gradient(#e8e8e8 0% 25%, #fff 0% 50%) 50% / 20px 20px;
}

.stamp-preview-wrap canvas {
  display: block;
}

#stampBgCanvas {
  max-width: 80vw;
  max-height: 65vh;
}

.stamp-seal-overlay {
  position: absolute;
  cursor: move;
  pointer-events: auto;
  z-index: 5;
  /* size set by JS */
}

.stamp-seal-overlay canvas {
  display: block;
  max-width: none;
  max-height: none;
}

.stamp-empty-hint {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: var(--text2);
  font-size: 14px;
  text-align: center;
  pointer-events: none;
}

.stamp-empty-hint .hint-icon {
  font-size: 48px;
  display: block;
  margin-bottom: 12px;
  opacity: 0.4;
}

.panel-left {
  width: 370px;
  min-width: 370px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 18px;
}

.panel-left::-webkit-scrollbar { width: 4px; }
.panel-left::-webkit-scrollbar-track { background: transparent; }
.panel-left::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.panel-center {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    repeating-linear-gradient(0deg, transparent, transparent 19px, rgba(0,0,0,0.015) 19px, rgba(0,0,0,0.015) 20px),
    repeating-linear-gradient(90deg, transparent, transparent 19px, rgba(0,0,0,0.015) 19px, rgba(0,0,0,0.015) 20px),
    var(--bg);
  position: relative;
}

.preview-wrapper {
  position: relative;
}

.preview-canvas-wrap {
  position: relative;
  z-index: 1;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-md);
  background: #fff;
}

#sealCanvas {
  display: block;
}

.preview-size-label {
  position: absolute;
  bottom: -28px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 11px;
  color: var(--text2);
  white-space: nowrap;
}

.panel-right {
  width: 250px;
  min-width: 250px;
  background: var(--surface);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  padding: 18px;
}

.panel-right::-webkit-scrollbar { width: 4px; }
.panel-right::-webkit-scrollbar-track { background: transparent; }
.panel-right::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.section {
  margin-bottom: 18px;
}

.section-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.8px;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.section-title::before {
  content: '';
  width: 3px;
  height: 14px;
  background: var(--accent);
  border-radius: 2px;
}

.tabs {
  display: flex;
  gap: 3px;
  background: var(--surface2);
  padding: 3px;
  border-radius: var(--radius);
  margin-bottom: 14px;
}

.tab {
  flex: 1;
  padding: 7px 4px;
  text-align: center;
  font-size: 12px;
  font-weight: 600;
  color: var(--text2);
  border-radius: 7px;
  cursor: pointer;
  transition: all 0.2s;
  border: none;
  background: none;
}

.tab:hover { color: var(--text); background: rgba(0,0,0,0.04); }
.tab.active {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 1px 4px var(--accent-glow);
}

.form-group {
  margin-bottom: 12px;
}

.form-label {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--text2);
  margin-bottom: 5px;
}

.form-input {
  width: 100%;
  padding: 8px 10px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 7px;
  color: var(--text);
  font-size: 13px;
  transition: border-color 0.2s, box-shadow 0.2s;
  outline: none;
}

.form-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.form-input::placeholder { color: #aaa; }

select.form-input {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%238a7e74' viewBox='0 0 16 16'%3E%3Cpath d='M8 11L3 6h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 28px;
}

.range-group {
  display: flex;
  align-items: center;
  gap: 10px;
}

.range-group input[type="range"] {
  flex: 1;
  -webkit-appearance: none;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  outline: none;
}

.range-group input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  box-shadow: 0 1px 4px var(--accent-glow);
}

.range-val {
  font-size: 12px;
  color: var(--text2);
  min-width: 32px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.color-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.color-presets {
  display: flex;
  gap: 5px;
}

.color-dot {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid transparent;
  transition: all 0.15s;
}

.color-dot:hover, .color-dot.active {
  border-color: var(--text);
  transform: scale(1.12);
}

input[type="color"] {
  width: 30px;
  height: 30px;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  background: none;
  padding: 2px;
}

input[type="color"]::-webkit-color-swatch-wrapper { padding: 1px; }
input[type="color"]::-webkit-color-swatch { border-radius: 4px; border: none; }

.btn {
  padding: 8px 16px;
  border-radius: 7px;
  font-size: 12px;
  font-weight: 600;
  border: none;
  cursor: pointer;
  transition: all 0.15s;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

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

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

.btn-secondary {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-secondary:hover { background: var(--border); }

.btn-block { width: 100%; justify-content: center; }

.template-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.template-card {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
  text-align: center;
  cursor: pointer;
  transition: all 0.15s;
}

.template-card:hover {
  border-color: var(--accent);
  background: var(--accent-light);
}

.template-card.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-glow);
}

.template-card canvas {
  width: 70px;
  height: 70px;
  margin-bottom: 4px;
}

.template-card .name {
  font-size: 11px;
  color: var(--text2);
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.toggle-label {
  font-size: 12px;
  color: var(--text);
}

.toggle {
  position: relative;
  width: 38px;
  height: 20px;
  background: var(--border);
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.2s;
}

.toggle.active { background: var(--accent); }

.toggle::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.2s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.15);
}

.toggle.active::after { transform: translateX(18px); }

.divider {
  height: 1px;
  background: var(--border);
  margin: 16px 0;
}

.seal-style-tabs {
  display: flex;
  gap: 3px;
  background: var(--surface2);
  padding: 3px;
  border-radius: 7px;
}

.seal-style-tab {
  flex: 1;
  padding: 6px 8px;
  text-align: center;
  font-size: 12px;
  font-weight: 600;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.15s;
  border: none;
  background: none;
  color: var(--text2);
}

.seal-style-tab:hover { color: var(--text); }
.seal-style-tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.toast {
  position: fixed;
  bottom: 30px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--text);
  color: #fff;
  padding: 10px 22px;
  border-radius: var(--radius);
  font-size: 13px;
  z-index: 9999;
  transition: transform 0.3s ease;
  box-shadow: var(--shadow-md);
}

.toast.show { transform: translateX(-50%) translateY(0); }

.font-list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
  gap: 8px;
}

.font-search {
  flex: 1;
  max-width: 140px;
  padding: 4px 8px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 5px;
  font-size: 11px;
  color: var(--text);
  outline: none;
  transition: border-color 0.2s;
}

.font-search:focus { border-color: var(--accent); }

.font-list {
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--surface2);
  max-height: 260px;
  overflow-y: auto;
  overflow-x: hidden;
}

.font-list::-webkit-scrollbar { width: 4px; }
.font-list::-webkit-scrollbar-track { background: transparent; }
.font-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.font-group-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 10px;
  font-size: 11px;
  font-weight: 700;
  color: var(--text2);
  background: var(--surface);
  cursor: pointer;
  user-select: none;
  position: sticky;
  top: 0;
  z-index: 1;
  border-bottom: 1px solid var(--border);
}

.font-group-title:hover { color: var(--text); }

.font-group-arrow {
  font-size: 10px;
  transition: transform 0.2s;
}

.font-group.collapsed .font-group-arrow { transform: rotate(-90deg); }
.font-group.collapsed .font-group-items { display: none; }

.font-group-items { padding: 3px 0; }

.font-item {
  padding: 6px 12px 6px 16px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  transition: all 0.12s;
  border-left: 3px solid transparent;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.font-item:hover {
  background: var(--accent-light);
  color: var(--accent2);
}

.font-item.active {
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  border-left-color: var(--accent2);
  border-left-width: 4px;
}

.font-item.installed:not(.active) {
  background: rgba(22, 163, 74, 0.06);
  border-left-color: var(--green);
}

.font-item.hidden { display: none; }

.font-custom-row {
  display: flex;
  gap: 6px;
  margin-top: 6px;
  align-items: center;
}

.font-custom-row .form-input { flex: 1; }

/* Preview background modes */
#sealCanvas.bg-paper {
  background: linear-gradient(135deg, #f5f0e1 0%, #ede0c8 30%, #f0e6d3 60%, #e8d8b8 100%);
}

#sealCanvas.bg-white {
  background: #ffffff;
}

.bg-mode-tabs {
  display: flex;
  gap: 3px;
  background: var(--surface2);
  padding: 3px;
  border-radius: var(--radius);
  margin-top: 10px;
}

.bg-mode-tab {
  flex: 1;
  padding: 5px 6px;
  text-align: center;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  border-radius: 5px;
  border: none;
  background: none;
  color: var(--text2);
  transition: all 0.15s;
}

.bg-mode-tab:hover { color: var(--text); }
.bg-mode-tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

/* Font favorites */
.font-item .fav-toggle {
  float: right;
  font-size: 14px;
  opacity: 0.4;
  cursor: pointer;
  transition: opacity 0.15s;
  margin-left: 6px;
}

.font-item .fav-toggle:hover,
.font-item .fav-toggle.fav-active {
  opacity: 1;
}

/* Saved Seals (My Seals) */
.saved-seals-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 6px;
  max-height: 300px;
  overflow-y: auto;
}

.saved-seals-grid::-webkit-scrollbar { width: 4px; }
.saved-seals-grid::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.saved-seal-item {
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 6px;
  text-align: center;
  cursor: pointer;
  transition: all 0.15s;
  position: relative;
}

.saved-seal-item:hover {
  border-color: var(--accent);
  background: var(--accent-light);
}

.saved-seal-item img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: contain;
  display: block;
}

.saved-seal-item .seal-label {
  font-size: 10px;
  color: var(--text2);
  margin-top: 3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.saved-seal-item .del-btn {
  position: absolute;
  top: 3px;
  right: 3px;
  width: 16px;
  height: 16px;
  background: rgba(0,0,0,0.5);
  color: #fff;
  border: none;
  border-radius: 50%;
  font-size: 9px;
  cursor: pointer;
  line-height: 1;
  display: none;
}

.saved-seal-item:hover .del-btn { display: block; }

.saved-seal-item.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-glow);
  background: var(--accent-light);
}

.saved-empty {
  text-align: center;
  font-size: 11px;
  color: var(--text2);
  padding: 16px 0;
  grid-column: 1 / -1;
}

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

.btn-save:hover { background: #15803d; }

@media (max-width: 1100px) {
  .panel-right { display: none; }
}
@media (max-width: 800px) {
  .main { flex-direction: column; height: auto; }
  .panel-left { width: 100%; min-width: auto; max-height: 50vh; }
  .panel-center { min-height: 400px; }
}
