/* ─── CSS Custom Properties ─────────────────────────────────────────────── */
:root {
  --bg: #f7f8fa;
  --bg-surface: #ffffff;
  --bg-surface2: #f0f1f5;
  --border: #d0d3da;
  --text: #1a1c23;
  --text-muted: #6b7280;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-fg: #ffffff;
  --danger: #dc2626;
  --danger-hover: #b91c1c;

  --type-number: #2563eb;
  --type-date: #7c3aed;
  --type-boolean: #059669;
  --type-string: #6b7280;

  --radius: 8px;
  --radius-sm: 4px;
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  --font-mono: "JetBrains Mono", "Fira Code", "Cascadia Code", "Menlo", monospace;
  --shadow: 0 1px 4px rgba(0,0,0,0.08);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.12);
}

[data-theme="dark"] {
  --bg: #0f1117;
  --bg-surface: #1a1d27;
  --bg-surface2: #22273a;
  --border: #2e3348;
  --text: #e2e8f0;
  --text-muted: #8892a4;
  --accent: #60a5fa;
  --accent-hover: #93c5fd;
  --accent-fg: #0f1117;
  --danger: #f87171;
  --danger-hover: #fca5a5;

  --type-number: #60a5fa;
  --type-date: #c4b5fd;
  --type-boolean: #34d399;
  --type-string: #8892a4;
}

/* ─── Reset & Base ──────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 15px; }

body {
  font-family: var(--font-ui);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  line-height: 1.5;
  transition: background 0.2s, color 0.2s;
}

a { color: var(--accent); }

/* ─── Layout ────────────────────────────────────────────────────────────── */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1.25rem;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  gap: 1rem;
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-left {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.app-title {
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.app-subtitle {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.app-main {
  flex: 1;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.app-footer {
  text-align: center;
  padding: 0.75rem;
  font-size: 0.75rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
}

/* ─── Panel ─────────────────────────────────────────────────────────────── */
.panel {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
}

.hidden { display: none !important; }

/* ─── Drop zone ─────────────────────────────────────────────────────────── */
.drop-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 1.25rem 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
  color: var(--text-muted);
  font-size: 0.875rem;
  margin-bottom: 0.75rem;
  user-select: none;
}

.drop-zone:hover,
.drop-zone.active {
  background: var(--bg-surface2);
  border-color: var(--accent);
  color: var(--accent);
}

.drop-icon {
  width: 28px;
  height: 28px;
  opacity: 0.6;
}

/* ─── Textarea ──────────────────────────────────────────────────────────── */
.csv-textarea {
  width: 100%;
  font-family: var(--font-mono);
  font-size: 0.825rem;
  background: var(--bg-surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  padding: 0.6rem 0.75rem;
  resize: vertical;
  min-height: 120px;
  outline: none;
  transition: border-color 0.15s;
}

.csv-textarea:focus {
  border-color: var(--accent);
}

/* ─── Controls row ──────────────────────────────────────────────────────── */
.controls-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-top: 0.75rem;
  flex-wrap: wrap;
}

.controls-left,
.controls-right {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.paste-row {
  margin-bottom: 0.4rem;
}

/* ─── Labels / selects ──────────────────────────────────────────────────── */
.label {
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.825rem;
  cursor: pointer;
  user-select: none;
  color: var(--text);
}

.select {
  font-family: var(--font-ui);
  font-size: 0.825rem;
  padding: 0.3rem 0.5rem;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  cursor: pointer;
  outline: none;
}

.select:focus {
  border-color: var(--accent);
}

.select-sm {
  padding: 0.2rem 0.4rem;
  font-size: 0.8rem;
}

/* ─── Buttons ───────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-family: var(--font-ui);
  font-size: 0.825rem;
  font-weight: 500;
  padding: 0.4rem 0.9rem;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s, opacity 0.15s;
  white-space: nowrap;
  line-height: 1.4;
  background: transparent;
  color: var(--text);
}

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

.btn-primary {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn-ghost {
  border-color: var(--border);
  background: var(--bg-surface);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--bg-surface2);
}

.btn-sm {
  padding: 0.25rem 0.6rem;
  font-size: 0.8rem;
}

.icon-btn {
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.8rem;
  padding: 0.1rem 0.3rem;
  border-radius: var(--radius-sm);
  line-height: 1;
  display: inline-flex;
  align-items: center;
  transition: color 0.12s, background 0.12s;
}

.icon-btn:hover {
  color: var(--danger);
  background: rgba(220,38,38,0.08);
}

/* ─── Toast ─────────────────────────────────────────────────────────────── */
.toast {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  padding: 0.6rem 1rem;
  border-radius: var(--radius);
  font-size: 0.875rem;
  font-weight: 500;
  opacity: 0;
  pointer-events: none;
  transform: translateY(8px);
  transition: opacity 0.2s, transform 0.2s;
  z-index: 9999;
}

.toast.show {
  opacity: 1;
  transform: none;
  pointer-events: auto;
}

.toast-success {
  background: #16a34a;
  color: #fff;
}

.toast-error {
  background: var(--danger);
  color: #fff;
}

/* ─── Empty state ───────────────────────────────────────────────────────── */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
  padding: 3rem 1rem;
  color: var(--text-muted);
  text-align: center;
}

.empty-icon {
  width: 64px;
  height: 64px;
  opacity: 0.3;
}

/* ─── Table panel ───────────────────────────────────────────────────────── */
.table-panel {
  padding: 0;
  overflow: hidden;
}

.table-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.toolbar-left,
.toolbar-right {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

/* ─── Stats bar ─────────────────────────────────────────────────────────── */
.stats-bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.825rem;
  color: var(--text-muted);
}

.stat-item strong {
  color: var(--text);
  font-weight: 600;
}

.stat-sep {
  color: var(--border);
}

/* ─── Search ────────────────────────────────────────────────────────────── */
.search-input {
  font-family: var(--font-ui);
  font-size: 0.825rem;
  padding: 0.3rem 0.6rem;
  background: var(--bg-surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  outline: none;
  width: 180px;
  transition: border-color 0.15s, width 0.2s;
}

.search-input:focus {
  border-color: var(--accent);
  width: 220px;
}

/* ─── Dropdown ──────────────────────────────────────────────────────────── */
.dropdown {
  position: relative;
}

.dropdown-menu {
  display: none;
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  min-width: 200px;
  z-index: 200;
  list-style: none;
  padding: 0.25rem 0;
}

.dropdown-menu.open {
  display: block;
}

.dropdown-item {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  padding: 0.45rem 1rem;
  font-size: 0.825rem;
  font-family: var(--font-ui);
  color: var(--text);
  cursor: pointer;
  transition: background 0.12s;
}

.dropdown-item:hover {
  background: var(--bg-surface2);
}

.dropdown-divider {
  height: 1px;
  background: var(--border);
  margin: 0.25rem 0;
}

/* ─── Table ─────────────────────────────────────────────────────────────── */
.table-wrapper {
  overflow-x: auto;
  overflow-y: auto;
  max-height: 60vh;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--font-mono);
  font-size: 0.8rem;
  table-layout: auto;
}

.data-table thead {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg-surface);
}

.data-table th {
  padding: 0.5rem 0.6rem;
  text-align: left;
  font-weight: 600;
  font-family: var(--font-ui);
  font-size: 0.8rem;
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
  position: relative;
  user-select: none;
  color: var(--text);
}

.data-table th.sortable {
  cursor: pointer;
}

.data-table th.sortable:hover {
  background: var(--bg-surface2);
}

.data-table th.row-num,
.data-table td.row-num {
  width: 2.5rem;
  min-width: 2.5rem;
  text-align: right;
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 0.75rem;
  padding-right: 0.5rem;
  background: var(--bg-surface2);
  border-right: 1px solid var(--border);
}

.data-table th.col-actions,
.data-table td.row-actions {
  width: 2rem;
  min-width: 2rem;
  text-align: center;
  border-left: 1px solid var(--border);
  background: var(--bg-surface2);
}

.data-table td {
  padding: 0.35rem 0.6rem;
  border-bottom: 1px solid var(--border);
  min-width: 80px;
  max-width: 300px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  vertical-align: top;
}

.data-table td.editable {
  cursor: text;
}

.data-table td.editable:focus {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  white-space: normal;
  overflow: visible;
  background: var(--bg-surface2);
}

.data-table tbody tr:hover td {
  background: var(--bg-surface2);
}

.data-table tbody tr:hover td.row-num,
.data-table tbody tr:hover td.row-actions {
  background: var(--bg-surface2);
}

.no-results {
  text-align: center;
  color: var(--text-muted);
  padding: 2rem !important;
  font-family: var(--font-ui) !important;
}

/* ─── Type tags ─────────────────────────────────────────────────────────── */
.type-tag {
  display: inline-block;
  font-size: 0.65rem;
  font-family: var(--font-ui);
  font-weight: 500;
  padding: 0.1rem 0.35rem;
  border-radius: 100px;
  margin-left: 0.35rem;
  vertical-align: middle;
  letter-spacing: 0.01em;
}

.type-number { background: rgba(37,99,235,0.12); color: var(--type-number); }
.type-date   { background: rgba(124,58,237,0.12); color: var(--type-date); }
.type-boolean{ background: rgba(5,150,105,0.12);  color: var(--type-boolean); }
.type-string { background: rgba(107,114,128,0.1); color: var(--type-string); }

/* ─── Pagination ─────────────────────────────────────────────────────────── */
.pagination {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.6rem 1rem;
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
  font-size: 0.825rem;
}

.page-info {
  color: var(--text-muted);
  flex: 1;
  text-align: center;
  min-width: 120px;
}

.pagination-size {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  margin-left: auto;
}

/* ─── Utilities ─────────────────────────────────────────────────────────── */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
}

/* ─── Responsive ─────────────────────────────────────────────────────────── */
@media (max-width: 600px) {
  .app-header { padding: 0.6rem 0.75rem; }
  .app-main { padding: 0.75rem; }
  .app-subtitle { display: none; }
  .search-input { width: 130px; }
  .search-input:focus { width: 160px; }
  .controls-row { flex-direction: column; align-items: stretch; }
  .controls-left, .controls-right { justify-content: flex-start; }
  .table-wrapper { max-height: 50vh; }
}
