/* ============================================================
   羽见 · 观鸟地图 — 自定义样式
   仅用于 Tailwind 无法表达的动态/复合样式（地图标记、滚动条、
   分段控件状态、动画、腾讯地图信息窗口定制等）
   ============================================================ */

:root {
  --moss-600: #326e46;
  --moss-700: #2a583a;
  --ink: #1e293b;
}

html,
body {
  margin: 0;
  padding: 0;
  font-family: "PingFang SC", "HarmonyOS Sans SC", "Microsoft YaHei", system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

/* iOS Safari 的 100vh 等于「含地址栏」的大视口，直接用它会让页面比可见区高，
   底部内容被推出屏幕、必须下拉才能看到。这里统一改用动态视口单位 dvh，
   并用 height:100% 兜底老浏览器；同时锁住滚动与橡皮筋回弹。 */
html {
  height: 100%;
  overflow: hidden;
}

body.app-shell {
  height: 100vh;
  height: 100dvh;
  max-height: 100dvh;
  width: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-overflow-scrolling: touch;
}

body.app-shell > main {
  min-height: 0;
}

/* 刘海屏 / 全面屏安全区：顶栏与页脚各自让出系统占位 */
#app-header {
  padding-left: max(0.625rem, env(safe-area-inset-left));
  padding-right: max(0.625rem, env(safe-area-inset-right));
  padding-top: env(safe-area-inset-top);
  height: calc(3rem + env(safe-area-inset-top));
}

#app-footer {
  height: calc(1.75rem + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
}

@media (min-width: 640px) {
  #app-header {
    height: calc(3.5rem + env(safe-area-inset-top));
    padding-left: max(1.25rem, env(safe-area-inset-left));
    padding-right: max(1.25rem, env(safe-area-inset-right));
  }
  #app-footer {
    height: calc(2rem + env(safe-area-inset-bottom));
  }
}

/* iOS 在输入框字号小于 16px 时会自动放大整个页面，
   放大后页面只能显示一部分、还要左右拖动。
   这个现象只在触屏设备出现，所以只在 coarse pointer 下抬到 16px；
   桌面端即便窗口很窄也不会自动缩放，保持设计字号，避免和周边 11–13px 的文字不搭。 */
@media (hover: none) and (pointer: coarse) {
  input,
  select,
  textarea {
    font-size: 16px !important;
  }
  #sort-select {
    height: 30px;
    padding-right: 4px;
  }
}

@media (max-width: 767px) {
  input[type="search"]::-webkit-search-cancel-button {
    -webkit-appearance: none;
  }
  /* 弹窗与抽屉里的滚动区域不把滚动传递给页面 */
  #sidebar,
  #results-rail,
  #result-list,
  #species-modal-card,
  #source-modal,
  #help-modal,
  #spot-suggest,
  #species-suggest {
    overscroll-behavior: contain;
  }
}

/* ---------------- 滚动条 ---------------- */
.custom-scroll::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
.custom-scroll::-webkit-scrollbar-track {
  background: transparent;
}
.custom-scroll::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 999px;
}
.custom-scroll::-webkit-scrollbar-thumb:hover {
  background: #94a3b8;
}

/* ---------------- 分段控件 ---------------- */
.seg-btn {
  height: 28px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  color: #64748b;
  background: transparent;
  transition: all 0.18s ease;
}
.seg-btn:hover {
  color: #334155;
  background: rgba(255, 255, 255, 0.7);
}
.seg-btn.active {
  background: #ffffff;
  color: var(--moss-700);
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.12);
  font-weight: 600;
}

/* ---------------- 类群 / 稀有度胶囊 ---------------- */
.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 27px;
  padding: 0 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  border: 1px solid #e2e8f0;
  background: #fff;
  color: #475569;
  transition: all 0.18s ease;
  cursor: pointer;
  user-select: none;
}
.chip:hover {
  border-color: #94a3b8;
  transform: translateY(-1px);
}
.chip.active {
  color: #fff;
  border-color: transparent;
  box-shadow: 0 4px 12px -4px rgba(15, 23, 42, 0.35);
}
.chip .dot {
  width: 6px;
  height: 6px;
  border-radius: 999px;
}

/* 物种标签 */
.stag {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  max-width: 100%;
  padding: 3px 7px;
  border-radius: 6px;
  font-size: 11.5px;
  background: #f1f5f9;
  color: #334155;
  border: 1px solid #e2e8f0;
}
.stag button {
  color: #94a3b8;
  font-size: 12px;
  line-height: 1;
}
.stag button:hover {
  color: #e11d48;
}

/* ---------------- 线性图标 ---------------- */
.ico {
  display: inline-block;
  flex-shrink: 0;
  vertical-align: -0.15em;
}

/* 触屏上避免双击缩放带来的 300ms 点击延迟 */
button,
.chip,
.seg-btn,
.rec-card,
.stag button,
a {
  touch-action: manipulation;
}
.stag .ico {
  color: var(--moss-600);
}
.stag button .ico {
  color: inherit;
  vertical-align: 0;
}
.map-tool .ico {
  pointer-events: none;
}

/* ---------------- 地图工具按钮 ---------------- */
.map-tool {
  width: 34px;
  height: 32px;
  border-radius: 8px;
  font-size: 15px;
  display: grid;
  place-items: center;
  color: #475569;
  transition: all 0.16s ease;
}
.map-tool:hover {
  background: #f1f5f9;
  color: #1e293b;
}
.map-tool.active {
  background: #e0efe3;
  color: var(--moss-700);
  box-shadow: inset 0 0 0 1px #96c6a1;
}

/* ---------------- 观测点标记 ---------------- */
.bird-pin {
  position: relative;
  border-radius: 999px;
  border: 1.5px solid #fff;
  box-shadow: 0 2px 6px rgba(15, 23, 42, 0.35);
  transition: transform 0.15s ease;
  cursor: pointer;
}
.bird-pin::after {
  content: "";
  position: absolute;
  inset: -4px;
  border-radius: 999px;
  opacity: 0;
  transition: opacity 0.15s ease;
}
.bird-pin:hover {
  transform: scale(1.45);
  z-index: 500;
}
.bird-pin.rare {
  border: 2px solid #f43f5e;
}
.bird-pin.selected {
  transform: scale(1.7);
  z-index: 600;
  box-shadow: 0 0 0 5px rgba(50, 110, 70, 0.25);
}
.bird-pin.pulse {
  animation: pinPulse 1.6s ease-out 2;
}
@keyframes pinPulse {
  0% {
    box-shadow: 0 0 0 0 rgba(50, 110, 70, 0.55);
  }
  100% {
    box-shadow: 0 0 0 16px rgba(50, 110, 70, 0);
  }
}

/* 鸟点（地点本身）标记 */
.spot-pin {
  width: 24px;
  height: 24px;
  border-radius: 8px;
  background: #fff;
  border: 1.5px solid #cbd5e1;
  display: grid;
  place-items: center;
  font-size: 12px;
  box-shadow: 0 2px 6px rgba(15, 23, 42, 0.18);
  cursor: pointer;
  transition: all 0.15s ease;
}
.spot-pin:hover {
  border-color: var(--moss-600);
  transform: scale(1.12);
}
.spot-pin.active {
  border-color: var(--moss-600);
  background: #e0efe3;
  box-shadow: 0 0 0 4px rgba(50, 110, 70, 0.18);
}

/* 选点十字准星 */
.origin-pin {
  width: 28px;
  height: 28px;
  border-radius: 999px;
  background: var(--moss-600);
  border: 3px solid #fff;
  box-shadow: 0 0 0 3px rgba(50, 110, 70, 0.3), 0 4px 10px rgba(15, 23, 42, 0.3);
  display: grid;
  place-items: center;
  color: #fff;
  font-size: 12px;
}

/* ---------------- 腾讯地图容器与信息窗口 ---------------- */
#map {
  width: 100%;
  height: 100%;
  font-family: inherit;
  background: #e2e8f0;
  /* 关键：腾讯地图会在容器内插入 z-index:1000 的图层，
     若 #map 不形成独立层叠上下文，该图层会盖住 z-[900] 的自定义工具条，
     导致底图切换、热区、缩放等按钮点不到。这里隔离出来。 */
  position: absolute;
  z-index: 0;
  isolation: isolate;
}

/* 腾讯地图自带控件（指北针 + 缩放按钮）会吸附在右上角，
   与自定义工具条位置冲突。缩放与视角操作已由右侧工具条接管，
   这里直接隐藏自带控件，保证任何浏览器下都不再遮挡。 */
#map .rotate-circle,
#map .tmap-zoom-control,
#map .tmap-scale-control,
#map .tmap-pitch-control {
  display: none !important;
}

/* 兜底：部分版本控件外层容器会占用点击区域，一并收掉 */
#map div[contenteditable="false"]:has(> .rotate-circle):has(> .tmap-zoom-control),
#map div[contenteditable="false"]:has(> .tmap-zoom-control) {
  pointer-events: none !important;
}
.bird-info {
  width: 238px;
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 10px 28px -10px rgba(15, 23, 42, 0.45);
  line-height: 1.45;
}
#map .tmap-attribution,
#map .tmap-copyright {
  font-size: 9px !important;
  opacity: 0.7;
}
.pop-media {
  position: relative;
  height: 104px;
  background: linear-gradient(135deg, #e0efe3, #cbd5e1);
  overflow: hidden;
}
.pop-media img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.pop-media .pop-emoji {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 40px;
  opacity: 0.85;
}
.pop-rare {
  position: absolute;
  top: 6px;
  left: 6px;
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 999px;
  background: rgba(244, 63, 94, 0.92);
  color: #fff;
  font-weight: 600;
}

/* 热区圆 */
.heat-ring {
  transition: all 0.25s ease;
}

/* ---------------- 记录卡片 ---------------- */
.rec-card {
  border: 1px solid #e9eef4;
  border-radius: 12px;
  padding: 9px 10px;
  background: #fff;
  transition: all 0.16s ease;
  cursor: pointer;
  /* iOS 上连续点按会先选中文字，抢走 click，这里禁掉 */
  -webkit-user-select: none;
  user-select: none;
}
.rec-card:hover {
  border-color: #96c6a1;
  background: #f7fbf8;
  transform: translateX(-2px);
  box-shadow: 0 6px 16px -10px rgba(15, 23, 42, 0.4);
}
.rec-card.selected {
  border-color: var(--moss-600);
  background: #f2f8f3;
  box-shadow: 0 0 0 2px rgba(50, 110, 70, 0.14);
}

/* ---------------- 环形图 / 条形 ---------------- */
.donut {
  width: 58px;
  height: 58px;
  border-radius: 999px;
  position: relative;
  background: #eef2f7;
}
.donut::after {
  content: "";
  position: absolute;
  inset: 12px;
  border-radius: 999px;
  background: #fff;
}
.donut-legend-row {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 10.5px;
  color: #64748b;
  line-height: 1.2;
}
.mini-bar-track {
  height: 5px;
  border-radius: 999px;
  background: #eef2f7;
  overflow: hidden;
}
.mini-bar-fill {
  height: 100%;
  border-radius: 999px;
  transition: width 0.45s cubic-bezier(0.22, 1, 0.36, 1);
}

/* 图片加载失败时的回退 */
.img-fallback {
  display: grid !important;
  place-items: center;
}
.img-fallback::after {
  content: "";
  width: 16px;
  height: 16px;
  border-radius: 999px;
  border: 1.5px solid #94a3b8;
  opacity: 0.65;
}

/* ---------------- 动画 ---------------- */
.pop-in {
  animation: popIn 0.24s cubic-bezier(0.22, 1.2, 0.36, 1) both;
}
@keyframes popIn {
  from {
    opacity: 0;
    transform: translateY(10px) scale(0.97);
  }
  to {
    opacity: 1;
    transform: none;
  }
}
.fade-in {
  animation: fadeIn 0.3s ease both;
}
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}
.slide-up {
  animation: slideUp 0.35s cubic-bezier(0.22, 1, 0.36, 1) both;
}
@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(18px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

/* ---------------- 响应式：侧栏与记录栏 ---------------- */
@media (max-width: 767px) {
  #sidebar {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(86vw, 330px);
    transform: translateX(-100%);
    transition: transform 0.28s cubic-bezier(0.22, 1, 0.36, 1);
    box-shadow: 12px 0 32px -12px rgba(15, 23, 42, 0.4);
  }
  #sidebar.open {
    transform: none;
  }
}

@media (max-width: 1279px) {
  #results-rail {
    position: absolute;
    top: 0;
    bottom: 0;
    right: 0;
    width: 344px;
    max-width: 88vw;
    transform: translateX(102%);
    transition: transform 0.28s cubic-bezier(0.22, 1, 0.36, 1);
    box-shadow: -12px 0 32px -12px rgba(15, 23, 42, 0.4);
  }
  #results-rail.open {
    transform: none;
  }
}

/* ---------------- 移动端体验优化 ---------------- */
@media (max-width: 767px) {
  /* 触控目标放大：分段控件与胶囊 */
  .seg-btn {
    height: 34px;
    font-size: 12.5px;
  }
  .chip {
    height: 31px;
    padding: 0 11px;
    font-size: 12.5px;
  }
  .map-tool {
    width: 36px;
    height: 34px;
  }
  /* 地图浮层收紧，给地图本体留出可视面积 */
  #stat-card {
    top: 8px;
    left: 8px;
    max-width: 196px;
  }
  #stat-card .px-3 {
    padding-left: 10px;
    padding-right: 10px;
  }
  #map-tools {
    top: 8px;
    right: 8px;
    gap: 6px;
  }
  #map-tools > div {
    padding: 3px;
    gap: 3px;
  }
  /* 记录入口固定在底部居中，拇指可达，避免与右上角工具条抢位置 */
  #btn-toggle-results {
    position: fixed;
    left: 50%;
    bottom: calc(44px + env(safe-area-inset-bottom));
    transform: translateX(-50%);
    height: 40px;
    padding: 0 18px;
    border-radius: 999px;
    font-size: 12.5px;
    z-index: 950;
  }
  /* 移动端图例铺开在开关按钮上方 */
  #legend-card {
    right: 10px;
    bottom: calc(58px + env(safe-area-inset-bottom));
    max-width: 176px;
  }
  #btn-legend {
    bottom: calc(10px + env(safe-area-inset-bottom));
  }
  /* 记录列表在窄屏接近全宽，读起来不挤 */
  #results-rail.open {
    box-shadow: -12px 0 32px -12px rgba(15, 23, 42, 0.4);
  }
  #toast {
    bottom: calc(94px + env(safe-area-inset-bottom));
  }
  /* 抽屉顶部与底部留出安全区，底部按钮不被 Home 指示条压住 */
  #sidebar {
    padding-bottom: calc(1.5rem + env(safe-area-inset-bottom));
  }
  #results-rail {
    padding-bottom: env(safe-area-inset-bottom);
  }
  #species-modal-card,
  #source-modal > div:last-child,
  #help-modal > div:last-child {
    max-height: calc(92dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  }
  /* 弹窗内边距收紧，避免左右贴边 */
  #species-modal,
  #source-modal,
  #help-modal {
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
  }
}

@media (max-width: 380px) {
  #sidebar {
    width: 88vw;
  }
  #stat-card {
    max-width: 168px;
  }
  #stat-card #stat-species,
  #stat-card #stat-records {
    font-size: 18px;
  }
}

/* 横屏手机：纵向空间极小，收起页脚把高度让给地图 */
@media (max-height: 500px) and (orientation: landscape) {
  #app-header {
    height: calc(2.5rem + env(safe-area-inset-top));
  }
  #app-footer {
    display: none;
  }
  #btn-toggle-results {
    bottom: calc(10px + env(safe-area-inset-bottom));
  }
}

@media (max-width: 479px) {
  #results-rail {
    width: 100%;
    max-width: 100%;
  }
}

/* 记录栏展开时，底部悬浮按钮与图例按钮让位 */
@supports selector(body:has(*)) {
  body:has(#results-rail.open) #btn-toggle-results,
  body:has(#results-rail.open) #btn-legend {
    opacity: 0;
    pointer-events: none;
  }
}

#btn-toggle-results,
#btn-legend {
  transition: opacity 0.2s ease, transform 0.15s ease, background-color 0.16s ease, color 0.16s ease;
}

/* 地图浮层提示（腾讯地图信息窗口内文本） */
.bird-info .pop-media {
  height: 104px;
}
