/*
 * สไตล์เฉพาะหน้า "ภาพรวม" (dashboard): การ์ดตัวเลข กราฟ ตารางข้อมูลของกราฟ แท่งแพลตฟอร์ม โพสต์เด่น บัญชีเด่น
 * แปลงจากคลาส Tailwind ของ components/dashboard/* และ components/charts/* เดิม — สีทุกตัวอ้าง CSS variable จาก app.css (ห้าม hardcode สี)
 * ค่าที่คำนวณจากข้อมูล (ความกว้างของแท่ง) ตั้งผ่าน CSSOM โดย js/charts/bars.js ไม่ใช้ attribute style ในมาร์กอัป
 */

/* ---- เนื้อหาจางลงระหว่างโหลดใหม่ (ข้อมูลเดิมยังอยู่): .is-refreshing ของ app.css ให้ opacity — ที่นี่เพิ่มให้การกลับมาเต็มก็นุ่มนวล ---- */
.dash-fade { transition: opacity 0.2s; }
.empty-state.chart-empty { padding-block: 2.5rem; }
.empty-state.empty-compact { padding-block: 2rem; }

/* ---- มาร์กสีของ series (legend / ทูลทิป / ส่วนของแท่ง) ---- */
.fill-primary { background: var(--chart-main); }
.fill-gold { background: var(--chart-previous); }
.fill-positive { background: var(--positive); }
.fill-neutral { background: var(--neutral); }
.fill-negative { background: var(--negative); }
.legend-item { display: inline-flex; align-items: center; gap: 0.5rem; font-size: 13px; }
.legend-line { width: 1rem; height: 2px; flex-shrink: 0; border-radius: 9999px; }
.legend-swatch { width: 0.75rem; height: 0.75rem; flex-shrink: 0; border-radius: 3px; }
.legend-swatch-sm { width: 0.625rem; height: 0.625rem; flex-shrink: 0; border-radius: 3px; }
.tip-line { width: 0.875rem; height: 2px; flex-shrink: 0; border-radius: 9999px; }
.tip-swatch { width: 0.625rem; height: 0.625rem; flex-shrink: 0; border-radius: 3px; }

/* ---- การ์ดตัวเลข (StatTile) ---- */
.stat-value { font-size: 36px; font-weight: 700; line-height: 1; letter-spacing: -0.03em; }
/* KPI หลัก (จำนวน Mentions): ตัวเลขไล่สี + sparkline ไล่สีประจำแบรนด์ */
.stat-tile-hero .stat-value { width: fit-content; padding-bottom: 0.1em; font-size: 48px; background: var(--kpi-gradient); -webkit-background-clip: text; background-clip: text; color: transparent; }
.spark-line-sun { fill: none; stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; }
.spark-stop-1 { stop-color: var(--spark-1); }
.spark-stop-2 { stop-color: var(--spark-2); }
.spark-stop-3 { stop-color: var(--spark-3); }
.kpi-unit { padding-bottom: 0.125rem; }
.sparkline { width: 100%; max-width: 10rem; height: 2.75rem; }
.spark-line { fill: none; stroke: var(--chart-axis); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; opacity: 0.55; }
.spark-dot { fill: var(--chart-main); stroke: var(--card); stroke-width: 2; }
/* แท่งบาง 8px ของสัดส่วนความรู้สึก */
/* % ลบ ข้างคะแนนความรู้สึกสุทธิ + บรรทัดบอกฐานของตัวเลข (นับจากอะไร AI วิเคราะห์ไปแล้วเท่าไร) */
.kpi-negative { padding-bottom: 0.25rem; font-size: 0.8125rem; line-height: 1.3846; }
.kpi-negative-value { font-weight: 600; color: var(--negative-ink); }
.sentiment-basis { margin-bottom: 0.75rem; font-size: 0.75rem; line-height: 1.3333; color: var(--muted-foreground); }
.kpi-sentiment-bar { display: flex; width: 100%; height: 0.5rem; gap: 2px; overflow: hidden; border-radius: 9999px; background: var(--muted); }
.kpi-sentiment-seg { height: 100%; }
.kpi-sentiment-seg:first-child { border-top-left-radius: 9999px; border-bottom-left-radius: 9999px; }
.kpi-sentiment-seg:last-child { border-top-right-radius: 9999px; border-bottom-right-radius: 9999px; }

/* ---- Delta: ลูกศร + เครื่องหมาย + % เสมอ (สีบอกโทนเท่านั้น ไม่ใช่ตัวสื่อความหมายเดียว) ---- */
.delta { display: inline-flex; align-items: center; gap: 0.25rem; border-radius: 9999px; padding: 0.125rem 0.5rem; font-size: 0.75rem; line-height: 1.3333; font-weight: 600; }
.delta-na { background: var(--muted); color: var(--muted-foreground); font-weight: 500; }
.delta-primary { background: var(--primary-soft); color: var(--secondary-foreground); }
.delta-positive { background: var(--positive-soft); color: var(--positive-ink); }
.delta-negative { background: var(--negative-soft); color: var(--negative-ink); }
.delta-lg { padding: 0.375rem 0.75rem; font-size: 1.25rem; line-height: 1.4; }

/* ---- กรอบกราฟ (ChartCard) ---- */
.chart-body { transition: opacity 0.2s; }
.chart-box { position: relative; width: 100%; min-width: 0; }
.chart-box-line { height: 300px; }
.chart-box-bars { height: 220px; }
.chart-unavailable { display: flex; height: 100%; align-items: center; justify-content: center; font-size: 0.875rem; color: var(--muted-foreground); }

/* ตารางข้อมูลของกราฟ (คู่เทียบที่เข้าถึงได้): เลื่อนในกรอบ หัวตารางติดบน */
.chart-table-wrap { max-height: 20rem; overflow: auto; border-radius: var(--radius-md); border: 1px solid var(--border); }
.chart-table { width: 100%; font-size: 0.875rem; line-height: 1.4286; }
.chart-table th { position: sticky; top: 0; background: var(--muted); padding: 0.5rem 0.75rem; font-size: 0.75rem; line-height: 1.3333; font-weight: 500; color: var(--muted-foreground); }
.chart-table td { padding: 0.375rem 0.75rem; }
.chart-table tbody tr { border-top: 1px solid var(--border); }

/* ทูลทิปของกราฟ (HTML ภายนอก) */
.chart-tip { position: absolute; z-index: 10; width: max-content; max-width: calc(100% - 8px); pointer-events: none; }
.chart-tip-box { min-width: 11rem; border-radius: var(--radius-lg); border: 1px solid var(--border); background: var(--card); padding: 0.625rem 0.75rem; font-size: 0.75rem; line-height: 1.3333; color: var(--card-foreground); box-shadow: var(--shadow-pop); }
.chart-tip-title { margin-bottom: 0.375rem; font-weight: 500; color: var(--muted-foreground); }
.chart-tip-rows { display: grid; gap: 0.25rem; }
.chart-tip-row { display: flex; align-items: center; gap: 0.5rem; }
.chart-tip-value { margin-left: auto; padding-left: 1rem; font-size: 0.875rem; line-height: 1.4286; font-weight: 600; color: var(--foreground); }
.chart-tip-total { display: flex; margin-top: 0.125rem; border-top: 1px solid var(--border); padding-top: 0.375rem; }

/* ---- แท่งแบ่งส่วนของสัดส่วนความรู้สึก (SentimentBar, 28px) ---- */
.sentiment-bar { display: flex; width: 100%; height: 1.75rem; gap: 2px; }
.sentiment-seg { display: flex; min-width: 0; align-items: center; gap: 0.25rem; border-radius: 4px; padding-inline: 0.5rem; font-size: 0.75rem; line-height: 1.3333; font-weight: 600; transition: filter 0.15s; }
.sentiment-seg:hover { filter: brightness(1.1); }
.sentiment-seg.fill-positive { color: var(--on-positive); }
.sentiment-seg.fill-neutral { color: var(--on-neutral); }
.sentiment-seg.fill-negative { color: var(--on-negative); }

/* ---- แท่งแนวนอนของแต่ละแพลตฟอร์ม (PlatformBars) ---- */
.platform-bars { display: flex; flex-direction: column; justify-content: space-around; gap: 0.875rem; }
@media (min-width: 1280px) {
  .platform-bars { min-height: 300px; }
}
.platform-row { position: relative; display: grid; grid-template-columns: 24px minmax(0, 1fr); align-items: center; gap: 0.75rem; border-radius: var(--radius-md); outline: none; }
.platform-row:focus-visible { box-shadow: 0 0 0 2px var(--ring); }
.platform-track { display: flex; align-items: center; gap: 0.5rem; border-left: 1px solid var(--chart-grid); padding-block: 0.375rem; }
.platform-bar { width: calc((100% - 120px) * var(--ratio, 0)); min-width: 3px; height: 0.875rem; border-radius: 0 4px 4px 0; background: var(--chart-main); transition: filter 0.2s, width 0.2s; }
.platform-row:hover .platform-bar,
.platform-row:focus .platform-bar { filter: brightness(1.25); }
.platform-count { flex-shrink: 0; font-size: 0.875rem; line-height: 1.4286; font-weight: 600; }
.platform-share { flex-shrink: 0; font-size: 0.75rem; line-height: 1.3333; color: var(--muted-foreground); }
.platform-main { min-width: 0; }
/* แถบบางใต้แท่ง: ความรู้สึกบนแพลตฟอร์มนั้น (กว้างเท่ารางของแท่ง) + "ลบ N%" */
.platform-mood { display: flex; align-items: center; gap: 0.5rem; border-left: 1px solid var(--chart-grid); padding-bottom: 0.25rem; }
.platform-mood-bar { display: flex; width: calc(100% - 120px); height: 0.25rem; gap: 2px; overflow: hidden; border-radius: 0 9999px 9999px 0; background: var(--muted); }
.platform-mood-seg { height: 100%; min-width: 2px; }
.platform-neg { flex-shrink: 0; font-size: 0.75rem; line-height: 1.3333; color: var(--muted-foreground); }
.platform-neg.is-negative { color: var(--negative-ink); font-weight: 600; }
.platform-tip { display: none; position: absolute; top: -2.25rem; left: 6rem; z-index: 10; pointer-events: none; white-space: nowrap; border-radius: var(--radius-lg); border: 1px solid var(--border); background: var(--card); padding: 0.375rem 0.75rem; font-size: 0.75rem; line-height: 1.3333; box-shadow: var(--shadow-pop); }
.platform-row:hover .platform-tip,
.platform-row:focus .platform-tip { display: block; }

/* ---- โพสต์ที่ engagement สูงสุด ---- */
.top-post { display: grid; grid-template-columns: auto auto minmax(0, 1fr); column-gap: 1rem; row-gap: 0.5rem; border-radius: var(--radius-lg); border: 1px solid var(--border); background: color-mix(in srgb, var(--muted) 50%, transparent); padding: 1rem; }
.top-post-rank { display: flex; width: 2rem; height: 2rem; align-items: center; justify-content: center; border-radius: 9999px; background: var(--primary-soft); color: var(--secondary-foreground); font-size: 0.875rem; line-height: 1.4286; font-weight: 600; }
.top-post-rank.is-first { background: var(--primary); color: var(--primary-foreground); }
.top-post-meta { margin-top: 0.625rem; display: flex; flex-wrap: wrap; align-items: center; column-gap: 1rem; row-gap: 0.375rem; font-size: 0.75rem; line-height: 1.3333; color: var(--muted-foreground); }
.top-post-link { margin-left: auto; display: inline-flex; align-items: center; gap: 0.25rem; font-weight: 500; color: var(--primary-ink); text-underline-offset: 4px; }
.top-post-link:hover { text-decoration: underline; }
/* ---- เจตนาของข้อความ: หนึ่งแถวต่อเจตนา (ชื่อ + ป้ายสิ่งที่ต้องทำ + จำนวน) แท่งแบ่งสีตามความรู้สึกอยู่ใต้ชื่อ ---- */
.intent-summary { margin-bottom: 0.875rem; font-size: 0.8125rem; line-height: 1.3846; color: var(--muted-foreground); }
.intent-bars { display: grid; gap: 0.875rem; }
.intent-row { display: grid; gap: 0.375rem; min-width: 0; }
.intent-row-head { display: flex; flex-wrap: wrap; align-items: center; column-gap: 0.5rem; row-gap: 0.25rem; min-width: 0; }
.intent-row-name { font-size: 0.875rem; font-weight: 500; }
.intent-row-count { margin-left: auto; font-size: 0.8125rem; white-space: nowrap; }
.intent-stack { display: flex; width: calc(100% * var(--ratio, 0)); min-width: 6px; height: 0.625rem; gap: 2px; overflow: hidden; border-radius: 4px; transition: width 0.2s; }
.intent-seg { height: 100%; min-width: 2px; }
.intent-note { margin-top: 0.875rem; font-size: 0.75rem; line-height: 1.3333; color: var(--muted-foreground); }
@media (prefers-reduced-motion: reduce) {
  .intent-stack { transition: none; }
}

/* ---- เสียงลบที่ต้องดู: การ์ดโพสต์แบบเดียวกับโพสต์เด่น + เหตุผลของ AI ---- */
.negative-post-rank { background: var(--negative-soft, var(--muted)); color: var(--negative-ink); }
.negative-reason { margin-top: 0.5rem; border-left: 2px solid var(--negative-ink); padding-left: 0.625rem; font-size: 0.8125rem; line-height: 1.3846; overflow-wrap: anywhere; }
.negative-reason.is-lexicon { border-left-color: var(--border); color: var(--muted-foreground); }
.negative-reason-label { font-weight: 600; }
.negative-post-source { margin-left: 0; }
.line-clamp-3 { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; line-clamp: 3; overflow: hidden; }

/* ---- บัญชีที่พูดถึงบ่อยที่สุด ---- */
.card-content-flush { padding-inline: 0; padding-bottom: 0.5rem; }
.table .col-platform { width: 3.5rem; padding-right: 0; }
.author-name { display: block; max-width: 180px; }

/* ---- คำอธิบายสัญลักษณ์เหนือกราฟ และหัวของการ์ดโพสต์เด่น ---- */
.chart-legend { display: flex; flex-wrap: wrap; column-gap: 1.25rem; row-gap: 0.25rem; }
.top-post-head { display: flex; flex-wrap: wrap; align-items: center; column-gap: 0.5rem; row-gap: 0.25rem; font-size: 13px; }

/* จอแคบ: ตัวเลือกช่วงวันที่ยาวกว่าจอ — เลื่อนในกรอบของมันเอง ไม่ดันทั้งแถวตัวกรองให้กว้างเกินจอจนช่องกรอง/ช่องค้นหาถูกตัดขอบขวา */
[data-slot='range'] > div { min-width: 0; max-width: 100%; }
[data-slot='range'] .segmented { max-width: 100%; overflow-x: auto; }

/* ---- การ์ด "ประเด็นที่ถูกพูดถึง": แท่งแนวนอนต่อประเด็น ยาวตามจำนวนโพสต์ แบ่งสีตามความรู้สึก ---- */
.topic-bars { display: grid; gap: 0.75rem; }
.topic-row { display: grid; grid-template-columns: minmax(0, 1fr); gap: 0.25rem; min-width: 0; }
.topic-row-head { display: flex; flex-wrap: wrap; align-items: baseline; column-gap: 0.5rem; min-width: 0; }
.topic-row-name { font-size: 0.875rem; font-weight: 500; overflow-wrap: anywhere; }
.topic-row-keyword { font-size: 0.75rem; color: var(--muted-foreground); }
.topic-row-neg { font-size: 0.75rem; color: var(--muted-foreground); }
.topic-row-neg.is-negative { color: var(--negative-ink); font-weight: 600; }
.topic-track { display: flex; align-items: center; gap: 0.5rem; }
.topic-stack { display: flex; width: calc((100% - 56px) * var(--ratio, 0)); min-width: 6px; height: 0.875rem; gap: 2px; overflow: hidden; border-radius: 4px; transition: width 0.2s; }
.topic-seg { height: 100%; min-width: 2px; }
.topic-count { font-size: 0.8125rem; font-weight: 600; }
.topic-trend { display: none; }
.topic-sparkline { width: 6rem; max-width: 6rem; height: 1.5rem; }
@media (min-width: 768px) {
  .topic-row { grid-template-columns: minmax(8rem, 16rem) minmax(0, 1fr) 6rem; align-items: center; gap: 1rem; }
  .topic-trend { display: block; }
}
@media (prefers-reduced-motion: reduce) {
  .topic-stack { transition: none; }
}

/* ---- Mentions ล่าสุด (ฟีดสด): js/pages/dashboard/live-feed.js ---- */
.feed-card { display: flex; height: 100%; flex-direction: column; }
.feed-card .card-content { flex: 1; min-height: 0; }
.feed-list { display: grid; }
.feed-item { display: grid; grid-template-columns: auto minmax(0, 1fr); column-gap: 0.75rem; border-bottom: 1px solid var(--border); padding: 0.75rem 0; }
.feed-item:first-child { padding-top: 0; }
.feed-item:last-child { border-bottom: 0; padding-bottom: 0; }
/* โพสต์ที่เพิ่งเข้ามา: เรืองแสงอุ่นสั้น ๆ แล้วจางหาย */
/* โพสต์ใหม่ "strike": เลื่อนเข้ามา แล้วมีแสงส้มวาบผ่านหนึ่งครั้ง */
.feed-item.is-new { animation: feed-in 0.2s var(--ease-out); background-image: var(--strike-flash); background-repeat: no-repeat; background-size: 60% 100%; background-position: 260% 0; }
.feed-item.is-new { animation: feed-in 0.2s var(--ease-out), feed-strike 0.7s var(--ease-out) 0.1s; }
@keyframes feed-in { from { opacity: 0; transform: translateX(-12px); } to { opacity: 1; transform: none; } }
@keyframes feed-strike { from { background-position: -160% 0; } to { background-position: 260% 0; } }
@media (prefers-reduced-motion: reduce) {
  .feed-item.is-new { background-image: none; }
}
.feed-head { display: flex; align-items: baseline; justify-content: space-between; gap: 0.5rem; font-size: 13px; }
.feed-time { flex-shrink: 0; font-size: 0.75rem; color: var(--muted-foreground); }
.feed-text { margin-top: 0.125rem; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2; overflow: hidden; font-size: 13px; line-height: 1.55; color: var(--muted-foreground); overflow-wrap: anywhere; }
.feed-meta { margin-top: 0.375rem; display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; }
.feed-link, .feed-all { border-radius: var(--radius-sm); font-size: 0.75rem; font-weight: 500; color: var(--primary-ink); text-underline-offset: 4px; }
.feed-link:hover, .feed-all:hover { text-decoration: underline; }
.feed-footer { border-top: 1px solid var(--border); padding: 0.75rem 1.25rem; }
.feed-all { display: inline-flex; align-items: center; gap: 0.25rem; }
.feed-stale { margin-bottom: 0.75rem; display: flex; flex-wrap: wrap; align-items: center; gap: 0.375rem; border-radius: var(--radius-md); background: var(--gold-soft); padding: 0.375rem 0.625rem; font-size: 0.75rem; color: var(--gold-foreground); }
.feed-stale .btn-link { font-size: 0.75rem; color: inherit; text-decoration: underline; }
.feed-error { border: 0; box-shadow: none; padding: 1.5rem 0.5rem; }
/* แถวที่มีการ์ดสองใบ: สูงเท่ากัน (ไม่เหลือช่องว่างใต้ใบที่เตี้ยกว่า) */
[data-slot='trend'] > .card, [data-slot='sentiment'] > .card, [data-slot='platforms'] > .card { height: 100%; }
/* กราฟแนวโน้มยืดเต็มความสูงของแถว (สูงเท่าฟีดข้าง ๆ) */
[data-slot='trend'] > .card { display: flex; flex-direction: column; }
[data-slot='trend'] .chart-body { display: flex; flex: 1; flex-direction: column; }
[data-slot='trend'] .chart-box-line { flex: 1; min-height: 300px; height: auto; }
