:root,
[data-theme="dark"] {
  --bg: #0f1419;
  --bg-accent: #1a2332;
  --surface: #171c24;
  --surface-2: #1f2630;
  --border: #2d3644;
  --text: #eef2f8;
  --muted: #93a0b4;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --warning-bg: rgba(251, 191, 36, 0.12);
  --warning-text: #fcd34d;
  --radius: 10px;
}

[data-theme="light"] {
  --bg: #eef2f7;
  --bg-accent: #dbeafe;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #d5dde8;
  --text: #111827;
  --muted: #64748b;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --warning-bg: #fef3c7;
  --warning-text: #92400e;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: radial-gradient(circle at top, var(--bg-accent) 0%, var(--bg) 55%);
  color: var(--text);
  line-height: 1.5;
}

.container {
  max-width: 1180px;
  margin: 0 auto;
  padding: 2rem 1.25rem 3rem;
}

.app-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 1.75rem;
}

.app-header h1 {
  margin: 0 0 0.35rem;
  font-size: 1.85rem;
  letter-spacing: -0.02em;
}

.app-header p {
  margin: 0.35rem 0 0;
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1.5;
}

.app-help {
  margin-top: 1rem;
  padding: 1rem 1.15rem;
  background: var(--surface-2, var(--surface));
  border: 1px solid var(--border);
  border-radius: var(--radius, 8px);
  font-size: 0.9rem;
  color: var(--muted);
}

.app-help-intro {
  margin: 0 0 0.75rem;
  line-height: 1.55;
}

.app-help-tips,
.app-help-features {
  margin: 0.5rem 0 0;
}

.app-help-tips summary,
.app-help-features summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 0.35rem;
}

.app-help ul {
  margin: 0.35rem 0 0;
  padding-left: 1.2rem;
}

.app-help li {
  margin-bottom: 0.3rem;
  line-height: 1.45;
}

.home-app-intro {
  font-size: 0.88rem;
  color: var(--muted);
  line-height: 1.45;
  margin: 0.25rem 0 0.5rem;
}

.app-nav {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  flex-wrap: wrap;
}

.app-nav a {
  color: var(--text);
  text-decoration: none;
  padding: 0.5rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
}

.app-nav a:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.app-nav a.active {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  color: var(--accent);
}

.nav-tools {
  position: relative;
}

.nav-tools summary {
  list-style: none;
  cursor: pointer;
  color: var(--text);
  padding: 0.5rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
}

.nav-tools summary::-webkit-details-marker {
  display: none;
}

.nav-tools summary:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.nav-tools-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 0.35rem);
  z-index: 20;
  display: grid;
  grid-template-columns: repeat(2, minmax(9rem, 1fr));
  gap: 0.35rem;
  min-width: 18rem;
  max-height: 70vh;
  overflow: auto;
  padding: 0.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}

.nav-tools:not([open]) .nav-tools-menu {
  display: none;
}

.nav-tools-menu a {
  display: block;
  white-space: nowrap;
}

.hidden-label {
  display: none !important;
}

.correlation-table td,
.correlation-table th {
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.correlation-table tbody th {
  text-align: left;
}

.chart-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.85rem;
}

.chart-tabs .btn-secondary.active {
  border-color: var(--accent);
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 10%, transparent);
}

.distribution-chart-wrap {
  min-height: 320px;
  margin-bottom: 1rem;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 0.75rem;
}

.distribution-stats .stats-card {
  margin: 0;
}

.distribution-stats .stats-card ul {
  margin: 0;
  padding-left: 1.1rem;
  font-size: 0.88rem;
}

.distribution-stats .stats-card li + li {
  margin-top: 0.3rem;
}

.stats-results {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.stats-section h3 {
  margin: 0 0 0.65rem;
  font-size: 0.95rem;
}

.stats-section h4 {
  margin: 0 0 0.55rem;
  font-size: 0.9rem;
}

.stats-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.85rem;
  background: var(--surface-2);
  margin-bottom: 0.75rem;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 0.55rem 0.85rem;
  margin-bottom: 0.75rem;
}

.stats-grid span {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
}

.stats-grid strong {
  font-size: 0.92rem;
}

.table-scroll {
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.stats-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

.stats-table th,
.stats-table td {
  padding: 0.55rem 0.7rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
  white-space: nowrap;
}

.stats-table th {
  background: #243044;
  position: sticky;
  top: 0;
}

[data-theme="light"] .stats-table th {
  background: #e2e8f0;
}

.stats-table tbody tr:last-child td {
  border-bottom: none;
}

.stats-table td:first-child {
  font-weight: 600;
  color: var(--muted);
}

.hidden-label {
  display: none !important;
}

.chart-wrap {
  position: relative;
  min-height: 420px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  padding: 0.75rem;
}

.chart-wrap canvas {
  width: 100% !important;
  height: 100% !important;
  min-height: 380px;
}

.chart-wrap .empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
}

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.25rem;
}

@media (max-width: 960px) {
  .grid {
    grid-template-columns: 1fr;
  }
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem;
}

.panel h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.85rem;
}

label {
  display: block;
  margin-bottom: 0.45rem;
  font-size: 0.88rem;
  color: var(--muted);
}

input[type="text"],
input[type="search"],
select,
textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  padding: 0.65rem 0.75rem;
  font: inherit;
}

textarea {
  min-height: 240px;
  resize: vertical;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.85rem;
  line-height: 1.45;
}

select {
  cursor: pointer;
}

.filter-input {
  max-width: 220px;
  margin: 0;
}

input:focus,
select:focus,
textarea:focus {
  outline: 2px solid rgba(59, 130, 246, 0.35);
  border-color: var(--accent);
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
  margin: 0.85rem 0 1rem;
}

.drop-zone {
  position: relative;
  margin-bottom: 1rem;
}

.drop-zone.dragover textarea {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

.drop-hint {
  margin: 0.35rem 0 0;
  font-size: 0.82rem;
  color: var(--muted);
}

.options-panel {
  margin-bottom: 0.85rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.65rem 0.75rem;
  background: var(--surface-2);
}

.options-panel summary {
  cursor: pointer;
  font-weight: 600;
  font-size: 0.92rem;
}

.options-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.65rem 1rem;
  margin-top: 0.75rem;
}

.options-grid .span-2 {
  grid-column: 1 / -1;
}

.custom-field[hidden] {
  display: none !important;
}

.attrs-input {
  min-height: 4.5rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.82rem;
  line-height: 1.4;
}

.field-hint {
  margin: 0 0 0.75rem;
  font-size: 0.82rem;
  color: var(--muted);
  line-height: 1.45;
}

.field-hint code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.8rem;
  padding: 0.08rem 0.28rem;
  border-radius: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
}

#html-attrs-panel[hidden] {
  display: none;
}

.hint code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.82rem;
  padding: 0.1rem 0.3rem;
  border-radius: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
}

@media (max-width: 640px) {
  .options-grid {
    grid-template-columns: 1fr;
  }

  .container {
    padding: 1.25rem 1rem 2rem;
  }

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

  .app-header button {
    width: 100%;
  }

  .toolbar,
  .actions {
    flex-direction: column;
    align-items: stretch;
  }

  .toolbar button,
  .actions button,
  .file-label {
    width: 100%;
    justify-content: center;
    min-height: 44px;
  }

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

  .filter-input {
    max-width: none;
    width: 100%;
  }
}

.checkbox {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  margin: 0;
  color: var(--text);
  cursor: pointer;
  font-size: 0.88rem;
}

.checkbox input {
  accent-color: var(--accent);
}

.file-label {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.45rem 0.75rem;
  border: 1px dashed var(--border);
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.88rem;
  color: var(--muted);
  background: var(--surface-2);
}

.file-label:hover {
  border-color: var(--accent);
  color: var(--text);
}

.file-label input {
  display: none;
}

button {
  border: none;
  border-radius: 8px;
  padding: 0.65rem 1.1rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  background: var(--accent);
  color: white;
  min-height: 42px;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s ease, transform 0.1s ease, opacity 0.15s ease;
}

button:active:not(:disabled) {
  transform: scale(0.98);
}

button.is-busy {
  opacity: 0.85;
  cursor: wait;
}

button:hover:not(:disabled) {
  background: var(--accent-hover);
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-secondary {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}

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

.actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.65rem;
  margin-top: 1rem;
}

.hint {
  margin: 0.65rem 0 0;
  font-size: 0.82rem;
  color: var(--muted);
}

kbd {
  display: inline-block;
  padding: 0.1rem 0.35rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 0.78rem;
  background: var(--surface-2);
}

#status {
  color: var(--muted);
  font-size: 0.88rem;
}

#status.error,
.error-text {
  color: #f87171;
}

.stats {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem 1rem;
  margin-bottom: 0.75rem;
  font-size: 0.84rem;
  color: var(--muted);
}

.stats strong {
  color: var(--text);
}

.warnings {
  margin-bottom: 0.75rem;
  padding: 0.65rem 0.75rem;
  border-radius: 8px;
  background: var(--warning-bg);
  color: var(--warning-text);
  font-size: 0.84rem;
}

.warnings p {
  margin: 0;
}

.warnings p + p {
  margin-top: 0.35rem;
}

.preview-wrap {
  overflow: auto;
  max-height: 560px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
}

.preview-wrap table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}

.preview-wrap th,
.preview-wrap td {
  padding: 0.55rem 0.7rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

.preview-wrap th {
  position: sticky;
  top: 0;
  background: #243044;
  font-weight: 600;
  white-space: nowrap;
}

[data-theme="light"] .preview-wrap th {
  background: #e2e8f0;
}

.preview-wrap th.sortable {
  cursor: pointer;
  user-select: none;
}

.preview-wrap th.sortable:hover {
  background: rgba(59, 130, 246, 0.18);
}

.preview-wrap tbody tr:last-child td {
  border-bottom: none;
}

.preview-wrap tbody tr:nth-child(even) {
  background: rgba(127, 127, 127, 0.04);
}

.empty {
  margin: 0;
  padding: 2rem 1rem;
  text-align: center;
  color: var(--muted);
}

@media print {
  body {
    background: white;
    color: black;
  }

  .container > :not(.grid),
  .panel:first-child,
  .panel-head,
  .stats,
  .warnings,
  .actions,
  .hint {
    display: none !important;
  }

  .grid {
    display: block;
  }

  .preview-wrap {
    max-height: none;
    border: none;
  }
}

/* Homepage */
.home-page {
  background:
    radial-gradient(ellipse 80% 50% at 50% -10%, rgba(59, 130, 246, 0.18) 0%, transparent 55%),
    radial-gradient(circle at top, var(--bg-accent) 0%, var(--bg) 55%);
}

[data-theme="light"] .home-page {
  background:
    radial-gradient(ellipse 80% 50% at 50% -10%, rgba(37, 99, 235, 0.12) 0%, transparent 55%),
    radial-gradient(circle at top, var(--bg-accent) 0%, var(--bg) 55%);
}

.home-shell {
  max-width: 1080px;
  margin: 0 auto;
  padding: 1.5rem 1.25rem 3rem;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.home-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  margin-bottom: 3rem;
  flex-wrap: wrap;
}

.home-brand {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  color: var(--text);
  text-decoration: none;
}

.home-brand:hover {
  color: var(--accent);
}

.home-hero {
  text-align: center;
  max-width: 720px;
  margin: 0 auto 3.5rem;
}

.home-eyebrow {
  display: inline-block;
  margin: 0 0 1rem;
  padding: 0.35rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  background: var(--surface);
}

.home-hero h1 {
  margin: 0 0 1rem;
  font-size: clamp(2rem, 5vw, 2.85rem);
  line-height: 1.12;
  letter-spacing: -0.03em;
}

.home-lead {
  margin: 0 auto 1.75rem;
  max-width: 580px;
  font-size: 1.08rem;
  color: var(--muted);
}

.home-hero-actions {
  display: flex;
  justify-content: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.home-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.72rem 1.25rem;
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: 600;
  text-decoration: none;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.home-btn-primary {
  background: var(--accent);
  color: #fff;
  border: 1px solid var(--accent);
}

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

.home-btn-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

.home-btn-secondary:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.home-apps h2,
.home-steps h2 {
  margin: 0 0 1.25rem;
  font-size: 1.15rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  text-align: center;
}

.home-app-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 1rem;
  margin-bottom: 3rem;
}

.home-app-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.35rem;
  display: flex;
  flex-direction: column;
  transition: border-color 0.15s, transform 0.15s;
}

.home-app-card:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
}

.home-app-icon {
  width: 48px;
  height: 48px;
  margin-bottom: 1rem;
  color: var(--accent);
}

.home-app-icon svg {
  width: 100%;
  height: 100%;
}

.home-app-icon-stats {
  color: #22c55e;
}

.home-app-icon-chart {
  color: #f97316;
}

.home-app-card h3 {
  margin: 0 0 0.45rem;
  font-size: 1.1rem;
  letter-spacing: -0.02em;
}

.home-app-card > p {
  margin: 0 0 0.85rem;
  font-size: 0.92rem;
  color: var(--muted);
  flex-grow: 0;
}

.home-app-card ul {
  margin: 0 0 1.15rem;
  padding-left: 1.1rem;
  font-size: 0.85rem;
  color: var(--muted);
  flex-grow: 1;
}

.home-app-card li + li {
  margin-top: 0.35rem;
}

.home-card-link {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--accent);
  text-decoration: none;
}

.home-card-link:hover {
  color: var(--accent-hover);
}

.home-steps {
  margin-bottom: 2.5rem;
}

.home-step-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}

.home-step-list li {
  display: flex;
  gap: 0.85rem;
  align-items: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem;
}

.home-step-num {
  flex-shrink: 0;
  width: 2rem;
  height: 2rem;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--accent);
}

.home-step-list strong {
  display: block;
  margin-bottom: 0.25rem;
  font-size: 0.95rem;
}

.home-step-list p {
  margin: 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.home-features {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.75rem;
  margin-bottom: 2rem;
}

.home-feature {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.9rem 1rem;
}

.home-feature strong {
  display: block;
  margin-bottom: 0.2rem;
  font-size: 0.9rem;
}

.home-feature span {
  font-size: 0.82rem;
  color: var(--muted);
}

.home-footer {
  margin-top: auto;
  padding-top: 2rem;
  border-top: 1px solid var(--border);
  text-align: center;
}

.home-footer p {
  margin: 0;
  font-size: 0.88rem;
  color: var(--muted);
}

.home-footer a {
  color: var(--accent);
  text-decoration: none;
}

.home-footer a:hover {
  text-decoration: underline;
}

@media (max-width: 900px) {
  .home-app-grid,
  .home-step-list {
    grid-template-columns: 1fr;
  }

  .home-features {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  .home-header {
    flex-direction: column;
    align-items: stretch;
  }

  .home-header .app-nav {
    justify-content: flex-start;
  }
}
