:root {
  --bg-primary: #0f172a;
  --bg-secondary: #1e293b;
  --bg-card: rgba(30, 41, 59, 0.7);
  --bg-card-hover: rgba(51, 65, 85, 0.8);
  --border-color: rgba(255, 255, 255, 0.1);
  --text-primary: #f8fafc;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --brand-green: #10b981;
  --brand-green-hover: #059669;
  --brand-blue: #3b82f6;
  --brand-yellow: #f59e0b;
  --brand-red: #ef4444;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.2);
  --shadow-md: 0 4px 20px rgba(0, 0, 0, 0.3);
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  color-scheme: dark;
}

/* Telegram WebApp & Light Theme Adaptations */
@media (prefers-color-scheme: light) {
  :root:not(.dark) {
    --bg-primary: #f8fafc;
    --bg-secondary: #ffffff;
    --bg-card: rgba(255, 255, 255, 0.9);
    --bg-card-hover: #f1f5f9;
    --border-color: rgba(0, 0, 0, 0.1);
    --text-primary: #0f172a;
    --text-secondary: #475569;
    --text-muted: #94a3b8;
    color-scheme: light;
  }
}

body.telegram-app {
  --bg-primary: var(--tg-theme-bg-color, #0f172a);
  --bg-secondary: var(--tg-theme-secondary-bg-color, #1e293b);
  --text-primary: var(--tg-theme-text-color, #f8fafc);
  --text-secondary: var(--tg-theme-hint-color, #94a3b8);
  --brand-blue: var(--tg-theme-button-color, #3b82f6);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: var(--font-sans);
  background-color: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding-top: max(12px, env(safe-area-inset-top));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  padding-left: max(12px, env(safe-area-inset-left));
  padding-right: max(12px, env(safe-area-inset-right));
}

.container {
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
}

/* Header */
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  margin-bottom: 16px;
  backdrop-filter: blur(10px);
}

.logo-area {
  display: flex;
  align-items: center;
  gap: 10px;
}

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

.logo-title {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.logo-subtitle {
  font-size: 11px;
  color: var(--text-secondary);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 500;
  border-radius: 20px;
  background: rgba(16, 185, 129, 0.15);
  color: var(--brand-green);
  border: 1px solid rgba(16, 185, 129, 0.3);
}

.status-dot {
  width: 8px;
  height: 8px;
  background-color: var(--brand-green);
  border-radius: 50%;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% { transform: scale(0.95); opacity: 0.8; }
  50% { transform: scale(1.2); opacity: 1; }
  100% { transform: scale(0.95); opacity: 0.8; }
}

.btn-icon {
  background: transparent;
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  border-radius: var(--radius-sm);
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
  font-size: 16px;
}

.btn-icon:hover {
  background: rgba(255, 255, 255, 0.08);
}

/* Controls & Filters Bar */
.controls-bar {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 16px;
}

.search-box {
  position: relative;
  width: 100%;
}

.search-icon {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  font-size: 15px;
}

.search-input {
  width: 100%;
  padding: 12px 14px 12px 40px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-size: 15px;
  outline: none;
  transition: border-color 0.2s;
}

.search-input:focus {
  border-color: var(--brand-green);
}

.search-clear {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 16px;
  display: none;
}

.warehouse-pills {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
  scrollbar-width: none;
}

.warehouse-pills::-webkit-scrollbar {
  display: none;
}

.pill-btn {
  padding: 8px 16px;
  border-radius: 20px;
  border: 1px solid var(--border-color);
  background: var(--bg-secondary);
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.2s;
}

.pill-btn:hover {
  background: rgba(255, 255, 255, 0.08);
}

.pill-btn.active {
  background: var(--brand-green);
  color: #ffffff;
  border-color: var(--brand-green);
  box-shadow: 0 2px 10px rgba(16, 185, 129, 0.3);
}

/* Stock Meta Bar */
.meta-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 12px;
  padding: 0 4px;
}

/* Stock Cards / Table */
.stock-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.stock-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
  cursor: pointer;
  transition: transform 0.15s, background-color 0.15s, border-color 0.15s;
  backdrop-filter: blur(10px);
}

.stock-card:hover {
  background: var(--bg-card-hover);
  border-color: rgba(16, 185, 129, 0.4);
  transform: translateY(-1px);
}

.stock-card:active {
  transform: scale(0.99);
}

.stock-main {
  flex: 1;
  min-width: 0;
}

.stock-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-bottom: 6px;
  min-width: 0;
}

.code-badge {
  font-family: monospace;
  font-size: 11px;
  font-weight: 600;
  background: rgba(255, 255, 255, 0.08);
  padding: 2px 7px;
  border-radius: var(--radius-sm);
  color: var(--brand-blue);
  border: 1px solid rgba(59, 130, 246, 0.25);
  white-space: nowrap !important;
  flex-shrink: 0;
}

.warehouse-tag {
  font-size: 11px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 140px;
  flex-shrink: 1;
}

.stock-title {
  font-size: 15px;
  font-weight: 500;
  line-height: 1.35;
  color: var(--text-primary);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.stock-quantities {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
  flex-shrink: 0;
  min-width: 75px;
}

.qty-badge {
  font-size: 15px;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: var(--radius-sm);
  background: rgba(16, 185, 129, 0.2);
  color: var(--brand-green);
  border: 1px solid rgba(16, 185, 129, 0.4);
  white-space: nowrap;
}

.qty-badge.low {
  background: rgba(239, 68, 68, 0.2);
  color: var(--brand-red);
  border-color: rgba(239, 68, 68, 0.4);
}

.qty-badge.warning {
  background: rgba(245, 158, 11, 0.2);
  color: var(--brand-yellow);
  border-color: rgba(245, 158, 11, 0.4);
}

.qty-dst {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
}

/* Modals */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

.modal-overlay.open {
  opacity: 1;
  pointer-events: auto;
}

.modal-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  max-width: 650px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow-md);
  transform: translateY(20px);
  transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1);
  display: flex;
  flex-direction: column;
}

.modal-overlay.open .modal-card {
  transform: translateY(0);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 18px 20px;
  border-bottom: 1px solid var(--border-color);
}

.modal-body {
  padding: 20px;
}

.chart-controls {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-bottom: 16px;
}

.chart-range-btn {
  padding: 6px 14px;
  font-size: 13px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s;
}

.chart-range-btn.active {
  background: var(--brand-blue);
  color: #fff;
  border-color: var(--brand-blue);
}

.chart-container {
  position: relative;
  height: 280px;
  width: 100%;
}

/* Settings Form */
.form-group {
  margin-bottom: 18px;
}

.form-label {
  display: block;
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 8px;
}

.form-select, .form-input {
  width: 100%;
  padding: 10px 14px;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 14px;
  outline: none;
}

.form-toggle {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--border-color);
}

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0; left: 0; right: 0; bottom: 0;
  background-color: #475569;
  transition: 0.3s;
  border-radius: 24px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 18px;
  width: 18px;
  left: 3px;
  bottom: 3px;
  background-color: white;
  transition: 0.3s;
  border-radius: 50%;
}

input:checked + .slider {
  background-color: var(--brand-green);
}

input:checked + .slider:before {
  transform: translateX(20px);
}

.btn-primary {
  width: 100%;
  padding: 12px;
  background: var(--brand-green);
  color: #ffffff;
  border: none;
  border-radius: var(--radius-md);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.btn-primary:hover {
  background: var(--brand-green-hover);
}

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

.btn-force-poll {
  background: #3b82f6;
  margin-top: 10px;
}

.btn-force-poll:hover {
  background: #2563eb;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: #1e293b;
  border: 1px solid var(--brand-green);
  color: #f8fafc;
  padding: 12px 24px;
  border-radius: var(--radius-md);
  font-size: 14px;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  z-index: 2000;
  opacity: 0;
  transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.3s;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

.loading-spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: #fff;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* --- Search & Sort Controls --- */
.search-sort-row {
  display: flex;
  gap: 8px;
  width: 100%;
}

.search-box {
  flex: 1;
  position: relative;
  min-width: 0;
}

.sort-box {
  flex-shrink: 0;
  width: 190px;
}

.sort-select {
  width: 100%;
  height: 100%;
  padding: 10px 12px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-size: 13px;
  font-weight: 500;
  outline: none;
  cursor: pointer;
  transition: border-color 0.2s;
}

.sort-select:focus {
  border-color: var(--brand-green);
}

@media (max-width: 600px) {
  .search-sort-row {
    flex-direction: column;
    gap: 8px;
  }
  .sort-box {
    width: 100%;
  }
}

/* --- Auto Refresh & Mini Progress Bar --- */
.auto-refresh-container {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
}

.refresh-progress-track {
  width: 100px;
  height: 4px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 2px;
  overflow: hidden;
}

.refresh-progress-bar {
  width: 0%;
  height: 100%;
  background: var(--brand-green);
  border-radius: 2px;
  transition: width 0.25s linear;
}

/* --- Floating Scroll-To-Top Button --- */
.scroll-top-btn {
  position: fixed;
  bottom: 24px;
  right: 20px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--brand-green);
  color: #ffffff;
  border: none;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
  font-size: 20px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  opacity: 0;
  visibility: hidden;
  transform: translateY(20px);
  transition: opacity 0.25s, transform 0.25s, visibility 0.25s, background-color 0.2s;
  z-index: 999;
}

.scroll-top-btn.visible {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.scroll-top-btn:hover {
  background: var(--brand-green-hover);
  transform: translateY(-2px);
}

/* --- Secondary & Danger Buttons --- */
.btn-secondary {
  padding: 8px 14px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-secondary:hover {
  background: rgba(255, 255, 255, 0.14);
}

.btn-danger {
  width: 100%;
  padding: 10px 14px;
  background: rgba(239, 68, 68, 0.15);
  border: 1px solid rgba(239, 68, 68, 0.4);
  color: #ef4444;
  border-radius: var(--radius-md);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-danger:hover {
  background: #ef4444;
  color: #ffffff;
}

/* --- ntfy Quick Connect Box --- */
.ntfy-quick-box {
  background: rgba(16, 185, 129, 0.06);
  border: 1px dashed rgba(16, 185, 129, 0.35);
  border-radius: var(--radius-md);
  padding: 14px;
}

.btn-ntfy-connect {
  width: 100%;
  padding: 12px 14px;
  background: #0ea5e9;
  color: #ffffff;
  border: none;
  border-radius: var(--radius-md);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: background 0.2s;
  margin-bottom: 8px;
}

.btn-ntfy-connect:hover {
  background: #0284c7;
}

.ntfy-sub-actions {
  display: flex;
  gap: 6px;
  margin-bottom: 8px;
}

.ntfy-sub-actions .btn-secondary {
  flex: 1;
  font-size: 12px;
  padding: 8px 6px;
  white-space: nowrap;
  text-align: center;
}

.ntfy-topic-hint {
  font-size: 11px;
  color: var(--text-muted);
  word-break: break-all;
}

/* --- Section Headers --- */
.form-section-header {
  font-size: 14px;
  font-weight: 700;
  color: var(--brand-green);
  margin: 18px 0 10px 0;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border-color);
}

/* --- Admin Section & User List --- */
.admin-section {
  border-top: 2px dashed rgba(245, 158, 11, 0.3);
  padding-top: 14px;
  margin-top: 16px;
}

.admin-users-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 200px;
  overflow-y: auto;
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 8px;
}

.user-item-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 8px;
  background: var(--bg-card);
  border-radius: 6px;
  font-size: 12px;
}

.user-item-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.user-item-role {
  font-size: 10px;
  padding: 1px 5px;
  border-radius: 4px;
  font-weight: 600;
  display: inline-block;
  width: fit-content;
}

.user-item-role.admin {
  background: rgba(245, 158, 11, 0.2);
  color: #f59e0b;
}

.user-item-role.user {
  background: rgba(255, 255, 255, 0.1);
  color: var(--text-secondary);
}

.btn-user-delete {
  padding: 4px 8px;
  background: rgba(239, 68, 68, 0.15);
  border: 1px solid rgba(239, 68, 68, 0.4);
  color: #ef4444;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}

.btn-user-delete:hover {
  background: #ef4444;
  color: #fff;
}

/* --- Badges for demand in stock cards --- */
.demand-badge {
  font-size: 11px;
  font-weight: 600;
  color: #f97316;
  background: rgba(249, 115, 22, 0.15);
  border: 1px solid rgba(249, 115, 22, 0.3);
  padding: 2px 7px;
  border-radius: 4px;
  white-space: nowrap !important;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  gap: 3px;
  line-height: 1.2;
}

.new-badge {
  font-size: 11px;
  font-weight: 600;
  color: #10b981;
  background: rgba(16, 185, 129, 0.15);
  border: 1px solid rgba(16, 185, 129, 0.3);
  padding: 2px 7px;
  border-radius: 4px;
  white-space: nowrap !important;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  gap: 3px;
  line-height: 1.2;
}

/* Mobile responsive refinements */
@media (max-width: 480px) {
  body {
    padding-left: 8px;
    padding-right: 8px;
  }
  header {
    padding: 10px 12px;
  }
  .logo-title {
    font-size: 16px;
  }
  .logo-subtitle {
    font-size: 10px;
  }
  .btn-icon {
    width: 32px;
    height: 32px;
    font-size: 14px;
  }
  .warehouse-tag {
    max-width: 110px;
  }
  .stock-card {
    padding: 10px 12px;
    gap: 8px;
  }
  .stock-title {
    font-size: 13.5px;
  }
  .qty-badge {
    font-size: 14px;
    padding: 2px 8px;
  }
  .qty-dst {
    font-size: 10px;
  }
  .meta-bar {
    font-size: 12px;
  }
  .ntfy-sub-actions {
    flex-wrap: wrap;
  }
  .ntfy-sub-actions .btn-secondary {
    flex: 1 1 calc(50% - 4px);
  }
}

