/* ──────────────────────────────────────────────────────────────────────────────
 * AllocLab UI v5 — Shared Components
 * 跨頁共用：icon-btn / card / pill / chip / qbar / sticky-header
 * 個別 page CSS 放 50-pages/<page>.css
 * ────────────────────────────────────────────────────────────────────────────── */

/* ---------- icon-btn (44×44 tap target) ---------- */
.al-icon-btn {
  width: 44px;
  height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-inner);
  color: var(--al-ink);
  position: relative;
  transition: background var(--t-fast) var(--ease);
}
.al-icon-btn:hover { background: var(--al-surface); }
.al-icon-btn svg { width: 20px; height: 20px; }
.al-icon-btn .al-dot {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 7px;
  height: 7px;
  background: var(--al-accent);
  border-radius: 50%;
  border: 1.5px solid var(--al-bg);
}

/* ---------- card base ---------- */
.al-card {
  background: var(--al-surface);
  border-radius: var(--radius-card);
  padding: var(--sp-4);
}
.al-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--sp-3);
  gap: var(--sp-3);
}
.al-card-head h3 {
  margin: 0;
  font-size: var(--fs-lg);
  font-weight: 600;
  color: var(--al-ink);
}
.al-card-head .sub {
  font-size: var(--fs-sm);
  color: var(--al-ink-muted);
  font-variant-numeric: tabular-nums;
}
.al-card-cta {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  margin-top: var(--sp-3);
  padding: var(--sp-3);
  border-top: 1px solid var(--al-border-soft);
  font-size: var(--fs-base);
  color: var(--al-accent);
  font-weight: 500;
  min-height: 44px;
}
.al-card-cta svg { width: 16px; height: 16px; }

/* ---------- pill ---------- */
.al-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  border-radius: var(--radius-pill);
  font-size: var(--fs-xs);
  font-weight: 600;
  letter-spacing: 0.02em;
  line-height: 1.4;
}

/* Stance pill — a11y plan §A11y #5：禁借 up/dn token、改 accent/ink-2/ink-muted */
.al-pill-stance.pos {
  background: var(--al-active-bg);
  color: var(--al-accent);
}
.al-pill-stance.mixed {
  background: var(--al-surface-2);
  color: var(--al-ink-2);
}
.al-pill-stance.neg {
  background: var(--al-surface-2);
  color: var(--al-ink-muted);
}
.al-pill-stance.insufficient {
  background: var(--al-surface-2);
  color: var(--al-ink-muted);
  font-style: italic;
}

/* Filter pill — a11y plan §A11y #6：min-height 44 */
.al-pill-filter {
  padding: 8px 14px;
  border-radius: var(--radius-pill);
  font-size: var(--fs-base);
  background: transparent;
  color: var(--al-ink-2);
  border: 1px solid var(--al-border-md);
  min-height: 44px;
  display: inline-flex;
  align-items: center;
}
.al-pill-filter[aria-pressed="true"] {
  background: var(--al-accent);
  color: #fff;
  border-color: var(--al-accent);
}

/* Tier pill */
.al-pill-tier {
  display: inline-block;
  padding: 3px 10px;
  border-radius: var(--radius-pill);
  background: rgba(0, 0, 0, 0.3);
  color: var(--al-ink-2);
  font-size: var(--fs-xs);
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

/* ---------- chip ---------- */
.al-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  border-radius: var(--radius-chip);
  background: var(--al-surface-2);
  color: var(--al-ink-2);
  font-size: var(--fs-sm);
  font-weight: 500;
}

/* ---------- qbar (quantile bar，a11y role=meter) ---------- */
.al-qbar {
  display: inline-block;
  position: relative;
  width: 56px;
  height: 4px;
  background: var(--al-bg);
  border-radius: 2px;
  overflow: hidden;
  vertical-align: middle;
}
.al-qbar > i {
  position: absolute;
  inset: 0 auto 0 0;
  background: var(--al-accent);
  border-radius: 2px;
}

/* ---------- sticky header (跨頁) ---------- */
.al-sticky-hdr {
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  background: var(--al-bg);
  border-bottom: 1px solid var(--al-border);
}

/* ---------- Polaris brand ---------- */
.al-brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--al-ink);
  min-height: 44px;
}
.al-brand__mark {
  width: 22px;
  height: 22px;
  color: var(--al-accent);
  flex: 0 0 auto;
}
.al-brand__mark svg { width: 100%; height: 100%; display: block; overflow: visible; }
.al-brand__word {
  font-size: var(--fs-lg);
  font-weight: 600;
  letter-spacing: 0.01em;
  color: var(--al-ink);
}
.al-brand__word em {
  font-style: normal;
  color: var(--al-accent);
}

/* ---------- common arrow ---------- */
.al-arrow {
  display: inline-flex;
  width: 16px;
  height: 16px;
}
.al-arrow svg { width: 100%; height: 100%; }

/* ============================================================================
 * Phase 1 additions — Stock-detail-driven cross-page components
 * ============================================================================ */

/* ---------- al-tabs-swipe ---------- */
.al-tabs {
  position: sticky;
  z-index: var(--z-sticky);
  display: flex;
  gap: 4px;
  overflow-x: auto;
  overflow-y: hidden;
  background: var(--al-bg);
  border-bottom: 1px solid var(--al-border);
  padding: 0 var(--pad-x);
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
  scroll-snap-type: x proximity;
}
.al-tabs::-webkit-scrollbar { display: none; }
.al-tab {
  flex: 0 0 auto;
  min-height: var(--tabs-h);
  padding: 0 16px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  position: relative;
  font-size: var(--fs-md);
  color: var(--al-ink-muted);
  font-weight: 500;
  white-space: nowrap;
  scroll-snap-align: start;
  transition: color var(--t-fast) var(--ease);
}
.al-tab[aria-selected="true"] {
  color: var(--al-accent);
  font-weight: 600;
}
.al-tab[aria-selected="true"]::after {
  content: "";
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 0;
  height: 2px;
  background: var(--al-accent);
  border-radius: 2px;
}

.al-panels {
  display: flex;
  align-items: flex-start;             /* 各 panel 保留自然高度，不被最高拉齊 */
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x mandatory;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  transition: height 220ms var(--ease); /* tab 切換時高度動畫 */
}
.al-panels::-webkit-scrollbar { display: none; }
.al-panels > [role="tabpanel"] {
  flex: 0 0 100%;
  width: 100%;
  min-width: 0;
  align-self: flex-start;
  scroll-snap-align: start;
  scroll-snap-stop: always;
  padding: var(--sp-4) var(--pad-x);
  overflow-y: visible;
}
.al-panels > [role="tabpanel"]:focus-visible {
  outline: 2px solid var(--al-accent);
  outline-offset: -2px;
}
.al-section-heading {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- al-ai-card (n-layer pattern) ---------- */
.al-ai-card__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
  padding-bottom: var(--sp-3);
  margin-bottom: var(--sp-3);
  border-bottom: 1px solid var(--al-border-soft);
}
.al-ai-card__lbl {
  display: flex;
  align-items: center;
  gap: 8px;
}
.al-ai-card__title {
  font-size: var(--fs-md);
  font-weight: 600;
  color: var(--al-ink);
}
.al-ai-card__score {
  display: flex;
  align-items: baseline;
  gap: 4px;
  font-variant-numeric: tabular-nums;
}
.al-ai-card__score .n {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--al-ink);
}
.al-ai-card__score .t {
  font-size: var(--fs-xs);
  color: var(--al-ink-muted);
}
.al-layer {
  padding: var(--sp-3);
  background: var(--al-surface-2);
  border-radius: var(--radius-inner);
}
.al-layer + .al-layer { margin-top: 8px; }
.al-layer__head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}
.al-layer__num {
  width: 18px;
  height: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.68rem;
  font-weight: 600;
  color: var(--al-ink-muted);
  background: var(--al-bg);
  border-radius: var(--radius-chip);
  font-variant-numeric: tabular-nums;
}
.al-layer__title {
  font-size: var(--fs-base);
  font-weight: 600;
  color: var(--al-ink);
}
.al-layer__body {
  font-size: var(--fs-base);
  color: var(--al-ink-2);
  line-height: var(--lh-relaxed);
  padding-left: 26px;
}
.al-layer__metrics {
  display: flex;
  gap: var(--sp-3);
  flex-wrap: wrap;
  padding-left: 26px;
  margin-top: 4px;
}
.al-layer__metrics .m {
  display: inline-flex;
  align-items: baseline;
  gap: 4px;
  font-size: var(--fs-sm);
  font-variant-numeric: tabular-nums;
}
.al-layer__metrics .m .k { color: var(--al-ink-muted); }
.al-layer__metrics .m .v {
  color: var(--al-ink);
  font-weight: 600;
}

/* ---------- al-metric-grid (2-col + qbar) ---------- */
.al-metric-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--sp-3);
}
.al-metric {
  padding: var(--sp-3);
  background: var(--al-surface-2);
  border-radius: var(--radius-inner);
}
.al-metric .k {
  font-size: var(--fs-sm);
  color: var(--al-ink-muted);
  margin-bottom: 4px;
}
.al-metric .v {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--al-ink);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
}
.al-metric .v .u {
  font-size: 0.7rem;
  color: var(--al-ink-muted);
  margin-left: 2px;
  font-weight: 500;
}
.al-metric .al-qbar {
  width: 100%;
  height: 4px;
  margin-top: 8px;
}
.al-metric .qmeta {
  font-size: 0.68rem;
  color: var(--al-ink-muted);
  margin-top: 4px;
  font-variant-numeric: tabular-nums;
  display: flex;
  justify-content: space-between;
}

/* ---------- al-peer-table ---------- */
.al-peer-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--fs-base);
  font-variant-numeric: tabular-nums;
}
.al-peer-table th,
.al-peer-table td {
  padding: 8px 4px;
  text-align: right;
  border-bottom: 1px solid var(--al-border-soft);
}
.al-peer-table th:first-child,
.al-peer-table td:first-child { text-align: left; }
.al-peer-table thead th {
  font-size: var(--fs-sm);
  color: var(--al-ink-muted);
  font-weight: 500;
}
.al-peer-table tbody td { color: var(--al-ink); }
.al-peer-table tbody td:first-child {
  color: var(--al-ink);
  font-weight: 500;
}
.al-peer-table tbody tr.is-self td:first-child { color: var(--al-accent); }
.al-peer-table tbody tr:last-child td { border-bottom: 0; }

/* ---------- al-peer-scroll (horizontal) ---------- */
.al-peer-scroll {
  display: flex;
  gap: 10px;
  overflow-x: auto;
  margin: 0 calc(-1 * var(--pad-x));
  padding: 0 var(--pad-x);
  scrollbar-width: none;
}
.al-peer-scroll::-webkit-scrollbar { display: none; }
.al-peer-card {
  flex: 0 0 132px;
  padding: var(--sp-3);
  background: var(--al-surface-2);
  border-radius: var(--radius-inner);
}
.al-peer-card .code {
  font-family: var(--ff-mono);
  font-size: var(--fs-base);
  font-weight: 600;
  color: var(--al-ink-muted);
  font-variant-numeric: tabular-nums;
}
.al-peer-card .name {
  font-size: var(--fs-md);
  color: var(--al-ink);
  margin: 2px 0 8px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.al-peer-card .p {
  font-size: var(--fs-md);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.al-peer-card .p.up { color: var(--al-up); }
.al-peer-card .p.dn { color: var(--al-dn); }
.al-peer-card .c {
  font-size: var(--fs-sm);
  margin-top: 2px;
  font-variant-numeric: tabular-nums;
}
.al-peer-card .c.up { color: var(--al-up); }
.al-peer-card .c.dn { color: var(--al-dn); }

/* ---------- al-news-list ---------- */
.al-news-list { padding: 0; margin: 0; list-style: none; }
.al-news-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px var(--sp-3);
  padding: var(--sp-3) 0;
  border-bottom: 1px solid var(--al-border-soft);
}
.al-news-item:first-child { padding-top: 0; }
.al-news-item:last-child { border-bottom: 0; padding-bottom: 0; }
.al-news-item .news-meta {
  font-size: var(--fs-sm);
  color: var(--al-ink-muted);
  font-variant-numeric: tabular-nums;
}
.al-news-item .news-title {
  grid-column: 1 / -1;
  font-size: var(--fs-base);
  font-weight: 500;
  color: var(--al-ink);
  line-height: var(--lh-base);
}

/* ---------- al-action-bar (sticky bottom, before app-nav) ---------- */
.al-action-bar {
  position: fixed;
  inset: auto 0 var(--app-nav-h) 0;
  height: var(--action-h);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  background: var(--al-surface);
  border-top: 1px solid var(--al-border);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: var(--z-sticky);
}
.al-action-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--al-ink-2);
  font-size: 0.65rem;       /* tighter (was fs-xs 0.6875rem) */
  min-height: 44px;          /* a11y tap target preserved via touch hit area */
  transition: color var(--t-fast) var(--ease);
  position: relative;
}
.al-action-btn svg { width: 18px; height: 18px; }
/* expand hit area to 44px without enlarging visible bar */
.al-action-btn::before {
  content: "";
  position: absolute;
  inset: -8px 0;
  pointer-events: auto;
}
.al-action-btn:hover { color: var(--al-ink); }
.al-action-btn.is-active { color: var(--al-accent); }

/* Auto-reserve space below content when an action-bar is on the page (stock detail).
 * Uses :has() so pages don't need to set a body class.
 * margin-bottom not padding：避免 page JS set height 後被 border-box 吃掉。 */
body:has(.al-action-bar) .shell main {
  margin-bottom: calc(var(--action-h) + var(--app-nav-h) + var(--sp-4));
}

@media (min-width: 768px) {
  .al-action-bar {
    inset: auto 0 0 80px;
  }
  body:has(.al-action-bar) .shell main {
    margin-bottom: calc(var(--action-h) + var(--sp-4));
  }
}
