/* =========================================================
   习字工坊 · 设计令牌体系
   primitives(原料) → semantic(语义) → component(组件)
   气质：宣纸暖白 + 墨黑 + 朱砂红印章；暗色仅翻转语义层
   ========================================================= */

:root {
  /* —— 原料层（只有刻度与色值，组件不直接消费） —— */
  --p-seal:        #c0392b;   /* 朱砂红，唯一品牌色，纯色不渐变 */
  --p-seal-soft:  #f7e4e0;
  --p-seal-ink:   #9b2c20;
  --p-ink-900:    #211c18;
  --p-ink-600:    #5b5249;
  --p-ink-400:    #8d8377;
  --p-paper:      #f6f1e7;   /* 宣纸暖白 */
  --p-paper-2:    #efe8da;
  --p-surface:    #ffffff;
  --p-line:       #e8e0d0;
  --p-line-2:     #d9cdb8;
  --p-dark-bg:      #1b1815;
  --p-dark-surface: #242019;
  --p-dark-surface2:#2c2820;
  --p-dark-line:    #39342b;
  --p-dark-line2:   #4a4439;
  --p-dark-ink:     #ece5d8;
  --p-dark-ink-2:   #aba294;
  --p-dark-seal:    #e0745f;
  --p-dark-seal-soft:#3a2620;

  /* —— 语义层（表达“用在哪”） —— */
  --bg:            var(--p-paper);
  --surface:       var(--p-surface);
  --surface-2:     var(--p-paper-2);
  --text:          var(--p-ink-900);
  --text-muted:    var(--p-ink-600);
  --text-faint:    var(--p-ink-400);
  --border:        var(--p-line);
  --border-strong: var(--p-line-2);
  --brand:         var(--p-seal);
  --brand-soft:    var(--p-seal-soft);
  --brand-ink:     var(--p-seal-ink);
  --focus:         rgba(192, 57, 43, 0.30);

  /* —— 间距（4px 基数，关系语言） —— */
  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px;
  --s5: 24px; --s6: 32px; --s7: 48px;

  /* —— 圆角（亲和而克制） —— */
  --r-sm: 8px; --r-md: 12px; --r-lg: 18px;

  /* —— 字号（先锚正文 15px，再派生层级） —— */
  --fs-xs: 12px; --fs-sm: 13px; --fs-base: 15px;
  --fs-md: 17px; --fs-lg: 21px; --fs-xl: 23px;

  /* —— 动效（工具类：低预算） —— */
  --t-fast: 130ms; --t-base: 200ms;

  --font-ui: "PingFang SC", "Microsoft YaHei", "Hiragino Sans GB",
             "Noto Sans SC", -apple-system, BlinkMacSystemFont, sans-serif;
  --font-brush: "Ma Shan Zheng", "KaiTi", "STKaiti", serif;

  --shadow-card: 0 1px 2px rgba(33,28,24,.04), 0 14px 34px rgba(33,28,24,.07);
  --shadow-sheet: 0 1px 1px rgba(33,28,24,.05), 0 18px 48px rgba(33,28,24,.14);
  --shadow-pop: 0 18px 50px rgba(33,28,24,.22);
}

[data-theme="dark"] {
  --bg:            var(--p-dark-bg);
  --surface:       var(--p-dark-surface);
  --surface-2:     var(--p-dark-surface2);
  --text:          var(--p-dark-ink);
  --text-muted:    var(--p-dark-ink-2);
  --text-faint:    #8a8174;
  --border:        var(--p-dark-line);
  --border-strong: var(--p-dark-line2);
  --brand:         var(--p-dark-seal);
  --brand-soft:    var(--p-dark-seal-soft);
  --brand-ink:     #f0b1a4;
  --focus:         rgba(224, 116, 95, 0.38);
  --shadow-card:   0 1px 2px rgba(0,0,0,.4), 0 14px 34px rgba(0,0,0,.5);
  --shadow-sheet:  0 1px 1px rgba(0,0,0,.5), 0 18px 48px rgba(0,0,0,.6);
  --shadow-pop:    0 18px 50px rgba(0,0,0,.6);
}

/* =========================================================
   基础
   ========================================================= */
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: var(--font-ui);
  font-size: var(--fs-base);
  line-height: 1.55;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  transition: background var(--t-base) ease, color var(--t-base) ease;
}
h1, h2, h3 { margin: 0; font-weight: 700; }
button { font-family: inherit; cursor: pointer; }
::selection { background: var(--brand-soft); }

/* =========================================================
   应用骨架：侧栏 + 主区
   ========================================================= */
.app { display: flex; min-height: 100vh; }

.sidebar {
  width: 248px; flex: none;
  display: flex; flex-direction: column;
  padding: var(--s5) var(--s4);
  background: var(--surface);
  border-right: 1px solid var(--border);
  position: sticky; top: 0; height: 100vh;
}

.brand { display: flex; align-items: center; gap: var(--s3); padding: 0 var(--s2) var(--s5); }
.brand__seal {
  width: 42px; height: 42px; flex: none;
  display: grid; place-items: center;
  background: var(--brand-soft);
  border: 2px solid var(--brand);
  border-radius: 10px;
  color: var(--brand);
  font-family: var(--font-brush);
  font-size: 26px; line-height: 1;
}
.brand__text { display: flex; flex-direction: column; line-height: 1.2; }
.brand__name { font-family: var(--font-brush); font-size: 22px; font-weight: 400; letter-spacing: 1px; }
.brand__sub { font-size: var(--fs-xs); color: var(--text-faint); letter-spacing: .5px; }

.nav { display: flex; flex-direction: column; gap: var(--s2); }
.nav__item {
  display: flex; align-items: center; gap: var(--s3);
  width: 100%; padding: var(--s3) var(--s3);
  border: none; border-radius: var(--r-md);
  background: transparent; color: var(--text-muted);
  font-size: var(--fs-base); font-weight: 600;
  text-align: left;
  transition: background var(--t-fast) ease, color var(--t-fast) ease;
}
.nav__item svg { flex: none; color: currentColor; }
.nav__item:hover { background: var(--surface-2); color: var(--text); }
.nav__item.is-active {
  background: var(--brand-soft); color: var(--brand);
  box-shadow: inset 3px 0 0 var(--brand);
}
.nav__item--ghost { color: var(--text-muted); }

.sidebar__foot { margin-top: auto; padding-top: var(--s5); border-top: 1px solid var(--border); }

/* =========================================================
   主区
   ========================================================= */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.panel { display: none; flex: 1; padding: var(--s6) var(--s6) var(--s5); }
.panel.is-active { display: flex; flex-direction: column; }

.panel__head { margin-bottom: var(--s5); }
.panel__title { font-size: var(--fs-xl); letter-spacing: .5px; }
.panel__desc { margin: var(--s1) 0 0; font-size: var(--fs-sm); color: var(--text-muted); }

.layout {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  gap: var(--s6);
  align-items: start;
}

/* —— 控制面板（浮起卡片，靠留白分组） —— */
.controls {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: var(--s5);
  box-shadow: var(--shadow-card);
  position: sticky; top: var(--s5);
  display: flex; flex-direction: column; gap: var(--s5);
}

.field { display: flex; flex-direction: column; gap: var(--s2); }
.field--row { flex-direction: row; align-items: center; justify-content: space-between; }
.field__label {
  display: flex; align-items: center; justify-content: space-between;
  font-size: var(--fs-sm); font-weight: 600; color: var(--text);
  letter-spacing: .2px;
}
.field__value { font-size: var(--fs-xs); color: var(--text-faint); font-weight: 500; font-variant-numeric: tabular-nums; }
.field__help { margin: 0; font-size: var(--fs-xs); color: var(--text-faint); }

/* —— 分段选择器（网格自适应，active 为朱砂实色） —— */
.seg { display: grid; grid-template-columns: repeat(auto-fit, minmax(76px, 1fr)); gap: var(--s2); }
.seg__btn {
  padding: var(--s2) var(--s3);
  border: 1px solid var(--border-strong); border-radius: var(--r-sm);
  background: var(--surface); color: var(--text-muted);
  font-size: var(--fs-sm); font-weight: 600;
  transition: all var(--t-fast) ease;
}
.seg__btn:hover { border-color: var(--brand); color: var(--text); }
.seg__btn.is-active { background: var(--brand); color: #fff; border-color: var(--brand); }

/* —— 模板样式：分组卡片选择器 —— */
.type-list { display: flex; flex-direction: column; gap: var(--s4); }
.type-group { display: flex; flex-direction: column; gap: var(--s2); }
.type-group__label {
  font-size: var(--fs-xs); font-weight: 700; color: var(--text-faint);
  letter-spacing: 1.5px; text-transform: none; padding-left: 2px;
}
.type-chips { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: var(--s2); }
.type-chip {
  padding: var(--s3) var(--s2);
  border: 1px solid var(--border-strong); border-radius: var(--r-sm);
  background: var(--surface); color: var(--text-muted);
  font-size: var(--fs-sm); font-weight: 600; line-height: 1.2;
  transition: all var(--t-fast) ease;
}
.type-chip:hover { border-color: var(--brand); color: var(--text); transform: translateY(-1px); box-shadow: 0 2px 6px rgba(33,28,24,.06); }
.type-chip.is-active {
  background: var(--brand-soft); color: var(--brand-ink);
  border-color: var(--brand); box-shadow: inset 0 0 0 1px var(--brand);
}

/* 禁用的滑块（固定版式） */
.range:disabled { opacity: .45; cursor: not-allowed; }
.range:disabled::-webkit-slider-thumb { background: var(--text-faint); }
.range:disabled::-moz-range-thumb { background: var(--text-faint); }

/* —— 原生控件美化 —— */
.select {
  width: 100%; padding: var(--s2) var(--s3);
  border: 1px solid var(--border-strong); border-radius: var(--r-sm);
  background: var(--surface); color: var(--text);
  font-size: var(--fs-base); font-family: inherit;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238d8377' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center;
  padding-right: 32px;
}
.select:focus-visible { outline: 3px solid var(--focus); border-color: var(--brand); }

.textarea {
  width: 100%; padding: var(--s3);
  border: 1px solid var(--border-strong); border-radius: var(--r-sm);
  background: var(--surface-2); color: var(--text);
  font-size: var(--fs-base); font-family: inherit; line-height: 1.7;
  resize: vertical;
}
.textarea:focus-visible { outline: 3px solid var(--focus); border-color: var(--brand); }

/* 滑块：朱砂圆点 + 暖色轨道 */
.range { width: 100%; height: 22px; appearance: none; background: transparent; accent-color: var(--brand); }
.range::-webkit-slider-runnable-track { height: 4px; border-radius: 999px; background: var(--border-strong); }
.range::-moz-range-track { height: 4px; border-radius: 999px; background: var(--border-strong); }
.range::-webkit-slider-thumb {
  appearance: none; width: 18px; height: 18px; margin-top: -7px;
  border-radius: 50%; background: var(--brand);
  border: 3px solid var(--surface); box-shadow: 0 1px 3px rgba(33,28,24,.25);
  transition: transform var(--t-fast) ease;
}
.range::-moz-range-thumb {
  width: 18px; height: 18px; border-radius: 50%; background: var(--brand);
  border: 3px solid var(--surface); box-shadow: 0 1px 3px rgba(33,28,24,.25);
}
.range:hover::-webkit-slider-thumb { transform: scale(1.08); }
.range:focus-visible { outline: none; }
.range:focus-visible::-webkit-slider-thumb { box-shadow: 0 0 0 4px var(--focus); }

/* —— 开关 —— */
.switch { position: relative; display: inline-block; width: 44px; height: 24px; flex: none; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch__track {
  position: absolute; inset: 0; border-radius: 999px;
  background: var(--border-strong); transition: background var(--t-base) ease;
}
.switch__track::after {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 18px; height: 18px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.25);
  transition: transform var(--t-base) ease;
}
.switch input:checked + .switch__track { background: var(--brand); }
.switch input:checked + .switch__track::after { transform: translateX(20px); }
.switch input:focus-visible + .switch__track { outline: 3px solid var(--focus); }

/* —— 按钮 —— */
.actions { display: flex; flex-direction: column; gap: var(--s2); margin-top: var(--s1); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s2);
  padding: var(--s3) var(--s4);
  border-radius: var(--r-md); border: 1px solid transparent;
  font-size: var(--fs-base); font-weight: 650;
  transition: background var(--t-fast) ease, border-color var(--t-fast) ease, transform var(--t-fast) ease;
}
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: 3px solid var(--focus); }
.btn--primary { background: var(--brand); color: #fff; }
.btn--primary:hover { filter: brightness(1.06); }
.btn--ghost { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
.btn--ghost:hover { background: var(--surface-2); border-color: var(--brand); color: var(--brand); }

/* —— 图标按钮（抽屉用） —— */
.icon-btn {
  display: inline-grid; place-items: center;
  width: 38px; height: 38px;
  border: 1px solid var(--border); border-radius: var(--r-md);
  background: var(--surface); color: var(--text);
  transition: background var(--t-fast) ease, border-color var(--t-fast) ease;
}
.icon-btn:hover { background: var(--surface-2); border-color: var(--border-strong); }

/* =========================================================
   字体选择卡片
   ========================================================= */
.font-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: var(--s2); }
.font-card {
  padding: var(--s3);
  border: 1px solid var(--border-strong); border-radius: var(--r-md);
  background: var(--surface); color: var(--text);
  text-align: left; min-height: 66px;
  display: flex; flex-direction: column; gap: 1px;
  transition: all var(--t-fast) ease;
}
.font-card:hover { border-color: var(--brand); }
.font-card.is-active { border-color: var(--brand); background: var(--brand-soft); }
.font-card__name { font-size: var(--fs-sm); font-weight: 600; }
.font-card__sample { font-size: 28px; line-height: 1.05; color: var(--text); }
.font-card__meta { font-size: 10px; color: var(--text-faint); }

/* =========================================================
   预览：纸张浮于桌面
   ========================================================= */
.preview { display: flex; flex-direction: column; align-items: center; gap: var(--s4); min-height: 60vh; }
.preview__desk {
  width: 100%; display: flex; justify-content: center;
  padding: var(--s6) var(--s4);
}
.sheet {
  background: #fff;
  box-shadow: var(--shadow-sheet);
  border-radius: 2px;
  max-width: 100%;
}
.sheet svg { display: block; width: 100%; height: auto; max-width: 210mm; }
.preview__hint { font-size: var(--fs-xs); color: var(--text-faint); margin: 0; text-align: center; }

/* =========================================================
   设置抽屉
   ========================================================= */
.drawer-overlay { position: fixed; inset: 0; z-index: 40; background: rgba(33,28,24,.42); animation: fade var(--t-base) ease; }
.drawer {
  position: fixed; top: 0; right: 0; z-index: 50;
  width: min(360px, 90vw); height: 100%;
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow-pop);
  display: flex; flex-direction: column;
  transform: translateX(100%);
  transition: transform var(--t-base) ease;
}
.drawer.is-open { transform: translateX(0); }
.drawer__head { display: flex; align-items: center; justify-content: space-between; padding: var(--s4) var(--s5); border-bottom: 1px solid var(--border); }
.drawer__title { font-size: var(--fs-lg); }
.drawer__body { padding: var(--s5); display: flex; flex-direction: column; gap: var(--s5); }

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

/* =========================================================
   页脚
   ========================================================= */
.footer { text-align: center; padding: var(--s5); font-size: var(--fs-xs); color: var(--text-faint); border-top: 1px solid var(--border); margin-top: auto; }

/* =========================================================
   响应式（移动优先向上，断点少而准）
   ========================================================= */
@media (max-width: 900px) {
  .app { flex-direction: column; min-height: 0; }
  .sidebar {
    width: 100%; height: auto; position: static;
    flex-direction: row; align-items: center; gap: var(--s3);
    padding: var(--s3) var(--s4); border-right: none; border-bottom: 1px solid var(--border);
  }
  .brand { padding: 0; flex: none; }
  .brand__text { display: none; }
  .nav { flex-direction: row; flex: 1; gap: var(--s2); }
  .nav__item { width: auto; padding: var(--s2) var(--s3); }
  .nav__item span { display: none; }
  .nav__item.is-active { box-shadow: inset 0 -3px 0 var(--brand); }
  .sidebar__foot { margin-top: 0; padding-top: 0; border-top: none; }
  .panel { padding: var(--s4); }
  .layout { grid-template-columns: 1fr; gap: var(--s4); }
  .controls { position: static; }
}

/* =========================================================
   打印：仅保留纸张，铺满 A4
   ========================================================= */
@media print {
  .sidebar, .panel__head, .controls, .footer,
  .drawer, .drawer-overlay, .preview__hint { display: none !important; }
  .app { display: block; min-height: 0; }
  .main { display: block; padding: 0; }
  .panel { display: block !important; padding: 0; }
  .preview { min-height: 0; }
  .preview__desk { padding: 0; display: block; }
  .sheet { box-shadow: none; border-radius: 0; max-width: none; }
  .sheet svg { max-width: none; width: auto; height: auto; }
  @page { size: A4; margin: 0; }
}
