:root {
  --bg: #f7f7fb;
  --bg-elevated: #ffffff;
  --text: #1a1a2e;
  --text-muted: #6b6b80;
  --border: #e4e4ee;
  --accent: #6c5ce7;
  --accent-soft: #efecff;
  --cell-hover: #eeeeff;
  --shadow: 0 2px 10px rgba(20, 20, 40, 0.06);
  --radius: 12px;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #14141f;
  --bg-elevated: #1d1d2c;
  --text: #eceef5;
  --text-muted: #9494ab;
  --border: #2c2c40;
  --accent: #8b7cf6;
  --accent-soft: #29253f;
  --cell-hover: #262638;
  --shadow: 0 2px 14px rgba(0, 0, 0, 0.35);
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14141f;
    --bg-elevated: #1d1d2c;
    --text: #eceef5;
    --text-muted: #9494ab;
    --border: #2c2c40;
    --accent: #8b7cf6;
    --accent-soft: #29253f;
    --cell-hover: #262638;
    --shadow: 0 2px 14px rgba(0, 0, 0, 0.35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Apple SD Gothic Neo",
    "Noto Sans KR", "Malgun Gothic", Roboto, sans-serif;
  transition: background 0.2s ease, color 0.2s ease;
}

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

/* ---------- Header ---------- */

.app-header {
  position: sticky;
  top: 0;
  z-index: 50;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.header-row {
  display: flex;
  align-items: center;
  gap: 16px;
  max-width: 1100px;
  margin: 0 auto;
  padding: 14px 20px;
  flex-wrap: wrap;
}

.logo {
  font-size: 20px;
  font-weight: 800;
  margin: 0;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 6px;
}

.search-wrap {
  position: relative;
  flex: 1 1 280px;
  min-width: 180px;
  display: flex;
  align-items: center;
}

.search-icon {
  position: absolute;
  left: 12px;
  opacity: 0.6;
  pointer-events: none;
  font-size: 14px;
}

#search {
  width: 100%;
  padding: 10px 34px 10px 34px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

#search:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.search-clear {
  position: absolute;
  right: 8px;
  border: none;
  background: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 14px;
  padding: 4px 6px;
  border-radius: 50%;
}
.search-clear:hover { background: var(--cell-hover); color: var(--text); }

.header-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.tone-picker {
  display: flex;
  gap: 5px;
  padding: 4px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
}

.tone-swatch {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
}
.tone-swatch.active { border-color: var(--accent); }
.tone-0 { background: linear-gradient(135deg, #ffd93b, #ffb200); }
.tone-1 { background: #ffdfc4; }
.tone-2 { background: #eec19b; }
.tone-3 { background: #c98e5f; }
.tone-4 { background: #8d5a3d; }
.tone-5 { background: #5a3825; }

.icon-btn {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 999px;
  width: 36px;
  height: 36px;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover { background: var(--cell-hover); }

.lang-select {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 999px;
  height: 36px;
  padding: 0 12px;
  font-size: 13px;
  cursor: pointer;
}
.lang-select:hover { background: var(--cell-hover); }

.hidden { display: none !important; }

.mode-switch {
  max-width: 1100px;
  margin: 0 auto;
  display: flex;
  gap: 6px;
  padding: 0 20px 10px;
}

.mode-tab {
  padding: 7px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.mode-tab:hover { color: var(--text); background: var(--cell-hover); }
.mode-tab.active {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: var(--accent);
}

.category-nav {
  max-width: 1100px;
  margin: 0 auto;
  display: flex;
  gap: 4px;
  overflow-x: auto;
  padding: 0 20px 10px;
  scrollbar-width: thin;
}

.category-nav::-webkit-scrollbar { height: 6px; }
.category-nav::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.cat-tab {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid transparent;
  background: none;
  color: var(--text-muted);
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}
.cat-tab:hover { background: var(--cell-hover); color: var(--text); }
.cat-tab.active {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: var(--accent);
  font-weight: 600;
}
.cat-tab .cat-emoji { font-size: 16px; }

/* Twemoji <img> standing in for an emoji glyph — sized relative to
   whatever text context it's dropped into (cell, nav pill, section title). */
.emoji-img {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  object-fit: contain;
}

/* ---------- Main ---------- */

main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 20px 20px 60px;
}

.emoji-section {
  margin-bottom: 28px;
  scroll-margin-top: 128px;
  /* Skip layout/paint entirely for off-screen categories (kaomoji mode
     alone has 48 of these, most never on screen at once) — this is what
     actually keeps mode switches light regardless of how many cells exist. */
  content-visibility: auto;
  contain-intrinsic-size: auto 500px;
}

.section-title {
  font-size: 15px;
  font-weight: 700;
  margin: 0 0 10px;
  color: var(--text);
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.section-title .count {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
}

.emoji-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(52px, 1fr));
  gap: 6px;
}

.emoji-cell {
  position: relative;
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  user-select: none;
  transition: transform 0.08s ease, background 0.12s ease, border-color 0.12s ease;
  line-height: 1;
}

.emoji-cell:hover {
  background: var(--cell-hover);
  border-color: var(--accent);
  transform: translateY(-2px) scale(1.06);
  z-index: 1;
}

.emoji-cell:active { transform: scale(0.94); }

.flow-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.kaomoji-cell,
.art-cell {
  aspect-ratio: auto;
  width: auto;
  height: auto;
  flex-direction: column;
  align-items: flex-start;
  justify-content: flex-start;
  text-align: left;
}

.kaomoji-cell {
  padding: 10px 16px;
  font-size: 16px;
  white-space: nowrap;
}

.art-cell {
  padding: 14px 18px;
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: 13px;
  line-height: 1.45;
}

.art-cell .cell-glyph { white-space: pre; display: block; }

.art-name-tag,
.kaomoji-name-tag {
  display: block;
  margin-top: 6px;
  font-size: 11px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Apple SD Gothic Neo",
    "Noto Sans KR", sans-serif;
  color: var(--text-muted);
  white-space: normal;
}

.fav-btn {
  position: absolute;
  top: -4px;
  right: -4px;
  font-size: 12px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  cursor: pointer;
  transition: opacity 0.12s ease;
}

.emoji-cell:hover .fav-btn,
.fav-btn.is-fav {
  opacity: 1;
}

.fav-btn.is-fav { color: #f5a623; }

.empty-msg {
  color: var(--text-muted);
  font-size: 14px;
  padding: 20px 0;
  text-align: center;
}

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: var(--shadow);
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.app-footer {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 20px 40px;
  color: var(--text-muted);
  font-size: 12px;
  line-height: 1.7;
  border-top: 1px solid var(--border);
}

@media (max-width: 560px) {
  .emoji-grid { grid-template-columns: repeat(auto-fill, minmax(44px, 1fr)); }
  .emoji-cell { font-size: 22px; border-radius: 8px; }
  .logo { font-size: 17px; }
}

/* ---------- Generator ---------- */

.generator-desc {
  color: var(--text-muted);
  font-size: 13px;
  margin: -4px 0 18px;
  line-height: 1.6;
}

.generator-panel {
  display: grid;
  grid-template-columns: 260px 1fr;
  gap: 20px;
  align-items: start;
}

.generator-controls {
  display: flex;
  flex-direction: column;
  gap: 14px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}

.file-drop {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 18px 10px;
  border: 1.5px dashed var(--border);
  border-radius: 10px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-muted);
  transition: border-color 0.15s ease, color 0.15s ease;
}
.file-drop:hover { border-color: var(--accent); color: var(--text); }
.file-drop input[type="file"] {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  overflow: hidden;
}

.control-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--text);
}
.control-row span { display: flex; align-items: center; gap: 4px; }
.control-row b { color: var(--accent); font-weight: 700; }

.control-row select {
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
}

.control-row input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.checkbox-row {
  flex-direction: row;
  align-items: center;
  gap: 8px;
}
.checkbox-row input { accent-color: var(--accent); width: 16px; height: 16px; }

.generator-hint {
  font-size: 11px;
  color: var(--text-muted);
  margin: 0;
  line-height: 1.5;
}

.generator-output-wrap {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  min-height: 320px;
  display: flex;
  flex-direction: column;
}

.generator-output-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  font-size: 12px;
  color: var(--text-muted);
}

.gen-copy-btn {
  width: auto;
  padding: 0 14px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}
.gen-copy-btn:disabled { opacity: 0.5; cursor: not-allowed; }

.generator-output {
  flex: 1;
  margin: 0;
  overflow: auto;
  white-space: pre;
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: 8px;
  line-height: 1.05;
  letter-spacing: 0;
  color: var(--text);
  background: var(--bg);
  border-radius: 8px;
  padding: 10px;
  cursor: text;
}

.generator-output:empty { display: none; }

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