:root {
  --paper: #f3efe6;
  --paper-2: #ebe5d8;
  --ink: #191814;
  --ink-2: #4a463d;
  --muted: #8a8476;
  --line: #d9d1c0;
  --red: #df4321;
  --sans: -apple-system, "PingFang SC", "HarmonyOS Sans SC", "MiSans", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --st: env(safe-area-inset-top, 0px);
  --sb: env(safe-area-inset-bottom, 0px);
  --sl: env(safe-area-inset-left, 0px);
  --sr: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--paper); color: var(--ink); font-family: var(--sans); }
body { min-height: 100dvh; -webkit-text-size-adjust: 100%; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
[hidden] { display: none !important; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------------- 首页 ---------------- */
#home {
  max-width: 560px;
  margin: 0 auto;
  padding: calc(var(--st) + 22px) calc(var(--sr) + 20px) calc(var(--sb) + 120px) calc(var(--sl) + 20px);
}
.top { display: flex; align-items: center; justify-content: space-between; margin-bottom: 26px; }
.mark { margin: 0; font-size: 30px; font-weight: 800; letter-spacing: .08em; }
.mark::after { content: ""; display: inline-block; width: 8px; height: 8px; margin-left: 4px; background: var(--red); border-radius: 50%; vertical-align: 4px; }
.ghost-btn {
  width: 40px; height: 40px; border-radius: 50%;
  border: 1.5px solid var(--line); color: var(--ink-2);
  font-family: var(--mono); font-size: 17px; font-weight: 600;
}

.slot {
  position: relative; display: block;
  height: 176px; padding: 22px;
  background: var(--ink); color: var(--paper);
  border-radius: 6px;
  cursor: pointer; user-select: none;
  transition: transform .15s ease, background .2s;
  overflow: hidden;
}
.slot::before {
  content: ""; position: absolute; inset: 10px;
  border: 1px dashed rgba(243, 239, 230, .22); border-radius: 3px;
  pointer-events: none;
}
.slot:active { transform: scale(.985); }
.slot.drag { background: #2a2822; }
.slot-title { position: absolute; left: 24px; bottom: 22px; font-size: 24px; font-weight: 700; letter-spacing: .02em; }
.slot-meta { position: absolute; left: 24px; top: 22px; font-family: var(--mono); font-size: 12px; color: rgba(243, 239, 230, .5); letter-spacing: .04em; }
.slot-plus {
  position: absolute; right: 22px; top: 22px;
  width: 56px; height: 56px; border-radius: 50%; background: var(--red);
}
.slot-plus::before, .slot-plus::after {
  content: ""; position: absolute; left: 50%; top: 50%;
  width: 22px; height: 2.5px; background: #fff; border-radius: 2px;
  transform: translate(-50%, -50%);
}
.slot-plus::after { transform: translate(-50%, -50%) rotate(90deg); }

.text-btn { display: inline-block; margin-top: 6px; padding: 12px 2px; font-size: 15px; color: var(--ink-2); text-decoration: underline; text-underline-offset: 4px; text-decoration-color: var(--line); }

#recentWrap { margin-top: 26px; }
.label {
  margin: 0 0 4px; padding-bottom: 10px; border-bottom: 1.5px solid var(--ink);
  font-family: var(--mono); font-size: 12px; font-weight: 600; letter-spacing: .12em; color: var(--ink);
}
#recentCount { color: var(--muted); margin-left: 4px; }
#recent { list-style: none; margin: 0; padding: 0; }
.item { display: flex; align-items: stretch; border-bottom: 1px solid var(--line); }
.item-open {
  flex: 1; min-width: 0; text-align: left;
  display: grid; grid-template-columns: auto 1fr; grid-template-rows: auto auto; column-gap: 12px; row-gap: 4px;
  padding: 15px 0;
}
.item-open:active { background: var(--paper-2); }
.tag {
  grid-row: 1 / 3; align-self: center;
  width: 50px; padding: 5px 0; text-align: center;
  font-size: 11px; font-weight: 700; letter-spacing: .04em;
  border: 1.5px solid var(--ink-2); color: var(--ink-2); border-radius: 3px;
}
.tag[data-kind="deck"] { border-color: var(--red); color: var(--red); }
.tag[data-kind="3d"] { background: var(--ink); border-color: var(--ink); color: var(--paper); }
.item-title { font-size: 16px; font-weight: 600; line-height: 1.35; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-meta { font-family: var(--mono); font-size: 12px; color: var(--muted); }
.item-del { width: 48px; flex: none; display: grid; place-items: center; color: var(--muted); }
.item-del svg { width: 18px; height: 18px; }

.install {
  position: fixed; left: 12px; right: 12px; bottom: calc(var(--sb) + 12px);
  max-width: 536px; margin: 0 auto;
  display: flex; align-items: center; gap: 10px;
  padding: 12px 8px 12px 16px;
  background: #fffdf8; border: 1.5px solid var(--ink); border-radius: 6px;
  box-shadow: 4px 4px 0 var(--ink);
  font-size: 14px; line-height: 1.5;
}
.install p { flex: 1; margin: 0; }
.install-go { flex: none; padding: 9px 14px; background: var(--red); color: #fff; font-weight: 700; border-radius: 4px; }
.install-x { flex: none; width: 36px; height: 36px; font-size: 22px; color: var(--muted); }

/* ---------------- 对话框 ---------------- */
.sheet {
  width: min(560px, 100vw); max-width: 100vw; max-height: 88dvh;
  margin: auto auto 0; padding: 0;
  border: 0; border-radius: 12px 12px 0 0;
  background: var(--paper); color: var(--ink);
}
.sheet::backdrop { background: rgba(20, 19, 16, .45); }
@media (min-width: 600px) { .sheet { margin: auto; border-radius: 10px; } }
.sheet-body { padding: 22px 20px calc(var(--sb) + 18px); }
.sheet h3 { margin: 0 0 12px; font-size: 18px; }
.sheet textarea {
  display: block; width: 100%; height: 34dvh; margin: 14px 0 0; padding: 12px;
  font: 13px/1.5 var(--mono); color: var(--ink);
  background: #fffdf8; border: 1.5px solid var(--line); border-radius: 4px; resize: none;
}
.sheet textarea:focus { outline: none; border-color: var(--ink); }
.sheet-actions { display: flex; justify-content: flex-end; align-items: center; gap: 16px; margin-top: 16px; }
.primary { padding: 13px 20px; background: var(--ink); color: var(--paper); font-weight: 700; border-radius: 4px; }
#clipBtn { width: 100%; background: var(--red); color: #fff; }

.help { font-size: 15px; line-height: 1.65; }
.help-tabs { display: flex; gap: 0; margin-bottom: 20px; border: 1.5px solid var(--ink); border-radius: 4px; overflow: hidden; width: max-content; }
.help-tabs button { padding: 7px 18px; font-size: 14px; font-weight: 600; }
.help-tabs button[aria-selected="true"] { background: var(--ink); color: var(--paper); }
.help section h3 { font-size: 16px; margin: 0 0 6px; }
.help section h3:not(:first-child) { margin-top: 22px; }
.help ol { margin: 0; padding-left: 1.3em; }
.help li { margin: 4px 0; }
.help p { margin: 8px 0 0; }
.help .note { color: var(--ink-2); font-size: 13.5px; }
.help code { font: 12.5px var(--mono); background: #fffdf8; border: 1px solid var(--line); padding: 1px 5px; border-radius: 3px; word-break: break-all; user-select: all; }

/* ---------------- 查看器 ---------------- */
#viewer { position: fixed; inset: 0; background: #0c0c0b; overflow: hidden; overflow: clip; z-index: 10; }
#wrap { position: absolute; left: 0; top: 0; transform-origin: 0 0; overflow: hidden; overflow: clip; }
#frame { position: absolute; left: 0; top: 0; border: 0; background: #fff; transform-origin: 0 0; display: block; }

/* 正常竖屏时：避开刘海和底部横条 */
#viewer { --pt: var(--st); --pb: var(--sb); --pl: var(--sl); --pr: var(--sr); }
/* 横过来时：刘海在左，横条在右 */
#viewer.rot { --pt: 0px; --pb: 0px; --pl: var(--st); --pr: var(--sb); }

#loading { position: absolute; inset: 0; display: grid; place-items: center; background: #0c0c0b; color: rgba(255,255,255,.7); font-size: 14px; letter-spacing: .1em; }
#loading span::after { content: ""; display: block; width: 28px; height: 2px; margin: 12px auto 0; background: var(--red); animation: ld 1s ease-in-out infinite; }
@keyframes ld { 0%, 100% { transform: scaleX(.2); } 50% { transform: scaleX(1.4); } }

.bar, .dock {
  position: absolute; left: 0; right: 0;
  display: flex; align-items: center;
  color: #f3efe6;
  background: rgba(16, 15, 13, .82);
  -webkit-backdrop-filter: blur(14px) saturate(140%); backdrop-filter: blur(14px) saturate(140%);
  transition: transform .25s ease, opacity .25s ease;
  opacity: 0; pointer-events: none;
}
.bar { top: 0; gap: 4px; padding: calc(var(--pt) + 4px) calc(var(--pr) + 6px) 4px calc(var(--pl) + 6px); transform: translateY(-100%); }
.dock { bottom: 0; gap: 4px; justify-content: center; flex-wrap: wrap; padding: 8px calc(var(--pr) + 10px) calc(var(--pb) + 8px) calc(var(--pl) + 10px); transform: translateY(100%); }
#viewer.bar-on .bar, #viewer.bar-on .dock { opacity: 1; transform: none; pointer-events: auto; }
.bar-title { flex: 1; min-width: 0; font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: center; }
.icon-btn { width: 44px; height: 44px; display: grid; place-items: center; flex: none; }

.seg { display: flex; border: 1.5px solid rgba(243,239,230,.35); border-radius: 5px; overflow: hidden; margin-right: 6px; }
.seg button { padding: 8px 12px; font-size: 13px; font-weight: 600; color: rgba(243,239,230,.75); }
.seg button[aria-checked="true"] { background: #f3efe6; color: var(--ink); }
.tool { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; min-width: 52px; height: 48px; font-size: 11px; color: rgba(243,239,230,.75); border-radius: 5px; }
.tool svg { width: 20px; height: 20px; }
.tool.on { color: #fff; }
.tool.on svg { color: var(--red); }

#handle {
  position: absolute; top: calc(var(--pt) + 10px); right: calc(var(--pr) + 10px);
  width: 38px; height: 38px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center; gap: 3px;
  background: rgba(16, 15, 13, .5); opacity: .7;
  transition: opacity .2s;
}
#handle i { width: 4px; height: 4px; border-radius: 50%; background: #f3efe6; }
#viewer.bar-on #handle { opacity: 0; pointer-events: none; }

.nav {
  position: absolute; bottom: calc(var(--pb) + 14px);
  width: 46px; height: 46px; border-radius: 50%;
  display: none; place-items: center;
  background: rgba(16, 15, 13, .45); color: #f3efe6; opacity: .75;
  transition: bottom .25s ease;
}
.nav:active { opacity: 1; background: var(--red); }
.nav-prev { left: calc(var(--pl) + 12px); }
.nav-next { right: calc(var(--pr) + 12px); }
#viewer.with-nav .nav { display: grid; }
#viewer.bar-on .nav { bottom: calc(var(--pb) + 84px); }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--sb) + 96px); z-index: 50;
  max-width: calc(100vw - 40px);
  padding: 11px 16px; border-radius: 5px;
  background: var(--ink); color: var(--paper);
  font-size: 14px; line-height: 1.45; text-align: center;
  transform: translate(-50%, 10px); opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s;
}
#toast.on { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
