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

:root {
  --bg: #0a0a0f;
  --surface: #12121a;
  --surface2: #1a1a26;
  --border: #2a2a3a;
  --border-light: #3a3a4e;
  --text: #e8e8f0;
  --text-dim: #8888a0;
  --accent: #6c5ce7;
  --accent-light: #8b7cf0;
  --accent-glow: rgba(108, 92, 231, 0.15);
  --danger: #e74c3c;
  --success: #2ecc71;
  --warning: #f39c12;
  --radius: 8px;
  --radius-sm: 4px;
}

html { font-size: 15px; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

#app {
  max-width: 1100px;
  margin: 0 auto;
  padding: 2rem 1.5rem;
}

.view { display: none; }
.view.active { display: block; }

/* Header */
header { margin-bottom: 2rem; }
header h1 {
  font-size: 1.8rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}
.subtitle {
  color: var(--text-dim);
  margin-top: 0.4rem;
  font-size: 0.95rem;
}
.back-link {
  color: var(--accent-light);
  text-decoration: none;
  font-size: 0.85rem;
  display: inline-block;
  margin-bottom: 0.5rem;
}
.back-link:hover { text-decoration: underline; }

header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1rem;
}
header h1 { flex: 1; }
#view-table header { flex-direction: column; align-items: flex-start; gap: 0.3rem; }
#view-table header h1 { display: flex; align-items: center; gap: 1rem; width: 100%; }

/* Buttons */
button {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 0.55rem 1.2rem;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
  transition: background 0.15s, transform 0.1s;
}
button:hover { background: var(--accent-light); }
button:active { transform: scale(0.97); }
.btn-danger { background: var(--danger); }
.btn-danger:hover { background: #c0392b; }
.btn-sm { padding: 0.3rem 0.7rem; font-size: 0.78rem; }
.btn-icon {
  background: transparent;
  color: var(--text-dim);
  padding: 0.2rem 0.4rem;
  font-size: 0.85rem;
}
.btn-icon:hover { color: var(--danger); background: rgba(231,76,60,0.1); }

/* Inputs */
input[type="text"] {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.55rem 0.8rem;
  border-radius: var(--radius);
  font-size: 0.9rem;
  outline: none;
  transition: border-color 0.15s;
}
input[type="text"]:focus { border-color: var(--accent); }
input[type="text"]::placeholder { color: var(--text-dim); }

/* Forms */
.create-form, .ask-form {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1rem;
}
.create-form input, .ask-form input { flex: 1; }
.ask-form .btn-ghost { flex: none; }

/* Add items bar */
.add-items-bar {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1rem;
  align-items: center;
}
.add-items-bar input:first-child { flex: 1; }

/* Tables list */
.tables-list { display: flex; flex-direction: column; gap: 0.5rem; }
.table-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.2rem;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.table-card:hover { border-color: var(--accent); background: var(--accent-glow); }
.table-card-name { font-weight: 600; }
.table-card-date { color: var(--text-dim); font-size: 0.8rem; }

/* Data table */
.table-container {
  overflow-x: auto;
  margin-bottom: 1.5rem;
  border-radius: var(--radius);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}
thead {
  position: sticky;
  top: 0;
  z-index: 1;
}
th {
  background: var(--surface2);
  color: var(--text-dim);
  font-weight: 600;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  text-align: left;
  padding: 0.7rem 0.8rem;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
th .col-header {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
td {
  padding: 0.65rem 0.8rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
tr:hover td { background: var(--surface); }

td.cell-name {
  font-weight: 500;
  white-space: nowrap;
}
td.cell-name a {
  color: var(--text);
  text-decoration: none;
  border-bottom: 1px dashed var(--border-light);
  transition: border-color 0.15s, color 0.15s;
}
td.cell-name a:hover {
  color: var(--accent-light);
  border-bottom-color: var(--accent-light);
}

td.cell-value {
  min-width: 100px;
}

/* Loading shimmer */
.cell-loading {
  display: inline-block;
  width: 60px;
  height: 1em;
  background: linear-gradient(90deg, var(--surface2) 25%, var(--border) 50%, var(--surface2) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  border-radius: var(--radius-sm);
}
@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.cell-value.just-filled {
  animation: flash-in 0.5s ease;
}
@keyframes flash-in {
  0% { background: var(--accent-glow); }
  100% { background: transparent; }
}

/* Empty state */
.empty-state {
  text-align: center;
  color: var(--text-dim);
  padding: 3rem 1rem;
  font-size: 0.95rem;
}

/* Status bar */
.status-bar {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem 0.8rem;
  font-size: 0.82rem;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.status-bar.hidden { display: none; }
.status-bar .spinner {
  width: 14px;
  height: 14px;
  border: 2px solid var(--border-light);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.status-bar.error { border-color: var(--danger); color: var(--danger); }

/* Search section */
.search-section {
  margin-bottom: 1.5rem;
}
.search-form {
  display: flex;
  gap: 0.5rem;
}
.search-form input { flex: 1; }
.search-results {
  margin-top: 0.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  max-height: 320px;
  overflow-y: auto;
}
.search-results.hidden { display: none; }
.search-result-item {
  display: flex;
  align-items: flex-start;
  gap: 0.6rem;
  padding: 0.6rem 0.8rem;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.1s;
}
.search-result-item:last-child { border-bottom: none; }
.search-result-item:hover { background: var(--accent-glow); }
.search-result-item input[type="checkbox"] {
  margin-top: 0.25rem;
  accent-color: var(--accent);
  flex-shrink: 0;
}
.search-result-item .sr-info { flex: 1; min-width: 0; }
.search-result-item .sr-title {
  font-weight: 500;
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.search-result-item .sr-url {
  color: var(--accent-light);
  font-size: 0.78rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.search-result-item .sr-snippet {
  color: var(--text-dim);
  font-size: 0.78rem;
  margin-top: 0.15rem;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.search-actions {
  display: flex;
  justify-content: flex-end;
  padding: 0.5rem 0.8rem;
  border-top: 1px solid var(--border);
  background: var(--surface2);
  gap: 0.5rem;
}
.search-actions .count { color: var(--text-dim); font-size: 0.82rem; line-height: 2; }
.search-loading {
  padding: 1.5rem;
  text-align: center;
  color: var(--text-dim);
  font-size: 0.85rem;
}

.btn-ghost {
  background: transparent;
  color: var(--text-dim);
  border: 1px solid var(--border);
}
.btn-ghost:hover {
  color: var(--text);
  border-color: var(--border-light);
  background: var(--surface);
}

/* Sortable columns */
th.sortable {
  cursor: pointer;
  user-select: none;
  transition: background 0.15s;
}
th.sortable:hover {
  background: var(--border);
}
.sort-icon {
  font-size: 0.7rem;
  color: var(--text-dim);
  margin-left: 0.3rem;
  opacity: 0.4;
}
.sort-icon.active {
  opacity: 1;
  color: var(--accent-light);
}
.col-sort-target {
  cursor: pointer;
}

/* Wizard progress stepper */
.wizard-progress {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.2rem;
  margin-bottom: 1rem;
}
.wizard-progress.hidden { display: none; }
.wizard-steps {
  display: flex;
  gap: 0;
  align-items: center;
  justify-content: space-between;
  position: relative;
}
.wizard-step {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.3rem;
  flex: 1;
  position: relative;
  z-index: 1;
}
.wizard-step .step-num {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--surface2);
  border: 2px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--text-dim);
  transition: all 0.3s;
}
.wizard-step .step-label {
  font-size: 0.7rem;
  color: var(--text-dim);
  text-align: center;
  transition: color 0.3s;
}
.wizard-step.active .step-num {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
  box-shadow: 0 0 12px rgba(108, 92, 231, 0.4);
  animation: pulse-glow 1.5s ease-in-out infinite;
}
.wizard-step.active .step-label {
  color: var(--accent-light);
  font-weight: 600;
}
.wizard-step.done .step-num {
  border-color: var(--success);
  background: var(--success);
  color: #fff;
}
.wizard-step.done .step-label {
  color: var(--success);
}
@keyframes pulse-glow {
  0%, 100% { box-shadow: 0 0 8px rgba(108, 92, 231, 0.3); }
  50% { box-shadow: 0 0 18px rgba(108, 92, 231, 0.6); }
}
.wizard-detail {
  text-align: center;
  color: var(--text-dim);
  font-size: 0.82rem;
  margin-top: 0.7rem;
  min-height: 1.2em;
}

/* Disabled overlay for controls during wizard */
.controls-bar.disabled-overlay {
  opacity: 0.35;
  pointer-events: none;
  filter: grayscale(0.5);
  position: relative;
}

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 10vh;
  z-index: 100;
  animation: fade-in 0.15s ease;
}
.modal-overlay.hidden { display: none; }
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: 560px;
  max-width: 95vw;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  animation: slide-up 0.2s ease;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}
@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes slide-up {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.2rem;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 {
  font-size: 1.1rem;
  font-weight: 600;
}
.modal-close {
  font-size: 1.3rem;
  padding: 0.2rem 0.5rem;
}
.modal-body {
  padding: 1.2rem;
  overflow-y: auto;
  flex: 1;
}
.modal-body .add-items-bar {
  margin-bottom: 0;
}
.modal-body .search-results {
  position: relative;
  margin-top: 0.5rem;
  margin-bottom: 0.5rem;
}
.manual-add-section {
  margin-top: 1rem;
}
.manual-add-divider {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  color: var(--text-dim);
  font-size: 0.78rem;
  margin-bottom: 0.8rem;
}
.manual-add-divider::before,
.manual-add-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}
.manual-add-form {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}
.manual-add-form input:first-child { flex: 1; }
.manual-add-form .url-input { width: 160px; flex: none; }

/* Units toggle */
.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}
.units-toggle {
  display: flex;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  flex-shrink: 0;
}
.units-btn {
  padding: 0.35rem 0.75rem;
  font-size: 0.75rem;
  background: transparent;
  color: var(--text-dim);
  border: none;
  cursor: pointer;
  transition: all 0.15s;
}
.units-btn:hover {
  color: var(--text);
}
.units-btn.active {
  background: var(--accent);
  color: #fff;
}

/* Responsive */
@media (max-width: 640px) {
  #app { padding: 1rem; }
  .add-items-bar { flex-wrap: wrap; }
  .add-items-bar .url-input { width: 100%; }
  header h1 { font-size: 1.4rem; }
  .wizard-steps { gap: 0.2rem; }
  .wizard-step .step-label { font-size: 0.6rem; }
  .wizard-step .step-num { width: 24px; height: 24px; font-size: 0.65rem; }
}
