/* ==========================================================================
   Modern UI/UX Design System — Light Mode (default) & Dark Mode
   ========================================================================== */
:root,
[data-theme='light'] {
  --bg: #f8fafc;
  --bg-accent: radial-gradient(circle at 0% 0%, rgba(13, 148, 136, 0.07) 0%, transparent 45%),
               radial-gradient(circle at 100% 0%, rgba(79, 70, 229, 0.06) 0%, transparent 45%);
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --surface-hover: #f8fafc;
  --topbar-bg: rgba(255, 255, 255, 0.86);
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-body: #334155;
  --muted: #64748b;
  --primary: #0d9488;
  --primary-hover: #0f766e;
  --primary-fg: #ffffff;
  --primary-soft: rgba(13, 148, 136, 0.10);
  --primary-border: rgba(13, 148, 136, 0.30);
  --secondary: #4f46e5;
  --secondary-soft: rgba(79, 70, 229, 0.09);
  --secondary-border: rgba(79, 70, 229, 0.25);
  --gradient-bar: linear-gradient(90deg, #0d9488 0%, #2563eb 100%);
  --amber-bg: #fef3c7;
  --amber-fg: #b45309;
  --danger-bg: #fef2f2;
  --danger-border: #fecaca;
  --danger-fg: #dc2626;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-md: 0 4px 16px rgba(15, 23, 42, 0.06), 0 1px 3px rgba(15, 23, 42, 0.04);
  --shadow-lg: 0 24px 56px rgba(15, 23, 42, 0.16);
  --backdrop: rgba(15, 23, 42, 0.45);
  --input-bg: #ffffff;
  --track-bg: #e2e8f0;
  --radius: 14px;
}

[data-theme='dark'] {
  --bg: #0b1120;
  --bg-accent: radial-gradient(circle at 0% 0%, rgba(20, 184, 166, 0.12) 0%, transparent 45%),
               radial-gradient(circle at 100% 0%, rgba(99, 102, 241, 0.12) 0%, transparent 45%);
  --surface: #111827;
  --surface-2: #182234;
  --surface-hover: #1e293b;
  --topbar-bg: rgba(11, 17, 32, 0.84);
  --border: #1f2937;
  --border-strong: #334155;
  --text: #f8fafc;
  --text-body: #cbd5e1;
  --muted: #94a3b8;
  --primary: #14b8a6;
  --primary-hover: #2dd4bf;
  --primary-fg: #042f2e;
  --primary-soft: rgba(20, 184, 166, 0.14);
  --primary-border: rgba(20, 184, 166, 0.35);
  --secondary: #818cf8;
  --secondary-soft: rgba(129, 140, 248, 0.14);
  --secondary-border: rgba(129, 140, 248, 0.30);
  --gradient-bar: linear-gradient(90deg, #14b8a6 0%, #6366f1 100%);
  --amber-bg: rgba(245, 158, 11, 0.16);
  --amber-fg: #fbbf24;
  --danger-bg: rgba(239, 68, 68, 0.15);
  --danger-border: rgba(239, 68, 68, 0.38);
  --danger-fg: #f87171;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.25);
  --shadow-md: 0 8px 28px rgba(0, 0, 0, 0.32);
  --shadow-lg: 0 24px 64px rgba(0, 0, 0, 0.6);
  --backdrop: rgba(2, 6, 23, 0.72);
  --input-bg: #0f172a;
  --track-bg: #1e293b;
  --radius: 14px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, 'Noto Sans Thai', sans-serif;
  background-color: var(--bg);
  background-image: var(--bg-accent);
  background-attachment: fixed;
  color: var(--text-body);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  transition: background-color 0.2s ease, color 0.2s ease;
}

.container {
  max-width: 1140px;
  margin: 0 auto;
  padding: 0 20px;
}

/* Sticky Glassmorphic Topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--topbar-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.topbar::before {
  content: '';
  display: block;
  height: 3px;
  background: var(--gradient-bar);
}

.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding-top: 14px;
  padding-bottom: 14px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-icon {
  font-size: 26px;
}

.brand h1 {
  margin: 0;
  font-size: 19px;
  font-weight: 800;
  color: var(--text);
  letter-spacing: -0.02em;
}

.subtitle {
  margin: 2px 0 0;
  font-size: 12.5px;
  color: var(--muted);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.toggle-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
  cursor: pointer;
}

.toggle-label input {
  accent-color: var(--primary);
}

.main-content {
  padding-top: 28px;
  padding-bottom: 60px;
}

/* KPI Stats */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 16px;
  margin-bottom: 28px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 22px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  box-shadow: var(--shadow-sm);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.stat-label {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--muted);
}

.stat-value {
  font-size: 30px;
  font-weight: 800;
  color: var(--text);
  letter-spacing: -0.02em;
}

.stat-value.accent {
  color: var(--primary);
}

.stat-value.highlight {
  color: var(--secondary);
}

.section-heading {
  margin: 36px 0 16px;
  font-size: 19px;
  font-weight: 800;
  color: var(--text);
  letter-spacing: -0.015em;
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Simulator Card */
.simulator-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 24px;
  margin-bottom: 28px;
  box-shadow: var(--shadow-md);
}

.simulator-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 20px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border);
}

.simulator-head h2 {
  margin: 0;
  font-size: 19px;
  font-weight: 800;
  color: var(--text);
}

.sim-steps {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-bottom: 18px;
}

.sim-step {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px 18px;
}

.sim-step-title {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 15px;
  color: var(--text);
  margin-bottom: 10px;
}

.sim-num {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  border-radius: 999px;
  background: var(--gradient-bar);
  color: #ffffff;
  font-size: 12px;
  font-weight: 800;
  box-shadow: 0 2px 6px rgba(13, 148, 136, 0.25);
}

.sim-step-desc {
  margin: -4px 0 12px;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.55;
}

.sim-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 12px;
}

.sim-sources-grid {
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
}

.sim-square-grid {
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
}

.sim-portrait-grid {
  grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
}

.sim-option {
  display: flex;
  align-items: center;
  gap: 10px;
  text-align: left;
  background: var(--surface);
  color: var(--text-body);
  border: 1.5px solid var(--border);
  border-radius: 12px;
  padding: 10px;
  cursor: pointer;
  transition: border-color 0.15s ease, box-shadow 0.15s ease, transform 0.12s ease, background 0.15s ease;
}

.sim-option:hover {
  border-color: var(--primary);
  transform: translateY(-2px);
  box-shadow: var(--shadow-sm);
}

.sim-option.selected {
  border: 2px solid var(--primary);
  background: var(--primary-soft);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

.sim-option.source-card {
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
}

.sim-option.image-only-card {
  padding: 6px;
  justify-content: center;
  align-items: center;
}

.sim-previews-pair {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.sim-preview-shot {
  width: 100%;
  height: auto;
  aspect-ratio: 9 / 16;
  max-height: 220px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
}

.sim-preview-shot.single {
  grid-column: 1 / -1;
}

.sim-preview-placeholder {
  grid-column: 1 / -1;
  height: 140px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  border: 1px dashed var(--border-strong);
  background: var(--surface-2);
  color: var(--muted);
  font-size: 12px;
}

.sim-image-full {
  width: 100%;
  background: var(--surface-2);
  border-radius: 8px;
}

.sim-image-full.square {
  height: auto;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  border-radius: 10px;
}

.sim-image-full.portrait {
  height: auto;
  aspect-ratio: 9 / 16;
  max-height: 220px;
  object-fit: cover;
  border-radius: 8px;
}

.sim-option-meta {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.sim-option-meta strong {
  font-size: 13px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sim-option-meta span {
  font-size: 11px;
  color: var(--muted);
}

.sim-empty {
  font-size: 13px;
  color: var(--muted);
}

.sim-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

/* Campaign Cards & Live Results */
.campaigns-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
  gap: 20px;
}

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

.campaign-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.campaign-body {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  flex: 1;
}

.campaign-head {
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.campaign-title {
  margin: 0 0 6px;
  font-size: 17px;
  font-weight: 800;
  color: var(--text);
}

.badges {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid transparent;
}

.badge.active {
  background: var(--primary-soft);
  color: var(--primary);
  border-color: var(--primary-border);
}

.badge.paused {
  background: var(--amber-bg);
  color: var(--amber-fg);
}

.badge.ended {
  background: var(--danger-bg);
  color: var(--danger-fg);
  border-color: var(--danger-border);
}

.badge.mode {
  background: var(--secondary-soft);
  color: var(--secondary);
  border-color: var(--secondary-border);
}

/* 4-Visual Showcase on Campaign Live Results Cards */
.campaign-visuals-4 {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  align-items: end;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px;
}

.visual-slot {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.visual-caption {
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.visual-img,
.visual-empty {
  width: 100%;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
}

.visual-img.portrait,
.visual-empty.portrait {
  aspect-ratio: 9 / 16;
  max-height: 128px;
  object-fit: cover;
}

.visual-img.square,
.visual-empty.square {
  aspect-ratio: 1 / 1;
  max-width: 84px;
  object-fit: cover;
}

.visual-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 12px;
}

.results-box {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.results-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
}

.tally-item {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.tally-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  font-size: 14px;
}

.tally-name {
  font-weight: 600;
  color: var(--text);
}

.tally-score {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
}

.progress-track {
  height: 7px;
  background: var(--track-bg);
  border-radius: 999px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: var(--gradient-bar);
  border-radius: 999px;
  transition: width 0.35s ease;
}

.card-actions {
  margin-top: auto;
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

/* Refined UI/UX Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease, box-shadow 0.15s ease;
}

.btn:hover:not(:disabled) {
  transform: translateY(-1px);
}

.btn:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

.btn.primary {
  background: var(--primary);
  color: var(--primary-fg);
  border: 1px solid transparent;
  box-shadow: 0 2px 8px rgba(13, 148, 136, 0.22);
  flex: 1;
}

.btn.primary:hover:not(:disabled) {
  background: var(--primary-hover);
  box-shadow: 0 4px 14px rgba(13, 148, 136, 0.3);
}

.btn.secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
}

.btn.secondary:hover:not(:disabled) {
  background: var(--surface-2);
  border-color: var(--primary);
}

.btn.ghost {
  background: transparent;
  color: var(--text-body);
  border: 1px solid var(--border);
}

.btn.ghost:hover:not(:disabled) {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--border-strong);
}

.btn.full {
  width: 100%;
  padding: 12px;
  font-size: 15px;
}

.empty-state {
  grid-column: 1 / -1;
  background: var(--surface);
  border: 1px dashed var(--border-strong);
  border-radius: 16px;
  padding: 48px 24px;
  text-align: center;
  color: var(--muted);
}

.empty-state a {
  color: var(--primary);
  font-weight: 700;
}

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: var(--backdrop);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.hidden {
  display: none !important;
}

.modal-card {
  width: 100%;
  max-width: 500px;
  max-height: 90vh;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 24px;
  box-shadow: var(--shadow-lg);
}

.modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.modal-title-wrap {
  display: flex;
  align-items: center;
  gap: 12px;
}

.modal-logo {
  width: 44px;
  height: 44px;
  border-radius: 10px;
  object-fit: cover;
  border: 1px solid var(--border);
}

.modal-header h2 {
  margin: 0 0 4px;
  font-size: 19px;
  color: var(--text);
}

.icon-btn {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  width: 34px;
  height: 34px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 16px;
}

.modal-desc {
  margin: 0 0 14px;
  font-size: 13px;
  color: var(--muted);
}

.otp-box {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  margin-bottom: 14px;
}

.otp-label {
  font-size: 12.5px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 8px;
}

.input-row {
  display: flex;
  gap: 8px;
}

.input-row input {
  flex: 1;
  min-width: 0;
  background: var(--input-bg);
  color: var(--text);
  border: 1.5px solid var(--border);
  border-radius: 9px;
  padding: 10px 12px;
  font-size: 14px;
}

.input-row input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

.alert {
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 13px;
  margin-bottom: 12px;
  line-height: 1.45;
}

.alert.error {
  background: var(--danger-bg);
  border: 1px solid var(--danger-border);
  color: var(--danger-fg);
}

.alert.success {
  background: var(--primary-soft);
  border: 1px solid var(--primary-border);
  color: var(--primary);
}

.alert.info {
  background: var(--secondary-soft);
  border: 1px solid var(--secondary-border);
  color: var(--secondary);
}

.ballot-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 16px;
}

.ballot-option {
  width: 100%;
  text-align: left;
  background: var(--surface);
  color: var(--text);
  border: 1.5px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  cursor: pointer;
}

.ballot-option:hover:not(:disabled) {
  border-color: var(--primary);
}

.ballot-option.selected {
  border: 2px solid var(--primary);
  background: var(--primary-soft);
}

.ballot-option:disabled {
  opacity: 0.6;
  cursor: default;
}

.ballot-name {
  font-weight: 700;
  font-size: 15px;
  color: var(--text);
}

.ballot-sub {
  font-size: 12px;
  color: var(--muted);
  margin-top: 3px;
}

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