:root {
  --page-background: #f4f5f2;
  --panel-background: #ffffff;
  --text: #1f2933;
  --muted: #5f6b76;
  --border: #cfd6dc;
  --accent: #31688e;
  --accent-dark: #244d6a;
  --accent-soft: #e8f1f6;
  --source-badge: #fde725;
  --process-background: #eef4f1;
  --danger: #9b2c2c;
  --shadow: 0 8px 24px rgba(31, 41, 51, 0.08);
  --radius: 16px;
}

* {
  box-sizing: border-box;
}

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

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  width: min(1120px, calc(100% - 32px));
  margin: 0 auto;
  padding: 48px 0 72px;
}

.page-header {
  max-width: 1120px;
  margin-bottom: 32px;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 750;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

h1,
h2 {
  margin-top: 0;
  line-height: 1.15;
}

h1 {
  margin-bottom: 12px;
  font-size: clamp(2rem, 5vw, 3.4rem);
}

h2 {
  margin-bottom: 18px;
  font-size: 1.45rem;
}

.intro,
.field-help,
.form-message,
.empty-state {
  color: var(--muted);
}

.intro {
  margin: 0;
  font-size: 1.05rem;
}

.builder,
.history-panel,
.legend {
  margin-top: 24px;
  padding: 28px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel-background);
  box-shadow: var(--shadow);
}

.builder {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 24px;
}

.builder > h2,
.builder > .button-row,
.builder > .form-message {
  grid-column: 1 / -1;
}

.control-group {
  margin: 0 0 24px;
  padding: 0;
  border: 0;
}

.control-group legend {
  margin-bottom: 4px;
  font-weight: 750;
}

.field-help {
  margin: 0 0 12px;
  font-size: 0.92rem;
}

.option-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}

.option-card {
  position: relative;
  display: flex;
  min-height: 96px;
  align-items: center;
  gap: 12px;
  padding: 14px;
  border: 2px solid var(--border);
  border-radius: 14px;
  background: #fff;
  transition:
    border-color 140ms ease,
    background-color 140ms ease,
    transform 140ms ease;
}

.option-card:hover {
  transform: translateY(-1px);
  border-color: var(--accent);
}

.option-card:has(input:checked) {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.option-card input {
  position: absolute;
  inset: 10px auto auto 10px;
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
}

.option-icon {
  width: 48px;
  height: 48px;
  flex: 0 0 48px;
  object-fit: contain;
  margin-left: 20px;
}

.option-label {
  padding-left: 4px;
  font-weight: 650;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 8px;
}

.primary-button,
.secondary-button,
.text-button,
.remove-button {
  border-radius: 10px;
  font-weight: 700;
}

.primary-button,
.secondary-button {
  padding: 11px 16px;
  border: 1px solid transparent;
}

.primary-button {
  background: var(--accent);
  color: #fff;
}

.primary-button:hover {
  background: var(--accent-dark);
}

.secondary-button {
  border-color: var(--border);
  background: #fff;
  color: var(--text);
}

.text-button,
.remove-button {
  padding: 8px 10px;
  border: 0;
  background: transparent;
}

.text-button {
  color: var(--accent);
}

.remove-button {
  color: var(--danger);
}

.export-buttons {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.form-message {
  min-height: 1.5em;
  margin: 12px 0 0;
}

.section-heading-row {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 16px;
}

.history-output {
  display: grid;
  gap: 18px;
}

.visual-sentence {
  display: grid;
  grid-template-columns: minmax(118px, 150px) auto minmax(160px, 1fr) auto;
  align-items: center;
  gap: 18px;
  padding: 18px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: #fff;
}

.component-token {
  position: relative;
  display: grid;
  min-height: 124px;
  place-items: center;
  padding: 14px;
  border: 2px solid var(--accent);
  border-radius: 22px;
  background: var(--accent-soft);
}

.component-token > img {
  width: 72px;
  height: 72px;
  object-fit: contain;
}

.component-name {
  margin-top: 8px;
  text-align: center;
  font-size: 0.85rem;
  font-weight: 750;
}

.source-badges {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.source-badge {
  position: absolute;
  display: grid;
  width: 34px;
  height: 34px;
  place-items: center;
  border: 2px solid #fff;
  border-radius: 50%;
  background: var(--source-badge);
  box-shadow: 0 2px 8px rgba(31, 41, 51, 0.18);
}

.source-badge img {
  width: 22px;
  height: 22px;
  object-fit: contain;
}

.source-badge.position-0 {
  top: -10px;
  left: -10px;
}

.source-badge.position-1 {
  top: -10px;
  right: -10px;
}

.source-badge.position-2 {
  bottom: -10px;
  left: -10px;
}

.source-badge.position-3 {
  right: -10px;
  bottom: -10px;
}

.operator {
  color: var(--muted);
  font-size: 2rem;
  font-weight: 500;
}

.process-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.process-token {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--process-background);
  font-weight: 700;
}

.process-token img {
  width: 28px;
  height: 28px;
  object-fit: contain;
}

.legend-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 18px;
}

.legend-group h3 {
  margin: 0 0 10px;
  font-size: 1rem;
}

.legend-items {
  display: grid;
  gap: 8px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 10px;
}

.legend-item img {
  width: 28px;
  height: 28px;
  object-fit: contain;
}

.empty-state {
  margin: 0;
  padding: 28px;
  border: 2px dashed var(--border);
  border-radius: 14px;
  text-align: center;
}

.lesson-title-group {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 8px;
}

.lesson-title-group label {
  font-weight: 700;
}

.lesson-title-group input {
  padding: 10px 12px;
  border: 2px solid var(--border);
  border-radius: 10px;
  font: inherit;
}

.lesson-title-group input:focus {
  outline: none;
  border-color: var(--accent);
}

.sentence-note-group {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.sentence-note-group label {
  font-weight: 700;
}

.optional-label {
  color: var(--muted);
  font-weight: 400;
}

.sentence-note-group input {
  width: 100%;
  padding: 10px 12px;
  border: 2px solid var(--border);
  border-radius: 10px;
  font: inherit;
}

.sentence-note-group input:focus {
  outline: none;
  border-color: var(--accent);
}

.sentence-content {
  display: grid;
  gap: 10px;
}

.sentence-note {
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.4;
}

.matrix-panel {
  margin-top: 24px;
  padding: 28px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel-background);
  box-shadow: var(--shadow);
}

.matrix-intro {
  margin: -6px 0 22px;
  color: var(--muted);
}

.design-matrix {
  width: 100%;
}

.matrix-header,
.matrix-row {
  display: grid;
  grid-template-columns:
    minmax(170px, 1.2fr)
    minmax(190px, 1fr)
    34px
    minmax(190px, 1fr)
    minmax(210px, 1.4fr);
  gap: 14px;
  align-items: center;
}

.matrix-header {
  padding: 0 14px 10px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 750;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.matrix-operator-heading {
  text-align: center;
}

.matrix-rows {
  display: grid;
  gap: 10px;
}

.matrix-row {
  min-height: 92px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: #fff;
}

.matrix-component-cell {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  font-weight: 700;
}

.matrix-component-cell img {
  width: 48px;
  height: 48px;
  flex: 0 0 48px;
  object-fit: contain;
}

.matrix-component-cell span {
  min-width: 0;
}

.matrix-icon-group {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 7px;
}

.matrix-icon-button {
  display: grid;
  min-width: 0;
  min-height: 52px;
  place-items: center;
  padding: 7px;
  border: 2px solid var(--border);
  border-radius: 10px;
  background: #fff;
}

.matrix-icon-button img {
  width: 30px;
  height: 30px;
  object-fit: contain;
}

.matrix-operator {
  color: var(--muted);
  text-align: center;
  font-size: 1.8rem;
}

.matrix-note-input {
  width: 100%;
  min-width: 0;
  padding: 10px 11px;
  border: 2px solid var(--border);
  border-radius: 10px;
  background: #fff;
  color: var(--text);
  font: inherit;
}

.matrix-icon-button:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.matrix-icon-button:focus-visible {
  outline: 3px solid rgba(49, 104, 142, 0.28);
  outline-offset: 2px;
}

.matrix-icon-button.is-selected,
.matrix-icon-button[aria-pressed="true"] {
  border-color: var(--accent);
  background: var(--accent-soft);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.matrix-icon-button.is-selected img,
.matrix-icon-button[aria-pressed="true"] img {
  transform: scale(1.05);
}

.matrix-note-input:focus {
  outline: none;
  border-color: var(--accent);
}

@media (max-width: 760px) {
  .app-shell {
    width: min(100% - 20px, 1120px);
    padding-top: 24px;
  }

  .builder,
  .history-panel,
  .legend {
    padding: 20px;
  }

  .builder {
    grid-template-columns: 1fr;
  }

  .builder > h2,
  .builder > .button-row,
  .builder > .form-message {
    grid-column: auto;
  }

  .visual-sentence {
    grid-template-columns: 1fr;
    justify-items: center;
    text-align: center;
  }

  .operator {
    transform: rotate(90deg);
  }

  .section-heading-row {
    align-items: flex-start;
    flex-direction: column;
  }
  
	.export-buttons {
	  justify-content: flex-start;
	}

	.matrix-header {
	  display: none;
	}

	.matrix-row {
	  grid-template-columns: 1fr;
	  gap: 12px;
	}

	.matrix-component-cell {
	  padding-bottom: 6px;
	  border-bottom: 1px solid var(--border);
	}

	.matrix-operator {
	  transform: rotate(90deg);
	}

	.matrix-icon-group {
	  grid-template-columns: repeat(4, minmax(48px, 1fr));
	}
  
}
