/* ===== Design tokens ===== */
:root {
  --bg: #0f1419;
  --bg-elevated: #1a2332;
  --bg-soft: #243044;
  --border: #2d3a4f;
  --border-focus: #5b9fd4;
  --text: #e8eef6;
  --text-muted: #8b9cb3;
  --text-dim: #6b7c93;
  --accent: #3d9cf0;
  --accent-hover: #5aaff5;
  --accent-soft: rgba(61, 156, 240, 0.15);
  --success: #3ecf8e;
  --warning: #e8a838;
  --warning-bg: rgba(232, 168, 56, 0.12);
  --danger: #e85d5d;
  --danger-bg: rgba(232, 93, 93, 0.12);
  --badge-bg: #2a3a52;
  --row-hover: rgba(61, 156, 240, 0.06);
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.35);
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
  --mono: "Cascadia Code", "Fira Code", "SF Mono", ui-monospace, monospace;
  --max-w: 1400px;
  --header-h: auto;
}

/* ===== Reset / base ===== */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  background-image:
    radial-gradient(ellipse 80% 50% at 50% -20%, rgba(61, 156, 240, 0.12), transparent),
    radial-gradient(ellipse 40% 30% at 100% 100%, rgba(62, 207, 142, 0.04), transparent);
}

h1, h2, h3 {
  margin: 0 0 0.5rem;
  font-weight: 650;
  letter-spacing: -0.02em;
  line-height: 1.25;
}

h2 { font-size: 1.15rem; }
h3 { font-size: 0.95rem; color: var(--text-muted); }

p { margin: 0 0 0.75rem; }

button, input, textarea, select {
  font: inherit;
  color: inherit;
}

/* ===== Accessibility ===== */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  top: -100px;
  left: 1rem;
  z-index: 100;
  padding: 0.5rem 1rem;
  background: var(--accent);
  color: #fff;
  border-radius: var(--radius-sm);
  text-decoration: none;
  font-weight: 600;
}

.skip-link:focus {
  top: 0.5rem;
}

:focus-visible {
  outline: 2px solid var(--border-focus);
  outline-offset: 2px;
}

/* ===== Header / footer ===== */
.site-header {
  border-bottom: 1px solid var(--border);
  background: rgba(15, 20, 25, 0.85);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 20;
}

.header-inner {
  max-width: var(--max-w);
  margin: 0 auto;
  padding: 1rem 1.25rem 1.1rem;
}

.logo {
  font-size: 1.35rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.2rem;
}

.logo-mark {
  color: var(--accent);
  font-size: 1.5rem;
  line-height: 1;
}

.tagline {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.9rem;
  max-width: 42rem;
}

.site-footer {
  max-width: var(--max-w);
  margin: 0 auto;
  padding: 1.5rem 1.25rem 2.5rem;
  color: var(--text-dim);
  font-size: 0.85rem;
  border-top: 1px solid var(--border);
  margin-top: 1rem;
}

.site-footer p { margin: 0; }

/* ===== Layout ===== */
.layout {
  max-width: var(--max-w);
  margin: 0 auto;
  padding: 1.25rem;
  display: grid;
  grid-template-columns: minmax(300px, 1fr) minmax(320px, 1.35fr);
  gap: 1.25rem;
  align-items: start;
}

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

.panel {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.15rem 1.25rem 1.35rem;
  box-shadow: var(--shadow);
}

/* ===== Form controls ===== */
.field-label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 0.4rem;
}

.paste-area,
.stop-words-edit,
.results-filter,
.sort-select,
input[type="number"] {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.65rem 0.75rem;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.paste-area {
  resize: vertical;
  min-height: 8rem;
  font-family: var(--mono);
  font-size: 0.875rem;
  line-height: 1.55;
}

.paste-area:focus,
.stop-words-edit:focus,
.results-filter:focus,
.sort-select:focus,
input[type="number"]:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px var(--accent-soft);
  outline: none;
}

.hint {
  font-size: 0.8rem;
  color: var(--text-dim);
  margin: 0.35rem 0 0.9rem;
}

.hint-inline {
  font-weight: 400;
  color: var(--text-dim);
}

/* ===== Drop zone ===== */
.drop-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  margin-bottom: 1rem;
  background: var(--bg);
}

.drop-zone:hover,
.drop-zone:focus-visible {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.drop-zone.drag-over {
  border-color: var(--accent);
  background: var(--accent-soft);
  border-style: solid;
}

.drop-zone-inner p {
  margin: 0.25rem 0;
}

.drop-icon {
  font-size: 1.75rem;
  display: block;
  margin-bottom: 0.35rem;
}

.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  text-decoration: underline;
  cursor: pointer;
  padding: 0;
  font-weight: 600;
}

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

/* ===== File list ===== */
.file-list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.file-list-header h3 {
  margin: 0;
  display: flex;
  align-items: center;
  gap: 0.45rem;
}

.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.4rem;
  height: 1.4rem;
  padding: 0 0.4rem;
  font-size: 0.75rem;
  font-weight: 700;
  background: var(--badge-bg);
  border-radius: 999px;
  color: var(--text-muted);
}

.file-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 14rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.file-list:empty::after {
  content: "No files added yet.";
  display: block;
  color: var(--text-dim);
  font-size: 0.85rem;
  padding: 0.5rem 0;
}

.file-item {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 0.5rem;
  align-items: center;
  padding: 0.55rem 0.7rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
}

.file-item.error {
  border-color: rgba(232, 93, 93, 0.4);
  background: var(--danger-bg);
}

.file-item.loading {
  opacity: 0.7;
}

.file-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 500;
}

.file-meta {
  color: var(--text-dim);
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.file-remove {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 0.15rem 0.35rem;
  border-radius: 4px;
  line-height: 1;
  font-size: 1rem;
}

.file-remove:hover {
  color: var(--danger);
  background: var(--danger-bg);
}

.warning {
  margin-top: 0.6rem;
  padding: 0.55rem 0.75rem;
  background: var(--warning-bg);
  border: 1px solid rgba(232, 168, 56, 0.35);
  border-radius: var(--radius-sm);
  color: var(--warning);
  font-size: 0.85rem;
}

/* ===== Options ===== */
.options-panel {
  margin: 1rem 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
}

.options-panel summary {
  padding: 0.7rem 0.9rem;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--text-muted);
  user-select: none;
}

.options-panel summary:hover {
  color: var(--text);
}

.options-panel[open] summary {
  border-bottom: 1px solid var(--border);
  color: var(--text);
}

.options-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.85rem 1rem;
  padding: 1rem;
}

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

.option-row {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.85rem;
}

.option-row label {
  color: var(--text-muted);
  font-weight: 500;
}

.option-row.checkbox-row {
  flex-direction: row;
  align-items: center;
  gap: 0.5rem;
  padding-top: 1.4rem;
}

.option-row.checkbox-row label {
  color: var(--text);
  cursor: pointer;
}

.option-row.full-width {
  grid-column: 1 / -1;
}

.option-row input[type="number"] {
  max-width: 8rem;
}

.option-row input[type="checkbox"] {
  width: 1.05rem;
  height: 1.05rem;
  accent-color: var(--accent);
  cursor: pointer;
}

.stop-words-edit {
  font-family: var(--mono);
  font-size: 0.8rem;
  resize: vertical;
}

/* ===== Buttons ===== */
.actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
  margin-top: 0.25rem;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  padding: 0.55rem 1.1rem;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}

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

.btn-primary {
  background: var(--accent);
  color: #fff;
  min-width: 7.5rem;
}

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

.btn-secondary {
  background: var(--bg-soft);
  border-color: var(--border);
  color: var(--text);
}

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

.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-muted);
  padding: 0.4rem 0.65rem;
}

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

/* ===== Progress ===== */
.progress-wrap {
  flex: 1;
  min-width: 10rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.progress-bar {
  height: 6px;
  background: var(--bg);
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--success));
  border-radius: 999px;
  transition: width 0.2s ease;
}

.progress-label {
  font-size: 0.75rem;
  color: var(--text-dim);
}

/* ===== Results ===== */
.results-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.results-header h2 {
  margin: 0;
}

.export-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.empty-state,
.error-state {
  padding: 2rem 1rem;
  text-align: center;
  color: var(--text-muted);
}

.error-state {
  background: var(--danger-bg);
  border: 1px solid rgba(232, 93, 93, 0.35);
  border-radius: var(--radius-sm);
  color: var(--danger);
  text-align: left;
  padding: 0.85rem 1rem;
}

/* Collapsed-by-default summary (stats + chart) */
.stats-panel {
  margin-bottom: 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
}

.stats-panel summary {
  padding: 0.55rem 0.85rem;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.85rem;
  color: var(--text-muted);
  user-select: none;
}

.stats-panel summary:hover {
  color: var(--text);
}

.stats-panel[open] summary {
  border-bottom: 1px solid var(--border);
  color: var(--text);
}

.stats-panel-body {
  padding: 0.85rem;
}

.summary-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(7.5rem, 1fr));
  gap: 0.6rem;
  margin-bottom: 0.75rem;
}

.summary-stats:empty {
  display: none;
  margin-bottom: 0;
}

.stat-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.7rem 0.85rem;
}

.stat-value {
  display: block;
  font-size: 1.25rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}

.stat-label {
  font-size: 0.75rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* Top-N bar chart (pure CSS) */
.top-chart {
  margin-bottom: 1rem;
}

.top-chart:empty {
  display: none;
}

.chart-title {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  margin: 0 0 0.5rem;
}

.chart-bars {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.chart-row {
  display: grid;
  grid-template-columns: minmax(4rem, 10rem) 1fr auto;
  gap: 0.5rem;
  align-items: center;
  font-size: 0.78rem;
}

.chart-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: var(--mono);
  color: var(--text-muted);
}

.chart-track {
  height: 10px;
  background: var(--bg);
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.chart-bar {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), #7ec4f8);
  border-radius: 999px;
  min-width: 2px;
}

.chart-count {
  font-variant-numeric: tabular-nums;
  color: var(--text-dim);
  min-width: 2rem;
  text-align: right;
}

.results-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.results-filter {
  flex: 1;
  min-width: 10rem;
  max-width: 20rem;
}

.sort-select {
  width: auto;
  min-width: 10rem;
  cursor: pointer;
}

/* Table */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  max-height: min(55vh, 28rem);
  overflow-y: auto;
}

.results-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

.results-table th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--bg-soft);
  text-align: left;
  padding: 0.55rem 0.7rem;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.results-table td {
  padding: 0.5rem 0.7rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.results-table tbody tr:hover {
  background: var(--row-hover);
}

.results-table tbody tr.expanded {
  background: var(--accent-soft);
}

.ngram-cell {
  font-family: var(--mono);
  font-size: 0.85rem;
  word-break: break-word;
  max-width: 18rem;
}

.num-cell {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.source-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
}

.source-badge {
  display: inline-block;
  max-width: 7rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 0.1rem 0.4rem;
  font-size: 0.7rem;
  background: var(--badge-bg);
  border-radius: 4px;
  color: var(--text-muted);
}

.expand-btn {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  padding: 0.25rem 0.55rem;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.expand-btn:hover,
.expand-btn[aria-expanded="true"] {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-soft);
}

/* Location panel */
.location-panel {
  margin-top: 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  max-height: min(55vh, 36rem);
  display: flex;
  flex-direction: column;
}

.location-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.55rem 0.75rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.location-panel-header h3 {
  margin: 0;
  color: var(--text);
  font-size: 0.9rem;
}

.location-list {
  list-style: none;
  margin: 0;
  padding: 0.5rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.location-item {
  padding: 0.55rem 0.7rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.82rem;
}

.location-meta {
  color: var(--text-dim);
  font-size: 0.75rem;
  margin-bottom: 0.3rem;
  font-variant-numeric: tabular-nums;
}

.location-context {
  font-family: var(--mono);
  font-size: 0.8rem;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--text-muted);
}

.location-context mark {
  background: rgba(61, 156, 240, 0.35);
  color: var(--text);
  padding: 0 0.15em;
  border-radius: 2px;
}

/* ===== Reduced motion ===== */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
  }
}
