/* ── SVGrid — Structured Variation Grids ───────────────────────────────── */

:root {
  /* Cell colours (used when "Colour cells" is on, or for hidden state in coloured mode) */
  --vg-blue-bg: #dbeafe;
  --vg-blue-text: #1e3a5f;
  --vg-blue-hidden: #93c5fd;
  --vg-yellow-bg: #fef3c7;
  --vg-yellow-text: #78350f;
  --vg-yellow-hidden: #fcd34d;

  /* Undefined cells (coloured mode) */
  --vg-undefined-bg: #e2e8f0;
  --vg-undefined-text: #475569;
  --vg-undefined-hidden: #cbd5e1;

  /* Bold borders for classroom visibility */
  --vg-cell-border: #334155;
  --vg-cell-divider: #94a3b8;

  /* Sidebar */
  --vg-sidebar-width: 170px;
}

[data-theme="dark"] {
  --vg-blue-bg: #1e3a5f;
  --vg-blue-text: #bfdbfe;
  --vg-blue-hidden: #1e40af;
  --vg-yellow-bg: #78350f;
  --vg-yellow-text: #fef3c7;
  --vg-yellow-hidden: #92400e;
  --vg-undefined-bg: #334155;
  --vg-undefined-text: #94a3b8;
  --vg-undefined-hidden: #1e293b;
  --vg-cell-border: #94a3b8;
  --vg-cell-divider: #475569;
}

/* ── Selection Screen ─────────────────────────────────────────────────────── */

.vg-select {
  max-width: 1400px;
  margin: 0 auto;
  padding: 24px 32px;
  overflow-y: auto;
  max-height: calc(100vh - var(--sm-header-height));
}

.vg-select-header {
  max-width: 640px;   /* keep title and intro text at a comfortable reading width */
  margin-bottom: 8px;
}

.vg-select-title {
  font-size: 1.5rem;
  font-weight: 600;
  color: var(--sm-text);
  margin: 0 0 8px;
}

.vg-select-intro {
  color: var(--sm-text-muted);
  font-size: 0.95rem;
  line-height: 1.5;
  margin: 0 0 24px;
}

.vg-strand-heading {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--sm-text);
  margin: 24px 0 12px;
}

.vg-card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
}

.vg-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  padding: 16px;
  background: var(--sm-surface);
  border: 1px solid var(--sm-border);
  border-radius: 8px;
  cursor: pointer;
  text-align: left;
  transition: border-color var(--sm-transition-fast);
  font-family: var(--sm-font);
  min-height: 44px;
}

.vg-card:hover,
.vg-card:focus-visible {
  border-color: var(--sm-indigo);
  outline: none;
}

.vg-card-title {
  font-weight: 600;
  font-size: 1rem;
  color: var(--sm-text);
  margin-bottom: 4px;
  display: block;
}

.vg-card-desc {
  font-size: 0.85rem;
  color: var(--sm-text-muted);
  line-height: 1.4;
  display: block;
}

/* ── Grid View — Layout with Sidebar ──────────────────────────────────────── */

.vg-grid-view {
  display: flex;
  flex-direction: row;
  height: calc(100vh - var(--sm-header-height));
  overflow: hidden;
}

/* ── Sidebar ──────────────────────────────────────────────────────────────── */

.vg-sidebar {
  width: var(--vg-sidebar-width);
  min-width: var(--vg-sidebar-width);
  border-right: 1px solid var(--sm-border);
  background: var(--sm-surface);
  padding: 12px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  font-size: 0.8rem;
}

.vg-sb-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.vg-sb-label {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--sm-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.vg-sb-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.vg-sb-row label {
  font-size: 0.8rem;
  color: var(--sm-text);
  min-width: 55px;
}

/* Pair gap for companion-style grids */
.vg-cell.vg-pair-gap {
  border-right-width: 6px;
  border-right-color: var(--vg-cell-border);
}

.vg-sb-spinner {
  width: 52px;
  height: 30px;
  border: 1px solid var(--sm-border);
  border-radius: 4px;
  background: var(--sm-bg);
  color: var(--sm-text);
  font-family: var(--sm-font);
  font-size: 0.85rem;
  text-align: center;
  padding: 0 2px;
}

.vg-sb-spinner:focus {
  outline: 2px solid var(--sm-indigo);
  outline-offset: -1px;
}

.vg-sb-divider {
  height: 1px;
  background: var(--sm-border);
  margin: 4px 0;
}

.vg-sb-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 5px 8px;
  border: 1px solid var(--sm-border);
  border-radius: 5px;
  background: var(--sm-surface);
  color: var(--sm-text);
  font-family: var(--sm-font);
  font-size: 0.78rem;
  cursor: pointer;
  min-height: 32px;
  width: 100%;
  text-align: center;
  transition: background var(--sm-transition-fast);
}

.vg-sb-btn:hover {
  background: var(--sm-indigo-light);
}

.vg-sb-btn.vg-active {
  background: var(--sm-indigo);
  color: #fff;
  border-color: var(--sm-indigo);
}

.vg-sb-sign-row {
  justify-content: space-between;
}

.vg-sb-sign-label {
  font-size: 0.8rem;
  color: var(--sm-text);
  flex: 1;
  min-width: 0;
}

.vg-sb-sign-btn {
  width: auto;
  min-width: 44px;
  flex: 0 0 auto;
  font-weight: 600;
  font-size: 1rem;
}

/* Radio group for Toggle / Highlight */
.vg-sb-radio {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.vg-sb-radio-item {
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  font-size: 0.8rem;
  color: var(--sm-text);
  padding: 2px 0;
}

.vg-sb-radio-item input[type="radio"] {
  margin: 0;
  accent-color: var(--sm-indigo);
}

/* Swatches + colour picker row */
.vg-sb-swatches {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 5px;
  margin-top: 2px;
}

.vg-sb-swatch {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
  flex-shrink: 0;
  transition: border-color var(--sm-transition-fast), transform var(--sm-transition-fast);
}

.vg-sb-swatch:hover {
  transform: scale(1.15);
}

.vg-sb-swatch.vg-swatch-active {
  border-color: var(--sm-text);
}

.vg-sb-colour-pick {
  width: 28px;
  height: 28px;
  border: 1px solid var(--sm-border);
  border-radius: 4px;
  padding: 1px;
  cursor: pointer;
  background: none;
}

.vg-sb-colour-pick::-webkit-color-swatch-wrapper {
  padding: 0;
}

.vg-sb-colour-pick::-webkit-color-swatch {
  border: none;
  border-radius: 3px;
}

/* Checkbox */
.vg-sb-check {
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  font-size: 0.8rem;
  color: var(--sm-text);
  padding: 2px 0;
}

.vg-sb-check input[type="checkbox"] {
  margin: 0;
  accent-color: var(--sm-indigo);
}

/* ── Main Grid Area ───────────────────────────────────────────────────────── */

.vg-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px;
  overflow: hidden;
  min-width: 0;
}

.vg-grid-container {
  display: grid;
  grid-template-areas:
    ".    up    ."
    "left grid  right"
    ".    down  .";
  grid-template-columns: 40px 1fr 40px;
  grid-template-rows: 40px 1fr 40px;
  max-width: 100%;
  max-height: calc(100vh - var(--sm-header-height) - 24px);
  flex: 1;
  width: 100%;
}

/* ── Arrow Buttons ────────────────────────────────────────────────────────── */

.vg-arrow {
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  color: var(--sm-text-muted);
  font-size: 1.3rem;
  cursor: pointer;
  min-width: 40px;
  min-height: 40px;
  transition: color var(--sm-transition-fast);
  padding: 0;
}

.vg-arrow:hover {
  color: var(--sm-indigo);
}

.vg-arrow-up    { grid-area: up;    justify-self: center; position: relative; z-index: 1; }
.vg-arrow-down  { grid-area: down;  justify-self: center; }
.vg-arrow-left  { grid-area: left;  align-self: center; }
.vg-arrow-right { grid-area: right; align-self: center; }

/* Grid title — spans full top row, sits left of the centred up arrow */
.vg-grid-title {
  grid-row: 1;
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  padding-left: 8px;
  padding-right: 50%;  /* keep text left of centre so it clears the arrow */
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--sm-text-muted);
  pointer-events: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Grid ─────────────────────────────────────────────────────────────────── */

.vg-grid {
  grid-area: grid;
  display: grid;
  gap: 0;
  width: 100%;
  height: 100%;
  border: 2px solid var(--vg-cell-border);
}

/* ── Cells ────────────────────────────────────────────────────────────────── */

.vg-cell {
  display: flex;
  flex-direction: column;
  border-right: 2px solid var(--vg-cell-border);
  border-bottom: 2px solid var(--vg-cell-border);
  overflow: hidden;
  min-width: 0;
  min-height: 0;
}

/* Remove right border on last column, bottom border on last row */
.vg-cell:nth-child(n) {
  /* borders set above; selectively removed via JS class */
}

.vg-cell-invalid {
  background: var(--sm-bg);
  border-color: transparent;
}

/* ── Cell Halves ──────────────────────────────────────────────────────────── */

.vg-blue,
.vg-yellow {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 4px 6px;
  font-size: 1.4rem;
  font-weight: 600;
  text-align: center;
  word-break: break-word;
  min-height: 36px;
  user-select: none;
  -webkit-user-select: none;
  transition: background var(--sm-transition-fast);
  background: var(--sm-surface);
  color: var(--sm-text);
}

/* Divider between blue and yellow halves */
.vg-blue {
  border-bottom: 1px solid var(--vg-cell-divider);
}

/* Hidden state — default (uncoloured): just hide the text */
.vg-blue:not(.vg-shown),
.vg-yellow:not(.vg-shown) {
  color: transparent;
}

/* ── Coloured Mode ────────────────────────────────────────────────────────── */

.vg-coloured .vg-blue {
  background: var(--vg-blue-hidden);
  color: var(--vg-blue-hidden);
}

.vg-coloured .vg-blue.vg-shown {
  background: var(--vg-blue-bg);
  color: var(--vg-blue-text);
}

.vg-coloured .vg-yellow {
  background: var(--vg-yellow-hidden);
  color: var(--vg-yellow-hidden);
}

.vg-coloured .vg-yellow.vg-shown {
  background: var(--vg-yellow-bg);
  color: var(--vg-yellow-text);
}

/* ── Undefined cells (coloured mode) ─────────────────────────────────────── */

.vg-coloured .vg-cell-undefined .vg-blue {
  background: var(--vg-undefined-hidden);
  color: var(--vg-undefined-hidden);
}

.vg-coloured .vg-cell-undefined .vg-blue.vg-shown {
  background: var(--vg-undefined-bg);
  color: var(--vg-undefined-text);
}

.vg-coloured .vg-cell-undefined .vg-yellow {
  background: var(--vg-undefined-hidden);
  color: var(--vg-undefined-hidden);
}

.vg-coloured .vg-cell-undefined .vg-yellow.vg-shown {
  background: var(--vg-undefined-bg);
  color: var(--vg-undefined-text);
}

/* ── Shown state (uncoloured mode) ────────────────────────────────────────── */

.vg-blue.vg-shown,
.vg-yellow.vg-shown {
  color: var(--sm-text);
}

/* Coloured overrides come from .vg-coloured above (higher specificity via parent) */

/* ── Cell highlight (per-cell, applied via inline style for colour) ────────── */

.vg-highlighted {
  /* highlight colour applied via inline background-color */
}

/* ── Hover on cells ───────────────────────────────────────────────────────── */

.vg-blue:hover,
.vg-yellow:hover {
  opacity: 0.85;
}

/* ── Labels ───────────────────────────────────────────────────────────────── */

.vg-label {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.65rem;
  font-weight: 600;
  color: var(--sm-text-muted);
  padding: 2px 4px;
  user-select: none;
  -webkit-user-select: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.vg-label-col {
  border-bottom: 1px solid var(--vg-cell-divider);
}

.vg-label-row {
  border-right: 1px solid var(--vg-cell-divider);
}

/* ── Mobile sidebar toggle ────────────────────────────────────────────────── */

.vg-sidebar-toggle {
  display: none;
  position: fixed;
  bottom: 60px;
  left: 8px;
  width: 40px;
  height: 40px;
  border-radius: 8px;
  border: 1px solid var(--sm-border);
  background: var(--sm-surface);
  color: var(--sm-text);
  font-size: 1.2rem;
  cursor: pointer;
  z-index: 50;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  align-items: center;
  justify-content: center;
}

/* ── Mobile adjustments ───────────────────────────────────────────────────── */

@media (max-width: 700px) {
  .vg-select {
    padding: 16px;
  }

  .vg-select-header {
    max-width: none;  /* full width on mobile */
  }

  .vg-sidebar {
    position: fixed;
    top: var(--sm-header-height);
    left: 0;
    bottom: 0;
    z-index: 40;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 2px 0 12px rgba(0,0,0,0.15);
    width: 180px;
    min-width: 180px;
  }

  .vg-sidebar.vg-sidebar-open {
    transform: translateX(0);
  }

  .vg-sidebar-toggle {
    display: flex;
  }

  .vg-grid-view {
    /* On mobile, main area takes full width */
  }

  .vg-blue,
  .vg-yellow {
    font-size: 1rem;
    min-height: 28px;
    padding: 2px 3px;
  }

  .vg-grid-container {
    grid-template-columns: 32px 1fr 32px;
    grid-template-rows: 32px 1fr 32px;
  }

  .vg-card {
    min-height: 56px;
  }
}

@media (max-width: 500px) {
  .vg-blue,
  .vg-yellow {
    font-size: 0.85rem;
    min-height: 24px;
    padding: 2px 2px;
  }

  .vg-label {
    font-size: 0.55rem;
    padding: 1px 2px;
  }
}

/* ── Custom Picker Modal (Evaluating Expressions) ─────────────────────────── */

.vg-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--sm-toolbar-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.vg-modal-card {
  background: var(--sm-surface);
  border-radius: 12px;
  padding: 20px 24px;
  max-width: 400px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 8px 32px color-mix(in srgb, var(--sm-text) 18%, transparent);
}

.vg-modal-title {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--sm-text);
  margin: 0 0 12px;
}

.vg-modal-actions {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.vg-modal-actions .vg-sb-btn {
  flex: 1;
  min-height: 28px;
  font-size: 0.75rem;
}

.vg-modal-family {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--sm-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: 12px 0 6px;
}

.vg-modal-check {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
  cursor: pointer;
  font-size: 0.9rem;
  color: var(--sm-text);
}

.vg-modal-check input[type="checkbox"] {
  margin: 0;
  accent-color: var(--sm-indigo);
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

.vg-modal-expr-name {
  flex: 1;
}

.vg-modal-done {
  margin-top: 16px;
  font-size: 0.9rem;
  padding: 8px 16px;
}
