/* Global sizing reset to reduce mobile overflow */
*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
  font-family: "Arial", sans-serif;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  min-height: 100vh;
  overflow-x: hidden; /* prevent accidental horizontal scroll */
}

/* Make container stretch and center for spacious form view */
html,
body {
  height: 100%;
}

.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  padding: clamp(16px, 4vw, 30px);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  margin-top: 20px;
  margin-bottom: 20px;
  overflow-x: hidden;
}

.page {
  display: none;
}

.page.active {
  display: block;
}

.nav-bar {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-bottom: 30px;
  padding: 20px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 15px;
}

.nav-btn {
  padding: 12px 24px;
  background: linear-gradient(45deg, #667eea, #764ba2);
  color: white;
  border: none;
  border-radius: 25px;
  cursor: pointer;
  font-weight: bold;
  transition: all 0.3s ease;
}

.nav-btn:hover,
.nav-btn.active {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
  background: linear-gradient(45deg, #5a6fd8, #6a4190);
}

.word-cloud {
  position: relative;
  width: 100%;
  height: 60vh;
  min-height: 420px;
  overflow-y: auto;
  overflow-x: hidden;
  border-radius: 15px;
  background: linear-gradient(45deg, #f093fb 0%, #f5576c 50%, #4facfe 100%);
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  align-content: flex-start;
  justify-content: center;
  padding: 20px;
  box-sizing: border-box;
  scrollbar-width: none;
  -ms-overflow-style: none;
  max-width: 100%;
}

/* Floating animation mode */
/* (Removed old floating animation styles) */

.word-cloud::-webkit-scrollbar {
  display: none;
}

.word {
  display: inline-block;
  margin: 5px;
  padding: 8px 12px;
  border-radius: 25px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-weight: bold;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);
  position: relative;
  background: rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(5px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  max-width: 200px;
  text-align: center;
}

.word:hover {
  transform: scale(1.1) rotate(1deg);
  background: rgba(255, 255, 255, 0.9);
  color: #333 !important;
  text-shadow: none;
  z-index: 10;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
}

/* Cloud theme palette menu */
.cloud-theme-menu {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 50;
}
.cloud-theme-toggle {
  background: rgba(255, 255, 255, 0.8);
  border: 1px solid rgba(0, 0, 0, 0.15);
  border-radius: 8px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);
  transition: background 0.2s, transform 0.2s;
}
.cloud-theme-toggle:hover {
  background: #ffffff;
  transform: translateY(-2px);
}
.cloud-theme-panel {
  display: none;
  position: absolute;
  top: 46px;
  right: 0;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(6px);
  padding: 14px 16px 12px;
  border-radius: 14px;
  width: 240px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.25);
  border: 1px solid rgba(0, 0, 0, 0.1);
  font-size: 14px;
}
.cloud-theme-panel.open {
  display: block;
  animation: fadeInTheme 0.18s ease;
}
@keyframes fadeInTheme {
  from {
    opacity: 0;
    transform: translateY(-4px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
.cloud-theme-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  gap: 8px;
}
.cloud-theme-row label {
  font-weight: 600;
  font-size: 12px;
  flex: 1 1 auto;
}
.cloud-theme-row input[type="color"] {
  width: 44px;
  height: 32px;
  padding: 0;
  border: 1px solid #ccc;
  border-radius: 8px;
  cursor: pointer;
  background: #fff;
}
.cloud-theme-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 4px;
}
.cloud-theme-actions button {
  background: linear-gradient(45deg, #667eea, #764ba2);
  color: #fff;
  border: none;
  padding: 6px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 12px;
}
.cloud-theme-actions button:hover {
  filter: brightness(1.1);
}

.cloud-theme-warning {
  font-size: 11px;
  margin: 4px 0 6px;
  line-height: 1.3;
  font-weight: 600;
  color: #d9534f;
  display: none;
}
.cloud-theme-warning.visible {
  display: block;
}
.range-val {
  font-size: 11px;
  font-weight: 600;
  min-width: 34px;
  text-align: right;
}
.cloud-theme-row input[type="range"] {
  flex: 1;
}
body.dark-mode .cloud-theme-warning {
  color: #ff766b;
}

body.dark-mode .cloud-theme-toggle {
  background: rgba(40, 40, 40, 0.85);
  color: #fff;
  border-color: rgba(255, 255, 255, 0.15);
}
body.dark-mode .cloud-theme-toggle:hover {
  background: #333;
}
body.dark-mode .cloud-theme-panel {
  background: rgba(30, 30, 30, 0.95);
  color: #eee;
  border-color: rgba(255, 255, 255, 0.1);
}
body.dark-mode .cloud-theme-row label {
  color: #ddd;
}
body.dark-mode .cloud-theme-row input[type="color"] {
  border-color: #444;
}

/* Email log status badges */
.email-status-badge {
  display: inline-block;
  padding: 2px 6px;
  border-radius: 10px;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.3px;
  background: #ddd;
  color: #222;
}
.email-status-ok {
  background: #28a745;
  color: #fff;
}
.email-status-error {
  background: #d9534f;
  color: #fff;
}
.email-status-exception {
  background: #ff9800;
  color: #fff;
}
.email-status-disabled,
.email-status-no_from,
.email-status-no_targets {
  background: #6c757d;
  color: #fff;
}
.email-status-throttled {
  background: #17a2b8;
  color: #fff;
}
.email-status-skipped {
  background: #999;
  color: #fff;
}

/* Stats */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
  margin-bottom: 30px;
}

/* Controls section styles */
.controls {
  text-align: center;
  margin-bottom: 20px;
}

.controls button {
  margin: 5px 10px;
  padding: 10px 20px;
  background: linear-gradient(45deg, #667eea, #764ba2);
  color: white;
  border: none;
  border-radius: 25px;
  cursor: pointer;
  font-weight: bold;
  transition: all 0.3s ease;
}

.controls button:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
  background: linear-gradient(45deg, #5a6fd8, #6a4190);
}

.controls button.active {
  background: linear-gradient(45deg, #5a6fd8, #6a4190);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.controls .danger {
  background: linear-gradient(45deg, #ff4757, #ff3742);
}

@media (max-width: 768px) {
  .container {
    margin: 10px;
    padding: clamp(14px, 4vw, 22px);
  }
  .form-row {
    grid-template-columns: 1fr;
  }
  .word-cloud {
    height: 55vh;
    min-height: 360px;
    padding: 16px;
  }
  .nav-bar {
    flex-wrap: wrap;
    gap: 12px;
  }
  .nav-btn {
    flex: 1 1 45%;
  }
}

.single-add-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 24px 32px;
  align-items: start;
  margin-top: 10px;
}

/* Larger fields for Add page */
.single-add-grid .fg label {
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.single-add-grid select,
.single-add-grid input[type="text"],
.single-add-grid input[type="number"],
.single-add-grid textarea {
  width: 100%;
  font-size: 1.15rem;
  padding: 16px 18px;
  border-radius: 14px;
  border: 2px solid #d9d9ec;
  background: #ffffff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.06);
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
  resize: vertical;
}

.single-add-grid textarea {
  min-height: 160px;
  line-height: 1.4;
}

.single-add-grid select:focus,
.single-add-grid input:focus,
.single-add-grid textarea:focus {
  outline: none;
  border-color: #764ba2;
  box-shadow: 0 0 0 3px rgba(118, 75, 162, 0.25);
}

.single-add-grid .actions button {
  font-size: 1.25rem;
  padding: 20px;
  border-radius: 18px;
}

/* Center the Add page content with breathing room */
#addPage {
  max-width: 1400px;
  margin: 0 auto;
}

#addPage h1 {
  font-size: 2.5rem;
  text-align: center;
  margin-top: 10px;
  margin-bottom: 30px;
}

@media (min-width: 1500px) {
  #addPage .single-add-grid {
    grid-template-columns: repeat(3, minmax(320px, 1fr));
  }
}

@media (max-width: 800px) {
  .single-add-grid {
    grid-template-columns: 1fr;
    gap: 18px;
  }
  #addPage h1 {
    font-size: 2rem;
  }
}

.single-add-grid .form-group.full-width {
  grid-column: 1 / -1;

  /* Extra-small refinements */
  @media (max-width: 520px) {
    .container {
      padding-left: clamp(10px, 3vw, 16px);
      padding-right: clamp(10px, 3vw, 16px);
    }
    #cloudPage h1,
    #addPage h1,
    #adminPage h1 {
      text-align: center;
    }
    .stats-grid {
      justify-items: center;
    }
    .stats-grid .stat-item {
      width: 100%;
      max-width: 260px;
      margin-left: auto;
      margin-right: auto;
    }
    .controls {
      text-align: center;
    }
    .nav-bar {
      padding: 14px 10px;
    }
    .nav-btn {
      flex: 1 1 48%;
    }
    .manage-filters {
      width: 100%;
    }
  }

  @media (max-width: 380px) {
    .container {
      padding-left: 10px;
      padding-right: 10px;
    }
    .nav-btn {
      flex: 1 1 100%;
    }
    .stats-grid {
      grid-template-columns: 1fr;
    }
  }
}

#wordMetaTooltip {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
  max-width: 240px;
  word-break: break-word;
}

.field-error {
  color: #d9534f;
  font-size: 12px;
  min-height: 14px;
  margin-top: 2px;
}

.manage-filters {
  display: flex;
  gap: 10px;
  margin: 10px 0 20px;
  flex-wrap: wrap;
}
.manage-filters input,
.manage-filters select {
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid #ccc;
  min-width: 160px;
}

/* Responsive table for manage list */
@media (max-width: 900px) {
  #sayingsList table thead {
    display: none;
  }
  #sayingsList table,
  #sayingsList tbody,
  #sayingsList tr,
  #sayingsList td {
    display: block;
    width: 100%;
  }
  #sayingsList tr {
    margin: 0 0 18px;
    background: rgba(255, 255, 255, 0.6);
    padding: 12px 14px;
    border-radius: 12px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
  }
  #sayingsList td {
    border: none !important;
    padding: 4px 0 !important;
    font-size: 0.95rem;
  }
  #sayingsList td:last-child {
    margin-top: 6px;
  }
  #sayingsList button.mini-btn {
    margin-right: 6px;
  }
}

#wordMetaTooltip {
  opacity: 0;
  transition: opacity 0.15s ease;
}
#wordMetaTooltip[style*="display: block"] {
  opacity: 1;
}

/* Migration banner */
.migration-banner {
  background: linear-gradient(90deg, #22c55e, #16a34a);
  color: #fff;
  padding: 14px 20px;
  border-radius: 14px;
  margin-bottom: 18px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  box-shadow: 0 6px 18px -4px rgba(0, 0, 0, 0.25);
}
.migration-banner button.close-banner {
  background: rgba(255, 255, 255, 0.15);
  border: none;
  color: #fff;
  padding: 6px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
}
.migration-banner button.close-banner:hover {
  background: rgba(255, 255, 255, 0.28);
}

/* Dark mode */
@media (prefers-color-scheme: dark) {
  body:not(.light-mode) {
    background: linear-gradient(135deg, #1f2937 0%, #312e81 100%);
  }
}
body.dark-mode {
  background: radial-gradient(circle at 30% 30%, #1e1e2f, #0f0f17 70%);
  color: #e6e6ef;
}
body.dark-mode .container {
  background: rgba(30, 30, 45, 0.9);
}
body.dark-mode .nav-bar {
  background: rgba(255, 255, 255, 0.06);
}
body.dark-mode .nav-btn {
  background: linear-gradient(45deg, #4338ca, #6d28d9);
}
body.dark-mode .nav-btn:hover,
body.dark-mode .nav-btn.active {
  background: linear-gradient(45deg, #4f46e5, #7c3aed);
}
body.dark-mode .stats-grid .stat-item {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.15);
}
body.dark-mode .word-cloud {
  background: linear-gradient(45deg, #312e81, #7e22ce, #3b82f6);
}
body.dark-mode .word {
  background: rgba(255, 255, 255, 0.15);
  border-color: rgba(255, 255, 255, 0.25);
  color: #fff;
}
body.dark-mode .word:hover {
  background: #fff;
  color: #222 !important;
}
body.dark-mode .form-section {
  background: rgba(255, 255, 255, 0.08);
}
body.dark-mode select,
body.dark-mode input,
body.dark-mode textarea {
  background: #1f2230;
  color: #e6e6ef;
  border-color: #3f3f52;
}
body.dark-mode select:focus,
body.dark-mode input:focus,
body.dark-mode textarea:focus {
  border-color: #7c3aed;
  box-shadow: 0 0 0 3px rgba(124, 58, 237, 0.35);
}
body.dark-mode .manage-filters input,
body.dark-mode .manage-filters select {
  background: #1f2230;
  color: #e6e6ef;
  border-color: #3f3f52;
}
body.dark-mode #sayingsList tr {
  background: rgba(255, 255, 255, 0.06);
}
body.dark-mode .migration-banner {
  background: linear-gradient(90deg, #15803d, #166534);
}

/* Responsive overflow prevention */
.manage-filters input,
.manage-filters select {
  max-width: 100%;
}
.word {
  word-break: break-word;
}
.word-cloud {
  -webkit-tap-highlight-color: transparent;
}

@media (max-width: 480px) {
  .nav-bar {
    padding: 12px 14px;
  }
  .nav-btn {
    padding: 10px 14px;
    font-size: 0.9rem;
  }
  .stats-grid {
    gap: 14px;
  }
  .cloud-theme-panel {
    right: 0;
    width: 220px;
  }
}

/* Floating settings cog */
.settings-cog {
  position: fixed;
  top: calc(env(safe-area-inset-top, 0px) + 4px);
  right: calc(env(safe-area-inset-right, 0px) + 6px);
  width: 50px;
  height: 50px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: #fff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 25px;
  cursor: pointer;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
  z-index: 2000;
  user-select: none;
  transition: transform 0.25s ease, box-shadow 0.25s ease;
}
.settings-cog:active {
  transform: scale(0.92);
}
@media (max-width: 640px) {
  .settings-cog {
    width: 46px;
    height: 46px;
    font-size: 23px;
    top: calc(env(safe-area-inset-top, 0px) + 2px);
    right: calc(env(safe-area-inset-right, 0px) + 4px);
  }
}
.settings-cog:hover {
  transform: rotate(25deg) scale(1.05);
}
.settings-cog.open {
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
}

.settings-menu {
  position: absolute;
  top: 60px;
  right: 0;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 18px;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 180px;
  box-shadow: 0 18px 38px -8px rgba(0, 0, 0, 0.35);
  opacity: 0;
  pointer-events: none;
  transform: translateY(-6px);
  transition: opacity 0.25s ease, transform 0.25s ease;
}
.settings-cog.open .settings-menu {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}
.settings-menu button {
  background: linear-gradient(45deg, #667eea, #764ba2);
  color: #fff;
  border: none;
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  text-align: left;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);
}
.settings-menu button:hover {
  background: linear-gradient(45deg, #5a6fd8, #6a4190);
}
body.dark-mode .settings-cog {
  background: linear-gradient(135deg, #4338ca, #6d28d9);
}
body.dark-mode .settings-menu {
  background: rgba(30, 30, 45, 0.95);
}
body.dark-mode .settings-menu button {
  background: linear-gradient(45deg, #4338ca, #6d28d9);
}
body.dark-mode .settings-menu button:hover {
  background: linear-gradient(45deg, #4f46e5, #7c3aed);
}

/* Admin lists */
.admin-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 10px;
}
.admin-pill {
  background: linear-gradient(45deg, #667eea, #764ba2);
  color: #fff;
  padding: 8px 14px;
  border-radius: 999px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.95rem;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);
}
.admin-pill button.remove-pill {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: #fff;
  cursor: pointer;
  border-radius: 50%;
  width: 24px;
  height: 24px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.admin-pill button.remove-pill:hover {
  background: rgba(255, 255, 255, 0.35);
}
body.dark-mode .admin-pill {
  background: linear-gradient(45deg, #4338ca, #6d28d9);
}
body.dark-mode .admin-pill button.remove-pill {
  background: rgba(255, 255, 255, 0.2);
}
body.dark-mode .admin-pill button.remove-pill:hover {
  background: rgba(255, 255, 255, 0.35);
}

/* Theme transition */
body,
body * {
  transition: background-color 0.35s ease, color 0.35s ease,
    border-color 0.35s ease;
}

/* --- Vertical stacked layout for Add form (override previous grid) --- */
.single-add-grid {
  display: flex;
  flex-direction: column;
  max-width: 900px;
  margin: 10px auto 50px;
  gap: 28px;
}

.single-add-grid .fg {
  width: 100%;
}

.single-add-grid textarea {
  min-height: 200px;
}

@media (min-width: 1200px) {
  .single-add-grid {
    max-width: 1000px;
    gap: 32px;
  }
  #addPage h1 {
    margin-bottom: 40px;
  }
}
