:root {
  /* Coherent Dark Theme */
  --bg: #0b0f14;
  --fg: #e5e7eb;
  --muted: #9ca3af;
  --card-bg: #111827;
  --card-border: #1f2937;
  --accent: #60a5fa;
  
  /* Error/Shake colors */
  --error-bg: #450a0a;
  --error-border: #ef4444;
  --error-fg: #fecaca;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, Segoe UI, Roboto, "Helvetica Neue", Arial, "Noto Sans", "Liberation Sans", sans-serif;
}

.app-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.75rem 1rem; border-bottom: 1px solid var(--card-border);
  position: sticky; top: 0; background: var(--bg); z-index: 10;
}

.header-branding { display: flex; flex-direction: column; }
.header-branding h1 { margin: 0; font-size: 1.5rem; line-height: 1.2; }
.header-branding .copyright { font-size: 0.75rem; color: var(--muted); margin-top: 2px; }
.header-branding .copyright a { color: inherit; text-decoration: none; }
.header-branding .copyright a:hover { text-decoration: underline; }

.view { padding: 1rem; max-width: 1000px; margin: 0 auto; }

h1, h2 { margin: 0.5rem 0; }

.config-form .form-row {
  display: grid; grid-template-columns: 180px 1fr; gap: 0.5rem; align-items: center; margin-bottom: 0.5rem;
}
.config-form input, .config-form select {
  padding: 0.5rem; border: 1px solid var(--card-border); border-radius: 6px; background: var(--card-bg); color: var(--fg);
}
.primary { background: var(--accent); color: white; border: none; padding: 0.6rem 1rem; border-radius: 6px; cursor: pointer; }
.secondary {
  background: var(--card-bg);
  color: var(--fg);
  border: 1px solid var(--card-border);
  padding: 0.6rem 1rem;
  border-radius: 6px;
  cursor: pointer;
}
.secondary:hover { border-color: var(--muted); }
button:disabled {
  opacity: 0.5; cursor: not-allowed;
}

.statusbar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.statusbar .actions > * { margin-right: 0.5rem; }

/* Dynamic columns driven by --cols */
.grid {
  --cols: 4;
  display: grid;
  grid-template-columns: repeat(var(--cols), 1fr);
  gap: 0.75rem;
  transition: opacity 0.3s ease, transform 0.3s ease;
}

/* Found groups container (rows stack vertically) */
.found-rows {
  --cols: 4;
  display: block;
  margin-bottom: 0.75rem;
}

/* Each found group row uses the same column count and width as the grid */
.found-row {
  display: flex;
  flex-direction: row;
  align-items: center;
  padding: 8px;
  border-radius: 8px;
  margin-bottom: 8px;
  min-height: 95px;
}

.card {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 12px;
  padding: 4px;
  min-height: 95px;
  display: flex; flex-direction: column; gap: 0;
  justify-content: center;
  align-items: center;
  text-align: center;
  color: var(--fg);
  cursor: pointer; user-select: none; outline: none;
  overflow: hidden;
}
.card:focus-visible { box-shadow: 0 0 0 3px var(--accent); }
.card[aria-pressed="true"] { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent) inset; }

.card .caption {
  font-size: 0.85rem; color: var(--muted); margin-bottom: 0.1rem;
  white-space: nowrap;
  width: max-content;
}
.card .caption .katex { font-size: 1em; }

.card .lines {
  display: flex; flex-direction: column; gap: 0.2rem;
  align-items: center; justify-content: center;
  
  transform-origin: center;
}
.card .line { font-size: 1rem; line-height: 1.2; white-space: nowrap; }

/* Locked (found) cards are non-interactive and slightly toned */
.card--locked {
  cursor: default;
  pointer-events: none;
  opacity: 0.95;
}

/* Inline math fallback (if KaTeX not loaded) */
.inline-math {
  font-family: "Cambria Math", "STIX Two Text", serif;
  white-space: nowrap;
}

/* Display math block inside a card (KaTeX) */
.card .display-math {
  display: block;
  text-align: center;
  margin: 0.15rem 0;
}
.card .display-math .katex { font-size: 1.15em; }
.katex { color: var(--fg); }

/* Shuffle animation */
.grid.fade-out { opacity: 0; transform: scale(0.95); }
.grid.fade-in { opacity: 1; transform: scale(1); }

.hidden { display: none !important; }

/* ---------- Animations & Extra Styles (Merged) ---------- */

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  10%, 30%, 50%, 70%, 90% { transform: translateX(-4px); }
  20%, 40%, 60%, 80% { transform: translateX(4px); }
}

.shake {
  animation: shake 0.5s;
  background-color: var(--error-bg) !important;
  border-color: var(--error-border) !important;
  color: var(--error-fg);
}

@keyframes pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); }
}

.pop {
  animation: pop 0.3s ease-in-out;
}

@keyframes slideIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.slide-in {
  animation: slideIn 0.5s ease-out forwards;
}

.found-row-title {
  font-weight: bold;
  text-align: center;
  margin-right: 10px;
  flex: 0 0 20%;
}

.found-row-cards {
  display: flex;
  flex-direction: row;
  gap: 8px;
  flex: 1;
}

.found-card {
  font-size: 0.85em;
  flex: 1;
  min-width: 0;
  container-type: inline-size;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 2px;
  min-height: 79px;
}

.found-card .lines {
  white-space: nowrap;
  width: max-content;
  text-align: center;
  font-size: 1em;
  transform-origin: center;
}

.toast-container {
  position: fixed;
  top: 60px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  background-color: var(--fg, #e5e7eb);
  color: var(--bg, #0b0f14);
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 0.9rem;
  opacity: 0;
  transform: translateY(10px);
  transition: opacity 0.3s ease, transform 0.3s ease;
  box-shadow: 0 2px 8px rgba(0,0,0,0.5);
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

.infinite {
  font-size: 1.5em;
  vertical-align: -0.1em;
  display: inline-block;
}

.puzzle-meta {
  margin-top: 1.5rem;
  text-align: center;
  font-size: 0.85rem;
  color: var(--muted);
}
