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

:root {
  --bg-base: #0d0f14;
  --bg-panel: #131720;
  --bg-card: #1a1f2e;
  --bg-hover: #1e2535;
  --bg-active: #232b3e;
  --bg-header: #0f1319;
  --border: #1e2535;
  --border-subtle: #161c28;
  --accent: #3b82f6;
  --accent-dim: #1d4ed8;
  --accent-glow: rgba(59,130,246,0.15);
  --green: #10b981;
  --red: #ef4444;
  --yellow: #f59e0b;
  --purple: #8b5cf6;
  --orange: #f97316;
  --text-primary: #e2e8f0;
  --text-secondary: #94a3b8;
  --text-muted: #475569;
  --text-accent: #60a5fa;
  --row-even: #0d1117;
  --row-odd: #111420;
  --row-hover: #1a2035;
  --row-selected: #1e3a5f;
  --header-row: #0f1622;
  --cell-sel-overlay: rgba(255,255,255,0.08);
  --hl-amber: #7a5500;
  --hl-red: #7a1a1a;
  --hl-green: #1a5c2e;
  --hl-blue: #1a3a6b;
  --font-mono: 'Noto Sans Mono', 'Noto Sans TC', monospace;
  --font-ui: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --sidebar-w: 280px;
  --header-h: 48px;
  --footer-h: 28px;
  --col-h: 32px;
  --row-h: 28px;
  --radius: 6px;
  --transition: 0.15s ease;
}

:root.light {
  --bg-base: #f1f5f9;
  --bg-panel: #ffffff;
  --bg-card: #f8fafc;
  --bg-hover: #e2e8f0;
  --bg-active: #dde4ee;
  --bg-header: #ffffff;
  --border: #cbd5e1;
  --border-subtle: #e2e8f0;
  --accent: #2563eb;
  --accent-dim: #1d4ed8;
  --accent-glow: rgba(37,99,235,0.12);
  --text-primary: #0f172a;
  --text-secondary: #334155;
  --text-muted: #94a3b8;
  --text-accent: #2563eb;
  --row-even: #ffffff;
  --row-odd: #f8fafc;
  --row-hover: #e8f0fe;
  --row-selected: #bfdbfe;
  --header-row: #f1f5f9;
  --cell-sel-overlay: rgba(0,0,0,0.08);
  --hl-amber: #fde68a;
  --hl-red: #fca5a5;
  --hl-green: #86efac;
  --hl-blue: #93c5fd;
}

html, body { height: 100%; overflow: hidden; background: var(--bg-base); color: var(--text-primary); font-family: var(--font-ui); font-size: 13px; }

#app { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }

.hidden { display: none !important; }

/* Header */
#header {
  display: flex; align-items: center; justify-content: space-between;
  height: var(--header-h); padding: 0 12px; gap: 12px;
  background: var(--bg-header); border-bottom: 1px solid var(--border);
  flex-shrink: 0; z-index: 100;
}
.header-left { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 0 0 auto; }
.header-center { display: flex; align-items: center; flex: 1; min-width: 0; justify-content: center; }
.header-right { display: flex; align-items: center; gap: 8px; flex: 0 0 auto; }

.logo { display: flex; align-items: center; gap: 6px; font-weight: 700; letter-spacing: -0.5px; user-select: text; }
.logo-icon { display: block; flex-shrink: 0; }
.logo-accent { font-size: 16px; color: var(--accent); }
.logo-sub { font-size: 14px; color: var(--text-secondary); font-weight: 600; }
.file-name { font-size: 12px; color: var(--text-secondary); background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); padding: 2px 8px; max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; align-self: center; cursor: pointer; }
.copy-toast { position: fixed; background: #1e1e1e; border: 1px solid #555; border-radius: var(--radius); padding: 5px 14px; font-size: 13px; font-weight: 500; color: #ffffff; white-space: nowrap; pointer-events: none; opacity: 1; transition: opacity 0.3s; z-index: 9999; }
.light .copy-toast { background: #2a2a2a; border-color: #666; color: #fff; }
.copy-toast.fade { opacity: 0; }


.icon-btn {
  display: flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; border: none; background: transparent;
  color: var(--text-secondary); cursor: pointer; border-radius: var(--radius);
  transition: background var(--transition), color var(--transition);
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text-primary); }

.lang-btn { font-size: 12px; font-weight: 600; letter-spacing: 0.3px; width: auto; padding: 0 8px; }

.kofi-btn {
  display: flex; align-items: center; gap: 6px;
  padding: 0 12px; height: 30px; border-radius: 20px;
  background: #fcbf47; color: #323842;
  font-size: 13px; font-weight: 700; text-decoration: none;
  white-space: nowrap; transition: background 0.15s;
}
.kofi-btn:hover { background: #e6a800; }

.github-btn {
  display: flex; align-items: center; gap: 6px;
  padding: 0 12px; height: 30px; border-radius: 20px;
  background: #24292f; color: #ffffff;
  font-size: 13px; font-weight: 700; text-decoration: none;
  white-space: nowrap; transition: background 0.15s;
}
.github-btn:hover { background: #444d56; }
.light .github-btn { background: #24292f; color: #ffffff; }
.light .github-btn:hover { background: #444d56; }

.icon-btn-sm {
  display: flex; align-items: center; justify-content: center;
  width: 20px; height: 20px; border: none; background: transparent;
  color: var(--text-muted); cursor: pointer; border-radius: 4px; font-size: 11px;
  transition: background var(--transition), color var(--transition);
}
.icon-btn-sm:hover { background: var(--bg-hover); color: var(--text-primary); }

.btn-primary {
  display: flex; align-items: center; gap: 6px;
  padding: 0 12px; height: 30px; border: none;
  background: var(--accent); color: #fff; cursor: pointer;
  border-radius: var(--radius); font-size: 12px; font-weight: 500;
  transition: background var(--transition);
}
.btn-primary:hover { background: #2563eb; }

.btn-primary-lg {
  padding: 10px 24px; border: none; background: var(--accent); color: #fff;
  cursor: pointer; border-radius: var(--radius); font-size: 14px; font-weight: 500;
  transition: background var(--transition);
}
.btn-primary-lg:hover { background: #2563eb; }

.link-btn { border: none; background: transparent; color: var(--text-accent); cursor: pointer; font-size: 11px; }
.link-btn:hover { text-decoration: underline; }

/* Search Bar */
.search-bar {
  display: flex; align-items: center; gap: 6px; max-width: 520px; width: 100%;
  background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 0 8px; height: 30px;
}
.search-icon { color: var(--text-muted); display: flex; align-items: center; }
#search-input {
  flex: 1; border: none; background: transparent; color: var(--text-primary);
  font-family: var(--font-mono); font-size: 12px; outline: none;
}
#search-input::placeholder { color: var(--text-muted); }
.search-options { display: flex; gap: 2px; }
.search-opt {
  display: flex; align-items: center; gap: 3px; padding: 2px 5px;
  border-radius: 3px; cursor: pointer; color: var(--text-muted); font-size: 11px; font-weight: 600;
  transition: background var(--transition), color var(--transition);
  user-select: none;
}
.search-opt:hover { background: var(--bg-hover); color: var(--text-secondary); }
.search-opt input { display: none; }
.search-opt.active { background: var(--accent-glow); color: var(--accent); }
.search-count { font-size: 11px; color: var(--text-muted); white-space: nowrap; }

/* Layout */
#main-layout { display: flex; flex: 1; overflow: hidden; }

/* Sidebar */
.sidebar {
  width: var(--sidebar-w); background: var(--bg-panel); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; overflow: hidden; flex-shrink: 0;
  transition: width 0.2s ease, opacity 0.2s ease;
}
.sidebar.collapsed { width: 0; opacity: 0; pointer-events: none; }

.sidebar-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px 8px; border-bottom: 1px solid var(--border-subtle);
  font-size: 11px; font-weight: 600; color: var(--text-secondary); text-transform: uppercase; letter-spacing: 0.05em;
}
.sidebar-actions { display: flex; align-items: center; gap: 6px; }
.divider { color: var(--text-muted); }

.sidebar-search { padding: 8px 10px; border-bottom: 1px solid var(--border-subtle); }
#col-search {
  width: 100%; padding: 4px 8px; background: var(--bg-card); border: 1px solid var(--border);
  border-radius: 4px; color: var(--text-primary); font-size: 12px; outline: none;
}
#col-search:focus { border-color: var(--accent); }

.col-list { flex: 1; overflow-y: auto; padding: 4px 0; }
.col-list::-webkit-scrollbar { width: 4px; }
.col-list::-webkit-scrollbar-track { background: transparent; }
.col-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.col-item {
  display: flex; align-items: center; gap: 8px; padding: 5px 12px;
  cursor: pointer; transition: background var(--transition);
  user-select: none; position: relative;
}
.col-item:hover { background: var(--bg-hover); }
.col-item.hidden-col { opacity: 0.55; }
.col-item.hidden-col .col-name { color: var(--text-secondary); }
.col-item.col-dragging { opacity: 0.35; }
.col-item.col-drop-above::before,
.col-item.col-drop-below::after {
  content: ''; position: absolute; left: 8px; right: 8px; height: 2px;
  background: var(--accent); border-radius: 1px; pointer-events: none;
}
.col-item.col-drop-above::before { top: -1px; }
.col-item.col-drop-below::after { bottom: -1px; }

.col-drag-handle {
  font-size: 13px; color: var(--text-muted); cursor: grab; flex-shrink: 0;
  opacity: 0; transition: opacity var(--transition); line-height: 1;
}
.col-item:hover .col-drag-handle { opacity: 1; }
.col-drag-handle:active { cursor: grabbing; }

.col-checkbox {
  width: 14px; height: 14px; border: 1.5px solid var(--text-muted); border-radius: 3px;
  flex-shrink: 0; display: flex; align-items: center; justify-content: center;
  transition: background var(--transition), border-color var(--transition);
}
.col-item:not(.hidden-col) .col-checkbox {
  background: var(--accent); border-color: var(--accent);
}
.col-checkbox::after {
  content: ''; width: 8px; height: 5px; border-left: 2px solid #fff;
  border-bottom: 2px solid #fff; transform: rotate(-45deg) translate(1px, -1px);
  opacity: 0; transition: opacity var(--transition);
}
.col-item:not(.hidden-col) .col-checkbox::after { opacity: 1; }

.col-name {
  font-family: var(--font-mono); font-size: 11px; color: var(--text-secondary);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1;
}

/* Content Area */
#content-area { flex: 1; display: flex; flex-direction: column; overflow: hidden; position: relative; }

/* SEO Text */
.about {
  position: absolute; width: 1px; height: 1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; user-select: none;
}

/* Welcome Screen */
.welcome-screen {
  flex: 1; display: flex; align-items: center; justify-content: center;
  background: var(--bg-base);
}
.welcome-inner { text-align: center; display: flex; flex-direction: column; align-items: center; gap: 16px; }
.welcome-icon { color: var(--text-muted); }
.welcome-inner h1 { font-size: 28px; font-weight: 700; color: var(--text-primary); letter-spacing: -1px; }
.welcome-inner > p { color: var(--text-secondary); font-size: 14px; }
.welcome-features { display: flex; flex-direction: column; gap: 8px; text-align: left; color: var(--text-secondary); font-size: 13px; }
.feature-item { display: flex; align-items: center; gap: 10px; }
.feature-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.feature-dot.green { background: var(--green); box-shadow: 0 0 6px var(--green); }
.feature-dot.blue { background: var(--accent); box-shadow: 0 0 6px var(--accent); }
.feature-dot.purple { background: var(--purple); box-shadow: 0 0 6px var(--purple); }
.feature-dot.orange { background: var(--orange); box-shadow: 0 0 6px var(--orange); }
.welcome-actions { display: flex; align-items: center; gap: 12px; margin-top: 8px; }
.or-text { color: var(--text-secondary); font-size: 13px; }
.drag-hint { color: var(--text-secondary); font-size: 13px; }
.shortcut-hints { display: flex; gap: 16px; color: var(--text-secondary); font-size: 13px; }
kbd {
  display: inline-block; padding: 1px 4px; background: var(--bg-card); border: 1px solid var(--border);
  border-radius: 3px; font-family: var(--font-mono); font-size: 10px; color: var(--text-secondary);
}

/* Export */
.btn-export {
  display: flex; align-items: center; gap: 6px;
  padding: 0 12px; height: 30px; border: 1px solid var(--border);
  background: transparent; color: var(--text-primary); cursor: pointer;
  border-radius: var(--radius); font-size: 12px; font-weight: 500;
  transition: background var(--transition);
}
.btn-export:hover { background: var(--bg-hover); }

.export-modal-wrap {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  z-index: 10000; pointer-events: none;
}
.export-modal-wrap:not(.hidden) { pointer-events: all; }
.export-modal {
  background: var(--bg-panel); border: 1px solid var(--border);
  border-radius: 8px; padding: 20px 24px; width: 300px;
  display: flex; flex-direction: column; gap: 14px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.3);
}
.export-modal-title { font-size: 13px; font-weight: 600; color: var(--text-primary); }
.export-progress-row { display: flex; align-items: center; gap: 10px; }
.export-progress-bar-wrap { flex: 1; height: 4px; background: var(--bg-card); border-radius: 2px; overflow: hidden; }
.export-progress-bar-fill { height: 100%; background: #22c55e; border-radius: 2px; transition: width 0.15s ease; width: 0%; }
.export-pct { font-size: 11px; color: var(--text-muted); font-family: var(--font-mono); min-width: 32px; text-align: right; }
.export-modal-footer { display: flex; justify-content: flex-end; }

/* Help Modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center; z-index: 10000;
}
.modal {
  background: var(--bg-panel); border: 1px solid var(--border); border-radius: 8px;
  width: 520px; max-height: 80vh; display: flex; flex-direction: column; overflow: hidden;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.modal-title { font-size: 13px; font-weight: 600; color: var(--text-primary); }
.modal-body { overflow-y: auto; padding: 12px 16px; display: flex; flex-direction: column; gap: 16px; }
.help-section-title {
  font-size: 12px; font-weight: 600; color: var(--accent); text-transform: uppercase;
  letter-spacing: 0.08em; margin-bottom: 6px;
}
.help-row {
  display: grid; grid-template-columns: max-content 1fr; align-items: baseline;
  padding: 4px 0; border-bottom: 1px solid var(--border-subtle);
  user-select: text; cursor: text;
}
.help-row:last-child { border-bottom: none; }
.help-keys { display: flex; align-items: center; flex-wrap: wrap; gap: 3px; user-select: text; }
.help-desc { font-size: 12px; color: var(--text-secondary); padding-left: 12px; user-select: text; }
.modal-body kbd { padding: 2px 6px; font-size: 11px; border-bottom: 2px solid var(--border); }

/* Progress */
.progress-container {
  flex: 1; display: flex; align-items: center; justify-content: center; background: var(--bg-base);
}
.progress-inner { width: 400px; display: flex; flex-direction: column; gap: 12px; }
.progress-title { font-size: 14px; color: var(--text-primary); font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.progress-bar-wrap { width: 100%; height: 4px; background: var(--bg-card); border-radius: 2px; overflow: hidden; }
.progress-bar-fill { height: 100%; background: linear-gradient(90deg, var(--accent), #7c3aed); border-radius: 2px; transition: width 0.1s ease; }
.progress-detail { font-size: 11px; color: var(--text-muted); font-family: var(--font-mono); }

/* Table */
.table-container { flex: 1; display: flex; flex-direction: column; overflow: hidden; background: var(--bg-base); }

#table-header-canvas-wrap {
  flex-shrink: 0; overflow: hidden; border-bottom: 1px solid var(--border);
  background: var(--header-row);
}
#header-canvas { display: block; }

#table-body-wrap {
  flex: 1; position: relative; overflow: hidden; cursor: default; outline: none;
}
#body-canvas { display: block; }

#v-scrollbar-track {
  position: absolute; right: 0; top: 0; bottom: 0;
  width: 8px; background: transparent;
}
#v-scrollbar-thumb {
  position: absolute; right: 1px; width: 6px;
  background: var(--text-muted); border-radius: 3px; opacity: 0.5;
  cursor: pointer; transition: opacity 0.15s;
}
#v-scrollbar-thumb:hover { opacity: 0.9; }

#h-scrollbar-track {
  position: absolute; left: 0; right: 8px; bottom: 0;
  height: 8px; background: transparent;
}
#h-scrollbar-thumb {
  position: absolute; top: 1px; height: 6px;
  background: var(--text-muted); border-radius: 3px; opacity: 0.5;
  cursor: pointer; transition: opacity 0.15s;
}
#h-scrollbar-thumb:hover { opacity: 0.9; }

.table-footer {
  flex-shrink: 0; display: flex; align-items: center; gap: 16px;
  padding: 0 12px; height: var(--footer-h);
  background: var(--bg-header); border-top: 1px solid var(--border);
  font-size: 11px; color: var(--text-muted);
}
#footer-rows span, #footer-cols span { color: var(--text-secondary); font-family: var(--font-mono); }


/* Drop Overlay */
#drop-overlay {
  position: fixed; inset: 0; z-index: 9999;
  background: rgba(13,15,20,0.92); display: flex; align-items: center; justify-content: center;
  backdrop-filter: blur(4px);
}
.drop-box {
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  padding: 48px 64px; border: 2px dashed var(--accent); border-radius: 16px;
  color: var(--accent);
}
.drop-box p { font-size: 20px; font-weight: 600; }
.drop-box span { font-size: 13px; color: var(--text-muted); }

/* Context Menu */
.context-menu {
  position: fixed; z-index: 9999; min-width: 160px;
  background: var(--bg-panel); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: 0 8px 24px rgba(0,0,0,0.5);
  padding: 4px 0;
}
.ctx-item {
  padding: 6px 14px; font-size: 12px; color: var(--text-secondary); cursor: pointer;
  transition: background var(--transition), color var(--transition);
}
.ctx-item:hover { background: var(--bg-hover); color: var(--text-primary); }
.ctx-sep { height: 1px; background: var(--border); margin: 3px 0; }

.ctx-has-sub { position: relative; display: flex; align-items: center; justify-content: space-between; }
.ctx-arrow { font-size: 10px; color: var(--text-muted); pointer-events: none; }
.ctx-submenu {
  display: none; position: absolute; left: calc(100% - 2px); top: -4px;
  min-width: 150px; background: var(--bg-panel); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: 0 8px 24px rgba(0,0,0,0.5);
  padding: 4px 0; z-index: 10000;
}
.ctx-has-sub:hover .ctx-submenu { display: block; }
.ctx-sub-left .ctx-submenu { left: auto; right: calc(100% - 2px); }
.ctx-color-picker { min-width: 0; }
.ctx-colors { display: flex; gap: 6px; padding: 8px 10px; }
.ctx-color-dot {
  display: inline-block; width: 20px; height: 20px;
  border-radius: 50%; cursor: pointer; flex-shrink: 0;
  border: 2px solid transparent;
  transition: transform 0.1s, border-color 0.1s;
}
.ctx-color-dot:hover { transform: scale(1.25); border-color: var(--text-primary); }

::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

body.drag-active #drop-overlay { display: flex !important; }

/* Column Filter Popup */
.col-filter-popup {
  position: fixed; z-index: 9999; width: 220px;
  background: var(--bg-panel); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: 0 8px 24px rgba(0,0,0,0.5);
  padding: 10px; display: flex; flex-direction: column; gap: 8px;
}
.cfp-title {
  font-size: 11px; font-weight: 600; color: var(--text-secondary);
  text-transform: uppercase; letter-spacing: 0.05em;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.col-filter-popup select,
.col-filter-popup input[type=text] {
  width: 100%; padding: 4px 8px; background: var(--bg-card); border: 1px solid var(--border);
  border-radius: 4px; color: var(--text-primary); font-size: 12px; outline: none;
  font-family: var(--font-mono);
}
.col-filter-popup select:focus,
.col-filter-popup input[type=text]:focus { border-color: var(--accent); }
.col-filter-popup textarea {
  width: 100%; padding: 4px 8px; background: var(--bg-card); border: 1px solid var(--border);
  border-radius: 4px; color: var(--text-primary); font-size: 12px; outline: none;
  font-family: var(--font-mono); resize: vertical; min-height: 80px; box-sizing: border-box;
}
.col-filter-popup textarea:focus { border-color: var(--accent); }
.cfp-actions { display: flex; align-items: center; gap: 8px; }

#footer-sort { color: var(--text-muted); font-size: 11px; }
#footer-sort span { color: var(--accent); font-family: var(--font-mono); }
