:root {
  --background: #f4f6f9;
  --surface: #ffffff;
  --border: #d5dbe4;
  --text: #1b2430;
  --muted: #5b6675;
  --accent: #1f5fbf;
  --accent-text: #ffffff;
  --success: #1a7f4b;
  --success-soft: #e6f5ec;
  --danger: #c0362c;
  --danger-soft: #fdecea;
  --panel: #f0f4fa;
  --network-bits: #1f5fbf;
  --host-bits: #b45309;
  --mono: ui-monospace, "Cascadia Mono", "SF Mono", Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --background: #12161c;
    --surface: #1b212a;
    --border: #333d4b;
    --text: #e6eaf0;
    --muted: #9aa6b5;
    --accent: #6ea8ff;
    --accent-text: #0d1420;
    --success: #4cc38a;
    --success-soft: #16301f;
    --danger: #ff7b72;
    --danger-soft: #3a1c1a;
    --panel: #222a35;
    --network-bits: #6ea8ff;
    --host-bits: #f0a94b;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 2rem 1rem 4rem;
  background: var(--background);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.55;
}

main {
  max-width: 46rem;
  margin: 0 auto;
}

h1 {
  margin: 0 0 0.25rem;
  font-size: 1.7rem;
  line-height: 1.2;
  text-wrap: balance;
}

.intro {
  margin: 0 0 1.5rem;
  color: var(--muted);
}

.back-link {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
  padding: 0.5rem 1rem;
  border: 1px solid var(--border);
  border-radius: 0.4rem;
  background: var(--surface);
  color: var(--accent);
  font-weight: 600;
  text-decoration: none;
}

.back-link svg {
  flex: none;
  width: 1.15rem;
  height: 1.15rem;
}

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

.back-link:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.statement,
.question {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  padding: 1.25rem;
  margin-bottom: 1rem;
}

.statement {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  border-left: 0.35rem solid var(--accent);
}

.statement-label {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.statement-address {
  margin: 0;
  font-family: var(--mono);
  font-size: clamp(1.5rem, 6vw, 2.2rem);
  font-weight: 600;
  letter-spacing: 0.02em;
}

.question h2 {
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
  margin: 0 0 1rem;
  font-size: 1.1rem;
  line-height: 1.3;
}

.question-number {
  flex: none;
  width: 1.7rem;
  height: 1.7rem;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 0.9rem;
  line-height: 1.7rem;
  text-align: center;
}

.question.is-correct {
  border-color: var(--success);
}

.question.is-correct .question-number {
  background: var(--success);
}

.field-group {
  margin-bottom: 0.9rem;
}

.group-label {
  display: block;
  margin-bottom: 0.3rem;
  color: var(--muted);
  font-size: 0.9rem;
}

.fields {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.3rem;
}

.dot {
  font-family: var(--mono);
  font-weight: 700;
}

input {
  padding: 0.45rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 0.4rem;
  background: var(--background);
  color: var(--text);
  font-family: var(--mono);
  font-size: 1rem;
  text-align: center;
}

input.binary { width: calc(8ch + 1.2rem); }
input.decimal { width: calc(3ch + 1.4rem); }
input.count { width: calc(6ch + 1.4rem); }

input:focus-visible,
button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

input.is-wrong {
  border-color: var(--danger);
  background: var(--danger-soft);
}

input:read-only {
  border-color: var(--success);
  background: var(--success-soft);
}

.actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 1rem;
  margin-top: 1rem;
}

button {
  padding: 0.5rem 1rem;
  border: 1px solid var(--border);
  border-radius: 0.4rem;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}

button:hover:not(:disabled) {
  border-color: var(--accent);
}

button.primary {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}

button:disabled {
  opacity: 0.5;
  cursor: default;
}

.feedback {
  margin: 0;
  font-weight: 600;
}

.feedback.is-correct { color: var(--success); }
.feedback.is-wrong { color: var(--danger); }

.help {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px dashed var(--border);
}

.help-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.panel {
  margin-top: 0.75rem;
  padding: 0.9rem 1rem;
  border-radius: 0.5rem;
  background: var(--panel);
}

.panel h3 {
  margin: 0 0 0.4rem;
  font-size: 1rem;
}

.panel p,
.panel ol,
.panel ul {
  margin: 0.4rem 0;
}

.panel ol,
.panel ul {
  padding-left: 1.3rem;
}

.panel code,
.final-answer strong {
  font-family: var(--mono);
}

.final-answer {
  padding-top: 0.5rem;
  border-top: 1px solid var(--border);
}

.binary-scroll {
  overflow-x: auto;
  margin: 0.5rem 0;
}

.binary-table {
  border-collapse: collapse;
  font-family: var(--mono);
  font-size: 0.9rem;
  white-space: nowrap;
}

.binary-table th,
.binary-table td {
  padding: 0.15rem 0.9rem 0.15rem 0;
  text-align: left;
  font-weight: 400;
}

.binary-table th {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--muted);
}

.binary-table .result-row th,
.binary-table .result-row td {
  border-top: 1px solid var(--text);
  font-weight: 700;
}

.weight-table {
  border-collapse: collapse;
  font-family: var(--mono);
  font-size: 0.9rem;
}

.weight-table th,
.weight-table td {
  min-width: 3.2rem;
  padding: 0.3rem 0.6rem;
  border: 1px solid var(--muted);
  text-align: center;
  font-weight: 400;
}

.weight-table th {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--muted);
}

.weight-table td.is-set {
  background: var(--surface);
  color: var(--network-bits);
  font-weight: 700;
}

.bits-network { color: var(--network-bits); }

.bits-host {
  color: var(--host-bits);
  /* text-decoration: underline; */
  text-underline-offset: 0.2em;
}

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

[hidden] { display: none !important; }

.exercise-list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.exercise-list li {
  margin-bottom: 1rem;
}

.exercise-link {
  display: block;
  padding: 1.25rem;
  border: 1px solid var(--border);
  border-left: 0.35rem solid var(--accent);
  border-radius: 0.75rem;
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
}

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

.exercise-link:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.exercise-title {
  display: block;
  color: var(--accent);
  font-size: 1.1rem;
  font-weight: 600;
  line-height: 1.3;
}

.exercise-description {
  display: block;
  margin-top: 0.3rem;
  color: var(--muted);
  font-size: 0.95rem;
}

.layer-list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.layer {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: start;
  gap: 0.6rem 0.9rem;
  padding: 0.9rem 0;
  border-top: 1px solid var(--border);
}

.layer:first-child {
  padding-top: 0;
  border-top: 0;
}

input.layer-name {
  width: 100%;
  max-width: 16rem;
  font-family: inherit;
  text-align: left;
}

.layer-description {
  margin: 0.35rem 0 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.layer-icons {
  display: flex;
  gap: 0.5rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.layer-icons li {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
  width: 5.5rem;
  color: var(--muted);
  font-size: 0.75rem;
  line-height: 1.2;
  text-align: center;
}

.layer-icons svg {
  width: 2rem;
  height: 2rem;
  color: var(--accent);
}

@media (max-width: 34rem) {
  .layer {
    grid-template-columns: auto minmax(0, 1fr);
  }

  .layer-icons {
    grid-column: 2;
  }
}
