/* ================= FindMy 管理台 ================= */
:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: #172033;
  --border: #334155;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #3b82f6;
  --fresh: #22c55e;
  --stale: #eab308;
  --old: #64748b;
  --danger: #ef4444;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: -apple-system, "PingFang TC", "PingFang SC", "Microsoft JhengHei", "Noto Sans TC", sans-serif;
  background: var(--bg); color: var(--text); font-size: 14px;
  display: flex; flex-direction: column; overflow: hidden;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }

/* ---------- 頂部工具列 ---------- */
#topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 14px; background: var(--panel);
  border-bottom: 1px solid var(--border); flex: 0 0 auto;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; }
.brand-icon { font-size: 18px; }
.topbar-spacer { flex: 1; }

.pill {
  padding: 3px 12px; border-radius: 999px; font-size: 12px; font-weight: 600;
  border: 1px solid var(--border);
}
.pill-green { color: var(--fresh); border-color: var(--fresh); }
.pill-red { color: var(--danger); border-color: var(--danger); }
.pill-gray { color: var(--muted); }
.pill-yellow { color: var(--stale); border-color: var(--stale); }

.accounts { display: flex; gap: 6px; font-size: 12px; color: var(--muted); }
.accounts .acct {
  background: var(--panel-2); border: 1px solid var(--border);
  padding: 3px 10px; border-radius: 6px; max-width: 220px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.accounts .acct b { color: var(--text); font-weight: 600; }

/* ---------- Apple 未登入提示橫額 ---------- */
.banner {
  flex: 0 0 auto; padding: 7px 14px; font-size: 12.5px; line-height: 1.5;
  background: rgba(234, 179, 8, .12); color: #fde68a;
  border-bottom: 1px solid rgba(234, 179, 8, .35);
}
.banner code {
  background: rgba(0, 0, 0, .3); padding: 1px 5px; border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11.5px;
}
.banner { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.banner-btn { flex: 0 0 auto; width: auto; }

/* ---------- Apple 帳號登入面板 ---------- */
.apple-panel {
  position: fixed; left: 0; right: 0; z-index: 1500;
  background: var(--panel); border-bottom: 1px solid var(--border);
  box-shadow: 0 14px 40px rgba(0, 0, 0, .55);
  max-height: calc(100vh - 56px); overflow: auto;
}
.ap-head {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--panel); z-index: 1;
}
.ap-title { font-weight: 700; font-size: 15px; }
.ap-note { font-size: 12px; }
.ap-close { margin-left: auto; }

.ap-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 0;
}
.ap-sec { padding: 14px 16px; display: flex; flex-direction: column; gap: 10px; }
.ap-sec + .ap-sec { border-left: 1px solid var(--border); }
.ap-sec-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.ap-sec-title { font-weight: 600; font-size: 13.5px; }
.ap-sec-status { font-size: 12px; color: var(--muted); }
.ap-sec-status.ok { color: var(--fresh); }
.ap-sec-status.wait { color: var(--stale); }

.ap-form { display: flex; flex-direction: column; gap: 8px; }
.ap-form input[type="email"], .ap-form input[type="password"], .ap-form input[type="text"] {
  /* 注意：不要加 width:100% —— 與 .ap-row 的 flex 搭配會觸發 Chromium
     insertText 的自動化異常；flex:1 已負責撐滿寬度 */
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px; padding: 7px 10px; font-size: 13px;
}
.ap-form select {
  flex: 1; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px; padding: 7px 10px; font-size: 13px;
}
.ap-row { display: flex; gap: 8px; align-items: center; }
.ap-row input { flex: 1; }
.ap-actions { display: flex; gap: 8px; }
.ap-hint { font-size: 12px; color: var(--muted); line-height: 1.5; }
.ap-check { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--muted); }
.ap-ok {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  font-size: 13px; color: var(--fresh);
}
.ap-err { color: var(--danger); font-size: 12.5px; line-height: 1.5; }
@media (max-width: 900px) {
  .ap-grid { grid-template-columns: 1fr; }
  .ap-sec + .ap-sec { border-left: none; border-top: 1px solid var(--border); }
}

.stats { display: flex; gap: 12px; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }

.ctl { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
select, input {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
  padding: 5px 8px; font-size: 13px;
}
select:focus, input:focus { outline: 1px solid var(--accent); }

.btn {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
  padding: 5px 10px; font-size: 14px; cursor: pointer; line-height: 1.2;
}
.btn:hover { border-color: var(--accent); }
.btn-ghost { background: transparent; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; width: 100%; padding: 9px; }
.btn-primary:hover { filter: brightness(1.1); }
.btn-sm { padding: 3px 9px; font-size: 12px; }

/* ---------- 主體佈局 ---------- */
#layout { flex: 1 1 auto; display: flex; min-height: 0; }

#sidebar {
  flex: 0 0 300px; display: flex; flex-direction: column;
  background: var(--panel); border-right: 1px solid var(--border);
  min-width: 0;
}
#main { flex: 1 1 auto; display: flex; min-width: 0; min-height: 0; position: relative; }
.view { flex: 1 1 auto; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.pane { flex: 1 1 auto; overflow-y: auto; min-height: 0; }
.sidebar-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 14px; font-weight: 700; font-size: 13px;
  border-bottom: 1px solid var(--border);
}
.device-list { flex: 1; overflow-y: auto; }

.device-item {
  display: grid; grid-template-columns: 10px 1fr auto; gap: 4px 10px;
  padding: 10px 14px; border-bottom: 1px solid var(--border);
  cursor: pointer; position: relative;
}
.device-item:hover { background: var(--panel-2); }
.device-item.active { background: rgba(59, 130, 246, .15); box-shadow: inset 3px 0 0 var(--accent); }

.dot { width: 9px; height: 9px; border-radius: 50%; margin-top: 6px; }
.dot.fresh { background: var(--fresh); box-shadow: 0 0 6px var(--fresh); }
.dot.stale { background: var(--stale); }
.dot.old { background: var(--old); }

.di-main { min-width: 0; }
.di-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.di-sub { font-size: 12px; color: var(--muted); display: flex; gap: 8px; margin-top: 2px; }
.di-side { text-align: right; font-size: 12px; color: var(--muted); }
.di-batt { font-weight: 600; color: var(--text); }

.empty { padding: 30px 16px; text-align: center; font-size: 13px; }

/* ---------- 地圖 ---------- */
#view-map { position: relative; }
#map { position: absolute; inset: 0; background: #0b1220; }
.leaflet-container { background: #0b1220; font-family: inherit; }
.leaflet-popup-content-wrapper, .leaflet-popup-tip {
  background: var(--panel); color: var(--text);
  box-shadow: 0 4px 18px rgba(0,0,0,.5);
}
.leaflet-popup-content { margin: 10px 14px; font-size: 13px; line-height: 1.55; }
.leaflet-popup-content b { font-size: 14px; }
.leaflet-control-attribution { background: rgba(30,41,59,.8) !important; color: var(--muted) !important; }
.leaflet-control-attribution a { color: var(--accent) !important; }
.leaflet-bar a { background: var(--panel) !important; color: var(--text) !important; border-color: var(--border) !important; }
.leaflet-bar a:hover { background: var(--panel-2) !important; }

/* 裝置標記 */
.dev-marker-wrap { background: none !important; border: none !important; }
.dev-marker {
  width: 32px; height: 32px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 15px; border: 2px solid #fff;
  background: var(--old); color: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,.5);
}
.dev-marker.fresh { background: var(--fresh); }
.dev-marker.stale { background: var(--stale); }
.dev-marker.old { background: var(--old); }
.dev-marker.selected { border-color: var(--accent); transform: scale(1.15); }

.popup-actions { margin-top: 8px; display: flex; gap: 6px; flex-wrap: wrap; }
.popup-actions .btn-sm { background: var(--accent); border-color: var(--accent); color: #fff; }
.popup-actions .btn-ghost { background: transparent; }

/* ---------- 歷史軌跡面板 ---------- */
#history-panel {
  position: absolute; right: 14px; top: 64px; bottom: 14px; width: 320px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  display: flex; flex-direction: column; z-index: 1000;
  box-shadow: 0 8px 30px rgba(0,0,0,.5);
}
.history-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 14px; border-bottom: 1px solid var(--border); font-weight: 700;
}
.history-list { flex: 1; overflow-y: auto; padding: 6px 0; }
.history-row {
  display: flex; justify-content: space-between; gap: 10px;
  padding: 7px 14px; font-size: 12px; cursor: pointer;
  border-left: 3px solid transparent;
}
.history-row:hover { background: var(--panel-2); border-left-color: var(--accent); }
.history-row .t { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.history-row .c { font-variant-numeric: tabular-nums; }

/* ---------- 登入浮層 ---------- */
#login-overlay {
  position: fixed; inset: 0; z-index: 2000;
  background: rgba(15, 23, 42, .92); backdrop-filter: blur(3px);
  display: flex; align-items: center; justify-content: center;
}
.login-card {
  width: 380px; max-width: 92vw; background: var(--panel);
  border: 1px solid var(--border); border-radius: 14px; padding: 26px;
  display: flex; flex-direction: column; gap: 14px;
}
.login-card h2 { font-size: 18px; }
.login-card p { font-size: 13px; line-height: 1.5; }
.login-card label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.login-error { color: var(--danger); font-size: 13px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--panel); border: 1px solid var(--accent);
  padding: 10px 18px; border-radius: 8px; z-index: 3000;
  box-shadow: 0 6px 24px rgba(0,0,0,.5); font-size: 13px; max-width: 80vw;
}
.toast.error { border-color: var(--danger); color: var(--danger); }

/* ---------- 分頁列 ---------- */
#tabs {
  flex: 0 0 auto; display: flex; gap: 2px;
  background: var(--panel-2); border-bottom: 1px solid var(--border);
  padding: 0 10px;
}
.tab {
  background: none; border: none; color: var(--muted);
  padding: 9px 16px; font-size: 13px; font-weight: 600; cursor: pointer;
  border-bottom: 2px solid transparent; position: relative;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.badge {
  display: inline-block; min-width: 18px; padding: 1px 6px; margin-left: 5px;
  background: var(--accent); color: #fff; border-radius: 999px;
  font-size: 11px; font-weight: 700; text-align: center;
}

/* ---------- 共用小元件 ---------- */
.small { font-size: 12px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.err { color: var(--danger); font-size: 12.5px; line-height: 1.5; }
.btn-danger { border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: rgba(239, 68, 68, .15); }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0; }
.check-row { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin: 8px 0; }
.check-row label { display: flex; align-items: center; gap: 6px; font-size: 13px; }
.inline-row { display: flex; align-items: center; gap: 8px; margin: 8px 0; font-size: 13px; }
code {
  background: rgba(0, 0, 0, .3); padding: 1px 5px; border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11.5px;
}

/* ---------- 地理圍欄：側邊列表 ---------- */
.zone-toolbar {
  display: flex; flex-direction: column; gap: 6px;
  padding: 10px 12px; border-bottom: 1px solid var(--border);
}
.zone-list { flex: 1; overflow-y: auto; }
.zone-item {
  display: grid; grid-template-columns: 12px 1fr auto; gap: 4px 10px;
  padding: 10px 12px; border-bottom: 1px solid var(--border);
  cursor: pointer; align-items: center;
}
.zone-item:hover { background: var(--panel-2); }
.zone-item.active { background: rgba(59, 130, 246, .15); box-shadow: inset 3px 0 0 var(--accent); }
.zone-swatch { width: 12px; height: 12px; border-radius: 3px; }
.zone-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.zone-sub { font-size: 11.5px; color: var(--muted); grid-column: 2; }
.zone-badge { font-size: 11px; color: var(--muted); text-align: right; }
.zone-badge b { color: var(--fresh); }

/* ---------- 事件篩選 ---------- */
.filter-box { display: flex; flex-direction: column; gap: 10px; padding: 14px 12px; }
.filter-box label { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; color: var(--muted); }

/* ---------- 視圖（事件 / 通知） ---------- */
.view-bar {
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
  padding: 12px 18px; border-bottom: 1px solid var(--border); background: var(--panel);
}
.view-bar h2 { font-size: 15px; }
.view-actions { display: flex; gap: 8px; }
.table-wrap { flex: 1 1 0; min-height: 0; overflow: auto; padding: 0 18px 18px; }
table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { color: var(--muted); font-weight: 600; position: sticky; top: 0; background: var(--panel); white-space: nowrap; }
td { font-variant-numeric: tabular-nums; }
tr:hover td { background: var(--panel-2); }
.ev-enter { color: var(--fresh); font-weight: 700; }
.ev-exit { color: #f59e0b; font-weight: 700; }
.notify-ok { color: var(--fresh); }
.notify-fail { color: var(--danger); }
.notify-none { color: var(--muted); }

/* ---------- 通知設定 ---------- */
.notify-grid {
  flex: 1 1 0; min-height: 0; overflow-y: auto; display: grid;
  grid-template-columns: repeat(auto-fit, minmax(380px, 1fr));
  gap: 16px; padding: 18px; align-content: start;
}
.card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 16px 18px; display: flex; flex-direction: column; gap: 4px;
}
.card h3 { font-size: 14.5px; margin-bottom: 8px; }
.wa-status-row { display: flex; align-items: center; gap: 10px; margin: 6px 0 10px; }
#wa-qr-box {
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  padding: 14px; background: #fff; border-radius: 10px; margin-bottom: 10px;
}
#wa-qr-box p { color: #334155; }
#wa-qr-img { width: 240px; height: 240px; image-rendering: pixelated; }
#rc-table input { width: 100%; }
#rc-table td { vertical-align: middle; }

/* ---------- 地理圍欄編輯面板 ---------- */
.side-panel {
  position: absolute; right: 14px; top: 14px; bottom: 14px; width: 340px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  display: flex; flex-direction: column; z-index: 1100; overflow: hidden;
  box-shadow: 0 8px 30px rgba(0, 0, 0, .5);
}
.sp-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 14px; border-bottom: 1px solid var(--border); font-weight: 700;
  flex: 0 0 auto;
}
/* 注意 min-height:0 —— 沒有它 flex 項目不會縮到內容以下，sp-foot 會被擠出面板 */
.sp-body { flex: 1 1 0; min-height: 0; overflow-y: auto; padding: 12px 14px; display: flex; flex-direction: column; gap: 4px; }
.sp-body > label { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; color: var(--muted); margin-bottom: 8px; }
.sp-foot {
  display: flex; gap: 8px; padding: 10px 14px;
  border-top: 1px solid var(--border); flex: 0 0 auto;
}
.ze-fieldset {
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px;
  margin: 4px 0 10px; font-size: 12.5px;
}
.ze-fieldset legend { color: var(--muted); padding: 0 5px; }
.check-list { display: flex; flex-direction: column; gap: 5px; max-height: 160px; overflow-y: auto; }
.check-list label { display: flex; align-items: center; gap: 7px; font-size: 12.5px; color: var(--text); }
.vertex-list { display: flex; flex-direction: column; gap: 4px; margin: 8px 0; max-height: 180px; overflow-y: auto; }
.vertex-row { display: flex; align-items: center; gap: 6px; font-size: 12px; }
.vertex-row .v-num { color: var(--muted); min-width: 34px; }
.vertex-row .v-coord { flex: 1; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.vertex-row button { flex: 0 0 auto; }

/* 地圖上的區域標籤 */
.zone-label {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 6px; padding: 2px 8px; font-size: 11.5px; font-weight: 600;
  color: var(--text); white-space: nowrap; box-shadow: 0 2px 8px rgba(0, 0, 0, .4);
}
.zone-label small { color: var(--muted); font-weight: 400; margin-left: 4px; }

/* ---------- 窄屏適配 ---------- */
@media (max-width: 760px) {
  #sidebar { flex-basis: 220px; }
  .stats { display: none; }
  #history-panel { width: calc(100vw - 28px); }
  .side-panel { width: calc(100vw - 28px); }
  #tabs { overflow-x: auto; }
}
