@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+Thai:wght@400;500;600;700;800&display=swap');

:root {
  --color-bg: #f4f5fa;
  --color-surface: #ffffff;
  --color-border: #e3e4ed;
  --color-text: #1c1c28;
  --color-muted: #656577;
  --color-primary: #4f46e5;
  --color-primary-dark: #3f37c9;
  --color-success: #15803d;
  --color-success-bg: #e8f8ee;
  --color-warning: #92600a;
  --color-warning-bg: #fff4de;
  --color-danger: #b3261e;
  --color-danger-bg: #fdecea;
  --radius: 14px;
  --radius-sm: 8px;
  --shadow-card: 0 1px 3px rgba(28, 28, 40, 0.06), 0 1px 2px rgba(28, 28, 40, 0.08);
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: "Noto Sans Thai", "Segoe UI", "TH Sarabun New", system-ui, sans-serif;
  font-size: 17px;
  line-height: 1.75;
  background: var(--color-bg);
  color: var(--color-text);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { line-height: 1.5; }

.site-header {
  padding: 28px 20px;
  text-align: center;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.site-header h1 {
  margin: 0 0 10px;
  font-size: 1.75rem;
  font-weight: 800;
  letter-spacing: -0.01em;
}

.site-header p { margin: 0; }

.event-meta {
  color: var(--color-muted);
  font-size: 0.95rem;
  margin-bottom: 10px !important;
}

.site-header a {
  display: inline-block;
  color: var(--color-primary);
  text-decoration: none;
  font-weight: 600;
  font-size: 0.95rem;
}

.site-header a:hover { text-decoration: underline; }

.category-nav {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin-top: 16px;
}

.category-nav a {
  font-size: 0.85rem;
  font-weight: 600;
  padding: 7px 14px;
  border-radius: 999px;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  color: var(--color-text);
}

.back-link-floating {
  position: fixed;
  top: 10px;
  left: 10px;
  z-index: 20;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--color-muted);
  text-decoration: none;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  padding: 6px 12px;
  border-radius: 999px;
  box-shadow: var(--shadow-card);
}

.back-link-floating:hover { color: var(--color-primary); }

.category-nav a:hover {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: #fff;
  text-decoration: none;
}

main {
  max-width: 1080px;
  margin: 0 auto;
  padding: 24px 20px 60px;
}

.results-grid {
  max-width: 1600px;
  margin: 44px auto 20px;
  padding: 0 20px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 20px;
  height: calc(100vh - 84px);
  min-height: 620px;
}

@media (max-width: 900px) {
  .results-grid {
    grid-template-columns: 1fr;
    grid-template-rows: none;
    height: auto;
  }
}

.results-grid .category {
  margin-bottom: 0;
  height: 100%;
  /* กันไม่ให้การ์ดที่มีกราฟกว้าง (เช่นหมวดที่มีหลายทีม) ดันความกว้างของ grid track
     ให้เกิน viewport — ค่า default ของ grid item คือ min-width:auto (ขยายตามเนื้อหา) */
  min-width: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

@media (max-width: 900px) {
  .results-grid .category { height: 460px; }
}

.category {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-card);
  padding: 24px;
  margin-bottom: 28px;
  scroll-margin-top: 88px;
}

.category h2 {
  margin: 0 0 16px;
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--color-primary-dark);
  padding-bottom: 14px;
  border-bottom: 2px solid var(--color-border);
}

.leading-team {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--color-success);
}

.notice {
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  background: var(--color-warning-bg);
  color: var(--color-warning);
  font-size: 0.95rem;
  font-weight: 600;
  margin-bottom: 16px;
}

.notice-success {
  background: var(--color-success-bg);
  color: var(--color-success);
}

.system-notice {
  max-width: 1080px;
  margin: 0 auto 20px;
  text-align: center;
  font-weight: 700;
  font-size: 1.05rem;
  background: var(--color-danger-bg);
  color: var(--color-danger);
  border-radius: 0;
}

.contestant-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

.contestant-card {
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--color-surface);
  box-shadow: var(--shadow-card);
  display: flex;
  flex-direction: column;
  transition: box-shadow 0.2s ease, transform 0.2s ease;
}

.contestant-card:hover {
  box-shadow: 0 4px 14px rgba(28, 28, 40, 0.12);
  transform: translateY(-2px);
}

.video-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  background: #000;
}

.video-wrap iframe {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
}

.contestant-card h3 {
  font-size: 1.05rem;
  font-weight: 700;
  margin: 14px 14px 2px;
  line-height: 1.5;
}

.clip-title {
  font-size: 0.9rem;
  font-style: italic;
  color: var(--color-muted);
  margin: 0 14px 10px;
  line-height: 1.5;
}

.btn-vote {
  margin: auto 14px 14px;
  padding: 13px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--color-primary);
  color: #fff;
  font-family: inherit;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s ease;
}

.btn-vote:hover:not(:disabled) { background: var(--color-primary-dark); }

.btn-vote:disabled {
  background: #c7c7d4;
  cursor: not-allowed;
}

.total-votes {
  color: var(--color-muted);
  font-size: 0.95rem;
  margin: -6px 0 18px;
}

.total-votes .total-count {
  font-weight: 700;
  color: var(--color-text);
}

.bar-chart {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: stretch;
  justify-content: space-around;
  gap: 6px;
  padding: 4px 2px 0;
}

.bar-col {
  display: flex;
  flex: 1 1 0;
  min-width: 0;
  max-width: 130px;
  flex-direction: column;
  align-items: center;
}

.bar-value {
  font-size: 0.75rem;
  color: var(--color-muted);
  white-space: nowrap;
  margin-bottom: 6px;
  font-variant-numeric: tabular-nums;
}

.bar-track {
  width: 60%;
  max-width: 52px;
  flex: 1;
  min-height: 0;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
  display: flex;
  align-items: flex-end;
  overflow: hidden;
}

.bar-fill {
  width: 100%;
  height: 0%;
  background: linear-gradient(0deg, var(--color-primary-dark), var(--color-primary));
  border-radius: 4px 4px 0 0;
  transition: height 0.4s ease;
}

.bar-label {
  font-size: 0.78rem;
  font-weight: 600;
  text-align: center;
  line-height: 1.4;
  margin-top: 8px;
  max-width: 90px;
  /* ความสูงคงที่ 3 บรรทัดเสมอ ไม่ว่าชื่อจะสั้นหรือยาว กันไม่ให้ตำแหน่ง "0" ของกราฟเลื่อนไม่เท่ากัน */
  height: calc(1.4em * 3);
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.bar-col.is-leading .bar-value::before { content: "🏆 "; }
.bar-col.is-leading .bar-fill { background: linear-gradient(0deg, #0f6e33, var(--color-success)); }
.bar-col.is-leading .bar-label { color: var(--color-success); }

.update-indicator {
  text-align: center;
  color: var(--color-muted);
  font-size: 0.9rem;
  padding-bottom: 32px;
}

/* จอแท็บเล็ต/มือถือใหญ่ — ชื่องานยาว ย่อลง และไม่ปักหัวเรื่องค้างบนจอ (เลื่อนหายไปได้) */
@media (max-width: 640px) {
  .site-header {
    position: static;
    padding: 14px 16px 12px;
  }
  .site-header h1 { font-size: 1.05rem; line-height: 1.3; margin-bottom: 8px; }
  .event-meta { font-size: 0.82rem; }
  .category-nav { margin-top: 12px; gap: 6px; }
  .category { scroll-margin-top: 12px; }
}

@media (max-width: 480px) {
  body { font-size: 16px; }
  .site-header { padding: 12px 14px 10px; }
  .site-header h1 { font-size: 0.9rem; line-height: 1.28; margin-bottom: 6px; }
  main { padding: 16px 14px 48px; }
  .category { padding: 16px; }
  .contestant-grid { grid-template-columns: 1fr; }
}

@media (max-width: 360px) {
  .site-header h1 { font-size: 0.84rem; }
}
