:root {
  --primary: #1A5F7A;
  --secondary: #227C9D;
  --accent: #17C3B2;
  --light: #E8F1F5;
  --text: #2D3748;
  --muted: #718096;
  --white: #FFFFFF;
  --shadow: 0 4px 20px rgba(0,0,0,0.07);
  --radius: 16px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body { font-family: 'Outfit', sans-serif; background: #F0F4F8; color: var(--text); min-height: 100vh; }

/* LOGIN */
.login-wrapper, .setup-wrapper {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #E8F1F5, #d0e8f0); padding: 2rem;
}
.login-card, .setup-card {
  background: var(--white); border-radius: var(--radius); padding: 3rem 2.5rem;
  width: 100%; max-width: 440px; box-shadow: 0 20px 60px rgba(26,95,122,0.12); text-align: center;
}
.login-logo { max-height: 80px; margin-bottom: 1.5rem; object-fit: contain; }
.login-card h1, .setup-card h1 { font-size: 1.8rem; font-weight: 700; color: var(--primary); margin-bottom: 0.4rem; }
.login-subtitle { color: var(--muted); margin-bottom: 2rem; font-size: 0.95rem; }

.form-group { margin-bottom: 1.2rem; text-align: left; }
.form-group label { display: block; font-weight: 500; color: var(--text); margin-bottom: 0.4rem; font-size: 0.9rem; }
.form-group input, .form-group textarea {
  width: 100%; padding: 0.8rem 1rem; border: 1.5px solid #CBD5E0; border-radius: 10px;
  font-family: 'Outfit', sans-serif; font-size: 1rem; color: var(--text); transition: border-color 0.2s; resize: vertical;
}
.form-group input:focus, .form-group textarea:focus { outline: none; border-color: var(--secondary); }

/* BUTTONS */
.btn {
  display: inline-flex; align-items: center; gap: 0.4rem;
  padding: 0.75rem 1.5rem; border-radius: 50px; font-family: 'Outfit', sans-serif;
  font-weight: 600; font-size: 0.95rem; cursor: pointer; border: none; text-decoration: none; transition: all 0.2s;
}
.btn-primary { background: linear-gradient(135deg, var(--secondary), var(--primary)); color: var(--white); box-shadow: 0 4px 15px rgba(26,95,122,0.25); }
.btn-primary:hover { transform: translateY(-2px); box-shadow: 0 8px 25px rgba(26,95,122,0.3); }
.btn-primary:disabled { opacity: 0.7; cursor: not-allowed; transform: none; }
.btn-secondary { background: var(--light); color: var(--secondary); border: 1px solid rgba(34,124,157,0.15); }
.btn-secondary:hover { background: rgba(34,124,157,0.12); }
.btn-sm { padding: 0.45rem 0.9rem; font-size: 0.82rem; }
.btn-full { width: 100%; justify-content: center; }
.btn-link { background: none; border: none; color: var(--muted); font-family: 'Outfit', sans-serif; font-size: 0.9rem; cursor: pointer; text-decoration: underline; }

.alert { padding: 0.8rem 1.2rem; border-radius: 10px; font-size: 0.9rem; margin-bottom: 1rem; }
.alert-error { background: #FFF5F5; color: #C53030; border: 1px solid #FED7D7; }

/* PERIOD FILTER */
.period-filter { display: flex; gap: 0.4rem; background: var(--white); padding: 4px; border-radius: 50px; box-shadow: var(--shadow); }
.period-btn {
  padding: 0.4rem 1rem; border: none; border-radius: 50px; font-family: 'Outfit', sans-serif;
  font-size: 0.85rem; font-weight: 500; cursor: pointer; background: transparent; color: var(--muted); transition: all 0.2s;
}
.period-btn.active { background: linear-gradient(135deg, var(--secondary), var(--primary)); color: white; box-shadow: 0 2px 10px rgba(26,95,122,0.2); }
.period-btn:hover:not(.active) { background: var(--light); color: var(--primary); }

/* HEADER */
.dash-header { background: var(--white); box-shadow: 0 2px 12px rgba(0,0,0,0.07); position: sticky; top: 0; z-index: 100; }
.dash-header-inner { max-width: 1400px; margin: 0 auto; padding: 0.7rem 2rem; display: flex; align-items: center; justify-content: space-between; }
.dash-logo { max-height: 52px; object-fit: contain; }
.dash-header-right { display: flex; align-items: center; gap: 1rem; }
.ig-badge {
  display: inline-flex; align-items: center; gap: 6px;
  background: linear-gradient(135deg, #f09433, #e6683c, #dc2743, #cc2366, #bc1888);
  color: white; padding: 0.35rem 0.8rem; border-radius: 50px; font-size: 0.82rem; font-weight: 600;
}
.cache-info { font-size: 0.75rem; color: var(--muted); }

/* MAIN */
.dash-main { padding: 1.5rem 1rem 4rem; }
.dash-container { max-width: 1400px; margin: 0 auto; }

.dash-title-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1.2rem; }
.dash-title-row h1 { font-size: 1.5rem; font-weight: 700; color: var(--primary); }

/* STATS */
.stats-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; margin-bottom: 1.2rem; }
.stat-card {
  background: var(--white); border-radius: var(--radius); padding: 1.2rem 1.3rem;
  box-shadow: var(--shadow); display: flex; align-items: center; gap: 1rem; transition: transform 0.2s;
}
.stat-card:hover { transform: translateY(-3px); }
.stat-icon { width: 48px; height: 48px; border-radius: 13px; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.stat-inner { display: flex; flex-direction: column; }
.stat-label { font-size: 0.75rem; color: var(--muted); font-weight: 500; text-transform: uppercase; letter-spacing: 0.5px; }
.stat-value { font-size: 1.7rem; font-weight: 700; color: var(--text); line-height: 1.2; margin-top: 2px; }

/* GRIDS */
.dash-grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1.2rem; }
.dash-grid-3-1 { grid-template-columns: 2.5fr 1fr; }
.dash-grid-1-2 { grid-template-columns: 1fr 1.8fr; }

/* CARD */
.card { background: var(--white); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.card-header { padding: 1.2rem 1.4rem 0; display: flex; align-items: baseline; justify-content: space-between; }
.card-header h2 { font-size: 1rem; font-weight: 700; color: var(--primary); }
.card-subtitle { font-size: 0.78rem; color: var(--muted); }
.chart-wrapper { padding: 1rem 1.2rem 1.2rem; }

/* TOP POSTS */
.top-posts-list { padding: 0.8rem 1.2rem 1.2rem; display: flex; flex-direction: column; gap: 0.6rem; }
.top-post-row {
  display: flex; align-items: center; gap: 0.8rem; padding: 0.5rem; border-radius: 10px;
  transition: background 0.2s; cursor: pointer;
}
.top-post-row:hover { background: var(--light); }
.top-rank { font-size: 0.82rem; font-weight: 700; color: var(--muted); min-width: 22px; }
.top-post-thumb { width: 42px; height: 42px; border-radius: 8px; background-size: cover; background-position: center; background-color: var(--light); flex-shrink: 0; }
.top-post-info { flex: 1; min-width: 0; }
.top-post-caption { font-size: 0.78rem; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-bottom: 3px; }
.top-post-bar-wrap { background: var(--light); border-radius: 4px; height: 4px; margin-bottom: 3px; }
.top-post-bar { height: 4px; border-radius: 4px; background: linear-gradient(90deg, var(--secondary), var(--accent)); }
.top-post-meta { display: flex; gap: 0.6rem; font-size: 0.75rem; color: var(--muted); }

/* POSTS GRID */
.posts-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 4px; padding: 0.8rem 1.2rem 1.2rem; }
.post-thumb { position: relative; aspect-ratio: 1; border-radius: 8px; overflow: hidden; cursor: pointer; display: block; }
.post-img { width: 100%; height: 100%; background-size: cover; background-position: center; background-color: var(--light); transition: transform 0.3s; }
.post-thumb:hover .post-img { transform: scale(1.06); }
.post-overlay {
  position: absolute; inset: 0; background: rgba(26,95,122,0.75);
  display: flex; align-items: center; justify-content: center; gap: 0.6rem;
  color: white; font-size: 0.78rem; font-weight: 600; opacity: 0; transition: opacity 0.2s;
}
.post-thumb:hover .post-overlay { opacity: 1; }
.type-badge { position: absolute; top: 5px; right: 5px; background: rgba(0,0,0,0.5); color: white; font-size: 0.65rem; padding: 2px 5px; border-radius: 4px; }

/* MODAL */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6); z-index: 1000;
  display: flex; align-items: center; justify-content: center; padding: 1rem;
  opacity: 0; pointer-events: none; transition: opacity 0.25s;
}
.modal-overlay.active { opacity: 1; pointer-events: all; }
.modal-card {
  background: var(--white); border-radius: var(--radius); max-width: 700px; width: 100%;
  max-height: 90vh; overflow-y: auto; position: relative;
  transform: scale(0.95); transition: transform 0.25s;
}
.modal-overlay.active .modal-card { transform: scale(1); }
.modal-close {
  position: absolute; top: 1rem; right: 1rem; background: rgba(0,0,0,0.1);
  border: none; border-radius: 50%; width: 32px; height: 32px; cursor: pointer;
  font-size: 0.9rem; color: var(--text); display: flex; align-items: center; justify-content: center;
  z-index: 10; transition: background 0.2s;
}
.modal-close:hover { background: rgba(0,0,0,0.2); }
.modal-body { display: flex; gap: 0; }
.modal-img-wrap { width: 280px; flex-shrink: 0; background: #000; border-radius: var(--radius) 0 0 var(--radius); overflow: hidden; min-height: 280px; display: flex; align-items: center; }
.modal-img { width: 100%; height: 100%; object-fit: cover; }
.modal-info { flex: 1; padding: 1.8rem 1.5rem; display: flex; flex-direction: column; }
.modal-meta { display: flex; align-items: center; gap: 0.6rem; margin-bottom: 0.8rem; }
.modal-type-badge { background: var(--light); color: var(--primary); padding: 3px 10px; border-radius: 20px; font-size: 0.78rem; font-weight: 600; }
.modal-date { font-size: 0.8rem; color: var(--muted); }
.modal-caption { font-size: 0.9rem; color: var(--text); line-height: 1.6; max-height: 120px; overflow-y: auto; margin-bottom: 1.2rem; flex: 1; }
.modal-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.6rem; }
.modal-stat { background: var(--light); border-radius: 10px; padding: 0.7rem; text-align: center; }
.ms-val { display: block; font-size: 1.1rem; font-weight: 700; color: var(--primary); }
.ms-label { display: block; font-size: 0.72rem; color: var(--muted); margin-top: 2px; }

/* SKELETON */
.skeleton { animation: pulse 1.5s ease-in-out infinite; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.45; } }
.sk-line { background: var(--light); border-radius: 4px; }
.sk-title { height: 11px; width: 60%; margin-bottom: 8px; }
.sk-value { height: 26px; width: 40%; }
.post-thumb.skeleton { background: var(--light); }
@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }

/* STORIES */
.stories-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 0.8rem; padding: 1rem 1.2rem 1.2rem; }
.story-card {
  border-radius: 12px; overflow: hidden; cursor: pointer;
  background: var(--light); transition: transform 0.2s, box-shadow 0.2s;
  border: 2px solid transparent;
}
.story-card:hover { transform: translateY(-3px); box-shadow: var(--shadow); border-color: var(--accent); }
.story-card.skeleton { height: 160px; }
.story-thumb {
  width: 100%; aspect-ratio: 9/16; max-height: 180px;
  background-size: cover; background-position: center; background-color: #c8d8e4;
  position: relative;
}
.story-info { padding: 0.6rem 0.8rem; }
.story-date { font-size: 0.73rem; color: var(--muted); display: block; margin-bottom: 0.4rem; }
.story-metrics { display: flex; flex-wrap: wrap; gap: 0.3rem; }
.story-metrics span { font-size: 0.75rem; color: var(--text); font-weight: 500; background: var(--white); padding: 2px 6px; border-radius: 6px; }
.no-stories { grid-column: 1/-1; text-align: center; padding: 2rem; color: var(--muted); font-size: 0.9rem; }

/* RESPONSIVE */
@media (max-width: 1200px) {
  .posts-grid { grid-template-columns: repeat(4, 1fr); }
  .dash-grid-3-1 { grid-template-columns: 1.8fr 1fr; }
}
@media (max-width: 900px) {
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .dash-grid-2, .dash-grid-3-1, .dash-grid-1-2 { grid-template-columns: 1fr; }
  .posts-grid { grid-template-columns: repeat(4, 1fr); }
  .modal-body { flex-direction: column; }
  .modal-img-wrap { width: 100%; height: 220px; border-radius: var(--radius) var(--radius) 0 0; }
  .dash-header-inner { padding: 0.7rem 1rem; }
  .ig-badge, .cache-info { display: none; }
}
@media (max-width: 540px) {
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .posts-grid { grid-template-columns: repeat(3, 1fr); }
  .modal-stats { grid-template-columns: repeat(2, 1fr); }
  .stat-value { font-size: 1.4rem; }
}
