:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #17202a;
  --muted: #6b7683;
  --line: #e4e7ec;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --green: #16a34a;
  --green-bg: #e6f6ec;
  --blue: #2563eb;
  --blue-bg: #e8effd;
  --amber: #b45309;
  --amber-bg: #fdf1e0;
  --red: #dc2626;
  --red-bg: #fdecec;
  --purple: #7c3aed;
  --purple-bg: #f1eafd;
  --gray: #6b7683;
  --gray-bg: #eef0f3;
  --radius: 16px;
  --shadow: 0 1px 3px rgba(16, 24, 40, .08), 0 1px 2px rgba(16, 24, 40, .04);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101418;
    --card: #1a2027;
    --text: #eef1f4;
    --muted: #8b96a3;
    --line: #29313b;
    --accent: #3b82f6;
    --green: #34d399;
    --green-bg: #10321f;
    --blue: #60a5fa;
    --blue-bg: #14263f;
    --amber: #fbbf24;
    --amber-bg: #3a2b0d;
    --red: #f87171;
    --red-bg: #3d1515;
    --purple: #a78bfa;
    --purple-bg: #291d44;
    --gray: #8b96a3;
    --gray-bg: #232a33;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--red); font-size: 14px; margin: 8px 0 0; }

.screen {
  min-height: 100vh;
  min-height: 100dvh;
  padding-bottom: env(safe-area-inset-bottom);
}

/* ---- PIN screen ---- */
#pin-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.pin-box {
  width: 100%;
  max-width: 320px;
  text-align: center;
}
.pin-logo { font-size: 56px; }
.pin-box h1 { margin: 8px 0 4px; font-size: 24px; }
.pin-box p { margin: 0 0 24px; }
#pin-input {
  width: 100%;
  font-size: 24px;
  text-align: center;
  letter-spacing: 8px;
  padding: 14px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
  margin-bottom: 12px;
}
#pin-input:focus { outline: 2px solid var(--accent); border-color: transparent; }

/* ---- Buttons ---- */
.btn {
  font: inherit;
  font-weight: 600;
  border: none;
  border-radius: 12px;
  padding: 12px 20px;
  cursor: pointer;
  background: var(--gray-bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.danger { background: var(--red-bg); color: var(--red); }
.btn.ghost { background: transparent; color: var(--muted); }
.btn.small { padding: 8px 14px; font-size: 14px; border-radius: 10px; }
.btn.full { width: 100%; }
.btn:active { opacity: .8; }
.btn:disabled { opacity: .5; cursor: default; }

.icon-btn {
  font-size: 22px;
  line-height: 1;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  width: 42px;
  height: 42px;
  cursor: pointer;
  color: var(--text);
  box-shadow: var(--shadow);
  -webkit-tap-highlight-color: transparent;
}
.icon-btn:active { opacity: .7; }
.icon-btn.on { background: var(--blue-bg); border-color: var(--accent); }

/* ---- Header ---- */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(12px + env(safe-area-inset-top)) 16px 12px;
  position: sticky;
  top: 0;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  z-index: 10;
}
.app-header h1 { margin: 0; font-size: 22px; }
.header-actions { display: flex; gap: 8px; }

/* ---- Banners ---- */
.banner {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 4px 16px 8px;
  padding: 12px 14px;
  background: var(--blue-bg);
  color: var(--text);
  border-radius: var(--radius);
  font-size: 14px;
}
.banner .share-icon { display: inline-block; transform: rotate(0deg); }
.banner-close {
  margin-left: auto;
  background: none;
  border: none;
  font-size: 20px;
  color: var(--muted);
  cursor: pointer;
  padding: 0 4px;
}

/* ---- Package list ---- */
.package-list {
  padding: 4px 16px 32px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-width: 640px;
  margin: 0 auto;
  width: 100%;
}

.pkg-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
  cursor: pointer;
  border: 1px solid var(--line);
  -webkit-tap-highlight-color: transparent;
}
.pkg-card:active { transform: scale(.99); }
.pkg-card.archived { opacity: .55; }

.pkg-top {
  display: flex;
  align-items: center;
  gap: 10px;
}
.pkg-name {
  font-weight: 650;
  font-size: 16px;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.status-badge {
  font-size: 12px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 999px;
  white-space: nowrap;
}
.st-green { background: var(--green-bg); color: var(--green); }
.st-blue { background: var(--blue-bg); color: var(--blue); }
.st-amber { background: var(--amber-bg); color: var(--amber); }
.st-red { background: var(--red-bg); color: var(--red); }
.st-purple { background: var(--purple-bg); color: var(--purple); }
.st-gray { background: var(--gray-bg); color: var(--gray); }

.pkg-sub {
  margin-top: 4px;
  font-size: 13px;
  color: var(--muted);
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.pkg-event {
  margin-top: 8px;
  font-size: 14px;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.pkg-eta {
  margin-top: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--blue);
  display: flex;
  align-items: center;
  gap: 6px;
}
.pkg-eta.delivered { color: var(--green); }

/* ---- Empty state ---- */
.empty-state {
  text-align: center;
  padding: 48px 32px;
  max-width: 400px;
  margin: 0 auto;
}
.empty-icon { font-size: 64px; }
.empty-state h2 { margin: 12px 0 4px; }
.empty-state p { margin: 0 0 20px; }

.archived-toggle-wrap { text-align: center; padding-bottom: 32px; }

/* ---- Sheets ---- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(9, 12, 16, .55);
  z-index: 50;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  animation: fadeIn .15s ease;
}
@keyframes fadeIn { from { opacity: 0; } }

.sheet {
  background: var(--bg);
  width: 100%;
  max-width: 640px;
  border-radius: 20px 20px 0 0;
  padding: 8px 20px calc(24px + env(safe-area-inset-bottom));
  max-height: 88vh;
  max-height: 88dvh;
  overflow-y: auto;
  animation: slideUp .22s ease;
}
@keyframes slideUp { from { transform: translateY(40px); opacity: .6; } }

.sheet-handle {
  width: 40px;
  height: 4px;
  border-radius: 2px;
  background: var(--line);
  margin: 6px auto 14px;
}
.sheet h2 { margin: 0 0 16px; font-size: 20px; }

.sheet label {
  display: block;
  font-size: 14px;
  font-weight: 600;
  margin: 14px 0 6px;
}
.sheet input[type="text"], .sheet textarea {
  width: 100%;
  font: inherit;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
  resize: none;
}
.sheet input:focus, .sheet textarea:focus { outline: 2px solid var(--accent); border-color: transparent; }

.sheet-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 20px;
}

/* ---- Detail view ---- */
.detail-header { display: flex; align-items: flex-start; gap: 10px; }
.detail-header h2 { margin: 0; flex: 1; font-size: 20px; word-break: break-word; }
.detail-meta { font-size: 13px; color: var(--muted); margin: 6px 0 2px; }
.detail-eta {
  margin: 14px 0;
  padding: 12px 14px;
  background: var(--blue-bg);
  color: var(--blue);
  font-weight: 600;
  border-radius: 12px;
  font-size: 14px;
}
.detail-eta.delivered { background: var(--green-bg); color: var(--green); }
.detail-error {
  margin: 14px 0;
  padding: 12px 14px;
  background: var(--red-bg);
  color: var(--red);
  border-radius: 12px;
  font-size: 14px;
}

.detail-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin: 14px 0 18px;
}

/* ---- Timeline ---- */
.timeline { margin: 8px 0 0; padding: 0; list-style: none; }
.timeline li {
  position: relative;
  padding: 0 0 18px 26px;
  border-left: 2px solid var(--line);
  margin-left: 8px;
}
.timeline li:last-child { border-left-color: transparent; padding-bottom: 4px; }
.timeline li::before {
  content: "";
  position: absolute;
  left: -7px;
  top: 2px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--muted);
  border: 2px solid var(--bg);
}
.timeline li.tl-current::before { background: var(--accent); }
.timeline li.tl-delivered::before { background: var(--green); }
.tl-time { font-size: 12px; color: var(--muted); }
.tl-desc { font-size: 14px; margin: 2px 0; line-height: 1.4; }
.tl-loc { font-size: 12px; color: var(--muted); }

.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid rgba(127, 127, 127, .3);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
  vertical-align: -3px;
}
@keyframes spin { to { transform: rotate(360deg); } }
