:root {
  --bg: #ffffff;
  --fg: #16181d;
  --muted: #6b7280;
  --line: #e5e7eb;
  --card: #ffffff;
  --accent: #2563eb;
  --header-h: 52px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --fg: #e8eaed;
    --muted: #9aa1ac;
    --line: #2a2e35;
    --card: #1c1f25;
    --accent: #60a5fa;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.5 system-ui, -apple-system, "Noto Sans TC", sans-serif;
  overflow-x: hidden;
}
a { color: inherit; }

.site-header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 500;
}
.brand { font-weight: 600; text-decoration: none; white-space: nowrap; }
.trip-summary {
  color: var(--muted); font-size: 13px; flex: 1; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.trip-summary strong { color: var(--fg); }
.header-right { margin-left: auto; color: var(--muted); font-size: 13px; }
.session-bar { display: flex; align-items: center; gap: 10px; font-size: 13px; }
.session-bar .who { color: var(--muted); }
.session-bar form { margin: 0; }
.session-bar .login, .session-bar .logout {
  border: 1px solid var(--line); background: var(--card); color: var(--fg);
  padding: 4px 10px; border-radius: 6px; text-decoration: none; cursor: pointer;
  font: inherit; font-size: 13px; white-space: nowrap;
}
.loading, .empty, .error { padding: 32px 16px; color: var(--muted); }
.error { color: #dc2626; }

.trip-body { height: 100vh; display: flex; flex-direction: column; }
.trip-root {
  flex: 1; min-height: 0; min-width: 0; overflow: hidden;
  display: flex; flex-direction: column;
}

/* ---------- 首頁 ---------- */
.home { padding: 20px 16px 48px; max-width: 1100px; margin: 0 auto; }
.home h2 { font-size: 15px; color: var(--muted); font-weight: 600; margin: 24px 0 12px; }
.trip-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 16px;
}
.trip-card {
  display: block;
  text-decoration: none;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  background: var(--card);
}
.trip-card:hover { border-color: var(--accent); }
.trip-card .cover { aspect-ratio: 4 / 3; background: var(--line); }
.trip-card .cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover-placeholder {
  width: 100%; height: 100%;
  display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 13px;
}
.card-body { padding: 10px 12px 12px; }
.card-body .title { font-weight: 600; margin-bottom: 6px; overflow-wrap: anywhere; }
.card-body .meta { display: flex; flex-wrap: wrap; gap: 10px; color: var(--muted); font-size: 13px; }
.badge-draft {
  display: inline-block; margin-top: 8px; padding: 1px 8px;
  border: 1px solid var(--accent); color: var(--accent);
  border-radius: 999px; font-size: 12px;
}

/* ---------- 地圖 ---------- */
.map-pane { width: 100%; height: 100%; }
.leaflet-container { background: var(--line); font: inherit; }

/* ---------- 地圖模式 ---------- */
.view-map { display: flex; flex-direction: column; height: 100%; }
.view-map .map-pane { flex: 1; min-height: 0; }
.filmstrip {
  flex: 0 0 auto;
  display: flex; gap: 8px;
  padding: 8px;
  overflow-x: auto;
  border-top: 1px solid var(--line);
  background: var(--bg);
}
.filmstrip .thumb {
  flex: 0 0 auto;
  width: 96px; height: 72px;
  padding: 0; border: 2px solid transparent; border-radius: 6px;
  background: var(--line); cursor: pointer; overflow: hidden;
}
.filmstrip .thumb:hover, .filmstrip .thumb:focus-visible { border-color: var(--accent); }
.filmstrip .thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.filmstrip .empty { padding: 16px; }

/* ---------- 分割模式 ---------- */
.view-split { display: grid; grid-template-columns: 1fr 380px; height: 100%; min-height: 0; }
.view-split .map-pane { height: 100%; }
.view-split .list-pane { overflow-y: auto; border-left: 1px solid var(--line); }
.photo-list { padding: 12px; display: flex; flex-direction: column; gap: 18px; }
.photo-list .item .frame, .story .chapter .frame { position: relative; }
.photo-list .item img {
  width: 100%; border-radius: 8px; display: block; cursor: crosshair; background: var(--line);
}
.frame .zoom {
  position: absolute; top: 8px; right: 8px;
  border: none; border-radius: 6px; padding: 3px 10px;
  background: rgba(0, 0, 0, 0.6); color: #fff;
  font: inherit; font-size: 12px; cursor: zoom-in;
}
.frame .zoom:hover { background: rgba(0, 0, 0, 0.85); }
.photo-list .note {
  margin: 8px 0 0; font-size: 15px; line-height: 1.6;
  overflow-wrap: anywhere; white-space: pre-wrap;
}
.photo-list .caption {
  margin-top: 6px; color: var(--muted); font-size: 13px;
  display: flex; gap: 8px; align-items: center;
}
.photo-list .approx {
  border: 1px solid var(--line); border-radius: 999px; padding: 0 7px; font-size: 12px;
}
.photo-list .locate {
  margin-left: auto;
  border: 1px solid var(--line); background: var(--card); color: var(--fg);
  border-radius: 6px; padding: 2px 10px; font: inherit; font-size: 12px; cursor: pointer;
}
.photo-list .locate:hover { border-color: var(--accent); color: var(--accent); }

/* 指出拍攝位置用的脈動光暈 */
.photo-halo { animation: halo-pulse 1.4s ease-out infinite; }
@keyframes halo-pulse {
  0%   { stroke-width: 3; opacity: 1; }
  70%  { stroke-width: 9; opacity: 0.15; }
  100% { stroke-width: 3; opacity: 1; }
}

/* ---------- 回放模式 ---------- */
.view-play { display: flex; flex-direction: column; height: 100%; min-height: 0; }
/* 地圖 1/3、相片 2/3，並排不互相遮擋 —— 看相片時仍看得到它在軌跡的哪一點。 */
.play-stage {
  flex: 1; min-height: 0; overflow: hidden;
  display: grid; grid-template-columns: 1fr 2fr;
}
/* grid 項目預設 min-width/min-height 是 auto，會被內容撐開。
   不歸零的話，一張 4000px 寬的原圖會把整個版面撐爆，
   連底下的播放控制列都被擠出畫面。 */
.view-play .map-pane { height: 100%; min-width: 0; min-height: 0; }

.photo-pane {
  display: flex; flex-direction: column;
  min-width: 0; min-height: 0; overflow: hidden;
  border-left: 1px solid var(--line); background: var(--bg);
}
.photo-pane .stage {
  flex: 1; min-height: 0; position: relative; overflow: hidden;
  display: flex; align-items: center; justify-content: center;
}
/* 絕對定位：相片完全不參與版面計算，再大也撐不破容器。 */
.photo-pane .stage img {
  position: absolute; inset: 12px; margin: auto;
  max-width: calc(100% - 24px); max-height: calc(100% - 24px);
  width: auto; height: auto;
  object-fit: contain; border-radius: 8px; display: block;
}
.photo-pane .placeholder {
  color: var(--muted); font-size: 14px; text-align: center;
  max-width: 380px; padding: 0 16px; line-height: 1.8;
}
.photo-pane .note {
  flex: 0 0 auto; margin: 0; padding: 8px 14px 0;
  font-size: 15px; line-height: 1.6;
  overflow-wrap: anywhere; white-space: pre-wrap;
  max-height: 22vh; overflow-y: auto;
}
.photo-pane .note[hidden] { display: none; }
.photo-pane textarea.note {
  margin: 8px 14px 0; padding: 8px 10px; width: calc(100% - 28px);
  border: 1px solid var(--line); border-radius: 6px;
  background: var(--card); color: var(--fg);
  font: inherit; font-size: 15px; line-height: 1.6;
  resize: vertical; min-height: 58px; max-height: 30vh;
}
.photo-pane textarea.note:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.pane-bar {
  flex: 0 0 auto;
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; border-top: 1px solid var(--line);
}
.pane-bar button {
  border: 1px solid var(--line); background: var(--card); color: var(--fg);
  border-radius: 6px; padding: 5px 12px; font: inherit; font-size: 13px; cursor: pointer;
}
.pane-bar button:disabled { opacity: 0.35; cursor: not-allowed; }
.pane-bar .caption {
  flex: 1; min-width: 0; color: var(--muted); font-size: 13px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.pane-bar .resume { background: var(--accent); border-color: var(--accent); color: #fff; }
.pane-bar .resume:disabled { background: var(--card); border-color: var(--line); color: var(--fg); }
.playback-bar {
  flex: 0 0 auto;
  display: flex; align-items: center; gap: 14px;
  padding: 10px 16px;
  border-top: 1px solid var(--line);
  background: var(--card);
}
.playback-bar .play {
  flex: 0 0 auto;
  width: 40px; height: 40px; border-radius: 50%;
  border: none; background: var(--accent); color: #fff;
  font-size: 15px; line-height: 1; cursor: pointer;
}
.playback-bar .play:disabled { opacity: 0.4; cursor: not-allowed; }
.playback-bar .scrub { flex: 1; min-width: 80px; accent-color: var(--accent); cursor: pointer; }
.playback-bar .clock {
  flex: 0 0 auto; font-variant-numeric: tabular-nums;
  color: var(--muted); font-size: 13px; min-width: 44px; text-align: right;
}
.playback-bar .speeds { flex: 0 0 auto; display: flex; gap: 4px; }
.playback-bar .speed {
  border: 1px solid var(--line); background: var(--bg); color: var(--fg);
  border-radius: 6px; padding: 4px 9px; font: inherit; font-size: 12px; cursor: pointer;
}
.playback-bar .speed[aria-pressed="true"] {
  background: var(--accent); border-color: var(--accent); color: #fff;
}

/* ---------- 燈箱 ---------- */
.lightbox {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(0, 0, 0, 0.88);
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 12px; padding: 24px;
}
.lightbox img { max-width: 100%; max-height: calc(100vh - 120px); object-fit: contain; }
.lightbox .caption {
  color: #ffffff; font-size: 16px; line-height: 1.6;
  max-width: 900px; text-align: center;
  overflow-wrap: anywhere; white-space: pre-wrap;
}
.lightbox .info {
  color: #e8eaed; font-size: 13px;
  display: flex; gap: 14px; align-items: center; flex-wrap: wrap; justify-content: center;
}
.lightbox .info a.orig { color: #93c5fd; }
.lightbox .close {
  position: absolute; top: 12px; right: 16px;
  background: none; border: none; color: #e8eaed;
  font-size: 32px; line-height: 1; cursor: pointer;
}

/* ---------- 切換器與未定位相片 ---------- */
.view-switcher { display: flex; gap: 4px; margin-left: auto; }
.view-switcher button {
  border: 1px solid var(--line); background: var(--card); color: var(--fg);
  padding: 5px 12px; border-radius: 6px; cursor: pointer; font: inherit; font-size: 13px;
}
.view-switcher button[aria-pressed="true"] {
  background: var(--accent); border-color: var(--accent); color: #fff;
}
.view-switcher button:disabled { opacity: 0.4; cursor: not-allowed; }

.editor-bar {
  flex: 0 0 auto;
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  padding: 10px 16px;
  border-top: 1px solid var(--line);
  background: var(--card);
}
.editor-bar input[type="text"] {
  border: 1px solid var(--line); background: var(--bg); color: var(--fg);
  padding: 5px 8px; border-radius: 6px; font: inherit; font-size: 13px;
}
.editor-bar input[name="title"] { flex: 1; min-width: 160px; }
.editor-bar input[name="tz"] { width: 60px; }
.editor-bar .tz-field { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.editor-bar button {
  border: 1px solid var(--line); background: var(--bg); color: var(--fg);
  padding: 5px 12px; border-radius: 6px; cursor: pointer; font: inherit; font-size: 13px;
}
.editor-bar button.publish { background: var(--accent); border-color: var(--accent); color: #fff; }
.editor-bar .msg { color: #dc2626; font-size: 13px; }
.editor-bar .warn { flex-basis: 100%; color: #b45309; font-size: 13px; }
.editor-bar .hint { flex-basis: 100%; color: var(--muted); font-size: 12px; }

.photo-manager {
  flex: 0 0 auto;
  border-top: 1px solid var(--line);
  padding: 12px 16px; max-height: 50vh; overflow-y: auto; background: var(--bg);
}
.pm-head { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.pm-head button {
  border: 1px solid var(--line); background: var(--card); color: var(--fg);
  padding: 4px 10px; border-radius: 6px; cursor: pointer; font: inherit; font-size: 13px;
}
.pm-head .msg { color: #dc2626; font-size: 13px; }
.pm-head .pm-close { margin-left: auto; }
/* 格子放寬到 220px：說明要打得下才有意義 */
.pm-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.pm-grid .cell {
  border: 2px solid transparent; border-radius: 8px; overflow: hidden;
  background: var(--card); display: flex; flex-direction: column;
}
.pm-grid .cell.is-private { border-color: #dc2626; }
.pm-grid .cell.is-private img { opacity: 0.45; }
.pm-grid .cell .thumb { position: relative; }
.pm-grid .cell img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; display: block; background: var(--line); }
.pm-grid .cell input[type="checkbox"] { position: absolute; top: 6px; left: 6px; z-index: 2; width: 18px; height: 18px; }
.pm-grid .cell .eye {
  position: absolute; bottom: 6px; right: 6px;
  border: none; border-radius: 999px; padding: 2px 10px;
  font: inherit; font-size: 12px; cursor: pointer;
  background: rgba(0, 0, 0, 0.65); color: #fff;
}
.pm-grid .cell .when {
  position: absolute; bottom: 6px; left: 6px;
  border-radius: 999px; padding: 2px 8px;
  font-size: 12px; background: rgba(0, 0, 0, 0.65); color: #fff;
}
.pm-grid .cell .cover {
  position: absolute; top: 6px; right: 6px;
  border: none; border-radius: 999px; padding: 2px 10px;
  font: inherit; font-size: 12px; cursor: pointer;
  background: rgba(0, 0, 0, 0.65); color: #fff;
}
.pm-grid .cell .cover.is-cover { background: #f59e0b; color: #16181d; }
.pm-grid .cell .cover:disabled { cursor: default; }
.pm-grid .cell .cover:disabled:not(.is-cover) { opacity: 0.35; }
.pm-grid .cell .cap {
  width: 100%; border: none; border-top: 1px solid var(--line);
  background: var(--card); color: var(--fg);
  padding: 7px 8px; font: inherit; font-size: 13px; line-height: 1.5;
  resize: vertical; min-height: 62px;
}
.pm-grid .cell .cap:focus { outline: 2px solid var(--accent); outline-offset: -2px; }

.share-bar {
  flex: 0 0 auto; display: flex; gap: 8px; align-items: center;
  padding: 8px 16px; border-top: 1px solid var(--line); font-size: 13px;
}
.share-bar a, .share-bar button {
  border: 1px solid var(--line); background: var(--card); color: var(--fg);
  padding: 4px 10px; border-radius: 6px; text-decoration: none; cursor: pointer;
  font: inherit; font-size: 13px;
}
.share-bar .hint { color: var(--muted); border: none; padding: 0; }

.unlocated {
  flex: 0 0 auto;
  border-top: 1px solid var(--line);
  padding: 12px 16px;
  background: var(--bg);
  max-height: 40vh;
  overflow-y: auto;
}
.unlocated summary { cursor: pointer; color: var(--muted); font-size: 13px; }
.unlocated-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 8px; margin-top: 12px;
}
.unlocated-grid img {
  width: 100%; aspect-ratio: 4 / 3; object-fit: cover;
  border-radius: 6px; cursor: zoom-in; background: var(--line);
}

@media (max-width: 860px) {
  .view-split { grid-template-columns: 1fr; }
  .view-split .list-pane { border-left: none; border-top: 1px solid var(--line); }
  /* 窄螢幕放不下並排，改成上地圖下相片 */
  .play-stage { grid-template-columns: 1fr; grid-template-rows: 40% 60%; }
  .photo-pane { border-left: none; border-top: 1px solid var(--line); }
  .playback-bar { gap: 8px; padding: 8px 10px; }
  .playback-bar .speeds { gap: 2px; }
}
