/* MGR Trend Dashboard — Custom CSS
   Tailwind CDN overrides + animations + dark mode
   ------------------------------------------------ */

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

/* ── Base ── */
*, *::before, *::after { box-sizing: border-box; }

:root {
  --sidebar-w: 240px;
  --topbar-h: 60px;

  /* Brand colors */
  --c-primary:   #3B82F6; /* blue-500 */
  --c-success:   #10B981; /* emerald-500 */
  --c-warning:   #F59E0B; /* amber-500 */
  --c-danger:    #EF4444; /* red-500 */
  --c-purple:    #8B5CF6; /* violet-500 */
  --c-cyan:      #06B6D4; /* cyan-500 */

  /* Light mode */
  --bg-body:     #F1F5F9;
  --bg-card:     #FFFFFF;
  --bg-sidebar:  #0F172A;
  --bg-topbar:   #FFFFFF;
  --text-primary:#0F172A;
  --text-muted:  #64748B;
  --border:      #E2E8F0;
}

[data-theme="dark"] {
  --bg-body:    #0B1120;
  --bg-card:    #1E293B;
  --bg-sidebar: #0B1120;
  --bg-topbar:  #1E293B;
  --text-primary:#F1F5F9;
  --text-muted:  #94A3B8;
  --border:      #334155;
}

html { font-family: 'Inter', 'Noto Sans Thai', sans-serif; }
body {
  background: var(--bg-body);
  color: var(--text-primary);
  margin: 0;
  min-height: 100vh;
  transition: background .3s, color .3s;
}

/* ── Layout ── */
.layout {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: var(--topbar-h) 1fr;
  min-height: 100vh;
}

.sidebar {
  grid-row: 1 / -1;
  background: var(--bg-sidebar);
  width: var(--sidebar-w);
  position: fixed;
  top: 0; left: 0;
  height: 100vh;
  overflow-y: auto;
  z-index: 50;
  display: flex;
  flex-direction: column;
}

.topbar {
  position: fixed;
  top: 0;
  left: var(--sidebar-w);
  right: 0;
  height: var(--topbar-h);
  background: var(--bg-topbar);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.5rem;
  z-index: 40;
}

.main-content {
  margin-left: var(--sidebar-w);
  margin-top: var(--topbar-h);
  padding: 1.5rem;
  min-height: calc(100vh - var(--topbar-h));
}

/* ── Sidebar ── */
.sidebar-logo {
  padding: 1.25rem 1.25rem 1rem;
  border-bottom: 1px solid rgba(255,255,255,.07);
}

.sidebar-logo .brand {
  font-size: 1.125rem;
  font-weight: 700;
  color: #fff;
  letter-spacing: -.02em;
}

.sidebar-logo .brand span { color: var(--c-primary); }

.sidebar-logo .subtitle {
  font-size: .7rem;
  color: rgba(255,255,255,.4);
  margin-top: 2px;
}

.nav-section {
  padding: .75rem 1rem .25rem;
  font-size: .65rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .1em;
  color: rgba(255,255,255,.3);
}

.nav-item {
  display: flex;
  align-items: center;
  gap: .625rem;
  padding: .55rem 1.25rem;
  margin: .125rem .625rem;
  border-radius: .5rem;
  color: rgba(255,255,255,.6);
  text-decoration: none;
  font-size: .875rem;
  font-weight: 500;
  transition: all .15s;
  cursor: pointer;
}

.nav-item:hover { background: rgba(255,255,255,.07); color: #fff; }
.nav-item.active { background: var(--c-primary); color: #fff; }
.nav-item .icon { width: 1.125rem; height: 1.125rem; flex-shrink: 0; }

.sidebar-footer {
  margin-top: auto;
  padding: 1rem 1.25rem;
  border-top: 1px solid rgba(255,255,255,.07);
  font-size: .8rem;
  color: rgba(255,255,255,.3);
}

/* ── Cards ── */
.card {
  background: var(--bg-card);
  border-radius: .875rem;
  border: 1px solid var(--border);
  padding: 1.25rem;
  transition: background .3s, border-color .3s;
}

.card-title {
  font-size: .8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--text-muted);
  margin-bottom: .75rem;
}

/* ── KPI Cards ── */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
}

.kpi-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: .875rem;
  padding: 1.25rem;
  position: relative;
  overflow: hidden;
}

.kpi-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 3px;
  background: var(--kpi-color, var(--c-primary));
  border-radius: .875rem .875rem 0 0;
}

.kpi-label {
  font-size: .75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--text-muted);
}

.kpi-value {
  font-size: 2rem;
  font-weight: 800;
  line-height: 1.1;
  margin: .375rem 0;
  font-variant-numeric: tabular-nums;
  letter-spacing: -.03em;
}

.kpi-delta {
  font-size: .75rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: .25rem;
}

.kpi-delta.up   { color: var(--c-success); }
.kpi-delta.down { color: var(--c-danger); }
.kpi-delta.neu  { color: var(--text-muted); }

/* ── Ranking Board ── */
.ranking-board { display: flex; flex-direction: column; gap: .375rem; }

.rank-item {
  display: grid;
  grid-template-columns: 2rem 1fr auto auto;
  align-items: center;
  gap: .625rem;
  padding: .5rem .75rem;
  border-radius: .5rem;
  background: var(--bg-body);
  border: 1px solid transparent;
  transition: all .2s;
}

.rank-item:hover { border-color: var(--c-primary); background: var(--bg-card); }

.rank-num {
  font-size: .75rem;
  font-weight: 700;
  text-align: center;
  color: var(--text-muted);
}

.rank-num.top3 {
  width: 1.5rem;
  height: 1.5rem;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: .7rem;
  font-weight: 800;
  color: #fff;
}

.rank-num.r1 { background: #F59E0B; }
.rank-num.r2 { background: #94A3B8; }
.rank-num.r3 { background: #B45309; }

.rank-title {
  font-size: .8rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.rank-hits {
  font-size: .8rem;
  font-weight: 700;
  color: var(--c-primary);
  font-variant-numeric: tabular-nums;
  min-width: 4rem;
  text-align: right;
}

.rank-sparkline { width: 60px; height: 24px; }

/* ── Number animation ── */
.num-anim {
  display: inline-block;
  transition: color .3s;
}

.num-anim.rising  { color: var(--c-success); }
.num-anim.falling { color: var(--c-danger); }

/* ── Badge ── */
.badge {
  display: inline-flex;
  align-items: center;
  padding: .2rem .6rem;
  border-radius: 999px;
  font-size: .7rem;
  font-weight: 600;
}

.badge-blue   { background: #DBEAFE; color: #1D4ED8; }
.badge-green  { background: #D1FAE5; color: #065F46; }
.badge-amber  { background: #FEF3C7; color: #92400E; }
.badge-red    { background: #FEE2E2; color: #991B1B; }
.badge-purple { background: #EDE9FE; color: #5B21B6; }

[data-theme="dark"] .badge-blue   { background: #1E3A5F; color: #93C5FD; }
[data-theme="dark"] .badge-green  { background: #064E3B; color: #6EE7B7; }
[data-theme="dark"] .badge-amber  { background: #451A03; color: #FCD34D; }
[data-theme="dark"] .badge-red    { background: #450A0A; color: #FCA5A5; }
[data-theme="dark"] .badge-purple { background: #2E1065; color: #C4B5FD; }

/* ── Rising Fast tag ── */
.rising-tag {
  display: inline-flex;
  align-items: center;
  gap: .25rem;
  font-size: .65rem;
  font-weight: 700;
  color: var(--c-success);
  background: #D1FAE5;
  padding: .15rem .45rem;
  border-radius: .25rem;
}

[data-theme="dark"] .rising-tag { background: #064E3B; }

/* ── Tables ── */
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .8rem;
}

.data-table th {
  text-align: left;
  padding: .625rem .875rem;
  font-size: .7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--text-muted);
  background: var(--bg-body);
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
}

.data-table td {
  padding: .625rem .875rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.data-table tbody tr:hover { background: var(--bg-body); }
.data-table tbody tr:last-child td { border-bottom: none; }

/* ── Page grid helpers ── */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; }

@media (max-width: 1280px) {
  .grid-4 { grid-template-columns: repeat(2, 1fr); }
  .grid-3 { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 900px) {
  :root { --sidebar-w: 0px; }
  .sidebar { transform: translateX(-240px); }
  .sidebar.open { transform: translateX(0); --sidebar-w: 240px; }
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
}

/* ── Chart containers ── */
.chart-sm  { height: 200px; }
.chart-md  { height: 300px; }
.chart-lg  { height: 400px; }
.chart-xl  { height: 480px; }

/* ── Forms ── */
.form-input {
  width: 100%;
  padding: .6rem .875rem;
  border-radius: .5rem;
  border: 1px solid var(--border);
  background: var(--bg-body);
  color: var(--text-primary);
  font-size: .875rem;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}

.form-input:focus {
  border-color: var(--c-primary);
  box-shadow: 0 0 0 3px rgba(59,130,246,.15);
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: .375rem;
  padding: .55rem 1.125rem;
  border-radius: .5rem;
  font-size: .875rem;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: all .15s;
}

.btn-primary { background: var(--c-primary); color: #fff; }
.btn-primary:hover { background: #2563EB; }
.btn-secondary { background: var(--bg-body); color: var(--text-primary); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--border); }
.btn-sm { padding: .375rem .75rem; font-size: .8rem; }

/* ── Skeleton loader ── */
.skeleton {
  background: linear-gradient(90deg, var(--border) 25%, var(--bg-body) 50%, var(--border) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
  border-radius: .375rem;
}

@keyframes shimmer {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ── Live dot ── */
.live-dot {
  display: inline-flex;
  align-items: center;
  gap: .375rem;
  font-size: .7rem;
  font-weight: 600;
  color: var(--c-success);
}

.live-dot::before {
  content: '';
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--c-success);
  animation: pulse-dot 2s infinite;
}

@keyframes pulse-dot {
  0%, 100% { opacity: 1; transform: scale(1); }
  50%       { opacity: .5; transform: scale(1.4); }
}

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ── Login page ── */
.login-wrapper {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-body);
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 1rem;
  padding: 2rem;
}

/* ── Misc ── */
.text-muted { color: var(--text-muted); }
.divider { height: 1px; background: var(--border); margin: 1rem 0; }
.section-gap { margin-bottom: 1.25rem; }
.truncate { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mono { font-variant-numeric: tabular-nums; font-family: 'Inter', monospace; }
