:root {
  --bg: #f3f5f8;
  --card: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --border: #d1d5db;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: radial-gradient(circle at top, #e7efff 0%, var(--bg) 42%);
  min-height: 100vh;
}

.page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.card {
  width: 100%;
  max-width: 420px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 10px 40px rgba(15, 23, 42, 0.08);
}

h1 {
  margin: 0 0 8px 0;
  font-size: 20px;
}

.title-with-icon {
  display: flex;
  align-items: center;
  gap: 10px;
}

.title-with-icon i {
  color: var(--primary);
  font-size: 16px;
}

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

label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
  font-size: 13px;
}

.field {
  margin-bottom: 16px;
}

.input-with-icon {
  position: relative;
}

.input-with-icon i {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  pointer-events: none;
}

input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 11px 12px 11px 38px;
  font-size: 13px;
}

input:focus {
  outline: none;
  border-color: var(--primary);
}

.input-with-icon:focus-within i {
  color: var(--primary);
}

button {
  width: 100%;
  border: 0;
  border-radius: 8px;
  padding: 11px 14px;
  font-size: 13px;
  font-weight: 600;
  color: #fff;
  background: var(--primary);
  cursor: pointer;
}

button:hover {
  background: var(--primary-hover);
}

button:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.action-row {
  display: flex;
  gap: 10px;
}

.action-row button {
  flex: 1;
}

.btn-secondary {
  background: #475569;
}

.btn-secondary:hover {
  background: #334155;
}

.status-message {
  min-height: 20px;
  margin-top: 12px;
  font-size: 12px;
}

.status-message.error {
  color: var(--danger);
}

.status-message.info {
  color: var(--muted);
}

.main {
  max-width: 860px;
  margin: 40px auto;
  padding: 0 24px;
}

body[data-page] {
  background: linear-gradient(145deg, #e2e8f0 0%, #f8fafc 52%, #dbeafe 100%);
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
}

.sidebar {
  background: linear-gradient(180deg, #f8fafc 0%, #eef2ff 100%);
  color: #334155;
  border-right: 1px solid #dbe2ee;
  padding: 24px 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.sidebar-top {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.logo-frame {
  min-height: 52px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.logo-frame img {
  max-width: 100%;
  max-height: 52px;
  object-fit: contain;
}

.tenant-meta {
  background: #e2e8f0;
  border-radius: 10px;
  padding: 10px 12px;
}

.tenant-label {
  color: #64748b;
  font-size: 11px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.tenant-value {
  font-size: 15px;
  font-weight: 700;
  color: #0f172a;
  margin-top: 2px;
}

.menu {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.menu-link {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 10px;
  color: #475569;
  text-decoration: none;
  font-weight: 600;
  font-size: 13px;
  transition: background-color 0.2s ease, color 0.2s ease, transform 0.2s ease;
}

.menu-link:hover {
  color: #0f172a;
  background: #e2e8f0;
  transform: translateX(2px);
}

.menu-link.active {
  color: #ffffff;
  background: linear-gradient(90deg, #2563eb, #1d4ed8);
  box-shadow: 0 8px 24px rgba(37, 99, 235, 0.35);
}

.menu-link i {
  width: 20px;
  text-align: center;
}

.logout-btn {
  margin-top: auto;
  width: auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: #fee2e2;
  border: 1px solid #fecaca;
  color: #b91c1c;
  font-size: 13px;
}

.logout-btn:hover {
  background: rgba(239, 68, 68, 0.3);
}

.content {
  padding: 28px clamp(20px, 4vw, 48px);
}

.content-header h1 {
  margin: 0;
  font-size: clamp(22px, 2.8vw, 28px);
}

.content-header p {
  margin: 8px 0 0;
  color: #475569;
  font-size: 13px;
}

.panel {
  margin-top: 20px;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid #cbd5e1;
  border-radius: 14px;
  padding: 20px;
  box-shadow: 0 14px 34px rgba(15, 23, 42, 0.08);
}

.panel h2 {
  margin-top: 0;
  font-size: 17px;
}

.panel p {
  font-size: 13px;
}

.hidden {
  display: none !important;
}

.content-header-inline {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

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

.auto-refresh-switch {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 7px 10px;
  border: 1px solid #cbd5e1;
  border-radius: 999px;
  background: #ffffff;
  color: #334155;
  font-size: 12px;
  cursor: pointer;
  margin-bottom: 0;
}

.auto-refresh-switch input {
  opacity: 0;
  pointer-events: none;
  width: 0;
  height: 0;
  margin: 0;
}

.switch-track {
  width: 34px;
  height: 20px;
  border-radius: 999px;
  background: #cbd5e1;
  position: relative;
  transition: background 0.2s ease;
}

.switch-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 999px;
  background: #ffffff;
  box-shadow: 0 1px 4px rgba(15, 23, 42, 0.25);
  transition: transform 0.2s ease;
}

.auto-refresh-switch input:checked + .switch-track {
  background: #2563eb;
}

.auto-refresh-switch input:checked + .switch-track .switch-thumb {
  transform: translateX(14px);
}

.switch-label {
  line-height: 1.2;
}

.btn-refresh {
  width: auto;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 9px 12px;
}

.metrics-grid {
  margin-top: 18px;
  display: grid;
  gap: 14px;
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.metric-card {
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid #cbd5e1;
  border-radius: 14px;
  padding: 14px;
  box-shadow: 0 10px 24px rgba(15, 23, 42, 0.06);
  position: relative;
  overflow: hidden;
  opacity: 0;
  transform: translateY(10px);
  animation: section-fade-in 0.35s ease forwards;
}

.metric-card.loading::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(110deg, transparent 8%, rgba(226, 232, 240, 0.35) 18%, transparent 33%);
  transform: translateX(-100%);
  animation: shimmer 1.2s linear infinite;
  pointer-events: none;
}

.metric-title {
  color: #475569;
  font-weight: 600;
  font-size: 12px;
  margin-bottom: 6px;
}

.metric-number {
  font-size: clamp(24px, 2vw, 30px);
  font-weight: 700;
  line-height: 1.1;
}

.metric-breakdown {
  list-style: none;
  padding: 0;
  margin: 10px 0 0;
  display: grid;
  gap: 4px;
}

.metric-breakdown li {
  display: grid;
  grid-template-columns: max-content auto;
  gap: 6px;
  color: #334155;
  font-size: 12px;
  align-items: center;
  justify-content: start;
}

.metric-breakdown li span {
  white-space: nowrap;
}

.metric-breakdown li strong {
  font-weight: 700;
  color: #0f172a;
}

.metric-breakdown .muted {
  color: #64748b;
}

.metric-loading {
  margin-top: 10px;
  color: #64748b;
  font-size: 12px;
  display: none;
}

.metric-card.loading .metric-loading {
  display: block;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.panel-head h2 {
  margin: 0;
}

.filters-row {
  margin-top: 14px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: end;
}

.filter-item {
  min-width: 150px;
}

.filter-item.grow {
  flex: 1;
  min-width: 220px;
}

.filters-row label {
  font-size: 12px;
  margin-bottom: 6px;
}

select {
  width: 100%;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  background: #ffffff;
  color: #1f2937;
  padding: 9px 10px;
  font-size: 13px;
}

.filters-row input {
  padding: 9px 10px;
}

.list-loading {
  margin-top: 14px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: #475569;
  font-size: 13px;
}

.empty-state {
  margin-top: 14px;
  color: #64748b;
  font-size: 13px;
}

.tenant-sections {
  margin-top: 14px;
  display: grid;
  gap: 14px;
}

.tenant-section {
  border: 1px solid #dbe2ee;
  border-radius: 12px;
  overflow: hidden;
  background: #ffffff;
  box-shadow: 0 8px 20px rgba(15, 23, 42, 0.05);
  opacity: 0;
  transform: translateY(8px);
  animation: section-fade-in 0.28s ease forwards;
}

.tenant-section-title {
  background: linear-gradient(90deg, #eff6ff, #f8fafc);
  border-bottom: 1px solid #dbe2ee;
  padding: 10px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.tenant-section-title h3 {
  margin: 0;
  font-size: 13px;
}

.tenant-section-title span {
  color: #64748b;
  font-size: 12px;
}

.section-title-tools {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.section-toggle {
  width: 22px;
  height: 22px;
  border-radius: 6px;
  border: 1px solid #cbd5e1;
  background: #ffffff;
  color: #475569;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.section-toggle:hover {
  background: #e2e8f0;
  color: #1e293b;
}

.ap-table-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.ap-table {
  width: max-content;
  min-width: 100%;
  border-collapse: collapse;
}

.ap-table th,
.ap-table td {
  padding: 10px 12px;
  border-bottom: 1px solid #eef2f7;
  text-align: left;
  font-size: 12px;
  white-space: nowrap;
}

.ap-table th {
  color: #64748b;
  font-weight: 700;
  background: #fcfdff;
}

.ap-table .actions-col,
.ap-table .actions-cell {
  width: 84px;
  min-width: 84px;
  max-width: 84px;
}

.status-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
}

.status-badge.online {
  color: #14532d;
  background: #dcfce7;
}

.status-badge.offline {
  color: #7f1d1d;
  background: #fee2e2;
}

.actions-cell {
  white-space: nowrap;
}

.icon-action {
  width: 24px;
  height: 24px;
  padding: 0;
  margin-right: 4px;
  border-radius: 6px;
  border: 1px solid #dbe2ee;
  background: #f8fafc;
  color: #64748b;
}

.icon-action:last-child {
  margin-right: 0;
}

.icon-action[disabled] {
  opacity: 0.45;
  cursor: not-allowed;
}

.tenant-manage-panel {
  margin-top: 16px;
}

.tm-form-grid {
  margin-top: 12px;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  align-items: end;
}

.tm-field label {
  font-size: 12px;
  margin-bottom: 6px;
}

.tm-field input {
  padding: 9px 10px;
}

.tm-form-actions {
  display: flex;
  justify-content: flex-start;
}

.inline-note {
  margin-top: 10px;
  font-size: 12px;
  color: #475569;
  min-height: 18px;
}

.inline-note.success {
  color: #166534;
}

.inline-note.error {
  color: #b91c1c;
}

.tm-actions-cell {
  white-space: nowrap;
}

.tm-action-btn {
  width: auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  margin-right: 6px;
  border-radius: 8px;
  border: 1px solid #cbd5e1;
  background: #f8fafc;
  color: #334155;
  font-size: 12px;
  cursor: pointer;
}

.tm-action-btn:last-child {
  margin-right: 0;
}

.tm-action-btn:hover {
  background: #e2e8f0;
}

.tm-action-btn.warning {
  border-color: #facc15;
  background: #fef9c3;
  color: #854d0e;
}

.tm-action-btn.warning:hover {
  background: #fde68a;
}

.tm-action-btn.danger {
  border-color: #fecaca;
  background: #fee2e2;
  color: #991b1b;
}

.tm-action-btn.danger:hover {
  background: #fecaca;
}

.selected-pill {
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  border-radius: 999px;
  background: #eff6ff;
  color: #1e40af;
  font-size: 12px;
  font-weight: 600;
}

.ai-batch-grid {
  margin-top: 12px;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  align-items: end;
}

.ai-batch-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-start;
}

.checkbox-col {
  width: 40px;
  min-width: 40px;
  max-width: 40px;
  text-align: center;
}

.checkbox-col input {
  width: 14px;
  height: 14px;
}

.sort-btn {
  width: auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 0;
  padding: 0;
  background: transparent;
  color: inherit;
  font-size: 12px;
  font-weight: 700;
}

.sort-btn i {
  font-size: 11px;
  color: #94a3b8;
}

.sort-btn:hover {
  background: transparent;
  color: inherit;
}

.ai-modal-card {
  max-width: 420px;
}

.tm-field input[type='file'] {
  padding: 8px 10px;
}

.fm-upload-grid {
  margin-top: 12px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: end;
}

.fm-upload-actions {
  display: inline-flex;
}

.fm-list-head {
  gap: 10px;
}

.fm-list-tools {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.fm-select-all-wrap {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 0;
  font-size: 12px;
  font-weight: 500;
  color: #334155;
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
}

.fm-select-all-wrap input {
  display: block;
  width: 14px;
  height: 14px;
  margin: 0;
  padding: 0;
}

.tm-modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
  padding: 16px;
}

.tm-modal-card {
  width: 100%;
  max-width: 380px;
  background: #ffffff;
  border-radius: 12px;
  border: 1px solid #dbe2ee;
  box-shadow: 0 18px 38px rgba(15, 23, 42, 0.18);
  padding: 16px;
}

.tm-modal-card h3 {
  margin: 0 0 12px;
  font-size: 16px;
}

.tm-modal-actions {
  margin-top: 12px;
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

@keyframes section-fade-in {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes shimmer {
  to {
    transform: translateX(100%);
  }
}

@media (max-width: 960px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    gap: 14px;
  }

  .menu {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .logout-btn {
    width: 100%;
  }

  .metrics-grid {
    grid-template-columns: 1fr;
  }

  .content-header-inline {
    flex-direction: column;
    align-items: flex-start;
  }

  .header-actions {
    width: 100%;
    flex-direction: column;
    align-items: stretch;
  }

  .auto-refresh-switch {
    justify-content: center;
  }

  .btn-refresh {
    width: 100%;
    justify-content: center;
  }

  .tm-form-grid {
    grid-template-columns: 1fr;
  }

  .tm-form-actions {
    justify-content: stretch;
  }

  .ai-batch-grid {
    grid-template-columns: 1fr;
  }

  .ai-batch-actions {
    flex-direction: column;
    align-items: stretch;
  }

  .fm-upload-grid {
    grid-template-columns: 1fr;
  }

  .fm-upload-actions {
    justify-content: stretch;
  }

  .fm-upload-actions .tm-action-btn {
    width: 100%;
    justify-content: center;
  }
}

@media (max-width: 640px) {
  .menu {
    grid-template-columns: 1fr;
  }

  .content {
    padding: 20px 16px;
  }

  .filters-row {
    flex-direction: column;
    align-items: stretch;
  }

  .filter-item,
  .filter-item.grow {
    min-width: 100%;
  }

  .ap-table th,
  .ap-table td {
    padding: 8px 10px;
  }

  .fm-list-tools {
    width: 100%;
    justify-content: flex-start;
  }
}
