:root {
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-muted: #f8f9fc;
  --text: #1f2937;
  --muted: #64748b;
  --border: #dbe2ef;
  --primary: #4f46e5;
  --primary-strong: #4338ca;
  --danger: #dc2626;
  --warning: #b45309;
  --shadow: 0 8px 24px rgba(15, 23, 42, 0.08);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  font-family: "Microsoft JhengHei", "Noto Sans TC", sans-serif;
  color: var(--text);
  background: var(--bg);
  min-height: 100vh;
}

#app {
  min-height: 100vh;
}

.tables-page {
  padding: 20px;
}

.container {
  max-width: 1440px;
  margin: 0 auto;
  background: var(--surface);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #ffffff;
  padding: 24px 28px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}

.header h1 {
  margin: 0 0 8px;
  font-size: 24px;
}

.header p {
  margin: 0;
  opacity: 0.92;
  font-size: 14px;
}

.refresh-catalog-btn {
  border: 1px solid rgba(255, 255, 255, 0.4);
  background: rgba(255, 255, 255, 0.18);
  color: #ffffff;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 700;
  padding: 10px 14px;
  cursor: pointer;
  white-space: nowrap;
}

.refresh-catalog-btn:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.28);
}

.refresh-catalog-btn:disabled {
  opacity: 0.65;
  cursor: not-allowed;
}

.content {
  padding: 26px;
}

.error-banner {
  margin-bottom: 16px;
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: #991b1b;
  border-radius: 8px;
  padding: 12px 14px;
  font-size: 14px;
}

.loading-panel,
.empty-state {
  background: var(--surface-muted);
  border: 1px dashed var(--border);
  border-radius: 8px;
  padding: 28px;
  text-align: center;
  color: var(--muted);
}

.table-category {
  margin-bottom: 28px;
}

.table-category:last-child {
  margin-bottom: 0;
}

.category-title {
  margin: 0 0 14px;
  padding-bottom: 10px;
  font-size: 20px;
  border-bottom: 2px solid rgba(79, 70, 229, 0.35);
}

.table-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 14px;
}

.table-card {
  background: var(--surface-muted);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.table-card:hover {
  border-color: #8b83f8;
  box-shadow: 0 6px 16px rgba(79, 70, 229, 0.15);
  transform: translateY(-2px);
}

.table-card.active {
  border-color: var(--primary);
  background: #eef2ff;
}

.table-card.disabled {
  opacity: 0.7;
  cursor: wait;
}

.table-name {
  margin: 0 0 8px;
  color: var(--primary);
  font-size: 16px;
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.table-info {
  margin: 0 0 4px;
  font-size: 13px;
  color: #475569;
}

.table-desc {
  margin: 8px 0 0;
  color: #64748b;
  font-style: italic;
  font-size: 12px;
  line-height: 1.45;
}

.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 11px;
  color: #ffffff;
  background: var(--primary);
}

.badge.large {
  background: var(--danger);
}

.data-viewer {
  margin-top: 24px;
}

.viewer-header {
  background: var(--primary);
  color: #ffffff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 14px 18px;
  border-radius: 10px 10px 0 0;
}

.viewer-header h3 {
  margin: 0;
  font-size: 18px;
}

.close-btn {
  border: 0;
  background: rgba(255, 255, 255, 0.2);
  color: #ffffff;
  border-radius: 6px;
  padding: 8px 14px;
  cursor: pointer;
  font-size: 13px;
}

.close-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

.stats {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  align-items: center;
  border: 1px solid var(--border);
  border-top: 0;
  padding: 14px 16px;
  background: var(--surface-muted);
}

.stat-item {
  display: flex;
  flex-direction: column;
  min-width: 120px;
}

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

.stat-value {
  margin-top: 3px;
  font-size: 18px;
  font-weight: 700;
  color: var(--primary);
}

.stat-value-table-name {
  font-size: 14px;
}

.filter-hint {
  font-size: 12px;
  color: var(--muted);
}

.query-btn,
.clear-btn {
  border: 0;
  border-radius: 6px;
  padding: 10px 16px;
  color: #ffffff;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}

.query-btn {
  margin-left: auto;
  background: var(--primary);
}

.query-btn:hover:not(:disabled) {
  background: var(--primary-strong);
}

.clear-btn {
  background: #64748b;
}

.clear-btn:hover:not(:disabled) {
  background: #475569;
}

.query-btn:disabled,
.clear-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.active-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 10px 16px;
  background: #fafbff;
}

.filter-tag {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: #e0e7ff;
  color: var(--primary);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 12px;
}

.filter-tag .remove {
  border: 0;
  background: transparent;
  color: inherit;
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  padding: 0;
}

.filter-tag .remove:hover {
  color: var(--danger);
}

.table-container {
  border: 1px solid var(--border);
  border-top: 0;
  border-radius: 0 0 10px 10px;
  max-height: 620px;
  overflow: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

thead {
  position: sticky;
  top: 0;
  background: #f8fafc;
  z-index: 1;
}

th {
  padding: 10px;
  text-align: left;
  border-bottom: 2px solid #dde5f2;
  color: #334155;
  white-space: nowrap;
}

.filter-row th {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  background: #eef2f7;
}

.filter-row input {
  width: 100%;
  border: 1px solid #cbd5e1;
  border-radius: 4px;
  padding: 6px 8px;
  font-size: 12px;
}

.filter-row input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(79, 70, 229, 0.18);
}

td {
  padding: 9px 10px;
  border-bottom: 1px solid #edf2f7;
  color: #1e293b;
}

tbody tr:hover {
  background: #f8fafc;
}

.loading,
.error,
.empty-hint {
  text-align: center;
  padding: 28px;
}

.loading {
  color: var(--primary);
}

.error {
  color: #991b1b;
  background: #fef2f2;
}

.empty-hint {
  color: var(--muted);
}

.null-value {
  color: #94a3b8;
}

@media (max-width: 900px) {
  .tables-page {
    padding: 12px;
  }

  .content {
    padding: 16px;
  }

  .header {
    padding: 18px;
    flex-direction: column;
  }

  .header h1 {
    font-size: 24px;
  }

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

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

  .query-btn,
  .clear-btn {
    width: 100%;
    margin-left: 0;
  }
}
