:root {
  --bg: #0b1120;
  --panel: #131c31;
  --panel-2: #1b2540;
  --line: #253253;
  --text: #e8eefc;
  --muted: #93a2c0;
  --accent: #4f8cff;
  --accent-soft: #1e3a6b;
  --danger: #ff6b6b;
  --ok: #3ddc97;
  --radius: 14px;
  --shadow: 0 12px 30px rgba(0, 0, 0, 0.45);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --cat-transit: #60a5fa;
  --cat-activity: #f0abfc;
  --cat-meal: #fbbf24;
  --cat-stay: #34d399;
  --cat-shop: #f472b6;
  --cat-other: #94a3b8;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6fb;
    --panel: #ffffff;
    --panel-2: #f0f3fa;
    --line: #dde3f0;
    --text: #16203a;
    --muted: #5c6a86;
    --accent: #2456d8;
    --accent-soft: #dbe6ff;
    --danger: #d92d20;
    --shadow: 0 10px 24px rgba(20, 34, 66, 0.12);
  }
}

* { box-sizing: border-box; }

button, a, select, input, textarea, label, summary {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

html, body { margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", sans-serif;
  -webkit-text-size-adjust: 100%;
  padding-bottom: calc(72px + var(--safe-bottom));
}

h1, h2, h3, h4 { margin: 0; font-weight: 600; }
a { color: var(--accent); }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.9em; }

/* ------------------------------ top bar ------------------------------ */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
  padding: calc(10px + env(safe-area-inset-top, 0px)) 14px 0;
}

.topbar-row { display: flex; align-items: center; gap: 8px; }
.topbar-actions { display: flex; gap: 6px; }

.trip-select {
  flex: 1;
  min-width: 0;
  appearance: none;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 9px 12px;
  font-size: 15px;
  font-weight: 600;
}

.icon-btn {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
  width: 44px;
  height: 44px;
  font-size: 17px;
  cursor: pointer;
  flex: 0 0 auto;
}
.icon-btn:hover { background: var(--panel-2); }

.trip-meta { color: var(--muted); font-size: 12.5px; margin-top: 6px; min-height: 16px; }

.tabs { display: flex; gap: 6px; margin-top: 8px; }
.tabs button {
  flex: 1;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  font-size: 14px;
  min-height: 44px;
  padding: 11px 4px;
  cursor: pointer;
}
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }

/* ------------------------------- view ------------------------------- */
.view { max-width: 760px; margin: 0 auto; padding: 14px 14px 24px; }

.day { margin-bottom: 22px; }
.day-head {
  position: sticky;
  top: var(--topbar-h, 108px);
  z-index: 5;
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 8px 0;
  background: linear-gradient(var(--bg) 70%, transparent);
  font-size: 15px;
}
.day-head .weekday { color: var(--muted); font-size: 13px; }
.day-head .tag {
  margin-left: auto;
  font-size: 11.5px;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 999px;
  padding: 2px 8px;
}

.timeline { list-style: none; margin: 0; padding: 0; }

.item {
  position: relative;
  display: grid;
  grid-template-columns: 52px 16px 1fr;
  gap: 0 8px;
  padding-bottom: 14px;
}

.item .time {
  font-variant-numeric: tabular-nums;
  font-size: 12.5px;
  color: var(--muted);
  padding-top: 12px;
  text-align: right;
}
.item .time b { display: block; color: var(--text); font-weight: 600; font-size: 13px; }

.item .rail { position: relative; }
.item .rail::before {
  content: "";
  position: absolute;
  left: 7px;
  top: 16px;
  bottom: -14px;
  width: 2px;
  background: var(--line);
}
.item:last-child .rail::before { display: none; }
.item .dot {
  position: absolute;
  left: 2px;
  top: 16px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--cat-color, var(--accent));
  box-shadow: 0 0 0 3px var(--bg);
}

/* The row the clock currently sits in. */
.item.current .dot {
  box-shadow: 0 0 0 3px var(--bg), 0 0 0 6px color-mix(in srgb, var(--accent) 45%, transparent);
}
.item.current .card { border-color: color-mix(in srgb, var(--accent) 55%, var(--line)); }
.chip-now { background: var(--accent); border-color: var(--accent); color: #fff; }

/* «现在» divider inside today's timeline. */
.now-marker {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 2px 0 12px;
  color: var(--accent);
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
}
.now-marker::before,
.now-marker::after {
  content: "";
  flex: 1;
  height: 1px;
  background: color-mix(in srgb, var(--accent) 40%, transparent);
}

/* Rows and days that are already behind us. */
.finished-group { margin-bottom: 10px; }
.finished-group > summary {
  cursor: pointer;
  list-style: none;
  color: var(--muted);
  font-size: 12.5px;
  padding: 7px 0;
  border-bottom: 1px dashed var(--line);
}
.finished-group > summary::before,
.day-finished > summary::before { content: "▸ "; }
.finished-group[open] > summary::before,
.day-finished[open] > summary::before { content: "▾ "; }
.day-finished { opacity: 0.7; }
.day-finished[open] { opacity: 1; }
.day-finished > summary.day-head {
  position: static;
  cursor: pointer;
  list-style: none;
  background: none;
}

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 11px 12px;
  margin-top: 7px;
}

.item-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.item-head h3 { font-size: 15.5px; flex: 1; min-width: 0; }
.card-actions { display: flex; gap: 2px; }

.chip {
  font-size: 11px;
  border-radius: 999px;
  padding: 2px 8px;
  background: var(--panel-2);
  color: var(--cat-color, var(--muted));
  border: 1px solid var(--line);
  white-space: nowrap;
}

.place { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-top: 6px; font-size: 13.5px; }
.place .name { color: var(--text); }
.place .addr { color: var(--muted); font-size: 12.5px; }

.map-links { display: inline-flex; gap: 8px; margin-left: auto; }
.map-links a {
  font-size: 12.5px;
  text-decoration: none;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 999px;
  padding: 8px 14px;
  white-space: nowrap;
}

.notes { color: var(--muted); font-size: 13.5px; margin: 6px 0 0; white-space: pre-wrap; }

/* Long unbroken runs (URLs, filenames, place names) must never widen a card. */
.notes,
.place,
.item-head h3,
.record-meta,
.record-body p,
.trip-note .note-body,
.viewer figcaption,
.hint { overflow-wrap: anywhere; }

.thumbs { display: flex; gap: 6px; overflow-x: auto; margin-top: 8px; padding-bottom: 2px; }
.thumb {
  position: relative;
  flex: 0 0 auto;
  width: 68px;
  height: 68px;
  border-radius: 10px;
  overflow: hidden;
  background: var(--panel-2);
  border: 1px solid var(--line);
  padding: 0;
  cursor: zoom-in;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb.missing { cursor: default; }
.thumb.missing::after {
  content: "本机无";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 11px;
  color: var(--muted);
  background: repeating-linear-gradient(45deg, var(--panel-2), var(--panel-2) 6px, var(--panel) 6px, var(--panel) 12px);
}

.inline-link {
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 999px;
  padding: 2px 9px;
  font-size: 12.5px;
  text-decoration: none;
  white-space: nowrap;
}

.trip-note { padding: 10px 12px; }
.trip-note summary { cursor: pointer; font-size: 13.5px; color: var(--accent); list-style: none; }
.trip-note summary::-webkit-details-marker { display: none; }
.trip-note summary::before { content: "▸ "; }
.trip-note[open] summary::before { content: "▾ "; }
.trip-note .note-body {
  margin-top: 8px;
  font-size: 13px;
  line-height: 1.65;
  color: var(--muted);
  white-space: pre-wrap;
}

.records { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.record {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 8px 10px;
}
.record.compact { background: none; border: none; padding: 0; }
.record-body { flex: 1; min-width: 0; }
.record-meta { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--muted); flex-wrap: wrap; }
.record-body p { margin: 5px 0 0; font-size: 13.5px; white-space: pre-wrap; }
.record .path-hint {
  margin: 5px 0 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  color: var(--muted);
  word-break: break-all;
}
.thumb.big { width: 92px; height: 92px; }

.btn, .link-btn {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 13.5px;
  cursor: pointer;
}
.btn:hover, .link-btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--line)); }
.link-btn { background: none; border: none; color: var(--accent); text-decoration: underline; padding: 8px 6px; min-height: 36px; }
.mini-btn {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 15px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 38px;
  min-height: 38px;
  padding: 0;
  border-radius: 9px;
}
.mini-btn:hover { color: var(--text); background: var(--panel-2); }

.empty {
  text-align: center;
  color: var(--muted);
  padding: 40px 16px;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
}
.empty h3 { color: var(--text); margin-bottom: 8px; }
.empty .btn-row { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; margin-top: 14px; }

/* ---------------------------- action bar ---------------------------- */
.actionbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  display: flex;
  gap: 10px;
  padding: 10px 14px calc(10px + var(--safe-bottom));
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}
.actionbar .btn { flex: 1; text-align: center; padding: 11px; }
.actionbar .btn.primary { flex: 1.2; }

/* ------------------------------ dialogs ------------------------------ */
dialog {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 0;
  width: min(560px, calc(100% - 20px));
  max-height: 88vh;
  /* dvh tracks the mobile browser chrome as it hides/shows */
  max-height: 88dvh;
  /* Sheet scrolls vertically only: a sideways drag must not move the dialog
     (iOS hands horizontal pans to scroll containers even when nothing overflows). */
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
}
dialog::backdrop { background: rgba(4, 8, 20, 0.62); }
body:has(dialog[open]) { overflow: hidden; overscroll-behavior: none; }
dialog form { padding: 16px; display: flex; flex-direction: column; gap: 10px; }
dialog h2 { font-size: 17px; }

label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
label.inline { flex-direction: row; align-items: center; gap: 6px; color: var(--text); }
input[type="text"], input[type="date"], input[type="time"], input[type="password"],
input:not([type]), select, textarea {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  font: inherit;
  /* 16px keeps iOS Safari from zooming the page when a field is focused. */
  font-size: 16px;
  width: 100%;
  /* Controls sit in a column flex <label>: the default min-width:auto would let the
     native date/select intrinsic width push the box out of the dialog on iOS. */
  min-width: 0;
  max-width: 100%;
}
textarea { resize: vertical; }

/* iOS Safari only: it lays out native date/time/select from their own content, which
   can push the box past the dialog edge no matter the CSS width. A styled box that
   still opens the native picker is the reliable shape; the glyphs keep them readable.
   Scoped by @supports so Chromium/Android keep their own icons. */
@supports (-webkit-touch-callout: none) {
  @media (max-width: 560px) {
    input[type="date"],
    input[type="time"],
    select {
      -webkit-appearance: none;
      appearance: none;
    }
    select {
      background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'><path d='M1 1.5 6 6.5l5-5' fill='none' stroke='%2393a2c0' stroke-width='2' stroke-linecap='round'/></svg>");
      background-repeat: no-repeat;
      background-position: right 12px center;
      padding-right: 34px;
    }
    input[type="date"],
    input[type="time"] {
      background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16'><rect x='1.6' y='3.2' width='12.8' height='11.2' rx='2' fill='none' stroke='%2393a2c0' stroke-width='1.5'/><path d='M1.6 6.6h12.8M5.2 1.6v3.2M10.8 1.6v3.2' fill='none' stroke='%2393a2c0' stroke-width='1.5' stroke-linecap='round'/></svg>");
      background-repeat: no-repeat;
      background-position: right 12px center;
      padding-right: 36px;
    }
  }
}
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
/* Native date/select controls carry a large intrinsic min width; without this the
   grid items cannot shrink below it and the fields paint over each other. */
.grid2 > * { min-width: 0; }

/* Phones: give each field its own row instead of squeezing two per line. */
@media (max-width: 560px) {
  .grid2 { grid-template-columns: 1fr; }
}
.hint { color: var(--muted); font-size: 12px; margin: 0; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; margin: 4px 0 0; padding: 0; }
.dialog-actions .btn { min-width: 72px; }

.kind-row { display: flex; gap: 8px; }
.kind input { display: none; }
.kind span {
  display: block;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 13.5px;
  cursor: pointer;
  background: var(--panel-2);
}
.kind input:checked + span { background: var(--accent); border-color: var(--accent); color: #fff; }

.photo-row { display: flex; gap: 8px; }
.photo-preview { border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.photo-preview img { display: block; width: 100%; max-height: 240px; object-fit: cover; }
.photo-preview-meta { display: flex; align-items: center; gap: 8px; padding: 6px 10px; font-size: 12px; color: var(--muted); }
.photo-preview-meta span { flex: 1; }

.geo-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.import-options { display: flex; gap: 14px; align-items: center; flex-wrap: wrap; font-size: 13px; }
.import-preview { font-size: 12.5px; color: var(--muted); max-height: 180px; overflow: auto; }
.import-preview ul { margin: 6px 0; padding-left: 18px; }
.import-preview .err { color: var(--danger); }

.viewer { width: min(920px, calc(100% - 16px)); background: #05070f; border-color: #1b2440; }
.viewer figure { margin: 0; }
.viewer img {
  display: block;
  width: 100%;
  max-height: 74vh;
  max-height: 74dvh;
  object-fit: contain;
  background: #05070f;
}
.viewer figcaption { padding: 10px 14px; font-size: 13px; color: var(--muted); white-space: pre-wrap; }
.viewer .dialog-actions { padding: 0 14px 14px; }
.viewer a.btn { text-decoration: none; }

.stats { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; margin: 0; font-size: 13px; }
.stats dt { color: var(--muted); }
.stats dd { margin: 0; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(84px + var(--safe-bottom));
  transform: translateX(-50%);
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 12px;
  padding: 10px 16px;
  font-size: 13.5px;
  box-shadow: var(--shadow);
  z-index: 60;
  max-width: min(460px, calc(100% - 24px));
}
.toast.error { border-color: var(--danger); color: var(--danger); }

@media (min-width: 620px) {
  .item { grid-template-columns: 64px 18px 1fr; }
  .thumb { width: 84px; height: 84px; }
}

/* Narrow phones (iPhone SE class): give the content column more room. */
@media (max-width: 360px) {
  .view { padding: 12px 10px 24px; }
  .item { grid-template-columns: 40px 14px 1fr; gap: 0 5px; }
  .item .rail::before { left: 6px; }
  .item .dot { left: 1px; }
  .card { padding: 10px; }
  .item-head { gap: 6px; }
  .item-head h3 { font-size: 15px; }
  .chip { font-size: 10.5px; padding: 2px 6px; }
  .icon-btn { width: 40px; height: 40px; font-size: 16px; }
  .trip-select { font-size: 14.5px; padding: 8px 10px; }
  .map-links a { padding: 7px 11px; }
}
