/* ============================================================
   portal.css — 啟動台主題層（明暗雙主題）
   作法：osh-ui.css 的 :root 本身就是一套完整亮色 token，本檔用
        CSS light-dark() 一次寫死「亮值, 暗值」，由 color-scheme 決定取哪個。
        <html data-theme="light|dark"> 覆寫使用者選擇；沒有屬性時跟隨系統。
   相容：不支援 light-dark() 的舊瀏覽器會忽略這些宣告，退回 osh-ui.css 的亮色，
        頁面仍可用（每個 --p- token 都另有一行純亮色 fallback 寫在前面）。
   引入順序：先 osh-ui.css，再本檔。
   ============================================================ */

:root { color-scheme: light dark; }
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"]  { color-scheme: dark; }

:root {
  /* ── osh-ui token 的暗色對應（亮色沿用 osh-ui.css 原值）── */
  --ui-background:         light-dark(#f4f6fa, #0b1120);
  --ui-foreground:         light-dark(#151b2b, #e6edf6);
  --ui-card:               light-dark(#ffffff, #131c2e);
  --ui-card-foreground:    light-dark(#151b2b, #e6edf6);
  --ui-popover:            light-dark(#ffffff, #131c2e);
  --ui-popover-foreground: light-dark(#151b2b, #e6edf6);

  --ui-primary:             light-dark(#1d4ed8, #38bdf8);
  --ui-primary-foreground:  light-dark(#ffffff, #06121f);
  --ui-primary-hover:       light-dark(#1e40af, #7dd3fc);
  --ui-secondary:           light-dark(#e9eef7, #1e293b);
  --ui-secondary-foreground:light-dark(#31405c, #cbd5e1);
  --ui-secondary-hover:     light-dark(#dde5f2, #273449);
  --ui-muted:               light-dark(#eef2f8, #1a2436);
  --ui-muted-foreground:    light-dark(#556074, #9aa9c2);
  --ui-accent:              light-dark(#e9eef7, #1e293b);
  --ui-accent-foreground:   light-dark(#151b2b, #e6edf6);

  --ui-brand:               light-dark(#b45309, #f59e0b);
  --ui-brand-foreground:    light-dark(#ffffff, #1a1205);
  --ui-brand-hover:         light-dark(#92400e, #fbbf24);

  --ui-border: light-dark(#d9e0ec, #243049);
  --ui-input:  light-dark(#c8d3e4, #2a3852);

  /* ── 啟動台專屬 token（每項先給亮色 fallback，再給 light-dark 版）── */
  --p-topbar-bg: rgba(255,255,255,.82);
  --p-topbar-bg: light-dark(rgba(255,255,255,.82), rgba(11,17,32,.72));
  --p-card-to: #fbfcfe;
  --p-card-to: light-dark(#fbfcfe, #0f1726);
  --p-hover-ring: rgba(29,58,95,.16);
  --p-hover-ring: light-dark(rgba(29,58,95,.16), rgba(56,189,248,.32));
  --p-glow-a: rgba(29,78,216,.07);
  --p-glow-a: light-dark(rgba(29,78,216,.07), rgba(56,189,248,.12));
  --p-glow-b: rgba(180,83,9,.06);
  --p-glow-b: light-dark(rgba(180,83,9,.06), rgba(245,158,11,.08));

  --p-danger: #dc2626;
  --p-danger: light-dark(#dc2626, #f87171);
  --p-led-idle: #94a3b8;
  --p-led-idle: light-dark(#94a3b8, #64748b);
  --p-led-up:   #059669;
  --p-led-up:   light-dark(#059669, #22c55e);
  --p-led-down: #dc2626;
  --p-led-down: light-dark(#dc2626, #ef4444);
  --p-led-wait: #b45309;
  --p-led-wait: light-dark(#b45309, #eab308);
  --p-led-local:#7c3aed;
  --p-led-local:light-dark(#7c3aed, #a78bfa);
  /* 燈點是非文字元素（3:1 就夠），但「HTTP 502」是文字，壓在離線卡的
     muted 底色上要 4.5:1 → 文字另用一組更飽和的紅。 */
  --p-down-text: #b91c1c;
  --p-down-text: light-dark(#b91c1c, #f87171);

  --p-wrap-max: 1360px;
  --p-gap: .85rem;

  --grid-glow:
    radial-gradient(1100px 560px at 82% -12%, var(--p-glow-a), transparent 62%),
    radial-gradient(880px 480px at 8% 112%, var(--p-glow-b), transparent 58%);
}
/* focus ring 的 RGB 三元組不是 color 值，light-dark() 用不上，只能分開寫 */
:root[data-theme="dark"] { --ui-ring: 56 189 248; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) { --ui-ring: 56 189 248; } }

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, "PingFang TC", "Noto Sans TC", "Segoe UI", system-ui, sans-serif;
  background: var(--ui-background);
  background-image: var(--grid-glow);
  background-attachment: fixed;
  color: var(--ui-foreground);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}

/* 只給螢幕閱讀器 */
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ── 頂列 ─────────────────────────────────────────────── */
.topbar {
  display: flex; align-items: center; gap: .75rem;
  padding: .7rem clamp(.85rem, 3vw, 1.5rem);
  border-bottom: 1px solid var(--ui-border);
  background: var(--p-topbar-bg);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  position: sticky; top: 0; z-index: 30;
}
.topbar .brand {
  display: flex; align-items: center; gap: .5rem;
  font-weight: 700; font-size: 1rem; letter-spacing: .2px; white-space: nowrap;
}
.topbar .brand .dot {
  width: 9px; height: 9px; border-radius: 50%; flex: none;
  background: var(--ui-primary); box-shadow: 0 0 10px var(--ui-primary);
}
.topbar .spacer { flex: 1; }
.topbar .meta { color: var(--ui-muted-foreground); font-size: .78rem; white-space: nowrap; }
.topbar .actions { display: flex; align-items: center; gap: .4rem; }

/* 搜尋盒 */
.searchbox { position: relative; flex: 1 1 auto; max-width: 380px; min-width: 0; }
.searchbox svg.mag {
  position: absolute; left: .6rem; top: 50%; transform: translateY(-50%);
  width: 15px; height: 15px; color: var(--ui-muted-foreground); pointer-events: none;
}
.searchbox input {
  width: 100%; height: 2.25rem;
  padding: 0 2.6rem 0 2rem;
  background: var(--ui-card); color: var(--ui-foreground);
  border: 1px solid var(--ui-input); border-radius: 999px;
  font: inherit; font-size: .86rem;
}
.searchbox input::placeholder { color: var(--ui-muted-foreground); }
.searchbox kbd {
  position: absolute; right: .5rem; top: 50%; transform: translateY(-50%);
  font: inherit; font-size: .68rem; line-height: 1;
  color: var(--ui-muted-foreground); background: var(--ui-muted);
  border: 1px solid var(--ui-border); border-radius: 5px; padding: .2rem .35rem;
  pointer-events: none;
}
.searchbox input:focus + kbd { opacity: 0; }

/* 圖示鈕（頂列） */
.iconbtn {
  height: 2.25rem; min-width: 2.25rem; padding: 0 .55rem;
  display: inline-flex; align-items: center; justify-content: center; gap: .35rem;
  background: transparent; color: var(--ui-foreground);
  border: 1px solid transparent; border-radius: var(--ui-radius-md);
  font: inherit; font-size: .8rem; cursor: pointer;
  transition: background-color .18s ease, color .18s ease, border-color .18s ease;
}
.iconbtn:hover { background: var(--ui-accent); color: var(--ui-accent-foreground); }
.iconbtn.is-outline { border-color: var(--ui-input); }
.iconbtn svg { width: 16px; height: 16px; }
.iconbtn .lbl { display: inline; }
.iconbtn.is-tiny { height: 1.7rem; min-width: 0; padding: 0 .5rem; font-size: .72rem; color: var(--ui-muted-foreground); }

/* ── 容器 ─────────────────────────────────────────────── */
.wrap { max-width: var(--p-wrap-max); margin: 0 auto; padding: 1.5rem clamp(.85rem, 3vw, 1.5rem) 4rem; }

/* ── 常用列 ───────────────────────────────────────────── */
.favs { display: none; margin-bottom: 1.4rem; }
/* :not([hidden]) 是必要的：作者端的 display 會蓋掉 [hidden] 的 UA 樣式，
   少了它，搜尋時用 JS 設 favs.hidden = true 完全沒有作用。 */
.favs.on:not([hidden]) { display: block; }
.favs-head { display: flex; align-items: center; gap: .5rem; margin-bottom: .55rem; }
.favs-head h2 { margin: 0; font-size: .82rem; font-weight: 600; color: var(--ui-muted-foreground); letter-spacing: .5px; }
.favs-head .line { flex: 1; height: 1px; background: var(--ui-border); }
.favs-list { display: flex; flex-wrap: wrap; gap: .45rem; }
.fav {
  display: inline-flex; align-items: center; gap: .4rem;
  height: 2.1rem; padding: 0 .75rem 0 .55rem;
  background: var(--ui-card); border: 1px solid var(--ui-border);
  border-radius: 999px; text-decoration: none; color: var(--ui-foreground);
  font-size: .82rem; white-space: nowrap; cursor: pointer;
  transition: border-color .18s ease, box-shadow .18s ease, color .18s ease;
}
.fav:hover { border-color: var(--ui-primary); box-shadow: 0 4px 14px -8px var(--p-hover-ring); }
.fav .fav-ico { font-size: 1rem; line-height: 1; }
.fav .led { width: 7px; height: 7px; border-radius: 50%; background: var(--p-led-idle); flex: none; }
.fav .led.up    { background: var(--p-led-up); }
.fav .led.down  { background: var(--p-led-down); }
.fav .led.local { background: var(--p-led-local); }
.fav .led.gone  { background: var(--ui-brand); }
.fav .led.paused { background: var(--p-led-idle); }

/* ── 區段標題 ─────────────────────────────────────────── */
.section { margin-bottom: 1.9rem; }
.section[hidden] { display: none; }
.section-head { display: flex; align-items: center; gap: .55rem; margin: 0 0 .8rem; }
.section-head h2 { margin: 0; font-size: 1rem; font-weight: 650; }
.section-head .count {
  font-size: .72rem; color: var(--ui-muted-foreground);
  background: var(--ui-muted); padding: .1rem .5rem; border-radius: 999px;
}
.section-head .line { flex: 1; height: 1px; background: var(--ui-border); }

/* ── 卡片牆 ───────────────────────────────────────────── */
.grid { display: grid; gap: var(--p-gap); grid-template-columns: repeat(auto-fill, minmax(272px, 1fr)); }

.tile {
  position: relative;
  display: flex; flex-direction: column; gap: .5rem;
  padding: .95rem 1rem .8rem;
  border: 1px solid var(--ui-border);
  border-radius: var(--ui-radius-lg);
  background: linear-gradient(160deg, var(--ui-card), var(--p-card-to));
  transition: transform .18s ease, border-color .18s ease, box-shadow .18s ease, opacity .18s ease;
}
.tile:hover, .tile:focus-within {
  transform: translateY(-2px);
  border-color: var(--ui-primary);
  box-shadow: 0 14px 32px -16px var(--p-hover-ring);
}
.tile[hidden] { display: none; }
/* 離線卡片：改用底色降階 + 圖示淡出來降權重。
   不用整張 opacity——那會把說明文字的對比一起拉到 AA 以下（實測 .62 只剩約 3.0:1）。 */
.tile.is-down { background: var(--ui-muted); }
.tile.is-down .tile-ico { opacity: .45; }
.tile.is-down .tile-name { color: var(--ui-muted-foreground); }
.tile.is-down:hover .tile-name, .tile.is-down:focus-within .tile-name { color: var(--ui-primary); }
/* 已停用：跟離線一樣降階，但用中性灰而不是紅——這是「刻意關掉的」，不是「壞掉的」 */
.tile.is-paused { background: var(--ui-muted); }
.tile.is-paused .tile-ico { opacity: .4; filter: grayscale(1); }
.tile.is-paused .tile-name { color: var(--ui-muted-foreground); }
.tile.is-paused:hover .tile-name, .tile.is-paused:focus-within .tile-name { color: var(--ui-primary); }

.tile-head { display: flex; align-items: flex-start; gap: .6rem; }
.tile-ico {
  width: 36px; height: 36px; flex: none;
  display: grid; place-items: center;
  font-size: 1.2rem; line-height: 1; border-radius: 10px;
  background: var(--ui-muted); border: 1px solid var(--ui-border);
}
.tile-name {
  flex: 1; min-width: 0;
  font-weight: 650; font-size: .98rem; line-height: 1.3;
  color: var(--ui-foreground); text-decoration: none;
  cursor: pointer;
}
/* 整張卡可點：用 stretched link，HTML 仍是合法的（工具鈕不巢在 <a> 裡） */
.tile-name::after { content: ''; position: absolute; inset: 0; border-radius: inherit; }
.tile-name:hover { color: var(--ui-primary); }
.tile-name:focus-visible { outline: none; }
.tile:focus-within { outline: 2px solid rgba(var(--ui-ring), .55); outline-offset: 2px; }

.tile-desc {
  margin: 0; font-size: .8rem; line-height: 1.5;
  color: var(--ui-muted-foreground); flex: 1;
}

/* 狀態燈 */
.status {
  display: inline-flex; align-items: center; gap: .3rem; flex: none;
  font-size: .68rem; color: var(--ui-muted-foreground); white-space: nowrap;
  padding-top: .18rem;
}
.status .led { width: 8px; height: 8px; border-radius: 50%; background: var(--p-led-idle); flex: none; }
.status.up    .led { background: var(--p-led-up);   box-shadow: 0 0 7px var(--p-led-up); }
.status.down  .led { background: var(--p-led-down); box-shadow: 0 0 7px var(--p-led-down); }
.status.local .led { background: var(--p-led-local); box-shadow: 0 0 7px var(--p-led-local); }
.status.gone  .led { background: var(--ui-brand); box-shadow: 0 0 7px var(--ui-brand); }
.status.paused .led { background: var(--p-led-idle); box-shadow: none; }
.status.checking .led { background: var(--p-led-wait); animation: pulse 1s infinite; }
.status.down .txt  { color: var(--p-down-text); }
.status.local .txt { color: var(--p-led-local); }
.status.gone  .txt { color: var(--ui-brand); }
.status.paused .txt { color: var(--ui-muted-foreground); }
@keyframes pulse { 50% { opacity: .3; } }

/* 技術標籤（最多 3 個 + 計數） */
.tile-tags { display: flex; flex-wrap: wrap; gap: .28rem; }
.tag {
  font-size: .68rem; line-height: 1.5; color: var(--ui-secondary-foreground);
  background: var(--ui-secondary); border: 1px solid var(--ui-border);
  border-radius: 999px; padding: .05rem .48rem; white-space: nowrap;
}
.tag.more { color: var(--ui-muted-foreground); background: transparent; }

/* 工具列：預設淡出，hover 或鍵盤 focus 才浮現 */
.tile-tools {
  position: relative; z-index: 1;
  display: flex; align-items: center; gap: .35rem;
  margin-top: .1rem; min-height: 1.7rem;
  opacity: 0; transition: opacity .18s ease;
}
.tile:hover .tile-tools, .tile:focus-within .tile-tools, .tile.tools-on .tile-tools { opacity: 1; }
@media (hover: none) { .tile-tools { opacity: 1; } }  /* 觸控裝置沒有 hover，一律顯示 */

.tool {
  display: inline-flex; align-items: center; gap: .25rem;
  font-size: .7rem; line-height: 1; font-family: inherit;
  color: var(--ui-muted-foreground);
  background: transparent; border: 1px solid var(--ui-border);
  border-radius: 7px; padding: .28rem .5rem;
  text-decoration: none; cursor: pointer; white-space: nowrap;
  transition: color .18s ease, border-color .18s ease;
}
.tool:hover { color: var(--ui-primary); border-color: var(--ui-primary); }
.tool.is-public { color: var(--ui-brand); border-color: var(--ui-brand); }
.tool.is-public:hover { color: var(--ui-brand-hover); border-color: var(--ui-brand-hover); }
.tool.note { color: var(--ui-brand); border-color: transparent; cursor: help; padding-left: .15rem; }
.tool .chev { transition: transform .18s ease; }
.tool.on { color: var(--ui-primary); border-color: var(--ui-primary); }
.tool.on .chev { transform: rotate(180deg); }
.tile-tools .spacer { flex: 1; }

/* 摘要面板 */
.tile-sum {
  position: relative; z-index: 1;
  margin-top: .1rem; padding-top: .55rem;
  border-top: 1px dashed var(--ui-border);
  font-size: .76rem; line-height: 1.6; color: var(--ui-muted-foreground);
}
.tile-sum[hidden] { display: none; }
.tile-sum .alltags { display: flex; flex-wrap: wrap; gap: .28rem; margin-top: .5rem; }

/* ── 列表檢視 ─────────────────────────────────────────── */
.grid.view-list { display: flex; flex-direction: column; gap: .3rem; }
.view-list .tile {
  flex-direction: row; align-items: center; gap: .75rem;
  padding: .5rem .75rem; border-radius: var(--ui-radius-md); min-height: 2.9rem;
}
.view-list .tile:hover, .view-list .tile:focus-within { transform: none; }
.view-list .tile-head { flex: 0 0 auto; align-items: center; gap: .55rem; min-width: 0; }
.view-list .tile-ico { width: 26px; height: 26px; font-size: .95rem; border-radius: 7px; }
.view-list .tile-name { flex: 0 0 auto; font-size: .88rem; }
.view-list .tile-head .status { order: -1; padding-top: 0; }
.view-list .tile-head .status .txt { display: none; }
.view-list .tile-desc {
  flex: 1; min-width: 0; font-size: .76rem;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.view-list .tile-tags { display: none; }
.view-list .tile-tools { margin-top: 0; min-height: 0; flex: none; }
.view-list .tile-sum { flex-basis: 100%; margin-top: 0; }
@media (max-width: 720px) {
  .view-list .tile-desc { display: none; }
}

/* ── 登入頁 ───────────────────────────────────────────── */
.login-shell { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; }
.login-card {
  width: 100%; max-width: 380px;
  border: 1px solid var(--ui-border); border-radius: var(--ui-radius-xl);
  background: linear-gradient(160deg, var(--ui-card), var(--p-card-to));
  padding: 2rem 1.75rem; box-shadow: var(--ui-shadow-lg);
}
.login-card h1 { margin: 0 0 .35rem; font-size: 1.4rem; }
.login-card p { margin: 0 0 1.4rem; color: var(--ui-muted-foreground); font-size: .85rem; }
.login-card .field { margin-bottom: 1rem; }
.login-card .err { color: var(--p-danger); font-size: .8rem; min-height: 1.1rem; margin-top: .4rem; }

/* ── admin 表格 ───────────────────────────────────────── */
.admin-toolbar { display: flex; gap: .6rem; align-items: center; margin: 1rem 0; flex-wrap: wrap; }
.admin-toolbar .spacer { flex: 1; }
.tbl-wrap { overflow-x: auto; border: 1px solid var(--ui-border); border-radius: var(--ui-radius-lg); }
table.sys { width: 100%; border-collapse: collapse; font-size: .85rem; min-width: 760px; }
table.sys th, table.sys td { padding: .55rem .6rem; text-align: left; border-bottom: 1px solid var(--ui-border); }
table.sys th { background: var(--ui-muted); color: var(--ui-muted-foreground); font-weight: 600; position: sticky; top: 0; }
table.sys input, table.sys select {
  width: 100%; background: var(--ui-background); color: var(--ui-foreground);
  border: 1px solid var(--ui-input); border-radius: 7px; padding: .35rem .5rem; font: inherit; font-size: .82rem;
}
.admin-hint { font-size: .85rem; color: var(--ui-muted-foreground); }
.tool .ghico, .fav .ghico { vertical-align: -1px; }
table.sys .col-drag  { width: 34px; }
table.sys .col-st    { width: 70px; }
table.sys .col-pub   { text-align: center; }
table.sys .f-status  { font-size: .72rem; color: var(--ui-muted-foreground); }
table.sys .col-icon  { width: 56px; }
table.sys .col-cat   { width: 96px; }
table.sys .col-act   { width: 92px; white-space: nowrap; }
.drag-handle { cursor: grab; color: var(--ui-muted-foreground); user-select: none; }
tr.dragging { opacity: .4; }

.toast {
  position: fixed; bottom: 1.5rem; left: 50%; transform: translateX(-50%);
  background: var(--ui-card); border: 1px solid var(--ui-primary);
  color: var(--ui-foreground); padding: .7rem 1.1rem; border-radius: 10px;
  box-shadow: var(--ui-shadow-lg); font-size: .85rem; z-index: 50;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }

.empty { text-align: center; color: var(--ui-muted-foreground); padding: 3rem 1rem; }
a.plain { color: var(--ui-primary); text-decoration: none; }
a.plain:hover { text-decoration: underline; }

/* ── 手機（<720px）：頂列拆兩行、觸控目標放大 ───────────── */
@media (max-width: 720px) {
  /* 手機三行：品牌＋動作鈕同一行（鈕靠右）、搜尋整行、狀態摘要最後一行 */
  .topbar { flex-wrap: wrap; row-gap: .5rem; padding-bottom: .55rem; }
  .topbar .spacer { display: none; }
  .topbar .brand { order: 1; }
  .topbar .actions { order: 2; margin-left: auto; }
  .searchbox { order: 3; flex-basis: 100%; max-width: none; }
  .topbar .meta { order: 4; flex-basis: 100%; }
  .searchbox kbd { display: none; }
  .topbar .meta { font-size: .72rem; }
  .topbar .actions { gap: .15rem; }
  .iconbtn { height: 2.5rem; min-width: 2.5rem; padding: 0 .45rem; }
  .iconbtn .lbl { display: none; }
  .grid { grid-template-columns: 1fr; }
  .wrap { padding-top: 1.15rem; }
}
