* { box-sizing: border-box; }
[hidden] { display: none !important; }

:root {
  --ink: #2e2a27;
  --muted: #8b8178;
  --line: #e7e0d6;
  --accent: #e2574c;
  --panel: #fffdf9;
  --head: 62px;
  --cork:
    radial-gradient(circle at 20% 30%, rgba(255, 255, 255, .18) 0 1px, transparent 2px) 0 0 / 7px 7px,
    radial-gradient(circle at 70% 60%, rgba(80, 50, 20, .15) 0 1px, transparent 2px) 0 0 / 11px 11px,
    linear-gradient(135deg, #c9a274, #b98f5f);
  --wood: repeating-linear-gradient(90deg, #d9c7ae 0 3px, #d4c0a5 3px 7px, #dcc9b0 7px 12px);
}

body {
  margin: 0;
  font-family: "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  color: var(--ink);
  background: #efe9e1;
  min-height: 100vh;
}
button, input, select { font: inherit; color: inherit; }
a { color: inherit; text-decoration: none; }
code { background: #f1ebe2; padding: 1px 6px; border-radius: 6px; }

/* ---------- Header ---------- */
.top {
  height: var(--head);
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 0 24px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.logo { display: inline-flex; align-items: center; gap: 10px; }
.logo-mark { width: 44px; height: 44px; transition: transform .3s cubic-bezier(.3, 1.6, .5, 1); }
.logo:hover .logo-mark { transform: rotate(-8deg) scale(1.08); }
.logo-text { display: flex; flex-direction: column; line-height: 1; }
.logo-text b { font-size: 19px; font-weight: 800; letter-spacing: 3px; }
.logo-text i {
  font-family: "Caveat", cursive; font-style: normal; font-size: 17px;
  color: var(--accent); margin-top: 1px; transform: rotate(-3deg); transform-origin: left;
}
.user { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 14px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 16px;
  border: 1.5px solid var(--line);
  border-radius: 12px;
  background: #fff;
  cursor: pointer;
  font-size: 14px;
  white-space: nowrap;
  transition: .15s;
}
.btn:hover { border-color: #cfc4b6; transform: translateY(-1px); }
.btn:disabled { opacity: .6; pointer-events: none; }
.btn.primary { background: var(--ink); border-color: var(--ink); color: #fff; }
.btn.primary:hover { background: #000; }
.btn.danger:hover { border-color: var(--accent); color: var(--accent); }
.btn.small { padding: 6px 12px; font-size: 13px; }
.btn.block { width: 100%; }
.link { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 13px; text-decoration: underline; }

/* ---------- Generic ---------- */
.view { min-height: calc(100vh - var(--head)); }
.center { display: flex; align-items: center; justify-content: center; padding: 24px; background: var(--wood); }
.card {
  background: var(--panel);
  border-radius: 22px;
  padding: 32px;
  box-shadow: 0 20px 50px rgba(60, 40, 20, .2);
  text-align: center;
  max-width: 440px;
}
.muted { color: var(--muted); font-size: 14px; }

/* ---------- Auth ---------- */
.auth-card { width: min(400px, 100%); }
.auth-hero img { width: 84px; height: 84px; display: block; margin: 0 auto 4px; }
.auth-card h2 { font-size: 19px; line-height: 1.5; margin: 6px 0 20px; }
.tabs { display: grid; grid-template-columns: 1fr 1fr; background: #f1ebe2; border-radius: 12px; padding: 4px; margin-bottom: 16px; }
.tabs button { border: 0; background: none; padding: 8px; border-radius: 9px; cursor: pointer; color: var(--muted); }
.tabs button.on { background: #fff; color: var(--ink); font-weight: 600; box-shadow: 0 1px 4px rgba(0, 0, 0, .08); }
#authForm { display: flex; flex-direction: column; gap: 10px; margin-bottom: 10px; }
.auth-msg { min-height: 1.4em; font-size: 14px; margin: 10px 0 0; }
.auth-msg.ok { color: #2f8a4c; }
.auth-msg.err { color: var(--accent); }

input[type="text"], input[type="email"], input[type="password"], input[type="date"], select {
  width: 100%;
  padding: 10px 12px;
  border: 1.5px solid var(--line);
  border-radius: 10px;
  background: #fff;
}
input:focus, select:focus { outline: none; border-color: #bfb2a2; }

/* ---------- Boards (albums / album / share) ---------- */
.board { background: var(--cork); padding-bottom: 40px; }
.board.drag { outline: 4px dashed #fff; outline-offset: -14px; }
.board-head {
  display: flex; align-items: center; flex-wrap: wrap; gap: 10px;
  padding: 16px 24px;
  background: rgba(255, 253, 249, .92);
  border-bottom: 1px solid var(--line);
}
.board-head h2 { margin: 0; font-size: 20px; }
.spacer { flex: 1; }
.editable { cursor: text; border-bottom: 1.5px dashed transparent; }
.editable:hover { border-bottom-color: #cfc4b6; }
.board-empty {
  text-align: center; color: #fff; font-size: 16px;
  text-shadow: 0 1px 3px rgba(0, 0, 0, .35);
  margin: 80px 20px;
}

.share-toggle { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; }

.album-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 34px 26px;
  padding: 34px 28px;
}
.album-card {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  color: #fff; text-shadow: 0 1px 3px rgba(0, 0, 0, .4);
  perspective: 1100px;
  transition: opacity .35s, filter .35s;
}
.album-card > b { margin-top: 14px; font-size: 16px; }
.album-card > small { opacity: .9; }

/* ---------- 相簿：一本精裝書 ---------- */
.book {
  position: relative; width: 168px; height: 212px;
  transform-style: preserve-3d;
  transition: transform .5s cubic-bezier(.3, 1.3, .5, 1);
}
/* 內頁：右側露出一疊紙的邊 */
.book-pages {
  position: absolute; inset: 5px -7px 5px 10px;
  border-radius: 2px 5px 5px 2px;
  background:
    repeating-linear-gradient(90deg, #fbf7ef 0 2px, #ddd2bf 2px 3px) right / 9px 100% no-repeat,
    linear-gradient(90deg, rgba(0, 0, 0, .12), transparent 18px),
    #fbf7ef;
  box-shadow: 0 12px 26px rgba(40, 25, 10, .45), 0 2px 4px rgba(0, 0, 0, .2);
}
/* 封面：以左邊書背為軸翻開 */
.book-cover {
  position: absolute; inset: 0;
  transform-origin: left center;
  transform-style: preserve-3d;
  transition: transform .75s cubic-bezier(.55, .05, .25, 1);
}
.cover-front, .cover-back {
  position: absolute; inset: 0;
  border-radius: 3px 8px 8px 3px;
  backface-visibility: hidden; -webkit-backface-visibility: hidden;
}
.cover-front {
  background:
    radial-gradient(circle at 30% 20%, rgba(255, 255, 255, .18), transparent 55%),
    radial-gradient(rgba(0, 0, 0, .12) 1px, transparent 1.4px) 0 0 / 5px 5px,
    linear-gradient(160deg, rgba(255, 255, 255, .08), rgba(0, 0, 0, .25)),
    var(--c, #8a4f3d);
  box-shadow: inset -2px 0 0 rgba(255, 255, 255, .12), inset 0 0 0 1px rgba(0, 0, 0, .25);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  padding: 18px 14px 16px 26px;
}
.cover-front::before { /* 書背 */
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 14px;
  border-radius: 3px 0 0 3px;
  background: linear-gradient(90deg, rgba(0, 0, 0, .35), rgba(0, 0, 0, .1) 60%, rgba(255, 255, 255, .15) 85%, rgba(0, 0, 0, .25));
}
.cover-front::after { /* 車線 */
  content: ""; position: absolute; inset: 8px 8px 8px 20px;
  border: 1.5px dashed rgba(255, 255, 255, .35); border-radius: 5px;
  pointer-events: none;
}
.cover-photo { position: relative; z-index: 1; transform: rotate(-3deg); }
.cover-photo canvas { display: block; box-shadow: 0 4px 10px rgba(0, 0, 0, .4); }
.book-pages { display: flex; align-items: center; justify-content: center; }
.page-photo { transform: rotate(2deg) scale(1.08); }
.page-photo canvas { display: block; box-shadow: 0 3px 8px rgba(0, 0, 0, .25); }
.page-empty { font-family: "Long Cang", "Caveat", cursive; font-size: 18px; color: #a8998a; text-shadow: none; }
.cover-photo:has(canvas)::before, .cover-photo:has(canvas)::after { /* 紙膠帶 */
  content: ""; position: absolute; z-index: 2; width: 34px; height: 12px;
  background: rgba(255, 244, 214, .75); box-shadow: 0 1px 2px rgba(0, 0, 0, .15);
}
.cover-photo::before { top: -5px; left: -10px; transform: rotate(-35deg); }
.cover-photo::after { bottom: -5px; right: -10px; transform: rotate(-35deg); }
.cover-blank {
  width: 92px; height: 112px; display: flex; align-items: center; justify-content: center;
  border: 2px dashed rgba(255, 255, 255, .45); border-radius: 4px;
  font-size: 30px; color: rgba(255, 255, 255, .7); text-shadow: none;
}
.cover-title {
  position: relative; z-index: 1;
  max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-family: "Long Cang", "Caveat", cursive; font-size: 19px; line-height: 1.1;
  color: #f3dfb0; text-shadow: 0 1px 0 rgba(0, 0, 0, .45);
}
/* 封面內側 */
.cover-back {
  transform: rotateY(180deg);
  background: linear-gradient(270deg, rgba(0, 0, 0, .14), transparent 30px), #f1e8d8;
  box-shadow: inset 0 0 0 6px var(--c, #8a4f3d);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.cover-back span {
  font-family: "Long Cang", "Caveat", cursive; font-size: 22px; color: #5a4636;
  text-shadow: none; text-align: center; word-break: break-all;
}

/* 滑過：封面微微掀起 */
.album-card:hover .book { transform: translateY(-5px); }
.album-card:hover .book-cover { transform: rotateY(-22deg); }
/* 點下去：封面翻開、書往前靠近 */
.album-card.opening .book { transform: translateY(-8px) scale(1.12); transition-duration: .75s; }
.album-card.opening .book-cover { transform: rotateY(-168deg); }
.album-grid.opening .album-card:not(.opening) { opacity: .35; filter: blur(1px); }
.album-grid.opening .album-card { pointer-events: none; }

/* 進入相簿：照片一張張釘上牆 */
@keyframes pinIn {
  from { opacity: 0; translate: 0 -26px; scale: .85; }
  70% { opacity: 1; translate: 0 3px; scale: 1.02; }
  to { opacity: 1; translate: 0 0; scale: 1; }
}
@keyframes fadeDown { from { opacity: 0; translate: 0 -10px; } to { opacity: 1; translate: 0 0; } }
.wall.enter .wall-item { animation: pinIn .55s cubic-bezier(.3, 1.2, .5, 1) both; animation-delay: calc(var(--i, 0) * 60ms + 80ms); }
#viewAlbum:has(.wall.enter) .board-head { animation: fadeDown .4s ease-out both; }

@media (prefers-reduced-motion: reduce) {
  .book, .book-cover { transition: none; }
  .album-card:hover .book-cover { transform: none; }
}

.wall {
  display: flex; flex-wrap: wrap; justify-content: center; gap: 34px 30px;
  padding: 40px 28px;
}
.wall-item {
  position: relative; display: block;
  border: 0; padding: 0; background: none; cursor: pointer;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .15), 0 12px 26px rgba(40, 25, 10, .35);
  transition: transform .2s, box-shadow .2s;
}
.wall-item canvas { display: block; }
.wall-item:nth-child(4n+1) { transform: rotate(-3deg); }
.wall-item:nth-child(4n+2) { transform: rotate(2deg) translateY(8px); }
.wall-item:nth-child(4n+3) { transform: rotate(-1deg) translateY(-4px); }
.wall-item:nth-child(4n) { transform: rotate(3.5deg); }
.wall-item::before { /* 圖釘 */
  content: ""; position: absolute; top: -7px; left: 50%; z-index: 1;
  width: 16px; height: 16px; margin-left: -8px; border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #ff8a7a, #c0392b 60%, #7a1f16);
  box-shadow: 0 3px 4px rgba(0, 0, 0, .35);
}
.wall-item:hover { transform: rotate(0) scale(1.04); z-index: 2; box-shadow: 0 4px 8px rgba(0, 0, 0, .2), 0 22px 40px rgba(40, 25, 10, .4); }

.upload-bar {
  position: relative; height: 30px; margin: 14px 24px 0;
  background: rgba(255, 255, 255, .85); border-radius: 10px; overflow: hidden;
}
.upload-bar div { height: 100%; width: 0; background: #e8892d; transition: width .3s; }
.upload-bar span { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: 13px; }

/* ---------- Editor ---------- */
.editor { display: grid; grid-template-columns: 1fr 320px; }
.stage-wrap {
  display: flex; flex-direction: column; min-width: 0;
  background: radial-gradient(circle at 30% 20%, rgba(255, 255, 255, .5), transparent 60%), var(--wood);
}
.editor-head { display: flex; align-items: center; gap: 12px; padding: 14px 20px 0; }
.save-status { font-size: 13px; color: #6d6257; }
.stage { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 16px 20px 10px; }
.card-holder {
  position: relative;
  transform: rotate(-1.5deg);
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 18px 40px rgba(60, 40, 20, .28);
  transition: transform .3s;
}
.card-holder:hover { transform: rotate(0deg); }
#preview { display: block; cursor: grab; touch-action: none; }
#preview.dragging { cursor: grabbing; }
.loading {
  position: absolute; top: 10px; right: 10px;
  background: rgba(0, 0, 0, .6); color: #fff; font-size: 12px;
  padding: 4px 10px; border-radius: 99px;
}
.tip { color: #6d6257; font-size: 13px; margin: 16px 0 0; }

.strip { display: flex; gap: 14px; padding: 14px 20px 18px; overflow-x: auto; background: rgba(0, 0, 0, .08); }
.strip:empty { display: none; }
.strip .thumb { flex: none; display: block; box-shadow: 0 4px 10px rgba(0, 0, 0, .2); transition: transform .15s; }
.strip .thumb:nth-child(odd) { transform: rotate(-3deg); }
.strip .thumb:nth-child(even) { transform: rotate(2.5deg); }
.strip .thumb:hover { transform: rotate(0) translateY(-4px); }
.strip .thumb.active { outline: 3px solid var(--accent); outline-offset: 3px; }
.strip canvas { display: block; }

.panel {
  background: var(--panel);
  border-left: 1px solid var(--line);
  padding: 18px 20px 30px;
  overflow-y: auto;
  max-height: calc(100vh - var(--head));
  position: sticky; top: var(--head);
}
.group { margin-bottom: 20px; }
.group h3 { font-size: 13px; color: var(--muted); font-weight: 600; margin: 0 0 8px; letter-spacing: 1px; }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.seg button { padding: 8px 4px; font-size: 13px; border: 1.5px solid var(--line); border-radius: 10px; background: #fff; cursor: pointer; }
.seg button.on { border-color: var(--ink); background: var(--ink); color: #fff; }
.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.swatches button { width: 30px; height: 30px; border-radius: 50%; border: 1.5px solid rgba(0, 0, 0, .15); cursor: pointer; }
.swatches button.on { box-shadow: 0 0 0 2.5px var(--panel), 0 0 0 4.5px var(--ink); }
.filters { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.filters button { border: 0; padding: 0; background: none; cursor: pointer; font-size: 12px; color: var(--muted); }
.filters canvas { display: block; width: 100%; aspect-ratio: 1; border-radius: 8px; margin-bottom: 4px; background: #e9e2d8; outline: 2px solid transparent; outline-offset: 2px; }
.filters button.on { color: var(--ink); font-weight: 600; }
.filters button.on canvas { outline-color: var(--ink); }
input[type="range"] { width: 100%; accent-color: var(--ink); }
input[type="color"] { width: 44px; height: 40px; padding: 2px; border: 1.5px solid var(--line); border-radius: 10px; background: #fff; flex: none; }
.row { display: flex; gap: 8px; align-items: center; margin-top: 8px; }
.group > .row:first-of-type:nth-child(2) { margin-top: 0; }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.actions .wide { grid-column: 1 / -1; }

.switch { position: relative; display: inline-block; width: 46px; height: 26px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch span { position: absolute; inset: 0; background: #d8d0c5; border-radius: 99px; cursor: pointer; transition: .2s; }
.switch span::before { content: ""; position: absolute; width: 20px; height: 20px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: .2s; }
.switch input:checked + span { background: #e8892d; }
.switch input:checked + span::before { transform: translateX(20px); }

/* ---------- Lightbox (share) ---------- */
.lightbox {
  position: fixed; inset: 0; z-index: 10;
  background: rgba(25, 20, 15, .85);
  display: flex; align-items: center; justify-content: center;
}
.lightbox canvas { box-shadow: 0 20px 60px rgba(0, 0, 0, .5); }
.lb-btn {
  position: absolute; border: 0; background: rgba(255, 255, 255, .15); color: #fff;
  width: 48px; height: 48px; border-radius: 50%; font-size: 26px; cursor: pointer;
}
.lb-btn:hover { background: rgba(255, 255, 255, .3); }
.lb-close { top: 18px; right: 18px; font-size: 20px; }
.lb-prev { left: 18px; top: 50%; margin-top: -24px; }
.lb-next { right: 18px; top: 50%; margin-top: -24px; }
.lb-dl { position: absolute; bottom: 22px; left: 50%; transform: translateX(-50%); }
.lb-dl:hover { transform: translateX(-50%) translateY(-1px); }
.lb-actions { position: absolute; bottom: 22px; left: 50%; transform: translateX(-50%); display: flex; gap: 10px; }
.wall-item { cursor: zoom-in; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 28px; z-index: 20;
  transform: translate(-50%, 20px);
  background: var(--ink); color: #fff;
  padding: 10px 18px; border-radius: 12px;
  opacity: 0; transition: .3s; pointer-events: none;
  max-width: 90vw;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 860px) {
  .editor { grid-template-columns: 1fr; }
  .panel { position: static; max-height: none; border-left: 0; border-top: 1px solid var(--line); }
  .top { padding: 0 14px; }
  #userEmail { display: none; }
}
