:root {
  --gray-50: #f9fafb;
  --gray-100: #f3f4f6;
  --gray-200: #e5e7eb;
  --gray-300: #d1d5db;
  --gray-500: #6b7280;
  --gray-700: #374151;
  --gray-900: #111827;
  --surface: #ffffff;
  --hover-row: #f7faff;
  --zebra-row: #fbfcfd;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #eff6ff;
  --green: #16a34a;
  --green-soft: #f0fdf4;
  --amber: #d97706;
  --amber-soft: #fffbeb;
  --red: #dc2626;
  --red-soft: #fef2f2;
  --radius-panel: 9px;
  --radius-sm: 6px;
  --shadow-card: 0 1px 3px rgba(16, 24, 40, 0.06), 0 1px 2px rgba(16, 24, 40, 0.04);
  --shadow-modal: 0 20px 48px rgba(16, 24, 40, 0.22);
  --overlay-bg: #10182888;
}

*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--gray-50);
  color: var(--gray-700);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

@media print {
  body { display: none !important; }
}

.tabular {
  font-variant-numeric: tabular-nums;
}

.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

.hidden {
  display: none !important;
}

/* Focus visibility */
button:focus-visible,
input:focus-visible,
select:focus-visible,
a:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  padding: 0 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--gray-200);
  background: var(--surface);
  color: var(--gray-700);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 140ms ease, border-color 140ms ease, transform 100ms ease;
  white-space: nowrap;
}
.btn:hover:not(:disabled) {
  background: var(--gray-100);
  border-color: var(--gray-300);
}
.btn:active:not(:disabled) {
  transform: translateY(1px);
}
.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}
.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #ffffff;
}
.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}
.btn-danger {
  background: var(--red-soft);
  border-color: #fecaca;
  color: var(--red);
}
.btn-danger:hover:not(:disabled) {
  background: #fee2e2;
  border-color: #fca5a5;
}
.btn-success {
  background: var(--green-soft);
  border-color: #bbf7d0;
  color: var(--green);
}
.btn-success:hover:not(:disabled) {
  background: #dcfce7;
}
.btn-sm {
  min-height: 32px;
  padding: 0 10px;
  font-size: 12px;
}
.btn-icon {
  min-width: 36px;
  padding: 0 8px;
}

/* Badges */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  line-height: 1.4;
  white-space: nowrap;
}
.badge-blue { background: var(--primary-soft); color: var(--primary); }
.badge-green { background: var(--green-soft); color: var(--green); }
.badge-amber { background: var(--amber-soft); color: var(--amber); }
.badge-red { background: var(--red-soft); color: var(--red); }
.badge-gray { background: var(--gray-100); color: var(--gray-500); }

/* ================= LOGIN SCREEN ================= */
.login-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
}
.login-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-panel);
  box-shadow: var(--shadow-card);
  padding: 32px 28px;
}
.brand-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 24px;
}
.brand-mark {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-panel);
  background: var(--primary);
  color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 16px;
  flex-shrink: 0;
}
.brand-title {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
  color: var(--gray-900);
  line-height: 1.25;
}
.brand-sub {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--gray-500);
}
.form-group {
  margin-bottom: 16px;
}
.form-label {
  display: block;
  font-size: 14px;
  font-weight: 500;
  color: var(--gray-900);
  margin-bottom: 6px;
}
.form-label .req {
  color: var(--red);
  margin-left: 2px;
}
.form-input, .form-select {
  width: 100%;
  min-height: 40px;
  padding: 8px 12px;
  border: 1px solid var(--gray-300);
  border-radius: var(--radius-sm);
  font-size: 14px;
  color: var(--gray-900);
  background: var(--surface);
}
.form-input:hover, .form-select:hover {
  border-color: var(--gray-500);
}
.form-help {
  margin-top: 4px;
  font-size: 12px;
  color: var(--gray-500);
}
.alert-box {
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  margin-bottom: 16px;
  border: 1px solid transparent;
}
.alert-error {
  background: var(--red-soft);
  border-color: #fecaca;
  color: #991b1b;
}
.alert-info {
  background: var(--primary-soft);
  border-color: #bfdbfe;
  color: #1e40af;
}
.alert-warn {
  background: var(--amber-soft);
  border-color: #fde68a;
  color: #92400e;
}
.login-security-note {
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--gray-200);
  font-size: 12px;
  color: var(--gray-500);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* ================= APP SHELL ================= */
.app-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--surface);
  border-bottom: 1px solid var(--gray-200);
  padding: 0 24px;
  min-height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.topbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}
.topbar-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 700;
  color: var(--gray-900);
  white-space: nowrap;
}
.topbar-brand .dot {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  background: var(--primary);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
}
.nav-tabs {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-left: 8px;
}
.nav-tab {
  min-height: 36px;
  padding: 0 12px;
  border-radius: var(--radius-sm);
  border: none;
  background: transparent;
  color: var(--gray-500);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
}
.nav-tab:hover {
  background: var(--gray-100);
  color: var(--gray-900);
}
.nav-tab.active {
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: 600;
}
.topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}
.user-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--gray-100);
  font-size: 12px;
  color: var(--gray-700);
}

/* Main Content */
.main-wrap {
  width: 100%;
  max-width: 1280px;
  margin: 0 auto;
  padding: 20px 24px 48px;
  flex: 1;
}

.security-ribbon {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px 14px;
  margin-bottom: 16px;
  background: var(--surface);
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-panel);
  font-size: 12px;
  color: var(--gray-700);
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 16px;
}
.root-pills {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}
.agent-bar {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 10px;
  margin-bottom: 16px;
}
.agent-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-panel);
  cursor: pointer;
  text-align: left;
  transition: border-color 140ms ease, background-color 140ms ease;
}
.agent-card:hover {
  border-color: var(--gray-300);
  background: var(--hover-row);
}
.agent-card.active {
  border-color: var(--primary);
  background: var(--primary-soft);
}
.agent-card.offline {
  opacity: 0.62;
}
.status-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}
.dot-online { background: var(--green); }
.dot-offline { background: var(--gray-300); }
.checkbox-group {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding: 8px 10px;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-sm);
  background: var(--surface);
}
.checkbox-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  cursor: pointer;
}
.search-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  max-width: 360px;
  min-width: 200px;
}

.breadcrumbs {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 12px;
  font-size: 14px;
}
.crumb-btn {
  border: none;
  background: transparent;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  color: var(--primary);
  font-size: 14px;
  cursor: pointer;
}
.crumb-btn:hover { background: var(--primary-soft); }
.crumb-sep { color: var(--gray-300); }
.crumb-current { color: var(--gray-900); font-weight: 600; padding: 4px 6px; }

/* Card & File Table */
.panel-card {
  background: var(--surface);
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-panel);
  box-shadow: var(--shadow-card);
  overflow: hidden;
}
.table-scroll {
  width: 100%;
  overflow-x: auto;
}
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}
.data-table th {
  height: 37px;
  padding: 0 12px;
  text-align: left;
  background: #eef2f6;
  color: #536174;
  font-weight: 600;
  border-bottom: 1px solid var(--gray-200);
  white-space: nowrap;
}
.data-table td {
  height: 44px;
  padding: 0 12px;
  border-bottom: 1px solid var(--gray-200);
  color: #455268;
  vertical-align: middle;
}
.data-table tbody tr:nth-child(even) {
  background: var(--zebra-row);
}
.data-table tbody tr:hover {
  background: var(--hover-row);
}
.data-table .num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.file-row {
  cursor: pointer;
}
.file-name-cell {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.file-icon {
  width: 30px;
  height: 30px;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  flex-shrink: 0;
}
.icon-folder { background: #fef3c7; color: #b45309; }
.icon-pdf { background: #fee2e2; color: #b91c1c; }
.icon-office { background: #dbeafe; color: #1d4ed8; }
.icon-dxf { background: #e0f2fe; color: #0369a1; }
.icon-3d { background: #ede9fe; color: #6d28d9; }
.icon-image { background: #dcfce7; color: #15803d; }
.icon-other { background: var(--gray-100); color: var(--gray-500); }

.file-name-text {
  font-size: 14px;
  font-weight: 500;
  color: var(--gray-900);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 440px;
}

/* Empty & Skeleton States */
.empty-state {
  padding: 56px 24px;
  text-align: center;
  color: var(--gray-500);
}
.empty-state h3 {
  margin: 12px 0 6px;
  font-size: 16px;
  color: var(--gray-900);
}
.skeleton-row {
  height: 44px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--gray-200);
  display: flex;
  align-items: center;
  gap: 12px;
}
.skeleton-bar {
  height: 14px;
  border-radius: 4px;
  background: linear-gradient(90deg, var(--gray-100) 25%, #e8ecf1 50%, var(--gray-100) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.2s infinite;
}
@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ================= PREVIEW MODAL ================= */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: var(--overlay-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
}
.preview-dialog {
  width: 100%;
  height: 100%;
  max-width: 1440px;
  max-height: 94vh;
  background: var(--surface);
  border-radius: var(--radius-panel);
  box-shadow: var(--shadow-modal);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}
.preview-header {
  min-height: 52px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--gray-200);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--surface);
}
.preview-title-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}
.preview-title {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  color: var(--gray-900);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.preview-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}
.preview-stage {
  position: relative;
  flex: 1;
  overflow: hidden;
  background: var(--gray-100);
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
}
.preview-2d-viewport {
  width: 100%;
  height: 100%;
  overflow: hidden;
  position: relative;
  cursor: grab;
  display: flex;
  align-items: center;
  justify-content: center;
}
.preview-2d-viewport.dragging {
  cursor: grabbing;
}
.preview-2d-content {
  transform-origin: center center;
  will-change: transform;
  width: 100%;
  height: 100%;
  padding: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.preview-2d-content img {
  display: block;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  pointer-events: none;
  -webkit-user-drag: none;
  box-shadow: 0 4px 18px rgba(16, 24, 40, 0.12);
  background: #ffffff;
}
.preview-2d-content .dxf-svg-box {
  width: 100%;
  height: 100%;
  max-width: 1120px;
  max-height: 100%;
  background: #ffffff;
  box-shadow: 0 4px 18px rgba(16, 24, 40, 0.12);
}
.preview-2d-content .dxf-svg-box svg {
  width: 100%;
  height: 100%;
  display: block;
}
.preview-loading-overlay {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: rgba(243, 244, 246, 0.88);
  text-align: center;
  padding: 24px;
}
.preview-3d-host {
  width: 100%;
  height: 100%;
  position: relative;
  background: linear-gradient(180deg, #f1f5f9 0%, #e2e8f0 100%);
}
.preview-watermark-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 20;
}
.preview-text-box {
  width: 100%;
  height: 100%;
  overflow: auto;
  padding: 24px;
  background: #ffffff;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  white-space: pre-wrap;
  word-break: break-word;
  position: relative;
}
.preview-footer {
  min-height: 44px;
  padding: 6px 16px;
  border-top: 1px solid var(--gray-200);
  background: var(--surface);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-size: 12px;
  color: var(--gray-500);
}
.spinner {
  width: 24px;
  height: 24px;
  border: 3px solid var(--gray-200);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ================= ADMIN SECTION ================= */
.admin-grid {
  display: grid;
  gap: 20px;
}
.section-head {
  padding: 14px 16px;
  border-bottom: 1px solid var(--gray-200);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
}
.section-title {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: var(--gray-900);
}
.inline-form {
  padding: 16px;
  background: var(--gray-50);
  border-bottom: 1px solid var(--gray-200);
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  align-items: end;
}
.tunnel-body {
  padding: 16px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 20px;
  align-items: start;
}
.tunnel-url-box {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: 10px 12px;
  background: var(--primary-soft);
  border: 1px solid #bfdbfe;
  border-radius: var(--radius-sm);
  margin-bottom: 14px;
}
.tunnel-url-link {
  font-size: 14px;
  font-weight: 600;
  color: var(--primary-hover);
  text-decoration: none;
  word-break: break-all;
  flex: 1;
}
.tunnel-qr-card {
  width: 196px;
  padding: 12px;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-panel);
  background: var(--surface);
  text-align: center;
}
.tunnel-qr-card svg {
  width: 168px;
  height: 168px;
  display: block;
  margin: 0 auto 6px;
}
@media (max-width: 768px) {
  .tunnel-body {
    grid-template-columns: 1fr;
  }
  .tunnel-qr-card {
    margin: 0 auto;
  }
}

/* Toast */
.toast-stack {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 90;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.toast {
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow-modal);
  background: var(--gray-900);
  color: #ffffff;
}
.toast-success { background: var(--green); }
.toast-error { background: var(--red); }

/* Mobile Responsive */
@media (max-width: 768px) {
  .topbar {
    padding: 8px 12px;
    flex-wrap: wrap;
  }
  .main-wrap {
    padding: 14px 12px 40px;
  }
  .toolbar {
    flex-direction: column;
    align-items: stretch;
  }
  .search-bar {
    max-width: 100%;
    width: 100%;
  }
  .user-pill .hide-mobile {
    display: none;
  }
  .file-name-text {
    max-width: 205px;
  }
  .hide-sm {
    display: none;
  }
  .modal-backdrop {
    padding: 0;
  }
  .preview-dialog {
    max-height: 100vh;
    border-radius: 0;
  }
  .preview-header {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    padding: 10px 12px;
  }
  .preview-controls {
    justify-content: flex-end;
  }
  .preview-footer {
    flex-direction: column;
    align-items: flex-start;
    gap: 2px;
    padding: 6px 12px;
  }
  .btn {
    min-height: 38px;
  }
  .btn-sm {
    min-height: 34px;
    padding: 0 8px;
  }
}
