:root {
  --radical: #2563eb;
  --phonetic: #dc2626;
  --neutral: #374151;
  --bg: #fafaf9;
  --card-bg: #ffffff;
  --border: #e5e5e5;
  --text: #1c1917;
  --text-muted: #78716c;
  --accent: #0c4a6e;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

header {
  text-align: center;
  padding: 2rem 1rem 1rem;
}

h1 {
  font-size: 1.8rem;
  font-weight: 700;
  margin-bottom: 0.25rem;
}

.subtitle {
  color: var(--text-muted);
  font-size: 0.95rem;
  margin-bottom: 1.25rem;
}

.color-radical { color: var(--radical); font-weight: 600; }
.color-phonetic { color: var(--phonetic); font-weight: 600; }

.search-box {
  max-width: 480px;
  margin: 0 auto;
}

#search {
  width: 100%;
  padding: 0.75rem 1rem;
  font-size: 1.1rem;
  border: 2px solid var(--border);
  border-radius: 8px;
  outline: none;
  transition: border-color 0.15s;
}

#search:focus {
  border-color: var(--accent);
}

/* Tabs */
.browse-tabs {
  display: flex;
  justify-content: center;
  gap: 0.25rem;
  padding: 1rem 1rem 0;
  border-bottom: 1px solid var(--border);
}

.tab {
  padding: 0.5rem 1.25rem;
  border: none;
  background: none;
  font-size: 0.95rem;
  cursor: pointer;
  color: var(--text-muted);
  border-bottom: 2px solid transparent;
  transition: all 0.15s;
}

.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

/* Panels */
main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 1.5rem 1rem;
}

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

/* Kanji grid */
.kanji-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
  gap: 0.5rem;
}

.kanji-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.5rem;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  transition: box-shadow 0.15s, transform 0.1s;
}

.kanji-card:hover {
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  transform: translateY(-1px);
}

.kanji-card .char {
  font-size: 2rem;
  line-height: 1.2;
}

.kanji-card .meaning {
  font-size: 0.7rem;
  color: var(--text-muted);
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

/* Filter buttons (grade, jlpt) */
.filter-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.filter-btn {
  padding: 0.4rem 1rem;
  border: 1px solid var(--border);
  background: var(--card-bg);
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: all 0.15s;
}

.filter-btn:hover { border-color: var(--accent); }
.filter-btn.active { background: var(--accent); color: white; border-color: var(--accent); }

/* Radical & phonetic grids */
.radical-grid, .phonetic-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(60px, 1fr));
  gap: 0.4rem;
  margin-bottom: 1.5rem;
  /* The full list is long (200+ entries); cap it so the selected results
     stay visible below instead of being pushed off-screen. */
  max-height: 38vh;
  overflow-y: auto;
  padding: 0.25rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card-bg);
}

.radical-btn, .phonetic-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.4rem 0.2rem;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.8rem;
  transition: all 0.15s;
}

.radical-btn:hover, .phonetic-btn:hover { border-color: var(--accent); }
.radical-btn.active, .phonetic-btn.active { background: var(--accent); color: white; border-color: var(--accent); }

.radical-btn .char, .phonetic-btn .char {
  font-size: 1.4rem;
  line-height: 1.2;
}

.radical-btn .count, .phonetic-btn .count {
  font-size: 0.65rem;
  color: var(--text-muted);
}

.radical-btn.active .count, .phonetic-btn.active .count { color: rgba(255,255,255,0.8); }

/* Detail overlay */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 1rem;
}

.overlay.hidden { display: none; }

.detail-card {
  background: var(--card-bg);
  border-radius: 12px;
  padding: 2rem;
  max-width: 600px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  position: relative;
}

.close-btn {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: var(--text-muted);
  line-height: 1;
}

.close-btn:hover { color: var(--text); }

/* Detail content */
.detail-header {
  display: flex;
  gap: 1.5rem;
  align-items: flex-start;
  margin-bottom: 1.5rem;
}

.detail-svg {
  flex-shrink: 0;
  width: 160px;
  height: 160px;
}

.detail-svg svg {
  width: 100%;
  height: 100%;
}

.detail-info h2 {
  font-size: 1.5rem;
  margin-bottom: 0.25rem;
}

.detail-meanings {
  font-size: 1.1rem;
  margin-bottom: 0.5rem;
}

.detail-readings {
  font-size: 0.95rem;
  color: var(--text-muted);
}

.detail-readings span { margin-right: 1rem; }

.detail-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 1rem 0;
}

.meta-tag {
  padding: 0.2rem 0.6rem;
  background: #f5f5f4;
  border-radius: 4px;
  font-size: 0.85rem;
  color: var(--text-muted);
}

/* Component legend */
.component-legend {
  display: flex;
  gap: 1.5rem;
  margin: 1rem 0;
  padding: 0.75rem 1rem;
  background: #fafaf9;
  border-radius: 8px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.95rem;
}

.legend-swatch {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  flex-shrink: 0;
}

.legend-char {
  font-size: 1.3rem;
  font-weight: 500;
}

.legend-link {
  border: none;
  background: none;
  padding: 0;
  cursor: pointer;
  color: inherit;
  font-family: inherit;
  border-bottom: 1px dashed currentColor;
}

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

.legend-label {
  font-size: 0.85rem;
  color: var(--text-muted);
}

/* Related kanji */
.related-section {
  margin-top: 1rem;
}

.related-section h3 {
  font-size: 0.9rem;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}

.related-kanji {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.related-kanji button {
  padding: 0.3rem 0.6rem;
  border: 1px solid var(--border);
  background: var(--card-bg);
  border-radius: 4px;
  cursor: pointer;
  font-size: 1.2rem;
  transition: all 0.15s;
}

.related-kanji button:hover {
  border-color: var(--accent);
  background: #f0f9ff;
}

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

/* Loading */
.loading {
  text-align: center;
  padding: 2rem;
  color: var(--text-muted);
}

@media (max-width: 480px) {
  .detail-header { flex-direction: column; align-items: center; }
  .detail-info { text-align: center; }
  .component-legend { flex-direction: column; gap: 0.5rem; }
  .kanji-grid { grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); }
}
